Title: Bottom-up Parsing
1Bottom-up Parsing
2Task
- Parsing (of course) but do it this way
- Bottom-up
- Easy and algorithmic
- Efficiently
- Knowing (input) as little as possible
- Marking errors as soon as possible
- Using a stack explicitly
3Example
- type ? simple simple array simple of type
- simple ? integer char num ptpt num
array
num
ptpt
num
Lex. An.
4Example
- type ? simple simple array simple of type
- simple ? integer char num ptpt num
array
Lex. An.
5Example
r6
- type ? simple simple array simple of type
- simple ? integer char num ptpt num
Reduction r6
simple
num
ptpt
num
array
simple
of
char
Lex. An.
6Example
r6
- type ? simple simple array simple of type
- simple ? integer char num ptpt num
simple
num
ptpt
num
array
simple
of
char
Lex. An.
7Example
r6
r5
- type ? simple simple array simple of type
- simple ? integer char num ptpt num
Reduction r5
simple
num
ptpt
num
simple
char
array
simple
of
Lex. An.
8Example
r6
r5
- type ? simple simple array simple of type
- simple ? integer char num ptpt num
simple
num
ptpt
num
char
array
simple
of
simple
Lex. An.
9Example
r6
r5
r1
- type ? simple simple array simple of type
- simple ? integer char num ptpt num
Reduction r1
simple
type
type
num
ptpt
num
char
array
simple
of
Lex. An.
10Example
r6
r5
r1
- type ? simple simple array simple of type
- simple ? integer char num ptpt num
simple
num
ptpt
num
char
Lex. An.
11Example
r6
r5
r1
- type ? simple simple array simple of type
- simple ? integer char num ptpt num
r3
Reduction r3
type
(no more)
simple
of
array
num
ptpt
num
char
Lex. An.
12stack
input
action
array num ptpt num of char
shift
array
num ptpt num of char
shift
array
num ptpt num of char
shift
array num
ptpt num of char
shift
array num ptpt
num of char
shift
array num ptpt num
of char
reduce 6
array simple
of char
shift
array simple
of char
shift
array simple of
char
shift
array simple of char
reduce 5
array simple of simple
reduce 1
array simple of type
reduce 3
type
OK
13array num ptpt num of char
array
num
ptpt
num
Lex. An.
14array simple of char
array num ptpt num of char
array
simple
of
char
Lex. An.
15array simple of simple
array simple of char
array num ptpt num of char
array
simple
simple
of
Lex. An.
16array simple of type
array simple of simple
array simple of char
array num ptpt num of char
of
array
array
type
simple
of
Lex. An.
17type
array simple of type
array simple of simple
array simple of char
array num ptpt num of char
of
array
type
Lex. An.
18type
-gt rm
array simple of type
-gt rm
array simple of simple
array simple of char
-gt rm
-gt rm
array num ptpt num of char
of
array
type
Lex. An.
19Wrong example
- type ? simple simple array simple of type
- simple ? integer char num ptpt num
array
num
ptpt
num
Lex. An.
20Wrong Example
- type ? simple simple array simple of type
- simple ? integer char num ptpt num
array
Lex. An.
21Wrong Example
- type ? simple simple array simple of type
- simple ? integer char num ptpt num
simple
num
ptpt
num
array
simple
Lex. An.
22Wrong Example
- type ? simple simple array simple of type
- simple ? integer char num ptpt num
simple
num
ptpt
num
array
simple
Lex. An.
23Wrong Example
- type ? simple simple array simple of type
- simple ? integer char num ptpt num
type
simple
num
ptpt
num
array
of
char
Lex. An.
24Wrong Example
- type ? simple simple array simple of type
- simple ? integer char num ptpt num
simple
num
ptpt
num
array
type
of
char
Lex. An.
25Wrong Example
- type ? simple simple array simple of type
- simple ? integer char num ptpt num
simple
num
ptpt
num
simple
char
array
type
of
Lex. An.
26Wrong Example
- type ? simple simple array simple of type
- simple ? integer char num ptpt num
simple
num
ptpt
num
char
array
type
of
simple
Lex. An.
27Wrong Example
- type ? simple simple array simple of type
- simple ? integer char num ptpt num
?
simple
type
type
num
ptpt
num
char
array
type
of
Lex. An.
28type
-gt 3
array simple of type
-gt 1
array simple of simple
array simple of char
-gt 5
-gt 6
array num ptpt num of char
29stack
input
action
array num ptpt num of char
shift
array
num ptpt num of char
shift
array
num ptpt num of char
shift
array num
ptpt num of char
shift
array num ptpt
num of char
shift
array num ptpt num
of char
reduce 6
array simple
of char
shift
array simple
of char
shift
array simple of
char
shift
array simple of char
reduce 5
array simple of simple
reduce 1
array simple of type
reduce 3
type
OK
30Knowing read input (and actions done), and (at
most) the next input symbol
What is the handle?
Shift or Reduce? If Reduce, which rule?
31Some tips
- type ? simple simple array simple of type
- simple ? integer char num ptpt num
Dont reduce type ? simple after
When there is simple at the top of the stack
reduce type ? simple
after of
when the next input symbol is
or mark error
if the next input symbol is not nor
Knowing (input) as little as possible
Marking errors as soon as possible
32How to decide shift-reduce
- type ? simple simple array simple of type
- simple ? integer char num ptpt num
- At the beginning (state 0)
- Shift on integer, char, num. (state 1)
- Shift on array (state2)
- otherwise Mark error
- State1 (integer, char, num on top)
- If num on top, shift on ptpt (state 3), mark
error otherwise - Else, reduce by 4 or 5,depending on the symbol at
the top - State 2 (array on top)
- Shift on (state 4)
- Mark error otherwise
- State 3 (num ptpt on top)
- Shift on num (state 5)
- Mark error otherwise
Let Yacc do it
33S ? c S A d d A ? a B a B ? a b
S
c S A d
S ? cSAd
c S a d
A ? a
c d a d
c c d a a d a d
S ? d
34stack
input
action
shift
c d a d
shift
c
d a d
reduce 2 S ? d
c d
a d
shift
c S
a d
reduce 4 A ? a
d
c S a
shift
c S A
d
reduce 1 S ? cSAd
c S A d
OK
S
S
d
c
c S A d
S ? cSAd
-gt rm
c S a d
-gt rm
A ? a
d
a
c d a d
S ? d
-gt rm
35stack
input
action
shift
c d a d
shift
c
d a d
reduce 2 S ? d
c d
a d
shift
c S
a d
reduce 4 A ? a
d
c S a
shift
c S A
d
reduce 1 S ? cSAd
c S A d
OK
S
LR
(1)
LALR(1)
n
YACC ?
36stack
input
action
shift 1
c d a d
0
shift 2
0 c 1
d a d
reduce 2 S ? d goto 1, S 4
0 c 1 d 2
a d
shift 5
0 c 1 S 4
a d
reduce 4 A ? a goto 4, A 6
d
0 c 1 S 4 a 5
shift 10
0 c 1 S 4 A 6
d
reduce 1 S ? cSAd goto 0, S 3
0 c 1 S 5 A 6 d 10
OK
0 S 3
Action Action Action Action Action GoTo GoTo GoTo
a b c d S A B
0 s1 s2 3
1 s1 s2 4
2 r2 r2 r2 r2 r2
3 OK
4 s5 6
0
3
d
c
1
4
6
10
5 s7 s8 r4 9
6 s10
7 r5 r5 r5 r5 r5
8 r6 r6 r6 r6 r6
9 r3 r3 r3 r3 r3
10 r1 r1 r1 r1 r1
d
a
2
5
37stack
input
action
shift 1
c d a d
0
shift 2
0 c 1
d a d
reduce 2 S ? d goto 1, S 4
0 c 1 d 2
a d
shift 5
0 c 1 S 4
a d
reduce 4 A ? a goto 4, A 6
d
0 c 1 S 4 a 5
shift 10
0 c 1 S 4 A 6
d
reduce 1 S ? cSAd goto 0, S 3
0 c 1 S 5 A 6 d 10
OK
0 S 3
Action Action Action Action Action GoTo GoTo GoTo
a b c d S A B
0 s1 s2 3
1 s1 s2 4
2 r2 r2 r2 r2 r2
3 OK
4 s5 6
0
3
d
c
1
4
6
10
5 s7 s8 r4 9
6 s10
7 r5 r5 r5 r5 r5
8 r6 r6 r6 r6 r6
9 r3 r3 r3 r3 r3
10 r1 r1 r1 r1 r1
d
a
2
5
380 accept . S end (0)
Action Action Action Action Action GoTo GoTo GoTo
a b c d S A B
0 s1 s2 3
1 s1 s2 4
2 r2 r2 r2 r2 r2
3 OK
4 s5 6
1 S c . S A d (1)
2 S d . (2)
3 accept S . end (0)
4 S c S . A d (1)
5 A a . B (3) A a . (4)
5 s7 s8 r4 9
6 s10
7 r5 r5 r5 r5 r5
8 r6 r6 r6 r6 r6
9 r3 r3 r3 r3 r3
10 r1 r1 r1 r1 r1
6 S c S A . d (1)
7 B a . (5)
8 B b . (6)
9 A a B . (3)
10 S c S A d . (1)
39stack
input
action
0 accept . S end (0)
shift 1
c d a d
0
1 S c . S A d (1)
shift 2
0 c 1
d a d
2 S d . (2)
reduce 2 S ? d goto 1, S 4
0 c 1 d 2
a d
shift 5
0 c 1 S 4
a d
reduce 4 A ? a goto 4, A 6
d
0 c 1 S 4 a 5
shift 10
0 c 1 S 4 A 6
d
reduce 1 S ? cSAd goto 0, S 3
0 c 1 S 5 A 6 d 10
OK
0 S 3
Action Action Action Action Action GoTo GoTo GoTo
a b c d S A B
0 s1 s2 3
1 s1 s2 4
2 r2 r2 r2 r2 r2
3 OK
4 s5 6
0
3
d
c
1
4
6
10
5 s7 s8 r4 9
6 s10
7 r5 r5 r5 r5 r5
8 r6 r6 r6 r6 r6
9 r3 r3 r3 r3 r3
10 r1 r1 r1 r1 r1
d
a
2
5
40stack
input
action
shift 1
c d a d
0
shift 2
0 1
d a d
reduce 2 S ? d goto 1, S 4
0 1 2
a d
shift 5
0 1 4
a d
reduce 4 A ? a goto 4, A 6
d
0 1 4 5
shift 10
0 1 4 6
d
reduce 1 S ? cSAd goto 0, S 3
0 1 5 6 10
OK
0 3
Action Action Action Action Action GoTo GoTo GoTo
a b c d S A B
0 s1 s2 3
1 s1 s2 4
2 r2 r2 r2 r2 r2
3 OK
4 s5 6
0
3
d
c
1
4
6
10
5 s7 s8 r4 9
6 s10
7 r5 r5 r5 r5 r5
8 r6 r6 r6 r6 r6
9 r3 r3 r3 r3 r3
10 r1 r1 r1 r1 r1
d
a
2
5
41(No Transcript)
42Conflicts
S ? cAba cbbb bA A ? b
b
b
b
b
43Conflicts
S ? cAba cbbb bA A ? b
b
A
44Conflicts
S ? cAba cbbb bA A ? b
S
45Conflicts
S ? cAba cbbb bA A ? b
shift/reduce conflict
b
c
c
b
b
46Conflicts
S ? cAba cbbb bA A ? b
4 shift/reduce conflict (shift 8, reduce 4) on
b state 4 S c b . b b (2) A b .
(4)
shift/reduce conflict
c
b
b
47Conflicts
S ? cAba cBbb bB A ? b B ? e
shift/reduce conflict
b
c
c
48Conflicts
S ? cAba cBbb bB A ? b B ? e
1 shift/reduce conflict (shift 4, reduce 5) on
b state 1 S c . A b a (1) S c . B
b b (2) B . (5)
shift/reduce conflict
b
c
c
49Conflicts
S ? cAba cBbb bB A ? b a B ? b
reduce/reduce conflict
b
c
c
b
b
50Conflicts
S ? cAba cBbb bB A ? b a B ? b
4 reduce/reduce conflict (reduce 4, reduce 6) on
b state 4 A b . (4) B b . (6)
reduce/reduce conflict
b
c
b