Predictive Parsing Table Construction for a Classic Expression Grammar (LL(1))
We are given the grammar (intended as an LL(1) expression grammar):
Interpreting the “/” as alternative productions, this means:
A predictive parsing table is typically built for an grammar using FIRST and FOLLOW sets, and the table is filled using the grammar’s productions and these sets. The standard method relies on the fact that each cell has at most one production for parsing.
Because the grammar uses direct left recursion ( and ), predictive parsing tables are constructed after transforming to an equivalent grammar (eliminating left recursion and left-factoring where needed). Left-recursion elimination is a standard prerequisite for table-driven predictive parsing.
Key terms
- LL(1)
- FIRST
- FOLLOW
- Predictive parsing table
Mermaid view of the structure (operators by precedence):
Footnotes
-
(LL(1) parse table construction using FIRST/FOLLOW) - https://en.wikipedia.org/wiki/LL_parser - Explains predictive parsing tables and the use of FIRST/FOLLOW and handling. ↩
-
(Left recursion elimination and expression grammar transformation) - https://en.wikipedia.org/wiki/Left_recursion - Describes elimination of left recursion and standard transformation patterns. ↩
LL(1) Parsing: FIRST/FOLLOW and Parse Table (Theory)
Step 1: Convert to an grammar (eliminate left recursion)
A common equivalent form introduces helper nonterminals so that:
- addition () is handled by an suffix chain
- multiplication () is handled by a suffix chain
Use the transformations (standard form for left-recursive expressions):
Start from:
Eliminate left recursion:
- For :
- For :
- stays the same:
So the grammar is:
This is the classical expression grammar that is LL(1) for operators and parentheses/identifiers.
[CalloutBlock]{type="tip"} title: "Pro Tip" content: "For predictive parsing tables, you almost always need the form. If a grammar is left-recursive, first eliminate left recursion (and left-factor if needed), then compute FIRST/FOLLOW and fill the table."
Footnotes
-
(Left recursion elimination and expression grammar transformation) - https://en.wikipedia.org/wiki/Left_recursion - Describes elimination of left recursion and standard transformation patterns. ↩
Construct the Predictive Parsing Table (LL(1) workflow)
- 1Step 1
For every nonterminal , compute FIRST(A) using productions and propagation.
- 2Step 2
Compute FOLLOW(A) for each nonterminal. Put end-marker \ in FOLLOW of the start symbol.
- 3Step 3
For each production : for each terminal put into table cell . If , then for each put into .
- 4Step 4
Ensure each table cell has at most one production; otherwise the grammar is not LL(1) as-is.
Step 2: Compute FIRST sets
Terminals: and end-marker .
Nonterminals: .
From productions:
-
- FIRST
-
- FIRST
-
- FIRST
-
- FIRST
-
- FIRST
Step 3: Compute FOLLOW sets
Start symbol is , so:
- FOLLOW contains .
We also use standard FOLLOW propagation rules:
- If there is a production , then everything in FIRST except goes into FOLLOW.
- If or is empty, then FOLLOW is included in FOLLOW.
Compute systematically:
- From :
- FOLLOW includes FIRST excluding :
- FIRST
- so add to FOLLOW
- Because , also add FOLLOW to FOLLOW:
- FOLLOW currently contains (and later may get )
- From :
- Again, is followed by , so:
- add to FOLLOW (already)
- and since , add FOLLOW to FOLLOW
- But FOLLOW depends on where appears:
- appears only in at the end, so FOLLOW includes FOLLOW.
- From :
- FOLLOW includes FIRST excluding :
- FIRST
- so add to FOLLOW
- Since , add FOLLOW to FOLLOW
- From :
- is followed by , so:
- add to FOLLOW
- and since , add FOLLOW to FOLLOW
- appears at the end of , so FOLLOW includes FOLLOW.
- From :
- is followed by , so add to FOLLOW.
Now assemble final FOLLOW sets:
- FOLLOW, ')'}$
- FOLLOW(E)={\
- FOLLOW, ')'}$
- FOLLOW(T)={ '+', \
- FOLLOW, ')'}$
These computations follow the standard FIRST/FOLLOW table-construction methodology for LL(1) parsing. 2
Footnotes
-
(LL(1) parse table construction using FIRST/FOLLOW) - https://en.wikipedia.org/wiki/LL_parser - Explains predictive parsing tables and the use of FIRST/FOLLOW and handling. ↩
-
(FIRST and FOLLOW set definitions and algorithms) - https://en.wikipedia.org/wiki/LL_parser#First_and_follow - Provides the FIRST/FOLLOW set computation concepts used for LL parsing tables. ↩
Step 4: Fill the predictive parsing table
We build where:
- is a nonterminal
- is a lookahead terminal in }$
Rules for table entries are exactly:
Productions:
- FIRST
- so:
- FIRST
- production: place in all columns in FOLLOW,')'}$
- ]=E' \rightarrow \epsilon$
- FIRST
- FIRST
- place in all columns in FOLLOW }$
- ]=T' \rightarrow \epsilon$
Footnotes
-
(LL(1) parse table construction using FIRST/FOLLOW) - https://en.wikipedia.org/wiki/LL_parser - Explains predictive parsing tables and the use of FIRST/FOLLOW and handling. ↩
Predictive Parsing Table M[A,a] for the LL(1) Expression Grammar
Empty cells are error/undefined. Rows are nonterminals; columns are lookahead terminals.
Table in explicit form (using productions)
| Nonterminal \ Lookahead | ||||||
|---|---|---|---|---|---|---|
| — | — | — | — | |||
| — | — | — | ||||
| — | — | — | — | |||
| — | — | |||||
| — | — | — | — |
This table is conflict-free for the LL(1) grammar above, enabling one-token lookahead predictive parsing. 2
[CalloutBlock]{type="warning"} title: "Warning" content: "The table above is for the transformed LL(1) grammar (). If you try to build a predictive table directly from the left-recursive form (), you will not get a valid LL(1) parsing table."
Footnotes
-
(LL(1) parse table construction using FIRST/FOLLOW) - https://en.wikipedia.org/wiki/LL_parser - Explains predictive parsing tables and the use of FIRST/FOLLOW and handling. ↩
-
(FIRST and FOLLOW set definitions and algorithms) - https://en.wikipedia.org/wiki/LL_parser#First_and_follow - Provides the FIRST/FOLLOW set computation concepts used for LL parsing tables. ↩
From Left-Recursive Grammar to Predictive Table
Rewrite grammar
1Introduce and to eliminate left recursion."
Compute FIRST
2Compute terminals that can start derivations from each nonterminal."
Compute FOLLOW
3Propagate lookaheads to nonterminals; add \ to FOLLOW."
Fill table
4Use FIRST/ and FOLLOW membership rules to place productions."
Common Edge Cases in Table Construction
Knowledge Check
For a production with , which FOLLOW set determines where to place this production in the predictive parsing table?