Compiler SLR Parser LR Parsing Algorithm Items LR

  • Slides: 10
Download presentation
Compiler SLR Parser

Compiler SLR Parser

LR Parsing Algorithm

LR Parsing Algorithm

Items ● LR parser using SLR parsing table is called an SLR parser. ●

Items ● LR parser using SLR parsing table is called an SLR parser. ● A grammar for which an SLR parser can be constructed is an SLR grammar. ● An LR(0) item (item) of a grammar G is a production of G with a dot at the some position of the right side. ● Ex: A -> a. Bb Possible LR(0) Items: A ->. a. Bb (four different possibility) A -> a. Bb. ● Sets of LR(0) items will be the states of action and goto table of the SLR parser. ● A production rule of the form A yields only one item A. ● Intuitively, an item shows how much of a production we have seen till the current point in the parsing procedure.

Computation Of Closure function closure ( I ) begin J : = I; repeat

Computation Of Closure function closure ( I ) begin J : = I; repeat for each item A. B in J and each production B of G such that B. is not in J do add B. to J until no more items can be added to J return J end

Goto Operation ● If I is a set of LR(0) items and X is

Goto Operation ● If I is a set of LR(0) items and X is a grammar symbol (terminal or non-terminal), then goto(I, X) is defined as follows: ● If A -> a. Xb in I then every item in closure({A -> a. X. b}) will be in goto(I, X). ● Example: I ={ E’ E. , E E. +T} goto(I, +) = { E -> E+. T T ->. T*F T ->. F F ->. (E) F ->. id }

Actions of A LR-Parser 1. shift s -- shifts the next input symbol and

Actions of A LR-Parser 1. shift s -- shifts the next input symbol and the state s onto the stack(So X 1 S 1. . . Xm Sm, ai ai+1. . . an $ ) ( So X 1 S 1. . . Xm Sm ai s, ai+1. . . an $ ) 2. reduce A (or rn where n is a production number) ○ pop 2|| (=r) items from the stack; ○ then push A and s where s=goto[sm-r, A] ○ ( So X 1 S 1. . . Xm Sm, ai ai+1. . . an $ ) ( So X 1 S 1. . . Xm-r Sm-r A s, ai. . . an $ ) ○ Output the reducing production reduce A 3. Accept – If action[Sm , ai ] = accept , Parsing successfully completed 4. Error -- Parser detected an error (an empty entry in the action table) and calls an error recovery routine.

Actions (When dot is in middle) 1. if [A ‐‐> α. aβ] ε Ii

Actions (When dot is in middle) 1. if [A ‐‐> α. aβ] ε Ii and read on ‘a’ produces Ij then ACTION [i , a] = SHIFT j. 2. if [A ‐‐> α. Xβ] ε Ii and read on ‘X’ produces Ij then GOTO [i , X] = j. (When dot is at end) 3. if [A ‐‐> α. ] ε Ii then ACTION [i , a] = REDUCE on A ‐> α for all a ε FOLLOW(A). 4. if [S’ ‐‐> S. ] ε Ii then ACTION [i , $] = ACCEPT.

Example Productions: E' ->E E ->E + T E ->T T ->T * F

Example Productions: E' ->E E ->E + T E ->T T ->T * F T ->F F->(E) F -> id

States I 0: E’ ->. E . I 4 : goto(I 0, ( )

States I 0: E’ ->. E . I 4 : goto(I 0, ( ) E ->. E+T F -> (. E) I 7: goto(I 2 , *) E ->. T E->. E+T T -> T*. F T ->. T*F E ->. T T ->. F T ->. T*F F ->. (E) T ->. F F ->. id F ->. (E) F ->. id I 8: goto( I 4 , E) F ->. id F ->(E. ) I 1: goto(I 0, E) E ->E. +T E’ -> E. I 5: goto(I 0, id) E ->E. +T F -> id. I 9: goto(I 6, T) E -> E+T. T -> T. *F I 6: goto(I 1, +) I 2: goto(I 0, T) E -> E+. T I 10: goto(I 7, F) E -> T. T ->. T*F T -> T*F. T -> T. *F T ->. F I 11 : goto(I 8 , ) ) F ->. (E) F -> (E). I 3: goto(I 0, F) T -> F. F ->. id

Parsing Tables of Expression Grammar Action Table Goto Table

Parsing Tables of Expression Grammar Action Table Goto Table