Top-Down Parsing and Intro to Bottom-Up ParsingBottom-Up Parsing •Bottom-up parsing is more...
Transcript of Top-Down Parsing and Intro to Bottom-Up ParsingBottom-Up Parsing •Bottom-up parsing is more...
![Page 1: Top-Down Parsing and Intro to Bottom-Up ParsingBottom-Up Parsing •Bottom-up parsing is more general than top-down parsing –And just as efficient –Builds on ideas in top-down](https://reader034.fdocuments.us/reader034/viewer/2022042606/5fb0e18455cbcf1c29211b7c/html5/thumbnails/1.jpg)
1
Top-Down Parsingand
Intro to Bottom-Up Parsing
Lecture 7
Instructor: Fredrik KjolstadSlides based on slides designed by Prof. Alex Aiken
![Page 2: Top-Down Parsing and Intro to Bottom-Up ParsingBottom-Up Parsing •Bottom-up parsing is more general than top-down parsing –And just as efficient –Builds on ideas in top-down](https://reader034.fdocuments.us/reader034/viewer/2022042606/5fb0e18455cbcf1c29211b7c/html5/thumbnails/2.jpg)
2
Predictive Parsers
• Like recursive-descent but parser can “predict” which production to use– By looking at the next few tokens– No backtracking
• Predictive parsers accept LL(k) grammars– L means “left-to-right” scan of input– L means “leftmost derivation”– k means “predict based on k tokens of lookahead”– In practice, LL(1) is used
![Page 3: Top-Down Parsing and Intro to Bottom-Up ParsingBottom-Up Parsing •Bottom-up parsing is more general than top-down parsing –And just as efficient –Builds on ideas in top-down](https://reader034.fdocuments.us/reader034/viewer/2022042606/5fb0e18455cbcf1c29211b7c/html5/thumbnails/3.jpg)
3
LL(1) vs. Recursive Descent
• In recursive-descent, – At each step, many choices of production to use– Backtracking used to undo bad choices
• In LL(1), – At each step, only one choice of production– That is
• When a non-terminal A is leftmost in a derivation• The next input symbol is t• There is a unique production A ® a to use
– Or no production to use (an error state)
• LL(1) is a recursive descent variant without backtracking
![Page 4: Top-Down Parsing and Intro to Bottom-Up ParsingBottom-Up Parsing •Bottom-up parsing is more general than top-down parsing –And just as efficient –Builds on ideas in top-down](https://reader034.fdocuments.us/reader034/viewer/2022042606/5fb0e18455cbcf1c29211b7c/html5/thumbnails/4.jpg)
4
Predictive Parsing and Left Factoring
• Recall the grammarE ® T + E | TT ® int | int * T | ( E )
• Hard to predict because– For T two productions start with int– For E it is not clear how to predict
• We need to left-factor the grammar
![Page 5: Top-Down Parsing and Intro to Bottom-Up ParsingBottom-Up Parsing •Bottom-up parsing is more general than top-down parsing –And just as efficient –Builds on ideas in top-down](https://reader034.fdocuments.us/reader034/viewer/2022042606/5fb0e18455cbcf1c29211b7c/html5/thumbnails/5.jpg)
5
Left-Factoring Example
• Recall the grammarE ® T + E | TT ® int | int * T | ( E )
• Factor out common prefixes of productionsE ® T XX ® + E | eT ® int Y | ( E )Y ® * T | e
![Page 6: Top-Down Parsing and Intro to Bottom-Up ParsingBottom-Up Parsing •Bottom-up parsing is more general than top-down parsing –And just as efficient –Builds on ideas in top-down](https://reader034.fdocuments.us/reader034/viewer/2022042606/5fb0e18455cbcf1c29211b7c/html5/thumbnails/6.jpg)
6
LL(1) Parsing Table Example
• Left-factored grammarE ® T X X ® + E | eT ® ( E ) | int Y Y ® * T | e
• The LL(1) parsing table:int * + ( ) $
E T X T XX + E e eT int Y ( E )Y * T e e e
leftmost non-terminal
next input token
rhs of production to use
![Page 7: Top-Down Parsing and Intro to Bottom-Up ParsingBottom-Up Parsing •Bottom-up parsing is more general than top-down parsing –And just as efficient –Builds on ideas in top-down](https://reader034.fdocuments.us/reader034/viewer/2022042606/5fb0e18455cbcf1c29211b7c/html5/thumbnails/7.jpg)
7
LL(1) Parsing Table Example (Cont.)
• Consider the [E, int] entry– “When current non-terminal is E and next input is
int, use production E ® T X”– This can generate an int in the first position
• Consider the [Y,+] entry– “When current non-terminal is Y and current token
is +, get rid of Y”– Y can be followed by + only if Y ® e
![Page 8: Top-Down Parsing and Intro to Bottom-Up ParsingBottom-Up Parsing •Bottom-up parsing is more general than top-down parsing –And just as efficient –Builds on ideas in top-down](https://reader034.fdocuments.us/reader034/viewer/2022042606/5fb0e18455cbcf1c29211b7c/html5/thumbnails/8.jpg)
8
LL(1) Parsing Tables. Errors
• Blank entries indicate error situations
• Consider the [E,*] entry– “There is no way to derive a string starting with *
from non-terminal E”
![Page 9: Top-Down Parsing and Intro to Bottom-Up ParsingBottom-Up Parsing •Bottom-up parsing is more general than top-down parsing –And just as efficient –Builds on ideas in top-down](https://reader034.fdocuments.us/reader034/viewer/2022042606/5fb0e18455cbcf1c29211b7c/html5/thumbnails/9.jpg)
9
Using Parsing Tables
• Method similar to recursive descent, except– For the leftmost non-terminal S– We look at the next input token a– And choose the production shown at [S,a]
• A stack records frontier of parse tree– Non-terminals that have yet to be expanded– Terminals that have yet to matched against the input– Top of stack = leftmost pending terminal or non-terminal
• Reject on reaching error state• Accept on end of input & empty stack
![Page 10: Top-Down Parsing and Intro to Bottom-Up ParsingBottom-Up Parsing •Bottom-up parsing is more general than top-down parsing –And just as efficient –Builds on ideas in top-down](https://reader034.fdocuments.us/reader034/viewer/2022042606/5fb0e18455cbcf1c29211b7c/html5/thumbnails/10.jpg)
10
LL(1) Parsing Algorithm
initialize stack = <S $> and next repeat
case stack of<X, rest> : if T[X,*next] = Y1…Yn
then stack ¬ <Y1… Yn rest>;else error ();
<t, rest> : if t == *next ++ then stack ¬ <rest>;else error ();
until stack == < >
![Page 11: Top-Down Parsing and Intro to Bottom-Up ParsingBottom-Up Parsing •Bottom-up parsing is more general than top-down parsing –And just as efficient –Builds on ideas in top-down](https://reader034.fdocuments.us/reader034/viewer/2022042606/5fb0e18455cbcf1c29211b7c/html5/thumbnails/11.jpg)
11
LL(1) Parsing Algorithm $ marks bottom of stack
For non-terminal X on top of stack, lookup production
Pop X, push production rhs on stack. Note leftmost symbol of rhs is on top of the stack.
initialize stack = <S $> and next repeat
case stack of<X, rest> : if T[X,*next] = Y1…Yn
then stack ¬ <Y1… Yn rest>;else error ();
<t, rest> : if t == *next ++ then stack ¬ <rest>;else error ();
until stack == < >
For terminal t on top of stack, check t matches next input token.
![Page 12: Top-Down Parsing and Intro to Bottom-Up ParsingBottom-Up Parsing •Bottom-up parsing is more general than top-down parsing –And just as efficient –Builds on ideas in top-down](https://reader034.fdocuments.us/reader034/viewer/2022042606/5fb0e18455cbcf1c29211b7c/html5/thumbnails/12.jpg)
12
LL(1) Parsing Example
Stack Input ActionE $ int * int $ T XT X $ int * int $ int Yint Y X $ int * int $ terminalY X $ * int $ * T* T X $ * int $ terminalT X $ int $ int Yint Y X $ int $ terminalY X $ $ eX $ $ e$ $ ACCEPT
E ® T XX ® + E | e
T ® int Y | ( E )Y ® * T | e
![Page 13: Top-Down Parsing and Intro to Bottom-Up ParsingBottom-Up Parsing •Bottom-up parsing is more general than top-down parsing –And just as efficient –Builds on ideas in top-down](https://reader034.fdocuments.us/reader034/viewer/2022042606/5fb0e18455cbcf1c29211b7c/html5/thumbnails/13.jpg)
13
Constructing Parsing Tables: The Intuition
• Consider non-terminal A, production A ® a, & token t• T[A,t] = a in two cases:
• If a ®* t b– a can derive a t in the first position– We say that t Î First(a)
• If a ®* e and S ®* γ A t d– Useful if stack has A, input is t, and A cannot derive t– In this case only option is to get rid of A (by deriving e)
• Can work only if t can follow A in at least one derivation– We say t Î Follow(A)
![Page 14: Top-Down Parsing and Intro to Bottom-Up ParsingBottom-Up Parsing •Bottom-up parsing is more general than top-down parsing –And just as efficient –Builds on ideas in top-down](https://reader034.fdocuments.us/reader034/viewer/2022042606/5fb0e18455cbcf1c29211b7c/html5/thumbnails/14.jpg)
14
Computing First Sets
DefinitionFirst(X) = { t | X ®* ta} È {e | X ®* e}
Algorithm sketch:1. First(t) = { t }2. e Î First(X)
• if X ® e or• if X ® A1 … An and e Î First(Ai) for all 1 £ i £ n
3. First(a) Í First(X)• if X ® A1 … An a and e Î First(Ai) for all 1 £ i £ n
![Page 15: Top-Down Parsing and Intro to Bottom-Up ParsingBottom-Up Parsing •Bottom-up parsing is more general than top-down parsing –And just as efficient –Builds on ideas in top-down](https://reader034.fdocuments.us/reader034/viewer/2022042606/5fb0e18455cbcf1c29211b7c/html5/thumbnails/15.jpg)
15
First Sets: Example
1. First(t) = { t }2. e Î First(X)
– if X ® e or– if X ® A1 … An and e Î First(Ai) for all 1 £ i £ n
3. First(a) Í First(X)– if X ® A1 … An a and e Î First(Ai) for all 1 £ i £ n
E ® T X X ® + E | eT ® ( E ) | int Y Y ® * T | e
First( E ) = First( X ) =
First( T ) = First( Y ) =
![Page 16: Top-Down Parsing and Intro to Bottom-Up ParsingBottom-Up Parsing •Bottom-up parsing is more general than top-down parsing –And just as efficient –Builds on ideas in top-down](https://reader034.fdocuments.us/reader034/viewer/2022042606/5fb0e18455cbcf1c29211b7c/html5/thumbnails/16.jpg)
16
First Sets: Example
• Recall the grammar E ® T X X ® + E | eT ® ( E ) | int Y Y ® * T | e
• First setsFirst( ( ) = { ( } First( T ) = {int, ( }First( ) ) = { ) } First( E ) = {int, ( }First( int) = { int } First( X ) = {+, e }First( + ) = { + } First( Y ) = {*, e }First( * ) = { * }
![Page 17: Top-Down Parsing and Intro to Bottom-Up ParsingBottom-Up Parsing •Bottom-up parsing is more general than top-down parsing –And just as efficient –Builds on ideas in top-down](https://reader034.fdocuments.us/reader034/viewer/2022042606/5fb0e18455cbcf1c29211b7c/html5/thumbnails/17.jpg)
17
Computing Follow Sets
• Definition:Follow(X) = { t | S ®* b X t d }
• Intuition– If X ® A B then First(B) Í Follow(A) and
Follow(X) Í Follow(B)• if B ®* e then Follow(X) Í Follow(A)
– If S is the start symbol then $ Î Follow(S)
![Page 18: Top-Down Parsing and Intro to Bottom-Up ParsingBottom-Up Parsing •Bottom-up parsing is more general than top-down parsing –And just as efficient –Builds on ideas in top-down](https://reader034.fdocuments.us/reader034/viewer/2022042606/5fb0e18455cbcf1c29211b7c/html5/thumbnails/18.jpg)
18
Computing Follow Sets (Cont.)
Algorithm sketch:1. $ Î Follow(S)2. First(b) - {e} Í Follow(X)
– For each production A ® a X b3. Follow(A) Í Follow(X)
– For each production A ® a X b where e Î First(b)
![Page 19: Top-Down Parsing and Intro to Bottom-Up ParsingBottom-Up Parsing •Bottom-up parsing is more general than top-down parsing –And just as efficient –Builds on ideas in top-down](https://reader034.fdocuments.us/reader034/viewer/2022042606/5fb0e18455cbcf1c29211b7c/html5/thumbnails/19.jpg)
19
Follow Sets: Example
• Recall the grammar E ® T X X ® + E | eT ® ( E ) | int Y Y ® * T | e
• Follow setsFollow( + ) = { int, ( } Follow( * ) = { int, ( } Follow( ( ) = { int, ( } Follow( E ) = {), $} Follow( X ) = {$, ) } Follow( T ) = {+, ) , $}Follow( ) ) = {+, ) , $} Follow( Y ) = {+, ) , $}Follow( int) = {*, +, ) , $}
![Page 20: Top-Down Parsing and Intro to Bottom-Up ParsingBottom-Up Parsing •Bottom-up parsing is more general than top-down parsing –And just as efficient –Builds on ideas in top-down](https://reader034.fdocuments.us/reader034/viewer/2022042606/5fb0e18455cbcf1c29211b7c/html5/thumbnails/20.jpg)
20
Computing the Follow Sets (for the Non-Terminals)
• Recall the grammar E ® T X X ® + E | eT ® ( E ) | int Y Y ® * T | e
• $ Î Follow(E)
![Page 21: Top-Down Parsing and Intro to Bottom-Up ParsingBottom-Up Parsing •Bottom-up parsing is more general than top-down parsing –And just as efficient –Builds on ideas in top-down](https://reader034.fdocuments.us/reader034/viewer/2022042606/5fb0e18455cbcf1c29211b7c/html5/thumbnails/21.jpg)
21
Computing the Follow Sets (for the Non-Terminals)
• Recall the grammar E ® T X X ® + E | eT ® ( E ) | int Y Y ® * T | e
• $ Î Follow(E)• First(X) Í Follow(T)• Follow(E) Í Follow(X)• Follow(E) Í Follow(T) because e Î First(X)
![Page 22: Top-Down Parsing and Intro to Bottom-Up ParsingBottom-Up Parsing •Bottom-up parsing is more general than top-down parsing –And just as efficient –Builds on ideas in top-down](https://reader034.fdocuments.us/reader034/viewer/2022042606/5fb0e18455cbcf1c29211b7c/html5/thumbnails/22.jpg)
22
Computing the Follow Sets (for the Non-Terminals)
• Recall the grammar E ® T X X ® + E | eT ® ( E ) | int Y Y ® * T | e
• $ Î Follow(E)• First(X) Í Follow(T)• Follow(E) Í Follow(X)• Follow(E) Í Follow(T) because e Î First(X)• ) Î Follow(E)
![Page 23: Top-Down Parsing and Intro to Bottom-Up ParsingBottom-Up Parsing •Bottom-up parsing is more general than top-down parsing –And just as efficient –Builds on ideas in top-down](https://reader034.fdocuments.us/reader034/viewer/2022042606/5fb0e18455cbcf1c29211b7c/html5/thumbnails/23.jpg)
23
Computing the Follow Sets (for the Non-Terminals)
• Recall the grammar E ® T X X ® + E | eT ® ( E ) | int Y Y ® * T | e
• $ Î Follow(E)• First(X) Í Follow(T)• Follow(E) Í Follow(X)• Follow(E) Í Follow(T) because e Î First(X)• ) Î Follow(E)• Follow(T) Í Follow(Y)
![Page 24: Top-Down Parsing and Intro to Bottom-Up ParsingBottom-Up Parsing •Bottom-up parsing is more general than top-down parsing –And just as efficient –Builds on ideas in top-down](https://reader034.fdocuments.us/reader034/viewer/2022042606/5fb0e18455cbcf1c29211b7c/html5/thumbnails/24.jpg)
24
Computing the Follow Sets (for the Non-Terminals)
• Recall the grammar E ® T X X ® + E | eT ® ( E ) | int Y Y ® * T | e
• $ Î Follow(E)• First(X) Í Follow(T)• Follow(E) Í Follow(X)• Follow(E) Í Follow(T) because e Î First(X)• ) Î Follow(E)• Follow(T) Í Follow(Y)• Follow(X) Í Follow(E)
![Page 25: Top-Down Parsing and Intro to Bottom-Up ParsingBottom-Up Parsing •Bottom-up parsing is more general than top-down parsing –And just as efficient –Builds on ideas in top-down](https://reader034.fdocuments.us/reader034/viewer/2022042606/5fb0e18455cbcf1c29211b7c/html5/thumbnails/25.jpg)
25
Computing the Follow Sets (for the Non-Terminals)
• Recall the grammar E ® T X X ® + E | eT ® ( E ) | int Y Y ® * T | e
• $ Î Follow(E)• First(X) Í Follow(T)• Follow(E) Í Follow(X)• Follow(E) Í Follow(T) because e Î First(X)• ) Î Follow(E)• Follow(T) Í Follow(Y)• Follow(X) Í Follow(E)• Follow(Y) Í Follow(T)
![Page 26: Top-Down Parsing and Intro to Bottom-Up ParsingBottom-Up Parsing •Bottom-up parsing is more general than top-down parsing –And just as efficient –Builds on ideas in top-down](https://reader034.fdocuments.us/reader034/viewer/2022042606/5fb0e18455cbcf1c29211b7c/html5/thumbnails/26.jpg)
26
Computing the Follow Sets (for the Non-Terminals)
• Recall the grammar E ® T X X ® + E | eT ® ( E ) | int Y Y ® * T | e
• $ Î Follow(E)• First(X) Í Follow(T)• Follow(E) Í Follow(X)• Follow(E) Í Follow(T)• ) Î Follow(E)• Follow(T) Í Follow(Y)• Follow(X) Í Follow(E)• Follow(Y) Í Follow(T)
![Page 27: Top-Down Parsing and Intro to Bottom-Up ParsingBottom-Up Parsing •Bottom-up parsing is more general than top-down parsing –And just as efficient –Builds on ideas in top-down](https://reader034.fdocuments.us/reader034/viewer/2022042606/5fb0e18455cbcf1c29211b7c/html5/thumbnails/27.jpg)
Computing the Follow Sets (for the Non-Terminals)
27
$ Follow(E)
Follow(X)
First(X)Follow(T)
)
Follow(Y)
![Page 28: Top-Down Parsing and Intro to Bottom-Up ParsingBottom-Up Parsing •Bottom-up parsing is more general than top-down parsing –And just as efficient –Builds on ideas in top-down](https://reader034.fdocuments.us/reader034/viewer/2022042606/5fb0e18455cbcf1c29211b7c/html5/thumbnails/28.jpg)
Computing the Follow Sets (for the Non-Terminals)
28
$ Follow(E)
Follow(X)
First(X)Follow(T)
)
Follow(Y)
+
$,),+
$,)$,)
$,),+
![Page 29: Top-Down Parsing and Intro to Bottom-Up ParsingBottom-Up Parsing •Bottom-up parsing is more general than top-down parsing –And just as efficient –Builds on ideas in top-down](https://reader034.fdocuments.us/reader034/viewer/2022042606/5fb0e18455cbcf1c29211b7c/html5/thumbnails/29.jpg)
Computing the Follow Sets (for all symbols)
29
$ Follow(E)
Follow(X)
First(X)Follow(T)
)
Follow(Y)
+
$,),+
$,)$,)
$,),+First(E) Follow(+)(,int (,int
Follow(()(,int
First(T) Follow(*)(,int (,int
Follow())
$,),+
Follow(int)First(Y)$,),+,**
![Page 30: Top-Down Parsing and Intro to Bottom-Up ParsingBottom-Up Parsing •Bottom-up parsing is more general than top-down parsing –And just as efficient –Builds on ideas in top-down](https://reader034.fdocuments.us/reader034/viewer/2022042606/5fb0e18455cbcf1c29211b7c/html5/thumbnails/30.jpg)
30
Constructing LL(1) Parsing Tables
• Construct a parsing table T for CFG G
• For each production A ® a in G do:– For each terminal t Î First(a) do
• T[A, t] = a– If e Î First(a), then for each t Î Follow(A) do
• T[A, t] = a– If e Î First(a) and $ Î Follow(A) do
• T[A, $] = a
![Page 31: Top-Down Parsing and Intro to Bottom-Up ParsingBottom-Up Parsing •Bottom-up parsing is more general than top-down parsing –And just as efficient –Builds on ideas in top-down](https://reader034.fdocuments.us/reader034/viewer/2022042606/5fb0e18455cbcf1c29211b7c/html5/thumbnails/31.jpg)
31
Notes on LL(1) Parsing Tables
• If any entry is multiply defined then G is not LL(1)– If G is ambiguous– If G is left recursive– If G is not left-factored– And in other cases as well
• Most programming language CFGs are not LL(1)
![Page 32: Top-Down Parsing and Intro to Bottom-Up ParsingBottom-Up Parsing •Bottom-up parsing is more general than top-down parsing –And just as efficient –Builds on ideas in top-down](https://reader034.fdocuments.us/reader034/viewer/2022042606/5fb0e18455cbcf1c29211b7c/html5/thumbnails/32.jpg)
32
Bottom-Up Parsing
• Bottom-up parsing is more general than top-down parsing– And just as efficient– Builds on ideas in top-down parsing
• Bottom-up is the preferred method
• Concepts today, algorithms next time
![Page 33: Top-Down Parsing and Intro to Bottom-Up ParsingBottom-Up Parsing •Bottom-up parsing is more general than top-down parsing –And just as efficient –Builds on ideas in top-down](https://reader034.fdocuments.us/reader034/viewer/2022042606/5fb0e18455cbcf1c29211b7c/html5/thumbnails/33.jpg)
33
An Introductory Example
• Bottom-up parsers don’t need left-factored grammars
• Revert to the “natural” grammar for our example:
E ® T + E | TT ® int * T | int | (E)
• Consider the string: int * int + int
![Page 34: Top-Down Parsing and Intro to Bottom-Up ParsingBottom-Up Parsing •Bottom-up parsing is more general than top-down parsing –And just as efficient –Builds on ideas in top-down](https://reader034.fdocuments.us/reader034/viewer/2022042606/5fb0e18455cbcf1c29211b7c/html5/thumbnails/34.jpg)
34
The Idea
Bottom-up parsing reduces a string to the start symbol by inverting productions:
int * int + int T ® intint * T + int T ® int * TT + int T ® intT + T E ® TT + E E ® T + EE
![Page 35: Top-Down Parsing and Intro to Bottom-Up ParsingBottom-Up Parsing •Bottom-up parsing is more general than top-down parsing –And just as efficient –Builds on ideas in top-down](https://reader034.fdocuments.us/reader034/viewer/2022042606/5fb0e18455cbcf1c29211b7c/html5/thumbnails/35.jpg)
35
Observation
• Read the productions in reverse (from bottom to top)
• This is a reverse rightmost derivation!
int * int + int T ® intint * T + int T ® int * TT + int T ® intT + T E ® TT + E E ® T + EE
![Page 36: Top-Down Parsing and Intro to Bottom-Up ParsingBottom-Up Parsing •Bottom-up parsing is more general than top-down parsing –And just as efficient –Builds on ideas in top-down](https://reader034.fdocuments.us/reader034/viewer/2022042606/5fb0e18455cbcf1c29211b7c/html5/thumbnails/36.jpg)
36
Important Fact #1
Important Fact #1 about bottom-up parsing:
A bottom-up parser traces a rightmost derivation in reverse
![Page 37: Top-Down Parsing and Intro to Bottom-Up ParsingBottom-Up Parsing •Bottom-up parsing is more general than top-down parsing –And just as efficient –Builds on ideas in top-down](https://reader034.fdocuments.us/reader034/viewer/2022042606/5fb0e18455cbcf1c29211b7c/html5/thumbnails/37.jpg)
37
A Bottom-up Parse
int * int + int
int * T + int
T + int
T + T
T + E
E
E
T E
+ int*int
T
int
T
![Page 38: Top-Down Parsing and Intro to Bottom-Up ParsingBottom-Up Parsing •Bottom-up parsing is more general than top-down parsing –And just as efficient –Builds on ideas in top-down](https://reader034.fdocuments.us/reader034/viewer/2022042606/5fb0e18455cbcf1c29211b7c/html5/thumbnails/38.jpg)
38
A Bottom-up Parse in Detail (1)
+ int*int int
int * int + int
![Page 39: Top-Down Parsing and Intro to Bottom-Up ParsingBottom-Up Parsing •Bottom-up parsing is more general than top-down parsing –And just as efficient –Builds on ideas in top-down](https://reader034.fdocuments.us/reader034/viewer/2022042606/5fb0e18455cbcf1c29211b7c/html5/thumbnails/39.jpg)
39
A Bottom-up Parse in Detail (2)
int * int + int
int * T + int
+ int*int int
T
![Page 40: Top-Down Parsing and Intro to Bottom-Up ParsingBottom-Up Parsing •Bottom-up parsing is more general than top-down parsing –And just as efficient –Builds on ideas in top-down](https://reader034.fdocuments.us/reader034/viewer/2022042606/5fb0e18455cbcf1c29211b7c/html5/thumbnails/40.jpg)
40
A Bottom-up Parse in Detail (3)
int * int + int
int * T + int
T + int T
+ int*int int
T
![Page 41: Top-Down Parsing and Intro to Bottom-Up ParsingBottom-Up Parsing •Bottom-up parsing is more general than top-down parsing –And just as efficient –Builds on ideas in top-down](https://reader034.fdocuments.us/reader034/viewer/2022042606/5fb0e18455cbcf1c29211b7c/html5/thumbnails/41.jpg)
41
A Bottom-up Parse in Detail (4)
int * int + int
int * T + int
T + int
T + T
T
+ int*int
T
int
T
![Page 42: Top-Down Parsing and Intro to Bottom-Up ParsingBottom-Up Parsing •Bottom-up parsing is more general than top-down parsing –And just as efficient –Builds on ideas in top-down](https://reader034.fdocuments.us/reader034/viewer/2022042606/5fb0e18455cbcf1c29211b7c/html5/thumbnails/42.jpg)
42
A Bottom-up Parse in Detail (5)
int * int + int
int * T + int
T + int
T + T
T + E
T E
+ int*int
T
int
T
![Page 43: Top-Down Parsing and Intro to Bottom-Up ParsingBottom-Up Parsing •Bottom-up parsing is more general than top-down parsing –And just as efficient –Builds on ideas in top-down](https://reader034.fdocuments.us/reader034/viewer/2022042606/5fb0e18455cbcf1c29211b7c/html5/thumbnails/43.jpg)
43
A Bottom-up Parse in Detail (6)
int * int + int
int * T + int
T + int
T + T
T + E
E
E
T E
+ int*int
T
int
T
![Page 44: Top-Down Parsing and Intro to Bottom-Up ParsingBottom-Up Parsing •Bottom-up parsing is more general than top-down parsing –And just as efficient –Builds on ideas in top-down](https://reader034.fdocuments.us/reader034/viewer/2022042606/5fb0e18455cbcf1c29211b7c/html5/thumbnails/44.jpg)
44
A Trivial Bottom-Up Parsing Algorithm
Let I = input stringrepeat
pick a non-empty substring b of Iwhere X® b is a production
if no such b, backtrackreplace one b by X in I
until I = “S” (the start symbol) or all possibilities are exhausted
![Page 45: Top-Down Parsing and Intro to Bottom-Up ParsingBottom-Up Parsing •Bottom-up parsing is more general than top-down parsing –And just as efficient –Builds on ideas in top-down](https://reader034.fdocuments.us/reader034/viewer/2022042606/5fb0e18455cbcf1c29211b7c/html5/thumbnails/45.jpg)
45
Questions
• Does this algorithm terminate?
• Does the algorithm handle all cases?
• How do we choose the substring to reduce at each step?
![Page 46: Top-Down Parsing and Intro to Bottom-Up ParsingBottom-Up Parsing •Bottom-up parsing is more general than top-down parsing –And just as efficient –Builds on ideas in top-down](https://reader034.fdocuments.us/reader034/viewer/2022042606/5fb0e18455cbcf1c29211b7c/html5/thumbnails/46.jpg)
46
Where Do Reductions Happen?
Important Fact #1 has an interesting consequence:– Let abw be a step of a bottom-up parse– Assume the next reduction is by X® b– Then w is a string of terminals
Why? Because aXw ® abw is a step in a right-most derivation
![Page 47: Top-Down Parsing and Intro to Bottom-Up ParsingBottom-Up Parsing •Bottom-up parsing is more general than top-down parsing –And just as efficient –Builds on ideas in top-down](https://reader034.fdocuments.us/reader034/viewer/2022042606/5fb0e18455cbcf1c29211b7c/html5/thumbnails/47.jpg)
47
Notation
• Idea: Split string into two substrings– Right substring is as yet unexamined by parsing
(a string of terminals)– Left substring has terminals and non-terminals
• The dividing point is marked by a |– The | is not part of the string
• Initially, all input is unexamined |x1x2 . . . xn
![Page 48: Top-Down Parsing and Intro to Bottom-Up ParsingBottom-Up Parsing •Bottom-up parsing is more general than top-down parsing –And just as efficient –Builds on ideas in top-down](https://reader034.fdocuments.us/reader034/viewer/2022042606/5fb0e18455cbcf1c29211b7c/html5/thumbnails/48.jpg)
48
Shift-Reduce Parsing
Bottom-up parsing uses only two kinds of actions:
Shift
Reduce
![Page 49: Top-Down Parsing and Intro to Bottom-Up ParsingBottom-Up Parsing •Bottom-up parsing is more general than top-down parsing –And just as efficient –Builds on ideas in top-down](https://reader034.fdocuments.us/reader034/viewer/2022042606/5fb0e18455cbcf1c29211b7c/html5/thumbnails/49.jpg)
49
Shift
• Shift: Move | one place to the right– Shifts a terminal to the left string
ABC|xyz Þ ABCx|yz
![Page 50: Top-Down Parsing and Intro to Bottom-Up ParsingBottom-Up Parsing •Bottom-up parsing is more general than top-down parsing –And just as efficient –Builds on ideas in top-down](https://reader034.fdocuments.us/reader034/viewer/2022042606/5fb0e18455cbcf1c29211b7c/html5/thumbnails/50.jpg)
50
Reduce
• Apply an inverse production at the right end of the left string– If A ® xy is a production, then
Cbxy|ijk Þ CbA|ijk
![Page 51: Top-Down Parsing and Intro to Bottom-Up ParsingBottom-Up Parsing •Bottom-up parsing is more general than top-down parsing –And just as efficient –Builds on ideas in top-down](https://reader034.fdocuments.us/reader034/viewer/2022042606/5fb0e18455cbcf1c29211b7c/html5/thumbnails/51.jpg)
51
The Example with Reductions Only
int * int | + int reduce T ® intint * T | + int reduce T ® int * T
T + int | reduce T ® intT + T | reduce E ® TT + E | reduce E ® T + EE |
![Page 52: Top-Down Parsing and Intro to Bottom-Up ParsingBottom-Up Parsing •Bottom-up parsing is more general than top-down parsing –And just as efficient –Builds on ideas in top-down](https://reader034.fdocuments.us/reader034/viewer/2022042606/5fb0e18455cbcf1c29211b7c/html5/thumbnails/52.jpg)
52
The Example with Shift-Reduce Parsing
|int * int + int shiftint | * int + int shiftint * | int + int shiftint * int | + int reduce T ® intint * T | + int reduce T ® int * TT | + int shiftT + | int shiftT + int | reduce T ® intT + T | reduce E ® TT + E | reduce E ® T + EE |
![Page 53: Top-Down Parsing and Intro to Bottom-Up ParsingBottom-Up Parsing •Bottom-up parsing is more general than top-down parsing –And just as efficient –Builds on ideas in top-down](https://reader034.fdocuments.us/reader034/viewer/2022042606/5fb0e18455cbcf1c29211b7c/html5/thumbnails/53.jpg)
53
A Shift-Reduce Parse in Detail (1)
+ int*int intm
|int * int + int
![Page 54: Top-Down Parsing and Intro to Bottom-Up ParsingBottom-Up Parsing •Bottom-up parsing is more general than top-down parsing –And just as efficient –Builds on ideas in top-down](https://reader034.fdocuments.us/reader034/viewer/2022042606/5fb0e18455cbcf1c29211b7c/html5/thumbnails/54.jpg)
54
A Shift-Reduce Parse in Detail (2)
+ int*int intm
|int * int + intint | * int + int
![Page 55: Top-Down Parsing and Intro to Bottom-Up ParsingBottom-Up Parsing •Bottom-up parsing is more general than top-down parsing –And just as efficient –Builds on ideas in top-down](https://reader034.fdocuments.us/reader034/viewer/2022042606/5fb0e18455cbcf1c29211b7c/html5/thumbnails/55.jpg)
55
A Shift-Reduce Parse in Detail (3)
+ int*int intm
|int * int + intint | * int + intint * | int + int
![Page 56: Top-Down Parsing and Intro to Bottom-Up ParsingBottom-Up Parsing •Bottom-up parsing is more general than top-down parsing –And just as efficient –Builds on ideas in top-down](https://reader034.fdocuments.us/reader034/viewer/2022042606/5fb0e18455cbcf1c29211b7c/html5/thumbnails/56.jpg)
56
A Shift-Reduce Parse in Detail (4)
+ int*int intm
|int * int + intint | * int + intint * | int + intint * int | + int
![Page 57: Top-Down Parsing and Intro to Bottom-Up ParsingBottom-Up Parsing •Bottom-up parsing is more general than top-down parsing –And just as efficient –Builds on ideas in top-down](https://reader034.fdocuments.us/reader034/viewer/2022042606/5fb0e18455cbcf1c29211b7c/html5/thumbnails/57.jpg)
57
A Shift-Reduce Parse in Detail (5)
+ int*int int
T
|int * int + intint | * int + intint * | int + intint * int | + intint * T | + int
m
![Page 58: Top-Down Parsing and Intro to Bottom-Up ParsingBottom-Up Parsing •Bottom-up parsing is more general than top-down parsing –And just as efficient –Builds on ideas in top-down](https://reader034.fdocuments.us/reader034/viewer/2022042606/5fb0e18455cbcf1c29211b7c/html5/thumbnails/58.jpg)
58
A Shift-Reduce Parse in Detail (6)
T
+ int*int int
T
|int * int + intint | * int + intint * | int + intint * int | + intint * T | + intT | + int
m
![Page 59: Top-Down Parsing and Intro to Bottom-Up ParsingBottom-Up Parsing •Bottom-up parsing is more general than top-down parsing –And just as efficient –Builds on ideas in top-down](https://reader034.fdocuments.us/reader034/viewer/2022042606/5fb0e18455cbcf1c29211b7c/html5/thumbnails/59.jpg)
59
A Shift-Reduce Parse in Detail (7)
T
+ int*int int
T
|int * int + intint | * int + intint * | int + intint * int | + intint * T | + intT | + intT + | int
m
![Page 60: Top-Down Parsing and Intro to Bottom-Up ParsingBottom-Up Parsing •Bottom-up parsing is more general than top-down parsing –And just as efficient –Builds on ideas in top-down](https://reader034.fdocuments.us/reader034/viewer/2022042606/5fb0e18455cbcf1c29211b7c/html5/thumbnails/60.jpg)
60
A Shift-Reduce Parse in Detail (8)
T
+ int*int int
T
|int * int + intint | * int + intint * | int + intint * int | + intint * T | + intT | + intT + | intT + int |
m
![Page 61: Top-Down Parsing and Intro to Bottom-Up ParsingBottom-Up Parsing •Bottom-up parsing is more general than top-down parsing –And just as efficient –Builds on ideas in top-down](https://reader034.fdocuments.us/reader034/viewer/2022042606/5fb0e18455cbcf1c29211b7c/html5/thumbnails/61.jpg)
61
A Shift-Reduce Parse in Detail (9)
T
+ int*int
T
int
T
|int * int + intint | * int + intint * | int + intint * int | + intint * T | + intT | + intT + | intT + int |T + T |
m
![Page 62: Top-Down Parsing and Intro to Bottom-Up ParsingBottom-Up Parsing •Bottom-up parsing is more general than top-down parsing –And just as efficient –Builds on ideas in top-down](https://reader034.fdocuments.us/reader034/viewer/2022042606/5fb0e18455cbcf1c29211b7c/html5/thumbnails/62.jpg)
62
A Shift-Reduce Parse in Detail (10)
T E
+ int*int
T
int
T
|int * int + intint | * int + intint * | int + intint * int | + intint * T | + intT | + intT + | intT + int |T + T |T + E | m
![Page 63: Top-Down Parsing and Intro to Bottom-Up ParsingBottom-Up Parsing •Bottom-up parsing is more general than top-down parsing –And just as efficient –Builds on ideas in top-down](https://reader034.fdocuments.us/reader034/viewer/2022042606/5fb0e18455cbcf1c29211b7c/html5/thumbnails/63.jpg)
63
A Shift-Reduce Parse in Detail (11)
E
T E
+ int*int
T
int
T
|int * int + intint | * int + intint * | int + intint * int | + intint * T | + intT | + intT + | intT + int |T + T |T + E |E |
m
![Page 64: Top-Down Parsing and Intro to Bottom-Up ParsingBottom-Up Parsing •Bottom-up parsing is more general than top-down parsing –And just as efficient –Builds on ideas in top-down](https://reader034.fdocuments.us/reader034/viewer/2022042606/5fb0e18455cbcf1c29211b7c/html5/thumbnails/64.jpg)
64
The Stack
• Left string can be implemented by a stack– Top of the stack is the |
• Shift pushes a terminal on the stack
• Reduce pops 0 or more symbols off of the stack (production rhs) and pushes a non-terminal on the stack (production lhs)
![Page 65: Top-Down Parsing and Intro to Bottom-Up ParsingBottom-Up Parsing •Bottom-up parsing is more general than top-down parsing –And just as efficient –Builds on ideas in top-down](https://reader034.fdocuments.us/reader034/viewer/2022042606/5fb0e18455cbcf1c29211b7c/html5/thumbnails/65.jpg)
65
Conflicts
• In a given state, more than one action (shift or reduce) may lead to a valid parse
• If it is legal to shift or reduce, there is a shift-reduce conflict
• If it is legal to reduce by two different productions, there is a reduce-reduce conflict
• You will see such conflicts in your project!– More next time . . .