Reference. Parsing with derivatives: A functional pearl
We present a functional approach to parsing unrestricted context-free grammars based on Brzozowski’s derivative of regular expressions. If we consider context-free grammars as recursive regular expressions, Brzozowski’s equational theory extends without modification to context-free grammars (and it generalizes to parser combinators). The supporting actors in this story are three concepts familiar to functional programmers - laziness, memoization and fixed points; these allow Brzozowski’s original equations to be transliterated into purely functional code in about 30 lines spread over three functions.
Yet, this almost impossibly brief implementation has a drawback: its performance is sour - in both theory and practice. The culprit? Each derivative can double the size of a grammar, and with it, the cost of the next derivative.
Fortunately, much of the new structure inflicted by the derivative is either dead on arrival, or it dies after the very next derivative. To eliminate it, we once again exploit laziness and memoization to transliterate an equational theory that prunes such debris into working code. Thanks to this compaction, parsing times become reasonable in practice.
We equip the functional programmer with two equational theories that, when combined, make for an abbreviated understanding and implementation of a system for parsing context-free languages.
Cite
Cited by (2)
CoStar: A verified ALL(*) parser lasserCoStarVerifiedALL2021
Zippy LL(1) parsing with derivatives EdelmannZippy2020
Cites 19 works (5 here)
With notes (5)
Total parser combinators danielssonTotalParserCombinators2010
A monadic parser combinator library which guarantees termination of parsing, while still allowing many forms of left recursion, is described. The library’s interface is similar to those of many other parser combinator libraries, with two important differences: one is that the interface clearly specifies which parts of the constructed parsers may be infinite, and which parts have to be finite, using dependent types and a combination of induction and coinduction; and the other is that the parser type is unusually informative.
The library comes with a formal semantics, using which it is proved that the parser combinators are as expressive as possible. The implementation is supported by a machine-checked correctness proof.
Regular-expression derivatives re-examined owensRegularexpressionDerivativesReexamined2009
An efficient context-free parsing algorithm Earley1970
On the translation of languages from left to right KNUTH1965607
Derivatives of Regular Expressions brzozowskiDerivativesRegularExpressions1964
External (14)
- Combinator Parsing: A Short Tutorial (2009)
- Packrat parsers can support left recursion (2008)
- Grammar analysis and parsing by abstract interpretation (2006)
- Parsing as abstract interpretation of grammar semantics (2002)
- Packrat parsing: simple, powerful, lazy, linear time (2002)
- Designing and Implementing Combinator Languages (1999)
- LR parsers for natural languages (1984)
- Selected Writings on Computing: A Personal Perspective (1982)
- Top down operator precedence (1973)
- Programming languages and their compilers: Preliminary notes (1970)
- Practical translators for LR(k) languages (1969)
- Recognition and parsing of context-free languages in time n3 (1967)
- An efficient recognition and syntax-analysis algorithm for context-free languages (1965)
- Syntactic Analysis and Operator Precedence (1963)