Replace manual lookahead logic with a parsing table. One table entry tells you exactly which production to apply — no guessing, no backtracking.
Recursive descent works but has a problem — each function needs hand-written lookahead logic. LL(1) automates this.
| Aspect | Recursive Descent | LL(1) Predictive |
|---|---|---|
| Lookahead logic | Hand-written in every function | Automatically derived from grammar |
| Decision mechanism | if(peek()=='+') scattered everywhere |
Single table lookup: M[NT, terminal] |
| Stack management | Implicit (call stack) | Explicit (you control the stack) |
| Automation | Must write parser by hand | Parser can be generated from grammar |
| Conflicts detectable? | No — silent ambiguities | Yes — duplicate table entries are conflicts |
M[A, a] tells us which production to use when A is on top of the stack and a is the current input token.
The table entries are derived from two fundamental sets: FIRST (what a string can start with) and FOLLOW (what can follow a non-terminal).
With the table built, parsing is O(n) — each input token is consumed exactly once, and each stack operation is O(1) table lookup.
FIRST(α) = all terminals that can appear as the first symbol of any string derived from α.
id or (.
FOLLOW(A) = all terminals that can appear immediately after A in any sentential form.
$ ∈ FOLLOW(S) where S is the start symbol. The end-of-input marker always follows the start.
Add FIRST(β) – {ε} to FOLLOW(B). Whatever can begin β can immediately follow B.
Add FOLLOW(A) to FOLLOW(B). If β can vanish, what follows A can also follow B.
For each production, determine which table cells to fill using FIRST and FOLLOW sets.
a in FIRST(α): set M[A, a] = A → αb in FOLLOW(A): set M[A, b] = A → α| id | + | * | ( | ) | $ | |
|---|---|---|---|---|---|---|
| E | ||||||
| E' | ||||||
| T | ||||||
| T' | ||||||
| F |
The finished table used by the predictive parser during simulation.
| NT \ Terminal | id | + | * | ( | ) | $ |
|---|---|---|---|---|---|---|
| E | E→TE' | E→TE' | ||||
| E' | E'→+TE' | E'→ε | E'→ε | |||
| T | T→FT' | T→FT' | ||||
| T' | T'→ε | T'→*FT' | T'→ε | T'→ε | ||
| F | F→id | F→(E) |
id + id * idWatch the explicit stack and parsing table work together to recognize the input.
| Stack (top → right) | Input | Action |
|---|