Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 536 Spring 20011 Introduction to Bottom-Up Parsing Lecture 11.

Similar presentations


Presentation on theme: "CS 536 Spring 20011 Introduction to Bottom-Up Parsing Lecture 11."— Presentation transcript:

1 CS 536 Spring 20011 Introduction to Bottom-Up Parsing Lecture 11

2 CS 536 Spring 20012 Outline The strategy: shift-reduce parsing Ambiguity and precedence declarations Next lecture: bottom-up parsing algorithms

3 CS 536 Spring 20013 Predictive Parsing Summary First and Follow sets are used to construct predictive tables –For non-terminal A and input t, use a production A  a where t  First( a ) –For non-terminal A and input t, if e  First(A) and t  Follow( a ), then use a production A  a where e  First( a ) We’ll see First and Follow sets again...

4 CS 536 Spring 20014 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 in practice Concepts today, algorithms next time

5 CS 536 Spring 20015 An Introductory Example Bottom-up parsers don’t need left-factored grammars Hence we can revert to the “natural” grammar for our example: E  T + E | T T  int * T | int | (E) Consider the string: int * int + int

6 CS 536 Spring 20016 The Idea Bottom-up parsing reduces a string to the start symbol by inverting productions: int * int + intT  int int * T + intT  int * T T + intT  int T + TE  T T + EE  T + E E

7 CS 536 Spring 20017 TEST YOURSELF #1 Question: find the rightmost derivation of the string int * int + int

8 CS 536 Spring 20018 Observation Read the productions found by bottom-up parse in reverse (i.e., from bottom to top) This is a rightmost derivation! int * int + intT  int int * T + intT  int * T T + intT  int T + TE  T T + EE  T + E E

9 CS 536 Spring 20019 Important Fact #1 Important Fact #1 about bottom-up parsing: A bottom-up parser traces a rightmost derivation in reverse

10 CS 536 Spring 200110 A Bottom-up Parse int * int + int int * T + int T + int T + T T + E E E TE + int * T T

11 CS 536 Spring 200111 A Bottom-up Parse in Detail (1) + int * int * int + int

12 CS 536 Spring 200112 A Bottom-up Parse in Detail (2) int * int + int int * T + int + int * T

13 CS 536 Spring 200113 A Bottom-up Parse in Detail (3) int * int + int int * T + int T + int T + int * T

14 CS 536 Spring 200114 A Bottom-up Parse in Detail (4) int * int + int int * T + int T + int T + T T + int * T T

15 CS 536 Spring 200115 A Bottom-up Parse in Detail (5) int * int + int int * T + int T + int T + T T + E TE + int * T T

16 CS 536 Spring 200116 A Bottom-up Parse in Detail (6) int * int + int int * T + int T + int T + T T + E E E TE + int * T T

17 CS 536 Spring 200117 A Trivial 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

18 CS 536 Spring 200118 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?

19 CS 536 Spring 200119 How to build the house of cards?

20 CS 536 Spring 200120 Where Do Reductions Happen Important Fact #1 has an interesting consequence: –Let  be a step of a bottom-up parse –Assume the next reduction is by X   –Then  is a string of terminals Why? Because  X    is a step in a right- most derivation

21 CS 536 Spring 200121 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 |x 1 x 2... x n

22 CS 536 Spring 200122 Shift-Reduce Parsing Bottom-up parsing uses only two kinds of actions: Shift Reduce

23 CS 536 Spring 200123 Shift Shift: Move | one place to the right –Shifts a terminal to the left string ABC|xyz  ABCx|yz

24 CS 536 Spring 200124 Reduce Apply an inverse production at the right end of the left string –If A  xy is a production, then Cbxy|ijk  CbA|ijk

25 CS 536 Spring 200125 The Example with Reductions Only int * int | + intreduce T  int int * T | + intreduce T  int * T T + int |reduce T  int T + T |reduce E  T T + E |reduce E  T + E E |

26 CS 536 Spring 200126 The Example with Shift-Reduce Parsing |int * int + intshift int | * int + intshift int * | int + intshift int * int | + intreduce T  int int * T | + intreduce T  int * T T | + intshift T + | intshift T + int |reduce T  int T + T |reduce E  T T + E |reduce E  T + E E |

27 CS 536 Spring 200127 A Shift-Reduce Parse in Detail (1) + int *  |int * int + int

28 CS 536 Spring 200128 A Shift-Reduce Parse in Detail (2) + int *  |int * int + int int | * int + int

29 CS 536 Spring 200129 A Shift-Reduce Parse in Detail (3) + int *  |int * int + int int | * int + int int * | int + int

30 CS 536 Spring 200130 A Shift-Reduce Parse in Detail (4) + int *  |int * int + int int | * int + int int * | int + int int * int | + int

31 CS 536 Spring 200131 A Shift-Reduce Parse in Detail (5) + int * T |int * int + int int | * int + int int * | int + int int * int | + int int * T | + int 

32 CS 536 Spring 200132 A Shift-Reduce Parse in Detail (6) T + int * T |int * int + int int | * int + int int * | int + int int * int | + int int * T | + int T | + int 

33 CS 536 Spring 200133 A Shift-Reduce Parse in Detail (7) T + int * T |int * int + int int | * int + int int * | int + int int * int | + int int * T | + int T | + int T + | int 

34 CS 536 Spring 200134 A Shift-Reduce Parse in Detail (8) T + int * T |int * int + int int | * int + int int * | int + int int * int | + int int * T | + int T | + int T + | int T + int | 

35 CS 536 Spring 200135 A Shift-Reduce Parse in Detail (9) T + int * T T |int * int + int int | * int + int int * | int + int int * int | + int int * T | + int T | + int T + | int T + int | T + T | 

36 CS 536 Spring 200136 A Shift-Reduce Parse in Detail (10) TE + int * T T |int * int + int int | * int + int int * | int + int int * int | + int int * T | + int T | + int T + | int T + int | T + T | T + E | 

37 CS 536 Spring 200137 A Shift-Reduce Parse in Detail (11) E TE + int * T T |int * int + int int | * int + int int * | int + int int * int | + int int * T | + int T | + int T + | int T + int | T + T | T + E | E | 

38 CS 536 Spring 200138 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)

39 CS 536 Spring 200139 Key Issue (will be resolved by algorithms) How do we decide when to shift or reduce? –Consider step int | * int + int –We could reduce by T  int giving T | * int + int –A fatal mistake: No way to reduce to the start symbol E

40 CS 536 Spring 200140 Conflicts Generic shift-reduce strategy: –If there is a handle on top of the stack, reduce –Otherwise, shift But what if there is a choice? –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

41 CS 536 Spring 200141 Source of Conflicts Ambiguous grammars always cause conflicts But beware, so do many non-ambiguous grammars

42 CS 536 Spring 200142 Conflict Example Consider our favorite ambiguous grammar: E  E + E |E * E |(E) |int

43 CS 536 Spring 200143 One Shift-Reduce Parse |int * int + intshift... E * E | + intreduce E  E * E E | + intshift E + | intshift E + int|reduce E  int E + E |reduce E  E + E E |

44 CS 536 Spring 200144 Another Shift-Reduce Parse |int * int + intshift... E * E | + intshift E * E + | intshift E * E + int |reduce E  int E * E + E|reduce E  E + E E * E |reduce E  E * E E |

45 CS 536 Spring 200145 Example Notes In the second step E * E | + int we can either shift or reduce by E  E * E Choice determines associativity of + and * As noted previously, grammar can be rewritten to enforce precedence Precedence declarations are an alternative

46 CS 536 Spring 200146 Precedence Declarations Revisited Precedence declarations cause shift-reduce parsers to resolve conflicts in certain ways Declaring “* has greater precedence than +” causes parser to reduce at E * E | + int More precisely, precedence declaration is used to resolve conflict between reducing a * and shifting a +

47 CS 536 Spring 200147 Precedence Declarations Revisited (Cont.) The term “precedence declaration” is misleading These declarations do not define precedence; they define conflict resolutions –Not quite the same thing!


Download ppt "CS 536 Spring 20011 Introduction to Bottom-Up Parsing Lecture 11."

Similar presentations


Ads by Google