1. Gyanastra
  2. Compiler Design
  3. FIRST and FOLLOW
  4. Computing FIRST Sets
Free lesson, no account needed 6 min read 8 practice questions after it
Asked in past papers3 times
  • GATE CSE20252012
  • UGC NET2013

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

  1. Take a production S→A B C DS \to A\,B\,C\,D. Whatever strings are derived from SS, they all begin with whatever AA can begin with, because AA is the leftmost symbol; BB, CC, DD do not affect the first symbol.
  2. If A→bA \to b (a terminal), every string derived from SS starts with bb, so FIRST(S)={b}FIRST(S) = \{b\}.
  3. Compute FIRST recursively down the chain: if B→cB \to c then FIRST(B)={c}FIRST(B) = \{c\}; if C→dC \to d then FIRST(C)={d}FIRST(C) = \{d\}; if D→eD \to e then FIRST(D)={e}FIRST(D) = \{e\}.
  4. If the leftmost symbol of the RHS is a variable, FIRST of the LHS is FIRST of that leftmost variable (e.g. S→A B C DS \to A\,B\,C\,D with A→bA \to b gives FIRST(S)={b}FIRST(S) = \{b\}).
  5. If the leftmost symbol can derive epsilon, substitute the epsilon away and find the FIRST of the remaining string.
  6. If A→b∣ϵA \to b \mid \epsilon, then FIRST(A)={b,ϵ}FIRST(A) = \{b, \epsilon\}.
  7. For S→A B C DS \to A\,B\,C\,D with A→b∣ϵA \to b \mid \epsilon: if AA derives bb, strings from SS start with bb, so b∈FIRST(S)b \in FIRST(S); if AA derives epsilon, AA vanishes and the string starts with BB, so if B→cB \to c then c∈FIRST(S)c \in FIRST(S).
  8. 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

  • FIRST(S)=FIRST(A)FIRST(S) = FIRST(A) — when the leftmost symbol of the RHS of S→A B C DS \to A\,B\,C\,D is the variable AA.
  • A→b⇒FIRST(A)={b}A \to b \Rightarrow FIRST(A) = \{b\} — a single terminal production gives that terminal.
  • A→b∣ϵ⇒FIRST(A)={b,ϵ}A \to b \mid \epsilon \Rightarrow FIRST(A) = \{b, \epsilon\} — epsilon is included when the variable derives epsilon.
  • S→A B C D, A→b∣ϵ, B→c⇒FIRST(S)={b,c}S \to A\,B\,C\,D,\ A \to b \mid \epsilon,\ B \to c \Rightarrow FIRST(S) = \{b, c\} — the first terminals of all alternatives together form the FIRST set.

Worked examples

Example 1 — no epsilon

Grammar:

  • S→A B C DS \to A\,B\,C\,D
  • A→bA \to b
  • B→cB \to c
  • C→dC \to d
  • D→eD \to e

Derivation reasoning: from SS we get A B C DA\,B\,C\,D, and AA always produces bb, so every string derived from SS begins with bb.

Non-terminalFIRST setReason
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 S→A B C DS \to A\,B\,C\,D with A→bA \to b, FIRST(S)={b}FIRST(S) = \{b\}: the start symbol of every derived string is bb; BB, CC, DD do not matter.

Example 3 — epsilon in the leftmost position

Grammar:

  • S→A B C DS \to A\,B\,C\,D
  • A→b∣ϵA \to b \mid \epsilon
  • B→cB \to c

FIRST(A)={b,ϵ}FIRST(A) = \{b, \epsilon\} because AA derives bb or epsilon.

FIRST(S)FIRST(S):

  • Case A→bA \to b: derived strings start with bb → b∈FIRST(S)b \in FIRST(S).
  • Case A→ϵA \to \epsilon: AA vanishes, the string becomes B C DB\,C\,D, which starts with cc (since B→cB \to c) → c∈FIRST(S)c \in FIRST(S).

Therefore FIRST(S)={b,c}FIRST(S) = \{b, c\}.

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: A→bA \to b gives FIRST(A)={b}FIRST(A) = \{b\}.
  • FIRST propagates along a chain: S→A B C DS \to A\,B\,C\,D and A→bA \to b gives FIRST(S)={b}FIRST(S) = \{b\}.
  • If a variable derives epsilon, epsilon is included in its FIRST set: A→b∣ϵA \to b \mid \epsilon gives FIRST(A)={b,ϵ}FIRST(A) = \{b, \epsilon\}.
  • 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 ∈FOLLOW(A),putA→αinM[A,∈FOLLOW(A), put A→α in M[A,].
    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
Pages 1–2 of 2