Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Example 4.42. S'  S, S  CC, C  cC, C  d State 0. Closure({[S'  S, $]}) = { S'  S, $ S   CC, FIRST( $)  S   CC, $ C   cC, FIRST(C$)  C.

Similar presentations


Presentation on theme: "1 Example 4.42. S'  S, S  CC, C  cC, C  d State 0. Closure({[S'  S, $]}) = { S'  S, $ S   CC, FIRST( $)  S   CC, $ C   cC, FIRST(C$)  C."— Presentation transcript:

1 1 Example 4.42. S'  S, S  CC, C  cC, C  d State 0. Closure({[S'  S, $]}) = { S'  S, $ S   CC, FIRST( $)  S   CC, $ C   cC, FIRST(C$)  C   cC, c / d C   d, FIRST(C$)  C   d, c / d } State 1. Closure({[S'  S , $]}) = {S'  S , $ } Constructing canonical LR(1) Parsing Tables (Aho, Sethi, & Ullman p230)

2 2 State 2. Closure({[S  C  C, $]}) = { S  C  C, $ C   cC, FIRST( $)  C   cC, $ C   d, FIRST( $)  C   d, $ } State 3. Closure({[C  c  C, c/d]}) = {C  c  C, c/d C   cC, FIRST( c/d)  C   cC, c/d C   d, FIRST( c/d)  C   d, c/d } Constructing canonical LR Parsing Tables (Aho, Sethi, & Ullman p230)

3 3 State 4. Closure({[C  d, c/d]}) = {C  d , c/d} State 5. Closure({[S  CC , $]}) = {S  CC , $} State 6. Closure({[C  c  C, $]}) = { C  c  C, $ C   cC, FIRST( $)  C   cC, $ C   d, FIRST( $)  C   d, $ } Constructing canonical LR Parsing Tables (Aho, Sethi, & Ullman p230)

4 4 State 7. Closure({[C  d , $]}) = {C  d , $} State 8. Closure({[C  cC , c/d]}) = {C  cC , c/d } State 9. Closure({[C  cC , $]}) = {C  cC , $ } Constructing canonical LR Parsing Tables (Aho, Sethi, & Ullman p230)

5 5 State 0. {S'  S, $; S  CC, $; C   cC, c/d; C  d, c/d} State 1. {S'  S , $ } State 2. { S  C  C, $; C   cC, $; C   d, $ } State 3. {C  c  C, c/d; C   cC, c/d; C   d, c/d} State 4. {C  d , c/d} State 5. {S  CC , $} State 6. { C  c  C, $; C   cC, $; C   d, $ } State 7. {C  d , $} State 8. {C  cC , c/d } State 9. {C  cC , $ } Constructing canonical LR Parsing Tables (Aho, Sethi, & Ullman p230)

6 6 Efficient Construction of LALR Parsing Tables(Aho, Sethi, & Ullman p240) 1. Kernel set: Represent a set of items I by its kernel, i.e. by those items that are either the initial item [S'  S, $], or that have the dot somewhere other than at the beginning of the right side. 2. Parsig actions: Any item calling for a reduction by A  will be in the kernel unless  = . Reduction by A   is called for on input a iff there is a kernel item [B  C , b] such that C ⇒ *A  for some , and a is in FIRST(  b).

7 7 Efficient Construction of LALR Parsing Tables(Aho, Sethi, & Ullman p240) 3. Shift actions: The shift actions generated by I can be determined from the kernel of I as follows: We shift on input a if there is a kernel item [B  C , b] where C ⇒ * ax in derivation in which the last step does not use an  -production. 4. Goto transition: If [B  X , b] is in the kernel of I, then [B  X , b] is in the kernel of goto(I, X). Item [A  X , a] is also in the kernel of goto(I, X) if there is an item [B  C , b] in the kernel of I, and C ⇒ *A  for some .

8 8 Efficient Construction of LALR Parsing Tables(Aho, Sethi, & Ullman p240) 5. Expand the proper lookahead symbols to kernels Spontaneously: Consider item [B  C , b] in the kernel of I. Suppose C ⇒ *A  for some , and A  X  is a production. Then A  X  is in goto(I, X). The lookahead symbols for A  X  are the set of FIRST(  ). By definition, $ is generated spontaneously as a lookahead for the item S'  S in the initial set of items.

9 9 Efficient Construction of LALR Parsing Tables(Aho, Sethi, & Ullman p240) 5. Expand the proper lookahead symbols to kernels Propagate: Another source of lookaheads for item A  X , if  ⇒ * , then the set b is also the lookaheaks of A  X . (B  C, C  A, FOLLOW(A)  FOLLOW(C)  FOLLOW(B))

10 10 Algorithm 4.12 Determining lookaheads Input. The kernel K of a set of LR(0) items I and a grammar symbol X Output. The lookaheads: propagated and spontaneously generated Method. for each item B  in K { J' = closure({[B , #]}); if [A  X , a] is in J' where a is not # then lookahead a is generated spontaneously for item A  X  in goto(I, X) if [A  X , #] is in J' then lookaheads propagate from B  in I to A  X  in goto(I, X) } Efficient Construction of LALR Parsing Tables(Aho, Sethi, & Ullman p240)

11 11 Example. S'  S, S  L = R | R,L  *R | id, R  L The Kernels of the sets of LR(0) items are: 0. S'   S 6. S  L=  R 1. S'  S  7. L  *R  2. S  L  =R 8. R  L  R  L  9. S  L=R  3. S  R  4. L  *  R 5. L  id  Efficient Construction of LALR Parsing Tables(Aho, Sethi, & Ullman p240)

12 12 Example. S'  S, S  L = R | R, L  *R | id, R  L 0. S'   S, S   L=R, S  R, L  *R, L  id, R  L 1. S'  S  2. S  L  =R, R  L  3. S  R  4. L  *  R 5. L  id  6. S  L=  R 7. L  *R  8. S  L=R  Efficient Construction of LALR Parsing Tables(Aho, Sethi, & Ullman p240)

13 13 Propagation of lookaheads (by algorithm 4.12) 0. S'   S on S 1. S'  S  on L 2. S  L  =R, R  L  on R 3. S  R  on * 4. L  *  R on id 5. L  id  2. S  L  =R on = 6. S  L=  R 6. S  L=  R on R 9. S  L=R  on L 8. R  L  on * 4. L  *  R on id 5. L  id  4. L  *  R on R 7. L  *R  on L 8. R  L  on id 5. L  id  on * 4. L  *  R Efficient Construction of LALR Parsing Tables(Aho, Sethi, & Ullman p240)

14 14 Efficient Construction of LALR Parsing Tables(Aho, Sethi, & Ullman p240) Computation of lookaheads: itemInitPass 1Pass 2 Pass3 0. S'   S $ Default$$$ 1. S'  S  $$$ 2. S  C  C $$$ 3. S  R  $$$ 4. L  *  R = spontaneous$ / = 5. L  id  = spontaneous$ / = 6. S  L=  R $$ 7. L  *R  =$ / = 8. R  L  =$ / = 9. S  L=R  $

15 15 Example 4.42. S'  S, S  CC, C  cC, C  d Kernels: 0. S'  S 1. S'  S  2. S  C  C 3. C  c  C 4. C  d  5. S  CC  6. C  cC  Constructing canonical LR Parsing Tables (Aho, Sethi, & Ullman p230)

16 16 Propagation of lookaheads (by algorithm 4.12) 0. S'  S on S 1. S'  S  on C 2. S  C  C on c 3. C  c  C on d 4. C  d  2. S  C  C on C 5. S  CC  3. C  c  C on C 6. C  cC  Constructing canonical LR Parsing Tables (Aho, Sethi, & Ullman p230)

17 17 Efficient Construction of LALR Parsing Tables(Aho, Sethi, & Ullman p240) Computation of lookaheads: itemInitPass 1Pass 2 Pass3 0. S'   S $ Default$$ 1. S'  S  $$ 2. S  C  C $$ 3. C  c  C c/d spontaneousc/d/$ 4. C  d  c/d spontaneousc/d/$ 5. S  CC  $ 6. C  cC  c/d spontaneousc/d/$


Download ppt "1 Example 4.42. S'  S, S  CC, C  cC, C  d State 0. Closure({[S'  S, $]}) = { S'  S, $ S   CC, FIRST( $)  S   CC, $ C   cC, FIRST(C$)  C."

Similar presentations


Ads by Google