Short answer: Earley’s algorithm parses a context-free grammar in O(n³) time for an input of length n, O(n²) when the grammar is unambiguous, and O(n) for many deterministic grammars. It keeps a chart of items, each a grammar rule with a dot and an origin index. Prediction adds rules for the symbol after the dot. Scanning matches a terminal against the next input symbol. Completion advances items that were waiting for a finished rule. The grammar does not have to be in Chomsky normal form.
What the problem actually asks
The CYK algorithm needs a grammar in Chomsky normal form and fills a triangular table of nonterminals. Many grammars are written with longer right-hand sides, nullable symbols, and left recursion. Rewriting them into Chomsky form changes the grammar the caller wrote. Earley’s chart works on the rules as given. An item records how much of a right-hand side has been recognized and where that attempt started.
Left recursion does not loop if an item is added to a set only once. Nullable symbols do chain if prediction and completion run to a fixpoint inside the current set. Both facts are implementation requirements of the same three operations.
What the algorithm guarantees
The start item with the dot at the end and origin 0 sits in the set after the last input symbol if and only if the string is in the language. Time is O(n³) for a general grammar, O(n²) for an unambiguous grammar, and O(n) for a range of grammars that behave like deterministic pushdown automata. Extra memory is the chart, O(n²) items in the general bound. Reconstructing one parse tree walks predecessor pointers stored when items are advanced. The recognition result itself is the accept item.
An ambiguous grammar can have exponentially many parses. The chart still has polynomial size and represents them as a packed forest. Emitting every tree can be exponential and is a separate traversal. CYK remains available when the grammar is already in Chomsky form and a boolean table is enough.
The working steps
- Add a rule from a fresh start symbol to the old start symbol. Put the item with the dot at the left of that rule and origin 0 into set 0.
- In set i, to a fixpoint: predict by adding every rule for the nonterminal after the dot, with origin i; complete by advancing items in the origin set that were waiting for a finished rule.
- Scan items whose dot is in front of a terminal equal to the next input symbol, and place the advanced item into set i+1.
- Reject duplicate items. A set never holds two copies of the same rule, dot, and origin.
- Accept when the fresh start rule, dot at the end, origin 0, is in the set past the last symbol.
The cubic bound is for recognition of a general context-free grammar. Chomsky normal form and a boolean product is CYK. Earley’s algorithm is the predict, scan, and complete chart.
Approaches that look simpler and fail
| Approach | Why it fails when a left-recursive rule is predicted without a duplicate check |
|---|---|
| Add a predicted item even when that item is already in the set | A left-recursive rule predicts itself forever. The set membership test is what makes the set finite. |
| Run completion only once through the set | A nullable symbol can finish a rule that reveals another completion in the same set. The operations run to a fixpoint. |
| Require every rule to have two symbols on the right | That restriction is Chomsky normal form, which CYK uses. Earley scans and completes the right-hand side the grammar actually has. |
| Emit every parse tree and quote a cubic bound | Recognition is cubic. An ambiguous grammar can have exponentially many trees. The chart is polynomial and the full enumeration is not. |
A Chomsky-form boolean table is CYK. Earley’s algorithm is the chart parse for a general context-free grammar.
A working example
This Python fragment fills Earley sets to a fixpoint: predict a nonterminal, scan a matching terminal, and complete a finished item back into its origin. Duplicate items are rejected. The string a+a is in the grammar and prints yes.
rules = {
"S'": [("S",)],
"S": [("S", "+", "M"), ("M",)],
"M": [("a",)],
}
nonterminal = set(rules)
tokens = ["a", "+", "a"]
chart = [[] for _ in range(len(tokens) + 1)]
seen = [set() for _ in chart]
def add(i, item):
if item not in seen[i]:
seen[i].add(item)
chart[i].append(item)
add(0, ("S'", ("S",), 0, 0))
for i in range(len(chart)):
j = 0
while j < len(chart[i]):
lhs, rhs, dot, origin = chart[i][j]
if dot < len(rhs):
nxt = rhs[dot]
if nxt in nonterminal:
for alt in rules[nxt]:
add(i, (nxt, alt, 0, i))
elif i < len(tokens) and tokens[i] == nxt:
add(i + 1, (lhs, rhs, dot + 1, origin))
else:
for lhs2, rhs2, dot2, origin2 in list(chart[origin]):
if dot2 < len(rhs2) and rhs2[dot2] == lhs:
add(i, (lhs2, rhs2, dot2 + 1, origin2))
j += 1
accepted = ("S'", ("S",), 1, 0) in seen[len(tokens)]
assert accepted
print("yes" if accepted else "no")
Where it is used
Earley’s algorithm parses a general context-free grammar in natural-language toolkits that do not want to convert the grammar to Chomsky normal form. Speech and grammar workbenches use it for that reason. CYK is the alternative after the conversion.
When you can drop this algorithm
Drop the general chart when the grammar is already in Chomsky normal form and only membership is needed. CYK is then a direct table. Keep Earley when the grammar should stay in the form it was written, including nullable and left-recursive rules.
Why this stays published
We publish this so a search for Earley’s algorithm returns predict, scan, and complete, and the cubic bound on a general grammar. Earley’s algorithm recognizes a context-free language by predict, scan, and complete steps on a chart, in O(n³) time without requiring Chomsky normal form.
本文由 HoHo 與 AI 協作整理,最後更新於 2026 年 11 月 13 日。
