compiler construction sohail aslam lecture 28. 2 stackinput ¤0¤0 id – id id $ s4 ¤0 id 4 –...
TRANSCRIPT
Compiler Compiler ConstructionConstruction
Compiler Compiler ConstructionConstruction
Sohail Aslam
Lecture 28
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
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
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
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
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
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
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
9
Stack Input¤0T2–5T2 $ r3 E→T¤0T2 –5E7 $ r2 E→T–E
¤0E1 $ accept
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]);
}
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;
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 →
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 →
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 →
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 →
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
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
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
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]
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]
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]
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