FIRST/FOLLOW, LL(1) Parsing Table, and LL(1) Determination for a Given CFG

FIRST/FOLLOW, LL(1) Parsing Table, and LL(1) Determination for a Given CFG

Verified Sources
Sep 12, 2026

We are given the CFG:

  • SABS \rightarrow AB
  • AaAϵA \rightarrow aA \mid \epsilon
  • BbBϵB \rightarrow bB \mid \epsilon

We will compute FIRST and FOLLOW sets for every non-terminal, then build the LL(1) parsing table and finally decide whether the grammar is LL(1).

Key conventions:

  • Let ϵ\epsilon denote the empty string.
  • Let end-of-input be the marker \$$, and for the start symbol S,wehave, we have $ \in \text{FOLLOW}(S)$. (This is the standard setup for FOLLOW-based table construction.)

Footnotes

  1. FOLLOW(S) includes end-of-input marker forstartsymbolhttps://en.wikipedia.org/wiki/LLparserNotestheuseoffor start symbol - https://en.wikipedia.org/wiki/LL_parser - Notes the use of in parsing table construction for LL(1).

FIRST & FOLLOW sets and LL(1) parsing table (educational overview)

Step 1: Compute FIRST sets

We start from the productions:

  • AaAϵA \rightarrow aA \mid \epsilon
  • BbBϵB \rightarrow bB \mid \epsilon
  • SABS \rightarrow AB

Because AA can either start with terminal aa (via aAaA) or be empty (via ϵ\epsilon), we get:

  • FIRST(A)={a,ϵ}\text{FIRST}(A) = \{a, \epsilon\}

Similarly, for BB:

  • FIRST(B)={b,ϵ}\text{FIRST}(B) = \{b, \epsilon\}

Now compute FIRST(S)\text{FIRST}(S) from SABS \rightarrow AB using the standard FIRST-of-concatenation rule:

  • Start with FIRST(A)\text{FIRST}(A)
  • If ϵFIRST(A)\epsilon \in \text{FIRST}(A), then also include FIRST(B)\text{FIRST}(B) (excluding ϵ\epsilon unless both can derive ϵ\epsilon)

Since ϵFIRST(A)\epsilon \in \text{FIRST}(A), we include FIRST(B)\text{FIRST}(B) as well:

  • From AA producing ϵ\epsilon, SS can begin with what BB begins with: {b}\{b\}.
  • SS can produce ϵ\epsilon only if both AA and BB derive ϵ\epsilon.

Thus:

  • FIRST(S)={a,b,ϵ}\text{FIRST}(S) = \{a, b, \epsilon\}

FIRST sets (systematic derivation)

  1. 1
    Step 1

    For terminals, FIRST(aa) = {aa} and FIRST(bb) = {bb}.

  2. 2
    Step 2

    Since aAaA begins with aa, add aa. Since ϵ\epsilon is an explicit production, add ϵ\epsilon.

  3. 3
    Step 3

    Analogously, add bb and ϵ\epsilon.

  4. 4
    Step 4

    Add FIRST(A) without ε → {aa}. Because ε ∈ FIRST(A), also add FIRST(B) without ε → {bb}. Add ε to FIRST(S) only if both A and B can derive ε.

  5. 5
    Step 5

    FIRST(AA)={a,ε}, FIRST(BB)={b,ε}, FIRST(SS)={a,b,ε}.

Step 2: Compute FOLLOW sets

Use the standard FOLLOW rules for CFGs:

  • \ \in \text{FOLLOW}(S)forthestartsymbolfor the start symbolS$.
  • For any production XαYβX \rightarrow \alpha Y \beta, everything in FIRST(β\beta) except ϵ\epsilon is added to FOLLOW(YY).
  • If ϵFIRST(β)\epsilon \in \text{FIRST}(\beta) (or β\beta can be empty), then FOLLOW(XX) is added to FOLLOW(YY). (This is the standard propagation rule used in FOLLOW computation.)

Now apply to each production:

From SABS \rightarrow AB

  • In ABAB, the non-terminal AA is followed by BB.

    • Add FIRST(B){ϵ}\text{FIRST}(B) \setminus \{\epsilon\} to FOLLOW(AA).
    • FIRST(B)={b,ϵ}\text{FIRST}(B) = \{b,\epsilon\}, so FIRST(B){ϵ}={b}\text{FIRST}(B)\setminus\{\epsilon\} = \{b\}.
    • Since ϵFIRST(B)\epsilon \in \text{FIRST}(B), also add FOLLOW(SS) to FOLLOW(AA).
  • For BB in ABAB:

    • BB is at the end of the production, so everything in FOLLOW(SS) is added to FOLLOW(BB).

Initialize:

  • FOLLOW(SS) contains $$$.

So:

  • FOLLOW(AA) includes {b}\{b\} and also FOLLOW(SS) which includes \$$ → FOLLOW(A)=)= {b,$}$
  • FOLLOW(BB) includes FOLLOW(SS) → FOLLOW(BB)= \{\}$

We can present results:

  • \text{FOLLOW}(S)=\{\}$
  • \text{FOLLOW}(A)=\{b,\}$
  • \text{FOLLOW}(B)=\{\}$

Footnotes

  1. FOLLOW(S) includes end-of-input marker forstartsymbolhttps://en.wikipedia.org/wiki/LLparserNotestheuseoffor start symbol - https://en.wikipedia.org/wiki/LL_parser - Notes the use of in parsing table construction for LL(1).

  2. FIRST and FOLLOW sets definitions and FOLLOW propagation rules - https://en.wikipedia.org/wiki/Context-free_grammar - Includes discussion of FIRST/FOLLOW and ε interactions in parsing algorithms.

FOLLOW sets (systematic derivation)

  1. 1
    Step 1

    By convention, \ \in \text{FOLLOW}(S)$.

  2. 2
    Step 2

    A is followed by B, so add FIRST(B){ε} = {b} to FOLLOW(A).

  3. 3
    Step 3

    Because ε ∈ FIRST(B), add FOLLOW(S) to FOLLOW(A).

  4. 4
    Step 4

    B is at the end, so add FOLLOW(S) to FOLLOW(B).

  5. 5
    Step 5

    FOLLOW(S)={\}, FOLLOW(A)={b,\}, FOLLOW(B)={\}.

Step 3: Build the LL(1) parsing table

We use the standard LL(1) table construction: For each production XγX \rightarrow \gamma:

  • For each terminal aFIRST(γ)a \in \text{FIRST}(\gamma), add XγX \rightarrow \gamma to table entry M[X,a]M[X,a].
  • If ϵFIRST(γ)\epsilon \in \text{FIRST}(\gamma), then for each terminal bFOLLOW(X)b \in \text{FOLLOW}(X), add XγX \rightarrow \gamma to M[X,b]M[X,b].2

Non-terminals: {S,A,B}\{S,A,B\}
Terminals: \{a,b,\}(weinclude(we include$$ as lookahead for table entries)

Productions:

  • SABS \rightarrow AB
  • AaAA \rightarrow aA
  • AϵA \rightarrow \epsilon
  • BbBB \rightarrow bB
  • BϵB \rightarrow \epsilon

Fill table rows

Row S: SABS \rightarrow AB
  • FIRST(AB)\text{FIRST}(AB) is FIRST(S)={a,b,ϵ}\text{FIRST}(S) = \{a,b,\epsilon\}
  • So:
    • Add SABS \rightarrow AB to M[S,a]M[S,a]
    • Add SABS \rightarrow AB to M[S,b]M[S,b]
  • Because ϵFIRST(AB)\epsilon \in \text{FIRST}(AB), add SABS \rightarrow AB to M[S,\]foreveryfor every $inFOLLOW( in FOLLOW(S$).
    • FOLLOW(SS) = {\$$} so only M[S,$]$ gets it.
Row A: productions AaAA \rightarrow aA and AϵA \rightarrow \epsilon
  • For AaAA \rightarrow aA:
    • FIRST(aA)={a}\text{FIRST}(aA)=\{a\}M[A,a]=AaAM[A,a] = A \rightarrow aA
  • For AϵA \rightarrow \epsilon:
    • Since ϵFIRST(ϵ)\epsilon \in \text{FIRST}(\epsilon), add this production to all M[A,b]M[A,b] where bFOLLOW(A)b \in \text{FOLLOW}(A).
    • FOLLOW(AA) = \{b,\}$
    • So M[A,b]=AϵM[A,b] = A \rightarrow \epsilon and M[A,\] = A \rightarrow \epsilon$
Row B: productions BbBB \rightarrow bB and BϵB \rightarrow \epsilon
  • For BbBB \rightarrow bB:
    • FIRST(bB)={b}\text{FIRST}(bB)=\{b\}M[B,b]=BbBM[B,b] = B \rightarrow bB
  • For BϵB \rightarrow \epsilon:
    • Add to M[B,\]sinceFOLLOW( since FOLLOW(B) = {$$}
    • So M[B,\] = B \rightarrow \epsilon$

Footnotes

  1. LL(1) parsing table construction with FIRST/FOLLOW (standard rule) - https://en.wikipedia.org/wiki/LL_parser - Describes LL parsing table population using FIRST/FOLLOW and ε handling.

  2. FIRST and FOLLOW sets definitions and FOLLOW propagation rules - https://en.wikipedia.org/wiki/Context-free_grammar - Includes discussion of FIRST/FOLLOW and ε interactions in parsing algorithms.

LL(1) parsing table entries (conceptual fill)

Each cell shows which production is selected (blank = error entry).

To avoid encoding limitations, here is the parsing table explicitly as a grid:

Non-terminal \ Lookaheadaabb$$$
SSSABS \rightarrow ABSABS \rightarrow ABSABS \rightarrow AB
AAAaAA \rightarrow aAAϵA \rightarrow \epsilonAϵA \rightarrow \epsilon
BBBbBB \rightarrow bBBϵB \rightarrow \epsilon

(“—” means no production added; the parser would treat it as an error for that lookahead.)

Common pitfall

When ε ∈ FIRST(γ), you must place the production in every table cell M[X,b] for b ∈ FOLLOW(X); forgetting FOLLOW propagation is a frequent mistake. (This rule is part of standard LL(1) table construction.)

Footnotes

  1. LL(1) parsing table construction with FIRST/FOLLOW (standard rule) - https://en.wikipedia.org/wiki/LL_parser - Describes LL parsing table population using FIRST/FOLLOW and ε handling.

LL(1) conflict check

A grammar is LL(1) iff each table cell M[X,a] contains at most one production. If two different productions land in the same cell, the grammar is not LL(1).2

Footnotes

  1. LL(1) parsing table construction with FIRST/FOLLOW (standard rule) - https://en.wikipedia.org/wiki/LL_parser - Describes LL parsing table population using FIRST/FOLLOW and ε handling.

  2. FIRST and FOLLOW sets definitions and FOLLOW propagation rules - https://en.wikipedia.org/wiki/Context-free_grammar - Includes discussion of FIRST/FOLLOW and ε interactions in parsing algorithms.

Workflow to decide LL(1)

Compute FIRST

1

Determine starting terminals (and whether ε is possible) for each non-terminal."

Compute FOLLOW

2

Track which terminals can follow each non-terminal; add \ to FOLLOW(S)."

Build LL(1) table

3

Use FIRST for non-ε productions and FOLLOW when ε can be derived."

Check LL(1) property

4

Ensure no cell has conflicting multiple productions."

Step 4: Determine whether the grammar is LL(1)

We check for conflicts in the constructed LL(1) table.

List filled cells:

  • M[S,a]M[S,a] has only SABS \rightarrow AB
  • M[S,b]M[S,b] has only SABS \rightarrow AB
  • M[S,\]hasonlyhas onlyS \rightarrow AB$
  • M[A,a]M[A,a] has only AaAA \rightarrow aA
  • M[A,b]M[A,b] has only AϵA \rightarrow \epsilon
  • M[A,\]hasonlyhas onlyA \rightarrow \epsilon$
  • M[B,b]M[B,b] has only BbBB \rightarrow bB
  • M[B,\]hasonlyhas onlyB \rightarrow \epsilon$

No cell receives two different productions. Therefore, the grammar is LL(1).

Sanity checks and interpretation

Knowledge Check

Question 1 of 4
Q1Single choice

For the production AϵA \rightarrow \epsilon, which set determines where this production goes in the LL(1) parsing table?