Download presentation
Presentation is loading. Please wait.
Published byAlexis Skinner Modified over 6 years ago
1
CS 488 Spring 2012 Lecture 4 Bapa Rao Cal State L.A.
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
8
Four possible actions of a shift-reduce parser
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
10
Case 1: expand A via nonterminal B
Case 2: expand A and B directly to terminal In both cases, handle on top of stack
11
Shift-reduce conflicts
Dangling-else Stmt if expr then stmt | if expr then stmt else stmt | other STACK has … if expr then 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)
13
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
14
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
15
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.