Compiler Design
UNIT -III
1
Compiler Design by Dr. S.Siva Nageswara Rao
2
Compiler Design by Dr. S.Siva Nageswara Rao
Introduction
Why LR parsing:
3
Compiler Design by Dr. S.Siva Nageswara Rao
Bottom-Up Parsing:
Shift-Reduce Parsing:
Shift-reduce parsing is a type of bottom -up parsing that attempts to construct a parse tree for an input string beginning at the leaves (the bottom) and working up towards the root (the top).
Example: Consider the grammar:
S → aABe
A → Abc | b
B → d
4
Compiler Design by Dr. S.Siva Nageswara Rao
Steps of reduction:
Handle:
A substring which is the right side of a production such that replacement of that substring by the production left side leads eventually to a reduction to the start symbol, by the reverse of a rightmost derivation is called a handle.
5
Compiler Design by Dr. S.Siva Nageswara Rao
Stack Implementation of Shift-Reduce Parsing:
$ w$
$ S $
6
Compiler Design by Dr. S.Siva Nageswara Rao
Example: The actions a shift-reduce parser in parsing the input string id1+id2*id3, according to the ambiguous grammar for arithmetic expression
Configuration of Shift Reduce Parser on input id1+id2*id3
7
Compiler Design by Dr. S.Siva Nageswara Rao
While the primary operations of the parser are shift and reduce, there are actually four possible actions a shift-reduce parser can make:
(1) shift, (2) reduce,(3) accept, and (4) error.
• In a shift action, the next input symbol is shifted unto the top of the stack.
• In a reduce action, the parser knows the right end of the handle is at the top of the stack. It must then locate the left end of the handle within the stack and decide with what non-terminal to replace the handle.
• In an accept action, the parser announces successful completion of parsing.
• In an error action, the parser discovers that a syntax error has occurred and calls an error recovery routine
8
Compiler Design by Dr. S.Siva Nageswara Rao
A stack implementation of a Shift-Reduce parser
9
Compiler Design by Dr. S.Siva Nageswara Rao
Conflicts During Shift-Reduce Parsing
left to right right-most k lookhead
scanning derivation
10
Compiler Design by Dr. S.Siva Nageswara Rao
Shift-Reduce Parsers
SLR
CFG
CLR
LALR
11
Compiler Design by Dr. S.Siva Nageswara Rao
Operator precedence parser
Operator precedence grammar have the property that
1. no production right side is ε or
2. has two adjacent nonterminals.
E 🡪 EAE | (E) | -E | id
A 🡪 + | - | * | / | ↑
The above grammar is not operator grammar, because the right side EAE has two nonterminals.
If we substitute for a each of its alternatives, we obtain the following operator grammar:
E🡪E+E | E-E | E*E | E/E | E↑E | (E) | -E | id
12
Compiler Design by Dr. S.Siva Nageswara Rao
Three disjoint precedence relations <, > and =
a<b means a “yields precedence to” b
a=b means a “has the same precedence as b”
a>b means a “takes precedence over” b
| id | + | * | $ |
id | | > | > | > |
+ | < | > | < | > |
* | < | > | > | > |
$ | < | < | < | |
13
Compiler Design by Dr. S.Siva Nageswara Rao
$id + id * id$
$ < id > + < id > * < id > $
$ < E > + < id > * < id > $
$ < + < id > * < id > $
$ < + < E > * < id > $
$ < + < * < id > $
$ < + < * < E > $
$ < + < * > $
$ < + < E * E > $
$ < + < E > $
$ < + > $
$ < E + E > $
$ < E > $
14
Compiler Design by Dr. S.Siva Nageswara Rao
Precedence Function
| id | + | * | $ |
id | | > | > | > |
+ | < | > | < | > |
* | < | > | > | > |
$ | < | < | < | |
| + | * | id | $ |
f | 2 | 4 | 4 | 0 |
g | 1 | 3 | 5 | 0 |
g+
fid
f*
g*
f+
gid
f$
g$
15
Compiler Design by Dr. S.Siva Nageswara Rao
LR Parsers
LR(k) parsing.
left to right right-most k lookhead
scanning derivation (k is omitted 🡺 it is 1)
16
Compiler Design by Dr. S.Siva Nageswara Rao
17
Compiler Design by Dr. S.Siva Nageswara Rao
LR Parsers
18
Compiler Design by Dr. S.Siva Nageswara Rao
LR Parsing Algorithm
Sm |
Xm |
Sm-1 |
Xm-1 |
. |
. |
S1 |
X1 |
S0 |
a1 | ... | ai | ... | an | $ |
Action Table terminals and $ s t four different a actions t e s | Goto Table non-terminal s t each item is a a state number t e s |
LR Parsing Algorithm |
stack
input
output
19
Compiler Design by Dr. S.Siva Nageswara Rao
A Configuration of LR Parsing Algorithm
( So X1 S1 ... Xm Sm, ai ai+1 ... an $ )
Stack Rest of Input
X1 ... Xm ai ai+1 ... an $
20
Compiler Design by Dr. S.Siva Nageswara Rao
Actions of A LR-Parser
( So X1 S1 ... Xm Sm, ai ai+1 ... an $ ) 🡺 ( So X1 S1 ... Xm Sm ai s, ai+1 ... an $ )
( So X1 S1 ... Xm Sm, ai ai+1 ... an $ ) 🡺 ( So X1 S1 ... Xm-r Sm-r A s, ai ... an $ )
21
Compiler Design by Dr. S.Siva Nageswara Rao
Reduce Action
( So X1 S1 ... Xm-r Sm-r Y1 Sm-r ...Yr Sm, ai ai+1 ... an $ )
🡺 ( So X1 S1 ... Xm-r Sm-r A s, ai ... an $ )
X1 ... Xm-r A ai ... an $ ⇒ X1 ... Xm Y1...Yr ai ai+1 ... an $
22
Compiler Design by Dr. S.Siva Nageswara Rao
(SLR) Parsing Tables for Expression Grammar
state | id | + | * | ( | ) | $ | | E | T | F |
0 | s5 | | | s4 | | | | 1 | 2 | 3 |
1 | | s6 | | | | acc | | | | |
2 | | r2 | s7 | | r2 | r2 | | | | |
3 | | r4 | r4 | | r4 | r4 | | | | |
4 | s5 | | | s4 | | | | 8 | 2 | 3 |
5 | | r6 | r6 | | r6 | r6 | | | | |
6 | s5 | | | s4 | | | | | 9 | 3 |
7 | s5 | | | s4 | | | | | | 10 |
8 | | s6 | | | s11 | | | | | |
9 | | r1 | s7 | | r1 | r1 | | | | |
10 | | r3 | r3 | | r3 | r3 | | | | |
11 | | r5 | r5 | | r5 | r5 | | | | |
Action Table
Goto Table
1) E → E+T
2) E → T
3) T → T*F
4) T → F
5) F → (E)
6) F → id
23
Compiler Design by Dr. S.Siva Nageswara Rao
Actions of A (S)LR-Parser -- Example
stack input action output
0 id*id+id$ shift 5
0id5 *id+id$ reduce by F→id F→id
0F3 *id+id$ reduce by T→F T→F
0T2 *id+id$ shift 7
0T2*7 id+id$ shift 5
0T2*7id5 +id$ reduce by F→id F→id
0T2*7F10 +id$ reduce by T→T*F T→T*F
0T2 +id$ reduce by E→T E→T
0E1 +id$ shift 6
0E1+6 id$ shift 5
0E1+6id5 $ reduce by F→id F→id
0E1+6F3 $ reduce by T→F T→F
0E1+6T9 $ reduce by E→E+T E→E+T
0E1 $ accept
24
Compiler Design by Dr. S.Siva Nageswara Rao
Constructing SLR Parsing Tables – LR(0) Item
(four different possibility) A → a.Bb
A → aB.b
A → aBb.
G’ is G with a new production rule S’→S where S’ is the new starting symbol.
25
Compiler Design by Dr. S.Siva Nageswara Rao
Construct LR parsing table for the given context-free grammar S–>AA , A–>aA|b�
26
Compiler Design by Dr. S.Siva Nageswara Rao
27
Compiler Design by Dr. S.Siva Nageswara Rao
STEP3: Find FOLLOW of LHS of production
FOLLOW(S)=$�FOLLOW(A)=a,b,$
STEP 4: Defining 2 functions:goto[list of non-terminals] and action[list of terminals] in the parsing table. Below is the SLR parsing table.
28
Compiler Design by Dr. S.Siva Nageswara Rao
29
Compiler Design by Dr. S.Siva Nageswara Rao
LALR Parser :
30
Compiler Design by Dr. S.Siva Nageswara Rao
Construct CLR parsing table for the given context free grammar�S-->AA A-->aA|b
STEP1- Find augmented grammar�The augmented grammar of the given grammar is:-
S'-->.S ,$ [0th production]
S-->.AA ,$ [1st production]
A-->.aA ,a|b [2nd production]
A-->.b ,a|b [3rd production]
31
Compiler Design by Dr. S.Siva Nageswara Rao
STEP2 – Find LR(0) collection of items�Below is the figure showing the LR(0) collection of items. We will understand everything one by one
The terminals of this grammar are {a,b}�The non-terminals of this grammar are {S,A}
32
Compiler Design by Dr. S.Siva Nageswara Rao
33
Compiler Design by Dr. S.Siva Nageswara Rao
Once we make a CLR parsing table, we can easily make a LALR parsing table from it.
In the step2 diagram, we can see that
In LALR parsing table construction , we merge these similar states.
34
Compiler Design by Dr. S.Siva Nageswara Rao
Now we have to remove the unwanted rows
The final LALR table looks like the below.
Now we have to remove the unwanted rows
The final LALR table looks like the below
35
Compiler Design by Dr. S.Siva Nageswara Rao
Dangling-else Ambiguity�
For example:
if (condition) {
} if (condition 1) {
} if (condition 2) {
} else
{
}
36
Compiler Design by Dr. S.Siva Nageswara Rao
The Problem of Dangling-else:
Dangling else can lead to serious problems. It can lead to wrong interpretations by the compiler and ultimately lead to wrong results.
For example:
37
Compiler Design by Dr. S.Siva Nageswara Rao
Resolving Dangling-else Problem
The first way is to design non-ambiguous programming languages.
Secondly, we can resolve the dangling-else problems in programming languages by using braces and indentation.
For example:
if (condition) {
if (condition 1) {
if (condition 2) { }
}
} else { }
In the above example, we are using braces and indentation so as to avoid confusion.
Third, we can also use the “if – else if – else” format so as to specifically indicate which “else” belongs to which “if”.
For example:
if(condition) {
} else if(condition-1) {
} else if(condition-2){
}
else{
}
38
Compiler Design by Dr. S.Siva Nageswara Rao
Error Recovery in LR Parsing:
LR Parser Basically Uses the Mentioned Two Techniques to Detect Errors:
39
Compiler Design by Dr. S.Siva Nageswara Rao
Syntactic Phase Recovery:
There are some of the errors that are detected during the syntactic phase recovery:
40
Compiler Design by Dr. S.Siva Nageswara Rao
Panic Mode Recovery:
41
Compiler Design by Dr. S.Siva Nageswara Rao
Difference between Top-Down Parsing and Bottom-Up Parsing��
Aspect | Top-Down Parsing | Bottom-Up Parsing |
Approach | Starts from the top (start symbol) of the grammar and tries to match it to the input string. | Starts from the input tokens and works upwards to construct the parse tree. |
Process | Recursive descent parsing is a common approach, where each nonterminal in the grammar has a corresponding parsing function. | Shift-reduce parsing is a common approach, where tokens are shifted onto a stack and then reduced according to grammar rules. |
Grammar Restrictions | Often used for LL(k) grammars, where k is the lookahead size. | Used for a broader range of grammars, including LR(k) grammars, where k is the lookahead size. |
Predictability | Sometimes limited due to the need for lookahead and potential left recursion. | Generally more predictable and can handle a wider range of grammars. |
Error Handling | Typically offers better error messages, as parsing functions are closely tied to grammar rules. | Error messages might be less descriptive, as errors are detected during the reduction phase. |
Constructed Tree | Constructs a parse tree from top to bottom, which can closely resemble the input structure. | Constructs a parse tree from bottom to top, which might differ from the input's visual structure. |
Examples | Recursive descent parsing, LL(k) parsers. | LR(0), SLR(1), LALR(1), LR(1) parsers. |
Complexity | Can be simpler to implement for certain grammars but might be less efficient. | Often more complex to implement but tends to be more efficient for larger grammars. |
42
Compiler Design by Dr. S.Siva Nageswara Rao