Professional Documents
Culture Documents
Module3 PPT
Module3 PPT
COMPILER CONSTRUCTION
Module III
LR PARSERS
Dr A. K. Jayswal
ASET(CSE)
MTech(CSE)-JNU
PhD(CSE)-JNU
GATE(CS), UGC-NET(CS)
1
Module III:
Different types of LR Parsers
Simple LR(0) collection of items
SLR parsing table
SLR parsing
LR(1) collection of items
CLR parsing table
CLR parsing
LALR parsing table
LALR parsing
2
Amity School of Engineering and Technology
Recommended Reading
Textbooks:
• Alfred V. Aho, Monica S. Lam, Ravi Sethi, Jeffrey D. Ullman, “Compilers:
Principles Techniques and Tool”, Second Edition, Pearson Publication, 2007.
• A.A Putambekar, “Compiler Construction”, Technical Publications, 2009.
Reference Book:
• Des Watson, A Practical Approach to Compiler Construction, First Edition, Springer,
2017
3
Amity School of Engineering and Technology
OBJECTIVES
After completing this section, you will be able to
4
Amity School of Engineering and Technology
Module Assessment
• Quiz (Conceptual and Numerical Based)
• Assignment
5
Amity School of Engineering and Technology
LR Parsers
The most powerful shift-reduce parsing (yet efficient) is:
LR Parsers
7
Amity School of Engineering and Technology
LR Parsers
8
Amity School of Engineering and Technology
LR Parsers
9
Amity School of Engineering and Technology
LR Parsers
10
Amity School of Engineering and Technology
LR Parsers
11
Amity School of Engineering and Technology
LR Parsers
12
Amity School of Engineering and Technology
LR Parsers
13
Amity School of Engineering and Technology
14
Amity School of Engineering and Technology
15
Amity School of Engineering and Technology
LR Parsers
Advantages of the LR parser
16
Amity School of Engineering and Technology
LR Parsers
Different types of LR-Parsers
SLR, LR and LALR work same (they used the same algorithm), only their parsing tables
are different.
17
Amity School of Engineering and Technology
LR Parsing Algorithm
18
Amity School of Engineering and Technology
Actions of A LR-Parser
1. shift s -- shifts the next input symbol and the state s onto the stack
( So X1 S1 ... Xm Sm, ai ai+1 ... an $ ) ( So X1 S1 ... Xm Sm ai s, ai+1 ... an $ )
4. Error -- Parser detected an error (an empty entry in the action table) 20
Amity School of Engineering and Technology
Reduce Action
• pop 2|| (=r) items from the stack; let us assume that = Y1Y2...Yr
• then push A and s where s=goto[sm-r,A]
21
Amity School of Engineering and Technology
A collection of sets of LR(0) items (the canonical LR(0) collection) is the basis for
constructing SLR parsers.
Augmented Grammar:
G’ is G with a new production rule S’S where S’ is the new starting symbol.
22
Amity School of Engineering and Technology
23
Amity School of Engineering and Technology
.
E E+T { E’ E kernel items
.
ET E E+T
.
T T*F E T
.
TF T T*F
.
F (E) T F
.
F id F (E)
F id }
24
Amity School of Engineering and Technology
Computation of Closure
function closure ( I )
begin
J := I;
repeat
for each item A .B in J and each production
B of G such that B. is not in J do
add B. to J
until no more items can be added to J
end
25
Amity School of Engineering and Technology
GOTO Operation
If I is a set of LR(0) items and X is a grammar symbol (terminal or non-terminal), then
goto(I,X) is defined as follows:
If A .X in I then every item in closure({A X.}) will be in goto(I,X).
If I is the set of items that are valid for some viable prefix , then goto(I,X) is the set of
items that are valid for the viable prefix X.
Example: I ={ E’ .E, E .E+T, E .T,
T .T*F, T .F,
F .(E), F .id }
goto(I,E) = { E’ E., E E.+T }
goto(I,T) = { E T., T T.*F }
goto(I,F) = {T F. }
goto(I,() = { F (.E), E .E+T, E .T, T .T*F, T .F,
F .(E), F .id }
goto(I,id) = { F id. }
26
Amity School of Engineering and Technology
Practice Questions
Ques 1: Consider the grammar S-> Aa| bAc| Bc| bBa, A->d, B->d
Compute closure(I) and goto(I)
27
Amity School of Engineering and Technology
• Algorithm:
C is { closure({S’.S}) }
repeat the followings until no more set of LR(0) items can be added to C.
for each I in C and each grammar symbol X
if goto(I,X) is not empty and not in C
add goto(I,X) to C
28
Amity School of Engineering and Technology
30
Amity School of Engineering and Technology
32
Parsing a string w from SLR(1) Parsing Table
33
(SLR) Parsing Tables for Expression Grammar
34
Actions of (S)LR-Parser -- Example
35
Amity School of Engineering and Technology
Practice Question
Ques 1: Consider the following grammar
E-> E+ T | T
T-> TF | F
F-> F * | a | b
Construct the SLR parsing table for this grammar.
Ques 2: Construct SLR Parsing table for the grammar
S-> L= R
S-> R
L->*R
L-> id
R-> L
36
Amity School of Engineering and Technology
SLR(1) Grammar
An LR parser using SLR(1) parsing tables for a grammar G is called as the SLR(1)
parser for G.
If a grammar G has an SLR(1) parsing table, it is called SLR(1) grammar (or SLR
grammar in short).
Every SLR grammar is unambiguous, but every unambiguous grammar is not a SLR
grammar.
37
Amity School of Engineering and Technology
If a state does not know whether it will make a reduction operation using the
production rule i or j for a terminal, we say that there is a reduce/reduce conflict.
If the SLR parsing table of a grammar G has a conflict, we say that that grammar is
not SLR grammar.
38
Amity School of Engineering and Technology
Conflict Example-1
39
Amity School of Engineering and Technology
Conflict Example-2
40
Amity School of Engineering and Technology
41
Amity School of Engineering and Technology
42
Amity School of Engineering and Technology
43
Amity School of Engineering and Technology
44
Amity School of Engineering and Technology
45
Amity School of Engineering and Technology
46
Amity School of Engineering and Technology
47
Amity School of Engineering and Technology
LR(1) Item
• To avoid some of invalid reductions, the states need to carry more information.
• Extra information is put into a state by including a terminal symbol as a second
component in an item.
LR(1) Item(cont.)
When ( in the LR(1) item A .,a ) is not empty, the look-head does not have any
affect.
When is empty (A .,a ), we do the reduction by A only if the next input
symbol is a (not for any terminal in FOLLOW(A)).
49
Amity School of Engineering and Technology
50
Amity School of Engineering and Technology
Goto Operation
• If I is a set of LR(1) items and X is a grammar symbol (terminal or
non-terminal), then goto(I,X) is defined as follows:
If A .X,a in I
then every item in closure({A X.,a}) will be in goto(I,X).
51
Amity School of Engineering and Technology
52
Amity School of Engineering and Technology
53
Amity School of Engineering and Technology
54
Amity School of Engineering and Technology
55
Amity School of Engineering and Technology
S’ S, $ I1
S C C, $ S (S’ S , $
C c C, c/d
C d, c/d
S C C, $ I5
C C c C, $ C S C C , $
C d, $
I2 c I6
C c C, $
c C c C, $ I9
C d, $
C cC , $
I7
d C d , $
c
C c C, c/d
C c C, c/d C I8
c I3 C d, c/d C c C , c/d
I4 d
56
C d , c/d
Amity School of Engineering and Technology
An Example
57
Amity School of Engineering and Technology
58
Amity School of Engineering and Technology
• The core of a set of LR(1) Items is the set of their first components (i.e., LR(0)
items)
• The core of the set of LR(1) items
{ (C c C, c/d),
(C c C, c/d),
(C d, c/d) }
is { C c C,
C c C,
Cd}
59
Amity School of Engineering and Technology
2. LALR parsers are often used in practice because LALR parsing tables are
smaller than LR(1) parsing tables.
3. The number of states in SLR and LALR parsing tables for a grammar G are equal.
61
Amity School of Engineering and Technology
62
Amity School of Engineering and Technology
We will find the states (sets of LR(1) items) in a canonical LR(1) parser with same
cores. Then we will merge them as a single state.
I1:L id.,= A new state: I12: L id.,=
L id.,$
I2:L id.,$ have same core, merge them
We will do this for all states of a canonical LR(1) parser to get the states of the LALR
parser.
In fact, the number of the states of the LALR parser for a grammar will be equal to
the number of states of the SLR parser for that grammar.
63
Amity School of Engineering and Technology
3. Create the parsing tables (action and goto tables) same as the construction of
the parsing tables of LR(1) parser.
1. Note that: If J=I1 ... Ik since I1,...,Ik have same cores
cores of goto(I1,X),...,goto(I2,X) must be same.
2. So, goto(J,X)=K where K is the union of all sets of items having same cores as
goto(I1,X).
65
Amity School of Engineering and Technology
66
Amity School of Engineering and Technology
c d $ S C
0 s36 s47 1 2
1 acc
2 s36 s47 5
36 s36 s47 89
47 r3 r3 r3
5 r1
89 r2 r2 r2
67
Amity School of Engineering and Technology
Shift/Reduce Conflict
• We say that we cannot introduce a shift/reduce conflict during the shrink
process for the creation of the states of a LALR parser.
• Assume that we can introduce a shift/reduce conflict. In this case, a state of LALR
parser must have:
A .,a and B .a,b
• This means that a state of the canonical LR(1) parser must have:
A .,a and B .a,c
But, this state has also a shift/reduce conflict. i.e. The original canonical LR(1)
parser has a conflict.
(Reason for this, the shift operation does not depend on lookaheads)
68
Amity School of Engineering and Technology
Reduce/Reduce Conflict
• But, we may introduce a reduce/reduce conflict during the shrink process for the
creation of the states of a LALR parser.
69
Amity School of Engineering and Technology
71
Amity School of Engineering and Technology
72
Amity School of Engineering and Technology
73
Amity School of Engineering and Technology
74
Amity School of Engineering and Technology
75
Amity School of Engineering and Technology
76
Amity School of Engineering and Technology
77
Amity School of Engineering and Technology
78