1 / 16

Compiler Theory – 08/10( ppt )

Compiler Theory – 08/10( ppt ). By Anindhya Sankhla 11CS30004 Group : G 29. Canonical LR(1) items. The canonical collection of LR(1) items: set of items derivable from [ S’ ::= S; eof ] set of all items that can derive the final configuration

calum
Download Presentation

Compiler Theory – 08/10( ppt )

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. Compiler Theory – 08/10(ppt) By Anindhya Sankhla 11CS30004 Group : G 29

  2. Canonical LR(1) items • The canonical collection of LR(1) items: • set of items derivable from [S’ ::= S; eof] • set of all items that can derive the final configuration • Speak in a normal tone of voice, and listen attentively. • Essentially • each set in the canonical collection of sets of LR(1) items represents a state in an NFA that recognizes viable prefixes. • Grouping together is really the subset construction.

  3. Canonical LR(1) items • To construct the canonical collection we need two functions : • closure (I ) • goto(I , X)

  4. LR(1) closure Given an item [A ::= α Bβ,a], its closure contains the item and any other items that can generate legal substrings to follow α. Thus, if the parser has viable prefix αon its stack, the input should reduce to B β (or γfor some other item [B ::= γb] in the closure).

  5. To compute closure(I) function closure(I) repeat new_item←false for each item [A ::= α Bβ,a] ϵ I, each production B ::= γϵG’, and each terminal bϵ FIRST(βa), if [B ::= γ,b] Ɇ I then add [B ::= γ,b] to I new_item←true endif until (new_item= false) return I .

  6. LR(1) goto • Let I be a set of LR(1) items and X be a grammar symbol. • Then, goto(I,X) is the closure of the set of all items [A ::= αX β, a] such that [A ::= α Xβ,a] ϵI • If I is the set of valid items for some viable prefix γ, then goto(I,X) is the set of valid items for the viable prefix γX. • goto(I,X) represents state after recognizing X in state I .

  7. To compute goto(I,X) function goto(I,X) J ← set of items [A ::= αX β, a] such that [A ::= α Xβ,a] ϵI J’ ← closure(J) return J’

  8. Collection of sets of LR(1) items • We start the construction of the collection of sets of LR(1) items with the item [S’ ::= S,eof], where S’ is the start symbol of the augmented grammar G’ S is the start symbol of G, and eofis the right end of string marker

  9. To compute the collection of sets of LR(1) items procedure items(G’) C ← {closure( {[S’ ::= S; eof]} )} repeat new_item←false for each set of items I in C and each grammar symbol X such that goto(I , X) ≠ϕand goto(I ,X) Ɇ C add goto(I , X) to C new_item ← true endfor until (new_item= false)

  10. LR(1) table construction • The Algorithm: • construct the collection of sets of LR(1) items for G’. • State i of the parser is constructed from Ii . • (a) if [A ::= α aβ, b] ϵ Ii and goto(Ii,a) = Ij, then set action[i, a] to “shift j”. (a must be a terminal) (b) if [A ::= α, a] ϵ Ii, then set action[i, a] to “reduce A ::= α”. (c) if [S’ ::= S , eof] ϵ Ii , then set action[i,eof] to “accept”. 3. If goto(Ii, A) = Ij, then set goto[i, A] to j. 4. All other entries in action and goto are set to “error” 5. The initial state of the parser is the state constructed from the set containing the item [S’ ::= S; eof].

  11. Example : The Grammar • goal ::= expr • expr::= term + expr • |term • term ::= factor * term • | factor • factor ::= id

  12. Example: ACTION and GOTO Table

  13. Example • Step 1 I0 ← {[g ::= e, eof ]} I0← closure(I0) { [g ::= e, eof], [e ::= t + e, eof], [e ::= t, eof], [t ::= f * t, +], [t ::= f * t, eof], [t ::= f, +], [t ::= f, eof], [f ::= id, +], [f ::= id, eof] }

  14. Example • Iteration 1 I1 ← goto(I0, e) I2 ← goto(I0, t) I3 ← goto(I0, f) I4 ← goto(I0, id) • Iteration 2 I5← goto(I2 ,+) I6← goto(I3 ,*) • Iteration 3 I7← goto(I5, e) I8← goto(I6, t)

  15. Example • I0: [g ::= e, eof], [e ::= t + e, eof], [e ::= t, eof], [t ::= f * t, {+, eof}], [t ::= f, {+, eof}], [f ::= id, {+, eof}] • I1: [g ::= e , eof] • I2: [e ::= t , eof], [e ::= t + e, eof] • I3: [t ::= f , {+, eof}], [t ::= f * t, {+, eof}] • I4: [f ::= id , {+, *, eof}] • I5: [e ::= t + e, eof], [e ::= t + e, eof], [e ::= t, eof], [t ::= f * t, {+, eof}], [t ::= f, {+, eof}], [f ::= id, {+, *, eof}] • I6: [t ::= f * t, {+, eof}], [t ::= f * t,{+, eof}], [t ::= f, {+, eof}], [f ::= id, {+, *, eof}] • I7: [e ::= t + e , eof] • I8: [t ::= f * t , {+, eof}]

  16. Thank You FOR YOUR ATTENTION !

More Related