Sub topic 15
LR(0) item sets and SLR(1) tables
This chapter assumes you know FIRST and FOLLOW sets, derivations, and how a shift-reduce parser uses a stack. It shows how to build the table that tells such a parser when to shift and when to reduce.
LR(0) items
An LR(0) item is a production with a dot in its body. The dot marks how much of the body the parser has already seen. For E -> E + T the items are:
E -> . E + TE -> E . + TE -> E + . TE -> E + T .
A production with a body of length n gives n + 1 items. A -> ε gives one item, A -> ..
Before building states, augment the grammar with a new start production S' -> S. Reducing by S' -> S is the accept action, which tells the parser when to stop.
Closure and goto
Two operations build every state.
- closure(I) starts with the items in I. Whenever an item has the form
A -> α . B βand B is a nonterminal, addB -> . γfor every production of B. Repeat until nothing new appears. - goto(I, X) takes every item in I of the form
A -> α . X β, moves the dot to giveA -> α X . β, and returns the closure of that set.
A state is a set of items. The canonical collection is the set of all states reachable from closure({S' -> . S}) by repeated goto.
A worked example
Use this grammar, with productions numbered for reduce actions:
E -> E + TE -> TT -> id
The augmented production is S' -> E, numbered 0.
| State | Items | Transitions |
|---|---|---|
| I0 | `S' -> .E`, `E -> .E+T`, `E -> .T`, `T -> .id` | E to I1, T to I2, id to I3 |
| I1 | `S' -> E.`, `E -> E.+T` | + to I4 |
| I2 | `E -> T.` | none |
| I3 | `T -> id.` | none |
| I4 | `E -> E+.T`, `T -> .id` | T to I5, id to I3 |
| I5 | `E -> E+T.` | none |
I0 contains the two E items and the T item only because of closure. The dot sits before E, so E's productions are added. One of those has the dot before T, so T's production is added too.
Filling the SLR(1) table
The table has an action part, indexed by state and terminal, and a goto part, indexed by state and nonterminal. For each state I:
- If
A -> α . a βis in I and goto(I, a) = J, set ACTION[I, a] = shift J. - If
A -> α .is in I and A is not S', set ACTION[I, a] = reduceA -> αfor every a in FOLLOW(A). - If
S' -> S .is in I, set ACTION[I, $] = accept. - If goto(I, A) = J for a nonterminal A, set GOTO[I, A] = J.
Every other cell is an error. Here FOLLOW(E) = FOLLOW(T) = { +, $ }.
| State | id | + | $ | E | T |
|---|---|---|---|---|---|
| 0 | s3 | 1 | 2 | ||
| 1 | s4 | accept | |||
| 2 | r2 | r2 | |||
| 3 | r3 | r3 | |||
| 4 | s3 | 5 | |||
| 5 | r1 | r1 |
To parse id + id $, keep a stack of states:
- Stack 0. Shift id: 0 3.
- Reduce by
T -> id. Pop one state, then GOTO[0, T] = 2: 0 2. - Reduce by
E -> T: 0 1. - Shift +: 0 1 4. Shift id: 0 1 4 3.
- Reduce by
T -> id. GOTO[4, T] = 5: 0 1 4 5. - Reduce by
E -> E + T. Pop three states, then GOTO[0, E] = 1: 0 1. The next symbol is$, so accept.
A reduction by a production with body length k pops k states.
LR(0) versus SLR(1)
An LR(0) parser applies rule 2 to every terminal, with no lookahead. A state that contains a complete item and any other item is then a conflict:
- A shift-reduce conflict arises when a complete item sits beside an item with the dot before a terminal.
- A reduce-reduce conflict arises when two complete items share a state.
SLR(1) removes many of these conflicts by reducing only on FOLLOW(A). In the example above, state 2 has just one complete item, so even LR(0) would work, but state 1 would not be a problem either way.
When SLR(1) still fails
Consider:
S -> L = R | RL -> * R | idR -> L
State goto(I0, L) contains S -> L . = R and R -> L .. Shifting on = comes from the first item. FOLLOW(R) = { =, $ }, because R can end the right side of L = R and FOLLOW(S) = { $ } flows to R. So rule 2 also puts reduce R -> L in the = column, which causes a shift-reduce conflict.
The grammar is unambiguous. The failure comes from FOLLOW(R) being a coarse, state-independent set. In this particular state, = can never follow R after a reduction, because that would give R = ... as a left side. LR(1) items carry a per-item lookahead and avoid the problem. That is the next chapter.
Exercises
Exercise
How many LR(0) items does the production A -> B C D have, and how many does A -> ε have?
Show answer
A -> B C D has a body of length 3, so it has 4 items. A -> ε has 1 item, A -> . .
Exercise
In the grammar S -> L = R | R, L -> * R | id, R -> L, why does the SLR(1) table have a conflict in the state containing S -> L . = R and R -> L . ?
Show answer
Shift on = comes from S -> L . = R. The reduce R -> L is placed on every terminal in FOLLOW(R) = { =, $ }, which includes =. The same cell gets both shift and reduce, so there is a shift-reduce conflict.
Exercise
A parser reduces by E -> E + T and the stack of states is 0 1 4 5. What is the stack after popping, and which table is consulted next?
Show answer
The body has 3 symbols, so 3 states are popped, leaving 0. The parser then looks up GOTO[0, E], which is 1, and pushes it.