Earley parser

Earley parser
ClassParsing, context-free
Data structureString
Worst-case performance
  • for non-right recursive LR(k) grammars
  • for unambiguous grammars
  • for all other context-free grammars
Worst-case space complexity
  • for non-right recursive LR(k) grammars
  • for all other context-free grammars
Leo variant
ClassParsing, context-free
Data structureString
Worst-case performance
  • for every LR-regular grammar and some others (including some ambiguous ones)
  • for all other context-free grammars
Worst-case space complexity
  • for every LR-regular grammar and some others (including some ambiguous ones)
  • for all other context-free grammars

In computer science, the Earley parser is an algorithm for parsing strings that belong to a given context-free language. Named after its inventor Jay Earley, it was first introduced in his dissertation in 1968 (and later appeared in abbreviated, more legible form in a journal).[1][2] It is a chart parser that uses dynamic programming.

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. However, the original Earley algorithm contained a bug, and its run time and memory use were high for the computers available at the time it was published. Earley parsing has since become a viable solution, as corrections and optimizations of the algorithm have been found and computers have become far more powerful.[3]

Earley parsers perform particularly well when the rules are written left-recursively. Some grammars can be automatically rewritten so that the Earley algorithm parses the language they describe more efficiently.

The worst-case costs of the original Earley algorithm are: linear time and space for non-right recursive LR(k) grammars, quadratic time and space for unambiguous grammars,[4] cubic time and quadratic space for all other context-free grammars, where is the length of the parsed string.[5]

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.

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.

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

  • the production currently being matched (X → α β)
  • the current position in that production (visually represented by the dot •)
  • the position i in the input at which the matching of this production began: the origin position

(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.

  • Prediction: For every state in S(k) of the form (X → α • Y β, j) (where j is the origin position as above), add (Y → • γ, k) to S(k) for every production in the grammar with Y on the left-hand side (Y → γ).
  • Scanning: If a is the next symbol in the input stream, for every state in S(k) of the form (X → α • a β, j), add (X → α a • β, j) to S(k+1).
  • Completion: For every state in S(k) of the form (Y → γ •, j), find all states in S(j) of the form (X → α • Y β, i) and add (X → α Y • β, i) to S(k).

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[6] 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:

<P> ::= <S>      # the start rule
<S> ::= <S> "+" <M> | <M>
<M> ::= <M> "*" <T> | <T>
<T> ::= "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[7] 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[8] 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[9] 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.

Fixes and optimizations

In 1972, Alfred Aho and Jeffrey Ullman fix the empty-rule bug in Earley's algorithm, but their solution makes the algorithm even more costly to run.[10]

In 1991, Joop Leo achieves parsing in linear time and space for every LR(k) grammar.[5]

In 1996, Philippe McLean and R. Nigel Horspool, seemingly unaware of Leo's work, combine Earley parsing with LR parsing.[11]

In 2002, John Aycock and R. Nigel Horspool, still seemingly unaware of Leo's work, solve the empty-rule problem of Earley's algorithm without increasing its cost.[12]

In 2010, Jeffrey Kegler combines the 1991 and 2002 optimizations in his open source Marpa parser.[13][14] He later publishes a formal description of the Marpa recognizer.[15][16] As of 2026, he claims that Marpa runs in linear time for:[17]

  • All grammar classes that recursive descent parses.
  • The grammar class that the yacc family parses.
  • Practically all unambiguous grammars.
  • Ambiguous grammars that are unions of a finite set of any of the above grammars.

See also

Citations

  1. ^ Earley, Jay (1968). An Efficient Context-Free Parsing Algorithm (PDF). Carnegie-Mellon Dissertation. Archived from the original (PDF) on 2017-09-22.
  2. ^ Earley, Jay (1970), "An efficient context-free parsing algorithm" (PDF), Communications of the ACM, 13 (2): 94–102, doi:10.1145/362007.362035, S2CID 47032707, archived from the original (PDF) on 2004-07-08
  3. ^ Jeffrey Kegler (2023-07-06). "Parsing: a timeline -- V3.1". Retrieved 2026-05-05.
  4. ^ John E. Hopcroft and Jeffrey D. Ullman (1979). Introduction to Automata Theory, Languages, and Computation. Reading/MA: Addison-Wesley. ISBN 978-0-201-02988-8. p.145
  5. ^ a b Leo, Joop M.I.M. (1991), "A general context-free parsing algorithm running in linear time on every LR(k) grammar without using lookahead", Theoretical Computer Science, 82 (1): 165–176, doi:10.1016/0304-3975(91)90180-A, MR 1112117
  6. ^ Jurafsky, D. (2009). Speech and Language Processing: An Introduction to Natural Language Processing, Computational Linguistics, and Speech Recognition. Pearson Prentice Hall. ISBN 9780131873216.
  7. ^ Earley, Jay (1968). An Efficient Context-Free Parsing Algorithm (PDF). Carnegie-Mellon Dissertation. p. 106. Archived from the original (PDF) on 2017-09-22. Retrieved 2012-09-12.
  8. ^ Tomita, Masaru (April 17, 2013). Efficient Parsing for Natural Language: A Fast Algorithm for Practical Systems. Springer Science and Business Media. p. 74. ISBN 978-1475718850. Retrieved 16 September 2015.
  9. ^ Scott, Elizabeth (April 1, 2008). "SPPF-Style Parsing From Earley Recognizers". Electronic Notes in Theoretical Computer Science. 203 (2): 53–67. doi:10.1016/j.entcs.2008.03.044.
  10. ^ Aho, Alfred; Ullman, Jeffrey (1972). The theory of parsing, translation, and compiling. Vol. 1. Prentice Hall. p. 321.
  11. ^ McLean, Philippe; Horspool, R. Nigel (1996). "A Faster Earley Parser" (PDF). LNCS. 1060: 281–293.
  12. ^ Aycock, John; Horspool, R. Nigel (2002). "Practical Earley Parsing" (PDF). The Computer Journal. 45 (6): 620–630. CiteSeerX 10.1.1.12.4254. doi:10.1093/comjnl/45.6.620.
  13. ^ Jeffrey Kegler (2010-06-05). "Marpa is now O(n) for Right Recursions". Retrieved 2026-05-05.
  14. ^ Kegler, Jeffrey (2011-11-18). "What is the Marpa algorithm?". Retrieved 20 August 2013.
  15. ^ Kegler, Jeffrey (2013-05-22), Marpa, A practical general parser: the recognizer
  16. ^ Kegler, Jeffrey (2023-01-27), Marpa, A practical general parser: the recognizer (3rd ed.)
  17. ^ Jeffrey Kegler. "The Marpa parser". Retrieved 2026-05-05.

Other reference materials

Content Disclaimer

Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.

  1. The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
  2. There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
  3. It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
  4. Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
  5. Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.