Sub topic 12
LL(1) grammars and predictive parsing tables
This chapter assumes you can compute FIRST and FOLLOW sets and can remove left recursion and left-factor a grammar. It shows how those sets become a parsing table, how a table-driven parser uses it, and how to tell when a grammar is not LL(1).
What LL(1) means
An LL(1) parser reads the input Left to right and builds a Leftmost derivation. It uses 1 token of lookahead. It is also predictive: it never backtracks. Given the nonterminal on top of the stack and the next input token, exactly one production may be chosen.
A grammar is LL(1) when its parsing table has at most one production in every cell. You can test this without building the table. For every pair of productions A → α | β, three conditions must hold:
- FIRST(α) ∩ FIRST(β) = ∅.
- At most one of α and β can derive ε.
- If β ⇒* ε, then FIRST(α) ∩ FOLLOW(A) = ∅.
A grammar that is left-recursive, ambiguous, or not left-factored can never be LL(1).
Building the table
For each production A → α, apply two rules:
- For every terminal a in FIRST(α), set M[A, a] = A → α.
- If ε is in FIRST(α), set M[A, b] = A → α for every b in FOLLOW(A). Include $ if it is in FOLLOW(A).
Every cell left empty is an error entry.
The expression grammar from the earlier chapters is:
E -> T E'
E' -> + T E' | ε
T -> F T'
T' -> * F T' | ε
F -> ( E ) | idIts FOLLOW sets are FOLLOW(E) = FOLLOW(E') = { $, ) }, FOLLOW(T) = FOLLOW(T') = { +, $, ) }, and FOLLOW(F) = { *, +, $, ) }. The resulting table is:
| Row | id | + | * | ( | ) | $ |
|---|---|---|---|---|---|---|
| E | E → T E' | E → T E' | ||||
| E' | E' → + T E' | E' → ε | E' → ε | |||
| T | T → F T' | T → F T' | ||||
| T' | T' → ε | T' → * F T' | T' → ε | T' → ε | ||
| F | F → id | F → ( E ) |
No cell holds two productions, so the grammar is LL(1). The table has 5 rows and 6 columns, but only 13 cells are filled. Questions that ask for the number of non-error entries expect you to count filled cells.
The parsing algorithm
The parser keeps a stack with $ at the bottom and the start symbol above it. Let X be the stack top and a the current token.
The body is pushed in reverse so that its leftmost symbol ends up on top. An ε-production pushes nothing.
Trace on `id + id`
The stack is written with its top on the left.
| Stack | Input | Action |
|---|---|---|
| E $ | id + id $ | E → T E' |
| T E' $ | id + id $ | T → F T' |
| F T' E' $ | id + id $ | F → id |
| id T' E' $ | id + id $ | match id |
| T' E' $ | + id $ | T' → ε |
| E' $ | + id $ | E' → + T E' |
| + T E' $ | + id $ | match + |
| T E' $ | id $ | T → F T' |
| F T' E' $ | id $ | F → id |
| id T' E' $ | id $ | match id |
| T' E' $ | $ | T' → ε |
| E' $ | $ | E' → ε |
| $ | $ | accept |
The productions used, read from top to bottom, are the leftmost derivation of the input. Each match step consumes one token. The number of table-driven steps in a trace is a common GATE count.
When a grammar is not LL(1)
The classic example is the dangling else after left factoring:
S -> i E t S S' | a
S' -> e S | ε
E -> bFIRST(S') = { e, ε } and FOLLOW(S') = FOLLOW(S) = { e, $ }. Rule 1 puts S' → e S in M[S', e]. Rule 2 puts S' → ε in M[S', e] because e is in FOLLOW(S'). The cell holds two productions, which violates condition 3, so the grammar is not LL(1).
This conflict comes from the grammar's ambiguity, not from a poor choice of lookahead. Parser generators resolve it by binding each else to the nearest if, which is the same as always choosing S' → e S in that cell.
Common errors
- Forgetting FOLLOW for nullable bodies. ε-productions are placed under FOLLOW(A), not under FIRST. Leaving this out produces tables with missing reduce-like entries.
- Putting ε in a column. ε is never an input symbol, so there is no ε column. Only terminals and $ appear.
- Checking only FIRST sets. Two alternatives can have disjoint FIRST sets and still conflict through FOLLOW, as in the exercise below.
- Skipping the transformations. Compute FIRST and FOLLOW only after removing left recursion and left factoring. Otherwise the sets describe the wrong grammar.
- Pushing the body left to right. The leftmost body symbol must end up on top of the stack.
Why the table is deterministic
The three conditions are exactly what rules 1 and 2 need to avoid writing two productions into one cell. This makes LL(1) parsing run in time linear in the input length. The cost is reduced power. Every LL(1) grammar is also SLR(1) and LR(1), but many LR grammars are not LL(1). A later chapter covers this hierarchy.
Exercise
Is the grammar S -> A B, A -> a | ε, B -> a | b LL(1)? Give the cell that decides it.
Show answer
No. FIRST(B) = { a, b }, so FOLLOW(A) = { a, b }. Since A -> ε, rule 2 puts A -> ε in M[A, a]. Rule 1 also puts A -> a in M[A, a]. That cell holds two productions, so the grammar violates condition 3: FIRST(a) ∩ FOLLOW(A) = { a } is not empty.
Exercise
For the expression grammar above, which production does the parser choose for T' when the stack top is T' and the next token is )? Why?
Show answer
T' -> ε. The token ) is in FOLLOW(T'), because T' ends T and T is followed by ) inside F -> ( E ). FIRST( F T') = { } does not contain ), so only the ε-production fills M[T', )].