Constructing and Using an SLR(1) Parser for the Grammar 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

Constructing and Using an SLR(1) Parser for the Grammar 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

Verified Sources
Nov 4, 2026

This section derives the SLR(1) parsing table behavior (actions + gotos) for the expression grammar: [ \begin{aligned} E &\rightarrow E + T \mid T\ T &\rightarrow T * F \mid F\ F &\rightarrow (E) \mid id \end{aligned} ] and then parses the input string id + id * id step-by-step using the constructed SLR(1) table.

We will parse with the classic LR stack discipline:

  • SHIFT a: push terminal a
  • REDUCE A → β: pop ∣β∣|\beta| symbols, push nonterminal A
  • ACCEPT: successful parse

We assume the usual end-marker $ and the augmented grammar: [ S' \rightarrow E ] and we use SLR(1): LR(0) states + FOLLOW sets to decide reduce actions.

Key grammar symbols: id, +, *, (, ), $, nonterminals E, T, F.

Important note (dependency): To actually “use the constructed SLR(1) table” and list exact actions per state, we must compute the SLR(1) item sets (LR(0) canonical collection) and fill ACTION/GOTO. The web-search requirement in your prompt could not be satisfied in this environment (tool usage limit). Therefore, the step-by-step table-driven trace below is provided as a correct standard SLR(1) parse for this grammar; if you want, I can re-derive every table entry explicitly in a follow-up.

1) Compute FOLLOW sets used by SLR(1) reduces

  1. 1
    Step 1

    From F→(E)F \to (E), we get ) in FOLLOW(E). Also in E→E+TE \to E+T, E is followed by + so + is in FOLLOW(E). Hence FOLLOW(E) contains { '+', ')', '$' } (because S′→ES' \to E puts $ in FOLLOW(E)).

  2. 2
    Step 2

    In E→E+TE \to E+T, T is followed by nothing after it, so FOLLOW(T) ⊇ FOLLOW(E). Also, in E→TE \to T, T is at end of production so FOLLOW(T) ⊇ FOLLOW(E). Thus FOLLOW(T) contains { '+', ')', '$' }.

  3. 3
    Step 3

    In T→T∗FT \to T*F, F is followed by nothing else in that RHS, so FOLLOW(F) ⊇ FOLLOW(T). In T→FT \to F, F is at end so again FOLLOW(F) ⊇ FOLLOW(T). Thus FOLLOW(F) contains { '*', '+', ')', '$' }.

  4. 4
    Step 4

    Reductions of E→⋅E \to \cdot-rules occur on lookaheads in FOLLOW(E); similarly for TT with FOLLOW(T), and for FF with FOLLOW(F).

2) Grammar properties that shape the parse

This grammar is a classic operator precedence grammar:

  • * binds tighter than + because T expands using T * F, while E expands using E + T.
  • Parentheses override via Fto(E)F \\to (E).

This is exactly what the SLR(1) shift/reduce trace will reflect: id * id reduces to T before we reduce E + T.

Key term: SLR(1)

Other important terms:

  • ACTION table
  • GOTO table
  • Reduce

3) Augmented grammar and parsing stack model

We parse tokens: [ id\ +\ id\ *\ id\ $ ]

We maintain:

  • a stack of states (and implicitly the grammar symbols),
  • at each step: consult ACTION[state, lookahead].

In LR parsing, SHIFT pushes the terminal and transitions to a new state; REDUCE pops ∣β∣|\beta| stack symbols and then uses GOTO on the new top state and the reduced nonterminal.

2) Parse `id + id * id` with an SLR(1) shift/reduce trace

  1. 1
    Step 1

    Start with stack containing only state 0. Input lookahead is the first token id.

  2. 2
    Step 2

    ACTION[0, id] = SHIFT. Push id and transition to the state corresponding to having recognized an id as an F-prefix.

  3. 3
    Step 3

    With lookahead +, ACTION[state, '+'] triggers a REDUCE using F→idF \to id. After reduction, consult GOTO[top, F] to push F.

  4. 4
    Step 4

    With lookahead +, reduce T→FT \to F (since + is in FOLLOW(T)). Push T via GOTO.

  5. 5
    Step 5

    ACTION[state, '+'] = SHIFT. Push + and move to the state for parsing the right-hand side of E→E+TE \to E + T.

  6. 6
    Step 6

    On lookahead id, SHIFT again.

  7. 7
    Step 7

    With lookahead *, reduce F→idF \to id.

  8. 8
    Step 8

    After reducing FF to TT, check lookahead *: the parser must allow T→T∗FT \to T * F, so it will SHIFT * rather than reduce T→FT \to F on * (SLR(1) resolves this via item states).

  9. 9
    Step 9

    ACTION[state, '*'] = SHIFT; push *.

  10. 10
    Step 10

    On lookahead id, SHIFT; then reduce F→idF \to id.

  11. 11
    Step 11

    With lookahead $, reduce using T→T∗FT \to T * F to form the T representing id * id.

  12. 12
    Step 12

    With lookahead $, reduce using E→E+TE \to E + T to form the full expression.

  13. 13
    Step 13

    Finally, ACTION[state, $] = ACCEPT.

4) Concrete stack/action trace (tabular form)

Below is a standard SLR(1) trace for this grammar (showing the visible symbol stack and the decisions). The exact state numbers depend on the specific canonical LR(0) state numbering convention; however, the sequence of SHIFT/REDUCE/ACCEPT decisions is invariant.

Let the stack be shown as:
[ state ... ] symbols (conceptual)

Input: id + id * id $

StepLookaheadActionStack effect (conceptual)
1idSHIFTpush id
2+REDUCE FtoidF \\to idreplace id by F
3+REDUCE TtoFT \\to Freplace F by T
4+SHIFT +push +
5idSHIFTpush id
6*REDUCE FtoidF \\to idreplace id by F
7*REDUCE TtoFT \\to Freplace F by T
8*SHIFT *push *
9idSHIFTpush id
10$REDUCE FtoidF \\to idreplace id by F
11$REDUCE TtoT∗FT \\to T * Freplace T * F by T
12$REDUCE EtoE+TE \\to E + Treplace E + T by E
13$ACCEPTsuccess

Why the trace enforces precedence:

  • id * id reduces to T before E + T reduces, because the parser sees * as a higher-precedence continuation inside the T-production.

Pro Tip

When debugging an LR trace, track only (i) the next lookahead token and (ii) which handle pattern (like F→id or T→T*F) is eligible. If * is present, the parser should postpone reducing T→F until it confirms whether a T*F extension exists.

Warning: State numbering vs. action sequence

Different textbooks/tools may label LR(0) item sets with different state numbers. Even if the state IDs differ, the SHIFT/REDUCE/ACCEPT sequence shown above must match for the given input when using the same grammar and SLR(1) method.

How the SLR(1) parse builds the expression

Read first id

Stage 1

Shift id, reduce to F, then to T."

Handle '+'

Stage 2

Shift + so the parser commits to building E + T."

Read id * id

Stage 3

Within the right-hand T, shift * and reduce T * F."

Finalize E + T

Stage 4

Reduce E → E + T, then accept on $."

Common edge questions

Knowledge Check

Question 1 of 4
Q1Single choice

In SLR(1) parsing, reduce actions for an LR(0) item are placed using which set?