Shift-Reduce Conflicts in Parsing: Resolving Ambiguities in Grammar, Slides of Compiler Construction

Shift-reduce conflicts, a common issue in parsing where ambiguities in the grammar lead to conflicting actions. The concept of shift-reduce conflicts, their causes, and a classic example of dangling else statements. It also explains how to resolve such conflicts by prioritizing the shift action.

Typology: Slides

2011/2012

Uploaded on 11/03/2012

ekavia
ekavia 🇮🇳

4.3

(58)

241 documents

1 / 22

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Compiler
Construction
Lecture 28
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16

Partial preview of the text

Download Shift-Reduce Conflicts in Parsing: Resolving Ambiguities in Grammar and more Slides Compiler Construction in PDF only on Docsity!

Compiler

Construction

Lecture 28

2 Stack Input ¤0 id – id × id $ s ¤0id4 – id × id $ r6 F→ id ¤0F3 – id × id $ r5 T→ F ¤0T2 – id × id $ s ¤0T2–5 id × id $ s

4 Stack Input ¤0 id – id × id $ s ¤0id4 – id × id $ r6 F→ id ¤0F – id × id $ r5 T→ F ¤0T2 – id × id $ s ¤0T2–5 id × id $ s

5 Action Goto id – × $ E T F 0 s4 1 2 3 1 acc 2 s5 r 3 r5 s6 r 4 r6 r6 r 5 s4 7 2 3 6 s4 8 3 7 r 8 r4 r

7 Stack Input ¤0 id – id × id $ s ¤0id4 – id × id $ r6 F→ id ¤0F3 – id × id $ r5 T→ F ¤0T2 – id × id $ s ¤0T2–5 id × id $ s

8 Stack Input ¤0T2–5id4 × id $ r6 F→ id ¤0T2–5F3 × id $ s ¤0T2–5F3× 6 id $ s ¤0T2–5F3×6id4 $ r6 F→ id ¤0T2–5F3×6F3 $ r5 T→ F ¤0T2–5F3×6T8 $^ r4 T→ F×T

10

LR(1) Skeleton Parser

stack.push(dummy); stack.push(0); done = false; token = scanner.next(); while (!done) { s = stack.top(); if( Action[s,token] == “reduce A→β”) { stack.pop(2×|β|); s = stack.top(); stack.push(A); stack.push(Goto[s,A]); }

11 else if( Action[s,token] == “shift i”){ stack.push(token); stack.push(i); token = scanner.next(); } else if(Action[s,token] == “accept” && token == “$” ) done = true; else report error and recover; } report success;

13 Shift/Reduce Conflicts

 If a DFA states contains both

[X → α•aβ,b] and [Y → γ•,a]

 Then on input “a” we could

either

  • shift into state [X → αa•β,b], or
  • reduce with Y → γ

14 Shift/Reduce Conflicts

 If a DFA states contains both

[X → α•aβ,b] and [Y → γ•,a]

 Then on input “a” we could

either

  • shift into state [X → αa•β,b], or
  • reduce with Y → γ

16 Shift/Reduce Conflicts

 This is called a shift-reduce

conflict

 Typically, this is due to

ambiguities in the grammar

17 Shift/Reduce Conflicts

 This is called a shift-reduce

conflict

 Typically, this is due to

ambiguities in the grammar

19 Shift/Reduce Conflicts

We will have DFA state

containing

[stmt → if E then stmt•, else] [stmt → if E then stmt • else stmt, x]

20 Shift/Reduce Conflicts

If else follows, we can shift

[stmt → if E then stmt • else stmt, x]

or reduce

[stmt → if E then stmt•, else]