Packrat Parsing: Simple, Powerful, Lazy, Linear Time Bryan Ford

Massachusetts Institute of Technology

Presented at

Abstract

Full Paper

Slides from ICFP Presentation

Example Parsers

ArithRecurse.hs: Recursive descent parser described in Section 2.1, for the trivial arithmetic expression language of Figure 1.

ArithPackrat.hs: Equivalent packrat parser for the same trivial language, Section 2.4.

ArithLeft.hs: Left recursion example for Section 3.1, which extends the above packrat parser with properly left-associative subraction, division, and modulo operators.

ArithLex.hs: Integrated lexical analysis example for Section 3.2, which extends the previous packrat parser with support for multiple-digit decimal literals and optional whitespace padding between literals, operators, and punctuation.

ArithMonad.hs: Example packrat parser, equivalent to ArithPackrat.hs, but using monadic combinators to express the parsing functions more succinctly and provide support for user-friendly error detection and reporting. Discussed in Section 3.3 of the paper. The following two library modules are required:

Pos.hs: Keeps track of line and column position while scanning input text.

Parse.hs: Monadic combinator library for packrat parsers, inspired by Daan Leijen's Parsec library. (Parsec was designed for traditional predictive parsers and mostly-predictive parsers with special-case backtracking.)

Enjoy!