1 / 17

Constructing canonical LR(1) Parsing Tables (Aho, Sethi, & Ullman p230)

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

lgarry
Download Presentation

Constructing canonical LR(1) Parsing Tables (Aho, Sethi, & Ullman p230)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Constructing canonical LR(1) Parsing Tables(Aho, Sethi, & Ullman p230) 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, $ }

  2. Constructing canonical LR Parsing Tables(Aho, Sethi, & Ullman p230) 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 }

  3. Constructing canonical LR Parsing Tables(Aho, Sethi, & Ullman p230) 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, $ }

  4. Constructing canonical LR Parsing Tables(Aho, Sethi, & Ullman p230) 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, $ }

  5. Constructing canonical LR Parsing Tables(Aho, Sethi, & Ullman p230) 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, $ }

  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. 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. 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. 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. Efficient Construction of LALR Parsing Tables(Aho, Sethi, & Ullman p240) 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) }

  11. Efficient Construction of LALR Parsing Tables(Aho, Sethi, & Ullman p240) • 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

  12. Efficient Construction of LALR Parsing Tables(Aho, Sethi, & Ullman p240) • 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

  13. Efficient Construction of LALR Parsing Tables(Aho, Sethi, & Ullman p240) 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

  14. Efficient Construction of LALR Parsing Tables(Aho, Sethi, & Ullman p240)

  15. Constructing canonical LR Parsing Tables(Aho, Sethi, & Ullman p230) 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

  16. Constructing canonical LR Parsing Tables(Aho, Sethi, & Ullman p230) 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

  17. Efficient Construction of LALR Parsing Tables(Aho, Sethi, & Ullman p240)

More Related