1 / 23

Compiler Construction LR

Compiler Construction LR. Rina Zviel-Girshin and Ohad Shacham School of Computer Science Tel-Aviv University. Administration. PA1 grades are available Theoretical assignment regarding LR parsing Submit to Paz Grimberg’s box Next recitation Tomorrow at 9:00 and 13:00 Friday at 10:00.

taline
Download Presentation

Compiler Construction LR

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

  2. Administration • PA1 grades are available • Theoretical assignment regarding LR parsing • Submit to Paz Grimberg’s box • Next recitation • Tomorrow at 9:00 and 13:00 • Friday at 10:00

  3. LR parsing • LR(0) • Building an LR(0) parser • Parse an input • Is a grammar LR(0)? • SLR(1) • Augmenting LR(0) parser to SLR(1) • Is a grammar SLR(1)? • LR(1) – canonical LR • LALR(1)

  4. LR(0) parsing • Construct transition relation between states • Use algorithms Initial item set and Next item set • States are set of LR(0) items • Shift items of the form PαSβ • Reduce items of the form Pα • Construct parsing table • If every state contains no conflicts use LR(0) parsing algorithm • If states contain conflict • Rewrite grammar or • Use stronger parsing technique

  5. LR(0) Example S  E$ E  T E  E+ T T i T  (E) • non-terminals denoted by upper-case letters • terminals denoted by lower-case letters

  6. LR(0) Example

  7. S0 S E$E TE E+TT iT  (E) S6 E  T T T S7 T  (E)E TE E+TT iT  (E) ( ( S5 i i T  i E E S8 ( S1 T  (E)E  E+T S  E$E  E+T i + + ) $ E  E+TT iT  (E) S3 S9 S2 T  (E) S  E$ T S4 E  E+T

  8. GOTO tablesymbol ACTION table

  9. LR(0) example: i + ( i + i) StackInputAction S0 i + (i + i) $ shift S0 i S5 + (i + i) $ reduce by Ti S0 T S6 + (i + i) $ reduce by ET S0 E S1 + (i + i) $ shift S0 E S1 + S3 (i + i) $ shift

  10. LR(0) example: i + ( i + i) StackInputAction S0 E S1 + S3 (i + i) $ shift S0 E S1 + S3 ( S7 i + i) $ shift S0 E S1 + S3 ( S7iS5 + i)$ reduce by Ti S0 E S1 + S3 ( S7TS6 + i)$ reduce by ET S0 E S1 + S3 ( S7ES8 + i)$ shift

  11. LR(0) example: i + ( i + i) StackInputAction S0 E S1 + S3 ( S7ES8 + i)$ shift S0 E S1 + S3 ( S7ES8+S3 i)$ shift S0 E S1 + S3 ( S7ES8+S3iS5 )$ reduce by Ti S0 E S1 + S3 ( S7ES8+S3TS4 )$ reduce by EE+T S0 E S1 + S3 ( S7ES8 )$ shift

  12. LR(0) example: i + ( i + i) StackInputAction S0 E S1 + S3 ( S7ES8 )$ shift S0 E S1 + S3 ( S7ES8)S9 $ reduce by T(E) S0 E S1 + S3TS4 $ reduce by EE+T S0 E S1 $ shift S0 E S1$S2 reduce by SE$ S0 S accept

  13. + + + 1 3 + 1 2 2 3 Example E  E+E E i E  (E) Ambiguous Is the grammar LR(0) ? 1 + 2 + 3

  14. Example E  E +T E  ( E ) E  T T i E  V = E V i Reduce – Reduce conflict Is the grammar LR(0) ? T i V i

  15. Example E  E +T E  ( E ) T i[E] T i Shift – Reduce conflict Is the grammar LR(0) ? T i[E] T i

  16. + + + 1 3 + 1 2 2 3 Example Is the grammar LR(0) ? E  E + E | E * E | num 1 + 2 + 3

  17. Example E  E + E | E * E | num E  E + T | T T  T * F | F F  num | id Is the grammar LR(0) ?

  18. Shift – Reduce conflict Shift – Reduce conflict 2 1 0 TT*F F num E  T  TT*F SE$ EE+T E  T TT*F T  F F num * T Example S  E$ E  E + T | T T  T * F | F F  num

  19. SLR(1) • Simple LR(1) parser • LR(0) + use of FOLLOW sets of non terminals • FOLLOW(A) = {t | S * At} • Calculate FOLLOW(A) for each item A   • Use these sets to break conflicts

  20. 2 1 0 TT*F F num E  T  TT*F SE$ EE+T E  T TT*F T  F F num * T SLR(1) Example S  E$ E  E + T | T T  T * F | F F  num FOLLOW(E) = {$,+}

  21. Example S’  S$ S  L = R | R L  *R | id R  L Is the grammar SLR(1) ? [S L= R] [R L] [L  R] [L  id] [S’  S$] [S  L=R] [S  R] [L  *R] [L  id] [R L] [S L =R] [R L ] = L Follow(R)= {$, =}

  22. LR • LR(0) • Simple LR – LR(0) + Follow sets • LR(1) – LR(0) + lookhead • Large parsing table • LALR(1) • Merge LR(1) states with same “LR(0) items” • May have reduce – reduce conflict on LR(1) grammar • Most common in practic

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

More Related