cs160-lec9 - CMPSC 160 Translation of Programming Languages...

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

View Full Document Right Arrow Icon
CMPSC 160 Translation of Programming Languages Lecture 9: LR(1) Parsing
Background image of page 1

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

View Full DocumentRight Arrow Icon
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
Background image of page 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) . ..
Background image of page 3

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

View Full DocumentRight Arrow Icon
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
Background image of page 4
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 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
Background image of page 5

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

View Full DocumentRight Arrow Icon
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
Background image of page 6
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
Background image of page 7

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

View Full DocumentRight Arrow Icon
High-level overview 1 Build the handle recognizing DFA (aka Canonical Collection of sets of LR(1) items), C = { I 0 , I 1 , . .. , I n } a) Introduce a new start symbol S’ which has only one production S’ S b) Initial state, I 0 should include • [ S’ S , $], along with any equivalent items • Derive equivalent items as closure( I 0 ) c) Repeatedly compute, for each I
Background image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/19/2012 for the course ENGR 361 taught by Professor Drexel during the Spring '12 term at Bloomsburg.

Page1 / 27

cs160-lec9 - CMPSC 160 Translation of Programming Languages...

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

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