Compiler Construction Sohail Aslam Lecture 28. 2 StackInput ¤0¤0 id – id id $ s4 ¤0 id 4 –...
-
Upload
laura-gage -
Category
Documents
-
view
242 -
download
3
Transcript of Compiler Construction Sohail Aslam Lecture 28. 2 StackInput ¤0¤0 id – id id $ s4 ¤0 id 4 –...
![Page 1: Compiler Construction Sohail Aslam Lecture 28. 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.](https://reader036.fdocuments.in/reader036/viewer/2022062300/56649c755503460f949291bb/html5/thumbnails/1.jpg)
Compiler Compiler ConstructionConstruction
Compiler Compiler ConstructionConstruction
Sohail Aslam
Lecture 28
![Page 2: Compiler Construction Sohail Aslam Lecture 28. 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.](https://reader036.fdocuments.in/reader036/viewer/2022062300/56649c755503460f949291bb/html5/thumbnails/2.jpg)
2
Stack Input¤0 id – id id
$s4
¤0id4 – id id $ r6 F→ id¤0F3 – id id $ r5 T→ F¤0T2 – id id $ s5¤0T2–5 id id $ s4
![Page 3: Compiler Construction Sohail Aslam Lecture 28. 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.](https://reader036.fdocuments.in/reader036/viewer/2022062300/56649c755503460f949291bb/html5/thumbnails/3.jpg)
3
Action Goto
id – $ E T F0 s4 1 2 3
1 acc
2 s5 r3
3 r5 s6 r5
4 r6 r6 r6
5 s4 7 2 3
6 s4 8 3
7 r2
8 r4 r4
![Page 4: Compiler Construction Sohail Aslam Lecture 28. 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.](https://reader036.fdocuments.in/reader036/viewer/2022062300/56649c755503460f949291bb/html5/thumbnails/4.jpg)
4
Stack Input¤0 id – id id
$s4
¤0id4 – id id $ r6 F→ id¤0F – id id $ r5 T→ F¤0T2 – id id $ s5¤0T2–5 id id $ s4
![Page 5: Compiler Construction Sohail Aslam Lecture 28. 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.](https://reader036.fdocuments.in/reader036/viewer/2022062300/56649c755503460f949291bb/html5/thumbnails/5.jpg)
5
Action Goto
id – $ E T F0 s4 1 2 3
1 acc
2 s5 r3
3 r5 s6 r5
4 r6 r6 r6
5 s4 7 2 3
6 s4 8 3
7 r2
8 r4 r4
![Page 6: Compiler Construction Sohail Aslam Lecture 28. 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.](https://reader036.fdocuments.in/reader036/viewer/2022062300/56649c755503460f949291bb/html5/thumbnails/6.jpg)
6
Stack Input¤0 id – id id
$s4
¤0id4 – id id $ r6 F→ id¤0F3 – id id $ r5 T→ F¤0T2 – id id $ s5¤0T2–5 id id $ s4
![Page 7: Compiler Construction Sohail Aslam Lecture 28. 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.](https://reader036.fdocuments.in/reader036/viewer/2022062300/56649c755503460f949291bb/html5/thumbnails/7.jpg)
7
Stack Input¤0 id – id id
$s4
¤0id4 – id id $ r6 F→ id¤0F3 – id id $ r5 T→ F¤0T2 – id id $ s5¤0T2–5 id id $ s4
![Page 8: Compiler Construction Sohail Aslam Lecture 28. 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.](https://reader036.fdocuments.in/reader036/viewer/2022062300/56649c755503460f949291bb/html5/thumbnails/8.jpg)
8
Stack Input¤0T2–5id4 id $ r6 F→ id¤0T2–5F3 id $ s6¤0T2–5F36 id $ s4¤0T2–5F36id4
$ r6 F→ id
¤0T2–5F36F3 $ r5 T→ F¤0T2–5F36T8 $ r4 T→
FT
![Page 9: Compiler Construction Sohail Aslam Lecture 28. 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.](https://reader036.fdocuments.in/reader036/viewer/2022062300/56649c755503460f949291bb/html5/thumbnails/9.jpg)
9
Stack Input¤0T2–5T2 $ r3 E→T¤0T2 –5E7 $ r2 E→T–E
¤0E1 $ accept
![Page 10: Compiler Construction Sohail Aslam Lecture 28. 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.](https://reader036.fdocuments.in/reader036/viewer/2022062300/56649c755503460f949291bb/html5/thumbnails/10.jpg)
10
LR(1) Skeleton ParserLR(1) Skeleton ParserLR(1) Skeleton ParserLR(1) Skeleton Parserstack.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]);
}
![Page 11: Compiler Construction Sohail Aslam Lecture 28. 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.](https://reader036.fdocuments.in/reader036/viewer/2022062300/56649c755503460f949291bb/html5/thumbnails/11.jpg)
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;
elsereport error and recover;
}report success;
![Page 12: Compiler Construction Sohail Aslam Lecture 28. 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.](https://reader036.fdocuments.in/reader036/viewer/2022062300/56649c755503460f949291bb/html5/thumbnails/12.jpg)
12
Shift/Reduce ConflictsShift/Reduce ConflictsShift/Reduce ConflictsShift/Reduce Conflicts If a DFA states contains both
[X → ab] and [Y → a]
Then on input “a” we could either
• shift into state [X → ab], or
• reduce with Y →
![Page 13: Compiler Construction Sohail Aslam Lecture 28. 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.](https://reader036.fdocuments.in/reader036/viewer/2022062300/56649c755503460f949291bb/html5/thumbnails/13.jpg)
13
Shift/Reduce ConflictsShift/Reduce ConflictsShift/Reduce ConflictsShift/Reduce Conflicts If a DFA states contains both
[X → ab] and [Y → a]
Then on input “a” we could either
• shift into state [X → ab], or
• reduce with Y →
![Page 14: Compiler Construction Sohail Aslam Lecture 28. 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.](https://reader036.fdocuments.in/reader036/viewer/2022062300/56649c755503460f949291bb/html5/thumbnails/14.jpg)
14
Shift/Reduce ConflictsShift/Reduce ConflictsShift/Reduce ConflictsShift/Reduce Conflicts If a DFA states contains both
[X → ab] and [Y → a]
Then on input “a” we could either
• shift into state [X → ab], or
• reduce with Y →
![Page 15: Compiler Construction Sohail Aslam Lecture 28. 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.](https://reader036.fdocuments.in/reader036/viewer/2022062300/56649c755503460f949291bb/html5/thumbnails/15.jpg)
15
Shift/Reduce ConflictsShift/Reduce ConflictsShift/Reduce ConflictsShift/Reduce Conflicts If a DFA states contains both
[X → ab] and [Y → a]
Then on input “a” we could either
• shift into state [X → ab], or
• reduce with Y →
![Page 16: Compiler Construction Sohail Aslam Lecture 28. 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.](https://reader036.fdocuments.in/reader036/viewer/2022062300/56649c755503460f949291bb/html5/thumbnails/16.jpg)
16
Shift/Reduce ConflictsShift/Reduce ConflictsShift/Reduce ConflictsShift/Reduce Conflicts
This is called a shift-reduce conflict
Typically, this is due to ambiguities in the grammar
![Page 17: Compiler Construction Sohail Aslam Lecture 28. 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.](https://reader036.fdocuments.in/reader036/viewer/2022062300/56649c755503460f949291bb/html5/thumbnails/17.jpg)
17
Shift/Reduce ConflictsShift/Reduce ConflictsShift/Reduce ConflictsShift/Reduce Conflicts
This is called a shift-reduce conflict
Typically, this is due to ambiguities in the grammar
![Page 18: Compiler Construction Sohail Aslam Lecture 28. 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.](https://reader036.fdocuments.in/reader036/viewer/2022062300/56649c755503460f949291bb/html5/thumbnails/18.jpg)
18
Shift/Reduce ConflictsShift/Reduce ConflictsShift/Reduce ConflictsShift/Reduce Conflicts
Classic example: dangling else
stmt → if E then stmt| if E then stmt else
stmt
![Page 19: Compiler Construction Sohail Aslam Lecture 28. 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.](https://reader036.fdocuments.in/reader036/viewer/2022062300/56649c755503460f949291bb/html5/thumbnails/19.jpg)
19
Shift/Reduce ConflictsShift/Reduce ConflictsShift/Reduce ConflictsShift/Reduce Conflicts
We will have DFA state containing
[stmt → if E then stmt, else]
[stmt → if E then stmt else stmt, x]
![Page 20: Compiler Construction Sohail Aslam Lecture 28. 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.](https://reader036.fdocuments.in/reader036/viewer/2022062300/56649c755503460f949291bb/html5/thumbnails/20.jpg)
20
Shift/Reduce ConflictsShift/Reduce ConflictsShift/Reduce ConflictsShift/Reduce Conflicts
If else follows, we can shift
[stmt → if E then stmt else stmt, x]
or reduce
[stmt → if E then stmt, else]
![Page 21: Compiler Construction Sohail Aslam Lecture 28. 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.](https://reader036.fdocuments.in/reader036/viewer/2022062300/56649c755503460f949291bb/html5/thumbnails/21.jpg)
21
Shift/Reduce ConflictsShift/Reduce ConflictsShift/Reduce ConflictsShift/Reduce Conflicts
If else follows, we can shift
[stmt → if E then stmt else stmt, x]
or reduce
[stmt → if E then stmt, else]
![Page 22: Compiler Construction Sohail Aslam Lecture 28. 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.](https://reader036.fdocuments.in/reader036/viewer/2022062300/56649c755503460f949291bb/html5/thumbnails/22.jpg)
22
Shift/Reduce ConflictsShift/Reduce ConflictsShift/Reduce ConflictsShift/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