Docsity

Shift/Reduce and Reduce/Reduce Conflicts in LR Parsing, Slides of Compiler Construction

Partial preview of the text

Download Shift/Reduce and Reduce/Reduce Conflicts in LR Parsing and more Slides Compiler Construction in PDF only on Docsity!

Compiler

Construction

Lecture 29

2

Shift/Reduce Conflicts

 Consider the ambiguous grammar E → E + E | E × E | int

 We will DFA state containg [E → E × E•, +] [E → E • + E, +]

4

[E → E × E•, +]

[E → E • + E, +]

 Again we have a shift/reduce conflict

 We need to reduce because × has precedence over +

5

[E → E × E•, +]

[E → E • + E, +]

 Again we have a shift/reduce conflict

 We need to reduce because × has precedence over +

7

Reduce/Reduce Conflicts

 This is called a reduce-reduce conflict

 Usually due to gross ambiguity in the grammar

8

LR(1) Table Size

 LR(1) parsing table for even a simple language can be extremely large with thousands of entries.

10

LR(1) Table Size

 It is possible to reduce the size of the table

 Many states in the DFA are similar

11

0

E → int on ),+

int

E

(

E int

E → int on $,+

accept on $

S E→→ • •E, $E+(E), $/+ E→ • int, $/+

E→ int • , $/+ 1

2 SE→→^ EE••, $ +(E), $/+

E→ E+•(E), $/+ 3

E E→→ E+(•E+(E), )/+•E), $/+ 4 E→ • int, )/+ 6 E E→→^ E+(•E+(E), )/+•E), $/+ E→ • int, )/+ E→ int • , )/+ 5

13

LR(1) Table Size

These states have the same core

E → int on $,+

E→ int • , $/+ 1 E → int on ),+

E→ int • , )/+ 5

14

LR(1) Table Size

Upon merge, we obtain

E → int on $,+,)

E→ int • , $/+/) 1'

16

The Core

Example:

the core of { [X → α•β, b], [Y → γ•δ, d] } is { X → α•β, Y → γ•δ }

17

Core of a Set of LR Items

 Consider the LR(1) states { [X → α•, a], [Y → β•, c] } { [X → α•, b], [Y → β•, d] }  They have the same core and can be merged

19

LALR States

 The merged state contains

{ [X → α•, a/b], [Y → β•, c/d] }

 These are called the LALR(1) states

20

LALR States

 The merged state contains

{ [X → α•, a/b], [Y → β•, c/d] }

 These are called the LALR(1) states