FIRST/FOLLOW, LL(1) Parsing Table, and LL(1) Determination for a Given CFG
We are given the CFG:
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 denote the empty string.
- Let end-of-input be the marker \$$, and for the start symbol S$ \in \text{FOLLOW}(S)$. (This is the standard setup for FOLLOW-based table construction.)
Footnotes
-
FOLLOW(S) includes end-of-input marker 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:
Because can either start with terminal (via ) or be empty (via ), we get:
Similarly, for :
Now compute from using the standard FIRST-of-concatenation rule:
- Start with
- If , then also include (excluding unless both can derive )
Since , we include as well:
- From producing , can begin with what begins with: .
- can produce only if both and derive .
Thus:
FIRST sets (systematic derivation)
- 1Step 1
For terminals, FIRST() = {} and FIRST() = {}.
- 2Step 2
Since begins with , add . Since is an explicit production, add .
- 3Step 3
Analogously, add and .
- 4Step 4
Add FIRST(A) without ε → {}. Because ε ∈ FIRST(A), also add FIRST(B) without ε → {}. Add ε to FIRST(S) only if both A and B can derive ε.
- 5Step 5
FIRST()={a,ε}, FIRST()={b,ε}, FIRST()={a,b,ε}.
Step 2: Compute FOLLOW sets
Use the standard FOLLOW rules for CFGs:
- \ \in \text{FOLLOW}(S)S$.
- For any production , everything in FIRST() except is added to FOLLOW().
- If (or can be empty), then FOLLOW() is added to FOLLOW(). (This is the standard propagation rule used in FOLLOW computation.)
Now apply to each production:
From
-
In , the non-terminal is followed by .
- Add to FOLLOW().
- , so .
- Since , also add FOLLOW() to FOLLOW().
-
For in :
- is at the end of the production, so everything in FOLLOW() is added to FOLLOW().
Initialize:
- FOLLOW() contains $$$.
So:
- FOLLOW() includes and also FOLLOW() which includes \$$ → FOLLOW(A{b,$}$
- FOLLOW() includes FOLLOW() → FOLLOW()= \{\}$
We can present results:
- \text{FOLLOW}(S)=\{\}$
- \text{FOLLOW}(A)=\{b,\}$
- \text{FOLLOW}(B)=\{\}$
Footnotes
-
FOLLOW(S) includes end-of-input marker in parsing table construction for LL(1). ↩
-
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)
- 1Step 1
By convention, \ \in \text{FOLLOW}(S)$.
- 2Step 2
A is followed by B, so add FIRST(B){ε} = {b} to FOLLOW(A).
- 3Step 3
Because ε ∈ FIRST(B), add FOLLOW(S) to FOLLOW(A).
- 4Step 4
B is at the end, so add FOLLOW(S) to FOLLOW(B).
- 5Step 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 :
- For each terminal , add to table entry .
- If , then for each terminal , add to .2
Non-terminals:
Terminals: \{a,b,\}$$ as lookahead for table entries)
Productions:
Fill table rows
Row S:
- is
- So:
- Add to
- Add to
- Because , add to M[S,\] $S$).
- FOLLOW() = {\$$} so only M[S,$]$ gets it.
Row A: productions and
- For :
- →
- For :
- Since , add this production to all where .
- FOLLOW() = \{b,\}$
- So and M[A,\] = A \rightarrow \epsilon$
Row B: productions and
- For :
- →
- For :
- Add to M[B,\]B) = {$$}
- So M[B,\] = B \rightarrow \epsilon$
Footnotes
-
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. ↩
-
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 \ Lookahead | $$$ | ||
|---|---|---|---|
| — |
(“—” 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
-
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
-
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. ↩
-
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
1Determine starting terminals (and whether ε is possible) for each non-terminal."
Compute FOLLOW
2Track which terminals can follow each non-terminal; add \ to FOLLOW(S)."
Build LL(1) table
3Use FIRST for non-ε productions and FOLLOW when ε can be derived."
Check LL(1) property
4Ensure 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:
- has only
- has only
- M[S,\]S \rightarrow AB$
- has only
- has only
- M[A,\]A \rightarrow \epsilon$
- has only
- M[B,\]B \rightarrow \epsilon$
No cell receives two different productions. Therefore, the grammar is LL(1).
Sanity checks and interpretation
Knowledge Check
For the production , which set determines where this production goes in the LL(1) parsing table?