Compiler Design · FIRST and FOLLOW
Computing FOLLOW Sets
Overview
FOLLOW sets record which terminals can appear immediately after a non-terminal during any derivation. They are computed by scanning the right-hand sides of productions, using FIRST sets and the FOLLOW of the left-hand side, and they always contain $ for the start symbol.
Key concepts
- FIRST of a string: the set of symbols that can appear at the beginning of any string derived from it.
To find FIRST of , derive all strings from ; whatever appears as the first symbol (first terminal) of those strings is FIRST of . - FOLLOW of a variable: the set of terminals that can follow that variable during derivation.
- Start marker
$: every input, e.g.A B C D, is treated asA B C D $— the input is always followed by$. Derivation therefore always starts with the start symbol followed by$, so FOLLOW() always contains$. - Right-hand-side scanning: instead of deriving strings, look at the RHS of each production, because every string is formed from RHS symbols.
If a variable appears in a RHS, whatever can come immediately after in that RHS determines what can follow . - Rightmost-symbol rule: if a variable is the last symbol of a RHS, whatever followed the LHS now follows the rightmost symbol of the RHS, so FOLLOW(variable) includes FOLLOW(LHS).
- No epsilon in FOLLOW: FOLLOW sets never contain epsilon. If nothing follows a variable,
$is present because the derivation starts with .
How it works
- Start symbol rule: put
$in FOLLOW(). - Symbol-in-RHS rule: if appears in a RHS, add FIRST of the string of symbols to the right of to FOLLOW().
- Rightmost-symbol rule (exception): if nothing is to the right of a variable in a RHS, do not write epsilon and do not automatically write
$;
instead set FOLLOW(variable) = FOLLOW(LHS non-terminal). - Never place epsilon in a FOLLOW set.
- Find FIRST before FOLLOW — it makes FOLLOW easier.
Reasoning behind the rightmost-symbol rule: suppose during derivation the string is . Replacing using gives — whatever previously followed (here ) now follows . Because the LHS is replaced by the RHS, whatever followed the LHS now follows the rightmost symbol of the RHS.
Formulas
- FOLLOW(start symbol) contains
$(end marker of input). - If production is ( a non-terminal, non-empty): .
- If production is ( is the rightmost symbol): .
- FIRST of a concatenation: when cannot derive epsilon; in general plus, if derives epsilon, .
- FOLLOW sets never contain epsilon.
Worked examples
Example 1: Grammar
| Non-terminal | FOLLOW computation | Rule used | Result |
|---|---|---|---|
$; does not appear in any RHS, so nothing else to add | Start symbol rule | { $ } | |
| appears in RHS as ; symbols after are , so FOLLOW() = FIRST() = FIRST() = c | Symbol-in-RHS rule | { c } | |
| Symbols after are , so FOLLOW() = FIRST() = FIRST() = d | Symbol-in-RHS rule | { d } | |
| Symbol after is , so FOLLOW() = FIRST() = e ( derives the terminal ) | Symbol-in-RHS rule | { e } | |
Nothing follows in the RHS, so FOLLOW() = FOLLOW() = $ | Rightmost-symbol rule | { $ } |
Epsilon is never written in a FOLLOW set; if nothing follows a variable, $ is present because the derivation starts with .
Example 2: Production
- is the rightmost symbol of the RHS, so FOLLOW() need not be
$. - FOLLOW() = FOLLOW(), because whatever followed now follows after the expansion .
- Derivation trace: string ; replace by , giving ; the symbol that followed now follows .
Comparison
| Aspect | FIRST | FOLLOW |
|---|---|---|
| Meaning | Symbols that can begin strings derived from a symbol/string | Terminals that can appear immediately after a variable in a derivation |
| Contains epsilon? | Yes, possible (when the symbol derives epsilon) | Never contains epsilon |
| Start symbol | FIRST() from derivations of | FOLLOW() always contains $ |
| When nothing is on the right in a RHS | — | Use FOLLOW of the LHS non-terminal, not epsilon and not automatically $ |
Revision summary
- FIRST = what can appear at the beginning of derived strings;
FOLLOW = what terminal can come after a variable during derivation. - The input is always followed by
$, so the derivation starts with ; hence FOLLOW() always contains$. - Compute FOLLOW by scanning the RHS of productions, not by deriving strings.
- If appears in a RHS, add FIRST of the symbols to the right of to FOLLOW().
- If a variable is the rightmost symbol of a RHS, FOLLOW(variable) = FOLLOW(LHS).
- FOLLOW never contains epsilon; absence of a following symbol means
$(or FOLLOW of the LHS), never epsilon. - Always find FIRST before FOLLOW.
Previous-year exam insights
- FOLLOW(A) is defined over sentential forms: the set of terminals that can appear immediately to the right of A in some sentential form, plus $ if A can be the rightmost symbol of a sentential form; equivalently FOLLOW(A) = { a | S ⇒* αAaβ } with $ as end marker.
Asked inUGC NET 2013GATE 1992 - FOLLOW sets are computed by iterating to a fixed point: repeatedly apply the rules until no set changes, since FOLLOW dependencies can be cyclic (e.g. A → B and B → A).
Asked inGATE 2019GATE 2017 - If a production is X → αYβ and β ⇒* ε (β nullable), then FOLLOW(Y) includes both FIRST(β) − {ε} and FOLLOW(X); ε itself is never placed in a FOLLOW set.
Asked inGATE 2017GATE 2025 - For the start symbol S, FOLLOW(S) contains the end marker $; if S appears on the right-hand side of any production, FOLLOW(S) also receives contributions from those occurrences.
Asked inGATE 2019GATE 2012 - In LL(1) table construction, entry M[A, a] is filled with A → α when a ∈ FIRST(α), and with A → α when α ⇒* ε and a ∈ FOLLOW(A); $ is treated as an ordinary terminal for table entries.
Asked inGATE 2012GATE 2025 - FIRST(α) = { t | α ⇒* tβ for some β } with t a terminal, and ε ∈ FIRST(α) iff α ⇒* ε;
FIRST of a terminal is itself, and FIRST(ε) = {ε}.
Asked inUGC NET 2013GATE 2025