FIRST, FOLLOW, and LL(1) Parsing Table for the Given Expression Grammar

FIRST, FOLLOW, and LL(1) Parsing Table for the Given Expression Grammar

Verified Sources
Sep 13, 2026

We are given the grammar (with terminals +,∗,(,),id\\{+,*,(,),id\\} and start symbol EE):

\\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:

  1. [keyword]{def="FIRST(A) is the set of terminals that can begin strings derived from A, possibly including epsilon\\epsilon"}
  2. keyword
  3. 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 epsiloninFIRST(α)\\epsilon \\in FIRST(α), then epsilon\\epsilon can be derived from α"}
  • keyword
  • [keyword]{def="LL(1) table entry uses FIRST(α); if epsiloninFIRST(α)\\epsilon \\in FIRST(α) then use FOLLOW(A)"}

Step 1 — Compute FIRST sets

  1. 1
    Step 1

    Use each nonterminal’s productions: E→TE′E\to TE', E′→+TE′∣ϵE'\to +TE'\mid\epsilon, T→FT′T\to FT', T′→∗FT′∣ϵT'\to *FT'\mid\epsilon, F→(E)∣idF\to (E)\mid id.

  2. 2
    Step 2

    Since F→(E)F\to (E) starts with (( and F→idF\to id starts with idid, we get FIRST(F)={(, id}FIRST(F)=\{(,\,id\}.

  3. 3
    Step 3

    T′→∗FT′T'\to *FT' starts with ∗*, and T′→ϵT'\to \epsilon contributes ϵ\epsilon, so FIRST(T′)={∗, ϵ}FIRST(T')=\{*,\,\epsilon\}.

  4. 4
    Step 4

    T→FT′T\to FT'. Because FIRST(F)={(,id}FIRST(F)=\{(,id\} and neither production of FF derives ϵ\epsilon, we have FIRST(T)=FIRST(F)={(, id}FIRST(T)=FIRST(F)=\{(,\,id\}.

  5. 5
    Step 5

    E′→+TE′E'\to +TE' starts with ++, and E′→ϵE'\to \epsilon contributes ϵ\epsilon, so FIRST(E′)={+, ϵ}FIRST(E')=\{+,\,\epsilon\}.

  6. 6
    Step 6

    E→TE′E\to TE'. Since FIRST(T)={(,id}FIRST(T)=\{(,id\} and TT cannot derive ϵ\epsilon (because T→FT′T\to FT' and FF cannot derive ϵ\epsilon), we get FIRST(E)=FIRST(T)={(, id}FIRST(E)=FIRST(T)=\{(,\,id\}.

FIRST summary

  • FIRST(E)=(,,idFIRST(E)=\\{(,\\,id\\}
  • FIRST(E′)=+,,epsilonFIRST(E')=\\{+,\\,\\epsilon\\}
  • FIRST(T)=(,,idFIRST(T)=\\{(,\\,id\\}
  • FIRST(T′)=∗,,epsilonFIRST(T')=\\{*,\\,\\epsilon\\}
  • FIRST(F)=(,,idFIRST(F)=\\{(,\\,id\\}

Step 2 — Compute FOLLOW sets

  1. 1
    Step 1

    Take \ as the end-marker (common LL(1) convention): since EE is the start symbol, \ is in FOLLOW(E)FOLLOW(E).

  2. 2
    Step 2

    Scan each production for every appearance of a nonterminal and apply FOLLOW rules based on what can come after it.

  3. 3
    Step 3

    In TE′TE', TT is followed by E′E' so FIRST(E′)∖{ϵ}={+}FIRST(E')\setminus\{\epsilon\}=\{+\} is added to FOLLOW(T)FOLLOW(T). Because ϵ∈FIRST(E′)\epsilon\in FIRST(E'), add FOLLOW(E)FOLLOW(E) to FOLLOW(T)FOLLOW(T).

  4. 4
    Step 4

    In +TE′+TE', TT is followed by E′E'. Again add FIRST(E′)∖{ϵ}={+}FIRST(E')\setminus\{\epsilon\}=\{+\} to FOLLOW(T)FOLLOW(T). Since ϵ∈FIRST(E′)\epsilon\in FIRST(E'), add FOLLOW(E′)FOLLOW(E') to FOLLOW(T)FOLLOW(T) (we will compute FOLLOW(E′)FOLLOW(E') from other placements).

  5. 5
    Step 5

    This production has no symbols after E′E', so its effect is already captured via positions where E′E' appears (not by the empty rule itself).

  6. 6
    Step 6

    In FT′FT', FF is followed by T′T'. Add FIRST(T′)∖{ϵ}={∗}FIRST(T')\setminus\{\epsilon\}=\{*\} to FOLLOW(F)FOLLOW(F). Since ϵ∈FIRST(T′)\epsilon\in FIRST(T'), add FOLLOW(T)FOLLOW(T) to FOLLOW(F)FOLLOW(F).

  7. 7
    Step 7

    In ∗FT′*FT', FF is followed by T′T'. Add FIRST(T′)∖{ϵ}={∗}FIRST(T')\setminus\{\epsilon\}=\{*\} to FOLLOW(F)FOLLOW(F). Since ϵ∈FIRST(T′)\epsilon\in FIRST(T'), add FOLLOW(T′)FOLLOW(T') to FOLLOW(F)FOLLOW(F).

  8. 8
    Step 8

    E′E' appears at the end of E→TE′E\to TE', so FOLLOW(E′)⊇FOLLOW(E)FOLLOW(E')\supseteq FOLLOW(E). Also E′E' appears at the end of E′→+TE′E'\to +TE', so FOLLOW(E′)⊇FOLLOW(E′)FOLLOW(E')\supseteq FOLLOW(E') (self-consistent).

  9. 9
    Step 9

    T′T' appears at the end of T→FT′T\to FT', so FOLLOW(T′)⊇FOLLOW(T)FOLLOW(T')\supseteq FOLLOW(T), and also at the end of T′→∗FT′T'\to *FT', giving self-consistency.

Solving the FOLLOW equations

Let’s denote:

  • FOLLOW(E)=\\{\\\}\,(we′llconfirmnootherterminalisforcedtofollow(we'll confirm no other terminal is forced to followEoutsideoftheoutside of the\case;case;Ealsoappearsinsidealso appears insideF\to (E)$, which changes this.)
  • From Fto(E)F\\to (E): the symbol after EE is )), so add )\\{)\\} to FOLLOW(E)FOLLOW(E).

So:

FOLLOW(E)=\\{\\$,\\,)\\}.

Now use EtoTE′E\\to TE':

  • TT is followed by E′E', so add FIRST(E′)setminusepsilon=+FIRST(E')\\setminus\\{\\epsilon\\}=\\{+\\} to FOLLOW(T)FOLLOW(T).
  • Because epsiloninFIRST(E′)\\epsilon\\in FIRST(E'), add FOLLOW(E)FOLLOW(E) to FOLLOW(T)FOLLOW(T).

Thus:

FOLLOW(T)=\\{+,\\$,\\,)\\}.

Now TtoFT′T\\to FT':

  • FF is followed by T′T', so add FIRST(T′)setminusepsilon=∗FIRST(T')\\setminus\\{\\epsilon\\}=\\{*\\} to FOLLOW(F)FOLLOW(F).
  • Because epsiloninFIRST(T′)\\epsilon\\in FIRST(T'), add FOLLOW(T)FOLLOW(T) to FOLLOW(F)FOLLOW(F).

So:

FOLLOW(F)=\\{*,\\, +,\\$,\\,)\\}.

Now derive FOLLOW(E′)FOLLOW(E'):

  • E′E' appears at end of EtoTE′E\\to TE', hence FOLLOW(E')\\supseteq FOLLOW(E)=\\{\\,\,)\}$.
  • No other terminal is added to FOLLOW(E′)FOLLOW(E') from other contexts (it is not followed by a terminal in any production where it occurs before the end).

So:

FOLLOW(E')=\\{\\$,\\,)\\}.

Finally FOLLOW(T′)FOLLOW(T'):

  • T′T' appears at end of TtoFT′T\\to FT', so FOLLOW(T')\\supseteq FOLLOW(T)=\\{+,\\,\,)\}$. Thus:
FOLLOW(T')=\\{+,\\$,\\,)\\}.

FOLLOW summary

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

Quick visual: how epsilon\\epsilon affects FOLLOW

Because E′RightarrowepsilonE'\\Rightarrow \\epsilon, symbols that can follow EE also become possible followers of TT in EtoTE′E\\to TE'. Similarly, because T′RightarrowepsilonT'\\Rightarrow \\epsilon, followers of TT can follow FF in TtoFT′T\\to FT'.

Pro Tip

When a nonterminal is followed by a suffix that can derive ϵ\epsilon, you must union FOLLOWFOLLOW of the left-hand side into the FOLLOWFOLLOW of the nonterminal (not just FIRSTFIRST).

Common Pitfall

Do not add ϵ\epsilon to FOLLOW sets. FOLLOW contains terminals only (and \). ϵ\epsilon is used only to decide whether to propagate FOLLOW across nullable suffixes.

Step 3 — Derive the LL(1) parsing table

  1. 1
    Step 1

    For each production A→αA\to \alpha: for each a∈FIRST(α)∖{ϵ}a\in FIRST(\alpha)\setminus\{\epsilon\}, set M[A,a]=A→αM[A,a]=A\to\alpha. If ϵ∈FIRST(α)\epsilon\in FIRST(\alpha), then for each b∈FOLLOW(A)b\in FOLLOW(A) set M[A,b]=A→αM[A,b]=A\to\alpha.

  2. 2
    Step 2

    Production: E→TE′E\to TE'. FIRST(TE′)=FIRST(E)={(,id}FIRST(TE')=FIRST(E)=\{(,id\}. So M[E,(]=E→TE′M[E,(]=E\to TE' and M[E,id]=E→TE′M[E,id]=E\to TE'.

  3. 3
    Step 3

    Productions: E′→+TE′E'\to +TE' and E′→ϵE'\to \epsilon. FIRST(+TE′)={+}FIRST(+TE')=\{+\} so M[E′,+]=E′→+TE′M[E',+]=E'\to +TE'. For E′→ϵE'\to \epsilon: since ϵ∈FIRST(E′)\epsilon\in FIRST(E'), add it over FOLLOW(E')=\{\,,)},so, so M[E',$$]=E'\to\epsilonandandM[E',)]=E'\to\epsilon$.

  4. 4
    Step 4

    Production: T→FT′T\to FT'. FIRST(FT′)={(,id}FIRST(FT')=\{(,id\}. So M[T,(]=T→FT′M[T,(]=T\to FT' and M[T,id]=T→FT′M[T,id]=T\to FT'.

  5. 5
    Step 5

    Productions: T′→∗FT′T'\to *FT' and T′→ϵT'\to \epsilon. FIRST(∗FT′)={∗}FIRST(*FT')=\{*\} so M[T′,∗]=T′→∗FT′M[T',*]=T'\to *FT'. For T′→ϵT'\to \epsilon, use FOLLOW(T')=\{+,\,,)}:so: so M[T',+]=T'\to\epsilon,, M[T',$$]=T'\to\epsilon,, M[T',)]=T'\to\epsilon$.

  6. 6
    Step 6

    Productions: F→(E)F\to (E) and F→idF\to id. So M[F,(]=F→(E)M[F,(]=F\to (E) and M[F,id]=F→idM[F,id]=F\to id.

LL(1) Parsing Table (M)

Rows are nonterminals, columns are lookahead terminals.

Parsing table as production listings

Let terminals be columns: (,id,+,∗,),(, id, +, *, ), \\.

Nonterminal EE:

  • M[E,(]=EtoTE′M[E,(] = E\\to TE'
  • M[E,id]=EtoTE′M[E,id] = E\\to TE'

Nonterminal E′E':

  • M[E′,+]=E′to+TE′M[E',+] = E'\\to +TE'
  • M[E′,)]=E′toepsilonM[E',)] = E'\\to \\epsilon
  • M[E′,M[E',\]=E′toepsilon] = E'\\to \\epsilon

Nonterminal TT:

  • M[T,(]=TtoFT′M[T,(] = T\\to FT'
  • M[T,id]=TtoFT′M[T,id] = T\\to FT'

Nonterminal T′T':

  • M[T′,∗]=T′to∗FT′M[T',*] = T'\\to *FT'
  • M[T′,+]=T′toepsilonM[T',+] = T'\\to \\epsilon
  • M[T′,)]=T′toepsilonM[T',)] = T'\\to \\epsilon
  • M[T′,M[T',\]=T′toepsilon] = T'\\to \\epsilon

Nonterminal FF:

  • M[F,(]=Fto(E)M[F,(] = F\\to (E)
  • M[F,id]=FtoidM[F,id] = F\\to id

All other cells are error/undefined.

Workflow to Build LL(1) Table

FIRST sets

1

Determine which terminals can begin each nonterminal’s derivations."

FOLLOW sets

2

Propagate terminal followers using ϵ\epsilon-nullable suffix logic."

LL(1) table

3

Use FIRSTFIRST for normal entries; use FOLLOWFOLLOW when ϵ\epsilon is possible."

Why this grammar is LL(1) (intuition)

FIRST/FOLLOW/LL(1) Self-Check Deck

1 / 5
Question · Term

What is FIRST(E')?

Click to reveal
Answer · Definition

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

Knowledge Check

Question 1 of 4
Q1Single choice

For the grammar, what is FIRST(T′)FIRST(T')?