Download presentation
Presentation is loading. Please wait.
Published byLaura Gage Modified over 10 years ago
1
Compiler Construction Sohail Aslam Lecture 28
2
2 StackInput ¤0¤0 id – id id $ s4 ¤0 id 4 – id id $ r6 F → id ¤0F3¤0F3 – id id $ r5 T → F ¤0T2¤0T2 – id id $ s5 ¤0T2–5¤0T2–5 id id $ s4
3
3 ActionGoto id– $ETF 0s4123 1acc 2s5r3 3r5s6r5 4r6 5s4723 6 83 7r2 8r4
4
4 StackInput ¤0¤0 id – id id $ s4 ¤0 id 4 – id id $ r6 F → id ¤0F¤0F – id id $ r5 T → F ¤0T2¤0T2 – id id $ s5 ¤0T2–5¤0T2–5 id id $ s4
5
5 ActionGoto id– $ETF 0s4123 1acc 2s5r3 3r5s6r5 4r6 5s4723 6 83 7r2 8r4
6
6 StackInput ¤0¤0 id – id id $ s4 ¤0 id 4 – id id $ r6 F → id ¤0F3¤0F3 – id id $ r5 T → F ¤0T2¤0T2 – id id $ s5 ¤0T2–5¤0T2–5 id id $ s4
7
7 StackInput ¤0¤0 id – id id $ s4 ¤0 id 4 – id id $ r6 F → id ¤0F3¤0F3 – id id $ r5 T → F ¤0T2¤0T2 – id id $ s5 ¤0T2–5¤0T2–5 id id $ s4
8
8 StackInput ¤0 T 2 – 5 id 4 id $ r6 F → id ¤0T2–5F3¤0T2–5F3 id $ s6 ¤0T2–5F36¤0T2–5F36 id $ s4 ¤0 T 2 – 5 F 3 6 id 4 $ r6 F → id ¤0T2–5F36F3¤0T2–5F36F3 $ r5 T → F ¤0T2–5F36T8¤0T2–5F36T8 $ r4 T → F T
9
9 StackInput ¤0T2–5T2¤0T2–5T2 $ r3 E → T ¤0 T 2 – 5 E 7 $ r2 E → T–E ¤0E1¤0E1 $ accept
10
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
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;
12
12 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 →
13
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
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 →
15
15 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
16 Shift/Reduce Conflicts This is called a shift-reduce conflict Typically, this is due to ambiguities in the grammar
17
17 Shift/Reduce Conflicts This is called a shift-reduce conflict Typically, this is due to ambiguities in the grammar
18
18 Shift/Reduce Conflicts Classic example: dangling else stmt → if E then stmt | if E then stmt else stmt
19
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
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 ]
21
21 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 ]
22
22 Shift/Reduce Conflicts [ stmt → if E then stmt , else ] [ stmt → if E then stmt else stmt, x ] Typical action is shift so that else matches with most recent if
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.