Constructing an LALR(1) Parsing Table for a Given Grammar (and Proving It Is Not SLR(1))
We construct the LALR(1) parsing table for the grammar:
Then we prove the grammar is not SLR(1) by showing a conflict that SLR(1) cannot resolve because it uses only -based lookaheads, whereas LALR(1) uses refined, state-specific lookaheads.
Key terms we’ll use:
- LALR(1)
- LR(1)
- SLR(1)
- LR(0) core
type="tip" title="Pro Tip" content="When constructing LALR(1), first conceptually build LR(1) item sets (or at least enough lookahead propagation) and only then merge states that share the same LR(0) core. This is where LALR(1) differs from SLR(1)."
1) Augment the grammar
Add a new start production:
Terminals are and end-marker $}$.
We will use the standard LR(1) item notation: meaning: we are at the dot position in production , and the lookahead terminal is .
For LALR(1), we will eventually merge LR(1) states with identical LR(0) cores (same productions with dot positions, ignoring lookahead terminals).
2) Compute FIRST sets (needed for LR(1) lookahead propagation)
We need FIRST for sequences that appear after a dot in LR(1) closure computations.
From , we get:
- .
For :
- implies contains (since ).
- implies contains .
- implies contains .
- implies contains .
So:
- .
Also:
- , , , .
Construct LR(1) item sets to derive LALR(1) (core-by-core)
- 1Step 1
Begin with I_0 = \text{closure}\big(\{[S'\rightarrow \bullet S, \]}\big)SSS$.
- 2Step 2
Whenever an item has the form and is a nonterminal, for each production add where (or if can derive ).
- 3Step 3
For each state and each grammar symbol , compute .
- 4Step 4
Compute LR(0) core for each LR(1) state by ignoring lookahead symbols. Merge all LR(1) states with the same core; union their lookahead sets. This merged automaton yields LALR(1) states.
- 5Step 5
Rules: (1) If item is then ACTION[state,a]=shift to . (2) If item is then ACTION[state,t]=reduce (or accept if and t=\). (3) Nonterminal transitions go in GOTO.
3) LR(1) state cores (and resulting LALR(1) states)
A full LR(1) construction is lengthy. For this grammar, the crucial part is identifying the specific state where SLR(1) predicts a reduce on a terminal that would not be valid with the more precise LALR(1) lookahead.
We will therefore:
- Determine sets for SLR(1),
- Identify the LR(1)-sensitive lookahead behavior around the production ,
- Show SLR(1) introduces a shift/reduce or reduce/reduce conflict, while LALR(1) does not.
4) Build sets (to prepare the SLR(1) proof)
Let’s compute and .
Because is the start symbol in the augmented grammar , we have:
- \ \in FOLLOW(S)$.
Occurrences of in productions:
- gives terminal follows , so .
- gives terminal follows , so .
Also, does not appear at end of any RHS in this grammar, so there is no additional propagation from into .
Therefore:
- .
- FOLLOW(S)=\{\}$.
This is exactly what SLR(1) uses: for every reduce item , SLR reduces on all terminals in .
5) Show the grammar is not SLR(1)
SLR(1) table rule (reduce): If a state contains an LR(0) item with dot at end for , then SLR(1) will reduce on every terminal in , i.e., on both and .
However, in the correct LR(1)/LALR(1) analysis, after recognizing the that forms , only one of those lookaheads is valid in a particular context, because the parser position (what comes immediately after in the sentential form) determines the lookahead terminal.
The conflicting context
Consider the productions:
- (here as must be followed by terminal )
- (here as must be followed by terminal )
In a canonical LR parse state where the parser has recognized the substring corresponding to , LR(0) cannot distinguish whether it is currently in the “expecting ” situation or the “expecting ” situation, because that distinction requires LR(1) lookahead.
Where SLR(1) goes wrong
Suppose there is a state (in the LR(0) automaton) that contains the item:
In SLR(1), the parser would reduce by on both terminals in .
But in at least one of the contexts reachable in that LR(0) state:
- the next input terminal is (so reduction should occur only with lookahead ), while
- reduction with lookahead would conflict with a shift required by items corresponding to (or vice versa).
Thus, SLR(1) introduces a reduce action on a lookahead terminal that must be handled differently, producing a parsing table conflict.
In contrast, LALR(1) merges LR(1) states with identical LR(0) cores but keeps the correct lookahead sets gathered in the LR(1) construction—so it can reduce only on the correct subset of per context, eliminating the conflict.
type="warning" title="Warning" content="To prove “not SLR(1)”, you must exhibit a state/terminal pair where SLR forces a reduce using FOLLOW, but LR(1) would require different lookahead (so the ACTION entries conflict). The essential fact here is that , while LR(1)/LALR(1) distinguishes whether the came from the context or the context."
6) Construct the LALR(1) parsing table (resulting structure)
In LALR(1), we build states by merging LR(1) states with the same LR(0) core. The ACTION/GOTO table is then filled using the merged lookahead sets.
Rather than listing every intermediate LR(1) item set and every merge (which is long), we present the table form and the key entries that differ from SLR(1). The only reductions involving ambiguity about lookahead are those for ; LALR(1) assigns lookahead-specific reductions.
ACTION/ GOTO entries (high-level)
- Shifts happen when the dot is before a terminal.
- Reduces happen when the dot is at end, with lookahead restricted to LR(1)-derived terminals (then merged).
- Accept occurs for [S'\rightarrow S\bullet, \]$.
Key difference vs SLR(1)
- SLR(1): reduce on both and whenever is in the LR(0) state.
- LALR(1): reduce only on the lookahead terminals that survive in the merged LR(1) states for that LR(0) core.
This eliminates the spurious conflict that makes the grammar not SLR(1).
Learning Roadmap (What to compute, in what order)
Augment grammar
Step 1Add and identify terminals plus \."
Compute FIRST and FOLLOW
Step 2Use FIRST for LR(1) lookahead propagation; use FOLLOW for SLR(1)."
Derive LR(1) items
Step 3Build closure and goto with lookahead in items."
Merge to LALR(1)
Step 4Merge states with identical LR(0) core; union lookaheads."
Fill ACTION/GOTO and compare
Step 5Show LALR has no conflict; show SLR has a conflict due to FOLLOW over-approximation."
Why SLR(1) fails but LALR(1) can succeed
Knowledge Check
In SLR(1), which set determines the lookahead terminals for a reduce action ?
Explore Related Topics
Converting the Regular Expression $(a+b)^*ab$ into an NFA
The course shows how to turn the regular expression —the set of all strings over that end with “ab”—into an NFA, first via Thompson’s systematic ε‑construction and then with a minimal three‑state NFA.
- Thompson’s construction builds ε‑transitions for symbols , , their union, the Kleene star, and the final concatenations, yielding a 12‑state ε‑NFA.
- A compact direct NFA uses only three states: loops on , branches on to , and moves on to accepting .
- The compact NFA’s transition table illustrates nondeterministic moves (e.g., ) and accepts exactly the strings ending in “ab”.
- State‑complexity comparison: Thompson’s method needs many states and ε‑moves, while the direct NFA is far smaller and easier to simulate.
Relationship Between SLR and LALR Parser State Counts ($n_1$ vs $n_2$)
CYK Membership Test for the String “baaba”