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: ETEE\to TE', E+TEϵE'\to +TE'\mid\epsilon, TFTT\to FT', TFTϵ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 FidF\to id starts with idid, we get FIRST(F)={(,id}FIRST(F)=\{(,\,id\}.

  3. 3
    Step 3

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

  4. 4
    Step 4

    TFTT\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+TEE'\to +TE' starts with ++, and EϵE'\to \epsilon contributes ϵ\epsilon, so FIRST(E)={+,ϵ}FIRST(E')=\{+,\,\epsilon\}.

  6. 6
    Step 6

    ETEE\to TE'. Since FIRST(T)={(,id}FIRST(T)=\{(,id\} and TT cannot derive ϵ\epsilon (because TFTT\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 TETE', TT is followed by EE' 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 EE'. 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 EE', so its effect is already captured via positions where EE' appears (not by the empty rule itself).

  6. 6
    Step 6

    In FTFT', FF is followed by TT'. 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 TT'. 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

    EE' appears at the end of ETEE\to TE', so FOLLOW(E)FOLLOW(E)FOLLOW(E')\supseteq FOLLOW(E). Also EE' appears at the end of E+TEE'\to +TE', so FOLLOW(E)FOLLOW(E)FOLLOW(E')\supseteq FOLLOW(E') (self-consistent).

  9. 9
    Step 9

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

Solving the FOLLOW equations

Let’s denote:

  • FOLLOW(E)=\\{\\\}\,(wellconfirmnootherterminalisforcedtofollow(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 EtoTEE\\to TE':

  • TT is followed by EE', 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 TtoFTT\\to FT':

  • FF is followed by TT', 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'):

  • EE' appears at end of EtoTEE\\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'):

  • TT' appears at end of TtoFTT\\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 ERightarrowepsilonE'\\Rightarrow \\epsilon, symbols that can follow EE also become possible followers of TT in EtoTEE\\to TE'. Similarly, because TRightarrowepsilonT'\\Rightarrow \\epsilon, followers of TT can follow FF in TtoFTT\\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 aFIRST(α){ϵ}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 bFOLLOW(A)b\in FOLLOW(A) set M[A,b]=AαM[A,b]=A\to\alpha.

  2. 2
    Step 2

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

  3. 3
    Step 3

    Productions: E+TEE'\to +TE' and EϵE'\to \epsilon. FIRST(+TE)={+}FIRST(+TE')=\{+\} so M[E,+]=E+TEM[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: TFTT\to FT'. FIRST(FT)={(,id}FIRST(FT')=\{(,id\}. So M[T,(]=TFTM[T,(]=T\to FT' and M[T,id]=TFTM[T,id]=T\to FT'.

  5. 5
    Step 5

    Productions: TFTT'\to *FT' and TϵT'\to \epsilon. FIRST(FT)={}FIRST(*FT')=\{*\} so M[T,]=TFTM[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 FidF\to id. So M[F,(]=F(E)M[F,(]=F\to (E) and M[F,id]=FidM[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,(]=EtoTEM[E,(] = E\\to TE'
  • M[E,id]=EtoTEM[E,id] = E\\to TE'

Nonterminal EE':

  • M[E,+]=Eto+TEM[E',+] = E'\\to +TE'
  • M[E,)]=EtoepsilonM[E',)] = E'\\to \\epsilon
  • M[E,M[E',\]=Etoepsilon] = E'\\to \\epsilon

Nonterminal TT:

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

Nonterminal TT':

  • M[T,]=TtoFTM[T',*] = T'\\to *FT'
  • M[T,+]=TtoepsilonM[T',+] = T'\\to \\epsilon
  • M[T,)]=TtoepsilonM[T',)] = T'\\to \\epsilon
  • M[T,M[T',\]=Ttoepsilon] = 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')?