Programming languages, arithmetic expressions, and natural language all share a property: their structure is context-free. Every valid sentence can be described by a grammar — a set of rules that say "a sentence is a noun phrase followed by a verb phrase," "a verb phrase is a verb followed by a noun phrase," and so on recursively.
Most parsers in practice demand a restricted form of grammar so they can run fast. LL parsers read left to right and allow only certain rule shapes. LR parsers are more powerful, but still reject many natural grammars and require preprocessing. Both silently refuse grammars that feel perfectly reasonable to write.
In 1970, Jay Earley published an algorithm that imposes no restrictions at all. Feed it any context-free grammar — ambiguous, left-recursive, wildly nested — and it will answer: "does this string belong to the language?" It does so in time in the worst case, for unambiguous grammars, and for most grammars found in practice (like most programming languages).
The secret is a chart: an array of sets of "Earley items" that grow column by column as each input token is read. Three simple rules — Predict, Scan, and Complete — propagate items through the chart until the whole input is accounted for.
Comments
Loading comments...