FIRST and FOLLOW Sets for a Given CFG

FIRST and FOLLOW Sets for a Given CFG

Verified Sources
Nov 4, 2026

We compute FIRST and FOLLOW sets for the context-free grammar:
S→aBDh,  B→cC,  C→bC∣ϵ,  D→EF,  E→g∣ϵ,  F→f∣ϵ.S \to aBDh,\; B \to cC,\; C \to bC \mid \epsilon,\; D \to EF,\; E \to g \mid \epsilon,\; F \to f \mid \epsilon.

Key supporting definitions/rules we use: epsilon-handling in FIRST computation and end-marker propagation in FOLLOW computation. A nonterminal can have ϵ\epsilon in its FIRST set if it can derive the empty string. For FOLLOW, the start symbol’s FOLLOW includes the end marker $$$.

We’ll compute systematically:

  • FIRST sets for S,B,C,D,E,FS,B,C,D,E,F
  • FOLLOW sets for S,B,C,D,E,FS,B,C,D,E,F and show the intermediate reasoning using the standard grammar rules.

FIRST and FOLLOW Sets (Theory + Examples)

Step 1 — Compute FIRST sets

  1. 1
    Step 1

    From C→bC∣ϵC \to bC \mid \epsilon, we have FIRST(C)={b,ϵ}.\text{FIRST}(C)=\{b,\epsilon\}.

  2. 2
    Step 2

    Since B→cCB\to cC, and cc is a terminal, the strings derived from BB always begin with cc; thus FIRST(B)={c}.\text{FIRST}(B)=\{c\}.

  3. 3
    Step 3

    From E→g∣ϵE\to g\mid \epsilon, we get FIRST(E)={g,ϵ}.\text{FIRST}(E)=\{g,\epsilon\}.

  4. 4
    Step 4

    From F→f∣ϵF\to f\mid \epsilon, we get FIRST(F)={f,ϵ}.\text{FIRST}(F)=\{f,\epsilon\}.

  5. 5
    Step 5

    Because D→EFD\to EF, we combine: start with FIRST(E)∖{ϵ}\text{FIRST}(E)\setminus\{\epsilon\}, and if EE can be ϵ\epsilon, also include FIRST(F)∖{ϵ}\text{FIRST}(F)\setminus\{\epsilon\}, and if both can be ϵ\epsilon, include ϵ\epsilon. Here: FIRST(E)∖{ϵ}={g}\text{FIRST}(E)\setminus\{\epsilon\} = \{g\}, FIRST(F)∖{ϵ}={f}\text{FIRST}(F)\setminus\{\epsilon\}=\{f\}, and both can be ϵ\epsilon, so ϵ\epsilon is included. Therefore FIRST(D)={g,f,ϵ}.\text{FIRST}(D)=\{g,f,\epsilon\}.

  6. 6
    Step 6

    Since S→aBDhS\to aBDh, the first symbol is terminal aa, so FIRST(S)={a}.\text{FIRST}(S)=\{a\}.

FIRST sets (final)

epsilon-aware results:

NonterminalFIRST set
SS{a}\{a\}
BB{c}\{c\}
CC{b,ϵ}\{b,\epsilon\}
DD{g,f,ϵ}\{g,f,\epsilon\}
EE{g,ϵ}\{g,\epsilon\}
FF{f,ϵ}\{f,\epsilon\}

Step 2 — Compute FOLLOW sets

  1. 1
    Step 1

    Because SS is the start symbol, we include the end marker: \\in\text{FOLLOW}(S).So. So \text{FOLLOW}(S)={$}.$

  2. 2
    Step 2

    In S→aBDhS\to aBDh, after BB comes DD, so add FIRST(D)∖{ϵ}\text{FIRST}(D)\setminus\{\epsilon\} to FOLLOW(B)\text{FOLLOW}(B). Since FIRST(D)={g,f,ϵ}\text{FIRST}(D)=\{g,f,\epsilon\}, add {g,f}.\{g,f\}. Also, because DD can derive ϵ\epsilon, add FIRST(h)={h}\text{FIRST}(h) = \{h\} to FOLLOW(B)\text{FOLLOW}(B). Thus FOLLOW(B)⊇{g,f,h}.\text{FOLLOW}(B)\supseteq \{g,f,h\}.

  3. 3
    Step 3

    In S→aBDhS\to aBDh, after DD comes terminal hh, so add hh to FOLLOW(D)\text{FOLLOW}(D). Thus h∈FOLLOW(D)h\in\text{FOLLOW}(D). So FOLLOW(D)⊇{h}.\text{FOLLOW}(D)\supseteq\{h\}.

  4. 4
    Step 4

    In B→cCB\to cC, CC is at the end of the right-hand side, so add FOLLOW(B)\text{FOLLOW}(B) to FOLLOW(C)\text{FOLLOW}(C). Therefore FOLLOW(C)⊇FOLLOW(B)⊇{g,f,h}.\text{FOLLOW}(C)\supseteq\text{FOLLOW}(B)\supseteq\{g,f,h\}.

  5. 5
    Step 5

    In C→bCC\to bC, the second CC is at the end, so add FOLLOW(C)\text{FOLLOW}(C) to itself (no new information). The ϵ\epsilon production also doesn’t add terminals directly.

  6. 6
    Step 6

    In D→EFD\to EF, after EE comes FF, so add FIRST(F)∖{ϵ}\text{FIRST}(F)\setminus\{\epsilon\} to FOLLOW(E)\text{FOLLOW}(E). Here FIRST(F)∖{ϵ}={f}\text{FIRST}(F)\setminus\{\epsilon\} = \{f\}, so f∈FOLLOW(E)f\in\text{FOLLOW}(E). Since F⇒∗ϵF\Rightarrow^*\epsilon, also add FOLLOW(D)\text{FOLLOW}(D) to FOLLOW(E)\text{FOLLOW}(E). We already have FOLLOW(D)⊇{h}\text{FOLLOW}(D)\supseteq\{h\}, so {h}⊆FOLLOW(E)\{h\}\subseteq\text{FOLLOW}(E). Hence FOLLOW(E)⊇{f,h}.\text{FOLLOW}(E)\supseteq\{f,h\}.

  7. 7
    Step 7

    In D→EFD\to EF, FF is at the end, so add FOLLOW(D)\text{FOLLOW}(D) to FOLLOW(F)\text{FOLLOW}(F). Therefore FOLLOW(F)⊇{h}.\text{FOLLOW}(F)\supseteq\{h\}.

  8. 8
    Step 8

    Now that FOLLOW(B)={g,f,h}\text{FOLLOW}(B)=\{g,f,h\}, it forces FOLLOW(C)={g,f,h}\text{FOLLOW}(C)=\{g,f,h\}. Those values already propagate consistently into FOLLOW(E)\text{FOLLOW}(E) and FOLLOW(F)\text{FOLLOW}(F) via FOLLOW(D)={h}\text{FOLLOW}(D)=\{h\}. No additional terminals appear.

FOLLOW sets (final)

End marker propagation and epsilon-aware propagation yield:

NonterminalFOLLOW set
SS{\{\\\}$
BB{g,f,h}\{g,f,h\}
CC{g,f,h}\{g,f,h\}
DD{h}\{h\}
EE{f,h}\{f,h\}
FF{h}\{h\}

Pro Tip: Epsilon changes what follows

When you process X→αYβX\to \alpha Y\beta and ϵ∈FIRST(β)\epsilon\in\text{FIRST}(\beta), you must also add FOLLOW(X)\text{FOLLOW}(X) into FOLLOW(Y)\text{FOLLOW}(Y). This is exactly why hh enters FOLLOW(B)\text{FOLLOW}(B) and FOLLOW(E)\text{FOLLOW}(E).

Pro Tip to avoid a common mistake

Do not add ϵ\epsilon itself into FOLLOW sets. FOLLOW contains terminals (and \$$ for start), not \epsilon.Inourcomputation,wealwaysused. In our computation, we always used \text{FIRST}(\cdot)\setminus{\epsilon}$ when adding to FOLLOW.

What you should do (recap roadmap)

Compute FIRST

Phase A

Start from productions with terminals and ϵ\epsilon, then build outward (e.g., CC, EE, FF first)."

Compute FOLLOW

Phase B

Initialize FOLLOW(SS) with $$$, then scan each production right-to-left with FIRST(remaining) logic."

Close under propagation

Phase C

Repeat until sets stop growing; epsilon-driven dependencies should already be satisfied."

Knowledge Check

Question 1 of 4
Q1Single choice

In the grammar S→aBDhS\to aBDh, which terminals are added to FOLLOW(B) from FIRST(D) when forming FOLLOW(B)?