Earley parser explained

Class:Parsing grammars that are context-free
Data:String
Time:

O(n3)

Best-Time:
Average-Time:

\Theta(n3)

In computer science, the Earley parser is an algorithm for parsing strings that belong to a given context-free language, though (depending on the variant) it may suffer problems with certain nullable grammars.[1] 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[2] in 1968 (and later appeared in an abbreviated, more legible, form in a journal).

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

{O}(n3)

, where n is the length of the parsed string, quadratic time for unambiguous grammars

{O}(n2)

,[3] 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 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 (including the empty string), 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. (Informally, input positions can be thought of as locations at token boundaries.) For every input position, the parser generates a state set. Each state is a tuple (X → α • β, i), consisting of

(Earley's original algorithm included a look-ahead in the state; later research showed this to have little practical effect on the parsing efficiency, and it has subsequently been dropped from most implementations.)

A state is finished when its current position is the last position of the right side of the production, that is, when there is no symbol to the right of the dot • in the visual representation of the state.

The state set at input position k is called S(k). The parser is seeded with S(0) consisting of only the top-level rule. The parser then repeatedly executes three operations: prediction, scanning, and completion.

Duplicate states are not added to the state set, only new ones. These three operations are repeated until no new states can be added to the set. The set is generally implemented as a queue of states to process, with the operation to be performed depending on what kind of state it is.

The algorithm accepts if (X → γ •, 0) ends up in S(n), where (X → γ) is the top level-rule and n the input length, otherwise it rejects.

Pseudocode

Adapted from Speech and Language Processing[4] by Daniel Jurafsky and James H. Martin,

DECLARE ARRAY S;

function INIT(words) S ← CREATE_ARRAY(LENGTH(words) + 1) for k ← from 0 to LENGTH(words) do S[k] ← EMPTY_ORDERED_SET

function EARLEY_PARSE(words, grammar) INIT(words) ADD_TO_SET((γ → •S, 0), S[0]) for k ← from 0 to LENGTH(words) do for each state in S[k] do // S[k] can expand during this loop if not FINISHED(state) then if NEXT_ELEMENT_OF(state) is a nonterminal then PREDICTOR(state, k, grammar) // non_terminal else do SCANNER(state, k, words) // terminal else do COMPLETER(state, k) end end return chart

procedure PREDICTOR((A → α•Bβ, j), k, grammar) for each (B → γ) in GRAMMAR_RULES_FOR(B, grammar) do ADD_TO_SET((B → •γ, k), S[k]) end

procedure SCANNER((A → α•aβ, j), k, words) if j < LENGTH(words) and a ⊂ PARTS_OF_SPEECH(words[k]) then ADD_TO_SET((A → αa•β, j), S[k+1]) end

procedure COMPLETER((B → γ•, x), k) for each (A → α•Bβ, j) in S[x] do ADD_TO_SET((A → αB•β, j), S[k]) end

Example

Consider the following simple grammar for arithmetic expressions:

::= # the start rule ::= "+" | ::= "*" | ::= "1" | "2" | "3" | "4"With the input: 2 + 3 * 4

This is the sequence of state sets:

(state no.) Production (Origin) Comment
S(0): • 2 + 3 * 4
1 P → • S 0 start rule
2 S → • S + M 0 predict from (1)
3 S → • M 0 predict from (1)
4 M → • M * T 0 predict from (3)
5 M → • T 0 predict from (3)
6 T → • number 0 predict from (5)
S(1): 2 • + 3 * 4
1 T → number • 0 scan from S(0)(6)
2 M → T • 0 complete from (1) and S(0)(5)
3 M → M • * T 0 complete from (2) and S(0)(4)
4 S → M • 0 complete from (2) and S(0)(3)
5 S → S • + M 0 complete from (4) and S(0)(2)
6 P → S • 0 complete from (4) and S(0)(1)
S(2): 2 + • 3 * 4
1 S → S + • M 0 scan from S(1)(5)
2 M → • M * T 2 predict from (1)
3 M → • T 2 predict from (1)
4 T → • number 2 predict from (3)
S(3): 2 + 3 • * 4
1 T → number • 2 scan from S(2)(4)
2 M → T • 2 complete from (1) and S(2)(3)
3 M → M • * T 2 complete from (2) and S(2)(2)
4 S → S + M • 0 complete from (2) and S(2)(1)
5 S → S • + M 0 complete from (4) and S(0)(2)
6 P → S • 0 complete from (4) and S(0)(1)
S(4): 2 + 3 * • 4
1 M → M * • T 2 scan from S(3)(3)
2 T → • number 4 predict from (1)
S(5): 2 + 3 * 4 •
1 T → number • 4 scan from S(4)(2)
2 M → M * T • 2 complete from (1) and S(4)(1)
3 M → M • * T 2 complete from (2) and S(2)(2)
4 S → S + M • 0 complete from (2) and S(2)(1)
5 S → S • + M 0 complete from (4) and S(0)(2)
6 P → S • 0 complete from (4) and S(0)(1)
The state (P → S •, 0) represents a completed parse. This state also appears in S(3) and S(1), which are complete sentences.

Constructing the parse forest

Earley's dissertation[5] 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[6] 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[7] is to build the parse forest as you go, augmenting each Earley item with a pointer to a shared packed parse forest (SPPF) node labelled with a triple (s, i, j) where s is a symbol or an LR(0) item (production rule with dot), 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 (there is only one with a given label), but may contain more than one derivation for ambiguous parses. So even if an operation does not add an Earley item (because it already exists), 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 (the non-terminal or completed item).

SPPF nodes are never labeled with a completed LR(0) item: instead they are labelled with the symbol that is produced so that all derivations are combined under one node regardless of which alternative production they come from.

Optimizations

Philippe McLean and R. Nigel Horspool in their paper "A Faster Earley Parser" combine Earley parsing with LR parsing and achieve an improvement in an order of magnitude.

See also

Other reference materials

  • Aycock . John . Horspool . R. Nigel . Nigel Horspool . 10.1093/comjnl/45.6.620 . 6 . . 620–630 . Practical Earley Parsing . 45 . 2002. 10.1.1.12.4254 .

Implementations

C, C++

Haskell

Java

C#

  • coonsta/earley - An Earley parser in C#
  • patrickhuber/pliant - An Earley parser that integrates the improvements adopted by Marpa and demonstrates Elizabeth Scott's tree building algorithm.
  • ellisonch/CFGLib - Probabilistic Context Free Grammar (PCFG) Library for C# (Earley + SPPF, CYK)

JavaScript

OCaml

  • Simple Earley - An implementation of a simple Earley-like parsing algorithm, with documentation.

Perl

  • Marpa::R2 – a Perl module. Marpa is an Earley's algorithm that includes the improvements made by Joop Leo, and by Aycock and Horspool.
  • Parse::Earley – a Perl module implementing Jay Earley's original algorithm

Python

  • Lark – an object-oriented, procedural implementation of an Earley parser, that outputs a SPPF.
  • NLTK – a Python toolkit with an Earley parser
  • Spark – an object-oriented little language framework for Python implementing an Earley parser
  • spark_parser – updated and packaged version of the Spark parser above, which runs in both Python 3 and Python 2
  • earley3.py – a stand-alone implementation of the algorithm in less than 150 lines of code, including generation of the parsing-forest and samples
  • tjr_python_earley_parser - a minimal Earley parser in Python
  • Earley Parsing - A well explained and complete Earley parser tutorial in Python with epsilon handling and Leo optimization for right-recursion.

Rust

  • Santiago – A lexing and parsing toolkit for Rust implementing an Earley parser.

Common Lisp

Scheme, Racket

Wolfram

  • properEarleyParser - A basic minimal implementation of an Earley parser in Wolfram programming language with some essential test cases.

Resources

Notes and References

  1. Web site: Kegler. Jeffrey. What is the Marpa algorithm?. 20 August 2013.
  2. Book: Earley , Jay . An Efficient Context-Free Parsing Algorithm . 1968 . Carnegie-Mellon Dissertation . 2012-09-12 . 2017-09-22 . https://web.archive.org/web/20170922004954/http://reports-archive.adm.cs.cmu.edu/anon/anon/usr/ftp/scan/CMU-CS-68-earley.pdf . dead .
  3. Book: 978-0-201-02988-8 . John E. Hopcroft and Jeffrey D. Ullman . Introduction to Automata Theory, Languages, and Computation . Reading/MA . Addison-Wesley . 1979 . registration . p.145
  4. Book: Jurafsky, D.. Speech and Language Processing: An Introduction to Natural Language Processing, Computational Linguistics, and Speech Recognition. 2009. Pearson Prentice Hall. 9780131873216.
  5. Book: Earley , Jay . An Efficient Context-Free Parsing Algorithm . 1968 . Carnegie-Mellon Dissertation . 106 . 2012-09-12 . 2017-09-22 . https://web.archive.org/web/20170922004954/http://reports-archive.adm.cs.cmu.edu/anon/anon/usr/ftp/scan/CMU-CS-68-earley.pdf . dead .
  6. Book: Tomita. Masaru. Efficient Parsing for Natural Language: A Fast Algorithm for Practical Systems. April 17, 2013. Springer Science and Business Media. 978-1475718850. 74. 16 September 2015.
  7. Scott. Elizabeth. SPPF-Style Parsing From Earley Recognizers. Electronic Notes in Theoretical Computer Science. April 1, 2008. 203. 2. 53–67. 10.1016/j.entcs.2008.03.044. free.