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

Slides:



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

Lesson 8 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Compiler Designs and Constructions
Compiler construction in4020 – lecture 4 Koen Langendoen Delft University of Technology The Netherlands.
Joey Paquet, 2000, 2002, 2008, Lecture 7 Bottom-Up Parsing II.
CH4.1 CSE244 SLR Parsing Aggelos Kiayias Computer Science & Engineering Department The University of Connecticut 371 Fairfield Road, Box U-155 Storrs,
Review: LR(k) parsers a1 … a2 … an $ LR parsing program Action goto Sm xm … s1 x1 s0 output input stack Parsing table.
Compiler Construction Sohail Aslam Lecture Finite Automaton of Items Then for every item A →  X  we must add an  -transition for every production.
CS 31003: Compilers  Difference between SLR and LR(1)  Construction of LR(1) parsing table  LALR parser Bandi Sumanth 11CS30006 Date : 9/10/2013.
Mooly Sagiv and Roman Manevich School of Computer Science
1 May 22, May 22, 2015May 22, 2015May 22, 2015 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University Azusa Pacific University,
LR Parsing – The Items Lecture 10 Mon, Feb 14, 2005.
LR Parsing Table Costruction
Bhaskar Bagchi (11CS10058) Lecture Slides( 9 th Sept. 2013)
Compiler Construction LR(0) + Visitor Ran Shaham and Ohad Shacham School of Computer Science Tel-Aviv University.
1 Example S'  S, S  CC, C  cC, C  d State 0. Closure({[S'  S, $]}) = { S'  S, $ S   CC, FIRST( $)  S   CC, $ C   cC, FIRST(C$)  C.
CH4.1 CSE244 More on LR Parsing Aggelos Kiayias Computer Science & Engineering Department The University of Connecticut 371 Fairfield Road, Unit 1155 Storrs,
By Neng-Fa Zhou Syntax Analysis lexical analyzer syntax analyzer semantic analyzer source program tokens parse tree parser tree.
Bottom-Up Syntax Analysis Mooly Sagiv Textbook:Modern Compiler Design Chapter (modified)
Bottom-Up Syntax Analysis Mooly Sagiv html:// Textbook:Modern Compiler Design Chapter
More SLR /LR(1) Professor Yihjia Tsai Tamkang University.
Lecture #8, Feb. 7, 2007 Shift-reduce parsing,
Bottom-Up Syntax Analysis Mooly Sagiv & Greta Yorsh Textbook:Modern Compiler Design Chapter (modified)
1 LR parsing techniques SLR (not in the book) –Simple LR parsing –Easy to implement, not strong enough –Uses LR(0) items Canonical LR –Larger parser but.
Bottom-Up Syntax Analysis Mooly Sagiv & Greta Yorsh Textbook:Modern Compiler Design Chapter (modified)
Bottom-Up Syntax Analysis Mooly Sagiv html:// Textbook:Modern Compiler Implementation in C Chapter 3.
Table-driven parsing Parsing performed by a finite state machine. Parsing algorithm is language-independent. FSM driven by table (s) generated automatically.
1 Bottom-up parsing Goal of parser : build a derivation –top-down parser : build a derivation by working from the start symbol towards the input. builds.
Bottom-up parsing Goal of parser : build a derivation
LALR Parsing Canonical sets of LR(1) items
410/510 1 of 21 Week 2 – Lecture 1 Bottom Up (Shift reduce, LR parsing) SLR, LR(0) parsing SLR parsing table Compiler Construction.
 an efficient Bottom-up parser for a large and useful class of context-free grammars.  the “ L ” stands for left-to-right scan of the input; the “ R.
Syntactic Analysis Natawut Nupairoj, Ph.D. Department of Computer Engineering Chulalongkorn University.
1 LR Parsers  The most powerful shift-reduce parsing (yet efficient) is: LR(k) parsing. LR(k) parsing. left to right right-most k lookhead scanning derivation.
Chapter 3-3 Chang Chi-Chung Bottom-Up Parsing LR methods (Left-to-right, Rightmost derivation)  LR(0), SLR, Canonical LR = LR(1), LALR 
Lesson 9 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Syntax Analysis - LR(0) Parsing Compiler Design Lecture (02/04/98) Computer Science Rensselaer Polytechnic.
–Exercise: construct the SLR parsing table for grammar: S->L=R, S->R L->*R L->id R->L –The grammar can have shift/reduce conflict or reduce/reduce conflict.
1 Syntax Analysis Part II Chapter 4 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2005.
4. Bottom-up Parsing Chih-Hung Wang
Three kinds of bottom-up LR parser SLR “Simple LR” –most restrictions on eligible grammars –built quite directly from items as just shown LR “Canonical.
CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 6: LR grammars and automatic parser generators.
1 Syntax Analysis Part II Chapter 4 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2007.
Bottom-up parsing. Bottom-up parsing builds a parse tree from the leaves (terminals) to the start symbol int E T * TE+ T (4) (2) (3) (5) (1) int*+ E 
Chapter 8. LR Syntactic Analysis Sung-Dong Kim, Dept. of Computer Engineering, Hansung University.
Announcements/Reading
Lec04-bottomupparser 4/13/2018 LR Parsing.
Programming Languages Translator
LR Parsing – The Items Lecture 10 Fri, Feb 13, 2004.
Compiler design Bottom-up parsing Concepts
Compiler Baojian Hua LR Parsing Compiler Baojian Hua
Unit-3 Bottom-Up-Parsing.
Table-driven parsing Parsing performed by a finite state machine.
Winter LR(0) Parsing Summary
Compiler Construction
Compiler design Bottom-up parsing: Canonical LR and LALR
LALR Parsing Canonical sets of LR(1) items
Bottom-Up Syntax Analysis
Canonical LR Parsing Tables
Syntax Analysis Part II
Syntax Analysis - LR(1) and LALR(1) Parsing
LR Parsing – The Tables Lecture 11 Wed, Feb 16, 2005.
Chapter 4. Syntax Analysis (2)
Compiler SLR Parser.
| b.
5. Bottom-Up Parsing Chih-Hung Wang
Chapter 4. Syntax Analysis (2)
Parsing Bottom-Up LR Table Construction.
Parsing Bottom-Up LR Table Construction.
Compiler design Bottom-up parsing: Canonical LR and LALR
Presentation transcript:

Compiler Construction LR Rina Zviel-Girshin and 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 1.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  α  2.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 S  E$ E  T E  E + T T  i T  ( E ) non-terminals denoted by upper-case letters terminals denoted by lower-case letters LR(0) Example

6 Precomputed LR(0) items: 1: S   E$ 2: S  E  $ 3: S  E $  LR(0) Example Precomputed LR(0) items: 4: E   T 5: E  T  6: E   E + T 7: E  E  + T 8: E  E +  T 9: E  E + T  10: T   i 11: T  i  12: T   (E) 13: T  (  E) 14: T  (E  ) 15: T  (E) 

7 E  T  T  (  E) E   T E   E+T T   i T   (E) T  i  S  E  $ E  E  +T T  (E  ) E  E  +T E  E+  T T   i T   (E) S  E$  T  (E)  E  E+T  S6S6 S7S7 S5S5 S1S1 S2S2 S4S4 S3S3 S9S9 S8S8 T T ( i i i T + ) ( E E $ + ( S   E$ E   T E   E+T T   i T   (E) S0S0 1: S   E$ 2: S  E  $ 3: S  E $  4: E   T 5: E  T  6: E   E + T 7: E  E  + T 8: E  E +  T 9: E  E + T  10: T   i 11: T  i  12: T   (E) 13: T  (  E) 14: T  (E  ) 15: T  (E) 

8 statei+()$ET 05716shift S  E$ reduce 3574shift 4 E  E+T reduce 5 TiTi 6 ETET 75786shift T  (E) reduce GOTO table symbol ACTION table

9 S 0 i + (i + i) $ shift StackInput Action S 0 i S 5 + (i + i) $reduce by T  i S 0 T S 6 + (i + i) $reduce by E  T S 0 E S 1 + (i + i) $shift S 0 E S 1 + S 3 (i + i) $shift LR(0) example: i + ( i + i) statei+()$ET 05716shift S  E$ reduce 3574shift 4 E  E+T reduce 5 TiTi 6 ETET 75786shift T  (E) reduce

10 StackInput Action LR(0) example: i + ( i + i) statei+()$ET 05716shift S  E$ reduce 3574shift 4 E  E+T reduce 5 TiTi 6 ETET 75786shift T  (E) reduce S 0 E S 1 + S 3 (i + i) $shift S 0 E S 1 + S 3 ( S 7 i + i) $shift S 0 E S 1 + S 3 ( S 7 iS 5 + i)$reduce by T  i S 0 E S 1 + S 3 ( S 7 TS 6 + i)$ reduce by E  T S 0 E S 1 + S 3 ( S 7 ES 8 + i)$ shift

11 StackInput Action LR(0) example: i + ( i + i) statei+()$ET 05716shift S  E$ reduce 3574shift 4 E  E+T reduce 5 TiTi 6 ETET 75786shift T  (E) reduce S 0 E S 1 + S 3 ( S 7 ES 8 + i)$ shift S 0 E S 1 + S 3 ( S 7 ES 8 +S 3 i)$ shift S 0 E S 1 + S 3 ( S 7 ES 8 +S 3 iS 5 )$ reduce by T  i S 0 E S 1 + S 3 ( S 7 ES 8 +S 3 TS 4 )$ reduce by E  E+T S 0 E S 1 + S 3 ( S 7 ES 8 )$ shift

12 StackInput Action LR(0) example: i + ( i + i) statei+()$ET 05716shift S  E$ reduce 3574shift 4 E  E+T reduce 5 TiTi 6 ETET 75786shift T  (E) reduce S 0 E S 1 + S 3 ( S 7 ES 8 )$ shift S 0 E S 1 + S 3 ( S 7 ES 8 )S 9 $ reduce by T  (E) S 0 E S 1 + S 3 TS 4 $ reduce by E  E+T S 0 E S 1 $ shift S 0 E S 1 $S 2 reduce by S  E$ S 0 S accept

13 E  E +E E  i E  ( E ) Is the grammar LR(0) ? Example Ambiguous

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

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

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

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 S  E$ E  E + T | T T  T * F | F F  num Example S  E$ E  E+T E   T T  T*F T   F F   num 0 E  T  T  T  *F 1 T T  T*  F F   num 2 * Shift – Reduce conflict Shift – Reduce conflict

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 S  E$ E  E + T | T T  T * F | F F  num SLR(1) Example S  E$ E  E+T E   T T  T*F T   F F   num 0 E  T  T  T  *F 1 T T  T*  F F   num 2 * FOLLOW(E) = {$,+}

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

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)