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

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 SS, derive all strings from SS; whatever appears as the first symbol (first terminal) of those strings is FIRST of SS.
  • 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 as A B C D $ — the input is always followed by $. Derivation therefore always starts with the start symbol SS followed by $, so FOLLOW(SS) 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 XX appears in a RHS, whatever can come immediately after XX in that RHS determines what can follow XX.
  • 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 S$S\text{\$}.

How it works

  1. Start symbol rule: put $ in FOLLOW(SS).
  2. Symbol-in-RHS rule: if XX appears in a RHS, add FIRST of the string of symbols to the right of XX to FOLLOW(XX).
  3. 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).
  4. Never place epsilon in a FOLLOW set.
  5. Find FIRST before FOLLOW — it makes FOLLOW easier.

Reasoning behind the rightmost-symbol rule: suppose during derivation the string is A a B C $A\,a\,B\,C\,\text{\$}. Replacing AA using A→B CA \to B\,C gives B C a B C $B\,C\,a\,B\,C\,\text{\$} — whatever previously followed AA (here aa) now follows CC. 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 X→αYβX \to \alpha Y \beta (YY a non-terminal, β\beta non-empty): FOLLOW(Y)⊇FIRST(β)\text{FOLLOW}(Y) \supseteq \text{FIRST}(\beta).
  • If production is X→αYX \to \alpha Y (YY is the rightmost symbol): FOLLOW(Y)⊇FOLLOW(X)\text{FOLLOW}(Y) \supseteq \text{FOLLOW}(X).
  • FIRST of a concatenation: FIRST(B C D)=FIRST(B)\text{FIRST}(B\,C\,D) = \text{FIRST}(B) when BB cannot derive epsilon; in general FIRST(X1X2…Xn)=FIRST(X1)\text{FIRST}(X_1 X_2 \dots X_n) = \text{FIRST}(X_1) plus, if X1X_1 derives epsilon, FIRST(X2…Xn)\text{FIRST}(X_2 \dots X_n).
  • FOLLOW sets never contain epsilon.

Worked examples

Example 1: Grammar S→A B C DS \to A\,B\,C\,D

Non-terminalFOLLOW computationRule usedResult
SS$; SS does not appear in any RHS, so nothing else to addStart symbol rule{ $ }
AAAA appears in RHS as A B C DA\,B\,C\,D; symbols after AA are B C DB\,C\,D, so FOLLOW(AA) = FIRST(B C DB\,C\,D) = FIRST(BB) = cSymbol-in-RHS rule{ c }
BBSymbols after BB are C DC\,D, so FOLLOW(BB) = FIRST(C DC\,D) = FIRST(CC) = dSymbol-in-RHS rule{ d }
CCSymbol after CC is DD, so FOLLOW(CC) = FIRST(DD) = e (DD derives the terminal ee)Symbol-in-RHS rule{ e }
DDNothing follows DD in the RHS, so FOLLOW(DD) = FOLLOW(SS) = $Rightmost-symbol rule{ $ }

Epsilon is never written in a FOLLOW set; if nothing follows a variable, $ is present because the derivation starts with S$S\text{\$}.

Example 2: Production A→B CA \to B\,C

  • CC is the rightmost symbol of the RHS, so FOLLOW(CC) need not be $.
  • FOLLOW(CC) = FOLLOW(AA), because whatever followed AA now follows CC after the expansion A→B CA \to B\,C.
  • Derivation trace: string …A a…$\dots A\,a \dots \text{\$}; replace AA by B CB\,C, giving …B C a…$\dots B\,C\,a \dots \text{\$}; the symbol aa that followed AA now follows CC.

Comparison

AspectFIRSTFOLLOW
MeaningSymbols that can begin strings derived from a symbol/stringTerminals that can appear immediately after a variable in a derivation
Contains epsilon?Yes, possible (when the symbol derives epsilon)Never contains epsilon
Start symbolFIRST(SS) from derivations of SSFOLLOW(SS) 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 S$S\text{\$}; hence FOLLOW(SS) always contains $.
  • Compute FOLLOW by scanning the RHS of productions, not by deriving strings.
  • If XX appears in a RHS, add FIRST of the symbols to the right of XX to FOLLOW(XX).
  • 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
Pages 1–2 of 2