Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "CS 488 Spring 2012 Lecture 4 Bapa Rao Cal State L.A."— Presentation transcript:

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

2

3

4 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 w can only have terminals

5

6

7

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

9

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)

12

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

16

17

18

19

20

21

22

23


Download ppt "CS 488 Spring 2012 Lecture 4 Bapa Rao Cal State L.A."

Similar presentations


Ads by Google