FIRST, FOLLOW, and LL(1) Parsing Table for the Given Expression Grammar
We are given the grammar (with terminals and start symbol ):
\\begin{aligned} E &\\rightarrow TE' \\\\ E' &\\rightarrow +TE' \\mid \\epsilon \\\\ T &\\rightarrow FT' \\\\ T' &\\rightarrow *FT' \\mid \\epsilon \\\\ F &\\rightarrow (E) \\mid id \\end{aligned}We will compute:
- [keyword]{def="FIRST(A) is the set of terminals that can begin strings derived from A, possibly including "}
- keyword
- the keyword
Finally, we derive the LL(1) parsing table.
LL(1) Parsing Table (FIRST & FOLLOW) - Simple Explanation
Key definitions used
- keyword
- [keyword]{def="If , then can be derived from α"}
- keyword
- [keyword]{def="LL(1) table entry uses FIRST(α); if then use FOLLOW(A)"}
Step 1 — Compute FIRST sets
- 1Step 1
Use each nonterminal’s productions: , , , , .
- 2Step 2
Since starts with and starts with , we get .
- 3Step 3
starts with , and contributes , so .
- 4Step 4
. Because and neither production of derives , we have .
- 5Step 5
starts with , and contributes , so .
- 6Step 6
. Since and cannot derive (because and cannot derive ), we get .
FIRST summary
Step 2 — Compute FOLLOW sets
- 1Step 1
Take \ as the end-marker (common LL(1) convention): since is the start symbol, \ is in .
- 2Step 2
Scan each production for every appearance of a nonterminal and apply FOLLOW rules based on what can come after it.
- 3Step 3
In , is followed by so is added to . Because , add to .
- 4Step 4
In , is followed by . Again add to . Since , add to (we will compute from other placements).
- 5Step 5
This production has no symbols after , so its effect is already captured via positions where appears (not by the empty rule itself).
- 6Step 6
In , is followed by . Add to . Since , add to .
- 7Step 7
In , is followed by . Add to . Since , add to .
- 8Step 8
appears at the end of , so . Also appears at the end of , so (self-consistent).
- 9Step 9
appears at the end of , so , and also at the end of , giving self-consistency.
Solving the FOLLOW equations
Let’s denote:
- FOLLOW(E)=\\{\\\}\,E\EF\to (E)$, which changes this.)
- From : the symbol after is , so add to .
So:
FOLLOW(E)=\\{\\$,\\,)\\}.Now use :
- is followed by , so add to .
- Because , add to .
Thus:
FOLLOW(T)=\\{+,\\$,\\,)\\}.Now :
- is followed by , so add to .
- Because , add to .
So:
FOLLOW(F)=\\{*,\\, +,\\$,\\,)\\}.Now derive :
- appears at end of , hence FOLLOW(E')\\supseteq FOLLOW(E)=\\{\\,\,)\}$.
- No other terminal is added to from other contexts (it is not followed by a terminal in any production where it occurs before the end).
So:
FOLLOW(E')=\\{\\$,\\,)\\}.Finally :
- appears at end of , so FOLLOW(T')\\supseteq FOLLOW(T)=\\{+,\\,\,)\}$. Thus:
FOLLOW summary
- FOLLOW(E)=\\{\\,\,)\}$
- FOLLOW(E')=\\{\\,\,)\}$
- FOLLOW(T)=\\{+,\\,\,)\}$
- FOLLOW(T')=\\{+,\\,\,)\}$
- FOLLOW(F)=\\{*,\\,+,\\,\,)\}$
Quick visual: how affects FOLLOW
Because , symbols that can follow also become possible followers of in . Similarly, because , followers of can follow in .
Pro Tip
When a nonterminal is followed by a suffix that can derive , you must union of the left-hand side into the of the nonterminal (not just ).
Common Pitfall
Do not add to FOLLOW sets. FOLLOW contains terminals only (and \). is used only to decide whether to propagate FOLLOW across nullable suffixes.
Step 3 — Derive the LL(1) parsing table
- 1Step 1
For each production : for each , set . If , then for each set .
- 2Step 2
Production: . . So and .
- 3Step 3
Productions: and . so . For : since , add it over FOLLOW(E')=\{\,,)}M[E',$$]=E'\to\epsilonM[E',)]=E'\to\epsilon$.
- 4Step 4
Production: . . So and .
- 5Step 5
Productions: and . so . For , use FOLLOW(T')=\{+,\,,)}M[T',+]=T'\to\epsilonM[T',$$]=T'\to\epsilonM[T',)]=T'\to\epsilon$.
- 6Step 6
Productions: and . So and .
LL(1) Parsing Table (M)
Rows are nonterminals, columns are lookahead terminals.
Parsing table as production listings
Let terminals be columns: .
Nonterminal :
Nonterminal :
- \
Nonterminal :
Nonterminal :
- \
Nonterminal :
All other cells are error/undefined.
Workflow to Build LL(1) Table
FIRST sets
1Determine which terminals can begin each nonterminal’s derivations."
FOLLOW sets
2Propagate terminal followers using -nullable suffix logic."
LL(1) table
3Use for normal entries; use when is possible."
Why this grammar is LL(1) (intuition)
FIRST/FOLLOW/LL(1) Self-Check Deck
Knowledge Check
For the grammar, what is ?