12 LR0 SLR PDF
12 LR0 SLR PDF
12 LR0 SLR PDF
CS453 Lecture
parse
s2
r2
s2
s1
r2
r2
S
g3
r2
r2
s1
g6
s5
s7
r1
r1
r1
r1
r1
r3
r3
r3
r3
r3
s2
r4
CS453
s1
r4
r4
g8
r4
r4
g4
(x,(x))$
stack
0
0(2
0(2x1
0(2S6
0(2L4
0(2L4,7
0(2L4,7(2
0(2L4,7(2x1
0(2L4,7(2S6
0(2L4,7(2L4
0(2L4,7(2L4)5
0(2L4,7S8
0(2L4
0(2L4)5
03S
Shift-reduce Parsing
input
action
(x,(x))$
s2
x,(x))$
s1
,(x))$ r2: Sx
,(x))$ r3: LS
,(x))$ s7
(x))$ s2
x))$ s1
))$ r2: Sx
))$ r3: LS
))$ s5
)$ r1: S (L)
)$ r4: LL,S
)$ s5
$ r1:S(L)
$ a
2
3: LS
4: LL , S
Shift-reduce Parsing
S x.
1:V[x]
S(.L)
L.L,S
L.S
S .x
S.(L)
2:V[(]
goto action:
Also in state 0
we could have
reduced to S
SS.$
3:V[S]
Transitions: the shifts and gotos explicitly connect the states. The
reduces implicitly move to another state by popping the rhs off the
stack, after which a goto with the lhs will produce a new next state
CS453
Shift-reduce Parsing
S.S$
S .x
S.(L)
0:V[]
S
(
SS.$
3:V[S]
S
L S.
6:V[(S]
CS453
S x.
1:V[x]
x
S(.L)
L.L,S
L.S
S.x
S.(L)
2:V[(]
L L,.S
S.x
S.(L)
7:V[(L] ,
S (L.)
L L.,S
4:V[(L]
)
S (L).
5:V[(L)]
Shift-reduce Parsing
L L,S.
8:V[(L,S]
List grammar
0: S S$
1: S( L )
2: Sx
3: LS
4: LL , S
Shift-reduce Parsing
S.S$
0:V[]
CS453
Shift-reduce Parsing
S.S$
S .x
S.(L)
0:V[]
S
(
SS.$
3:V[S]
S
L S.
6:V[(S]
CS453
S x.
1:V[x]
x
S(.L)
L.L,S
L.S
S.x
S.(L)
2:V[(]
L L,.S
S.x
S.(L)
7:V[(L] ,
S (L.)
L L.,S
4:V[(L]
)
S (L).
5:V[(L)]
Shift-reduce Parsing
L L,S.
8:V[(L,S]
List grammar
0: S S$
1: S( L )
2: Sx
3: LS
4: LL , S
Parse table
rows: states
columns: terminals (for shift and reduce actions)
non-terminals (for goto actions)
For each edge (X: (I, J))
if X is terminal, put shift J at (I, X)
if X is non-terminal, put goto J at (I, X)
if I contains SS . $ , put accept at (I, $)
if I contains A . where A . has grammar rule number n
for each terminal x, put reduce reduce n at (I, x)
CS453
Shift-reduce Parsing
parse
s2
r2
s2
r1
r3
s2
r4
CS453
r2
s1
r2
s1
s5
r1
r3
r4
r1
r3
s1
r4
S
g3
r2
r2
g6
a
s7
r1
r3
g4
r1
r3
g8
r4
r4
Shift-reduce Parsing
(x,(x))$
stack
input
0
(x,(x))$
0(2
x,(x))$
0(2x1
,(x))$
0(2S6
,(x))$
0(2L4
,(x))$
0(2L4,7
(x))$
0(2L4,7(2 x))$
0(2L4,7(2x1 ))$
0(2L4,7(2S6 ))$
0(2L4,7(2L4 ))$
0(2L4,7(2L4)5 )$
0(2L4,7S8
)$
0(2L4
)$
0(2L4)5
$
03S
$
action
s2
s1
r2: Sx
r3: LS
s7
s2
s1
r2: Sx
r3: LS
s5
r1: S (L)
r4: LL,S
s5
r1:S(L)
a
10
CS453 Lecture
11
CS453 Lecture
12
s3
r2
r2
2
3
CS453 Lecture
r2
ID
s1
r2
accept
s3
4
5
Goto
s1
s5
r0
r0
r0
r0
13
CS453 Lecture
14
In state 1:
we reduce (ET.) AND we shift
ET.
TT . *F
TT * . F
2
(TT . *F)
CS453
Shift-reduce Parsing
15
id
F
T
T*
T*id
T*F
T
E
CS453
input
action
id*id$
S
*id$ R: Fid
*id$ R: FT
*id$ S
id$ S
$ R: Fid
$ R: TT*F
$
R:E
$
accept
Shift-reduce Parsing
16
SLR parsing
SLR parsing is LR(0) parsing, but with a different reduce rule:
For each edge (X: (I, J))
if X is terminal, put shift J at (I, X)
if I contains A . where A . has rule number n
for each terminal x in Follow(A), put reduce reduce n at (I, x)
CS453
Shift-reduce Parsing
17
Shift-reduce Parsing
18
a
*(b+c)$
R:
Fid
F
*(b+c)$
R:
TF
T
*(b+c)$
S
T*
(b+c)$
S
T*(
b+c)$
S
T*(b
+c)$
R:
Fid
T*(F
+c)$
R:
TF
T*(T
+c)$
R:
ET
T*(E
+c)$
S
SE$
input: a*(b+c)$
SE$T$T*F$T*(E)$T*(E+T)$T*(E+F)$
T*(E+id)$T*(T+id)$
T*(F+id)$T*(id+id)$
F*(id+id)$id*(id+id)$
Rightmost
deriva/on
in
reverse
CS453
Shift-reduce Parsing
19