G27
Self-checkedTier 1 10 min readcd.first-follow

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 AA 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, ε\varepsilon is the empty string and \mathdollar\mathdollar is the end-of-input marker.

Precise definitions

Nullable. A nonterminal AA is nullable if A⇒∗εA \Rightarrow^{*} \varepsilon. A terminal is never nullable. A string of symbols is nullable if every symbol in it is.

FIRST(α\alpha) for a string α\alpha of grammar symbols: the set of terminals that can begin some string derived from α\alpha; if α⇒∗ε\alpha \Rightarrow^{*} \varepsilon, then ε∈FIRST(α)\varepsilon \in \text{FIRST}(\alpha) as well.

Rules:

  1. If XX is a terminal, FIRST(X)={X}\text{FIRST}(X) = \{X\}.
  2. If X→εX \to \varepsilon is a production, add ε\varepsilon to FIRST(X)\text{FIRST}(X).
  3. If X→Y1Y2⋯YkX \to Y_1 Y_2 \cdots Y_k: add FIRST(Y1)∖{ε}\text{FIRST}(Y_1) \setminus \{\varepsilon\} to FIRST(X)\text{FIRST}(X); if Y1Y_1 is nullable also add FIRST(Y2)∖{ε}\text{FIRST}(Y_2) \setminus \{\varepsilon\}; continue while the symbols so far are nullable; if all of Y1,…,YkY_1, \dots, Y_k are nullable, add ε\varepsilon.
  4. For a string, FIRST(X1X2⋯Xn)\text{FIRST}(X_1 X_2 \cdots X_n) is built the same way (stop at the first symbol that is not nullable).

FOLLOW(AA) for a nonterminal AA: the set of terminals aa that can appear immediately to the right of AA in some sentential form (that is, S⇒∗αAaβS \Rightarrow^{*} \alpha A a \beta), plus \mathdollar\mathdollar if AA can be the last symbol of a sentential form. FOLLOW is defined only for nonterminals and never contains ε\varepsilon.

Rules:

  1. Put \mathdollar\mathdollar in FOLLOW(S)\text{FOLLOW}(S) for the start symbol SS.
  2. For a production A→αBβA \to \alpha B \beta: add FIRST(β)∖{ε}\text{FIRST}(\beta) \setminus \{\varepsilon\} to FOLLOW(B)\text{FOLLOW}(B).
  3. For a production A→αBA \to \alpha B, or A→αBβA \to \alpha B \beta with ε∈FIRST(β)\varepsilon \in \text{FIRST}(\beta): add FOLLOW(A)\text{FOLLOW}(A) to FOLLOW(B)\text{FOLLOW}(B).

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 A→αA \to \alpha define the set of lookahead tokens for which it is chosen:

SELECT(A→α)={FIRST(α)if ε∉FIRST(α)(FIRST(α)∖{ε})∪FOLLOW(A)if ε∈FIRST(α)\text{SELECT}(A \to \alpha) = \begin{cases} \text{FIRST}(\alpha) & \text{if } \varepsilon \notin \text{FIRST}(\alpha)\\ (\text{FIRST}(\alpha) \setminus \{\varepsilon\}) \cup \text{FOLLOW}(A) & \text{if } \varepsilon \in \text{FIRST}(\alpha) \end{cases}

A grammar is LL(1) exactly when, for every nonterminal AA, the SELECT sets of its productions are pairwise disjoint. For A→α∣βA \to \alpha \mid \beta this is the condition: FIRST(α)∩FIRST(β)=∅\text{FIRST}(\alpha) \cap \text{FIRST}(\beta) = \emptyset; at most one of α,β\alpha, \beta derives ε\varepsilon; and if β⇒∗ε\beta \Rightarrow^{*} \varepsilon then FIRST(α)∩FOLLOW(A)=∅\text{FIRST}(\alpha) \cap \text{FOLLOW}(A) = \emptyset. 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 EE, TT and FF 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
  1. 1E → T E'
  2. 2E' → + T E'
  3. 3E' → ε
  4. 4T → F T'
  5. 5T' → * F T'
  6. 6T' → ε
  7. 7F → ( E )
  8. 8F → id
symbolFIRST
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.

0 / 9
computed by script: the algorithm is executed, one move per step. python3 scripts/gen-cd-traces.py

Now FOLLOW of the same grammar. FIRST is shown for reference. The first step puts \mathdollar\mathdollar in FOLLOW(E)\text{FOLLOW}(E) (rule 1); then, for example, F→(E)F \to ( E ) puts ) in FOLLOW(EE), because ) comes right after EE there.

FOLLOW sets of the expression grammar: start

Computed by a script, not typed
  1. 1E → T E'
  2. 2E' → + T E'
  3. 3E' → ε
  4. 4T → F T'
  5. 5T' → * F T'
  6. 6T' → ε
  7. 7F → ( E )
  8. 8F → id
symbolFIRSTFOLLOW
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).

0 / 10
computed by script: the algorithm is executed, one move per step. python3 scripts/gen-cd-traces.py

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
  1. 1S → A C B
  2. 2S → C b b
  3. 3S → B a
  4. 4A → d a
  5. 5A → B C
  6. 6B → g
  7. 7B → ε
  8. 8C → h
  9. 9C → ε
symbolnullableFIRST
Syes{ }
Ayes{ }
Byes{ }
Cyes{ }

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.

0 / 11
computed by script: the algorithm is executed, one move per step. python3 scripts/gen-cd-traces.py

FOLLOW sets of a grammar with empty productions: start

Computed by a script, not typed
  1. 1S → A C B
  2. 2S → C b b
  3. 3S → B a
  4. 4A → d a
  5. 5A → B C
  6. 6B → g
  7. 7B → ε
  8. 8C → h
  9. 9C → ε
symbolFIRSTFOLLOW
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).

0 / 8
computed by script: the algorithm is executed, one move per step. python3 scripts/gen-cd-traces.py

Worked examples

EX 1The expression grammar

Easy

For E→TE′E \to T E', E′→+TE′∣εE' \to + T E' \mid \varepsilon, T→FT′T \to F T', T′→∗FT′∣εT' \to * F T' \mid \varepsilon, F→(E)∣idF \to (E) \mid \text{id}, the final sets (from the animations above) are

FIRSTFOLLOW
EE{ (, id }{ ), $ }
E′E'{ +, ε }{ ), $ }
TT{ (, id }{ +, ), $ }
T′T'{ *, ε }{ +, ), $ }
FF{ (, id }{ +, *, ), $ }

Check one by hand: in E′→+TE′E' \to + T E' the symbol TT is followed by E′E', so FIRST(E′E') minus ε = {+} goes into FOLLOW(TT); because E′E' is nullable, FOLLOW(E′E') = {), $} goes in as well. That gives FOLLOW(TT) = {+, ), $}.

EX 2A small nullable grammar

Easy

S→ABS \to A B, A→aA∣εA \to a A \mid \varepsilon, B→bB∣cB \to b B \mid c. FIRST(AA) = {a, ε}. FIRST(BB) = {b, c}. AA is nullable, so FIRST(SS) = FIRST(AA)∖ε ∪ FIRST(BB) = {a, b, c}, with no ε because BB is not nullable. FOLLOW(SS) = {$}. In S→ABS \to A B, AA is followed by BB: FOLLOW(AA) = FIRST(BB) = {b, c}. BB is last in S→ABS \to A B and in B→bBB \to b B: FOLLOW(BB) = FOLLOW(SS) = {$}. This grammar is LL(1) (checked).

EX 3Nullable symbols in the middle

Medium

The grammar of the second pair of animations is S→ACB∣Cbb∣BaS \to A C B \mid C b b \mid B a, A→da∣BCA \to d a \mid B C, B→g∣εB \to g \mid \varepsilon, C→h∣εC \to h \mid \varepsilon. Nullable: BB and CC (directly), then AA (since A→BCA \to B C), then SS (since S→ACBS \to A C B). FIRST(BB) = {g, ε}, FIRST(CC) = {h, ε}, FIRST(AA) = {d} ∪ (FIRST(BB)∖ε) ∪ (FIRST(CC)∖ε) ∪ {ε} = {d, g, h, ε}. FIRST(SS): from ACBA C B all of {d, g, h} and, as everything is nullable, ε; from CbbC b b add {h, b}; from BaB a add {g, a}: {a, b, d, g, h, ε}. FOLLOW: FOLLOW(SS) = {$}; FOLLOW(AA) = FIRST(CBC B)∖ε ∪ FOLLOW(SS) = {h, g, $}; FOLLOW(BB) gets {a} from BaB a, {$} from S→ACBS \to A C B (end), and {h, g} from A→BCA \to B C … together {a, g, h, $}; FOLLOW(CC) = {b, g, h, $}. These agree with the animation and with the independent checks.

EX 4Deciding LL(1) with the sets

Medium

(a) S→aSb∣εS \to a S b \mid \varepsilon. FIRST(aSba S b) = {a}; the ε-production is chosen on FOLLOW(SS) = {b, $}. {a} and {b, $} are disjoint: LL(1). (b) S→aSa∣εS \to a S a \mid \varepsilon. Now FOLLOW(SS) = {a, $}, which meets {a}: at lookahead a the parser cannot tell whether to expand with aSaa S a or to take ε (this is a nullable-versus-FOLLOW conflict): not LL(1). (c) A→ab∣acA \to a b \mid a c: both alternatives have FIRST = {a}: not LL(1) (left factor it). (d) S→Aa∣bS \to A a \mid b, A→b∣εA \to b \mid \varepsilon: FIRST(AaA a) = {b, a} and FIRST(bb) = {b}: they share bb, 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 level

For S→(S)S∣εS \to ( S ) S \mid \varepsilon: SS is nullable and FIRST(SS) = {(, ε}. FOLLOW(SS) starts with $, and from (S)( S ) the symbol SS is followed by ), and from the last SS in (S)S( S ) S the follow set of SS is inherited (a self-reference that adds nothing new). So FOLLOW(SS) = {), $}. 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(SS)"; the rule is to apply the three FOLLOW rules once per production and iterate until nothing grows.

Cheat box

Cheat box

  • FIRST(α\alpha): terminals that can start a string derived from α\alpha, plus ε if α\alpha can vanish. FOLLOW(AA): terminals that can come right after AA, plus $ if AA can end the input. ε is never in FOLLOW.
  • FOLLOW rules: $ in FOLLOW(start); A→αBβA \to \alpha B \beta gives FIRST(β\beta)∖ε into FOLLOW(BB); if β\beta nullable or empty, FOLLOW(AA) into FOLLOW(BB).
  • 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

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

GATE 2025 · MSQQ5 · page 256 of the book
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
GATE 2024 · MCQQ6 · page 257 of the book
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
GATE 2021 · MCQQ11 · page 257 of the book
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
GATE 2017 · MCQQ19 · page 259 of the book
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, $}
GATE 2012 · MCQQ31 · page 261 of the book
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 S→aSb∣εS \to a S b \mid \varepsilon give FIRST(SS) and FOLLOW(SS).

Show answer

FIRST(SS) = {a, ε}; FOLLOW(SS) = {b, $}.

Question 2

Can ε be an element of a FOLLOW set?

Show answer

No. FOLLOW contains terminals and the end marker $ only.

Question 3

A→BCA \to B C, B→b∣εB \to b \mid \varepsilon, C→cC \to c. What is FIRST(AA)?

Show answer

{b, c}: FIRST(BB)∖ε = {b}, and since BB is nullable FIRST(CC) = {c} is added; CC is not nullable so ε is not added.

Question 4

Why does A→a∣εA \to a \mid \varepsilon with a∈FOLLOW(A)a \in \text{FOLLOW}(A) break LL(1)?

Show answer

On lookahead aa both A→aA \to a (FIRST) and A→εA \to \varepsilon (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(β\beta)∖ε into FOLLOW(BB) for A→αBβA \to \alpha B \beta; FOLLOW(AA) into FOLLOW(BB) when β\beta 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