CS 488 Spring 2012 Lecture 4

23
CS 488 Spring 2012 Lecture 4 Bapa Rao Cal State L.A.

description

CS 488 Spring 2012 Lecture 4. Bapa Rao Cal State L.A. Reverse rightmost derivation. E TT*FT* idF * idid *id Reduction Replace RHS of production with LHS Handle Substring (terms and non-terms) that matches the body of a production - PowerPoint PPT Presentation

Transcript of CS 488 Spring 2012 Lecture 4

Page 1: CS 488  Spring 2012 Lecture 4

CS 488 Spring 2012

Lecture 4

Bapa RaoCal State L.A.

Page 2: CS 488  Spring 2012 Lecture 4
Page 3: CS 488  Spring 2012 Lecture 4
Page 4: CS 488  Spring 2012 Lecture 4

Reverse rightmost derivation• ETT*FT*idF*idid*id• Reduction

– Replace RHS of production with LHS• Handle

– Substring (terms and non-terms) that matches the body of a production

– Reduction by handle represents one step along a reverse rightmost derivation

• “a handle” vs “the handle”– Ambiguity!– >1 rightmost derivation of αβw– w can only have terminals

Page 5: CS 488  Spring 2012 Lecture 4
Page 6: CS 488  Spring 2012 Lecture 4
Page 7: CS 488  Spring 2012 Lecture 4
Page 8: CS 488  Spring 2012 Lecture 4

Four possible actions of a shift-reduce parser

• Shift– Token on to top of stack

• Reduce– Handle to non-terminal on right of production– Handle always on top of stack– Never need to go “inside” stack to find handle

• Accept• Error

Page 9: CS 488  Spring 2012 Lecture 4
Page 10: CS 488  Spring 2012 Lecture 4

• Case 1: expand A via nonterminal B• Case 2: expand A and B directly to terminal

• In both cases, handle on top of stack

Page 11: CS 488  Spring 2012 Lecture 4

Shift-reduce conflicts• Dangling-else

Stmt if expr then stmt | if expr then stmt else stmt | otherSTACK has … if expr then stmtINPUT has else … $Should you shift the else or reduce?

• Array references and procedure calls have same syntas– P(a,b)– A(I,j)

Page 12: CS 488  Spring 2012 Lecture 4
Page 13: CS 488  Spring 2012 Lecture 4

Simple LR parsing

• Can handle nearly all programming languages• Most general method of nonbacktracking

parsing, still efficient• Earliest syntax error detection• Proper superset of LL parsable grammars

• Hard to construct by hand

Page 14: CS 488  Spring 2012 Lecture 4

Items and Closure• LR(0) item is a production of grammar with a dot somewhere on RHS

– How much have you seen on RHS• Canonical LR(0) collection• Build a DFSA: LR(0) automaton• States represent set of items• Augmented Grammar

– S’ S• Closure is Recursive-transitive closure of an item from the grammar• GOTO transition function• Kernel and non-kernel items

– Kernel: S’ .S and all items whose dots are not at left end– Non-kernel: all items with dots at left end except for S’ S

Page 15: CS 488  Spring 2012 Lecture 4

• GOTO transition function– GOTO(Item,GrammarSymbol) Closure of set of

all items such GrammarSymbol is reduced

• Kernel and non-kernel items– Kernel: S’ .S and all items whose dots are not at

left end– Non-kernel: all items with dots at left end except

for S’ S

Page 16: CS 488  Spring 2012 Lecture 4
Page 17: CS 488  Spring 2012 Lecture 4
Page 18: CS 488  Spring 2012 Lecture 4
Page 19: CS 488  Spring 2012 Lecture 4
Page 20: CS 488  Spring 2012 Lecture 4
Page 21: CS 488  Spring 2012 Lecture 4
Page 22: CS 488  Spring 2012 Lecture 4
Page 23: CS 488  Spring 2012 Lecture 4