Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 8. LR Syntactic Analysis Sung-Dong Kim, Dept. of Computer Engineering, Hansung University.

Similar presentations


Presentation on theme: "Chapter 8. LR Syntactic Analysis Sung-Dong Kim, Dept. of Computer Engineering, Hansung University."— Presentation transcript:

1

2 Chapter 8. LR Syntactic Analysis Sung-Dong Kim, Dept. of Computer Engineering, Hansung University

3 Introduction  Deterministic bottom-up parsing  Left to right scanning + Right parse  LR parser (2012-1) Compiler 2

4 Introduction  Contents  How LR parser works  How to construct parsing table from a given grammar  How to construct parsing table from an ambiguous grammar  How to implement parser (2012-1) Compiler 3

5 1. LR parser (1)  LR parser (2012-1) Compiler 4 SmSm Parsing Table Driver Routine a1a1 aiai anan $ : input stack

6 1. LR parser (2)  Parsing stack  State number + grammar symbol  S 0 X 1 S 1 X 2 X m S m  S 0 X 1 S 1 X 2 X m S m, where S i : state and X i  V.  Input buffer  Input string  a i  a i a i+1 a n $ (2012-1) Compiler 5

7 1. LR parser (3)  LR parsing table  Row: grammar symbols  Column: states  ACTION: reduce, shift, accept, error  GOTO: indicate next state on reduce action (2012-1) Compiler 6 symbol states ACTION tableGOTO table

8 1. LR parser (4)  Example 1  Grammar: 1. LIST  LIST, ELEMENT 2. LIST  ELEMENT 3. ELEMENT  a  Parsing table (2012-1) Compiler 7

9 1. LR parser (5)  Definition 8.1: Configuration  4 actions of the LR parser  Shift: ACTION[S m, a i ] = shift S (2012-1) Compiler 8 Configuration: representation for the current parsing status : stack contents + un-read input symbols S m a i (S 0 X 1 S 1 X m S m, a i a i+1 a n $) stack contents unscanned input (S 0 X 1 S 1  X m S m, a i a i+1  a n $)  (S 0 X 1 S 1  X m S m a i S, a i+1  a n $)

10 1. LR parser (5)  Reduce: ACTION[S m, a i ] = reduce A  , |  | = r  Accept: ACTION[S m, a i ] = accept  Error: ACTION[S m, a i ] = error (2012-1) Compiler 9 (S 0 X 1 S 1  X m S m, a i a i+1  a n $)  (S 0 X 1 S 1  X m-r S m-r, a i a i+1  a n $), GOTO(S m-r, A) = S  (S 0 X 1 S 1  X m-r S m-r AS, a i a i+1  a n $)

11 1. LR parser (6)  Advantages of the LR parser  Recognize the most programming languages  More general than LL parser  Detect errors during parsing (2012-1) Compiler 10

12 1. LR parser (7)  Example 2: a, a (2012-1) Compiler 11 0a,a$ s3 0 a 3,a$ r3 GOTO 2 0 ELEMENT 2,a$ r2 GOTO 1 0 LIST 1,a$ s4 0 LIST 1, 4a$ s3 0 LIST 1, 4 a 3$ r3 GOTO 5 0 LIST 1, 4 ELEMENT 5$ r1 GOTO 1 0 LIST 1$ accept initial configuration STACK INPUT ACTION

13 1. LR parser (8)  Construction of the LR parsing table  SLR: LR(0) items + FOLLOW  CLR: LR(1) items  LALR: LR(1) items or LR(0) items + LOOKAHEAD (2012-1) Compiler 12 ` SLR CLR LALR

14 2. Set of LR(0) items (1)  Definition 8.2: LR(0) item  Example 3: A  XYZ  LR(0) items: [A .XYZ], [A  X.YZ], [A  XY.Z], [A  XYZ.]  Definition 8.3: Augmented production (2012-1) Compiler 13 LR(0) item: productions whose rhs has the dot symbol When G = (V N, V T, P, S), G’ = (V N  {S’}, V T, P  {S’  S}, S’) is augmented grammar S’: new start state S’  S: augmented production

15 2. Set of LR(0) items (2)  Definition 8.4: Mark symbol  Definition 8.5: kernel item, closure item, reduce item (2012-1) Compiler 14 A symbol next to the dot symbol in LR(0) item Kernel item: [A  .  ] when   , and [S’ .S] Closure item: [A .  ] Reduce item: [A  .]

16 2. Set of LR(0) items (3)  [A  .  ]  Until now, the input strings derivable from  are seen (processed)  If the strings derivable from  are seen (processed), we can reduce using A    Definition 8.6: viable prefix  Not handle  Symbols in the parsing stack (2012-1) Compiler 15  1 in S  *  A    1  2 

17 2. Set of LR(0) items (4)  Definition 8.7: valid  When parsing stack content is  1, choose shift or reduce   2   : shift  2 =  : reduce (2012-1) Compiler 16 If S  *  A    1  2  and   V T *, LR(0) item [A   1.  2] is valid for the viable prefix  1

18 2. Set of LR(0) items (5)  Set of valid LR(0) items for all viable prefix  LR parsing table  Add production S’  S  Get set of LR(0) items by marking the productions from the above production: same set  a state  Definition 8.8: CLOSURE, set of items (2012-1) Compiler 17 CLOSURE(I) = I  {[B .  ] | [A  .B  ]  CLOSURE(I), B    P}

19 2. Set of LR(0) items (6)  Algorithm for CLOSURE(I) (2012-1) Compiler 18 Algorithm CLOSURE(I); begin CLOSURE := I; repeat if [A  .B  ] and B    P then if [B .  ]  CLOSURE then CLOSURE := CLOSURE  {[B .  ]} fi fi until no change end

20 2. Set of LR(0) items (7)  Example 5:  CLOSURE: all LR(0) items to be seen on a state  GOTO: state transition (2012-1) Compiler 19 S’  G G  E = E | f E  E + T | T T  T * f | f CLOSURE({[S’ .G]}) = { [S’ .G], [G .E=E], [G .f] [E .E+T], [E .T], [T .T*f], [T .f] } CLOSURE({[E  E.+T]}) = { [E  E.+T] }

21 2. Set of LR(0) items (8)  Definition 8.9: GOTO function  Function to get the next state by parsing mark symbol (2012-1) Compiler 20 GOTO(I, X) = CLOSURE({[A   X.  ] | A  .X  ]  I})

22 2. Set of LR(0) items (9)  Example 6:  I = {[G  E.=E], [E  E. + T]} GOTO(I, +) = CLOSURE({[E  E+.T]}) = {[E  E+.T], [T .T*f], [T .f]}  I = {[E .T], [T .T*f], [T .f]} GOTO(I, T) = CLOSURE({[E  T.], [T  T.*f]}) = {[E  T.], [T  T.*f]} (2012-1) Compiler 21

23 2. Set of LR(0) items (10)  Canonical collection C 0  GOTO(I 0, X) = I 1  GOTO(I 1, X) = I 2  …  GOTO(I n-1, X) = I n  C 0 = {I 0, I 1, … I n } (2012-1) Compiler 22 C 0 = {CLOSURE({[S’ .S]})}  {GOTO(I,X) | I  C 0, X  V}

24 (2012-1) Compiler 23 Algorithm Canonical_Collection; begin C 0 := {CLOSURE({[S’ .S]})}; repeat for I  C0 do Closure := CLOSURE(I); for each X  MARK SYMBOL of Closure do J := GOTO(I, X); if  J i = J then GOTO(I, X) := J i else GOTO(I, X) := J; C 0 := C 0  {J}; fi end for until C 0 doesn’t change end.

25 2. Set of LR(0) items (12)  Example 7: C0 construction (2012-1) Compiler 24 LIST  LIST, ELEMENT LIST  ELEMENT ELEMENT  a ACCEPT  LIST LIST  LIST, ELEMENT LIST  ELEMENT ELEMENT  a I0: CLOSURE({[ACCEPT .LIST]}) = {[ACCEPT .LIST], [LIST .LIST,ELEMENT], [LIST .ELEMENT], [ELEMENT .a]} GOTO(I0, LIST) = I1 = {[ACCEPT  LIST.], [LIST  LIST., ELEMENT]} GOTO(I0, ELEMENT) = I2 = {[LIST  ELEMENT.]} GOTO(I0, a) = I3 = {[ELEMENT  a.]} GOTO(I1,,) = I4 = {[LIST  LIST,.ELEMENT], [ELEMENT .a]} GOTO(I4, ELEMENT) = I5 = {[LIST  LIST, ELEMENT.]} GOTO(I4, a) = I3

26 2. Set of LR(0) items (13)  GOTO graph (2012-1) Compiler 25 I1I1 I2I2 I3I3 I0I0 I4I4 I5I5 LIST ELEMENT a a,

27 2. Set of LR(0) items (14)  Example 8: (2012-1) Compiler 26 P  b D ; S e D  d ; D | d S  s ; S | s [P '  P.] I1I1 [P ' .P] [P .bD;Se] I0I0 [P  bD.;Se] I3I3 [P  bD;.Se] [S .s;S] [S .s] I5I5 [P  bD;S.e] I7I7 [P  bD;Se.] I8I8 [P  b.D;Se] [D .d;D] [D .d] I2I2 [S  s.;S] [S  s.] I8I8 [S .s;.S] [S .s;S] [S .s] I 11 [D  d.;D] [D  d.] I4I4 [D  d;.D] [D .d;D] [D .d] I6I6 [D  d;D.] I9I9 [S  s;S.] I 12 S e s S D ; ; P b D d d s ;

28 3. SLR parsing table construction (1)  SLR  simple LR  Parsing table construction by using C 0 and FOLLOW  Each item set in C 0 is a state: |C 0 |  (|V|+1) (2012-1) Compiler 27

29 3. SLR parsing table construction (2)  SLR parsing table construction  If [A  .a  ]  I i and GOTO(I i, a) = I j then M[i, a] := shift j  If [A  .]  I i then for each a  FOLLOW(A) do M[i, a] := reduce A    If [S’  S.]  I i then M[i, $] := accept  If [A  .B  ]  I i and GOTO(I i, B) = I j then M[i, B] := j;  other parsing table entries are error  A state including [S’  S] is a start state (2012-1) Compiler 28

30 3. SLR parsing table construction (3)  Feature  Reduce action on the FOLLOW symbol of the lhs of the reduce item production  Example 10: (2012-1) Compiler 29 E  E+T | T T  T*F | F F  (E) | id 0. E’  E 1.E  E+T 2.E  T 3.T  T*F 4.T  F 5.F  (E) 6.F  id FOLLOW(E) = {$, +, )} FOLLOW(T) = {*, +, ), $} FOLLOW(F) = {*, +, ), $}

31 (2012-1) Compiler 30

32 (2012-1) Compiler 31

33 3. SLR parsing table construction (4)  Shift-reduce conflict  [A  .], [B  .a  ]  If a  FOLLOW(A)?  Reduce-reduce conflict  [A  .], [B  .]  If a  FOLLOW(A) and a  FOLLOW(B)?  Reason of the conflict: ambiguous grammar (2012-1) Compiler 32

34 3. SLR parsing table construction (5)  Solution  Determine the reduce action using the exact context  CLR, LALR (2012-1) Compiler 33

35 4. CLR parsing table construction (1)  FOLLOW  Set of terminal symbols which can appear next to the nonterminal in all sentential forms  Lookahead  Set of terminal symbols which can appear next to the lhs of an item on the specific state (2012-1) Compiler 34 FOLLOW set Lookahead set

36 4. CLR parsing table construction (2)  LR(1) item: LR(0) item + lookahead information  Definition 8.10: LR(1) item (2012-1) Compiler 35 LR(1) item: [A  . , a], where A    P and a  V T  {$} 1.Core: A  .  2.a: lookahead, reduce action on this symbol

37 4. CLR parsing table construction (2)  Definition 8.11: CLOSURE in LR(1) item  Example 11: (2012-1) Compiler 36 CLOSURE(I) = I  {[B . , b] | [A  .B , a]  CLOSURE(I), B    P, b  FIRST(  a)} CLOSURE({[S’ .S, $]}) = {[S’ .S, $], [S .CC, $], [C .cC, c/d], [C .d, c/d]} S’  S S  CC C  cC C  d

38 4. CLR parsing table construction (3)  CLR parsing table construction  If [A  .a , u]  I i and GOTO(I i, a) = I j then M[i,a] := shift j  If [A  ., u]  I i then M[i, u] := reduce A    If [S’  S., $]  I i then M[i, $] := accept  If [A  .B , u]  I i and GOTO(I i, B) = I j then M[i, B] := j; (2012-1) Compiler 37

39 4. CLR parsing table construction (4)  Canonical LR(1) parsing table  LR(1) grammar: no multi-defined entry  Example 12: (2012-1) Compiler 38 0. S’  S 1.S  CC 2.C  cC 3.C  d S  CC C  cC | d

40 (2012-1) Compiler 39 I0I0 [S .S,$] [S .CC,$] [C .cC,c/d] [C .d,c/d] [C  d.,c/d.] I4I4 [S'  S.,$] I1I1 [C  cC.,c/d.] I8I8 [C  c.C,c/d] [C .cC,c/d] [C .d,c/d] I3I3 [C  c.C,$] [C .cC,$] [C .d,$] I6I6 [C  d.,$] I7I7 [S'  CC.,$] I5I5 [C  cC.,$] I9I9 d c d C S C C c d c d c C [S  C.C,$] [C .cC,$] [C .d,$] I2I2

41 (2012-1) Compiler 40


Download ppt "Chapter 8. LR Syntactic Analysis Sung-Dong Kim, Dept. of Computer Engineering, Hansung University."

Similar presentations


Ads by Google