170 likes | 186 Views
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
E N D
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, $ }
Constructing canonical LR Parsing Tables(Aho, Sethi, & Ullman p230) State 2. Closure({[S CC, $]}) = { S CC, $ C cC, FIRST($) C cC, $ C d, FIRST($) C d, $ } State 3. Closure({[C cC , c/d]}) = {C cC , 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) State 4. Closure({[C d, c/d]}) = {C d, c/d} State 5. Closure({[S CC, $]}) = {S CC, $} State 6. Closure({[C cC, $]}) = { C cC, $ C cC, FIRST($) C cC, $ C d, FIRST($) C d, $ }
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, $ }
Constructing canonical LR Parsing Tables(Aho, Sethi, & Ullman p230) State 0. {S'S, $; SCC, $; C cC, c/d; Cd, c/d} State 1. {S' S, $ } State 2. { S CC, $; C cC, $; C d, $ } State 3. {C cC, c/d; C cC, c/d; C d, c/d} State 4. {C d, c/d} State 5. {S CC, $} State 6. { C cC, $; C cC, $; C d, $ } State 7. {C d, $} State 8. {C cC, c/d } State 9. {C cC, $ }
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).
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 .
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.
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))
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) }
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
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, Lid, RL 1. S' S 2. S L=R, RL 3. S R 4. L *R 5. Lid 6. S L=R 7. L *R 8. S L=R
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
Efficient Construction of LALR Parsing Tables(Aho, Sethi, & Ullman p240)
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 CC 3. C cC 4. C d 5. S CC 6. C cC
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 CC on c 3. C cC on d 4. C d 2. S CC on C 5. S CC 3. C cC on C 6. C cC
Efficient Construction of LALR Parsing Tables(Aho, Sethi, & Ullman p240)