1 / 12

Compiler Construction LR(1)

Compiler Construction LR(1). Rina Zviel-Girshin and Ohad Shacham School of Computer Science Tel-Aviv University. LR Grammar Hierarchy. Non-ambiguous CFG. CLR(1). LALR(1). SLR(1). LR(0). Example. S’  S$ S  A | xb A  aAb | B B  x. S 0.

farica
Download Presentation

Compiler Construction LR(1)

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 ConstructionLR(1) Rina Zviel-Girshinand Ohad Shacham School of Computer Science Tel-Aviv University

  2. LR Grammar Hierarchy Non-ambiguous CFG CLR(1) LALR(1) SLR(1) LR(0)

  3. Example S’  S$ S  A | xb A  aAb | B B  x

  4. S0 S‘S$S AS xbA aAbA  BB  x S7 S4 S’  S$ S  A | xb A  aAb | B B  x B  x A  B B B S3 x A  aAbA aAbA BB x a a S1 A S  A A S6 S x A  aAb S2 S  xbB x S’  S$ b S9 b S8 T S5 S10 A  aAb S  xb S’  S$

  5. S0 S‘S$S AS xbA aAbA  BB  x SLR(1) • Calculate FOLLOW sets for each non terminal • Use the FOLLOW set to break conflicts x S2 S  xbB x b S5 FOLLOW(B) = {b,$} S  xb

  6. LR(1) • Keeps the specific look-ahead with each item • Use the look-ahead to break conflicts • More accurate than use the FOLLOW set

  7. LR(1) S7 S11 S4 B  x {b} A  B {$} A  B {b} B B x S0 S3 S12 x B S‘S$S A {$}S xb {$}A aAb {$}A  B {$}B  x {$} A  aAb {$}A aAb {b}A B {b}B x {b} A  aAb {b}A aAb {b}A B {b}B x {b} a a a A A S1 A S6 S x S13 S  A {$} A  aAb {$} A  aAb {b} S2 S  xb {$}B x {$} S’  S$ S9 b b b T S8 S14 S5 S10 A  aAb {$} A  aAb {b} S  xb {$} S’  S$

  8. LR(0) S0 S‘S$S AS xbA aAbA  BB  x S7 S4 S’  S$ S  A | xb A  aAb | B B  x B  x A  B B B S3 x A  aAbA aAbA BB x a a S1 A S  A A S6 S x A  aAb S2 S  xbB x S’  S$ b S9 b S8 T S5 S10 A  aAb S  xb S’  S$

  9. LR(1) S7 S11 S4 B  x {b} A  B {$} A  B {b} B B x S0 S3 S12 x B S‘S$S A {$}S xb {$}A aAb {$}A  B {$}B  x {$} A  aAb {$}A aAb {b}A B {b}B x {b} A  aAb {b}A aAb {b}A B {b}B x {b} a a a A A S1 A S6 S x S13 S  A {$} A  aAb {$} A  aAb {b} S2 S  xb {$}B x {$} S’  S$ S9 b b b T S8 S14 S5 S10 A  aAb {$} A  aAb {b} S  xb {$} S’  S$

  10. LALR(1) S7 S4 B  x {b} A  B {$,b} B B S3,12 S0 x B S‘S$S A {$}S xb {$}A aAb {$}A  B {$}B  x {$} A  aAb {$,b}A aAb {b}A B {b}B x {b} a a A A S1 S6,13 S x S  A {$} A  aAb {$,b} S2 S  xb {$}B x {$} S’  S$ S9 b b T S8,14 S5 S10 A  aAb {$,b} S  xb {$} S’  S$

  11. LALR(1) • Combines states with the same items • Much smaller parsing tables • May introduce reduce-reduce conflicts • Cannot introduce shift-reduce conflict S  AtB {$} C  x {ty}

  12. LR Grammar Hierarchy Non-ambiguous CFG CLR(1) LALR(1) SLR(1) LR(0)

More Related