This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: 1 BottomUp Parsing II Lecture 8 Prof. Aiken CS 143 Lecture 8 1 Review: ShiftReduce Parsing Bottomup parsing uses two actions: Shift ABC  xyz ABCx  yz Reduce Cbxy  ijk CbA  ijk Prof. Aiken CS 143 Lecture 8 2 Recall: 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 – pushes a nonterminal on the stack • production lhs Prof. Aiken CS 143 Lecture 8 3 Key Issue • How do we decide when to shift or reduce? • Example grammar: E T + E  T T int * T  int  (E) • 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 Prof. Aiken CS 143 Lecture 8 4 Handles • Intuition: Want to reduce only if the result can still be reduced to the start symbol • Assume a rightmost derivation S * X • Then is a handle of Prof. Aiken CS 143 Lecture 8 5 Handles (Cont.) • Handles formalize the intuition – A handle is a string that can be reduced and also allows further reductions back to the start symbol • We only want to reduce at handles • Note: We have said what a handle is, not how to find handles Prof. Aiken CS 143 Lecture 8 6 2 Important Fact #2 Important Fact #2 about bottomup parsing: In shiftreduce parsing, handles appear only at the top of the stack, never inside Prof. Aiken CS 143 Lecture 8 7 Why? • Informal induction on # of reduce moves: • True initially, stack is empty • Immediately after reducing a handle – rightmost nonterminal on top of the stack – next handle must be to right of rightmost non terminal, because this is a rightmost derivation – Sequence of shift moves reaches next handle Prof. Aiken CS 143 Lecture 8 8 Summary of Handles • In shiftreduce parsing, handles always appear at the top of the stack • Handles are never to the left of the rightmost nonterminal – Therefore, shiftreduce moves are sufficient; the  need never move left • Bottomup parsing algorithms are based on recognizing handles Prof. Aiken CS 143 Lecture 8 9 Recognizing Handles • There are no known efficient algorithms to recognize handles • Solution: use heuristics to guess which stacks are handles • On some CFGs, the heuristics always guess correctly – For the heuristics we use here, these are the SLR grammars – Other heuristics work for other grammars Prof. Aiken CS 143 Lecture 8 10 Grammars Prof. Aiken CS 143 Lecture 8 11 All CFGs Unambiguous CFGs SLR CFGs will generate conflicts Viable Prefixes • It is not obvious how to detect handles • At each step the parser sees only the stack, not the entire input; start with that . . ....
View
Full Document
 Fall '09
 Bottomup parsing, LR parser, Topdown parsing, Prof. Aiken CS

Click to edit the document details