Compiler Design
Top-Down Parsing (LL)
Top-down questions follow one recipe: clean the grammar (remove left recursion, left-factor), compute FIRST/FOLLOW, fill the predictive table, and look for multiply-defined entries. Practise it on the expression grammar until it is automatic.
GATE CSE top-down parsing — recursive descent, left recursion elimination, left factoring, LL(1) conditions, predictive parse-table construction and parse traces, with sample questions, flashcards and drills.
Learning outcomes
- Eliminate immediate and indirect left recursion and apply left factoring
- State the LL(1) conditions and test a grammar with FIRST/FOLLOW sets
- Build an LL(1) predictive parse table and count its non-error entries
- Trace a table-driven predictive parser on an input string, step by step
Top-down parsing overview
- Top-down parsing starts from the start symbol and tries to derive the input by repeatedly replacing the leftmost non-terminal — it builds the parse tree from the root down and traces a LEFTMOST derivation.
- Recursive descent: one procedure per non-terminal; the procedure chooses a production, matches terminals and calls other procedures for non-terminals. With backtracking it can handle any grammar without left recursion (possibly in exponential time); without backtracking it needs a predictable choice.
- Predictive parsing is recursive descent with no backtracking: one lookahead token determines the production. A table-driven predictive parser uses an explicit stack (start with $ and S), the input buffer ending in $, and a table M[A, a].
- Table-driven algorithm: let X be the top of the stack and a the current input symbol. If X = a = $, accept. If X is a terminal equal to a, pop and advance. If X is a non-terminal, look up M[X, a]; replace X by the right-hand side (leftmost symbol on top); if the entry is empty, report an error.
- An LL(1) parser reads the input Left to right, builds a Leftmost derivation, and uses 1 token of lookahead. It runs in linear time O(n) and is simple to implement and to produce good error messages with.
- Backtracking versus predictive: a backtracking parser tries alternatives in order and undoes input consumption when one fails (possibly exponential time, poor error positions). A predictive parser uses FIRST/FOLLOW so the correct alternative is chosen without trying others; LL(1) grammars are exactly those for which one lookahead token suffices.
- Shape of a trace: parsing id + id with the LL(1) grammar makes 5 procedure calls (E, T, E′, T, E′) and 3 token matches. The order of procedure entries is the pre-order of the parse tree, and the call-stack depth equals the current depth in the tree.
- Recursive descent keeps its stack implicitly in procedure calls; the table-driven parser keeps the same information in an explicit stack. Both build the same leftmost derivation and both run in O(n) time for LL(1) grammars.
- Problems for top-down parsing: left recursion (infinite loop), common prefixes (needs left factoring or backtracking), ambiguity (cannot be LL(k)), and the need for lookahead to pick between alternatives.
- Error recovery in predictive parsing: panic mode — when the table entry is empty, skip input tokens until one in a synchronizing set (usually FOLLOW(A) and FIRST of higher constructs) is found, then pop A and continue.
| Stack top X | Lookahead a | Action |
|---|---|---|
| X = a = $ | same | accept |
| terminal X = a | same | pop X, advance input |
| terminal X ≠ a | different | syntax error |
| non-terminal X | M[X, a] = X → Y1…Yk | pop X, push Yk … Y1 (Y1 on top) |
| non-terminal X | M[X, a] empty | syntax error / recovery |
Worked example
Problem. What does a recursive-descent procedure for E → E + T | T do on the first call, if written directly from the grammar?
Solution. The procedure E() first calls E() again (to match the leftmost E) without consuming any input, so it recurses forever — a stack overflow. Left-recursive grammars must be rewritten before using recursive descent.
Eliminating left recursion and left factoring
- Immediate left recursion: A → A α1 | A α2 | β1 | β2 is rewritten as A → β1 A′ | β2 A′ ; A′ → α1 A′ | α2 A′ | ε. The language is unchanged, and the recursion becomes right recursion.
- Example: E → E + T | T becomes E → T E′; E′ → + T E′ | ε. Similarly T → T * F | F becomes T → F T′; T′ → * F T′ | ε. This produces the LL(1) expression grammar.
- Indirect left recursion (S → A a | b ; A → A c | S d | ε): order the non-terminals, substitute earlier ones into later ones, and then remove the immediate left recursion. For order S, A: A → A c | A a d | b d | ε becomes A → b d A′ | A′ ; A′ → c A′ | a d A′ | ε.
- Left factoring: if A → α β1 | α β2 share a common prefix α, rewrite as A → α A′ ; A′ → β1 | β2. It postpones the decision until enough of the input has been seen, so one lookahead can choose.
- Left factoring example: S → i E t S | i E t S e S | a becomes S → i E t S S′ | a ; S′ → e S | ε. (The result is still NOT LL(1) because the grammar is ambiguous — the else problem.)
- General algorithm: order the non-terminals A1 … An. For i = 1 … n and for j = 1 … i − 1, replace every Ai → Aj γ by Ai → δ1 γ | … | δk γ where Aj → δ1 | … | δk, then remove the immediate left recursion among the Ai productions.
- Size of the result: removing immediate left recursion from A with m left-recursive alternatives and n other alternatives gives n productions for A, m + 1 for A′, so n + m + 1 in total. E → E + T | T (m = n = 1) becomes 3 productions.
- Associativity warning: E → E − T | T is left-associative, but the rewritten right-recursive grammar would group 9 − 4 − 3 as 9 − (4 − 3) if values were only synthesized. The semantic actions must pass the running value down as an INHERITED attribute to keep (9 − 4) − 3.
- Left factoring may need several rounds: A → a b c | a b d | a e becomes A → a A′, A′ → b A″ | e, A″ → c | d. Repeat until no two alternatives of any non-terminal share a prefix.
- Left recursion removal and left factoring change the grammar but not the language; they do change the shape of the parse tree, which is why SDT with left-recursive grammars needs care (inherited attributes).
- Removing left recursion from a grammar with ε-productions or cycles requires first eliminating cycles (A ⇒+ A) and ε-productions.
Formulas to remember
-
A → Aα | β ⇒ A → βA′ ; A′ → αA′ | ε -
A → αβ1 | αβ2 ⇒ A → αA′ ; A′ → β1 | β2
| Problem | Before | After | Why it is needed |
|---|---|---|---|
| Immediate left recursion | A → A α | β | A → β A′ ; A′ → α A′ | ε | recursive descent would loop |
| Common prefix | A → α β1 | α β2 | A → α A′ ; A′ → β1 | β2 | FIRST sets of alternatives overlap |
| Indirect left recursion | S → A a ; A → S b | c | substitute S into A, then remove immediate recursion | A ⇒ S b ⇒ A a b |
Worked example
Problem. Remove left recursion from S → A a | b ; A → A c | S d | ε.
Solution. Substitute S into A: A → A c | A a d | b d | ε. Now remove the immediate left recursion in A: A → b d A′ | A′ ; A′ → c A′ | a d A′ | ε. The S-production S → A a | b is unchanged.
LL(1) conditions and the predictive parse table
- Table construction: for each production A → α: (1) for every terminal a in FIRST(α), put A → α in M[A, a]; (2) if ε ∈ FIRST(α), put A → α in M[A, b] for every b in FOLLOW(A) (including $). All other entries are errors.
- A grammar is LL(1) if and only if its table has NO multiply-defined entry. Equivalent test, for every pair of productions A → α | β: FIRST(α) ∩ FIRST(β) = ∅; at most one of α, β derives ε; and if β ⇒* ε then FIRST(α) ∩ FOLLOW(A) = ∅.
- A left-recursive, ambiguous or non-left-factored grammar can never be LL(1). The reverse is false: a grammar free of these can still fail the FIRST/FOLLOW tests.
- Counting entries: the number of non-error cells equals the sum over productions of |FIRST(α)| (plus |FOLLOW(A)| for a nullable α). For the expression grammar this gives 13 non-error entries.
- Selection set: SELECT(A → α) = FIRST(α) when α is not nullable, otherwise (FIRST(α) − ε) ∪ FOLLOW(A). A grammar is LL(1) exactly when, for every non-terminal, the selection sets of its productions are pairwise disjoint.
- Reading the figure: rows are non-terminals, columns are terminals plus $. The table of the expression grammar has 5 × 6 = 30 cells, of which 13 are filled; every blank cell is a syntax error. The number of multiply-defined cells is the number of LL(1) violations.
- Hierarchy: LL(1) ⊂ LL(2) ⊂ … ⊂ LL(k); every LL(1) grammar is unambiguous and LR(1). The class of languages for LL(k) is strictly smaller than DCFL.
- LL(1) predictive parsing of the dangling-else grammar fails: after left factoring, M[S′, e] contains both S′ → e S and S′ → ε because e ∈ FOLLOW(S′).
| 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 ) |
Worked example
Problem. Is S → i E t S S′ | a ; S′ → e S | ε ; E → b LL(1)?
Solution. FIRST(e S) = {e}. S′ → ε is chosen for every b in FOLLOW(S′) = FOLLOW(S) = {e, $}. So M[S′, e] gets both S′ → e S and S′ → ε: a multiply-defined entry. The grammar is not LL(1).
Worked example — parsing id + id with the LL(1) table
- Start with stack = $ E (E on top) and input = id + id $. At each step, compare the top of the stack with the lookahead and either match, expand using the table, or accept.
- Every expansion corresponds to a step of a leftmost derivation: E ⇒ T E′ ⇒ F T′ E′ ⇒ id T′ E′ ⇒ id E′ ⇒ id + T E′ ⇒ id + F T′ E′ ⇒ id + id T′ E′ ⇒ id + id E′ ⇒ id + id.
- The number of rows in the trace is the number of productions used plus the number of matched terminals plus the final accept. Here 9 productions + 3 matches (id, +, id) + 1 accept = 13 rows.
- Errors are detected as soon as the lookahead is not in the table row of the top non-terminal (the valid-prefix property of LL(1) parsing).
| Stack (top at left) | 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 |
Worked example
Problem. How many productions are used to parse id + id with the LL(1) expression grammar?
Solution. The trace uses E → T E′, T → F T′ (twice), F → id (twice), T′ → ε (twice), E′ → + T E′, and E′ → ε: 1 + 2 + 2 + 2 + 1 + 1 = 9 productions.
LL(1) conflicts — why a grammar fails
- FIRST/FIRST conflict: two alternatives of A can start with the same terminal (a visible common prefix, or one revealed after substituting a nullable symbol). Cure: left factoring or substitution.
- FIRST/FOLLOW conflict: A has a nullable alternative and FOLLOW(A) overlaps FIRST of another alternative, so the ε-entry and the other entry land in the same cell. Cure: rewrite the grammar; sometimes impossible (dangling else).
- Left recursion makes both alternatives of A → A α | β start with FIRST(β), so every cell in that row is doubly defined; cure by eliminating the recursion. Ambiguity always produces at least one conflict and cannot be cured by factoring alone.
- Test procedure: (1) look for left recursion; (2) compute nullable, FIRST and FOLLOW; (3) for each non-terminal with several alternatives test the three LL(1) conditions or build its row of the table; (4) one violation is enough to answer “not LL(1)”.
| Conflict | Example | Doubly defined cell | Cure |
|---|---|---|---|
| FIRST/FIRST | S → a S | a | M[S, a] | left factor |
| FIRST/FOLLOW | S → A a ; A → a | ε | M[A, a]: A → a and A → ε | rewrite grammar |
| FIRST/FIRST via nullable | S → A a | b ; A → b | ε | M[S, b]: S → A a and S → b | substitute A |
| Left recursion | E → E + T | T | row E, columns of FIRST(T) | eliminate recursion |
| Ambiguity | S′ → e S | ε (dangling else) | M[S′, e] | cannot be factored away |
Worked example
Problem. Is S → A a | b ; A → b | ε LL(1)?
Solution. FIRST(A a) = FIRST(A) − ε ∪ {a} = {a, b} because A is nullable, and FIRST(b) = {b}. They intersect in b, so M[S, b] holds both S → A a and S → b: a FIRST/FIRST conflict exposed through the nullable A. The grammar is not LL(1).
Which grammars are LL(1)? — quick checks
- S → a S b | ε is LL(1): FIRST(a S b) = {a}; FOLLOW(S) = {b, $} for the ε-production. The table has M[S, a] = a S b, M[S, b] = ε, M[S, $] = ε — no conflict.
- S → a S | a is NOT LL(1): both alternatives start with a (common prefix). Left factoring gives S → a S′; S′ → S | ε, with FIRST(S) = {a} and FOLLOW(S′) = {$}. These are disjoint, so the factored grammar is LL(1).
- S → A a A b | B b B a ; A → ε ; B → ε IS LL(1): FIRST(A a A b) = {a} and FIRST(B b B a) = {b} are disjoint, and the ε-productions of A and B are selected on {a, b}, which do not clash with each other.
- Any grammar with left recursion (E → E + T | T), ambiguity (E → E + E | id) or a common prefix (A → a b | a c) is not LL(1) until transformed.
- Strong LL(k): for k = 1 every LL(1) grammar is “strong”. For k > 1 the distinction between LL(k) and strong LL(k) matters, but GATE does not test it.
- LL(1) parsing cannot handle left-associative operators directly in the grammar; the left-associativity is restored in the semantic actions (inherited attributes) after rewriting the grammar into right-recursive form.
| Grammar | LL(1)? | Reason |
|---|---|---|
| S → aSb | ε | Yes | disjoint selection sets |
| S → aS | a | No | common prefix a |
| E → E + T | T | No | left recursive |
| E → T E′ ; E′ → + T E′ | ε | Yes (with T LL(1)) | right recursive, factored |
| S → iEtS | iEtSeS | a | No | common prefix / ambiguous |
| S → AaAb | BbBa ; A, B → ε | Yes | FIRST = {a} vs {b} |
Worked example
Problem. Check whether S → A a A b | B b B a ; A → ε ; B → ε is LL(1) by building its table.
Solution. FIRST(A a A b) = {a} and FIRST(B b B a) = {b}, so M[S, a] = S → A a A b and M[S, b] = S → B b B a. FOLLOW(A) = {a, b} and FOLLOW(B) = {a, b}, so A → ε is placed in M[A, a] and M[A, b]; similarly for B. No cell has two productions, so the grammar is LL(1).
Exam tips
- Order of work: remove left recursion → left factor → compute FIRST/FOLLOW → fill the table → look for double entries.
- ε-productions go under every terminal in FOLLOW(A), including $.
- Non-error entries of the table = Σ |FIRST(α)| (+ |FOLLOW(A)| for each nullable α).
- Ambiguous, left-recursive or non-left-factored grammars are never LL(1).
- A predictive parser needs no backtracking and runs in O(n) time; recursive descent with backtracking may take exponential time.
10 GATE-style MCQs for Top-Down Parsing (LL) with full explanations. Attempt each question before revealing the answer.
After eliminating left recursion from E → E + T | T, the grammar becomes:
Show answer & explanation
Answer: A
For A → A α | β the transformation is A → β A′ ; A′ → α A′ | ε, with α = + T and β = T.
The number of non-error (filled) entries in the LL(1) parse table of E → T E′ ; E′ → + T E′ | ε ; T → F T′ ; T′ → * F T′ | ε ; F → ( E ) | id is:
Show answer & explanation
Answer: C
E: 2 (id, ( ), E′: 3 (+ and ε on ), $), T: 2, T′: 4 (* and ε on +, ), $), F: 2. Total 13.
Which of the following grammars is LL(1)?
Show answer & explanation
Answer: B
A has a common prefix, C is ambiguous and left recursive, D is left recursive. B has disjoint selection sets: {a} for a S b and {b, $} for ε.
Grammar: S → i E t S S′ | a ; S′ → e S | ε ; E → b. The LL(1) parse table has a multiply-defined entry at:
Show answer & explanation
Answer: B
e ∈ FIRST(e S) and e ∈ FOLLOW(S′), so both S′ → e S and S′ → ε are placed in M[S′, e].
A recursive-descent parser written directly from the left-recursive grammar E → E + T | T will:
Show answer & explanation
Answer: C
The procedure E() calls E() first, repeating without progress.
The grammar A → α β1 | α β2 must be transformed before LL(1) parsing because:
Show answer & explanation
Answer: B
Both productions begin with α, so FIRST sets intersect; left factoring postpones the choice.
In an LL(1) table, the production A → ε is placed in M[A, b] for every b in:
Show answer & explanation
Answer: B
An ε-production is used when the lookahead can legally follow A, i.e. b ∈ FOLLOW(A) (including $).
For S → a S b | ε, the entries of the LL(1) table are:
Show answer & explanation
Answer: A
FIRST(a S b) = {a}; FOLLOW(S) = {b, $} for the ε-production.
Which one of the following statements about LL(1) grammars is FALSE?
Show answer & explanation
Answer: C
Unambiguity is necessary but not sufficient: E → E + T | T is unambiguous but left recursive, hence not LL(1).
The time taken by a table-driven LL(1) predictive parser on an input of n tokens is:
Show answer & explanation
Answer: B
Each stack operation either matches a token or expands a non-terminal in a way that is bounded by the derivation length, which is linear for an LL(1) grammar without ε-cycles.
Extra page-specific practice beyond the GATE samples. Attempt all 8, then reveal answers to check your reasoning.
Eliminate left recursion from A → A a | A b | c | d. The result is:
Show answer & explanation
Answer: A) A → c A′ | d A′ ; A′ → a A′ | b A′ | ε
Each non-left-recursive alternative β gets A′ appended; each left-recursive tail α goes into A′ → α A′ | ε.
For S → a A | b ; A → a A | ε, which statement is TRUE?
Show answer & explanation
Answer: B) It is LL(1) with M[A, $] = A → ε
FIRST(a A) = {a}, FOLLOW(A) = FOLLOW(S) = {$}. No cell clashes, so the grammar is LL(1).
The number of productions used to parse the string id with the LL(1) expression grammar is:
Show answer & explanation
Answer: C) 5
E → T E′, T → F T′, F → id, T′ → ε, E′ → ε: five productions.
Which production would be placed in M[T′, )] for the expression grammar?
Show answer & explanation
Answer: B) T′ → ε
) ∈ FOLLOW(T′) = { +, ), $ }, so the ε-production is selected.
A grammar is LL(1) iff its parse table:
Show answer & explanation
Answer: B) has at most one production in each entry
Multiply-defined entries are exactly the violations of the LL(1) conditions.
For the grammar S → A B ; A → a | ε ; B → b | ε, the table entry M[S, $] contains:
Show answer & explanation
Answer: A) S → A B
A B derives ε, so S → A B is placed under every terminal in FOLLOW(S) = {$} as well as under a and b.
The grammar S → a S a | b is:
Show answer & explanation
Answer: A) LL(1)
FIRST(a S a) = {a} and FIRST(b) = {b}: disjoint, so it is LL(1) (it generates aⁿ b aⁿ).
The stack content of a predictive parser initially is:
Show answer & explanation
Answer: C) $ S with S on top
The end marker is at the bottom and the start symbol is pushed above it.
Topic FAQs
Why can a top-down parser not handle left recursion?
Expanding A → A α produces A again as the leftmost symbol without consuming input, so the parser would expand A forever. The grammar must be rewritten into right-recursive form first.
What are the exact LL(1) conditions?
For every pair of alternatives A → α | β: FIRST(α) ∩ FIRST(β) = ∅, at most one of them derives ε, and if β ⇒* ε then FIRST(α) ∩ FOLLOW(A) = ∅. Equivalent: the parse table has no multiply-defined entry.
Does left factoring always make a grammar LL(1)?
No. Left factoring only removes common prefixes. The grammar may still be ambiguous (dangling else) or violate the FIRST/FOLLOW conditions.
How many entries does the LL(1) table of the expression grammar have?
Thirteen non-error entries: E has 2, E′ has 3, T has 2, T′ has 4 and F has 2.
Hover a card to reveal the answer with a 3D flip. On touch devices, tap the card or use Show answer.