lecture08

# lecture08 - 1 Bottom-Up Parsing II Lecture 8 Prof Aiken CS...

This preview shows pages 1–3. Sign up to view the full content.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 1 Bottom-Up Parsing II Lecture 8 Prof. Aiken CS 143 Lecture 8 1 Review: Shift-Reduce Parsing Bottom-up 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 non-terminal 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 bottom-up parsing: In shift-reduce 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 – right-most non-terminal on top of the stack – next handle must be to right of right-most non- terminal, because this is a right-most derivation – Sequence of shift moves reaches next handle Prof. Aiken CS 143 Lecture 8 8 Summary of Handles • In shift-reduce parsing, handles always appear at the top of the stack • Handles are never to the left of the rightmost non-terminal – Therefore, shift-reduce moves are sufficient; the | need never move left • Bottom-up 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

{[ snackBarMessage ]}

### Page1 / 15

lecture08 - 1 Bottom-Up Parsing II Lecture 8 Prof Aiken CS...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online