Compiler Design · FIRST and FOLLOW
Computing FIRST Sets
Overview
FIRST is one of the two functions (with FOLLOW) that must be known for a grammar before an LL(1) parsing table can be built.
It records which terminals can begin the strings derived from a variable or a string, and it is computed by looking only at the leftmost symbol of each production's right-hand side, substituting epsilon away when it appears.
Key concepts
- FIRST — the set of terminals that can appear as the first symbol of any string derived from a symbol (a variable or a string).
Equivalently: derive all possible strings from the symbol; the first terminal appearing at the beginning of those derived strings is its FIRST. - FOLLOW — the set of terminals that can appear immediately after a variable during derivation (introduced next).
- Leftmost-symbol rule — FIRST of a production's LHS depends only on the leftmost symbol of the RHS; everything after it is irrelevant unless epsilon appears.
- Epsilon rule — if the leftmost symbol can derive epsilon, that symbol disappears from the front of the string and the next symbol of the RHS becomes the first symbol; substitute the epsilon away and find the FIRST of the remaining string.
- Propagation along a chain — if the leftmost symbol of the RHS is a variable, FIRST of the LHS is FIRST of that leftmost variable.
How it works
- Take a production . Whatever strings are derived from , they all begin with whatever can begin with, because is the leftmost symbol; , , do not affect the first symbol.
- If (a terminal), every string derived from starts with , so .
- Compute FIRST recursively down the chain: if then ; if then ; if then .
- If the leftmost symbol of the RHS is a variable, FIRST of the LHS is FIRST of that leftmost variable (e.g. with gives ).
- If the leftmost symbol can derive epsilon, substitute the epsilon away and find the FIRST of the remaining string.
- If , then .
- For with : if derives , strings from start with , so ; if derives epsilon, vanishes and the string starts with , so if then .
- General procedure: whenever epsilon appears at a position, substitute it out, look at the remaining string, and find the FIRST of that remaining string.
Formulas
- — when the leftmost symbol of the RHS of is the variable .
- — a single terminal production gives that terminal.
- — epsilon is included when the variable derives epsilon.
- — the first terminals of all alternatives together form the FIRST set.
Worked examples
Example 1 — no epsilon
Grammar:
Derivation reasoning: from we get , and always produces , so every string derived from begins with .
| Non-terminal | FIRST set | Reason |
|---|---|---|
| S | { b } | Leftmost symbol of RHS is A; FIRST(S) = FIRST(A) |
| A | { b } | A → b, terminal b begins the only derived string |
| B | { c } | B → c |
| C | { d } | C → d |
| D | { e } | D → e |
Example 2 — leftmost symbol is a variable with a single terminal production
For with , : the start symbol of every derived string is ; , , do not matter.
Example 3 — epsilon in the leftmost position
Grammar:
because derives or epsilon.
:
- Case : derived strings start with → .
- Case : vanishes, the string becomes , which starts with (since ) → .
Therefore .
Revision summary
- FIRST and FOLLOW are the two functions needed before constructing an LL(1) parsing table.
- FIRST of a production's LHS depends only on the leftmost symbol of the RHS; the rest of the RHS is irrelevant unless epsilon appears.
- A variable deriving a single terminal has that terminal as its FIRST: gives .
- FIRST propagates along a chain: and gives .
- If a variable derives epsilon, epsilon is included in its FIRST set: gives .
- Whenever epsilon occurs at a position, substitute it out and compute FIRST of the remaining string; the first terminals of all these alternatives together form the FIRST set.
- Memorise the definition: FIRST = the set of first symbols (first terminals) of all strings derivable from the symbol/string.
Previous-year exam insights
- FIRST is computed by fixed-point iteration: initialise FIRST(a)={a} for each terminal a, FIRST(X)={ε} if X→ε is a production, then repeatedly apply FIRST(A) ⊇ FIRST(X1) and, while X1…Xi-1 all derive ε, FIRST(A) ⊇ FIRST(Xi), until no set changes.
Asked inGATE 2012GATE 2025 - For a string X1X2…Xn: add FIRST(X1)−{ε}; if ε∈FIRST(X1) add FIRST(X2)−{ε}; continue; add ε to FIRST of the string only if ε∈FIRST(Xi) for every i.
Asked inUGC NET 2013GATE 2012 - FOLLOW(X) = set of terminals a such that S ⇒* αXaβ;
FOLLOW(S) always contains the end marker $ (input right-end marker), and ε is never a member of a FOLLOW set.
Asked inGATE 2025GATE 2012UGC NET 2013 - FOLLOW rules: for A→αBβ add FIRST(β)−{ε} to FOLLOW(B); if β derives ε (or β is empty) add FOLLOW(A) to FOLLOW(B); for A→αB add FOLLOW(A) to FOLLOW(B).
Asked inGATE 2012 - LL(1) table construction: entry M[A,a] = A→α for each terminal a in FIRST(α);
if ε∈FIRST(α), also put A→α in M[A,b] for every b in FOLLOW(A);
if ε∈FIRST(α) and ].
Asked inGATE 2012GATE 2025 - A grammar is LL(1) iff no table cell has more than one entry, equivalently no two productions of a nonterminal have overlapping FIRST sets and at most one of them derives ε (and if one derives ε, its FOLLOW must not intersect the FIRST of the others).
Asked inGATE 2012