Title: CS 2130
1CS 2130
- Presentation 20
- Bottom-Up Parsing
- or
- Shift-Reduce Parsing
2Parsing
- 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"
3Introduction
- 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
4Imagine...
- 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?
5Problem...
- We cannot reduce
- ltexprgt num
-
- What we need is a way of recognizing that we must
reduce this first - num num num
6Recall 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
-
7Bottom 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
8Wirth-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)
9Bottom 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
10Let'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
11Bottom Up Parsing
12Bottom Up Parsing
- lt int id , id , id
- We can assume that the string has a leading
"less- than" (i.e. lt) precedence operator
13Bottom 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
14Bottom Up Parsing
15Bottom Up Parsing
- lt lttypegt lt id , id , id
- We continue in this fashion as long as we place
lt and operators
16Bottom 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
17Bottom 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
18Bottom 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
19Bottom Up Parsing
- lt lttypegt lt ltdec_listgt , id , id
20Bottom Up Parsing
- lt lttypegt lt ltdec_listgt , id , id
- We continue
21Bottom Up Parsing
- lt lttypegt lt ltdec_listgt , id , id
22Bottom 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
23Bottom Up Parsing
- lt lttypegt ltdec_listgt , id
- After reduction this using the rule
- ltdec_listgt ltdec_listgt, id
24Bottom Up Parsing
- lt lttypegt lt ltdec_listgt , id
25Bottom Up Parsing
- lt lttypegt lt ltdec_listgt , id
26Bottom Up Parsing
- lt lttypegt lt ltdec_listgt , id
27Bottom 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
28Bottom Up Parsing
29Bottom Up Parsing
30Bottom Up Parsing
31Bottom Up Parsing
- lt lttypegt ltdec_listgt gt
- A greater than precedence symbol is assumed after
the last symbol in the input.
32Bottom Up Parsing
33Bottom Up Parsing
- ltdeclgt
- Since ltdeclgt is out start symbol and we have
nothing left over - Successful Parse!
34Simple 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
35Bottom 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
36Example
Our stream of tokens int id , id , id
37Example
Stack
Our stream of tokens int id , id , id
38Example
Stack
Our stream of tokens int id , id , id
- Color Commentary
- Welcome to Monday Night Parsing
39Example
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
40Example
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
41Example
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
42Example
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
43Example
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
44Example
Stack
Our stream of tokens int id , id , id
lt lttypegt
We now compare the lttypegt with the id
Precedence Table Lookup
45Example
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
46Example
Stack
Our stream of tokens int id , id , id
lt id lt lttypegt
And then id with ','
Precedence Table Lookup
,
gt
id
47Example
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
48Example
Stack
Our stream of tokens int id , id , id
lt lttypegt
We now compare the ltdec_listgt with lttypegt
Precedence Table Lookup
49Example
Stack
Our stream of tokens int id , id , id
lt ltdec_listgt lt lttypegt
And push it onto the stack
50Example
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
51Example
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
52Example
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
,
53Example
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
,
54Example
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
55Example
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
56Example
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...
57Example
Stack
Our stream of tokens int id , id , id
id , lt ltdec_listgt lt lttypegt
A stack like this
58Example
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
59Example
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
60Example
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
61Example
Stack
Our stream of tokens int id , id , id
ltdec_listgt lt lttypegt
Now we consider the final token ''
Precedence Table Lookup
ltdec_listgt
62Example
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
63Result Successful Parse!!!
64Questions?
65Constructing the Precedence Table
- Being a table which when given two successive
symbols will return to us the correct
interstitial Wirth-Weber Operator
66Precedence 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
67PrecedenceTable
- 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
68PrecedenceTable
- 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
69PrecedenceTable
- 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
70Precedence 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
71Precedence 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
72PrecedenceTable
- 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
73Precedence 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
74PrecedenceTable
- 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
75Consider
76the details...
77PrecedenceTable
- 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
78PrecedenceTable
- 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
79Consider
80You 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)
82ltdeclgt lttypegt ltdec_listgt ltdec_listgt id
ltdec_listgt, id lttypegt char int float
Precedence Table Lookup
ltdec_listgt
lttypegt
83Graphically
ltdeclgt
ltdecl_listgt
ltdecl_listgt
lt
lttypegt
ltdecl_listgt
int
,
,
id
id
id
84PrecedenceTable
- 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
85Houston, 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,
86More 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
87Another Example
88Recall our expression grammar
- ltexprgt ltexprgt lttermgt lttermgtlttermgt
lttermgt ltfactorgt ltfactorgtltfactorgt '('
ltexprgt ')' num id -
89Precedence 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
90Some things are impossible
91Precedence 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
92We 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
93Precedence 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
94Precedence
- Between symbols there must be ?
- ltexprgt ltexprgt lttermgt lttermgt
- lttermgt lttermgt ltfactorgt ltfactorgt
- ltfactorgt '(' ltexprgt ')' num id
95Precedence
- Between symbols there must be
- ltexprgt ltexprgt lttermgt lttermgt
- lttermgt lttermgt ltfactorgt ltfactorgt
- ltfactorgt '(' ltexprgt ')' num id
-
96Precedence 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
97Precedence
- To determine "end points" we must look at
multiple rules to see how they interact...
ltfactorgt
(
ltexprgt
)
98Precedence
- To determine "end points" we must look at
multiple rules to see how they interact...
ltfactorgt
(
ltexprgt
)
To determine what goes here...
99Precedence
- To determine "end points" we must look at
multiple rules to see how they interact...
ltfactorgt
(
ltexprgt
)
To determine what goes here...
100Precedence
- To determine "end points" we must look at
multiple rules to see how they interact...
We look here.
ltfactorgt
(
ltexprgt
)
101Precedence
- ltexprgt ltexprgt lttermgt lttermgt
- lttermgt lttermgt ltfactorgt ltfactorgt
- ltfactorgt '(' ltexprgt ')' num id
What is the relationship between and ( ?
? (
102Precedence
- 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 )
103Precedence
- 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 )
104Precedence
- ltexprgt ltexprgt lttermgt lttermgt
- lttermgt lttermgt ltfactorgt ltfactorgt
- ltfactorgt '(' ltexprgt ')' num id
What is the relationship between and (
? (
105Precedence
- ltexprgt ltexprgt lttermgt lttermgt
- lttermgt lttermgt ltfactorgt ltfactorgt
- ltfactorgt '(' ltexprgt ')' num id
Again the grammar reveals that ( must come from
( ltexprgt )
? (
ltexprgt )
106Precedence
- 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 )
107Precedence 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
108Continuing to analyze in this way...
109Precedence 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
110Precedence 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
111Now 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
112Or
- Consider followed by lttermgt
- lttermgt
- Is it
- lttermgt
- lttermgt )
- lttermgt lt
ltexprgt ltexprgt lttermgt lttermgt lttermgt
lttermgt ltfactorgt ltfactorgt ltfactorgt '('
ltexprgt ')' num id
113Precedence 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
114Precedence 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
115Resolving Ambiguity
- lttermgt
- lt lttermgt ltfactorgt
- Solve by lookahead
- lttermgt or )
- lt lttermgt
- Ambiguity can be resolved by increasing k in LR(k)
116Recall 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
117Increasing k in LR(k) is not the only way
- We could rewrite the grammar!
118Original Grammar
- ltexprgt ltexprgt lttermgt lttermgtlttermgt
lttermgt ltfactorgt ltfactorgtltfactorgt '('
ltexprgt ')' num id -
119Sources of Ambiguity
- ltexprgt ltexprgt lttermgt lttermgtlttermgt
lttermgt ltfactorgt ltfactorgtltfactorgt '('
ltexprgt ')' num id -
120Add 2 New Rules
- ltexprgt ltexprgt lttermgt lttermgt
- lttermgt lttermgt ltfactorgt ltfactorgt
- ltfactorgt '(' ltexprgt ')' num id
- ltegt ltexprgt
- lttgt lttermgt
-
121Modify
- ltexprgt ltexprgt lttgt lttgt
- lttermgt lttermgt ltfactorgt ltfactorgt
- ltfactorgt '(' ltegt ')' num id
- ltegt ltexprgt
- lttgt lttermgt
-
122Original 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
123Questions?
124Note
- 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.
125So
- 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
126Performance
- 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
127Question
- 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.
128Example
1291 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
142Questions?
143(No Transcript)