- Parser Combinator
In mathematics and functional programming, Higher Order functions (HOF) are defined as the functions that can take functions as their input and can also produce functions as their output. The use of a HOF as an infix operator in a function-definition is known as a ‘combinator’. When combinators are used as basic building blocks to construct a
parsing technique, then they are called parser combinators and the parsing method is called combinatory-parsing (as higher-order functions ‘combine’ different parsers together). Parser combinators use a top-down parsing strategy which facilitates modular piecewise construction and testing.Parser combinators are straightforward to construct, ‘readable’, modular, well-structured and easily maintainable. They have been used extensively in the prototyping of compilers and processors for domain-specific languages such as natural language interfaces to databases, where complex and varied semantic actions are closely integrated with syntactic processing. In 1989, Richard Frost and John Launchbury demonstrated] use of parser combinators to construct Natural Language interpreters. Graham Hutton also used higher-order functions for basic parsing in 1992. Hutton, Graham. "Higher-order functions for parsing." "Journal of Functional Programming", Volume 2 Issue 3, Pages: 323– 343, 1992. ] S.D. Swierstra also exhibited the practical aspects of parser combinators in 2001. Swierstra, S. Doaitse. "Combinator parsers: From toys to tools. " "In G. Hutton, editor, Electronic Notes in Theoretical Computer Science," volume 41. Elsevier Science Publishers, 2001. ] In 2008, Frost, Hafiz and Callaghan described Frost, R., Hafiz, R. and Callaghan, P." Parser Combinators for Ambiguous Left-Recursive Grammars." " 10th International Symposium on Practical Aspects of Declarative Languages (PADL), ACM-SIGPLAN ", Volume 4902, year 2008, Pages: 167-181, January 2008, San Francisco.] a set of parser-combinators in Haskell that solve the long standing problem of accommodating left-recursion, and work as a complete top-down parsing tool in polynomial time and space.
Basic Idea
The core idea of parser combinators (which was popularized by
Philip Wadler in 1985 Wadler, Philip. "How to replace failure by a list of successes" "P. Jouannaud (ed.) Functional Programming Languages and Computer Architectures Lecture Notes in Computer Science 201", Springer-Verlag, Heidelberg, 113, Year: 1985, Pages: 113 - 128.] ) is that the results (success or failure) of a recognizer (or a parser) can be returned as a list. Multiple entries of this list represent multiple successes; repeated entries represent ambiguous results and an empty list represents a failure.In
functional programming , parser combinators can be used to build basic parsers and to construct complex parsers for rules (that define nonterminals) from other parsers. A production-rule of acontext-free grammar (CFG) may have one or more ‘alternatives’ and each alternative may consist of a sequence of non-terminal(s) and/or terminal(s), or the alternative may consist of a single non-terminal or terminal or ‘empty’. Parser combinators allow parsers to be defined in an embedded style, in code which is similar in structure to the rules of the grammar. As such, implementations can be thought of as executable specifications with all of the associated advantages. In order to achieve this, one has to define a set of combinators or infix operators to ‘glue’ different terminals and non-terminals to form a complete rule.The Combinators
To keep the discussion relatively straight forward, we discuss parser combinators in terms of recognizer only. Assume that the input is a sequence of tokens, of length
#input
the members of which are accessed through an indexj
. Recognizers are functions which take an indexj
as argument and which return a set of indices. Each index in the result set corresponds to a position at which the parser successfully finished recognizing a sequence of tokens that began at positionj
. An empty result set indicates that the recognizer failed to recognize any sequence beginning atj
. A non-empty result set indicates the recognizer ends at different positions successfully. (Note that, as we are defining results as a set, we cannot express ambiguity as it would require repeated entries in the set. Use of ‘list’ would solve the problem.) Following the definitions of two basic recognizers for terminals, we define two major combinators for alternative and sequencing:* The
empty
recognizer is a function which always succeeds returning a singleton set containing the current position::
* A recognizerterm ’x’
for a terminalx
is a function which takes an indexj
as input, and ifj
is less than#input
and if the token at positionj
in the input corresponds to the terminalx
, it returns a singleton set containingj + 1
, otherwise it returns the empty set.:* We call the ‘alternative’ combinator
<+>
, which is used as an infix operator between two recognizersp
andq
. The<+>
applies both of the recognizers on the same input positionj
and sums up the results returned by both of the recognizers, which is eventually returned as the final result. :* The sequencing of recognizers is done with the
*>
combinator. Like<+>
, it is also used as an infix operator between two recognizers –p
andq
. But it applies the first recognizerp
to the input positionj
, and if there is any successful result of this application, then the second recognizerq
is applied to every element of the result set returned by the first recognizer. The*>
ultimately returns the union of these applications of q.:Examples
* Consider a highly ambiguous CFG
s ::= ‘x’ s s | ɛ
. Using the combinators defined earlier, we can modularly define executable notations of this grammar in a modern functional language (e.g. Haskell) ass = term ‘x’ *> s *> s <+> empty
. When the recognizers
is applied on an input sequencexxxx
at position1
, according to the above definitions it would return a result set{5,4,3,2,1}
. Note that in real implementation if result set is defined as data type that supports repetition (i.e. list), then we can have the resulting list with all possible ambiguous results like[5, 4, 3, 2, 1,…., 5, 4, 3, 2,……]
.hortcomings and Solutions
The simple implementations of parser combinators have some shortcomings, which are common in top-down parsing. Naïve combinatory parsing requires
exponential time and space when parsing an ambiguous context free grammar. In 1996, Frost and SzydlowskiFrost, Richard. and Szydlowski, Barbara. "Memoizing Purely Functional Top-Down Backtracking Language Processors. " "Sci. Comput. Program. " 1996 - 27(3): 263-288. ] demonstrated howmemoization can be used with parser combinators to reduce the time complexity to polynomial. Later Frost used monadsFrost, Richard. "Monadic Memoization towards Correctness-Preserving Reduction of Search. " "Canadian Conference on AI 2003." p 66-80.] to construct the combinators for systematic and correct threading of memo-table throughout the computation.Like any top-down recursive descent parsing, the conventional parser combinators (like the combinators described above)won’t terminate while processing a left-recursive grammar (i.e.
s ::= s *> s *> term ‘x’|empty
). A recognition algorithm Frost, R. and Hafiz, R." A New Top-Down Parsing Algorithm to Accommodate Ambiguity and Left Recursion in Polynomial Time." "ACM SIGPLAN Notices", Volume 41 Issue 5, Pages: 46 - 54. Year: 2006] that accommodates ambiguous grammars with direct left-recursive rules is described by Frost and Hafiz in 2006. The algorithm curtails the otherwise ever-growing left-recursive parse by imposing depth restrictions. That algorithm was extended Frost, R., Hafiz, R. and Callaghan, P." Modular and Efficient Top-Down Parsing for Ambiguous Left-Recursive Grammars ." "10th International Workshop on Parsing Technologies (IWPT), ACL-SIGPARSE ", Pages: 109 - 120, June 2007, Prague.] to a complete parsing algorithm to accommodate indirect as well as direct left-recursion inpolynomial time, and to generate compact polynomial-size representations of the potentially-exponential number of parse trees for highly-ambiguous grammars by Frost, Hafiz and Callaghan in 2007. This extended algorithm accommodates indirect left-recursion by comparing its ‘computed-context’ with ‘current-context’. The same authors also described Frost, R., Hafiz, R. and Callaghan, P." Parser Combinators for Ambiguous Left-Recursive Grammars." " 10th International Symposium on Practical Aspects of Declarative Languages (PADL), ACM-SIGPLAN ", Volume 4902, year 2008, Pages: 167-181, January 2008, San Francisco.] their implementation of a set of parser combinators written in the Haskell programming language based on the same algorithm. The [http://www.cs.uwindsor.ca/~hafiz/proHome.html X-SAIGA] site has more about the algorithms and implementation details.References
External links
* [http://www.cs.uwindsor.ca/~hafiz/proHome.html X-SAIGA] - eXecutable SpecificAtIons of GrAmmars
Wikimedia Foundation. 2010.