0% found this document useful (0 votes)
17 views

Resolving Conflicts

The document discusses precedence and associativity to resolve conflicts in parsing expressions. It provides examples of parsing expressions with + and * operators and different precedences. It also discusses resolving the dangling else ambiguity in grammars with if-then-else statements.

Uploaded by

Shyam Karanth
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Resolving Conflicts

The document discusses precedence and associativity to resolve conflicts in parsing expressions. It provides examples of parsing expressions with + and * operators and different precedences. It also discusses resolving the dangling else ambiguity in grammars with if-then-else statements.

Uploaded by

Shyam Karanth
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Precedence and Associativity to

Resolve conflicts

3/9/2020 Dept Of CSE, MIT 1


Consider the Grammar

EE+E
EE*E
E( E )
Eid

3/9/2020 Dept Of CSE, MIT 2


DFA:
I0: E>.E E I1: E>E. + I4: EE+.E id
E.E+E EE.+E
E.E+E
E.E*E EE.*E 3
E.E*E
E. (E)
E. (E)
E.id
E.id 2
* (
id I2: E (.E) (
( E.E+E I5: EE*.E
E.E*E (
I3: Eid. E E.E+E
E. (E) E.E*E
id E.id I7: EE+E. E. (E)
3
EE.+E E.id
E EE.*E id
*
E
I6: E(E.) +
EE.+E 5
EE.*E I8: EE*E. *
4 EE.+E
5
EE.*E

+ * )
I9: E( E ). +
4 5

4
3/9/2020 Dept Of CSE, MIT 3
Initial Parse Table
State id + * ( ) $ E
0 s3 s2 1
1 s4 s5 accept
2 s3 s2 6
3 r4 r4 r4 r4
4 s3 s2 7
5 s3 s2 8
6 s4 s5 s9
7 r1,s4 r1,s5 r1 r1
8 r2,s4 r2,s5 r2 r2
9 r3 r3 r3 r3

3/9/2020 Dept Of CSE, MIT 4


How to resolve conflict??
 Conflict occurs in state 7 and 8.
 Consider four different cases
◦ id+id+id
◦ id*id*id
◦ id+id*id
◦ id*id+id
 Solution: In case of ‘+’ and ‘*’ give more precedence to *
In case of ‘+’ and ‘+’ or ‘*’ and ‘*’ go for left associativity.

3/9/2020 Dept Of CSE, MIT 5


Case1: id+id+id
Stack Symbol Input Action
0 id+id+id$ shift
03 id +id+id$ reduce; eid
01 E +id+id$ shift
014 E+ +id+id$ shift
0143 E+id +id$ reduce; eid
0147 E+E +id$ conflict; reduce
01 E +id$ shift
014 E+ id$ shift
0143 E+id $ reduce; eid
0147 E+E $ reduce; ee+e
01 E $ accept

3/9/2020 Dept Of CSE, MIT 6


Parse table will be
State id + * ( ) $ E
0 s3 s2 1
1 s4 s5 accept
2 s3 s2 6
3 r4 r4 r4 r4
4 s3 s2 7
5 s3 s2 8
6 s4 s5 s9
7 r1 r1,s5 r1 r1
8 r2,s4 r2,s5 r2 r2
9 r3 r3 r3 r3

3/9/2020 Dept Of CSE, MIT 7


Case 2: id+id*id
Stack Symbol Input Action
0 id+id*id$ shift
03 id +id*id$ reduce; eid
01 E +id*id$ shift
014 E+ Id*id$ shift
0143 E+id *id$ reduce; eid
0147 E+E *id$ conflict; Shift
01475 E+E* id$ shift
014753 E+E*id $ Reduce Eid
014758 E+E*E $ reduce; EE*E
0147 E+E $ reduce; EE+E
01 E $ accept

3/9/2020 Dept Of CSE, MIT 8


Parse table will be
State id + * ( ) $ E
0 s3 s2 1
1 s4 s5 accept
2 s3 s2 6
3 r4 r4 r4 r4
4 s3 s2 7
5 s3 s2 8
6 s4 s5 s9
7 r1 s5 r1 r1
8 r2,s4 r2,s5 r2 r2
9 r3 r3 r3 r3

3/9/2020 Dept Of CSE, MIT 9


Case 3: id*id*id
Stack Symbol Input Action
0 id*id*id$ Shift
03 id *id*id$ reduce; Eid
01 E *id*id$ shift
015 E* id*id$ shift
0153 E*id *id$ reduce; Eid
0158 E*E *id$ conflict; Reduce
01 E *id$ shift
015 E* id$ shift
0153 E*id $ reduce; Eid
0158 E*E $ reduce; EE*E
01 E $ accept

3/9/2020 Dept Of CSE, MIT 10


Parse table will be
State id + * ( ) $ E
0 s3 s2 1
1 s4 s5 accept
2 s3 s2 6
3 r4 r4 r4 r4
4 s3 s2 7
5 s3 s2 8
6 s4 s5 s9
7 r1 s5 r1 r1
8 r2,s4 r2 r2 r2
9 r3 r3 r3 r3

3/9/2020 Dept Of CSE, MIT 11


Case 3: id * id + id
Stack Symbol Input Action
0 id*id+id$ Shift
03 id *id+id$ reduce; Eid
01 E *id+id$ shift
015 E* id+id$ shift
0153 E*id +id$ reduce; Eid
0158 E*E +id$ conflict; Reduce
01 E +id$ shift
014 E+ id$ shift
0143 E+id $ reduce; Eid
0147 E+E $ reduce; EE+E
01 E $ accept

3/9/2020 Dept Of CSE, MIT 12


Parse table will be
State id + * ( ) $ E
0 s3 s2 1
1 s4 s5 accept
2 s3 s2 6
3 r4 r4 r4 r4
4 s3 s2 7
5 s3 s2 8
6 s4 s5 s9
7 r1 s5 r1 r1
8 r2 r2 r2 r2
9 r3 r3 r3 r3

3/9/2020 Dept Of CSE, MIT 13


The “Dangling-Else” ambiguity

Simplifying the above grammar

where,
i - if expr then
e- else
a- all other statements
S- stmt

3/9/2020 Dept Of CSE, MIT 14


LR(0) automaton
S
I0: S’.S I1: S’S.
S.iSeS
S.iS i
S.a I2: Si.SeS i
Si.S
S.iSeS
a a S.iS
S.a
I3: Sa.
S
a e I4: SiS.eS
SiS.
I5: SiSe.S
S.iSeS
S.iS
S.a I6: SiSeS.
S
i
2
3/9/2020 Dept Of CSE, MIT 15
Parse table
States i e a $ S
Conflict; So use disambiguating
0 s2 rules3 1
to match else with nearest
unmatched then. Hence Shift
1 accept
2 s2 s3 4
3 r3 r3
4 s5,r2 r2
5 s2 s3
6 r1 r1
3/9/2020 Dept Of CSE, MIT 16
Resulting Parse table
States i e a $ S
0 s2 s3 1
1 accept
2 s2 s3 4
3 r3 r3
4 S5 r2
5 s2 s3
6 r1 r1
3/9/2020 Dept Of CSE, MIT 17
Parsing Actions for “iiaea”
Stack Symbols Input Action
0 iiaea$ Shift
02 I iaea$ Shift
022 ii aea$ Shift
0223 iia ea$ Reduce Sa
0224 iiS ea$ Shift
02245 iiSe a$ Shift
022453 iiSea $ Sa
022456 iiSes $ SiSeS
024 iS $ SiS
01 S $ accept

3/9/2020 Dept Of CSE, MIT 18

You might also like