Predictive Parsing Table Construction for a Classic Expression Grammar (LL(1))

Predictive Parsing Table Construction for a Classic Expression Grammar (LL(1))

Verified Sources
Nov 4, 2026

We are given the grammar (intended as an LL(1) expression grammar):

  • E→E+T/TE \rightarrow E+T/T
  • T→T∗F/FT \rightarrow T*F/F
  • F→(E)/idF \rightarrow (E)/id

Interpreting the “/” as alternative productions, this means:

  • E→E+T∣TE \rightarrow E+T \mid T
  • T→T∗F∣FT \rightarrow T*F \mid F
  • F→(E)∣idF \rightarrow (E) \mid id

A predictive parsing table is typically built for an LL(1)LL(1) 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 (A,a)(A,a) has at most one production for LL(1)LL(1) parsing.

Because the grammar uses direct left recursion (E→E+TE \rightarrow E+T and T→T∗FT \rightarrow T*F), predictive parsing tables are constructed after transforming to an equivalent LL(1)LL(1) 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

  1. (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 epsilon\\epsilon handling. ↩

  2. (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 LL(1)LL(1) grammar (eliminate left recursion)

A common equivalent LL(1)LL(1) form introduces helper nonterminals so that:

  • addition (++) is handled by an E′E' suffix chain
  • multiplication (∗*) is handled by a T′T' suffix chain

Use the transformations (standard form for left-recursive expressions):

Start from:

  • E→E+T∣TE \rightarrow E+T \mid T
  • T→T∗F∣FT \rightarrow T*F \mid F
  • F→(E)∣idF \rightarrow (E) \mid id

Eliminate left recursion:

  1. For EE:
  • E→T E′E \rightarrow T\,E'
  • E′→+ T E′∣ϵE' \rightarrow +\,T\,E' \mid \epsilon
  1. For TT:
  • T→F T′T \rightarrow F\,T'
  • T′→∗ F T′∣ϵT' \rightarrow *\,F\,T' \mid \epsilon
  1. FF stays the same:
  • F→(E)∣idF \rightarrow (E) \mid id

So the LL(1)LL(1) grammar is:

  • E→TE′E \rightarrow T E'
  • E′→+TE′∣ϵE' \rightarrow + T E' \mid \epsilon
  • T→FT′T \rightarrow F T'
  • T′→∗FT′∣ϵT' \rightarrow * F T' \mid \epsilon
  • F→(E)∣idF \rightarrow (E) \mid id

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 LL(1)LL(1) 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

  1. (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)

  1. 1
    Step 1

    For every nonterminal AA, compute FIRST(A) using productions and ϵ\epsilon propagation.

  2. 2
    Step 2

    Compute FOLLOW(A) for each nonterminal. Put end-marker \ in FOLLOW of the start symbol.

  3. 3
    Step 3

    For each production A→αA \rightarrow \alpha: for each terminal a∈FIRST(α)a \in FIRST(\alpha) put A→αA \rightarrow \alpha into table cell (A,a)(A,a). If ϵ∈FIRST(α)\epsilon \in FIRST(\alpha), then for each b∈FOLLOW(A)b \in FOLLOW(A) put A→αA \rightarrow \alpha into (A,b)(A,b).

  4. 4
    Step 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: (,),+,∗,id(, ), +, *, id and end-marker \\.

Nonterminals: E,E′,T,T′,FE, E', T, T', F.

From productions:

  • F→(E)∣idF \rightarrow (E) \mid id

    • FIRST(F)={′(′,id}(F)=\{ '(', id \}
  • T→FT′T \rightarrow F T'

    • FIRST(T)=FIRST(F)={′(′,id}(T)=\text{FIRST}(F)=\{ '(', id \}
  • T′→∗FT′∣ϵT' \rightarrow * F T' \mid \epsilon

    • FIRST(T′)={′∗′,ϵ}(T')=\{ '*', \epsilon \}
  • E→TE′E \rightarrow T E'

    • FIRST(E)=FIRST(T)={′(′,id}(E)=\text{FIRST}(T)=\{ '(', id \}
  • E′→+TE′∣ϵE' \rightarrow + T E' \mid \epsilon

    • FIRST(E′)={′+′,ϵ}(E')=\{ '+', \epsilon \}

Step 3: Compute FOLLOW sets

Start symbol is EE, so:

  • FOLLOW(E)(E) contains \\.

We also use standard FOLLOW propagation rules:

  • If there is a production A→alphaBbetaA \rightarrow \\alpha B \\beta, then everything in FIRST(beta)(\\beta) except ϵ\epsilon goes into FOLLOW(B)(B).
  • If ϵinFIRST(beta)\epsilon \\in FIRST(\\beta) or beta\\beta is empty, then FOLLOW(A)(A) is included in FOLLOW(B)(B).

Compute systematically:

  1. From E→TE′E \rightarrow T E':
  • FOLLOW(T)(T) includes FIRST(E′)(E') excluding ϵ\epsilon:
    • FIRST(E′)={′+′,ϵ}(E')=\{ '+', \epsilon \}
    • so add ++ to FOLLOW(T)(T)
  • Because ϵinFIRST(E′)\epsilon \\in FIRST(E'), also add FOLLOW(E)(E) to FOLLOW(T)(T):
    • FOLLOW(E)(E) currently contains \\ (and later may get )))
  1. From E′→+TE′E' \rightarrow + T E':
  • Again, TT is followed by E′E', so:
    • add ++ to FOLLOW(T)(T) (already)
    • and since ϵinFIRST(E′)\epsilon \\in FIRST(E'), add FOLLOW(E′)(E') to FOLLOW(T)(T)
  • But FOLLOW(E′)(E') depends on where E′E' appears:
    • E′E' appears only in E→TE′E \rightarrow T E' at the end, so FOLLOW(E′)(E') includes FOLLOW(E)(E).
  1. From T→FT′T \rightarrow F T':
  • FOLLOW(F)(F) includes FIRST(T′)(T') excluding ϵ\epsilon:
    • FIRST(T′)={′∗′,ϵ}(T')=\{ '*', \epsilon \}
    • so add ∗* to FOLLOW(F)(F)
  • Since ϵinFIRST(T′)\epsilon \\in FIRST(T'), add FOLLOW(T)(T) to FOLLOW(F)(F)
  1. From T′→∗FT′T' \rightarrow * F T':
  • FF is followed by T′T', so:
    • add ∗* to FOLLOW(F)(F)
    • and since ϵinFIRST(T′)\epsilon \\in FIRST(T'), add FOLLOW(T′)(T') to FOLLOW(F)(F)
  • T′T' appears at the end of T→FT′T \rightarrow F T', so FOLLOW(T′)(T') includes FOLLOW(T)(T).
  1. From F→(E)F \rightarrow (E):
  • EE is followed by )), so add )) to FOLLOW(E)(E).

Now assemble final FOLLOW sets:

  • FOLLOW(E)={(E)=\{\\, ')'}$
  • FOLLOW(E′)=FOLLOW(E')=FOLLOW(E)={\,′)′}, ')'\}
  • FOLLOW(T)={′+′,(T)=\{ '+', \\, ')'}$
  • FOLLOW(T′)=FOLLOW(T')=FOLLOW(T)={ '+', \′,′)′}', ')'\}
  • FOLLOW(F)={′∗′,′+′,(F)=\{ '*', '+', \\, ')'}$

These computations follow the standard FIRST/FOLLOW table-construction methodology for LL(1) parsing. 2

Footnotes

  1. (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 epsilon\\epsilon handling. ↩

  2. (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 M[A,a]M[A,a] where:

  • AA is a nonterminal
  • aa is a lookahead terminal in {′(′,′)′,′+′,′∗′,id,\{ '(', ')', '+', '*', id, \\ }$

Rules for table entries are exactly:

  • If A→αA \rightarrow \alpha and a∈FIRST(alpha)a \in FIRST(\\alpha), then M[A,a]=ArightarrowalphaM[A,a]=A\\rightarrow\\alpha.
  • If ϵ∈FIRST(alpha)\epsilon \in FIRST(\\alpha), then for all binFOLLOW(A)b \\in FOLLOW(A), set M[A,b]=ArightarrowalphaM[A,b]=A\\rightarrow\\alpha.

Productions:

  1. E→TE′E \rightarrow T E'
  • FIRST(TE′)={′(′,id}(T E') = \{ '(', id \}
  • so:
    • M[E,′(′]=ErightarrowTE′M[E,'(']=E \\rightarrow T E'
    • M[E,id]=ErightarrowTE′M[E,id]=E \\rightarrow T E'
  1. E′→+TE′E' \rightarrow + T E'
  • FIRST(+TE′)={′+′}(+ T E')=\{ '+' \}
    • M[E′,′+]=E′rightarrow+TE′M[E','+]=E' \\rightarrow + T E'
  1. E′→ϵE' \rightarrow \epsilon
  • ϵ\epsilon production: place in all columns in FOLLOW(E′)={(E')=\{\\,')'}$
    • M[E′,′)]=E′rightarrowepsilonM[E',')]=E' \\rightarrow \\epsilon
    • M[E′,′M[E','\\]=E' \rightarrow \epsilon$
  1. T→FT′T \rightarrow F T'
  • FIRST(FT′)={′(′,id}(F T')=\{ '(', id \}
    • M[T,′(′]=TrightarrowFT′M[T,'(']=T \\rightarrow F T'
    • M[T,id]=TrightarrowFT′M[T,id]=T \\rightarrow F T'
  1. T′→∗FT′T' \rightarrow * F T'
  • FIRST(∗FT′)={′∗′}( * F T')=\{ '*' \}
    • M[T′,′∗]=T′rightarrow∗FT′M[T','*]=T' \\rightarrow * F T'
  1. T′→ϵT' \rightarrow \epsilon
  • place in all columns in FOLLOW(T′)={′+′,′)′,(T')=\{ '+', ')', \\ }$
    • M[T′,′+]=T′rightarrowepsilonM[T','+]=T' \\rightarrow \\epsilon
    • M[T′,′)]=T′rightarrowepsilonM[T',')]=T' \\rightarrow \\epsilon
    • M[T′,′M[T','\\]=T' \rightarrow \epsilon$
  1. F→(E)F \rightarrow (E)
  • M[F,′(′]=Frightarrow(E)M[F,'(']=F \\rightarrow (E)
  1. F→idF \rightarrow id
  • M[F,id]=FrightarrowidM[F,id]=F \\rightarrow id

Footnotes

  1. (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 epsilon\\epsilon 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 AA \ Lookahead aa′(′'('idid′+′'+'′∗′'*'′)′')'\\
EEErightarrowTE′E \\rightarrow T E'ErightarrowTE′E \\rightarrow T E'————
E′E'——E′rightarrow+TE′E' \\rightarrow + T E'—E′rightarrowepsilonE' \\rightarrow \\epsilonE′rightarrowepsilonE' \\rightarrow \\epsilon
TTTrightarrowFT′T \\rightarrow F T'TrightarrowFT′T \\rightarrow F T'————
T′T'——T′rightarrowepsilonT' \\rightarrow \\epsilonT′rightarrow∗FT′T' \\rightarrow * F T'T′rightarrowepsilonT' \\rightarrow \\epsilonT′rightarrowepsilonT' \\rightarrow \\epsilon
FFFrightarrow(E)F \\rightarrow (E)FrightarrowidF \\rightarrow id————

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 (E′,T′E',T'). If you try to build a predictive table directly from the left-recursive form (ErightarrowE+TE \\rightarrow E+T), you will not get a valid LL(1) parsing table."

Footnotes

  1. (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 epsilon\\epsilon handling. ↩

  2. (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

1

Introduce E′E' and T′T' to eliminate left recursion."

Compute FIRST

2

Compute terminals that can start derivations from each nonterminal."

Compute FOLLOW

3

Propagate lookaheads to nonterminals; add \ to FOLLOW(E)(E)."

Fill table

4

Use FIRST/ϵ\epsilon and FOLLOW membership rules to place productions."

Common Edge Cases in Table Construction

Knowledge Check

Question 1 of 4
Q1Single choice

For a production A→αA \rightarrow \alpha with ϵ∈FIRST(α)\epsilon \in FIRST(\alpha), which FOLLOW set determines where to place this production in the predictive parsing table?