FIRST and FOLLOW sets
Nullable symbols, FIRST of a symbol and of a string, FOLLOW of a nonterminal with the end marker, the rules and the fixed-point algorithm that computes them, worked by hand and animated pass by pass, and how they decide whether a grammar is LL(1).
Why it exists
A top-down parser standing at a nonterminal with several productions must pick one by looking at the next input token. A bottom-up parser must decide when to reduce. Both need to know which tokens can start what is derived from a symbol, and which tokens can come right after a symbol. Those two questions are answered by FIRST and FOLLOW.
Prerequisites
You need a grammar in a form suitable for parsing (ambiguity, left recursion and left factoring); the examples use the expression grammar after left recursion has been removed. Terminals are the tokens, nonterminals are the grammar variables, is the empty string and is the end-of-input marker.
Precise definitions
Nullable. A nonterminal is nullable if . A terminal is never nullable. A string of symbols is nullable if every symbol in it is.
FIRST() for a string of grammar symbols: the set of terminals that can begin some string derived from ; if , then as well.
Rules:
- If is a terminal, .
- If is a production, add to .
- If : add to ; if is nullable also add ; continue while the symbols so far are nullable; if all of are nullable, add .
- For a string, is built the same way (stop at the first symbol that is not nullable).
FOLLOW() for a nonterminal : the set of terminals that can appear immediately to the right of in some sentential form (that is, ), plus if can be the last symbol of a sentential form. FOLLOW is defined only for nonterminals and never contains .
Rules:
- Put in for the start symbol .
- For a production : add to .
- For a production , or with : add to .
Algorithm. The sets depend on each other (rule 3 of FOLLOW copies a whole set), so apply the rules repeatedly, pass after pass over all productions, until a complete pass changes nothing. This is a fixed-point computation; it terminates because the sets only grow and are bounded by the number of terminals.
Use in LL(1) parsing. For a production define the set of lookahead tokens for which it is chosen:
A grammar is LL(1) exactly when, for every nonterminal , the SELECT sets of its productions are pairwise disjoint. For this is the condition: ; at most one of derives ; and if then . The table construction is on the LL(1) page; SLR parsing uses FOLLOW as well (SLR).
How it works
Each animation runs the passes. A step happens only when a production adds something; the highlighted production is the one applied and the changed cell is shaded. First the FIRST sets of the expression grammar. Notice that , and all end up with the same FIRST set, since each of them begins with a factor.
FIRST sets of the expression grammar: start
Computed by a script, not typed- 1E → T E'
- 2E' → + T E'
- 3E' → ε
- 4T → F T'
- 5T' → * F T'
- 6T' → ε
- 7F → ( E )
- 8F → id
| symbol | FIRST |
|---|---|
| E | { } |
| E' | { } |
| T | { } |
| T' | { } |
| F | { } |
Step 0
Every FIRST set starts empty. A pass looks at every production A → X1 X2 ... and adds FIRST(X1); if X1 can vanish it also adds FIRST(X2), and so on; if the whole right side can vanish, ε is added too. Passes repeat until nothing changes.
Now FOLLOW of the same grammar. FIRST is shown for reference. The first step puts in (rule 1); then, for example, puts ) in FOLLOW(), because ) comes right after there.
FOLLOW sets of the expression grammar: start
Computed by a script, not typed- 1E → T E'
- 2E' → + T E'
- 3E' → ε
- 4T → F T'
- 5T' → * F T'
- 6T' → ε
- 7F → ( E )
- 8F → id
| symbol | FIRST | FOLLOW |
|---|---|---|
| E | {(, id} | {$} |
| E' | {+, ε} | { } |
| T | {(, id} | { } |
| T' | {*, ε} | { } |
| F | {(, id} | { } |
Step 0
FIRST is already known. FOLLOW(E) starts as {$} because the end marker follows the start symbol; all other FOLLOW sets are empty. For each production A → α B β: add FIRST(β) minus ε to FOLLOW(B), and if β can vanish (or is empty) add all of FOLLOW(A) to FOLLOW(B).
A grammar with nullable symbols is more interesting, because FIRST must look past a nullable symbol and FOLLOW must pass sets along. The nullable column is computed too.
FIRST sets of a grammar with empty productions: start
Computed by a script, not typed- 1S → A C B
- 2S → C b b
- 3S → B a
- 4A → d a
- 5A → B C
- 6B → g
- 7B → ε
- 8C → h
- 9C → ε
| symbol | nullable | FIRST |
|---|---|---|
| S | yes | { } |
| A | yes | { } |
| B | yes | { } |
| C | yes | { } |
Step 0
The nullable column is worked out first: a nonterminal is nullable when some production has a right side made only of nullable symbols (an empty right side counts). Every FIRST set starts empty. A pass looks at every production A → X1 X2 ... and adds FIRST(X1); if X1 can vanish it also adds FIRST(X2), and so on; if the whole right side can vanish, ε is added too. Passes repeat until nothing changes.
FOLLOW sets of a grammar with empty productions: start
Computed by a script, not typed- 1S → A C B
- 2S → C b b
- 3S → B a
- 4A → d a
- 5A → B C
- 6B → g
- 7B → ε
- 8C → h
- 9C → ε
| symbol | FIRST | FOLLOW |
|---|---|---|
| S | {b, a, d, g, h, ε} | {$} |
| A | {d, g, h, ε} | { } |
| B | {g, ε} | { } |
| C | {h, ε} | { } |
Step 0
FIRST is already known. FOLLOW(S) starts as {$} because the end marker follows the start symbol; all other FOLLOW sets are empty. For each production A → α B β: add FIRST(β) minus ε to FOLLOW(B), and if β can vanish (or is empty) add all of FOLLOW(A) to FOLLOW(B).
Worked examples
EX 1The expression grammar
EasyFor , , , , , the final sets (from the animations above) are
| FIRST | FOLLOW | |
|---|---|---|
{ (, id } | { ), $ } | |
{ +, ε } | { ), $ } | |
{ (, id } | { +, ), $ } | |
{ *, ε } | { +, ), $ } | |
{ (, id } | { +, *, ), $ } |
Check one by hand: in the symbol is followed by , so FIRST() minus ε = {+} goes into FOLLOW(); because is nullable, FOLLOW() = {), $} goes in as well. That gives FOLLOW() = {+, ), $}.
EX 2A small nullable grammar
Easy, , . FIRST() = {a, ε}. FIRST() = {b, c}. is nullable, so FIRST() = FIRST()∖ε ∪ FIRST() = {a, b, c}, with no ε because is not nullable. FOLLOW() = {$}. In , is followed by : FOLLOW() = FIRST() = {b, c}. is last in and in : FOLLOW() = FOLLOW() = {$}. This grammar is LL(1) (checked).
EX 3Nullable symbols in the middle
MediumThe grammar of the second pair of animations is , , , . Nullable: and (directly), then (since ), then (since ). FIRST() = {g, ε}, FIRST() = {h, ε}, FIRST() = {d} ∪ (FIRST()∖ε) ∪ (FIRST()∖ε) ∪ {ε} = {d, g, h, ε}. FIRST(): from all of {d, g, h} and, as everything is nullable, ε; from add {h, b}; from add {g, a}: {a, b, d, g, h, ε}. FOLLOW: FOLLOW() = {$}; FOLLOW() = FIRST()∖ε ∪ FOLLOW() = {h, g, $}; FOLLOW() gets {a} from , {$} from (end), and {h, g} from … together {a, g, h, $}; FOLLOW() = {b, g, h, $}. These agree with the animation and with the independent checks.
EX 4Deciding LL(1) with the sets
Medium(a) . FIRST() = {a}; the ε-production is chosen on FOLLOW() = {b, $}. {a} and {b, $} are disjoint: LL(1). (b) . Now FOLLOW() = {a, $}, which meets {a}: at lookahead a the parser cannot tell whether to expand with or to take ε (this is a nullable-versus-FOLLOW conflict): not LL(1). (c) : both alternatives have FIRST = {a}: not LL(1) (left factor it). (d) , : FIRST() = {b, a} and FIRST() = {b}: they share , so not LL(1), even though no alternative looks like the other. All four verdicts were confirmed by the independent enumeration check.
EX 5A set-valued question
GATE levelFor : is nullable and FIRST() = {(, ε}. FOLLOW() starts with $, and from the symbol is followed by ), and from the last in the follow set of is inherited (a self-reference that adds nothing new). So FOLLOW() = {), $}. The grammar is LL(1): the first production is selected on {(}, the ε-production on {), $}. Typical questions ask "what is FOLLOW of ...", "which terminal is in FIRST of ... but not in FOLLOW", or "how many elements are in FIRST()"; the rule is to apply the three FOLLOW rules once per production and iterate until nothing grows.
Cheat box
Cheat box
- FIRST(): terminals that can start a string derived from , plus ε if can vanish. FOLLOW(): terminals that can come right after , plus
$if can end the input. ε is never in FOLLOW. - FOLLOW rules:
$in FOLLOW(start); gives FIRST()∖ε into FOLLOW(); if nullable or empty, FOLLOW() into FOLLOW(). - FIRST of a string: take symbols left to right while they are nullable.
- Repeat passes until nothing changes.
- LL(1): the SELECT sets of the productions of each nonterminal are disjoint (FIRST/FIRST, and FIRST/FOLLOW if nullable).
- FOLLOW of the start symbol always contains
$.
Traps and GATE patterns
Connections
Across subjects
No cross-subject links yet.
The grammar must first be made suitable by removing left recursion and factoring. FIRST and FOLLOW feed the table of the LL(1) parser and the reduce entries of the SLR parser; LR(1) and LALR(1) use more precise lookahead sets instead (CLR and LALR).
PYQs
5 previous year questions from your book. I re-solved each one myself (4 by running or simulating, 1 by reasoning only); the book's key is shown next to my answer. All PYQs
Which of the following statement(s) is/are TRUE while computing First and Follow during top-down parsing by a compiler?
- (a)For a production A→ε, ε epsilon will be added to First(A)
- (b)If there is any input right end marker, it will be added to First(S), where S is the start symbol.
- (c)For a production A→ε, ε epsilon will be added to Follow(A).
- (d)If there is any input right end marker, it will be added to Follow(S), where S is the start symbol. COMPILER DESIGN
Consider the following grammar G, with S as the
start symbol. The grammar G has three incomplete
productions denoted by (1), (2), and (3):
S → daT | (1)
T → aS | bT | (2)
R → (3) | ε
The set of terminals is {a, b, c, d, f}. The FIRST
and FOLLOW sets of the different non-terminals
are as follows:
FIRST(S) = {c, d, f}, FIRST(T) = {a, b, ε},
FIRST(R) = {c, ε}
FOLLOW(S) = FOLLOW(T) = {c, f, $},
FOLLOW(R) = {f}- (a)(1) S→Rf (2) T→ε (3) R→cTR
- (b)(1) S→fR (2) T→ε (3) R→cTR
- (c)(1) S→fR (2) T→cT (3) R→cR
- (d)(1) S→Rf (2) T→cT (3) R→cR
Consider the following context-free grammar where
the set of terminals is {a,b,c,d,f}.
S→daT|Rf
T→aS|baT|ε
R→caTR|ε
The following is a partially-filled LL(1) parsing table.
a
b
c
d
f
$
S
(1)
S →da T
(2)
T
T →a S
T →ba T
(3)
T →ε
(4)
R
R →caTR
R →ε
Which one of the following choices represents the
correct combination for the numbered cells in the
parsing table ("blank" denotes that the corresponding
cell is empty)?
COMPILER DESIGN- (a)(1) S →Rf (2) S →Rf (3) T →ε (4) T →ε
- (b)(1) blank (2) S →R (3) T →ε (4) T →ε
- (c)(1) S →Rf (2) blank (3) blank (4) T →ε
- (d)(1) blank (2) S →Rf (3) blank (4) blank
Consider the following grammar:
P→xQRS
Q→yz|z
R→w|ε
S→y
What is FOLLOW(Q)?- (a){R}
- (b){w}
- (c){w, y}
- (d){w, $}
For the grammar below, a partial LL(1) parsing table
is also presented along with the grammar. Entries that
need to be filled are indicated as E1, E2 and E3. ε is
the empty string, $ indicates end of input, and |
separates alternate right hand sides of productions.
S → a A b B | b A a B | ε
A → S
B → S
a
b
$
S
E 1
E 2
S →ε
A
A → S
A → S
error
B
B →S
B →S
E3
The FIRST and FOLLOW sets for the non-terminals
A and B are:-
FIRST(A) = {a, b, ε} = FIRST(B)- (a)FOLLOW( A) = {a, b} FOLLOW(B) = {a, b, $}
- (b)FIRST(A) = {a, b, ε} = FIRST(B) FOLLOW(A) = {a, b} FOLLOW(B) = ∅ COMPILER DESIGN
- (c)FIRST(A) = {a, b, $} FIRST(B) = {a, b, ε} FOLLOW(A) = {a, b} FOLLOW(B) = {$}
- (d)FIRST(A) = {a, b} = FIRST(B) FOLLOW(A) = {a, b} FOLLOW(B) = {a, b}
Self-check
Question 1
For give FIRST() and FOLLOW().
Show answerHide
FIRST() = {a, ε}; FOLLOW() = {b, $}.
Question 2
Can ε be an element of a FOLLOW set?
Show answerHide
No. FOLLOW contains terminals and the end marker $ only.
Question 3
, , . What is FIRST()?
Show answerHide
{b, c}: FIRST()∖ε = {b}, and since is nullable FIRST() = {c} is added; is not nullable so ε is not added.
Question 4
Why does with break LL(1)?
Show answerHide
On lookahead both (FIRST) and (FOLLOW) are selected: two entries in one table cell.
Revision card
One-screen revision
- FIRST: starting terminals (ε if nullable). FOLLOW: terminals after the symbol,
$for the start symbol; never ε. - FOLLOW rules:
$for start; FIRST()∖ε into FOLLOW() for ; FOLLOW() into FOLLOW() when vanishes or is empty. - Iterate passes to a fixed point.
- LL(1) iff the SELECT sets of every nonterminal's productions are disjoint.
References
- ·Aho, Lam, Sethi, Ullman, Compilers: Principles, Techniques, and Tools (2nd edition), Chapter 4 Syntax Analysis (4.4.2 FIRST and FOLLOW)
- ·Cooper and Torczon, Engineering a Compiler, Chapter 3 Parsers (computing FIRST and FOLLOW sets)
Verification
Every set shown or quoted was computed by the fixed-point algorithm (scripts/cd_lib.py) and recomputed by a different method, from the language itself: all terminal strings up to length 7 derived from each nonterminal give FIRST and nullability, and all sentential forms up to length 9 give FOLLOW (scripts/enum-checks/cd_grammar.py); tests/cd-traces.test.ts recomputes the final tables of the four animations in TypeScript. The conventions (ε is never in a FOLLOW set; $ follows the start symbol) are those of the Dragon book. SECOND PASS (2026-11-02, done by the assistant, not a human check): every Self-check question was solved before the printed answer was read and all agreed; the definitions, worked examples, cheat box and traps were read claim by claim and the numbers re-derived by hand or by a small script (token counts, FIRST and FOLLOW sets, LL(1) tables and moves, LR item sets, state counts and parses, operator-precedence values, attribute evaluation and printing schemes, activation-record counts and access links, parameter-passing results, three-address code, jumping and backpatched code, leaders and blocks, value numbering and DAGs, gen/kill and data flow sets, Sethi-Ullman labels and live ranges); long C programs were not re-read line by line (they are compiled and compared by npm run verify:c, and their printed outputs were checked against the prose); tables and animations are machine output and were read, not re-run; No other defect found. Status raised to self-checked; verified needs a textbook or human check.
last verified: never
My notes (saved in this browser)