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 "" "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==