# lecture08 - CS 143 Lecture 8 1 Bottom-Up Par sing I I...

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

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

View Full Document

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

View Full Document

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

View Full Document

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

View Full Document

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

View Full Document

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

View Full Document

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: CS 143 Lecture 8 1 Bottom-Up Par sing I I Lecture 8 CS 143 Lecture 8 2 Review: Shift-Reduce Par sing Bottom-up parsing uses two actions: Shift ABC | xyz ⇒ ABCx | yz Reduce Cbxy | ijk ⇒ CbA | ijk CS 143 Lecture 8 3 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 CS 143 Lecture 8 4 Key I ssue • 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 CS 143 Lecture 8 5 H andles • I ntuition: 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 αβϖ CS 143 Lecture 8 6 H andles (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 CS 143 Lecture 8 7 I mpor tant Fact #2 I mportant Fact #2 about bottom-up parsing: I n shift-reduce parsing, handles appear only at the top of the stack, never inside CS 143 Lecture 8 8 Why? • I nformal induction on # of reduce moves: • True initially, stack is empty • I mmediately 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 CS 143 Lecture 8 9 Summar y of H andles • I n 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 CS 143 Lecture 8 10 Conflicts Again • Generic shift-reduce strategy: – I f there is a handle on top of the stack, reduce – Otherwise, shift • But what if there is a choice? – I f it is legal to shift or reduce, there is a shift-reduce conflict – I f it is legal to reduce by two different productions, there is a reduce-reduce conflict CS 143 Lecture 8 11 Sour ce of Conflicts • Ambiguous grammars always cause conflicts • But beware, so do many non-ambiguous grammars CS 143 Lecture 8 12 Conflict Example Consider our favorite ambiguous grammar: E → E + E | E * E | (E) | int CS 143 Lecture 8 13 One Shift-Reduce Par se | int * int + int shift . . . . . ....
View Full Document

{[ snackBarMessage ]}

### Page1 / 90

lecture08 - CS 143 Lecture 8 1 Bottom-Up Par sing I I...

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

View Full Document
Ask a homework question - tutors are online