This preview shows pages 1–5. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full 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 lookahead 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 lookahead symbol) LR(1) items [ , a ] (one token lookahead) LR(2) items [ , a b ] (two token lookahead) ... 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 lookahead symbols? Carry them along to choose correct reduction Lookahead symbols are for bookkeeping, unless item has at right end Has no direct use in [ , a ] In [ , a ], a lookahead 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 lookahead of * How did it choose to expand Term rather than reduce to Expr? Lookahead symbol is the key With lookahead of + or , parser should reduce to Expr With lookahead of * or / , parser should shift Parser uses lookahead to decide All this context from the grammar is encoded in the handle recognizing mechanism Highlevel 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
 Spring '08
 Ceniceros,H
 Numerical Analysis

Click to edit the document details