230 likes | 309 Views
CS 488 Spring 2012 Lecture 4. Bapa Rao Cal State L.A. Reverse rightmost derivation. E TT*FT* idF * idid *id Reduction Replace RHS of production with LHS Handle Substring (terms and non-terms) that matches the body of a production
E N D
CS 488 Spring 2012Lecture 4 Bapa Rao Cal State L.A.
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 • wcan only have terminals
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
Case 1: expand A via nonterminal B • Case 2: expand A and B directly to terminal • In both cases, handle on top of stack
Shift-reduce conflicts • Dangling-else Stmt ifexprthen stmt | ifexprthen stmt else stmt | other STACK has … if exprthen stmt INPUT has else … $ Should you shift the else or reduce? • Array references and procedure calls have same syntas • P(a,b) • A(I,j)
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
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
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