In
computer science
Computer science is the study of computation, automation, and information. Computer science spans theoretical disciplines (such as algorithms, theory of computation, information theory, and automation) to practical disciplines (includi ...
, the Earley parser is an
algorithm
In mathematics and computer science, an algorithm () is a finite sequence of rigorous instructions, typically used to solve a class of specific problems or to perform a computation. Algorithms are used as specifications for performing ...
for
parsing
Parsing, syntax analysis, or syntactic analysis is the process of analyzing a string of symbols, either in natural language, computer languages or data structures, conforming to the rules of a formal grammar. The term ''parsing'' comes from L ...
strings
String or strings may refer to:
*String (structure), a long flexible structure made from threads twisted together, which is used to tie, bind, or hang other objects
Arts, entertainment, and media Films
* ''Strings'' (1991 film), a Canadian anim ...
that belong to a given
context-free language
In formal language theory, a context-free language (CFL) is a language generated by a context-free grammar (CFG).
Context-free languages have many applications in programming languages, in particular, most arithmetic expressions are generated by ...
, though (depending on the variant) it may suffer problems with certain nullable grammars. The algorithm, named after its inventor,
Jay Earley
Jay Earley is an American computer scientist and psychologist. He invented the Earley parser in his early career in computer science. Later he became a clinical psychologist specializing in group therapy and Internal Family Systems Therapy (IFS), i ...
, is a
chart parser
In computer science, a chart parser is a type of parser suitable for ambiguous grammars (including grammars of natural languages). It uses the dynamic programming approach—partial hypothesized results are stored in a structure called a chart and ...
that uses
dynamic programming
Dynamic programming is both a mathematical optimization method and a computer programming method. The method was developed by Richard Bellman in the 1950s and has found applications in numerous fields, from aerospace engineering to economics. ...
; it is mainly used for parsing in
computational linguistics. It was first introduced in his dissertation
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 parser
In computer science, LR parsers are a type of bottom-up parser that analyse deterministic context-free languages in linear time. There are several variants of LR parsers: SLR parsers, LALR parsers, Canonical LR(1) parsers, Minimal LR(1) parse ...
s and
LL parser
In computer science, an LL parser (Left-to-right, leftmost derivation) is a top-down parser for a restricted context-free language. It parses the input from Left to right, performing Leftmost derivation of the sentence.
An LL parser is called a ...
s, which are more typically used in
compiler
In computing, a compiler is a computer program that translates computer code written in one programming language (the ''source'' language) into another language (the ''target'' language). The name "compiler" is primarily used for programs tha ...
s 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 grammar
In computer science, an ambiguous grammar is a context-free grammar for which there exists a string that can have more than one leftmost derivation or parse tree, while an unambiguous grammar is a context-free grammar for which every valid string ...
s
, and linear time for all
deterministic context-free grammar In formal grammar theory, the deterministic context-free grammars (DCFGs) are a proper subset of the context-free grammars. They are the subset of context-free grammars that can be derived from deterministic pushdown automata, and they generate the ...
s. 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
In formal language theory, the empty string, or empty word, is the unique string of length zero.
Formal theory
Formally, a string is a finite, ordered sequence of characters such as letters, digits or spaces. The empty string is the special cas ...
), X and Y represent single nonterminals, and ''a'' represents a terminal symbol.
Earley's algorithm is a top-down
dynamic programming
Dynamic programming is both a mathematical optimization method and a computer programming method. The method was developed by Richard Bellman in the 1950s and has found applications in numerous fields, from aerospace engineering to economics. ...
algorithm. In the following, we use Earley's dot notation: given a
production
Production may refer to:
Economics and business
* Production (economics)
* Production, the act of manufacturing goods
* Production, in the outline of industrial organization, the act of making products (goods and services)
* Production as a stati ...
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
token boundaries.) For every input position, the parser generates a ''state set''. Each state is a
tuple
In mathematics, a tuple is a finite ordered list (sequence) of elements. An -tuple is a sequence (or ordered list) of elements, where is a non-negative integer. There is only one 0-tuple, referred to as ''the empty tuple''. An -tuple is defi ...
(X → α • β, ''i''), consisting of
* the production currently being matched (X → α β)
* the 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 (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
by
Daniel Jurafsky
Daniel Jurafsky is a professor of linguistics and computer science at Stanford University, and also an author. With Daniel Gildea, he is known for developing the first automatic system for semantic role labeling (SRL). He is the author of ''The ...
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 ← EMPTY_ORDERED_SET
function EARLEY_PARSE(words, grammar)
INIT(words)
ADD_TO_SET((γ → •S, 0), S
for k ← from 0 to LENGTH(words) do
for each state in S do // S 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
end
procedure SCANNER((A → α•aβ, j), k, words)
if a ⊂ PARTS_OF_SPEECH(words then
ADD_TO_SET((A → αa•β, j), S +1
end
procedure COMPLETER((B → γ•, x), k)
for each (A → α•Bβ, j) in S do
ADD_TO_SET((A → αB•β, j), S
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:
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
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 (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 pape
"A Faster Earley Parser"combine Earley parsing with LR parsing and achieve an improvement in an order of magnitude.
See also
*
CYK algorithm
In computer science, the Cocke–Younger–Kasami algorithm (alternatively called CYK, or CKY) is a parsing algorithm for context-free grammars published by Itiroo Sakai in 1961. The algorithm is named after some of its rediscoverers: John Cocke, ...
*
Context-free grammar
*
Parsing algorithms
Citations
Other reference materials
*
*
*
Implementations
C, C++
'Yet Another Earley Parser (YAEP)'–
C/
C++
C++ (pronounced "C plus plus") is a high-level general-purpose programming language created by Danish computer scientist Bjarne Stroustrup as an extension of the C programming language, or "C with Classes". The language has expanded significan ...
libraries
'C Earley Parser'– an Earley parser C
Haskell
'Earley'– an Earley parser
DSL
Digital subscriber line (DSL; originally digital subscriber loop) is a family of technologies that are used to transmit digital data over telephone lines. In telecommunications marketing, the term DSL is widely understood to mean asymmetric dig ...
in
Haskell
Haskell () is a general-purpose, statically-typed, purely functional programming language with type inference and lazy evaluation. Designed for teaching, research and industrial applications, Haskell has pioneered a number of programming lan ...
Java
– a Java implementation of the Earley algorithm
PEN– a Java library that implements the Earley algorithm
Pep– a Java library that implements the Earley algorithm and provides charts and parse trees as parsing artifacts
digitalheir/java-probabilistic-earley-parser- a Java library that implements the probabilistic Earley algorithm, which is useful to determine the most likely parse tree from an ambiguous sentence
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
Nearley– an Earley parser that's starting to integrate the improvements that Marpa adopted
A Pint-sized Earley Parser– a toy parser (with annotated pseudocode) to demonstrate Elizabeth Scott's technique for building the shared packed parse forest
lagodiuk/earley-parser-js– a tiny JavaScript implementation of Earley parser (including generation of the parsing-forest)
digitalheir/probabilistic-earley-parser-javascript- JavaScript implementation of the probabilistic Earley parser
OCaml
Simple Earley- An implementation of a simple Earley-like parsing algorithm, with documentation.
Perl
Marpa::R2– a
Perl
Perl is a family of two high-level, general-purpose, interpreted, dynamic programming languages. "Perl" refers to Perl 5, but from 2000 to 2019 it also referred to its redesigned "sister language", Perl 6, before the latter's name was offic ...
module.
Marpais 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
Python may refer to:
Snakes
* Pythonidae, a family of nonvenomous snakes found in Africa, Asia, and Australia
** ''Python'' (genus), a genus of Pythonidae found in Africa and Asia
* Python (mythology), a mythical serpent
Computing
* Python (pro ...
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
CL-Earley-parser– a Common Lisp library implementing an Earley parser
Scheme, Racket
Charty-Racket– a
Scheme A scheme is a systematic plan for the implementation of a certain idea.
Scheme or schemer may refer to:
Arts and entertainment
* ''The Scheme'' (TV series), a BBC Scotland documentary series
* The Scheme (band), an English pop band
* ''The Schem ...
-
Racket implementation of an Earley parser
Wolfram
properEarleyParser- A basic minimal implementation of an Earley parser in
Wolfram programming language with some essential test cases.
Resources
The Accent compiler-compiler
{{parsers
Parsing algorithms
Dynamic programming