Parsec (parser)

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

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

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

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

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,  parses a digit, and   parses a specific string (like  ).

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  function that calls one of these utility functions (  in this case) to run the parser: We define a   type to make the type signature of   easier to read. If we wanted to alter this program, say to read either the string  or the string , we could use the operator  , provided by the   typeclass, to combine two parsers into a single parser that tries either: