Introduction
A top-down parser builds the parse tree from the root down, in preorder, and in doing so produces a leftmost derivation of the input. At each step it holds a leftmost nonterminal and must choose one of its productions. If the choice can be wrong, the parser has to back up and try another alternative (backtracking). If the next input token always determines the choice, the parser is predictive and runs in linear time.
The grammars for which one token of lookahead always suffices are the LL(1) grammars: input scanned from Left to right, Leftmost derivation, 1 token of lookahead. An LL(1) parser can be written by hand as a set of recursive procedures (recursive descent) or driven by a table and an explicit stack. Both use the same table, built from FIRST and FOLLOW sets.
Prerequisites
- FIRST and FOLLOW Sets: definitions and computation; the expression grammar and its sets are reused here.
- The grammar must first be free of left recursion and left-factored (see the note Grammar Transformations for Parsing).
Core concepts
| Term | Meaning |
|---|---|
| Recursive descent | One procedure per nonterminal; the procedure for chooses an -production and calls the procedures for its body |
| Backtracking | Trying alternatives in turn and undoing input consumption on failure; exponential in the worst case |
| Predictive parser | Recursive descent or table-driven parser that never backtracks, choosing by the lookahead |
| Parsing table | The production to use when is on top of the stack and is the next token; an empty entry means a syntax error |
| LL(1) grammar | A grammar whose table has at most one production in every entry |
The LL(1) conditions
A grammar is LL(1) if and only if, for every pair of distinct productions :
- (no terminal begins strings of both; this includes , so at most one of them is nullable);
- if , then (and symmetrically when is nullable).
Condition 2 is the one most often forgotten. It says: when the parser could choose the nullable alternative because the next token is something that may follow , that token must not also start the other alternative.
Algorithms
Building the table
BUILD-LL1-TABLE(G)
for each production A -> alpha
for each terminal a in FIRST(alpha) - {eps}
add A -> alpha to M[A, a]
if eps in FIRST(alpha)
for each b in FOLLOW(A) (b may be $)
add A -> alpha to M[A, b]
every entry still empty is an error entryThe grammar is LL(1) exactly when no entry receives two productions. Time: one FIRST-of-string per production and one FOLLOW lookup per nullable production, so linear in the grammar size times the number of terminals.
The table-driven parser
Pseudocode uses a stack whose top is written on the right, $ as the bottom marker and as the end of the input, and a for the current input token.
LL1-PARSE(M, S, input)
push $; push S
a <- first token of input$
loop
X <- top of stack
if X = $ and a = $ then accept
else if X is a terminal
if X = a then pop; a <- next token
else error
else if M[X, a] = X -> Y1 Y2 ... Yk
pop; push Yk, ..., Y2, Y1 (Y1 ends on top)
else errorEach step either matches and consumes a token or expands a nonterminal. For a fixed reduced LL(1) grammar, the total number of expansions in a successful parse is linear in the input length (with a grammar-dependent constant), so total time and stack space are O(n+1). Expansions between consecutive matches need not be bounded by a constant: one nullable cleanup burst can be linear in the input already consumed.
Recursive descent for the same grammar
PROC-E(): PROC-T(); PROC-E1()
PROC-E1(): if a = '+' then MATCH('+'); PROC-T(); PROC-E1()
else if a in { ')', '$' } then return (E' -> eps)
else error
PROC-T(): PROC-F(); PROC-T1()
PROC-T1(): if a = '*' then MATCH('*'); PROC-F(); PROC-T1()
else if a in { '+', ')', '$' } then return (T' -> eps)
else error
PROC-F(): if a = '(' then MATCH('('); PROC-E(); MATCH(')')
else if a = 'id' then MATCH('id')
else errorThe sets tested before returning on are exactly the FOLLOW sets; the call stack of the procedures plays the role of the explicit stack.
Worked examples
The table for the expression grammar
Grammar: E -> T E', E' -> + T E' | eps, T -> F T', T' -> * F T' | eps, F -> ( E ) | id. Using the FIRST and FOLLOW sets (FOLLOW(E') , FOLLOW(T') ):
id | + | * | ( | ) | $ | |
|---|---|---|---|---|---|---|
E -> T E' | E -> T E' | |||||
E' -> + T E' | E' -> eps | E' -> eps | ||||
T -> F T' | T -> F T' | |||||
T' -> eps | T' -> * F T' | T' -> eps | T' -> eps | |||
F -> id | F -> ( E ) |
13 of the 30 entries are filled and none has two productions, so the grammar is LL(1). The -entries sit exactly in the FOLLOW columns.
Parsing id + id * id
| Step | Stack (top at right) | Input | Action |
|---|---|---|---|
| 1 | $ E | id + id * id $ | E -> T E' |
| 2 | $ E' T | id + id * id $ | T -> F T' |
| 3 | $ E' T' F | id + id * id $ | F -> id |
| 4 | $ E' T' id | id + id * id $ | match id |
| 5 | $ E' T' | + id * id $ | T' -> eps |
| 6 | $ E' | + id * id $ | E' -> + T E' |
| 7 | $ E' T + | + id * id $ | match + |
| 8 | $ E' T | id * id $ | T -> F T' |
| 9 | $ E' T' F | id * id $ | F -> id |
| 10 | $ E' T' id | id * id $ | match id |
| 11 | $ E' T' | * id $ | T' -> * F T' |
| 12 | $ E' T' F * | * id $ | match * |
| 13 | $ E' T' F | id $ | F -> id |
| 14 | $ E' T' id | id $ | match id |
| 15 | $ E' T' | $ | T' -> eps |
| 16 | $ E' | $ | E' -> eps |
| 17 | $ | $ | accept |
There are 11 expansions (one per interior node of the parse tree, counting the -productions) and 5 matches (one per token), 16 moves before acceptance. The productions in the "Action" column, read top to bottom, are the leftmost derivation.

Detecting an error: id + * id
| Step | Stack (top at right) | Input | Action |
|---|---|---|---|
| 1 | $ E | id + * id $ | E -> T E' |
| 2 | $ E' T | id + * id $ | T -> F T' |
| 3 | $ E' T' F | id + * id $ | F -> id |
| 4 | $ E' T' id | id + * id $ | match id |
| 5 | $ E' T' | + * id $ | T' -> eps |
| 6 | $ E' | + * id $ | E' -> + T E' |
| 7 | $ E' T + | + * id $ | match + |
| 8 | $ E' T | * id $ | is empty: error |
The error is reported on *, the first token that cannot continue any sentence: id + is a prefix of valid input, id + * is not. This is the valid-prefix property of LL(1) parsing: the parser never consumes a token that cannot be part of a correct program. It may apply -expansions (step 5) before it notices an error, but it never shifts past one.
Is the grammar LL(1)?
| Grammar | LL(1)? | Reason |
|---|---|---|
S -> a S b | eps | Yes | FIRST() , FOLLOW() , disjoint |
S -> a B | a C, B -> b, C -> c | No | Both alternatives start with a (left-factor to fix) |
S -> a A | b, A -> c A | eps | Yes | FIRST() , FOLLOW() |
S -> A b, A -> b | eps | No | b is in FIRST() and in FOLLOW(): condition 2 fails |
S -> A | B, A -> a | eps, B -> b | eps | No | Both alternatives of are nullable; the grammar is ambiguous for the empty string |
S -> A a A b | B b B a, A -> eps, B -> eps | Yes | FIRST() , FIRST() |
S -> i E t S S' | a, S' -> e S | eps, E -> b | No | e is in FIRST() and in FOLLOW() |
Any left-recursive grammar, for example E -> E + T | T | No | FIRST() contains FIRST() |
S -> a S a | b S b | eps | No | FOLLOW() meets FIRST() and FIRST(): conflicts at and , although the grammar is unambiguous |
S -> ( S ) S | eps | Yes | FIRST() , FOLLOW() |
S -> a S b S | b S a S | eps | No | FOLLOW() ; conflicts at and , and the grammar is ambiguous (abab has two trees) |
S -> b S', S' -> a S' | eps | Yes | FIRST() , FOLLOW() |
The S -> i E t S S' row is the dangling-else grammar: the conflict is in . Hand-written parsers resolve it by always choosing , which attaches the else to the nearest if.
Important properties and observations
- LL(1) implies unambiguous. Each string has at most one leftmost derivation, because every step is forced. Hence an ambiguous grammar is never LL(1).
- LL(1) implies not left-recursive (for grammars without useless symbols). A left-recursive alternative shares FIRST with the alternative that ends the recursion.
- Language versus grammar. "LL(1)" is a property of a grammar. A language is called LL(1) if some LL(1) grammar generates it. Rewriting can turn a non-LL(1) grammar into an LL(1) one for the same language, but not always: some deterministic context-free languages have no LL() grammar for any .
- Relation to LR. Every LL(1) grammar is LR(1). It need not be SLR(1):
S -> A a A b | B b B a,A -> eps,B -> epsis LL(1) but its SLR(1) table has a reduce-reduce conflict, because FOLLOW() FOLLOW() . See the note CLR(1) and LALR(1) Parsing for the full picture. - Every regular language has an LL(1) grammar: the right-linear grammar read off a DFA has one production per state and symbol, so the next token always decides.
- Error recovery. In panic mode the parser skips input until a token in a synchronising set, typically FOLLOW of the nonterminal on top of the stack, then pops that nonterminal and continues.
Common misconceptions.
- "A grammar with -productions cannot be LL(1)." It can; the expression grammar above has two.
- "Left factoring and left-recursion removal always give LL(1)." They are necessary steps, not a guarantee; the dangling-else grammar stays non-LL(1).
- "The number of parsing steps equals the number of tokens." It equals expansions plus matches; expansions include every -production applied.
- "An unambiguous grammar without left recursion is LL(1)."
S -> a S a | b S b | epsis both, and still has two conflicts: the parser cannot tell where the middle of the string is. - "An empty table entry means the grammar is not LL(1)." Empty entries are error entries; only an entry with two productions is a conflict.
GATE examination perspective
- Fill a table entry. contains if , or if is nullable and . Questions ask for the entries of one row or for which entries hold -productions.
- Decide LL(1). Check both conditions for every pair of alternatives. A conflict through FOLLOW (condition 2) is the classic trap.
- Count moves or the stack contents. At a given step of the trace; keep the top of the stack at a consistent end and push bodies in reverse.
- Grammar classes. LL(1) grammars are unambiguous, not left-recursive, and LR(1). Not every LL(1) grammar is SLR(1) or LR(0).
- Recursive descent. Questions may show recursive procedures and ask which grammar they parse, or which input causes an infinite loop (left recursion).
- Where is the error detected? Trace until an empty entry or a terminal mismatch; by the valid-prefix property, the offending token is the first one that cannot extend a valid prefix.
- Nullable alternatives check against FOLLOW of the head. For , the test uses FOLLOW(), which contains
abecause of the inner in .
Summary
Sources
- A. V. Aho, M. S. Lam, R. Sethi, J. D. Ullman, Compilers, Principles, Techniques, and Tools, 2nd ed., Pearson Addison-Wesley, 2007: Section 2.4 (top-down and predictive parsing, left recursion), Section 4.4.1 (recursive-descent parsing), Section 4.4.3 (LL(1) grammars and the parsing-table construction), Section 4.4.4 (nonrecursive predictive parsing), Section 4.4.5 (error recovery in predictive parsing).