FIRST, FOLLOW, and LL(1) Parsing Table for a Grammar with ε-Productions
We have the grammar:
Let be the set of terminals that can appear first in strings derived from , and be the set of terminals that can appear immediately to the right of in some sentential form. These sets are used to build the LL(1) parsing table with the rule: for a production ,
- if then ,
- if then for every , set .
Key keywords: LL(1) , FIRST , FOLLOW , ε-production , Parsing table.
LL(1) FIRST/FOLLOW and Parsing Table (construction walkthrough)
Compute FIRST sets
- 1Step 1
A → aA | ε, so FIRST(A) = {a, ε}.
- 2Step 2
B → bB | ε, so FIRST(B) = {b, ε}.
- 3Step 3
C → cC | d, so FIRST(C) = {c, d} (no ε).
- 4Step 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
- 1Step 1
By convention for LL parsing, \ (end marker) is in FOLLOW(S). So FOLLOW(S) = {$ }.
- 2Step 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).
- 3Step 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.
- 4Step 4
So FOLLOW(A) gets FIRST(BC) \u2212 {ε} = {b, c, d}. Since ε \u2208 FIRST(BC) is false, FOLLOW(S) is NOT added.
- 5Step 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.
- 6Step 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)
FOLLOW sets (final)
- FOLLOW(S) = \\{\\\}$
- 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: \}$.
We list each production and the lookahead symbols that trigger it:
Productions
Use rules
- For with : fill for .
- For with : fill for all .
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.
| Nonterminal | lookahead a | lookahead b | lookahead c | lookahead d | lookahead $ |
|---|---|---|---|---|---|
| — | — | ||||
| — | — | ||||
| — | — | ||||
| — | — | — |
Why these entries?
- and no ε, so all get .
- For : so gets it.
- For : ε ∈ FIRST(ε), so use FOLLOW(A)=\{b,c,d\}; thus and get (and too, but that table cell is in conflict with how could also derive aA? Actually only applies at lookahead a, so is safe; in our explicit table above we left blank, so we correct it now: .)
- For : .
- For : FOLLOW(B)=\{c,d\}; so and .
Corrected explicit table (with the missing ε entry fixed)
| Nonterminal | lookahead a | lookahead b | lookahead c | lookahead d | lookahead $ |
|---|---|---|---|---|---|
| — | |||||
| — | |||||
| — | — | ||||
| — | — | — |
LL(1) parsing steps for input aabd
- 1Step 1
Put \ on stack bottom, then start symbol on top. Input is a a b d \.
- 2Step 2
Top is , lookahead is . Use . Replace by .
- 3Step 3
Top is , lookahead is . Use . Match terminal .
- 4Step 4
Top is , lookahead is next . Again use and match .
- 5Step 5
Now lookahead is and top is . Use and match .
- 6Step 6
Now lookahead is and top is . Use and match .
- 7Step 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.
| Step | Stack (top→bottom) | Input (current→end) | Action |
|---|---|---|---|
| 0 | Start | ||
| 1 | (on lookahead ) | ||
| 2 | |||
| 3 | match | ||
| 4 | (lookahead ) | ||
| 5 | match | ||
| 6 | |||
| 7 | match | ||
| 8 | |||
| 9 | match | ||
| 10 | (empty) | (empty) | accept |
Common edge cases with ε in LL(1)
Knowledge Check
What is for the grammar , , , ?
Explore Related Topics
Semantic Actions in Context-Free Grammars: Deriving Output for Input `aadbd`
Ambiguity in the Grammar \(S \rightarrow ABA,\; A \rightarrow aA \mid \epsilon,\; B \rightarrow bB \mid \epsilon\)
The grammar (S\rightarrow ABA,;A\rightarrow aA\mid\epsilon,;B\rightarrow bB\mid\epsilon) is ambiguous because the two (A) nonterminals can distribute the same (a)-string in multiple ways, especially when (B) derives (\epsilon).
- It generates the language (L(S)={a^i b^j a^k\mid i,j,k\ge0}=a^*b^a^).
- The shortest string (a) has two distinct left‑most derivations (or parse trees), proving ambiguity.
- Every string (a^n) with (n\ge1) can be split between the two (A) symbols in (n+1) ways, yielding multiple parse trees.
- The ambiguity stems from both (A) producing (a^*) and (B) being able to vanish via (\epsilon).
- An equivalent unambiguous grammar can be constructed, showing the ambiguity is a property of this grammar, not necessarily of the language.
Constructing an LALR(1) Parsing Table for a Given Grammar (and Proving It Is Not SLR(1))