CS 2130 - PowerPoint PPT Presentation

1 / 143
About This Presentation
Title:

CS 2130

Description:

Bottom-up Parsing. 1. Leaves root node. 2. Concrete abstract. 3. Uses grammar right left ... Bottom Up Parsing. Two Things must be understood: ... – PowerPoint PPT presentation

Number of Views:47
Avg rating:3.0/5.0
Slides: 144
Provided by: ble87
Category:
Tags: bottom

less

Transcript and Presenter's Notes

Title: CS 2130


1
CS 2130
  • Presentation 20
  • Bottom-Up Parsing
  • or
  • Shift-Reduce Parsing

2
Parsing
  • Parsing -- Syntax/Semantic Analysis
  • Top-down Parsing
  • 1. Root node ? leaves
  • 2. Abstract ? concrete
  • 3. Uses grammar left ? right
  • 4. Works by "guessing"
  • Bottom-up Parsing
  • 1. Leaves ? root node
  • 2. Concrete ? abstract
  • 3. Uses grammar right ? left
  • 4. Works by "pattern matching"

3
Introduction
  • Top down parsing
  • Scan across the string to be parsed
  • Attempt to find patterns that match the right
    hand side of a rule
  • Reduce them to the left hand side of the rule
  • If the eventual result is reduction to the start
    symbol then parse is successful

4
Imagine...
  • We are parsing
  • 1 2 3
  • or
  • num num num
  • We need some way to make sure that we don't turn
    the num num into ltexprgt lttermgt and reduce it
    to ltexprgt
  • Can num num be reduced to ltexprgt ?
  • Why is it a problem?

5
Problem...
  • We cannot reduce
  • ltexprgt num
  • What we need is a way of recognizing that we must
    reduce this first
  • num num num

6
Recall our expression grammar
  • ltexprgt ltexprgt lttermgt lttermgt
  • lttermgt lttermgt ltfactorgt ltfactorgt
  • ltfactorgt '(' ltexprgt ')' num id
  • It would suggest that what follows a must be a
    term.
  • It would also suggest that if a num is followed
    by a then we will somehow need to find a factor
    to perform
  • lttermgt lttermgt ltfactorgt

7
Bottom Up Parsing
  • Bottom up parsing tries to group tokens into
    things it can reduce (based on a rule in the
    grammar) in the correct sequence
  • This group of symbols is known as a handle.
  • Handles are indicated using special symbols known
    as Wirth-Weber operators
  • These symbols function like parentheses which can
    be used to indicate precedence
  • 1 (2 3)
  • We will determine where to put these symbols by
    examining the grammar and developing additional
    information to assist us

8
Wirth-Weber Operators
x lt y y has higher precedence than x (We
expect y will be involved in a reduction before
x) x y x and y have equal precedence (We
expect x and y will be involved in a reduction
together) x gt y x has higher precedence than
y (We expect x will be involved in a reduction
before y)
9
Bottom Up Parsing
  • Two Things must be understood
  • Given the ability to determine precedence between
    symbols how can we use this to parse a string?
  • How do we determine this precedence between
    symbols/tokens?
  • We deliberately choose to explain in this order
    and we'll use a very simple grammar to explain

10
Let's consider a grammar
  • ltdeclgt lttypegt ltdec_listgt
  • ltdec_listgt id ltdec_listgt, id
  • lttypegt char int float
  • Suppose we wish to parse
  • int i, j, k
  • The scanner will deliver to us
  • int id , id , id

11
Bottom Up Parsing
  • int id , id , id

12
Bottom Up Parsing
  • lt int id , id , id
  • We can assume that the string has a leading
    "less- than" (i.e. lt) precedence operator

13
Bottom Up Parsing
  • lt int gt id , id , id
  • We move from left to right (and in reality, we
    would normally proceed by asking a lexical
    scanner for the next token)
  • As we get to each token or symbol, we get its
    precedence from a precedence table that we'll
    present later

14
Bottom Up Parsing
  • lt lttypegt id , id , id

15
Bottom Up Parsing
  • lt lttypegt lt id , id , id
  • We continue in this fashion as long as we place
    lt and operators

16
Bottom Up Parsing
  • lt lttypegt lt id gt , id , id
  • When we place a gt operator we have found a
    handle or something that we should be able to
    reduce
  • We examine the rules of the grammer to see if
    there is a rule to match this handle

17
Bottom Up Parsing
  • lt lttypegt lt id gt , id , id
  • We find
  • ltdec_listgt id
  • Note If no rule is found we have a parse error

18
Bottom Up Parsing
  • lt lttypegt ltdec_listgt , id , id
  • Note that we have removed the entire handle and
    replaced it with the appropriate symbol from the
    grammar. We "backup" to examine the relationship
    between lttypegt and ltdec_listgt

19
Bottom Up Parsing
  • lt lttypegt lt ltdec_listgt , id , id

20
Bottom Up Parsing
  • lt lttypegt lt ltdec_listgt , id , id
  • We continue

21
Bottom Up Parsing
  • lt lttypegt lt ltdec_listgt , id , id

22
Bottom Up Parsing
  • lt lttypegt lt ltdec_listgt , id gt , id
  • We continue, again, until we find a handle
  • We can reduce this using the rule
  • ltdec_listgt ltdec_listgt, id

23
Bottom Up Parsing
  • lt lttypegt ltdec_listgt , id
  • After reduction this using the rule
  • ltdec_listgt ltdec_listgt, id

24
Bottom Up Parsing
  • lt lttypegt lt ltdec_listgt , id

25
Bottom Up Parsing
  • lt lttypegt lt ltdec_listgt , id

26
Bottom Up Parsing
  • lt lttypegt lt ltdec_listgt , id

27
Bottom Up Parsing
  • lt lttypegt lt ltdec_listgt , id gt
  • Again, finding a handle lets us search for a rule
    and if found perform the reduction

28
Bottom Up Parsing
  • lt lttypegt ltdec_listgt

29
Bottom Up Parsing
  • lt lttypegt ltdec_listgt

30
Bottom Up Parsing
  • lt lttypegt ltdec_listgt

31
Bottom Up Parsing
  • lt lttypegt ltdec_listgt gt
  • A greater than precedence symbol is assumed after
    the last symbol in the input.

32
Bottom Up Parsing
  • ltdeclgt

33
Bottom Up Parsing
  • ltdeclgt
  • Since ltdeclgt is out start symbol and we have
    nothing left over
  • Successful Parse!

34
Simple Precedence
  • The technique we have been using is known as
    Bottom-Up Parsing or Shift-Reduce Parsing
  • The action we take during operation is based on
    the precedence relationship found
  • x lt y
  • x y
  • x gt y
  • What happens if there is no relationship in the
    table?

Shift
Reduce
35
Bottom Up Parsing
  • What kind of algorithm?
  • Stack based
  • Known as semantic stack or shift/reduce algorithm
  • We won't code this algorithm but understanding
    this parsing technique will make some concepts
    found in yacc clearer

36
Example
Our stream of tokens int id , id , id
37
Example
Stack
Our stream of tokens int id , id , id
38
Example
Stack
Our stream of tokens int id , id , id
  • Color Commentary
  • Welcome to Monday Night Parsing

39
Example
Stack
Our stream of tokens int id , id , id
lt int
We will place the Wirth-Weber operator and
following token on the stack. Encountering the
end of a handle gt will initiate additional
processing
40
Example
Stack
Our stream of tokens int id , id , id
lt int
We get the next token (i.e. "id" ) and look up
the precedence relationship with the item on top
of the stack
Precedence Table Lookup
id
gt
int
41
Example
Stack
Our stream of tokens int id , id , id
lt int
Finding a gt, we search down the stack looking
for a lt and then reduce the handle we've found
Precedence Table Lookup
gt
42
Example
Stack
Our stream of tokens int id , id , id
lt int
Finding a gt, we search down the stack looking
for a lt and then reduce the handle we've found
using a rule from the grammar (NB No rule
implies parse error) lttypegt int
43
Example
Stack
Our stream of tokens int id , id , id
lt lttypegt
We put the new symbol (lttypegt) on the stack since
it is the only thing on the stack at this point.
If there were already symbols on the stack, we
would compare the newly developed symbol with the
top item on the stack using the precedence table
44
Example
Stack
Our stream of tokens int id , id , id
lt lttypegt
We now compare the lttypegt with the id
Precedence Table Lookup
45
Example
Stack
Our stream of tokens int id , id , id
lt id lt lttypegt
And place both the precedence operator and the id
token on the stack
46
Example
Stack
Our stream of tokens int id , id , id
lt id lt lttypegt
And then id with ','
Precedence Table Lookup
,
gt
id
47
Example
Stack
Our stream of tokens int id , id , id
lt id lt lttypegt
Finding a gt we again search back down the stack
to find the beginning of the handle and reduce it
appropriately ltdec_listgt id
48
Example
Stack
Our stream of tokens int id , id , id
lt lttypegt
We now compare the ltdec_listgt with lttypegt
Precedence Table Lookup
49
Example
Stack
Our stream of tokens int id , id , id
lt ltdec_listgt lt lttypegt
And push it onto the stack
50
Example
Stack
Our stream of tokens int id , id , id
lt ltdec_listgt lt lttypegt
Now the ltdec_listgt with the next token ','
Precedence Table Lookup
51
Example
Stack
Our stream of tokens int id , id , id
, lt ltdec_listgt lt lttypegt
Resulting in pushing the operator and ',' onto
the stack
Precedence Table Lookup
52
Example
Stack
Our stream of tokens int id , id , id
, lt ltdec_listgt lt lttypegt
Continuing by comparing the ',' with the next
token id
Precedence Table Lookup
id

,
53
Example
Stack
Our stream of tokens int id , id , id
id , lt ltdec_listgt lt lttypegt
Continuing by comparing the ',' with the next
token id
Precedence Table Lookup
id

,
54
Example
Stack
Our stream of tokens int id , id , id
id , lt ltdec_listgt lt lttypegt
Now we compare the id with the next token ','
Precedence Table Lookup
,
gt
id
55
Example
Stack
Our stream of tokens int id , id , id
id , lt ltdec_listgt lt lttypegt
Finding the gt causes us to search through the
stack for the matching lt processing the symbols
according to the appropriate rule ltdec_listgt
ltdec_listgt, id
56
Example
Stack
Our stream of tokens int id , id , id
lt ltdec_listgt lt lttypegt
We will now process the remaining ',' and id in
the same fashion to yield...
57
Example
Stack
Our stream of tokens int id , id , id
id , lt ltdec_listgt lt lttypegt
A stack like this
58
Example
Stack
Our stream of tokens int id , id , id
id , lt ltdec_listgt lt lttypegt
Once more we consult the mysterious all knowing
precedence table
Precedence Table Lookup

gt
id
59
Example
Stack
Our stream of tokens int id , id , id
id , lt ltdec_listgt lt lttypegt
Finding the gt causes us to search through the
stack for the matching lt processing the symbols
according to the appropriate rule ltdec_listgt
ltdec_listgt, id
60
Example
Stack
Our stream of tokens int id , id , id
ltdec_listgt lt lttypegt
Once more the precedence table yields
Precedence Table Lookup
ltdec_listgt

lttypegt
61
Example
Stack
Our stream of tokens int id , id , id
ltdec_listgt lt lttypegt
Now we consider the final token ''
Precedence Table Lookup


ltdec_listgt
62
Example
Stack
Our stream of tokens int id , id , id
ltdec_listgt lt lttypegt
Reaching the end of input we assume a handle has
been found (we add a gt) allowing us to find a
match with this rule ltdeclgt lttypegt
ltdec_listgt
63
Result Successful Parse!!!
64
Questions?
65
Constructing the Precedence Table
  • Being a table which when given two successive
    symbols will return to us the correct
    interstitial Wirth-Weber Operator

66
Precedence Table
  • May be constructed by examination
  • Examine samples of legal (and illegal) sentences
  • Develop relationships by what is necessary to
    make the parsing algorithm work
  • May also be constructed by computer program
  • Beyond the scope of this course
  • We will construct a precedence table by
    examination

67
PrecedenceTable
  • ltdeclgt lttypegt ltdec_listgt
  • ltdec_listgt id ltdec_listgt, id
  • lttypegt char int float

ltdeclgt
lttypegt
ltdec_listgt
id
,
char int float

ltdeclgt
lttypegt
ltdec_listgt
id
,
char int float

68
PrecedenceTable
  • ltdeclgt lttypegt ltdec_listgt
  • ltdec_listgt id ltdec_listgt, id
  • lttypegt char int float

ltdeclgt
lttypegt
ltdec_listgt
id
,
char int float

Consider all the possible illegal
combinations int int id id ,, id , id, id
int int id id id etc.
ltdeclgt
lttypegt
ltdec_listgt
id
,
char int float

69
PrecedenceTable
  • ltdeclgt lttypegt ltdec_listgt
  • ltdec_listgt id ltdec_listgt, id
  • lttypegt char int float

ltdeclgt
lttypegt
ltdec_listgt
id
,
char int float

Right Left
ltdeclgt
lttypegt
ltdec_listgt
id
,
char int float

70
Precedence Table
  • Between the symbols of any rule in the grammar,
    what must appear?
  • ltdeclgt lttypegt ltdec_listgt
  • ltdec_listgt id ltdec_listgt , id
  • lttypegt char int float

71
Precedence Table
  • Between the symbols of any rule in the grammar,
    what must appear?
  • ltdeclgt lttypegt ltdec_listgt
  • ltdec_listgt id ltdec_listgt , id
  • lttypegt char int float

72
PrecedenceTable
  • ltdeclgt lttypegt ltdec_listgt
  • ltdec_listgt id ltdec_listgt, id
  • lttypegt char int float

ltdeclgt
lttypegt
ltdec_listgt
id
,
char int float

Right Left
ltdeclgt
lttypegt

ltdec_listgt


id
,

char int float

73
Precedence Table
  • Then consider typical legal sentences
  • int id, id, id
  • The algorithm will require us to "find" the int
    and convert it to lttypegt
  • lt int gt id
  • Thus, between int and id the relationship must be
  • gt

74
PrecedenceTable
  • ltdeclgt lttypegt ltdec_listgt
  • ltdec_listgt id ltdec_listgt, id
  • lttypegt char int float

ltdeclgt
lttypegt
ltdec_listgt
id
,
char int float

Right Left
ltdeclgt
lttypegt
ltdec_listgt


id
,

char int float
gt

75
Consider
76
the details...
77
PrecedenceTable
  • ltdeclgt lttypegt ltdec_listgt
  • ltdec_listgt id ltdec_listgt, id
  • lttypegt char int float

ltdeclgt
lttypegt
ltdec_listgt
id
,
char int float

Right Left
ltdeclgt
lttypegt
lt
ltdec_listgt


id
gt
gt
,

char int float
gt

78
PrecedenceTable
  • ltdeclgt lttypegt ltdec_listgt
  • ltdec_listgt id ltdec_listgt, id
  • lttypegt char int float

ltdeclgt
lttypegt
ltdec_listgt
id
,
char int float

Right Left
ltdeclgt
lttypegt
lt
ltdec_listgt


id
gt
gt
,

char int float
gt

79
Consider
  • P. T. Barnum

80
You can fool some of the people all the time, and
all of the people someof the time but you can't
fool all of the people all the time
81
(No Transcript)
82
ltdeclgt lttypegt ltdec_listgt ltdec_listgt id
ltdec_listgt, id lttypegt char int float
Precedence Table Lookup
ltdec_listgt

lttypegt
83
Graphically
ltdeclgt
ltdecl_listgt
ltdecl_listgt
lt
lttypegt
ltdecl_listgt
int
,
,
id
id
id

84
PrecedenceTable
  • ltdeclgt lttypegt ltdec_listgt
  • ltdec_listgt id ltdec_listgt, id
  • lttypegt char int float

ltdeclgt
lttypegt
ltdec_listgt
id
,
char int float

Right Left
ltdeclgt
lttypegt
lt,
lt
ltdec_listgt


id
gt
gt
,

char int float
gt

85
Houston, we have a problem!
  • The occurence of multiple precedence operators in
    the same cell is referred to as complex
    precedence and is problematic for our algorithmic
    technique.

lt,
86
More than one relationship!
  • Gadzooks!
  • Actually, we could deal with lt using
    lookahead
  • (we'll see that in a moment)
  • However rules that allowed gt or gtlt would be
    known as a shift reduce error
  • Speaking of errors, finding two rules that match
    is known as a reduce-reduce error
  • Not finding a rule that matches is a syntax error

87
Another Example
88
Recall our expression grammar
  • ltexprgt ltexprgt lttermgt lttermgtlttermgt
    lttermgt ltfactorgt ltfactorgtltfactorgt '('
    ltexprgt ')' num id

89
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
90
Some things are impossible
  • ltexprgt ltexprgt
  • ()
  • )(

91
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
92
We know
  • From our previous example we note that certain
    items must be reduced immediately (e.g. int,
    float or char immediately becomes lttypegt
  • In a similar fashion we have
  • ltfactorgt num id
  • So, anything followed by a num or an id will have
    lt and a num or an id followed by anything will
    have gt

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

L R
ltexprgt
lttermgt
ltfactorgt

(
num

id
)
ltexprgt

lttermgt




ltfactorgt


lt
lt



lt
lt


lt
lt
(



)
gt
gt
gt
num
gt
gt
gt
id
94
Precedence
  • Between symbols there must be ?
  • ltexprgt ltexprgt lttermgt lttermgt
  • lttermgt lttermgt ltfactorgt ltfactorgt
  • ltfactorgt '(' ltexprgt ')' num id

95
Precedence
  • Between symbols there must be
  • ltexprgt ltexprgt lttermgt lttermgt
  • lttermgt lttermgt ltfactorgt ltfactorgt
  • ltfactorgt '(' ltexprgt ')' num id

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

L R
ltexprgt
lttermgt
ltfactorgt

(
num

id
)


ltexprgt



lttermgt



ltfactorgt



lt
lt



lt
lt


lt
lt
(



)
gt
gt
gt
num
gt
gt
gt
id
97
Precedence
  • To determine "end points" we must look at
    multiple rules to see how they interact...

ltfactorgt
(
ltexprgt

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

ltfactorgt
(
ltexprgt

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

ltfactorgt
(
ltexprgt

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

We look here.
ltfactorgt
(
ltexprgt

)
101
Precedence
  • ltexprgt ltexprgt lttermgt lttermgt
  • lttermgt lttermgt ltfactorgt ltfactorgt
  • ltfactorgt '(' ltexprgt ')' num id

What is the relationship between and ( ?
? (
102
Precedence
  • ltexprgt ltexprgt lttermgt lttermgt
  • lttermgt lttermgt ltfactorgt ltfactorgt
  • ltfactorgt '(' ltexprgt ')' num id

What is the relationship between and ( If we
have parentheses it must be this form
? (
ltexprgt )

103
Precedence
  • ltexprgt ltexprgt lttermgt lttermgt
  • lttermgt lttermgt ltfactorgt ltfactorgt
  • ltfactorgt '(' ltexprgt ')' num id

ltfactorgt
We go up the parse tree. Since ( ltexprgt ) will
be a factor and a factor will need to be reduced
as part of lttermgt ltfactorgt we conclude that we
will need to reduce the ( ltexprgt ) first
lt (
ltexprgt )

104
Precedence
  • ltexprgt ltexprgt lttermgt lttermgt
  • lttermgt lttermgt ltfactorgt ltfactorgt
  • ltfactorgt '(' ltexprgt ')' num id

What is the relationship between and (
? (
105
Precedence
  • ltexprgt ltexprgt lttermgt lttermgt
  • lttermgt lttermgt ltfactorgt ltfactorgt
  • ltfactorgt '(' ltexprgt ')' num id

Again the grammar reveals that ( must come from
( ltexprgt )
? (
ltexprgt )

106
Precedence
  • ltexprgt ltexprgt lttermgt lttermgt
  • lttermgt lttermgt ltfactorgt ltfactorgt
  • ltfactorgt '(' ltexprgt ')' num id

lttermgt
We examine the parse tree noting that a can
only be followed by a lttermgt
lt ltfactorgt
lt (
ltexprgt )

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

L R
ltexprgt
lttermgt
ltfactorgt

(
num

id
)


ltexprgt



lttermgt



ltfactorgt

lt
lt

lt
lt

lt
lt

lt

lt
lt
(



)
gt
gt
gt
num
gt
gt
gt
id
108
Continuing to analyze in this way...
109
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
110
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
111
Now for the complex part
  • Consider ( followed by ltexprgt
  • ( ltexprgt
  • Is it
  • ( ltexprgt )
  • or
  • ( ltexprgt lt

ltexprgt ltexprgt lttermgt lttermgt lttermgt
lttermgt ltfactorgt ltfactorgt ltfactorgt '('
ltexprgt ')' num id
112
Or
  • Consider followed by lttermgt
  • lttermgt
  • Is it
  • lttermgt
  • lttermgt )
  • lttermgt lt

ltexprgt ltexprgt lttermgt lttermgt lttermgt
lttermgt ltfactorgt ltfactorgt ltfactorgt '('
ltexprgt ')' num id
113
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
114
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
115
Resolving Ambiguity
  • lttermgt
  • lt lttermgt ltfactorgt
  • Solve by lookahead
  • lttermgt or )
  • lt lttermgt
  • Ambiguity can be resolved by increasing k in LR(k)

116
Recall our declaration grammar
Use this if the next token is ','
ltdeclgt lttypegt ltdec_listgt ltdec_listgt id
ltdec_listgt, id lttypegt char int float
Precedence Table Lookup
Use this if the next token is ''
ltdec_listgt

lttypegt
117
Increasing k in LR(k) is not the only way
  • We could rewrite the grammar!

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

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

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

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

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

123
Questions?
124
Note
  • 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.

125
So
  • It appears that this technique is quite simple
  • We
  • Construct a grammar
  • Examine it to produce a precedence table
  • Rewrite the grammar if we have complex precedence
  • Write a program to execute our stack based
    algorithm
  • Not so fast!
  • In the real world there is still an issue to deal
    with
  • Size

126
Performance
  • Size of table is O(n2)
  • For a "real" language this can be a problem
  • One possibility Use operator precedence
  • Only uses terminals
  • Thus the table size is not affected by adding
    non-terminals
  • We will not go into details of Operator
    Precedence Tables
  • You should be aware that they exist

127
Question
  • 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.

128
Example
129
  • lt num gt

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

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

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

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

1 2 3 Tokenized num num num
134
  • 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
135
  • 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
136
  • 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
137
  • 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
138
  • 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
139
  • 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
140
  • 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
141
  • 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
142
Questions?
143
(No Transcript)
Write a Comment
User Comments (0)
About PowerShow.com