CS 2130 - PowerPoint PPT Presentation

1 / 79
About This Presentation
Title:

CS 2130

Description:

CS 2130 Lecture 18 Bottom-Up Parsing or Shift-Reduce Parsing – PowerPoint PPT presentation

Number of Views:47
Avg rating:3.0/5.0
Slides: 80
Provided by: bleahy
Learn more at: http://www.cc.gatech.edu
Category:
Tags: parsing | reduce | shift

less

Transcript and Presenter's Notes

Title: CS 2130


1
CS 2130
  • Lecture 18
  • Bottom-Up Parsing
  • or
  • Shift-Reduce Parsing

2
Bottom Up Parsing
  • Assume we have a group of tokens
  • Bottom up parsing tries to group tokens into
    things it can reduce
  • Like our precedence rules make
  • 1 2 3
  • Understood to be
  • (1 (2 3))
  • Same idea but with bottom up parsing we use
    special operators Wirth-Weber

3
Wirth-Weber Operators
4
Operation
  • Given
  • a b c d e f g h

Keep in mind that the letters represent tokens
thus they can be operators, operands, special
symbols, etc
5
Operation
  • Given
  • lt a b c d e f g h gt

Assumed
6
Operation
  • Given
  • lt a b c d e f g h gt
  • Parser moves through tokens comparing precedence

7
Operation
  • Given
  • lt a b gt c d e f g h gt
  • A series starting with lt and ending with gt is
    called a handle.
  • When a handle is found the parser looks for a
    match with the right hand side of some production

8
Operation
  • Given
  • lt a b gt c d e f g h gt
  • Assume the following is in effect
  • r a b
  • s d e f
  • t r c s

9
Operation
  • Given
  • lt r c d e f g h gt
  • Assume the following is in effect
  • r a b
  • s d e f
  • t r c s

10
Operation
  • Given
  • lt r c d e f g h gt
  • We continue
  • Assume the following is in effect
  • r a b
  • s d e f
  • t r c s

11
Operation
  • Given
  • lt r c d e f g h gt
  • We continue
  • Assume the following is in effect
  • r a b
  • s d e f
  • t r c s

12
Operation
  • Given
  • lt r c lt d e f g h gt
  • We continue
  • Assume the following is in effect
  • r a b
  • s d e f
  • t r c s

13
Operation
  • Given
  • lt r c lt d e f g h gt
  • We continue
  • Assume the following is in effect
  • r a b
  • s d e f
  • t r c s

14
Operation
  • Given
  • lt r c lt d e f g h gt
  • We continue
  • Assume the following is in effect
  • r a b
  • s d e f
  • t r c s

15
Operation
  • Given
  • lt r c lt d e f gt g h gt
  • We continue
  • Assume the following is in effect
  • r a b
  • s d e f
  • t r c s

16
Operation
  • Given
  • lt r c lt d e f gt g h gt
  • We continue
  • Assume the following is in effect
  • r a b
  • s d e f
  • t r c s

17
Operation
  • Given
  • lt r c s g h gt
  • We continue
  • Assume the following is in effect
  • r a b
  • s d e f
  • t r c s

18
Operation
  • Given
  • lt r c s g h gt
  • We continue
  • Assume the following is in effect
  • r a b
  • s d e f
  • t r c s

19
Operation
  • Given
  • lt r c s gt g h gt
  • We continue
  • Assume the following is in effect
  • r a b
  • s d e f
  • t r c s

20
Operation
  • Given
  • lt r c s gt g h gt
  • We continue
  • Assume the following is in effect
  • r a b
  • s d e f
  • t r c s

21
Operation
  • Given
  • lt t g h gt
  • We continue
  • Assume the following is in effect
  • r a b
  • s d e f
  • t r c s

22
Operation
  • Given
  • lt t g h gt
  • We continue
  • Assume the following is in effect
  • r a b
  • s d e f
  • t r c s

23
Operation
  • Given
  • lt t g h gt
  • If there is a rule for "t g h" successful
    parse
  • If not, syntax error

24
What kind of algorithm?
  • Stack based
  • Known as semantic stack or shift/reduce algorithm

25
Example
  • lt a b lt c d gt e lt f gt g gt
  • lt a b gt x e lt f gt g gt
  • lt y lt x e z g gt
  • lt y m gt
  • lt n gt

If this is the start symbol Successful Parse!
26
Stack
a, lt
  • lt a b lt c d gt e lt f gt g gt

We process the tokens adding the token and the
precedence operator on its left to the stack.
Encountering a gt indicates a handle has been
found and thus will initiate special proceesing

27
Stack
b, a, lt
  • lt a b lt c d gt e lt f gt g gt

28
Stack
c, lt b, a, lt
  • lt a b lt c d gt e lt f gt g gt

29
Stack
d, c, lt b, a, lt
  • lt a b lt c d gt e lt f gt g gt

30
Stack
d, c, lt b, a, lt
  • lt a b lt c d gt e lt f gt g gt
  • lt a b gt x e lt f gt g gt

Finding a "gt" between d and e, we trace back...
31
Stack
x, gt b, a, lt
  • lt a b lt c d gt e lt f gt g gt
  • lt a b gt x e lt f gt g gt

x is not really on stack because of the gt
32
Stack
x, lt y, lt
  • lt a b lt c d gt e lt f gt g gt
  • lt a b gt x e lt f gt g gt
  • lt y lt x e lt f gt g gt

We trace back to process the ab handle and then
x goes on stack
33
Stack
e, x, lt y, lt
  • lt a b lt c d gt e lt f gt g gt
  • lt a b gt x e lt f gt g gt
  • lt y lt x e lt f gt g gt

Continuing...
34
Stack
f, lt e, x, lt y, lt
  • lt a b lt c d gt e lt f gt g gt
  • lt a b gt x e lt f gt g gt
  • lt y lt x e lt f gt g gt
  • lt y lt x e z g gt

Converting f to z
35
Stack
g, f, lt e, x, lt y, lt
  • lt a b lt c d gt e lt f gt g gt
  • lt a b gt x e lt f gt g gt
  • lt y lt x e lt f gt g gt
  • lt y lt x e z g gt

36
Stack
, gt g, z, lt e, x, lt y, lt
  • lt a b lt c d gt e lt f gt g gt
  • lt a b gt x e lt f gt g gt
  • lt y lt x e lt f gt g gt
  • lt y lt x e z g gt

signifies end of input
37
Stack
, gt m, y, lt
  • lt a b lt c d gt e lt f gt g gt
  • lt a b gt x e lt f gt g gt
  • lt y lt x e lt f gt g gt
  • lt y lt x e z g gt
  • lt y m gt

Reducing
38
Stack
, gt n, lt
  • lt a b lt c d gt e lt f gt g gt
  • lt a b gt x e lt f gt g gt
  • lt y lt x e lt f gt g gt
  • lt y lt x e z g gt
  • lt y m gt
  • lt n gt

If n is our start symbol successful parse!
39
Shift/Reduce Parsing
  • x lt y
  • x y
  • x gt y
  • ?

Shift
Reduce
Error No relationship ? Syntax error
Not allowed More than one relationship
Actually lt would be okay, gt or gtlt is called
shift/reduce error
40
Shift/Reduce Parsing
  • Reduce-Reduce Error
  • When trying to match, parser finds two rules
    which match
  • No Right Hand Side Match
  • Syntax error

41
Simple Precedence
  • Only Possibilities
  • x lt y
  • x y
  • x gt y
  • No relationship exists
  • (No others allowed)

42
Bottom-Up Parsing
  • No issues regarding left-recursive versus
    right-recursive such as those found with Top-down
    parsing
  • Note There are grammars that will break a
    bottom-up parser.

43
Precedence
  • Consider our simple grammar...
  • Between symbols there must be ?
  • ltexprgt ltexprgt lttermgt lttermgt
  • lttermgt lttermgt ltfactorgt ltfactorgt
  • ltfactorgt '(' ltexprgt ')' num id

44
Precedence
  • Consider our simple grammar...
  • Between symbols there must be
  • ltexprgt ltexprgt lttermgt lttermgt
  • lttermgt lttermgt ltfactorgt ltfactorgt
  • ltfactorgt '(' ltexprgt ')' num id

45
Precedence
  • To determine "end points" we must look at
    multiple rules to see how they interact...

x
c
d

46
Precedence
  • To determine "end points" we must look at
    multiple rules to see how they interact...

x
c
d

To determine what goes here...
47
Precedence
  • To determine "end points" we must look at
    multiple rules to see how they interact...

x
c
d

We look here.
48
Precedence
  • ltexprgt ltexprgt lttermgt lttermgt
  • lttermgt lttermgt ltfactorgt ltfactorgt
  • ltfactorgt '(' ltexprgt ')' num id

lttermgt
ltfactorgt
lt ltfactorgt
lt (
ltexprgt )
lt (
ltexprgt )


49
Precedence Table
  • ltexprgt ltexprgt lttermgt lttermgt
  • lttermgt lttermgt ltfactorgt ltfactorgt
  • ltfactorgt '(' ltexprgt ')' num id

L R
ltexprgt
lttermgt
ltfactorgt

(
num

id
)
ltexprgt
lttermgt
ltfactorgt


(
)
num
id
50
Precedence Table
  • ltexprgt ltexprgt lttermgt lttermgt
  • lttermgt lttermgt ltfactorgt ltfactorgt
  • ltfactorgt '(' ltexprgt ')' num id

L R
ltexprgt
lttermgt
ltfactorgt

(
num

id
)
ltexprgt
lttermgt
ltfactorgt


(
)
num
id
51
Precedence Table
  • ltexprgt ltexprgt lttermgt lttermgt
  • lttermgt lttermgt ltfactorgt ltfactorgt
  • ltfactorgt '(' ltexprgt ')' num id

L R
ltexprgt
lttermgt
ltfactorgt

(
num

id
)


ltexprgt
gt

gt
lttermgt
gt
gt
gt
ltfactorgt
lt
lt
lt
lt


lt
lt
lt

lt
lt
lt
(
gt
gt
gt
)
gt
gt
gt
num
gt
gt
gt
id
52
Precedence Table
  • ltexprgt ltexprgt lttermgt lttermgt
  • lttermgt lttermgt ltfactorgt ltfactorgt
  • ltfactorgt '(' ltexprgt ')' num id

L R
ltexprgt
lttermgt
ltfactorgt

(
num

id
)


ltexprgt
gt

gt
lttermgt
gt
gt
gt
ltfactorgt
lt
lt
lt
lt


lt
lt
lt

lt
lt
lt
(
'(' ltexprgt... '(' lt '(' ltexprgt...
gt
gt
gt
)
gt
gt
gt
num
gt
gt
gt
id
53
Precedence Table
  • ltexprgt ltexprgt lttermgt lttermgt
  • lttermgt lttermgt ltfactorgt ltfactorgt
  • ltfactorgt '(' ltexprgt ')' num id

L R
ltexprgt
lttermgt
ltfactorgt

(
num

id
)


ltexprgt
gt

gt
lttermgt
gt
gt
gt
ltfactorgt
lt
lt
lt
lt
lt


lt
lt
lt

lt
lt
lt
(
lt
gt
gt
gt
)
gt
gt
gt
num
gt
gt
gt
id
54
Precedence Table
  • ltexprgt ltexprgt lttermgt lttermgt
  • lttermgt lttermgt ltfactorgt ltfactorgt
  • ltfactorgt '(' ltexprgt ')' num id

L R
ltexprgt
lttermgt
ltfactorgt

(
num

id
)


ltexprgt
gt

gt
lttermgt
'(' ltexprgt ')'
gt
gt
gt
ltfactorgt
lt
lt
lt
lt
lt


lt
lt
lt

lt
lt
lt
(
lt
gt
gt
gt
)
'(' lt ltexprgt
gt
gt
gt
num
gt
gt
gt
id
55
Resolving Ambiguity
  • lttermgt
  • lt lttermgt ltfactorgt
  • Solve by lookahead
  • lttermgt or )
  • lt lttermgt
  • Ambiguity can be resolved by increasing k in
    LR(k) but that's not the only way
  • We could rewrite grammar

56
Original Grammar
  • ltexprgt ltexprgt lttermgt lttermgtlttermgt
    lttermgt ltfactorgt ltfactorgtltfactorgt '('
    ltexprgt ')' num id

57
Sources of Ambiguity
  • ltexprgt ltexprgt lttermgt lttermgtlttermgt
    lttermgt ltfactorgt ltfactorgtltfactorgt '('
    ltexprgt ')' num id

58
Add 2 New Rules
  • ltexprgt ltexprgt lttermgt lttermgt
  • lttermgt lttermgt ltfactorgt ltfactorgt
  • ltfactorgt '(' ltexprgt ')' num id
  • ltegt ltexprgt
  • lttgt lttermgt

59
Modify
  • ltexprgt ltexprgt lttgt lttermgt
  • lttermgt lttermgt ltfactorgt ltfactorgt
  • ltfactorgt '(' ltegt ')' num id
  • ltegt ltexprgt
  • lttgt lttermgt

60
Original Grammar
ltexprgt ltexprgt lttermgt lttermgtlttermgt
lttermgt ltfactorgt ltfactorgtltfactorgt '('
ltexprgt ')' num id
Rewritten Grammar
  • ltexprgt ltexprgt lttgt lttermgt
  • lttermgt lttermgt ltfactorgt ltfactorgt
  • ltfactorgt '(' ltegt ')' num id
  • ltegt ltexprgt
  • lttgt lttermgt

61
Performance
  • Size of table is O(n2)
  • If we use operator precedence then it only uses
    terminals thus the table size is not affected by
    adding non-terminals

62
Question 1
  • a lt b c d gt e
  • a ? x e

What happens if there is no relationship here?
63
Question 2
  • Where do precedence relationships come from?
  • Make a table by hand
  • Write a program to make table
  • How such a program works or how to write it are
    topics beyond the scope of this course.

64
Example
65
  • lt num gt

1 2 3 Tokenized num num num
66
  • lt num gt
  • lt ltfactorgt gt

1 2 3 Tokenized num num num
67
  • lt num gt
  • lt ltfactorgt gt
  • lt lttermgt gt

1 2 3 Tokenized num num num
68
  • lt num gt
  • lt ltfactorgt gt
  • lt lttermgt gt
  • lt ltexprgt

1 2 3 Tokenized num num num
69
  • lt num gt
  • lt ltfactorgt gt
  • lt lttermgt gt
  • lt ltexprgt
  • lt ltexprgt lt num

1 2 3 Tokenized num num num
70
  • lt num gt
  • lt ltfactorgt gt
  • lt lttermgt gt
  • lt ltexprgt
  • lt ltexprgt lt num
  • lt ltexprgt lt num gt

1 2 3 Tokenized num num num
71
  • lt num gt
  • lt ltfactorgt gt
  • lt lttermgt gt
  • lt ltexprgt
  • lt ltexprgt lt num
  • lt ltexprgt lt num gt
  • lt ltexprgt lt ltfactorgt gt

1 2 3 Tokenized num num num
72
  • lt num gt
  • lt ltfactorgt gt
  • lt lttermgt gt
  • lt ltexprgt
  • lt ltexprgt lt num
  • lt ltexprgt lt num gt
  • lt ltexprgt lt ltfactorgt gt
  • lt ltexprgt lt lttermgt

1 2 3 Tokenized num num num
73
  • lt num gt
  • lt ltfactorgt gt
  • lt lttermgt gt
  • lt ltexprgt
  • lt ltexprgt lt num
  • lt ltexprgt lt num gt
  • lt ltexprgt lt ltfactorgt gt
  • lt ltexprgt lt lttermgt
  • lt ltexprgt lt lttermgt lt num

1 2 3 Tokenized num num num
74
  • lt num gt
  • lt ltfactorgt gt
  • lt lttermgt gt
  • lt ltexprgt
  • lt ltexprgt lt num
  • lt ltexprgt lt num gt
  • lt ltexprgt lt ltfactorgt gt
  • lt ltexprgt lt lttermgt
  • lt ltexprgt lt lttermgt lt num
  • lt ltexprgt lt lttermgt lt num gt

1 2 3 Tokenized num num num
75
  • lt num gt
  • lt ltfactorgt gt
  • lt lttermgt gt
  • lt ltexprgt
  • lt ltexprgt lt num
  • lt ltexprgt lt num gt
  • lt ltexprgt lt ltfactorgt gt
  • lt ltexprgt lt lttermgt
  • lt ltexprgt lt lttermgt lt num
  • lt ltexprgt lt lttermgt lt num gt
  • lt ltexprgt lt lttermgt ltfactorgt gt

1 2 3 Tokenized num num num
76
  • lt num gt
  • lt ltfactorgt gt
  • lt lttermgt gt
  • lt ltexprgt
  • lt ltexprgt lt num
  • lt ltexprgt lt num gt
  • lt ltexprgt lt ltfactorgt gt
  • lt ltexprgt lt lttermgt
  • lt ltexprgt lt lttermgt lt num
  • lt ltexprgt lt lttermgt lt num gt
  • lt ltexprgt lt lttermgt ltfactorgt gt
  • lt ltexprgt lttermgt gt

1 2 3 Tokenized num num num
77
  • lt num gt
  • lt ltfactorgt gt
  • lt lttermgt gt
  • lt ltexprgt
  • lt ltexprgt lt num
  • lt ltexprgt lt num gt
  • lt ltexprgt lt ltfactorgt gt
  • lt ltexprgt lt lttermgt
  • lt ltexprgt lt lttermgt lt num
  • lt ltexprgt lt lttermgt lt num gt
  • lt ltexprgt lt lttermgt ltfactorgt gt
  • lt ltexprgt lttermgt gt
  • lt ltexprgt gt

1 2 3 Tokenized num num num
78
Questions?
79
(No Transcript)
Write a Comment
User Comments (0)
About PowerShow.com