Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Shift-Reduce Parsers
1.
2.
LR-Parsers
CFG
LR
LALR
SLR
LR Parsers
The most powerful shift-reduce parsing (yet efficient) is:
LR(k) parsing.
left to right
scanning
right-most
derivation
k lookhead
(k is omitted it is 1)
More on LR(k)
Can recognize virtually all programming language constructs (if CFG
can be given)
Most general non-backtracking shift-reduce method known, but can be
implemented efficiently
Class of grammars can be parsed is a superset of grammars parsed by
LL(k)
Can detect syntax errors as soon as possible
More on LR(k)
Main drawback: too tedious to do by hand for typical
programming lang. grammars
We need a parser generator
Many available
Yacc (yet another compiler compiler) or bison for C/C++
environment
CUP (Construction of Useful Parsers) for Java environment;
JavaCC is another example
We write the grammar and the generator produces the parser for that
grammar
LR Parsers
LR-Parsers
covers wide range of grammars.
SLR simple LR parser
LR most general LR parser
LALR intermediate LR parser (look-head LR parser)
SLR, LR and LALR work same (they used the same algorithm),
only their parsing tables are different.
LR Parsing Algorithm
input a1
... ai
... an
stack
Sm
Xm
LR Parsing Algorithm
Sm-1
output
Xm-1
.
.
Action Table
S1
X1
S0
Goto Table
terminals and $
s
t
a
t
e
s
four different
actions
non-terminal
s
t
a
t
e
s
each item is
a state number
id
s5
goto
)
s4
s6
r2
s7
r2
r2
r4
r4
r4
r4
s4
r6
1.
2.
3.
4.
5.
6.
Notation:
acc
s5
Rules:
r6
r6
s5
s4
s5
s4
EE+T
ET
TT*F
TF
F(E)
F id
r6
10
s6
s11
r1
s7
r1
r1
10
r3
r3
r3
r3
11
r5
r5
r5
r5
s5 = shift 5
r2 = reduce by
E T
9
sn
gn
rk
a
Meaning
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 $ )
LR Parsing Algorithm
Refer Text:
Compilers Principles Techniques and Tools by Alfred V Aho, Ravi
Sethi, Jeffery D Ulman
Page No. 218-219
12
input
id*id+id$
*id+id$
*id+id$
*id+id$
id+id$
+id$
+id$
+id$
+id$
id$
$
$
$
$
action
shift 5
reduce by Fid
reduce by TF
shift 7
shift 5
reduce by Fid
reduce by TT*F
reduce by ET
shift 6
shift 5
reduce by Fid
reduce by TF
reduce by EE+T
accept
output
Fid
TF
Fid
TT*F
ET
Fid
TF
EE+T
13
Key Idea
Deciding when to shift and when to reduce is based on a DFA
applied to the stack
Edges of DFA labeled by symbols that can be on stack
(terminals + non-terminals)
Transition table defines transitions (and characterizes the type
of LR parser)
14
SLR PARSING
The central idea in the SLR method is first to construct from
the grammar a DFA to recognize viable prefixes. We group
items into sets, which become the states of the SLR parser.
Viable prefixes:
The set of prefixes of a right sentential form that can appear on the
stack of a Shift-Reduce parser is called Viable prefixes.
Example :- a, aa, aab, and aabb are viable prefixes of aabbbbd.
16
LR(0) Item
An LR(0) item is a production and a position in its RHS marked by a dot
(e.g., A )
The dot tells how much of the RHS we have seen so far. For example,
for a production S XYZ,
S XYZ: we hope to see a string derivable from XYZ
S XYZ: we have just seen a string derivable from X and we hope
to see a string derivable from YZ
SXY.Z : we have just seen a string derivable from XY and we
hope to see a string derivable from Z
SXYZ. : we have seen a string derivable from XYZ and going to
reduce it to S
(X, Y, Z are grammar symbols)
17
Augmented Grammar
If G is a grammar with start symbol S, then G', the
augmented grammar for G, is G with
new start symbol S' and
the production S' S.
The purpose of the augmenting production is to indicate to
the parser when it should stop parsing and accept the input.
That is, acceptance occurs only when the parser is about to
reduce by the production S' S.
18
19
closure({[E E]}) =
{
{ [E E] }
{ [E E]
{ [E E]
[E E + T] [E E + T]
[E T] }
[E T]
[T T * F]
Add [E]
[T F] }
Add [T]
Add [F]
Grammar:
EE+T|T
TT*F|F
F(E)
F id
[E E]
[E E + T
[E T]
[T T * F]
[T F]
[F ( E )]
[F id] }
20
State 0
We start by adding item E' E to
state 0.
This item has a " " immediately to the
left of a nonterminal. Whenever this is
the case, we must perform step 3
(closure) of the set construction
algorithm.
We add the items E E + T and E
T to state 0, giving
I0:
E' E
E E+T
ET
T T*F
T F
F (E)
F id
{ E' E
EE+T
ET }
21
State 0
Reapplying closure to E T, we must add the
items T T * F and
T F to state 0, giving
I0:
{ E' E
EE+T
ET
TT*F
TF
}
E' E
E E+T
ET
T T*F
T F
F (E)
F id
22
State 0
Reapplying closure to T F, we must
add the items F ( E ) and F id
to state 0, giving
I0: { E' E
EE+T
ET
TT*F
TF
F(E)
F id
}
E' E
E E+T
ET
T T*F
T F
F (E)
F id
23
24
Then goto(I,E)
Suppose I ={ [E E]
[E E + T] = closure({[E E , E E + T]}
= { [E E ]
[E T]
[E E + T] }
[T T * F]
[T F]
[F ( E )]
[F id] }
Grammar:
EE+T|T
TT*F|F
F(E)
F id
25
E' E
E E+T
ET
T T*F
T F
F (E)
F id
I0
I1
26
State 1
State 1 starts with the items E' E and
E E + T. These items are formed from
items E' E and E E + T by
moving the "" one grammar symbol to the
right. In each case, the grammar symbol is
E.
Closure does not add any new items, so
state 1 ends up with the 2 items:
I1: {
E' E
EE+T
}
I0
E' E
E E+T
ET
T T*F
T F
F (E)
F id
I1
27
I0
E' E
E E+T
ET
T T*F
T F
F (E)
F id
I2
28
E' E
E E+T
ET
T T*F
T F
F (E)
F id
I3
29
30
E' E
E E+T
ET
T T*F
T F
F (E)
F id
31
State 4
Applying closure to E T, we add
items T T * F and
T F to state 4, giving
F(E)
EE+T
ET
TT*F
TF
E' E
E E+T
ET
T T*F
T F
F (E)
F id
32
State 4
Applying step 3 to T F, we add items F
( E ) and F id to state 4, giving the
final set of items
I 4: {
F(E)
EE+T
ET
TT*F
TF
F(E)
F id
}
The next slide shows the DFA to this point.
E' E
E E+T
ET
T T*F
T F
F (E)
F id
I0
I4
33
34
F id
}
Since this item is a complete item, we will
not be able to produce new states from state
5.
The next slide shows the DFA to this point.
I0
id
E' E
E E+T
ET
T T*F
T F
F (E)
F id
I4
35
36
E' E
E E+T
ET
T T*F
T F
F (E)
F id
I6: {
EE+T
T T*F
T F
F ( E )
F id
}
I1
I6
37
38
E' E
E E+T
ET
T T*F
T F
F (E)
F id
I7: {
TT*F
F (E)
F id
}
I2
I7
39
40
E' E
E E+T
ET
T T*F
T F
F (E)
F id
I8: {
F (E)
E E+T
}
No further items can be added to state 8
through closure.
There are other transitions from state 4,
but they do not result in new states.
I4
I8
41
43
I9 : {
E' E
E E+T
ET
T T*F
T F
F (E)
F id
E E+T
T T*F
}
All other transitions from state 6 go to
existing states. The next slide shows the
DFA to this point.
I6
I9
44
45
E' E
E E+T
ET
T T*F
T F
F (E)
F id
T T*F
}
All other transitions from state 7 go to
existing states. The next slide shows
the DFA to this point.
I7
I10
46
47
F (E)
}
All other transitions from state 8 go to
existing states.
State 9 has one transition to an existing
state (7). No other new states can be
added, so we are done.
The next slide shows the final DFA for
I8
viable prefixes.
E' E
E E+T
ET
T T*F
T F
F (E)
F id
I11
48
49
50
E E+T
ET
T T*F
TF
F (E)
F id
53
54
55
Action Table
1)
2)
3)
4)
5)
6)
E E+T
ET
T T*F
TF
F (E)
F id
state
id
s5
Goto Table
(
s4
1
2
3
4
5
6
7
8
9
10
11
56
57
Action Table
1)
2)
3)
4)
5)
6)
E E+T
ET
T T*F
TF
F (E)
F id
state
id
s5
Goto Table
(
s4
s6
acc
2
3
4
5
6
7
8
9
10
11
58
59
Action Table
1)
2)
3)
4)
5)
6)
E E+T
ET
T T*F
TF
F (E)
F id
state
id
s5
Goto Table
(
s4
s6
acc
2
3
4
5
r6
r6
r6
r6
7
8
9
10
60
The complete SLR parse table for the expression grammar is given on the next slide.
61
E E+T
ET
T T*F
TF
F (E)
F id
state
id
s5
Goto Table
)
s4
s6
r2
s7
r2
r2
r4
r4
r4
r4
s4
r6
acc
s5
r6
r6
s5
s4
s5
s4
r6
10
s6
s11
r1
s7
r1
r1
10
r3
r3
r3
r3
11
r5
r5
r5
r5
62
input
id*id+id$
*id+id$
*id+id$
*id+id$
id+id$
+id$
+id$
+id$
+id$
id$
$
$
$
$
action
shift 5
reduce by Fid
reduce by TF
shift 7
shift 5
reduce by Fid
reduce by TT*F
reduce by ET
shift 6
shift 5
reduce by Fid
reduce by TF
reduce by EE+T
accept
output
Fid
TF
Fid
TT*F
ET
Fid
TF
EE+T
63
SLR PARSING
The central idea behind SLR method was first to construct
from the grammar a DFA to recognize viable prefixes. We
group items into sets, which become the states of the SLR
parser.
Viable prefixes:
The set of prefixes of a right sentential form that can appear on the
stack of a Shift-Reduce parser is called Viable prefixes.
Example :- a, aa, aab, and aabb are viable prefixes of aabbbbd.
64
I0 = closure({[C C]})
I1 = goto(I0,C) = closure({[C C]})
goto(I0,C)
State I1:
C C final
State I4:
C A B
goto(I2,B)
State I0:
State I2:
goto
(
I
,
A
)
C C
0
C AB
C A B
B a
goto(I2,a)
A a
goto(I0,a) State I3:
A a
State I5:
B a
65
State I0:
State I1:
C C
C C
C A B
A a
1
start
C
A
2
a
a
3
s3
s5
r3
acc
4
r2
r4
Grammar:
1. C C
2. C A B
3. A a
4. B a
66
67
Conflict Example
S L=R
SR
L *R
L id
RL
I0: S .S
S .L=R
S .R
L .*R
L .id
R .L
Problem
FOLLOW(R)={=,$}
=
shift 6
reduce by R L
shift/reduce conflict
I1:S S.
I2:S L.=R
R L.
I6:S L=.R
R .L
L .*R
L .id
I9: S L=R.
I3:S R.
I4:L *.R
R .L
L .*R
L .id
I7:L *R.
I8:R L.
I5:L id.
68
Conflict Example2
S AaAb
S BbBa
A
B
I0: S .S
S .AaAb
S .BbBa
A.
B.
Problem
FOLLOW(A)={a,b}
FOLLOW(B)={a,b}
a
reduce by A
reduce by B
reduce/reduce conflict
reduce by A
reduce by B
reduce/reduce conflict
b
69