Earley parser: Difference between revisions

From formulasearchengine
Jump to navigation Jump to search
en>Jeffreykegler
Remove obsolete Marpa::XS
 
(One intermediate revision by one other user not shown)
Line 1: Line 1:
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|accessdate=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
My name is Precious and I am studying Gender and Women's Studies and Comparative Politics at Breda / Netherlands.<br><br>Here is my weblog [https://youtube.com/watch?v=NE60Qo_zdhw desert plants]
| 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}}</ref>  (and later appeared in abbreviated, more legible form in a journal<ref name="Earley2">{{citation
| last = Earley | first = Jay | authorlink = Jay Earley
| doi = 10.1145/362007.362035
| issue = 2
| journal = [[Communications of the ACM]]
| pages = 94–102
| title = An efficient context-free parsing algorithm
| volume = 13
| year = 1970}}</ref>).
 
Earley parsers are appealing because they can parse all context-free languages{{discuss}}, 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 grammars <math>{O}(n^2)</math>, and linear time for almost all [[LR parser|LR(k) grammars]]. 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 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 (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 → α β)
* our current position in that production (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.)
 
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 (X → γ •, ''j''), find states in S(''j'') of the form (Y → α • X β, ''i'') and add (Y → α X • β, ''i'') to S(''k'').
 
It is important to note that 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.
 
== Pseudocode ==
Adapted from <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=http://books.google.co.uk/books?id=fZmj5UNK8AQC}}</ref> by Daniel Jurafsky and James H. Martin
 
<syntaxhighlight lang="pascal">
function EARLEY-PARSE(words, grammar)
    ENQUEUE((γ → •S, 0), chart[0])
    for i ← from 0 to LENGTH(words) do
        for each state in chart[i] do
            if INCOMPLETE?(state) then
                if NEXT-CAT(state) is a nonterminal then
                    PREDICTOR(state, i, grammar)        // non-terminal
                else do
                    SCANNER(state, i)                    // terminal
            else do
                COMPLETER(state, i)
        end
    end
    return chart
 
procedure PREDICTOR((A → α•B, i), j, grammar)
    for each (B → γ) in GRAMMAR-RULES-FOR(B, grammar) do
        ADD-TO-SET((B → •γ, j), chart[ j])
    end
 
procedure SCANNER((A → α•B, i), j)
    if B ⊂ PARTS-OF-SPEECH(word[j]) then
        ADD-TO-SET((B → word[j], i), chart[j + 1])
    end
 
procedure COMPLETER((B → γ•, j), k)
    for each (A → α•Bβ, i) in chart[j] do
        ADD-TO-SET((A → αB•β, i), chart[k])
    end
</syntaxhighlight>
 
== Example ==
Consider the following simple grammar for arithmetic expressions:<syntaxhighlight lang="bnf">
 
<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:
 
(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.
 
== See also ==
* [[CYK algorithm]]
* [[Context-free grammar]]
* [[List of algorithms#Parsing|Parsing Algorithms]]
 
== Citations ==
{{Reflist}}
 
== Other Reference Materials ==
*{{cite book
| 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}}
*{{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}}.
 
*{{cite conference |first= Masaru|last= Tomita|title= LR parsers for natural languages |conference= 10th International Conference on Computational Linguistics |booktitle= COLING|pages= 354–357|year= 1984}}
 
==External links==
 
===C Implementations===
* [http://cocom.sourceforge.net/ammunition-13.html 'early'] An Earley parser [[C (programming language)|C]] -library.
* [https://bitbucket.org/abki/c-earley-parser/src 'C Earley Parser'] An Earley parser [[C (programming language)|C]]. {{Dead link|date=July 2013}}
 
===Java Implementations===
* [http://linguateca.dei.uc.pt/index.php?sep=recursos PEN] A Java library that implements the Earley algorithm.
* [http://www.ling.ohio-state.edu/~scott/#projects-pep Pep] A Java library that implements the Earley algorithm and provides charts and parse trees as parsing artifacts.
* [http://www.cs.umanitoba.ca/~comp4190/Earley/Earley.java] A Java implementation of Earley parser.
 
===JavaScript Implementations===
* [http://synth.wink.ws/moonyparser/ 'JavaScript Moony Parser'] A type of Earley parser written in [[JavaScript (programming language)|JavaScript]].
 
===Perl Implementations===
*  [https://metacpan.org/module/Marpa::R2 Marpa::R2] and [https://metacpan.org/module/Marpa::XS Marpa::XS], [[Perl]] modules.  [http://jeffreykegler.github.com/Marpa-web-site/ Marpa] is an Earley's algorithm that includes the improvements made by Joop Leo, and by Aycock and Horspool.
* [https://metacpan.org/module/Parse::Earley Parse::Earley] A [[Perl]] module that implements Jay Earley's original algorithm.
 
===Python Implementations===
* [http://www.cavar.me/damir/charty/python/ Charty] a [[Python (programming language)|Python]] implementation of an Earley parser.
* [http://nltk.org/ NLTK] a [[Python (programming language)|Python]] toolkit that has an Earley parser.
* [http://pages.cpsc.ucalgary.ca/~aycock/spark/ Spark] an Object Oriented "little language framework" for [[Python (programming language)|Python]] that implements an Earley parser.
* [http://github.com/tomerfiliba/tau/blob/master/earley3.py earley3.py] A stand-alone implementation of the algorithm in less than 150 lines of code, including generation of the parsing-forest and samples.
 
===Common Lisp Implementations===
* [http://www.cliki.net/CL-EARLEY-PARSER CL-EARLEY-PARSER] A Common Lisp library that implements an Earley parser.
 
===Scheme/Racket Implementations===
* [http://www.cavar.me/damir/charty/scheme/ Charty-Racket] A [[Scheme (programming language)|Scheme]] / [[Racket (programming language)|Racket]] implementation of an Earley parser.
 
===Resources===
* [http://accent.compilertools.net/Entire.html The Accent compiler-compiler]
 
*[http://www.amazon.com/All-But-Soul-Richard-Earley/dp/1436335817 All But a Soul - AI Parcer in C# by Richard Earley]
 
 
[[Category:Parsing algorithms]]
[[Category:Dynamic programming]]
 
{{Link FA|pl}}

Latest revision as of 10:23, 27 November 2014

My name is Precious and I am studying Gender and Women's Studies and Comparative Politics at Breda / Netherlands.

Here is my weblog desert plants