Haskell Parsers Part 1.

I really enjoy using Haskell. It’s probably the most intellectually satisfying programming language ever! Parsing is one of its many strengths and these next few posts will be about creating simple parsers from first principles and expressing them in two different ways.

For parsing, start with a newtype definition for a Parser and a data constructor P that has a function String -> [(a, String)] which will parse the string for an ‘a’. The result of the parse is a list of tuples. The first element of a tuple being an ‘a’ and the other is whatever is left of the string after parsing an ‘a’. The convention is that if the parser fails then an empty list is returned. (In this context failure means failing to parse an ‘a’ rather than some programmatic error.)

A parse function can now be defined as

In other words the parse function takes a Parser of ‘a’ and a String and returns a list (possibly empty) of ‘a’ and whatever of the string is not consumed by the process of parsing.
This can be written in point-free style as

(point-free is a Haskell style where function arguments can often be omitted – it can give cleaner code but sometimes it can produce obfuscated code!). This leads to the first parser, ch.

ch is a Parser of Char and either fails if the input string is empty or succeeds returning the first character in the string. If the above snippets are put into Haskell source file:

and loaded into ghci it can be run interactively by calling the parse function with the ch parser and a string to parse.

If there is no character to parse then an empty list is returned.

If there’s just one character in the string then that is returned and the string is completely consumed.

Or maybe map the parser over a list of strings…

The next part in this series will look at how to parse a specified character and the parsing of numbers etc.

Leave a Reply

Your email address will not be published. Required fields are marked *

ˆ Back To Top