Software:Parsec (parser)

From HandWiki
Revision as of 12:43, 9 February 2024 by Rtextdoc (talk | contribs) (change)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Parsec
Original author(s)Daan Leijen, Paolo Martini, Antoine Latter
Developer(s)Herbert Valerio Riedel, Derek Elkins, Antoine Latter, Roman Cheplyaka, Ryan Scott
Initial releaseNovember 2, 2006; 18 years ago (2006-11-02)[1]
Stable release
3.1.14.0 / August 10, 2019; 5 years ago (2019-08-10)[2]
Repositorygithub.com/haskell/parsec
Written inHaskell
Operating systemLinux, macOS, Windows
PlatformHaskell Platform
TypeParser combinator, Library
LicenseBSD-3
Websitehackage.haskell.org/package/parsec

Parsec is a library for writing parsers in Haskell.[3] It is based on higher-order parser combinators, so a complicated parser can be made out of many smaller ones.[4] It has been reimplemented in many other languages, including Erlang,[5] Elixir,[6] OCaml,[7] Racket,[8] and F#,[9][10] as well as imperative languages such as C#,[11] and Java.[12]

Because a parser combinator-based program is generally slower than a parser generator-based program,[13] Parsec is normally used for small domain-specific languages, while Happy is used for compilers such as GHC.[14]

Other Haskell parser combinator libraries that have been derived from Parsec include Megaparsec[15] and Attoparsec.[16]

Parsec is free software released under the BSD-3-Clause license.[17]

Example

Parsers written in Parsec start with simpler parsers, such as ones that recognize certain strings, and combine them to build a parser with more complicated behavior. For example, digit parses a digit, and string parses a specific string (like "hello").

Parser combinator libraries like Parsec provide utility functions to run the parsers on real values. A parser to recognize a single digit from a string can be split into two functions: one to create the parser, and a main function that calls one of these utility functions (parse in this case) to run the parser:

import Text.Parsec      -- has general parsing utility functions
import Text.Parsec.Char -- contains specific basic combinators
type Parser = Stream s m Char => ParsecT s u m String

parser :: Parser
parser = string "hello"

main :: IO ()
main = print (parse parser "<test>" "hello world")
-- prints 'Right "hello"'

We define a Parser type to make the type signature of parser easier to read. If we wanted to alter this program, say to read either the string "hello" or the string "goodbye", we could use the operator <|>, provided by the Alternative typeclass, to combine two parsers into a single parser that tries either:

parser = string "hello" <|> string "goodbye"

References

  1. "parsec 2.0". https://hackage.haskell.org/package/parsec-2.0. Retrieved 3 September 2019. 
  2. "Releases". https://github.com/haskell/parsec/releases. Retrieved 3 September 2019. 
  3. "Parsec on Haskell wiki". https://wiki.haskell.org/Parsec. Retrieved 29 May 2017. 
  4. Leijen, Daan; Meijer, Erik (July 2001). "Parsec: Direct Style Monadic Parser Combinators For The Real World". http://research.microsoft.com/pubs/65201/parsec-paper-letter.pdf. Retrieved 22 November 2014. 
  5. "Parsec Erlang". https://bitbucket.org/dmercer/parsec-erlang/. Retrieved 23 November 2014. 
  6. "Nimble Parsec". https://github.com/plataformatec/nimble_parsec/. Retrieved 18 December 2018. 
  7. "Parsec OCaml". http://lprousnth.files.wordpress.com/2007/08/pcl.pdf. Retrieved 23 November 2014. 
  8. "Megaparsack: Practical Parser Combinators". https://docs.racket-lang.org/megaparsack/index.html. 
  9. "XParsec by corsis". http://xparsec.corsis.tech/. Retrieved 29 May 2017. 
  10. "FParsec". http://www.quanttec.com/fparsec/. Retrieved 29 May 2017. 
  11. "CSharp monad". https://github.com/louthy/csharp-monad. Retrieved 10 December 2014. 
  12. "JParsec". https://github.com/jparsec/jparsec. Retrieved 14 October 2016. 
  13. "What is the difference between parser generators and parser combinators?" (in en). https://www.quora.com/What-is-the-difference-between-parser-generators-and-parser-combinators. 
  14. "The Glasgow Haskell Compiler (AOSA Vol. 2)". http://www.aosabook.org/en/ghc.html. Retrieved 23 November 2014. 
  15. "megaparsec: Monadic parser combinators". https://hackage.haskell.org/package/megaparsec-6.5.0. 
  16. "attoparsec: Fast combinator parsing for bytestrings and text". https://hackage.haskell.org/package/attoparsec. 
  17. "Parsec". 25 October 2021. https://github.com/haskell/parsec/blob/master/LICENSE. 

External links