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 • 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 ==