CSC376 · 2081 · 10 marks
Give an example of reduce-reduce conflict. Construct the SLR parsing table for the following grammar.E→(L)∣aE \rightarrow (L) \mid aE→(L)∣aL→L,E∣EL \rightarrow L, E \mid EL→L,E∣E
Give an example of reduce-reduce conflict. Construct the SLR parsing table for the following grammar.E→(L)∣aE \rightarrow (L) \mid aE→(L)∣aL→L,E∣EL \rightarrow L, E \mid EL→L,E∣E[10]
Reduce-Reduce Conflict and SLR Parsing Table
Part 1: Reduce-Reduce Conflict Example
A reduce-reduce conflict occurs when, in a single LR state, two or more complete items (items with the dot at the far right) call for reduction by different productions, and a lookahead symbol is in the FOLLOW set of more than one of the corresponding left-hand nonterminals. The parser cannot decide which production to reduce by.
Example Grammar:
S -> A
S -> B
A -> c
B -> c
After reading c, the state contains both complete items:
A -> c .
B -> c .
Since both $A$ and $B$ can appear in the same context (via S -> A and S -> B), the FOLLOW sets of $A$ and $B$ overlap (both contain $). On lookahead $, the parser cannot decide whether to reduce A -> c or B -> c. This is a reduce-reduce conflict.
Part 2: SLR Parsing Table
Given Grammar (augmented and numbered):
$$ \begin{aligned} (0)\ & E' \rightarrow E \ (1)\ & E \rightarrow (L) \ (2)\ & E \rightarrow a \ (3)\ & L \rightarrow L, E \ (4)\ & L \rightarrow E \end{aligned} $$
FIRST and FOLLOW Sets
$$\text{FIRST}(E) = \text{FIRST}(L) = {\ (\ ,\ a\ }$$
- $$\text{FOLLOW}(E') = { $ }$$
- $$\text{FOLLOW}(E) = { $,\ ),\ ,\ }$$ (from $E'\to E$:
$; fromL -> L,E:,; andEinherits fromL -> EwhereLis followed by)and,) - $\text{FOLLOW}(L) = { ),\ ,\ }$ (from
E -> (L):); fromL -> L,E:,)
Canonical LR(0) Item Sets
I0: E'->.E, E->.(L), E->.a → on E→I1, on ( →I2, on a→I3
I1: E'->E.
I2: E->(.L), L->.L,E, L->.E, E->.(L), E->.a → on L→I4, on E→I5, on ( →I2, on a→I3
I3: E->a.
I4: E->(L.), L->L.,E → on ) →I6, on , →I7
I5: L->E.
I6: E->(L).
I7: L->L,.E, E->.(L), E->.a → on E→I8, on ( →I2, on a→I3
I8: L->L,E.
SLR Parsing Table
| State | ( | a | ) | , | $ | E | L |
|---|---|---|---|---|---|---|---|
| 0 | s2 | s3 | 1 | ||||
| 1 | acc | ||||||
| 2 | s2 | s3 | 5 | 4 | |||
| 3 | r2 | r2 | r2 | ||||
| 4 | s6 | s7 | |||||
| 5 | r4 | r4 | |||||
| 6 | r1 | r1 | r1 | ||||
| 7 | s2 | s3 | 8 | ||||
| 8 | r3 | r3 |
Notes:
- Reductions by production (2)
E->aand (1)E->(L)appear on FOLLOW(E) ={ ), , , $ }. - Reductions by production (4)
L->Eand (3)L->L,Eappear on FOLLOW(L) ={ ), , }.
The table has no multiply-defined entries, so the grammar is SLR(1).
Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.