Bottom-up Parsing - PowerPoint PPT Presentation

About This Presentation
Title:

Bottom-up Parsing

Description:

Lex. An. type. r1. r5. r6. Reduction r3. r3. array [ num ptpt num ] of char ... Lex. An. array. simple. of. char. simple. num. ptpt. num. char. simple. type ... – PowerPoint PPT presentation

Number of Views:52
Avg rating:3.0/5.0
Slides: 51
Provided by: mluisago
Category:
Tags: bottom | lex | parsing

less

Transcript and Presenter's Notes

Title: Bottom-up Parsing


1
Bottom-up Parsing
2
Task
  • 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

3
Example
  • type ? simple simple array simple of type
  • simple ? integer char num ptpt num

array

num
ptpt
num

Lex. An.
4
Example
  • type ? simple simple array simple of type
  • simple ? integer char num ptpt num

array


Lex. An.
5
Example
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.
6
Example
r6
  • type ? simple simple array simple of type
  • simple ? integer char num ptpt num

simple
num
ptpt
num
array

simple

of
char

Lex. An.
7
Example
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.
8
Example
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.
9
Example
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.
10
Example
r6
r5
r1
  • type ? simple simple array simple of type
  • simple ? integer char num ptpt num

simple
num
ptpt
num
char

Lex. An.
11
Example
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.

12
stack
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
13
array num ptpt num of char
array

num
ptpt
num

Lex. An.
14
array simple of char
array num ptpt num of char
array

simple

of
char

Lex. An.
15
array simple of simple
array simple of char
array num ptpt num of char
array

simple
simple
of


Lex. An.
16
array 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.
17
type
array simple of type
array simple of simple
array simple of char
array num ptpt num of char

of
array

type

Lex. An.
18
type
-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.
19
Wrong example
  • type ? simple simple array simple of type
  • simple ? integer char num ptpt num

array

num
ptpt
num

Lex. An.
20
Wrong Example
  • type ? simple simple array simple of type
  • simple ? integer char num ptpt num

array


Lex. An.
21
Wrong Example
  • type ? simple simple array simple of type
  • simple ? integer char num ptpt num

simple
num
ptpt
num
array

simple

Lex. An.
22
Wrong Example
  • type ? simple simple array simple of type
  • simple ? integer char num ptpt num

simple
num
ptpt
num
array

simple

Lex. An.
23
Wrong Example
  • type ? simple simple array simple of type
  • simple ? integer char num ptpt num

type
simple
num
ptpt
num
array


of
char

Lex. An.
24
Wrong Example
  • type ? simple simple array simple of type
  • simple ? integer char num ptpt num

simple
num
ptpt
num
array

type

of
char

Lex. An.
25
Wrong 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.
26
Wrong 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.
27
Wrong 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.

28
type
-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
29
stack
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
30
Knowing read input (and actions done), and (at
most) the next input symbol
What is the handle?

Shift or Reduce? If Reduce, which rule?
31
Some 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
32
How 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
33
S ? 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
34
stack
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
35
stack
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 ?
36
stack
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
37
stack
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
38
0 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)
39
stack
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
40
stack
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)
42
Conflicts
S ? cAba cbbb bA A ? b
b

b
b
b

43
Conflicts
S ? cAba cbbb bA A ? b

b
A


44
Conflicts
S ? cAba cbbb bA A ? b

S


45
Conflicts
S ? cAba cbbb bA A ? b
shift/reduce conflict
b
c

c
b
b
46
Conflicts
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
47
Conflicts
S ? cAba cBbb bB A ? b B ? e
shift/reduce conflict
b
c
c
48
Conflicts
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
49
Conflicts
S ? cAba cBbb bB A ? b a B ? b
reduce/reduce conflict
b
c
c
b
b
50
Conflicts
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
Write a Comment
User Comments (0)
About PowerShow.com