This paper is a tutorial on defining recursive descent parsers
in Haskell. In the spirit
of one-stop shopping, the paper combines material from three areas
into a single
source. The three areas are functional parsers (Burge, 1975; Wadler, 1985;
Hutton,
1992; Fokker, 1995), the use of monads to structure functional programs
(Wadler,
1990, 1992a, 1992b), and the use of special syntax for monadic programs
in Haskell
(Jones, 1995; Peterson et al., 1996). More specifically, the paper
shows how to define
monadic parsers using do notation in Haskell.
Of course, recursive descent parsers defined by hand lack the efficiency
of bottom-up
parsers generated by machine (Aho et al., 1986; Mogensen, 1993;
Gill and
Marlow, 1995). However, for many research applications, a simple recursive
descent
parser is perfectly sufficient. Moreover, while parser generators typically
offer a
fixed set of combinators for describing grammars, the method described
here is
completely extensible: parsers are first-class values, and we have the
full power of
Haskell available to define new combinators for special applications. The
method is
also an excellent illustration of the elegance of functional programming.