FIRST, FOLLOW, and LL(1) Parsing Table for a Grammar with ε-Productions

FIRST, FOLLOW, and LL(1) Parsing Table for a Grammar with ε-Productions

Verified Sources
Nov 4, 2026

We have the grammar:
S→ABCS \to ABC
A→aA∣εA \to aA \mid \varepsilon
B→bB∣εB \to bB \mid \varepsilon
C→cC∣dC \to cC \mid d

Let FIRST(X)FIRST(X) be the set of terminals that can appear first in strings derived from XX, and FOLLOW(X)FOLLOW(X) be the set of terminals that can appear immediately to the right of XX in some sentential form. These sets are used to build the LL(1) parsing table M[X,a]M[X, a] with the rule: for a production X→αX \to \alpha,

  • if a∈FIRST(α)a \in FIRST(\alpha) then M[X,a]=X→αM[X,a] = X \to \alpha,
  • if ε∈FIRST(α)\varepsilon \in FIRST(\alpha) then for every b∈FOLLOW(X)b \in FOLLOW(X), set M[X,b]=X→αM[X,b] = X \to \alpha.

Key keywords: LL(1) , FIRST , FOLLOW , ε-production , Parsing table.

LL(1) FIRST/FOLLOW and Parsing Table (construction walkthrough)

Compute FIRST sets

  1. 1
    Step 1

    A → aA | ε, so FIRST(A) = {a, ε}.

  2. 2
    Step 2

    B → bB | ε, so FIRST(B) = {b, ε}.

  3. 3
    Step 3

    C → cC | d, so FIRST(C) = {c, d} (no ε).

  4. 4
    Step 4

    FIRST(ABC) depends on leading symbols: A can start with a or ε. If A ⇒ ε, then B can start with b or ε. If both A ⇒ ε and B ⇒ ε, then C starts with c or d. Therefore FIRST(S)=FIRST(ABC) = {a, b, c, d}.

Compute FOLLOW sets

  1. 1
    Step 1

    By convention for LL parsing, \ (end marker) is in FOLLOW(S). So FOLLOW(S) = {$ }.

  2. 2
    Step 2

    From S → A B C: after A comes B C. So add FIRST(BC) \u2212 {ε} to FOLLOW(A), and if ε ∈ FIRST(BC) then also add FOLLOW(S) to FOLLOW(A).

  3. 3
    Step 3

    B can be ε, so FIRST(BC) = FIRST(B) if B doesn't vanish; since B → bB | ε, we get FIRST(BC) = {b} ∪ FIRST(C) when B ⇒ ε = {b, c, d}. Also ε ∉ FIRST(BC) because C has no ε-production.

  4. 4
    Step 4

    So FOLLOW(A) gets FIRST(BC) \u2212 {ε} = {b, c, d}. Since ε \u2208 FIRST(BC) is false, FOLLOW(S) is NOT added.

  5. 5
    Step 5

    After B in S → A B C comes C, so add FIRST(C) to FOLLOW(B). Since ε ∉ FIRST(C), FOLLOW(S) is not added via this path.

  6. 6
    Step 6

    FOLLOW(B) = FIRST(C) = {c, d}. For C: C is at the end of S → ABC, so FOLLOW(C) includes FOLLOW(S) = { \ }.

FIRST sets (final)

  • FIRST(A)=a,varepsilonFIRST(A) = \\{a, \\varepsilon\\}
  • FIRST(B)=b,varepsilonFIRST(B) = \\{b, \\varepsilon\\}
  • FIRST(C)=c,dFIRST(C) = \\{c, d\\}
  • FIRST(S)=a,b,c,dFIRST(S) = \\{a, b, c, d\\}

FOLLOW sets (final)

  • FOLLOW(S) = \\{\\\}$
  • FOLLOW(A)=b,c,dFOLLOW(A) = \\{b, c, d\\}
  • FOLLOW(B)=c,dFOLLOW(B) = \\{c, d\\}
  • FOLLOW(C) = \\{\\\}$

ε-handling rule

When building FIRST for a sequence like ABC: if A ⇒ ε, then continue to B; if B ⇒ ε, then continue to C. This is exactly why FIRST(S) includes a, b, c, d.

FOLLOW propagation via 'last symbol' only

FOLLOW(X) only receives FOLLOW(Left-Hand Side) when X can appear at the end of some derivation from that production. Here, in S → ABC, C is last, so FOLLOW(C) gets FOLLOW(S).

LL(1) parsing table construction

Terminals: {a,b,c,d,\{a,b,c,d,\\\}$.

We list each production and the lookahead symbols that trigger it:

Productions

  • S→ABCS \to ABC
  • A→aAA \to aA
  • A→varepsilonA \to \\varepsilon
  • B→bBB \to bB
  • B→varepsilonB \to \\varepsilon
  • C→cCC \to cC
  • C→dC \to d

Use rules

  • For X→αX \to \alpha with varepsilonnotinFIRST(alpha)\\varepsilon \\notin FIRST(\\alpha): fill M[X,t]M[X,t] for tinFIRST(alpha)t \\in FIRST(\\alpha).
  • For X→alphaX \to \\alpha with varepsiloninFIRST(alpha)\\varepsilon \\in FIRST(\\alpha): fill M[X,t]M[X,t] for all tinFOLLOW(X)t \\in FOLLOW(X).

LL(1) Parsing Table M (entries shown compactly)

Columns are lookahead tokens; rows are nonterminals. Empty cells mean error.

The compact chart above encodes whether a production exists; here is the explicit table.

Nonterminallookahead alookahead blookahead clookahead dlookahead $
SSS→ABCS \to ABC—S→ABCS \to ABCS→ABCS \to ABC—
AAA→aAA \to aA—A→εA \to \varepsilonA→εA \to \varepsilon—
BB—B→bBB \to bBB→εB \to \varepsilonB→εB \to \varepsilon—
CC——C→cCC \to cCC→dC \to d—

Why these entries?

  • FIRST(ABC)=a,b,c,dFIRST(ABC)=\\{a,b,c,d\\} and no ε, so M[S,a],M[S,b],M[S,c],M[S,d]M[S,a],M[S,b],M[S,c],M[S,d] all get S→ABCS \to ABC.
  • For A→aAA \to aA: ainFIRST(aA)a \\in FIRST(aA) so M[A,a]M[A,a] gets it.
  • For A→varepsilonA \to \\varepsilon: ε ∈ FIRST(ε), so use FOLLOW(A)=\{b,c,d\}; thus M[A,c]M[A,c] and M[A,d]M[A,d] get A→varepsilonA \to \\varepsilon (and M[A,b]M[A,b] too, but that table cell is in conflict with how AA could also derive aA? Actually aAaA only applies at lookahead a, so M[A,b]M[A,b] is safe; in our explicit table above we left M[A,b]M[A,b] blank, so we correct it now: M[A,b]=AtovarepsilonM[A,b]=A \\to \\varepsilon.)
  • For B→bBB \to bB: M[B,b]=BtobBM[B,b]=B\\to bB.
  • For BtovarepsilonB \\to \\varepsilon: FOLLOW(B)=\{c,d\}; so M[B,c]=BtovarepsilonM[B,c]=B\\to \\varepsilon and M[B,d]=BtovarepsilonM[B,d]=B\\to \\varepsilon.

Corrected explicit table (with the missing ε entry fixed)

Nonterminallookahead alookahead blookahead clookahead dlookahead $
SSS→ABCS \to ABCS→ABCS \to ABCS→ABCS \to ABCS→ABCS \to ABC—
AAA→aAA \to aAA→varepsilonA \to \\varepsilonA→varepsilonA \to \\varepsilonA→varepsilonA \to \\varepsilon—
BB—B→bBB \to bBB→varepsilonB \to \\varepsilonB→varepsilonB \to \\varepsilon—
CC——C→cCC \to cCC→dC \to d—

LL(1) parsing steps for input aabd

  1. 1
    Step 1

    Put \ on stack bottom, then start symbol SS on top. Input is a a b d \.

  2. 2
    Step 2

    Top is SS, lookahead is aa. Use M[S,a]=S→ABCM[S,a]=S\to ABC. Replace SS by ABCA B C.

  3. 3
    Step 3

    Top is AA, lookahead is aa. Use M[A,a]=A→aAM[A,a]=A\to aA. Match terminal aa.

  4. 4
    Step 4

    Top is AA, lookahead is next aa. Again use A→aAA\to aA and match aa.

  5. 5
    Step 5

    Now lookahead is bb and top is BB. Use M[B,b]=B→bBM[B,b]=B\to bB and match bb.

  6. 6
    Step 6

    Now lookahead is dd and top is CC. Use M[C,d]=C→dM[C,d]=C\to d and match dd.

  7. 7
    Step 7

    Input is exhausted; stack top is \ and lookahead is \; accept.

Trace table (stack top on the left)

Let stack be written as: top ... bottom, and input as remaining unread symbols.

StepStack (top→bottom)Input (current→end)Action
0SS \\aabdaabd\\Start
1A B CA\,B\,C \\aabdaabd\\StoABCS\\to ABC (on lookahead aa)
2a A B Ca\,A\,B\,C \\aabdaabd\\AtoaAA\\to aA
3A B CA\,B\,C \\abdabd\\match aa
4a A B Ca\,A\,B\,C \\abdabd\\AtoaAA\\to aA (lookahead aa)
5A B CA\,B\,C \\bdbd\\match aa
6b B Cb\,B\,C \\bdbd\\BtobBB\\to bB
7B CB\,C \\dd\\match bb
8dd \\dd\\CtodC\\to d
9\\\\match dd
10(empty)(empty)accept

Common edge cases with ε in LL(1)

Knowledge Check

Question 1 of 4
Q1Single choice

What is FIRST(S)FIRST(S) for the grammar S→ABCS\to ABC, A→aA∣εA\to aA\mid\varepsilon, B→bB∣εB\to bB\mid\varepsilon, C→cC∣dC\to cC\mid d?