Presentation is loading. Please wait.

Presentation is loading. Please wait.

Compiler SLR Parser.

Similar presentations


Presentation on theme: "Compiler SLR Parser."— Presentation transcript:

1 Compiler SLR Parser

2 LR Parsing Algorithm

3 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 -> aBb Possible LR(0) Items: A -> .aBb (four different possibility) A -> a.Bb A -> aB.b A -> aBb. 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.

4 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

5 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 -> aX.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 }

6 Actions of A LR-Parser shift s -- shifts the next input symbol and the state s onto the stack(So X1 S1 ... Xm Sm, ai ai an $ ) ( So X1 S1 ... Xm Sm ai s, ai an $ ) 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 X1 S1 ... Xm Sm, ai ai an $ ) ( So X1 S1 ... 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.

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

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

9 States I0: E’ -> .E . I4 : goto(I0, ( )
E ->.E+T F -> (.E) I7: goto(I2 , *) E ->.T E-> .E+T T -> T*.F T ->.T*F E ->.T F ->.(E) T ->.F T -> .T*F F ->.id F ->.(E) T -> .F F ->.id F -> .(E) I8: goto( I4 , E) F -> .id F ->(E.) I1: goto(I0, E) E ->E.+T E’ -> E. I5: goto(I0, id) I9:goto(I6, T) E ->E.+T F -> id E -> E+T. T -> T.*F I6: goto(I1, +) I2: goto(I0, T) E -> E+.T I10: goto(I7, F) E -> T. T ->.T*F T -> T*F. T -> T.*F T ->.F I11 : goto(I8 , ) ) I3: goto(I0, F) F ->.(E) F -> (E). T -> F. F -> .id

10 Parsing Tables of Expression Grammar
Action Table Goto Table


Download ppt "Compiler SLR Parser."

Similar presentations


Ads by Google