# Compiler Construction LR(1) Rina Zviel-Girshin and Ohad Shacham School of Computer Science Tel-Aviv University.

## Presentation on theme: "Compiler Construction LR(1) Rina Zviel-Girshin and Ohad Shacham School of Computer Science Tel-Aviv University."— Presentation transcript:

Compiler Construction LR(1) Rina Zviel-Girshin and 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 S’  S\$ S  A | xb A  aAb | B B  x Example

4 A  B  A  a  Ab A   aAb A   B B   x S  A  S  x  b B  x  A  aA  b S  xb  A  aAb  S4S4 S3S3 S1S1 S2S2 S5S5 S8S8 S6S6 B B a A b A x b S‘   S\$ S   A S   xb A   aAb A   B B   x S0S0 S’  S\$ S  A | xb A  aAb | B B  x S’  S  \$ S’  S\$  S 10 S9S9 T S a B  x  S7S7 x

5 SLR(1) Calculate FOLLOW sets for each non terminal Use the FOLLOW set to break conflicts S  x  b B  x  S  xb  S2S2 S5S5 x b S‘   S\$ S   A S   xb A   aAb A   B B   x S0S0 FOLLOW(B) = {b,\$}

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 A  B  {\$} A  a  Ab {\$} A   aAb {b} A   B {b} B   x {b} S  A  {\$} S  x  b {\$} B  x  {\$} A  aA  b {\$} S  xb  {\$} A  aAb  {\$} S4S4 S3S3 S1S1 S2S2 S5S5 S8S8 S6S6 B B a A b A x b S‘   S\$ S   A {\$} S   xb {\$} A   aAb {\$} A   B {\$} B   x {\$} S0S0 S’  S  \$ S’  S\$  S 10 S9S9 T S A  B  {b} S 11 A  a  Ab {b} A   aAb {b} A   B {b} B   x {b} A  aA  b {b} A  aAb  {b} S 14 S 13 b A B  x  {b} S7S7 x x B a a S 12 LR(1)

8 A  B  A  a  Ab A   aAb A   B B   x S  A  S  x  b B  x  A  aA  b S  xb  A  aAb  S4S4 S3S3 S1S1 S2S2 S5S5 S8S8 S6S6 B B a A b A x b S‘   S\$ S   A S   xb A   aAb A   B B   x S0S0 S’  S\$ S  A | xb A  aAb | B B  x S’  S  \$ S’  S\$  S 10 S9S9 T S a B  x  S7S7 x LR(0)

9 A  B  {\$} A  a  Ab {\$} A   aAb {b} A   B {b} B   x {b} S  A  {\$} S  x  b {\$} B  x  {\$} A  aA  b {\$} S  xb  {\$} A  aAb  {\$} S4S4 S3S3 S1S1 S2S2 S5S5 S8S8 S6S6 B B a A b A x b S‘   S\$ S   A {\$} S   xb {\$} A   aAb {\$} A   B {\$} B   x {\$} S0S0 S’  S  \$ S’  S\$  S 10 S9S9 T S A  B  {b} S 11 A  a  Ab {b} A   aAb {b} A   B {b} B   x {b} A  aA  b {b} A  aAb  {b} S 14 S 13 b A B  x  {b} S7S7 x x B a a S 12 LR(1)

10 A  B  {\$,b} A  a  Ab {\$,b} A   aAb {b} A   B {b} B   x {b} S  A  {\$} S  x  b {\$} B  x  {\$} A  aA  b {\$,b} S  xb  {\$} A  aAb  {\$,b} S4S4 S 3,12 S1S1 S2S2 S5S5 S 8,14 S 6,13 B B a A b A x b S‘   S\$ S   A {\$} S   xb {\$} A   aAb {\$} A   B {\$} B   x {\$} S0S0 S’  S  \$ S’  S\$  S 10 S9S9 T S B  x  {b} S7S7 x B a LALR(1)

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)