Professional Documents
Culture Documents
SLR Parsing
SLR Parsing
Parsing
Shift-Reduce Parsers
SLR
2. LR-Parsers
– covers wide range of grammars.
• SLR – simple LR parser
• LR – most general LR parser
• LALR – intermediate LR parser (lookhead LR parser)
– SLR, LR and LALR work same, only their parsing tables are different.
LR Parsers
Drawback of LR method:
– Too much work to construct LR parser by hand
• Fortunately tools (LR parsers generators) are available
LR Parsing Algorithm
.
.
S1 Action Table Goto Table
terminals and $ non-terminal
X1
s s
S0 t four different t each item is
a actions a a state number t
t e
e s
s
Items and LR(0) Automaton
S -> AB
A -> a
B -> b
LR(0) collection of items
A -> a
B -> b
LR(0) collection of items
CLOSURE EXAMPLE:
LR(0) collection of items
CLOSURE EXAMPLE:
LR(0) collection of items
GOTO:
GOTO (I,X) = I’
LR(0) collection of items
GOTO EXAMPLE:
LR(0) collection of items
Consider the following grammar:
S-> AB
A-> a
B -> b
Let’s build an LR(0) Automaton for the grammar
First step is to augment the grammar by introducing a
new start symbol
S’->S
S-> AB
A-> a
B -> b
LR(0) collection of items
S’-> .S
S-> .AB
A->.a
I0
LR(0) collection of items
S’-> S.
S’-> .S S
S-> .AB I1
A->.a
I0
LR(0) collection of items
S’-> S.
S’-> .S S
S-> .AB I1
A->.a
I0
A
S-> A.B
B->.b
I2
LR(0) collection of items
S’-> S.
S’-> .S S
S-> .AB I1
A->.a
I0
A
S-> A.B
B->.b
a
I2
A->a.
I3
LR(0) collection of items
S’-> S.
S’-> .S S
S-> .AB I1
A->.a B S->AB.
I0
A
S-> A.B I4
B->.b
a
I2
A->a.
I3
LR(0) collection of items
S’-> S.
S’-> .S S
S-> .AB I1
A->.a B S->AB.
I0
A
S-> A.B I4
B->.b
a b
I2
B->b.
A->a.
I5
I3
LR Parse table
Action(i,a) GOTO(i,A)
Structure of LR Parsing Table
Building SLR(1) Parse table
S’-> S.
S’-> .S S
S-> .AB I1
A->.a B S->AB.
I0
A
S-> A.B I4
B->.b
a b
I2
B->b.
A->a.
I5
I3
SLR (1) parse table
States ACTION(I,a) GOTO (I,A)
a b $ S A B
0 s3
2 s5
5
SLR (1) parse table
States ACTION(I,a) GOTO (I,A)
a b $ S A B
0 s3 1 2
2 s5 4
5
SLR(1) PARSE TABLE
0. S’->S.
1. S-> AB.
2. A->a.
3. B->b.
LR(0) collection of items
S’-> S.
S’-> .S S
S-> .AB I1
A->.a B S->AB.
I0
A
S-> A.B I4
B->.b
a b
I2
B->b.
A->a.
I5
I3
SLR(1) PARSE TABLE
0. S’->S.
Follow (S’)={$}
Rule 0 is in state 1, so reduction/
acceptance in state 1
SLR (1) parse table
States ACTION(I,a) GOTO (I,A)
a b $ S A B
0 s3 1 2
1 accept
2 s5 4
5
LR(0) collection of items
S’-> S.
S’-> .S S
S-> .AB I1
A->.a B S->AB.
I0
A
S-> A.B I4
B->.b
a b
I2
B->b.
A->a.
I5
I3
SLR(1) PARSE TABLE
0. S’->S.
Follow (S’)={$}
Rule 0 is reduced in state 1, so
reduction/acceptance in state 1
1. S-> AB.
Follow (S) = {$}
Rule 1 is reduced in state 4, so reduction by rule 1
in state 4
SLR (1) parse table
States ACTION(I,a) GOTO (I,A)
a b $ S A B
0 S3 1 2
1 accept
2 S5 4
4 R1
5
LR(0) collection of items
S’-> S.
S’-> .S S
S-> .AB I1
A->.a B S->AB.
I0
A
S-> A.B I4
B->.b
a b
I2
B->b.
A->a.
I5
I3
SLR(1) PARSE TABLE
2. A-> a.
Follow (A)={b}
Rule 2 is reduced in state 3, so reduction by rule 2
in state 3
SLR (1) parse table
States ACTION(I,a) GOTO (I,A)
a b $ S A B
0 S3 1 2
1 accept
2 S5 4
3 R2
4 R1
5
LR(0) collection of items
S’-> S.
S’-> .S S
S-> .AB I1
A->.a B S->AB.
I0
A
S-> A.B I4
B->.b
a b
I2
B->b.
A->a.
I5
I3
SLR(1) PARSE TABLE
3. B->b.
Follow (B)={$}
Rule 3 is reduced in state 5 , so reduction by rule
3 in state 5
SLR (1) parse table
States ACTION(I,a) GOTO (I,A)
a b $ S A B
0 S3 1 2
1 accept
2 S5 4
3 R2
4 R1
5 R3
SLR PARSING USING PARSE
TABLE
Output of SLR(1) parser on “ab$”
a b $ S A B
0 S3 1 2
1 accept
2 S5 4
3 R2
4 R1
5 R3
Output of SLR(1) parser on “ab$”
03 a b $
SLR (1) parse table
States ACTION(I,a) GOTO (I,A)
a b $ S A B
0 S3 1 2
1 accept
2 S5 4
3 R2
4 R1
5 R3
Output of SLR(1) parser on “ab$”
0 a b $ Shift to 3
03 a b $ Reduce using
A->a
SLR PARSING USING PARSE
TABLE
Action (3,b) = Reduce using Rule 2
= Reduce using A->a
0 a b $ Shift to 3
03 a b $ Reduce using
A->a
02 a b $
SLR (1) parse table
States ACTION(I,a) GOTO (I,A)
a b $ S A B
0 S3 1 2
1 accept
2 S5 4
3 R2
4 R1
5 R3
Output of SLR(1) parser on “ab$”
0 a b $ Shift to 3
03 a b $ Reduce using
A->a
02 a b $ Shift to 5
SLR PARSING USING PARSE
TABLE
Output of SLR(1) parser on “ab$”
03 a b $ Reduce
using
A->a
02 a b $ Shift to 5
025 a b $
SLR (1) parse table
States ACTION(I,a) GOTO (I,A)
a b $ S A B
0 S3 1 2
1 accept
2 S5 4
3 R2
4 R1
5 R3
Output of SLR(1) parser on “ab$”
03 a b $ Reduce using
A->a
02 a b $ Shift to 5
03 a b $ Reduce using
A->a
02 a b $ Shift to 5
a b $ S A B
0 S3 1 2
1 accept
2 S5 4
3 R2
4 R1
5 R3
Output of SLR(1) parser on “ab$”
03 a b $ Reduce using
A->a
02 a b $ Shift to 5
03 a b $ Reduce using
A->a
02 a b $ Shift to 5
a b $ S A B
0 S3 1 2
1 accept
2 S5 4
3 R2
4 R1
5 R3
SLR PARSING USING PARSE
TABLE
Output of SLR(1) parser on “ab$”
03 a b $ Reduce using
A->a
02 a b $ Shift to 5