Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Earley parser
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
{{Short description|Algorithm for parsing context-free languages}} {{Infobox algorithm |name={{PAGENAMEBASE}} |class=[[Parsing]] grammars that are [[Context-free grammar|context-free]] |data=[[String (computer science)|String]] |time=<math>O(n^3)</math> |best-time={{plainlist| * <math>\Omega(n)</math> for all [[deterministic context-free grammar]]s * <math>\Omega(n^2)</math> for [[Ambiguous grammar|unambiguous grammars]] }} |average-time=<math>\Theta(n^3)</math> |space= }} In [[computer science]], the '''Earley parser''' is an [[algorithm]] for [[parsing]] [[String (computer science)|strings]] that belong to a given [[context-free language]], though (depending on the variant) it may suffer problems with certain nullable grammars.<ref>{{cite web|last=Kegler|first=Jeffrey|title=What is the Marpa algorithm?|url=http://blogs.perl.org/users/jeffrey_kegler/2011/11/what-is-the-marpa-algorithm.html|access-date=20 August 2013}}</ref> 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<ref name=Earley1>{{cite book | last=Earley | first=Jay | title=An Efficient Context-Free Parsing Algorithm | year=1968 | publisher=Carnegie-Mellon Dissertation | url=http://reports-archive.adm.cs.cmu.edu/anon/anon/usr/ftp/scan/CMU-CS-68-earley.pdf | access-date=2012-09-12 | archive-date=2017-09-22 | archive-url=https://web.archive.org/web/20170922004954/http://reports-archive.adm.cs.cmu.edu/anon/anon/usr/ftp/scan/CMU-CS-68-earley.pdf | url-status=dead }}</ref> in 1968 (and later appeared in abbreviated, more legible form in a journal).<ref name="Earley2">{{citation | last = Earley | first = Jay | author-link = Jay Earley | doi = 10.1145/362007.362035 | url = http://www-2.cs.cmu.edu/afs/cs.cmu.edu/project/cmt-55/lti/Courses/711/Class-notes/p94-earley.pdf | archive-url = https://web.archive.org/web/20040708052627/http://www-2.cs.cmu.edu/afs/cs.cmu.edu/project/cmt-55/lti/Courses/711/Class-notes/p94-earley.pdf | url-status = dead | archive-date = 2004-07-08 | issue = 2 | journal = [[Communications of the ACM]] | pages = 94β102 | title = An efficient context-free parsing algorithm | volume = 13 | year = 1970| s2cid = 47032707 }}</ref> Earley parsers are appealing because they can parse all context-free languages, unlike [[LR parser]]s and [[LL parser]]s, which are more typically used in [[compiler]]s but which can only handle restricted classes of languages. The Earley parser executes in cubic time in the general case <math>{O}(n^3)</math>, where ''n'' is the length of the parsed string, quadratic time for [[unambiguous grammar]]s <math>{O}(n^2)</math>,<ref>{{cite book | isbn=978-0-201-02988-8 | author=John E. Hopcroft and Jeffrey D. Ullman | title=Introduction to Automata Theory, Languages, and Computation | location=Reading/MA | publisher=Addison-Wesley | year=1979 | url-access=registration | url=https://archive.org/details/introductiontoau00hopc }} p.145</ref> and linear time for all [[deterministic context-free grammar]]s. It performs particularly well when the rules are written [[left recursion|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 (computer science)|string]] of [[Terminal and nonterminal symbols|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 [[Formal grammar#The syntax of grammars|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 ''n''th token. (Informally, input positions can be thought of as locations at [[Lexical analysis|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<ref name=Jurafsky>{{cite book|last=Jurafsky|first=D.|title=Speech and Language Processing: An Introduction to Natural Language Processing, Computational Linguistics, and Speech Recognition|year=2009|publisher=Pearson Prentice Hall|isbn=9780131873216|url=https://books.google.com/books?id=fZmj5UNK8AQC}}</ref> by [[Daniel Jurafsky]] and James H. Martin, <syntaxhighlight lang="pascal"> 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 </syntaxhighlight> == Example == Consider the following simple grammar for arithmetic expressions: <syntaxhighlight lang="text"> <P> ::= <S> # the start rule <S> ::= <S> "+" <M> | <M> <M> ::= <M> "*" <T> | <T> <T> ::= "1" | "2" | "3" | "4" </syntaxhighlight> With the input: 2 + 3 * 4 This is the sequence of state sets: {| class="wikitable" ! (state no.) !! Production !! (Origin) !! Comment |----------------------------------------- ! scope="row" colspan="4" style="text-align:left; background:#e9e9e9;font-family:monospace" | S(0): β’ 2 + 3 * 4 |- | 1 || style="font-family:monospace" |P β β’ S || 0 || start rule |- | 2 || style="font-family:monospace" |S β β’ S + M || 0 || predict from (1) |- | 3 || style="font-family:monospace" |S β β’ M || 0 || predict from (1) |- | 4 || style="font-family:monospace" |M β β’ M * T || 0 || predict from (3) |- | 5 || style="font-family:monospace" |M β β’ T || 0 || predict from (3) |- | 6 || style="font-family:monospace" |T β β’ number || 0 || predict from (5) |- ! scope="row" colspan="4" style="text-align:left; background:#e9e9e9;font-family:monospace" | S(1): 2 β’ + 3 * 4 |- | 1 || style="font-family:monospace" |T β number β’ || 0 || scan from S(0)(6) |- | 2 || style="font-family:monospace" |M β T β’ || 0 || complete from (1) and S(0)(5) |- | 3 || style="font-family:monospace" |M β M β’ * T || 0 || complete from (2) and S(0)(4) |- | 4 || style="font-family:monospace" |S β M β’ || 0 || complete from (2) and S(0)(3) |- | 5 || style="font-family:monospace" |S β S β’ + M || 0 || complete from (4) and S(0)(2) |- | 6 || style="font-family:monospace" |P β S β’ || 0 || complete from (4) and S(0)(1) |- ! scope="row" colspan="4" style="text-align:left; background:#e9e9e9;font-family:monospace" | S(2): 2 + β’ 3 * 4 |- | 1 || style="font-family:monospace" |S β S + β’ M || 0 || scan from S(1)(5) |- | 2 || style="font-family:monospace" |M β β’ M * T || 2 || predict from (1) |- | 3 || style="font-family:monospace" |M β β’ T || 2 || predict from (1) |- | 4 || style="font-family:monospace" |T β β’ number || 2 || predict from (3) |- ! scope="row" colspan="4" style="text-align:left; background:#e9e9e9;font-family:monospace" | S(3): 2 + 3 β’ * 4 |- | 1 || style="font-family:monospace" |T β number β’ || 2 || scan from S(2)(4) |- | 2 || style="font-family:monospace" |M β T β’ || 2 || complete from (1) and S(2)(3) |- | 3 || style="font-family:monospace" |M β M β’ * T || 2 || complete from (2) and S(2)(2) |- | 4 || style="font-family:monospace" |S β S + M β’ || 0 || complete from (2) and S(2)(1) |- | 5 || style="font-family:monospace" |S β S β’ + M || 0 || complete from (4) and S(0)(2) |- | 6 || style="font-family:monospace" |P β S β’ || 0 || complete from (4) and S(0)(1) |- ! scope="row" colspan="4" style="text-align:left; background:#e9e9e9;font-family:monospace" | S(4): 2 + 3 * β’ 4 |- | 1 || style="font-family:monospace" |M β M * β’ T || 2 || scan from S(3)(3) |- | 2 || style="font-family:monospace" |T β β’ number || 4 || predict from (1) |- ! scope="row" colspan="4" style="text-align:left; background:#e9e9e9;font-family:monospace" | S(5): 2 + 3 * 4 β’ |- | 1 || style="font-family:monospace" |T β number β’ || 4 || scan from S(4)(2) |- | 2 || style="font-family:monospace" |M β M * T β’ || 2 || complete from (1) and S(4)(1) |- | 3 || style="font-family:monospace" |M β M β’ * T || 2 || complete from (2) and S(2)(2) |- | 4 || style="font-family:monospace" |S β S + M β’ || 0 || complete from (2) and S(2)(1) |- | 5 || style="font-family:monospace" |S β S β’ + M || 0 || complete from (4) and S(0)(2) |- | 6 || style="font-family:monospace" |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<ref name=Earley3>{{cite book | last=Earley | first=Jay | title=An Efficient Context-Free Parsing Algorithm | year=1968 | publisher=Carnegie-Mellon Dissertation | page=106 | url=http://reports-archive.adm.cs.cmu.edu/anon/anon/usr/ftp/scan/CMU-CS-68-earley.pdf | access-date=2012-09-12 | archive-date=2017-09-22 | archive-url=https://web.archive.org/web/20170922004954/http://reports-archive.adm.cs.cmu.edu/anon/anon/usr/ftp/scan/CMU-CS-68-earley.pdf | url-status=dead }}</ref> 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 [[Masaru Tomita|Tomita]] noticed<ref>{{cite book|last1=Tomita|first1=Masaru|title=Efficient Parsing for Natural Language: A Fast Algorithm for Practical Systems|date=April 17, 2013|publisher=Springer Science and Business Media|isbn=978-1475718850|page=74|url=https://books.google.com/books?id=DAjkBwAAQBAJ&q=Tomita%20Efficient%20Parsing%20for%20natural%20Language&pg=PA74|access-date=16 September 2015}}</ref> 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<ref>{{cite journal|last1=Scott|first1=Elizabeth|title=SPPF-Style Parsing From Earley Recognizers|journal=Electronic Notes in Theoretical Computer Science|date=April 1, 2008|volume=203|issue=2|pages=53β67|doi=10.1016/j.entcs.2008.03.044|doi-access=free}}</ref> 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 [[syntactic ambiguity|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 [https://link.springer.com/content/pdf/10.1007%2F3-540-61053-7_68.pdf "A Faster Earley Parser"] combine Earley parsing with LR parsing and achieve an improvement in an order of magnitude. == See also == * [[CYK algorithm]] * [[Context-free grammar]] * [[List of algorithms#Parsing|Parsing algorithms]] == Citations == {{Reflist}} == Other reference materials == *{{cite journal | last1 = Aycock | first1 = John | last2 = Horspool | first2 = R. Nigel | author2-link = Nigel Horspool | doi = 10.1093/comjnl/45.6.620 | issue = 6 | journal = [[The Computer Journal]] | pages = 620β630 | title = Practical Earley Parsing | volume = 45 | year = 2002| citeseerx = 10.1.1.12.4254 }} *{{citation | last = Leo | first = Joop M. I. M. | doi = 10.1016/0304-3975(91)90180-A | issue = 1 | journal = [[Theoretical Computer Science (journal)|Theoretical Computer Science]] | mr = 1112117 | pages = 165β176 | title = A general context-free parsing algorithm running in linear time on every LR(''k'') grammar without using lookahead | volume = 82 | year = 1991 | doi-access = free }} *{{cite conference |first= Masaru|last= Tomita|title= LR parsers for natural languages |conference= 10th International Conference on Computational Linguistics |book-title= COLING|pages= 354β357|year= 1984|url=https://aclanthology.info/pdf/P/P84/P84-1073.pdf}} {{parsers}} [[Category:Parsing algorithms]] [[Category:Dynamic programming]]
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)
Pages transcluded onto the current version of this page
(
help
)
:
Template:Citation
(
edit
)
Template:Cite book
(
edit
)
Template:Cite conference
(
edit
)
Template:Cite journal
(
edit
)
Template:Cite web
(
edit
)
Template:Infobox algorithm
(
edit
)
Template:Parsers
(
edit
)
Template:Reflist
(
edit
)
Template:Short description
(
edit
)