SLR(1) Parsing Table for the Expression Grammar
We build an SLR(1) parsing table for the grammar:
using the standard SLR(1) method:
- construct LR(0) item sets via closure and goto;
- compute FOLLOW sets using FOLLOW;
- add reduce actions only on terminals in FOLLOW of the reduced nonterminal, using SLR(1) rule.
Key symbols/terminals: \{id,+,*,(,),\}$ $ is end-marker.
Note (Augmented grammar). For LR parsing we first add a new start production: We then compute item sets for this augmented grammar.
SLR Parsing (LR(0) + FOLLOW) - Intuitive walkthrough
Important keyword definitions (used throughout)
- LR(0) item
- closure
- goto
- FOLLOW
- SLR(1) parsing table
Compute FOLLOW sets
- 1Step 1
Terminals: \{id,+,*,(,),\}ES'\rightarrow E$).
- 2Step 2
From : add to FOLLOW(E). From : add to FOLLOW(E). Also, since , add \ to FOLLOW(E).
- 3Step 3
From : add FOLLOW(E) to FOLLOW(T). From : no direct FOLLOW(T) contribution there; instead, in add no new terminals.
- 4Step 4
From : add to FOLLOW(F). From : add FOLLOW(T) to FOLLOW(F).
- 5Step 5
"The resulting FOLLOW sets are: FOLLOW(E) = {,}; FOLLOW(T)= {$),+, }; FOLLOW(F)= {$),+,*,$$}."
(Displayed neatly below.)
FOLLOW sets (final)
Let \ $ be end-marker.
Check intuition.
- and naturally follow expressions inside or after completing .
- follows an because .
- \ E$ at end of input (accept state).
Construct LR(0) item sets (states) for SLR(1)
- 1Step 1
Use as the start production.
- 2Step 2
Place dot before then add items for productions of and further for symbols after dots.
- 3Step 3
For each state and each grammar symbol that appears right of a dot in some item of , compute as the kernel moved one position right, then apply closure.
- 4Step 4
This yields a finite number of LR(0) item sets for this grammar.
- 5Step 5
These transitions become the GOTO entries and the shift parts of ACTION.
LR(0) states (canonical set enumeration)
We use item notation like . Let the set of items for each state be:
Transitions from :
- on to
- on to
- on to
- on I_4$
- on to
Transitions:
- on to
Transitions:
- on to
(no outgoing transitions)
Transitions:
- on to
- on to
- on to
- on I_4$
- on to
(no outgoing transitions)
Transitions:
- on to
- on to
- on I_4$
- on to
- on none (already progressed)
Transitions:
- on to
- on I_4$
- on to
Transitions:
- on to
- on to
Transitions:
- on to
(no outgoing transitions)
(no outgoing transitions)
How to place reduce actions in SLR(1)
If a state contains an item like , then for SLR(1) you add reduce by on every terminal in . This uses FOLLOW to prevent many shift/reduce conflicts that LR(0) would cause.
Fill ACTION and GOTO tables
We use the standard mapping:
- If an item exists and (terminal ), then ACTION[i,a] = shift j.
- If an item exists and , then ACTION[i,a] = reduce for all .
- If an item exists, then ACTION[i,$$$]=accept.
Production numbering (for reduce actions):
ACTION table (entries: shift s, reduce r# , accept acc)
Terminals order: id,\; +,\; *,\; (,\; ),\; \.
| State | ACTION on id | ACTION on + | ACTION on * | ACTION on ( | ACTION on ) | ACTION on $ |
|---|---|---|---|---|---|---|
| 0 | s5 | s4 | ||||
| 1 | s6 | acc | ||||
| 2 | r2 | s7 | r2 | r2 | ||
| 3 | r4 | r4 | r4 | r4 | ||
| 4 | s5 | s4 | ||||
| 5 | r6 | r6 | r6 | r6 | ||
| 6 | s5 | s4 | ||||
| 7 | s5 | s4 | ||||
| 8 | s6 | s11 | ||||
| 9 | r1 | s7 | r1 | r1 | ||
| 10 | r3 | r3 | r3 | r3 | ||
| 11 | r5 | r5 | r5 | r5 |
How to read rows (example).
- In state : item gives accept on \ E\rightarrow E\cdot + T+I_6$.
- In state : item implies reduce by (2) on terminals in ; plus we also have so shift on .
GOTO table (nonterminals )
| State | GOTO on E | GOTO on T | GOTO on F |
|---|---|---|---|
| 0 | 1 | 2 | 3 |
| 1 | |||
| 2 | |||
| 3 | |||
| 4 | 8 | 2 | 3 |
| 5 | |||
| 6 | 9 | 3 | |
| 7 | 10 | ||
| 8 | |||
| 9 | |||
| 10 | |||
| 11 |
SLR(1) construction workflow
Augment grammar
1Add ."
Compute LR(0) states
2Iterate closure+goto to enumerate ."
Compute FOLLOW sets
3Find . "
Fill ACTION/GOTO
4Shifts from transitions; reduces on FOLLOW; accept from ."
Common pitfalls and edge cases
Knowledge Check
In SLR(1), when a state contains an item with , where do we place the reduce action ?