Skip to main content

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.

~4 hrs ~0.5–1 marks · High 10 samples · 8 practice · 10 cards

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 XLookahead aAction
X = a = $sameaccept
terminal X = asamepop X, advance input
terminal X ≠ adifferentsyntax error
non-terminal XM[X, a] = X → Y1…Ykpop X, push Yk … Y1 (Y1 on top)
non-terminal XM[X, a] emptysyntax 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.

Procedures E, E-prime and T with the call tree produced while parsing id + id by recursive descent
Recursive descent on id + id: five procedure calls and three token matches follow a leftmost derivation.

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
ProblemBeforeAfterWhy it is needed
Immediate left recursionA → A α | βA → β A′ ; A′ → α A′ | εrecursive descent would loop
Common prefixA → α β1 | α β2A → α A′ ; A′ → β1 | β2FIRST sets of alternatives overlap
Indirect left recursionS → A a ; A → S b | csubstitute S into A, then remove immediate recursionA ⇒ 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.

Two panels showing the rewrite of A -> A alpha | beta and the left factoring of A -> alpha beta1 | alpha beta2, each with an example
Left recursion elimination and left factoring: the two grammar clean-ups needed before LL(1) parsing.

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+*()$
EE → T E′E → T E′
E′E′ → + T E′E′ → εE′ → ε
TT → F T′T → F T′
T′T′ → εT′ → * F T′T′ → εT′ → ε
FF → idF → ( 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).

LL(1) predictive parse table for the expression grammar with FIRST based cells, epsilon cells and 13 filled entries
LL(1) table of the expression grammar: 13 of the 30 cells are filled; blue cells come from FIRST, yellow epsilon cells from FOLLOW.

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)InputAction
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)”.
ConflictExampleDoubly defined cellCure
FIRST/FIRSTS → a S | aM[S, a]left factor
FIRST/FOLLOWS → A a ; A → a | εM[A, a]: A → a and A → εrewrite grammar
FIRST/FIRST via nullableS → A a | b ; A → b | εM[S, b]: S → A a and S → bsubstitute A
Left recursionE → E + T | Trow E, columns of FIRST(T)eliminate recursion
AmbiguityS′ → 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.
GrammarLL(1)?Reason
S → aSb | εYesdisjoint selection sets
S → aS | aNocommon prefix a
E → E + T | TNoleft recursive
E → T E′ ; E′ → + T E′ | εYes (with T LL(1))right recursive, factored
S → iEtS | iEtSeS | aNocommon prefix / ambiguous
S → AaAb | BbBa ; A, B → εYesFIRST = {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.
Exam pattern · 10 questions

GATE sample questions & answers

10 GATE-style MCQs for Top-Down Parsing (LL) with full explanations. Attempt each question before revealing the answer.

Q1

After eliminating left recursion from E → E + T | T, the grammar becomes:

  • A) E → T E′ ; E′ → + T E′ | ε
  • B) E → E′ T ; E′ → + E′ | ε
  • C) E → T + E | T
  • D) E → T E′ ; E′ → E′ + T | ε
Show answer & explanation

Answer: A

For A → A α | β the transformation is A → β A′ ; A′ → α A′ | ε, with α = + T and β = T.

Q2 Table counting

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:

  • A) 11
  • B) 12
  • C) 13
  • D) 14
Show answer & explanation

Answer: C

E: 2 (id, ( ), E′: 3 (+ and ε on ), $), T: 2, T′: 4 (* and ε on +, ), $), F: 2. Total 13.

Q3

Which of the following grammars is LL(1)?

  • A) S → a S | a
  • B) S → a S b | ε
  • C) S → S S | a
  • D) E → E + T | T
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 ε.

Q4

Grammar: S → i E t S S′ | a ; S′ → e S | ε ; E → b. The LL(1) parse table has a multiply-defined entry at:

  • A) M[S, i]
  • B) M[S′, e]
  • C) M[E, b]
  • D) M[S′, $]
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].

Q5

A recursive-descent parser written directly from the left-recursive grammar E → E + T | T will:

  • A) accept all valid strings
  • B) reject all strings
  • C) loop infinitely without consuming input
  • D) require two tokens of lookahead
Show answer & explanation

Answer: C

The procedure E() calls E() first, repeating without progress.

Q6

The grammar A → α β1 | α β2 must be transformed before LL(1) parsing because:

  • A) it is ambiguous
  • B) it has common prefixes, so one lookahead cannot choose a production
  • C) it is left recursive
  • D) it has useless symbols
Show answer & explanation

Answer: B

Both productions begin with α, so FIRST sets intersect; left factoring postpones the choice.

Q7

In an LL(1) table, the production A → ε is placed in M[A, b] for every b in:

  • A) FIRST(A)
  • B) FOLLOW(A)
  • C) FIRST(A) ∪ FOLLOW(A)
  • D) the whole alphabet
Show answer & explanation

Answer: B

An ε-production is used when the lookahead can legally follow A, i.e. b ∈ FOLLOW(A) (including $).

Q8

For S → a S b | ε, the entries of the LL(1) table are:

  • A) M[S,a] = aSb ; M[S,b] = ε ; M[S,$] = ε
  • B) M[S,a] = aSb ; M[S,b] = aSb ; M[S,$] = ε
  • C) M[S,a] = ε ; M[S,b] = aSb
  • D) M[S,a] = aSb ; M[S,$] = ε only
Show answer & explanation

Answer: A

FIRST(a S b) = {a}; FOLLOW(S) = {b, $} for the ε-production.

Q9

Which one of the following statements about LL(1) grammars is FALSE?

  • A) Every LL(1) grammar is unambiguous
  • B) No LL(1) grammar is left recursive
  • C) Every unambiguous grammar is LL(1)
  • D) Every LL(1) grammar is LR(1)
Show answer & explanation

Answer: C

Unambiguity is necessary but not sufficient: E → E + T | T is unambiguous but left recursive, hence not LL(1).

Q10

The time taken by a table-driven LL(1) predictive parser on an input of n tokens is:

  • A) O(log n)
  • B) O(n)
  • C) O(n²)
  • D) O(2ⁿ)
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.

Topic drill · 8 questions

8 practice questions — Top-Down Parsing (LL)

Extra page-specific practice beyond the GATE samples. Attempt all 8, then reveal answers to check your reasoning.

P1

Eliminate left recursion from A → A a | A b | c | d. The result is:

  • A) A → c A′ | d A′ ; A′ → a A′ | b A′ | ε
  • B) A → A′ c | A′ d ; A′ → a | b
  • C) A → c A | d A | ε
  • D) A → c | d ; A′ → a A′ | b A′
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′ | ε.

P2

For S → a A | b ; A → a A | ε, which statement is TRUE?

  • A) It is not LL(1)
  • B) It is LL(1) with M[A, $] = A → ε
  • C) It is ambiguous
  • D) It is left recursive
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).

P3

The number of productions used to parse the string id with the LL(1) expression grammar is:

  • A) 3
  • B) 4
  • C) 5
  • D) 6
Show answer & explanation

Answer: C) 5

E → T E′, T → F T′, F → id, T′ → ε, E′ → ε: five productions.

P4

Which production would be placed in M[T′, )] for the expression grammar?

  • A) T′ → * F T′
  • B) T′ → ε
  • C) T′ → F
  • D) no production
Show answer & explanation

Answer: B) T′ → ε

) ∈ FOLLOW(T′) = { +, ), $ }, so the ε-production is selected.

P5

A grammar is LL(1) iff its parse table:

  • A) has no empty entries
  • B) has at most one production in each entry
  • C) has an ε-production
  • D) is left recursive
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.

P6

For the grammar S → A B ; A → a | ε ; B → b | ε, the table entry M[S, $] contains:

  • A) S → A B
  • B) nothing
  • C) S → ε
  • D) A → ε
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.

P7

The grammar S → a S a | b is:

  • A) LL(1)
  • B) not LL(1) because it is ambiguous
  • C) not LL(1) because of left recursion
  • D) an LL(2) grammar only
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ⁿ).

P8

The stack content of a predictive parser initially is:

  • A) S
  • B) $
  • C) $ S with S on top
  • D) empty
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.

Quick revision · 10 cards

10 flashcards — Top-Down Parsing (LL)

Hover a card to reveal the answer with a 3D flip. On touch devices, tap the card or use Show answer.

Q1 Which derivation does a top-down parser construct?
Q2 Rewrite A → A α | β without left recursion.
Q3 Rewrite A → α β1 | α β2 by left factoring.
Q4 When is a grammar LL(1)?
Q5 Where is an ε-production A → ε placed in an LL(1) table?
Q6 What are the three data structures of a table-driven predictive parser?
Q7 Can an ambiguous grammar be LL(1)?
Q8 What problem does left factoring solve?
Q9 What happens when a recursive-descent parser meets left recursion?
Q10 Which error recovery method is common in predictive parsing?