Parsec (parser)

ParsecOriginal author(s)| Daan Leijen, Paolo Martini, Antoine Latter

|

Developer(s)| Herbert Valerio Riedel, Derek Elkins, Antoine Latter, Roman Cheplyaka, Ryan Scott
Initial release| November 2, 2006; 18 years ago (2006-11-02)[1]
Stable release|

3.1.17.0 / April 5, 2024; 7 months ago (2024-04-05)[2]

Repository| github.com/haskell/parsec
Written in| Haskell
Operating system| Linux, macOS, Windows
Platform| Haskell Platform
Available in| English
Type| Parser combinator, library
License| BSD-2-clause
Website| hackage.haskell.org/package/parsec

Parsec is a library for writing parsers written in the programming language 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] F#,[9][10] and the imperative programming languages C#,[11] and Java.[12]

Because a parser combinator-based program is generally slower than a parser generator-based program,[citation needed] Parsec is normally used for small domain-specific languages, while Happy is used for compilers such as the Glasgow Haskell Compiler (GHC).[13]

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

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

## Example

[edit]

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

[edit]

1. ^ "parsec 2.0". Hackage. Retrieved 3 September 2019.
2. ^ "Releases". Github. Retrieved 22 September 2024.
3. ^ "Parsec on Haskell wiki". Haskell Wiki. Retrieved 29 May 2017.
4. ^ Leijen, Daan; Meijer, Erik (July 2001). "Parsec: Direct Style Monadic Parser Combinators For The Real World" (PDF). Microsoft Research. Retrieved 22 November 2014.
5. ^ "Parsec Erlang". BitBucket. Retrieved 23 November 2014.
6. ^ "Nimble Parsec". Github. Retrieved 18 December 2018.
7. ^ "Parsec OCaml" (PDF). The OCaml Summer Project. Retrieved 23 November 2014.
8. ^ "Megaparsack: Practical Parser Combinators".
9. ^ "XParsec by corsis". XParsec. Retrieved 29 May 2017.
10. ^ "FParsec". Quanttec. Retrieved 29 May 2017.
11. ^ "CSharp monad". Github. Retrieved 10 December 2014.
12. ^ "JParsec". Github. Retrieved 14 October 2016.
13. ^ "The Glasgow Haskell Compiler (AOSA Vol. 2)". The Architecture of Open Source Applications. Retrieved 23 November 2014.
14. ^ "megaparsec: Monadic parser combinators". Hackage. Retrieved 2018-09-10.
15. ^ "attoparsec: Fast combinator parsing for bytestrings and text". Hackage. Retrieved 2018-09-10.
16. ^ "Parsec". 25 October 2021.

## External links

[edit]

* Free software portal

* Parsec on the Haskell wiki
* Parsec on Hackage
* parsec on GitHub

* v
* t
* e

Haskell programming

Software|

| Implementations
(features)|

* Generic Haskell°
* Glasgow Haskell Compiler°
* Template Haskell°
* Gofer° → Hugs°
* York Haskell Compiler° (Yhc)

Dialects|

* Agda°
* Concurrent Haskell°
* Cryptol°
* Curry°
* Elm°
* Hume°
* Idris°
* Ωmega°
* Orwell°
* Pugs°
* TidalCycles°
* Ur°

Electronic design|

* Atom°
* Bluespec SystemVerilog (BSV)

Libraries|

* Haskell Platform°
* Parsec°
* QuickCheck°

Package managers|

* Cabal°
* Stack°

Windowing systems|

* Fudgets
* wxHaskell°
* xmonad°

Web frameworks|

* Servant°
* Snap°
* Yesod°

* Cardano°
* Darcs°
* Ganeti°
* git-annex°
* Haddock°
* HaXml°
* House°
* Liquid Haskell°
* LOLITA
* Pandoc°
* Paradox°
* SQream DB

Book|

* Real World Haskell

Community|

| Eponym|

* Haskell Curry

|

* Arvind
* Lennart Augustsson
* Richard Bird
* Jeremy Gibbons
* Andrew D. Gordon
* Paul Hudak
* John Hughes
* John Launchbury
* John MacFarlane
* Simon Marlow
* Conor McBride
* Erik Meijer
* Simon Peyton Jones
* David Roundy
* Joe Stoy
* Audrey Tang
* Simon Thompson
* Philip Wadler

Italics = discontinued
* ° = Open-source software
Book Category:Family Category:Software

This free and open-source software article is a stub. You can help Wikipedia by expanding it.

|

* v
* t
* e

*[v]: View this template
*[t]: Discuss this template
*[e]: Edit this template