§ 3.7Module 3

Top-down Parsing: Earley and Predictive Parsers

On this page

Top-down Parsing: Earley and Predictive Parsers

Recall first

Why can naïve recursive descent loop on A → A x? Which top-down parser uses a chart and can handle general CFGs?

First principles

The syllabus says “Early”; the standard algorithm is Earley parsing, named after Jay Earley. It is a top-down chart parser for general context-free grammars. An Earley state is written

[A → α • β, j]

in chart position i: the parser has recognized α, expects β, and the production began at origin j.

For each chart position, apply:

Duplicate states are stored once. A final completed start state indicates acceptance. Earley handles left recursion and grammars beyond CNF; worst-case complexity is cubic, with faster behavior for more restricted/unambiguous cases. It can preserve ambiguity in a shared chart rather than blindly committing to one derivation.

A predictive parser is a restricted top-down parser, commonly recursive descent with one-symbol lookahead (LL(1)). It selects a production using FIRST sets; if a production can derive ε, FOLLOW information helps decide. It requires a suitably factored grammar with no left recursion and no FIRST/FIRST or relevant FIRST/FOLLOW conflicts. It is simple and often linear, but grammar transformation may be needed and ambiguous/general CFGs are outside its comfortable range.

Worked micro-trace. For S→NP VP, NP→Det N, input the dog, start with [S→•NP VP,0]. Predictor adds [NP→•Det N,0], then [Det→•the,0]; scanner reads the; completion of Det advances NP to [NP→Det • N,0]; prediction adds N; scanner reads dog; completing N completes NP, advancing S to [S→NP • VP,0]. The trace shows prediction, scanning, and completion—not a stack-only reduction.

Exercise — reveal after answering

Which grammar repair helps predictive parsing with A → A x | y?

Answer: Remove left recursion, e.g. A → y A' and A' → x A' | ε, then use FIRST/FOLLOW-guided choice.

Exam lens

Correct the spelling to Earley politely if needed. List the three Earley operations and state chart positions. Contrast Earley’s generality with predictive parsing’s LL(1) speed/grammar restrictions.

Rapid revision checklist

Key takeaways

Sources