cs160-lec10

cs160-lec10 - 1 CMPSC 160 Translation of Programming...

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 1 CMPSC 160 Translation of Programming Languages Lecture 10: LR(1) Parsing Building LR(1) Handle Recognizing Machine In the last lecture we discussed how to build the LR(0) handle recognizing machine which did not use a lookahead symbol Now we will learn the LR(1) algorithm which uses one token lookahead 2 LR( k ) items An LR( k ) item is a pair [ A , B ], where A is a production with a at some position in the rhs B is a look-ahead string of length k (terminal symbols or $) Examples: [ , a], [ , a], [ , a], & [ , a] The in an item indicates the position of the top of the stack LR(0) items [ ] (no look-ahead symbol) LR(1) items [ , a ] (one token look-ahead) LR(2) items [ , a b ] (two token look-ahead) ... LR( k ) items The in an item indicates the position of the top of the stack [ , a ] means that the input seen so far is consistent with the use of immediately after the symbol on top of the stack [ , a ] means that the input seen so far is consistent with the use of at this point in the parse, and that the parser has already recognized . [ , a ] means that the parser has seen , and the lookahead a is consistent with reducing to (for LR(k) parsers, a is a string of terminal symbols of length k) The table construction algorithm uses LR(1) items to represent valid configurations of an LR(1) parser 3 LR(1) Items The production , with lookahead a , generates 4 items [ , a ], [ , a ], [ , a ], & [ , a ] The set of LR(1) items for a grammar is finite Whats the point of all these look-ahead symbols? Carry them along to choose correct reduction Look-ahead symbols are for bookkeeping, unless item has at right end Has no direct use in [ , a ] In [ , a ], a look-ahead of a implies a reduction by For { [ , a ],[ , b ] } lookahead = a reduce to lookahead FIRST( ) shift Limited right context is enough to pick the actions Back to x - 2 * y 1. Shift until TOS is the right end of a handle 2. Find the left end of the handle & reduce shift here reduce here 4 Back to Finding Handles Parser is in a state where the stack (the fringe) was Expr Term With look-ahead of * How did it choose to expand Term rather than reduce to Expr? Look-ahead symbol is the key With look-ahead of + or , parser should reduce to Expr With look-ahead of * or / , parser should shift Parser uses look-ahead to decide All this context from the grammar is encoded in the handle recognizing mechanism High-level overview 1 Build the handle recognizing DFA (aka Canonical Collection of sets of LR(1) items), C = { I , I 1 , ... , I n } a) Introduce a new start symbol...
View Full Document

Page1 / 14

cs160-lec10 - 1 CMPSC 160 Translation of Programming...

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online