Intro to Bottom-up Parsing Lecture 9
Bottom-Up Parsing Bottom-up parsing is more general than topdown parsing And just as efficient Builds on ideas in top-down parsing Bottom-up is the preferred method Concepts today, algorithms next time 2
An Introductory Example Bottom-up parsers don t need left-factored grammars Revert to the natural grammar for our example: E + E (E) Consider the string: + 3
he Idea Bottom-up parsing reduces a string to the start symbol by inverting productions: + + + + + E E E E + E 4
Observation Read the productions in reverse (from bottom to top) his is a rightmost derivation! + + + + + E E E E + E 5
Important Fact #1 Important Fact #1 about bottom-up parsing: A bottom-up parser traces a rightmost derivation in reverse 6
A Bottom-up Parse + E + + E + + E E + 7
A Bottom-up Parse in Detail (1) + + 8
A Bottom-up Parse in Detail (2) + + + 9
A Bottom-up Parse in Detail (3) + + + + 10
A Bottom-up Parse in Detail (4) + + + + + 11
A Bottom-up Parse in Detail (5) + + + E + + E + 12
A Bottom-up Parse in Detail (6) + E + + E + + E E + 13
A rivial Bottom-Up Parsing Algorithm Let I = input string repeat pick a non-empty substring β of I where X β is a production if no such β, backtrack replace one β by X in I until I = S (the start symbol) or all possibilities are exhausted 14
Questions Does this algorithm terminate? How fast is the algorithm? Does the algorithm handle all cases? How do we choose the substring to reduce at each step? 15
Where Do Reductions Happen? Important Fact #1 has an eresting consequence: Let αβω be a step of a bottom-up parse Assume the next reduction is by X β hen ω is a string of terminals Why? Because αxω αβω is a step in a rightmost derivation 16
Notation Idea: Split string o two substrings Right substring is as yet unexamined by parsing (a string of terminals) Left substring has terminals and non-terminals he dividing po is marked by a he is not part of the string Initially, all input is unexamined x 1 x 2... x n 17
Shift-Reduce Parsing Bottom-up parsing uses only two kinds of actions: Shift Reduce 18
Shift Shift: Move one place to the right Shifts a terminal to the left string ABC xyz ABCx yz 19
Reduce Apply an inverse production at the right end of the left string If A xy is a production, then Cbxy ijk CbA ijk 20
he Example with Reductions Only + + reduce reduce + + + E reduce reduce E reduce E + E E 21
he Example with Shift-Reduce Parsing + + + + + + + + + + E shift shift shift reduce reduce shift shift reduce reduce E reduce E + E E 22
A Shift-Reduce Parse in Detail (1) + + 23
A Shift-Reduce Parse in Detail (2) + + + 24
A Shift-Reduce Parse in Detail (3) + + + + 25
A Shift-Reduce Parse in Detail (4) + + + + + 26
A Shift-Reduce Parse in Detail (5) + + + + + + 27
A Shift-Reduce Parse in Detail (6) + + + + + + + 28
A Shift-Reduce Parse in Detail (7) + + + + + + + + 29
A Shift-Reduce Parse in Detail (8) + + + + + + + + + 30
A Shift-Reduce Parse in Detail (9) + + + + + + + + + + 31
A Shift-Reduce Parse in Detail (10) + + + + + E + + + + + E + 32
A Shift-Reduce Parse in Detail (11) + + + + + + + + + + E E E E + 33
he Stack Left string can be implemented by a stack op 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 nonterminal on the stack (production lhs) 34
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 shiftreduce conflict If it is legal to reduce by two different productions, there is a reduce-reduce conflict 35