Earley parser
In computer science, the Earley parser is an algorithm for parsing strings that belong to a given context-free language, though it may suffer problems with certain nullable grammars. The algorithm, named after its inventor, Jay Earley, is a chart parser that uses dynamic programming; it is mainly used for parsing in computational linguistics. It was first introduced in his dissertation in 1968.
Earley parsers are appealing because they can parse all context-free languages, unlike LR parsers and LL parsers, which are more typically used in compilers but which can only handle restricted classes of languages. The Earley parser executes in cubic time in the general case, where n is the length of the parsed string, quadratic time for unambiguous grammars, and linear time for all deterministic context-free grammars. It performs particularly well when the rules are written left-recursively.
Earley recogniser
The following algorithm describes the Earley recogniser. The recogniser can be easily modified to create a parse tree as it recognises, and in that way can be turned into a parser.The algorithm
In the following descriptions, α, β, and γ represent any string of terminals/nonterminals, X and Y represent single nonterminals, and a represents a terminal symbol.Earley's algorithm is a top-down dynamic programming algorithm. In the following, we use Earley's dot notation: given a production X → αβ, the notation X → α • β represents a condition in which α has already been parsed and β is expected.
Input position 0 is the position prior to input. Input position n is the position after accepting the nth token. For every input position, the parser generates a state set. Each state is a tuple, consisting of
- the production currently being matched
- the current position in that production
- the position i in the input at which the matching of this production began: the origin position
- Prediction: For every state in S of the form , add to S for every production in the grammar with Y on the left-hand side.
- Scanning: If a is the next symbol in the input stream, for every state in S of the form, add to S.
- Completion: For every state in S of the form, find all states in S of the form and add to S.
The algorithm accepts if ends up in S, where is the top level-rule and n the input length, otherwise it rejects.
Pseudocode
Adapted from Speech and Language Processing by Daniel Jurafsky and James H. Martin,DECLARE ARRAY S;
function INIT
S ← CREATE-ARRAY
for k ← from 0 to LENGTH do
S ← EMPTY-ORDERED-SET
function EARLEY-PARSE
INIT
ADD-TO-SET
for k ← from 0 to LENGTH do
for each state in S do // S can expand during this loop
if not FINISHED then
if NEXT-ELEMENT-OF is a nonterminal then
PREDICTOR // non-terminal
else do
SCANNER // terminal
else do
COMPLETER
end
end
return chart
procedure PREDICTOR
for each in GRAMMAR-RULES-FOR do
ADD-TO-SET
end
procedure SCANNER
if a ⊂ PARTS-OF-SPEECH then
ADD-TO-SET
end
procedure COMPLETER
for each in S do
ADD-TO-SET
end
Example
Consider the following simple grammar for arithmetic expressions: ::= # the start rule ::= "+"
With the input:
2 + 3 * 4
This is the sequence of state sets:
The state represents a completed parse. This state also appears in S and S, which are complete sentences.
Constructing the parse forest
Earley's dissertation briefly describes an algorithm for constructing parse trees by adding a set of pointers from each non-terminal in an Earley item back to the items that caused it to be recognized. But Tomita noticed that this does not take into account the relations between symbols, so if we consider the grammar S → SS | b and the string bbb, it only notes that each S can match one or two b's, and thus produces spurious derivations for bb and bbbb as well as the two correct derivations for bbb.Another method is to build the parse forest as you go, augmenting each Earley item with a pointer to a shared packed parse forest node labelled with a triple where s is a symbol or an LR item, and i and j give the section of the input string derived by this node. A node's contents are either a pair of child pointers giving a single derivation, or a list of "packed" nodes each containing a pair of pointers and representing one derivation. SPPF nodes are unique, but may contain more than one derivation for ambiguous parses. So even if an operation does not add an Earley item, it may still add a derivation to the item's parse forest.
- Predicted items have a null SPPF pointer.
- The scanner creates an SPPF node representing the non-terminal it is scanning.
- Then when the scanner or completer advance an item, they add a derivation whose children are the node from the item whose dot was advanced, and the one for the new symbol that was advanced over.
Citations
Other reference materials
Implementations
C, C++
- – C/C++ libraries
- – an Earley parser C
Haskell
- – an Earley parser DSL in Haskell
Java
- – a Java implementation of the Earley algorithm
- – a Java library that implements the Earley algorithm
- – a Java library that implements the Earley algorithm and provides charts and parse trees as parsing artifacts
- - a Java library that implements the probabilistic Earley algorithm, which is useful to determine the most likely parse tree from an ambiguous sentence
C#
- - An Earley parser in C#
- - An Earley parser that integrates the improvements adopted by Marpa and demonstrates Elizabeth Scott's tree building algorithm.
- - Probabilistic Context Free Grammar Library for C#
JavaScript
- – an Earley parser that's starting to integrate the improvements that Marpa adopted
- – a toy parser to demonstrate Elizabeth Scott's technique for building the shared packed parse forest
- – a tiny JavaScript implementation of Earley parser
- - JavaScript implementation of the probabilistic Earley parser
OCaml
- - An implementation of a simple Earley-like parsing algorithm, with documentation.
Perl
- – a Perl module. is an Earley's algorithm that includes the improvements made by Joop Leo, and by Aycock and Horspool.
- – a Perl module implementing Jay Earley's original algorithm
Python
- – an object-oriented, procedural implementation of an Earley parser in under 200 lines of code
- – a Python toolkit with an Earley parser
- – an object-oriented little language framework for Python implementing an Earley parser
- – updated and packaged version of the Spark parser above, which runs in both Python 3 and Python 2
- – a stand-alone implementation of the algorithm in less than 150 lines of code, including generation of the parsing-forest and samples
- - a minimal Earley parser in Python
Common Lisp
- – a Common Lisp library implementing an Earley parser
Scheme, Racket
- – a Scheme-Racket implementation of an Earley parser
Resources