1 / 23

CS 488 Spring 2012 Lecture 4

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

garin
Télécharger la présentation

CS 488 Spring 2012 Lecture 4

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. 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 • 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

  3. 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

  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

  5. 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)

  6. 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

  7. 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

  8. 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

More Related