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 What’s 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
This note was uploaded on 11/23/2010 for the course MATH 104b taught by Professor Ceniceros,h during the Spring '08 term at UCSB.
 Spring '08
 Ceniceros,H
 Numerical Analysis

Click to edit the document details