130 likes | 438 Views
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.
E N D
Compiler ConstructionLR(1) Rina Zviel-Girshinand 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
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 aAbA aAbA BB x a a S1 A S A A S6 S x A aAb S2 S xbB x S’ S$ b S9 b S8 T S5 S10 A aAb S xb S’ S$
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 xbB x b S5 FOLLOW(B) = {b,$} S xb
LR(1) • Keeps the specific look-ahead with each item • Use the look-ahead to break conflicts • More accurate than use the FOLLOW set
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 aAb {$}A aAb {b}A B {b}B x {b} A aAb {b}A aAb {b}A B {b}B x {b} a a a A A S1 A S6 S x S13 S A {$} A aAb {$} A aAb {b} S2 S xb {$}B x {$} S’ S$ S9 b b b T S8 S14 S5 S10 A aAb {$} A aAb {b} S xb {$} S’ S$
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 aAbA aAbA BB x a a S1 A S A A S6 S x A aAb S2 S xbB x S’ S$ b S9 b S8 T S5 S10 A aAb S xb S’ S$
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 aAb {$}A aAb {b}A B {b}B x {b} A aAb {b}A aAb {b}A B {b}B x {b} a a a A A S1 A S6 S x S13 S A {$} A aAb {$} A aAb {b} S2 S xb {$}B x {$} S’ S$ S9 b b b T S8 S14 S5 S10 A aAb {$} A aAb {b} S xb {$} S’ S$
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 aAb {$,b}A aAb {b}A B {b}B x {b} a a A A S1 S6,13 S x S A {$} A aAb {$,b} S2 S xb {$}B x {$} S’ S$ S9 b b T S8,14 S5 S10 A aAb {$,b} S xb {$} S’ S$
LALR(1) • Combines states with the same items • Much smaller parsing tables • May introduce reduce-reduce conflicts • Cannot introduce shift-reduce conflict S AtB {$} C x {ty}
LR Grammar Hierarchy Non-ambiguous CFG CLR(1) LALR(1) SLR(1) LR(0)