FIRST and FOLLOW Sets
Master the two most important sets in LL parsing — FIRST tells you what a grammar symbol can start with, and FOLLOW tells you what can come after it. Together they build the LL(1) parsing table.
Why Do We Need FIRST and FOLLOW?
A predictive parser needs to decide, given a non-terminal A and the current input token a, which production rule to use. This decision is encoded in a parsing table.
To build the parsing table, we need to answer two questions:
-
"What can A generate first?" — If A → α and α can start with
a, then when we seeawe should try this production. This is the FIRST set. -
"What can come after A?" — If A → ε (A can disappear), we need to know what comes after A in the input to decide if we should use A → ε. This is the FOLLOW set.
Quick Preview
These sets tell the parser exactly which production to pick at each step.
FIRST Set — Definition and Rules
FIRST(α) is the set of terminals that can appear as the first symbol of any string derived from α.
If α can derive the empty string (ε), then ε is also in FIRST(α).
Rules for Computing FIRST
Rule 1 — Terminal:
Rule 2 — Non-terminal A with production A → X₁ X₂ … Xₙ:
In other words: keep looking right until you find a symbol that cannot disappear.
Example — Computing FIRST
Grammar:
Step 1 — FIRST of terminals:
Step 2 — FIRST(F):
Step 3 — FIRST(T'):
Step 4 — FIRST(T):
Step 5 — FIRST(E'):
Step 6 — FIRST(E):
Final FIRST sets:
FOLLOW Set — Definition and Rules
FOLLOW(A) is the set of terminals that can appear immediately after A in some sentential form.
FOLLOW is only defined for non-terminals. It is never defined for ε.
Rules for Computing FOLLOW
Rule 1 — Start symbol:
Rule 2 — For each production B → α A β:
Example — Computing FOLLOW (same grammar)
Grammar:
FIRST sets (already computed):
Step 1 — FOLLOW(E):
Step 2 — FOLLOW(E'):
Step 3 — FOLLOW(T):
Step 4 — FOLLOW(T'):
Step 5 — FOLLOW(F):
Final FOLLOW sets:
FIRST Set — Advanced Examples (All Cases)
The examples below cover every edge case you will encounter when computing FIRST sets.
Example 2 — All Symbols Nullable (ε Reaches the LHS)
Grammar:
Step 1 — FIRST of A, B, C:
Step 2 — FIRST(S) for S → A B C:
Key rule: ε propagates all the way to FIRST(S) only when every symbol in the production can independently derive ε. If any single symbol cannot vanish, the chain stops there.
Example 3 — Recursive Productions
Grammar:
Step 1 — FIRST(A) and FIRST(B):
Step 2 — FIRST(S):
Note on recursion: In A → c A d, the recursive A is buried inside the production — it does not affect FIRST(A). FIRST only looks at the leftmost symbol of each production. For A → c A d, that is the terminal 'c'. The inner recursive A is never reached during the FIRST computation.
Example 4 — Multiple Alternatives, One Non-terminal Never Nullable
Grammar:
Step 1:
Step 2 — FIRST(S):
Key contrast with Example 2: B is not nullable, so the chain stops at B. ε does NOT reach FIRST(S) even though A can vanish.
Example 5 — Indirect Nullable via Another Non-terminal
Grammar:
Step 1 — bottom up:
Step 2 — FIRST(S) via S → X Y:
Takeaway: The ε-chain propagates even when the nullable group is itself wrapped inside another non-terminal (X). Always reduce to base cases first, then combine.
FIRST Edge Cases — Quick Reference
FOLLOW Set — Advanced Examples (All Cases)
The examples below cover every edge case you will encounter when computing FOLLOW sets.
Example 2 — Same Non-terminal Appearing Twice in One Production
Grammar:
FIRST sets:
FOLLOW(S) = { $ }
FOLLOW(A): A appears at two positions in the same production — analyse each separately.
Common trap: A can produce ε, so you might think FOLLOW(S) = { $ } should be in FOLLOW(A). It is not. Whether A can produce ε is irrelevant to what tokens can follow A in a sentential form — what matters is the concrete context around each occurrence.
Example 3 — FOLLOW Propagating Through a Nullable Chain
Grammar:
FIRST sets (computed earlier):
FOLLOW(S) = { $ }
FOLLOW(C): C is at the right end of S → A B C
FOLLOW(B): B is followed by C in S → A B C
FOLLOW(A): A is followed by "B C" in S → A B C
Final FOLLOW sets:
Pattern: FOLLOW "flows leftward" through nullable symbols. A's FOLLOW includes everything that B and C could start with, plus S's own FOLLOW (because B and C can both disappear, exposing S's surrounding context to A).
Example 4 — Non-terminal Appearing in Multiple Different Productions
Grammar:
FIRST sets:
FOLLOW(S) = { $ }
FOLLOW(B): From S → A B, B is at the end
FOLLOW(A): A appears in three places across two different productions — scan all of them.
Key rule: FOLLOW is the union over every occurrence of A anywhere in the grammar — across every production, every alternative.
Example 5 — ε in FIRST Triggers Two Different Paths in FOLLOW
Grammar:
($ is end-of-input, treated as a terminal here for clarity)
FIRST sets:
FOLLOW(S) = { $ }
FOLLOW(C): C followed by $ (terminal) in S → A B C $
FOLLOW(B): B followed by "C $" in S → A B C $
FOLLOW(A): A appears in two places:
Observation: { $ } is NOT in FOLLOW(A) even though FOLLOW(S) = { $ } and A is the first symbol of S. The terminal C always consumes 's' before end-of-input is reached after A.
FOLLOW Edge Cases — Quick Reference
Summary and Common Mistakes
Quick Reference
| Set | Defined For | Contains | Key Rule |
|---|---|---|---|
| FIRST(α) | Any string α | Terminals that α can start with; also ε if α ⟹* ε | Look at the leftmost symbol, continue right if it can be ε |
| FOLLOW(A) | Non-terminals only | Terminals that can appear right after A; also $ | Look at the right context of A in every production |
Common Mistakes
Mistake 1 — Adding ε to FOLLOW:
FOLLOW sets never contain ε. The ε in FIRST means "this can disappear." FOLLOW tracks what comes after in the actual input, not what the non-terminal produces.
Mistake 2 — Forgetting to propagate FOLLOW through ε-productions:
If A → α B and B can derive ε, then FOLLOW(A) must be added to FOLLOW(B).
Mistake 3 — Not adding $ to FOLLOW(S):
The start symbol S is always followed by end-of-input ($). Always add $ to FOLLOW(S) first.