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. |
many | Consumes an arbitrary number of patterns matching the given pattern and returns them as a list. |
many1 | Like many but requires at least one match. |
optional | Optionally parses a given pattern returning its value as a Maybe. |
try | Backtracking operator will let us parse ambiguous matching expressions and restart with a different pattern. |
当前内容版权归 Stephen Diehl 或其关联方所有,如需对内容或内容相关联开源项目进行关注与资助,请访问 Stephen Diehl .