2081 1

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]

Compiler Design and ConstructionChapter 2: Lexical, Syntax and Semantic AnalysisLR parsingNumerical: SLR parsing table

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$: $; from L -> L,E: ,; and E inherits from L -> E where L is followed by ) and ,)
  • $\text{FOLLOW}(L) = { ),\ ,\ }$ (from E -> (L): ); from L -> 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),$EL
0s2s31
1acc
2s2s354
3r2r2r2
4s6s7
5r4r4
6r1r1r1
7s2s38
8r3r3

Notes:

  • Reductions by production (2) E->a and (1) E->(L) appear on FOLLOW(E) = { ), , , $ }.
  • Reductions by production (4) L->E and (3) L->L,E appear 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.