Parser Combinators

For parsing in Haskell it is quite common to use a family of libraries known as Parser Combinators which let us write code to generate parsers which itself looks very similar to the BNF ( Backus–Naur Form ) of the parser grammar itself!

Structurally a parser combinator is a collection of higher-order functions which composes with other parsing functions as input and returns a new parser as its output. Our lexer will consist of functions which operate directly on matching string inputs and are composed with a variety of common combinators yielding the full parser. The Parsec library exposes a collection of combinators:

Combinators
<|>The choice operator tries to parse the first argument before proceeding to the second. Can be chained sequentially to generate a sequence of options.
manyConsumes an arbitrary number of patterns matching the given pattern and returns them as a list.
many1Like many but requires at least one match.
optionalOptionally parses a given pattern returning its value as a Maybe.
tryBacktracking operator will let us parse ambiguous matching expressions and restart with a different pattern.