Title: CS 2130
1CS 2130
- Lecture 18
- Bottom-Up Parsing
- or
- Shift-Reduce Parsing
2Bottom 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
3Wirth-Weber Operators
4Operation
Keep in mind that the letters represent tokens
thus they can be operators, operands, special
symbols, etc
5Operation
- Given
- lt a b c d e f g h gt
Assumed
6Operation
- Given
- lt a b c d e f g h gt
- Parser moves through tokens comparing precedence
7Operation
- 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
8Operation
- 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
9Operation
- 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
10Operation
- 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
11Operation
- 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
12Operation
- 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
13Operation
- 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
14Operation
- 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
15Operation
- 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
16Operation
- 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
17Operation
- 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
18Operation
- 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
19Operation
- 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
20Operation
- 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
21Operation
- 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
22Operation
- 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
23Operation
- Given
- lt t g h gt
- If there is a rule for "t g h" successful
parse - If not, syntax error
24What kind of algorithm?
- Stack based
- Known as semantic stack or shift/reduce algorithm
25Example
- 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!
26Stack
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
27Stack
b, a, lt
- lt a b lt c d gt e lt f gt g gt
28Stack
c, lt b, a, lt
- lt a b lt c d gt e lt f gt g gt
29Stack
d, c, lt b, a, lt
- lt a b lt c d gt e lt f gt g gt
30Stack
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...
31Stack
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
32Stack
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
33Stack
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...
34Stack
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
35Stack
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
36Stack
, 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
37Stack
, 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
38Stack
, 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!
39Shift/Reduce Parsing
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
40Shift/Reduce Parsing
- Reduce-Reduce Error
- When trying to match, parser finds two rules
which match - No Right Hand Side Match
- Syntax error
41Simple Precedence
- Only Possibilities
- x lt y
- x y
- x gt y
- No relationship exists
- (No others allowed)
42Bottom-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.
43Precedence
- Consider our simple grammar...
- Between symbols there must be ?
- ltexprgt ltexprgt lttermgt lttermgt
- lttermgt lttermgt ltfactorgt ltfactorgt
- ltfactorgt '(' ltexprgt ')' num id
44Precedence
- Consider our simple grammar...
- Between symbols there must be
- ltexprgt ltexprgt lttermgt lttermgt
- lttermgt lttermgt ltfactorgt ltfactorgt
- ltfactorgt '(' ltexprgt ')' num id
45Precedence
- To determine "end points" we must look at
multiple rules to see how they interact...
x
c
d
46Precedence
- To determine "end points" we must look at
multiple rules to see how they interact...
x
c
d
To determine what goes here...
47Precedence
- To determine "end points" we must look at
multiple rules to see how they interact...
x
c
d
We look here.
48Precedence
- ltexprgt ltexprgt lttermgt lttermgt
- lttermgt lttermgt ltfactorgt ltfactorgt
- ltfactorgt '(' ltexprgt ')' num id
lttermgt
ltfactorgt
lt ltfactorgt
lt (
ltexprgt )
lt (
ltexprgt )
49Precedence 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
50Precedence 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
51Precedence 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
52Precedence 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
53Precedence 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
54Precedence 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
55Resolving 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
56Original Grammar
- ltexprgt ltexprgt lttermgt lttermgtlttermgt
lttermgt ltfactorgt ltfactorgtltfactorgt '('
ltexprgt ')' num id -
57Sources of Ambiguity
- ltexprgt ltexprgt lttermgt lttermgtlttermgt
lttermgt ltfactorgt ltfactorgtltfactorgt '('
ltexprgt ')' num id -
58Add 2 New Rules
- ltexprgt ltexprgt lttermgt lttermgt
- lttermgt lttermgt ltfactorgt ltfactorgt
- ltfactorgt '(' ltexprgt ')' num id
- ltegt ltexprgt
- lttgt lttermgt
-
59Modify
- ltexprgt ltexprgt lttgt lttermgt
- lttermgt lttermgt ltfactorgt ltfactorgt
- ltfactorgt '(' ltegt ')' num id
- ltegt ltexprgt
- lttgt lttermgt
-
60Original 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
61Performance
- 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
62Question 1
What happens if there is no relationship here?
63Question 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.
64Example
651 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
78Questions?
79(No Transcript)