Lesson 9 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.

Slides:



Advertisements
Similar presentations
Compiler Construction Sohail Aslam Lecture StackInput ¤0¤0 id – id  id $ s4 ¤0 id 4 – id  id $ r6 F → id ¤0F3¤0F3 – id  id $ r5 T → F ¤0T2¤0T2.
Advertisements

Lesson 8 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Compiler Theory – 08/10(ppt) By Anindhya Sankhla 11CS30004 Group : G 29.
Compiler Designs and Constructions
 CS /11/12 Matthew Rodgers.  What are LL and LR parsers?  What grammars do they parse?  What is the difference between LL and LR?  Why do.
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.
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 Table Costruction
Bhaskar Bagchi (11CS10058) Lecture Slides( 9 th Sept. 2013)
– 1 – CSCE 531 Spring 2006 Lecture 9 SLR Parse Table Construction Topics SLR Parse Table Construction Sets of Items Closure(I) Readings: 4.7 Homework:
Cse321, Programming Languages and Compilers 1 6/12/2015 Lecture #10, Feb. 14, 2007 Modified sets of item construction Rules for building LR parse tables.
1 Chapter 5: Bottom-Up Parsing (Shift-Reduce). 2 - attempts to construct a parse tree for an input string beginning at the leaves (the bottom) and working.
Pertemuan 12, 13, 14 Bottom-Up Parsing
Compiler Construction LR Rina Zviel-Girshin and Ohad Shacham School of Computer Science Tel-Aviv University.
Lecture #8, Feb. 7, 2007 Shift-reduce parsing,
Bottom Up Parsing.
Chapter 4-2 Chang Chi-Chung Bottom-Up Parsing LR methods (Left-to-right, Rightmost derivation)  LR(0), SLR, Canonical LR = LR(1), LALR Other.
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.
Shift/Reduce and LR(1) Professor Yihjia Tsai Tamkang University.
Bottom-up parsing Goal of parser : build a derivation
1 Syntactic Analysis and Parsing (Based on: Compilers, Principles, Techniques and Tools, by Aho, Sethi and Ullman, 1986)
LALR Parsing Canonical sets of LR(1) items
 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.
Joey Paquet, 2000, 2002, 2012, Lecture 6 Bottom-Up Parsing.
410/510 1 of 21 Week 2 – Lecture 1 Bottom Up (Shift reduce, LR parsing) SLR, LR(0) parsing SLR parsing table Compiler Construction.
SLR PARSING TECHNIQUES Submitted By: Abhijeet Mohapatra 04CS1019.
Parsing Jaruloj Chongstitvatana Department of Mathematics and Computer Science Chulalongkorn University.
Lesson 10 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
CS 321 Programming Languages and Compilers Bottom Up Parsing.
 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 
Spring 2014Jim Hogg - UW - CSE - P501E-1 LR ~ Bottom-Up ~ Shift-Reduce - concluded Dotted Items Building the Handles DFA Building Action & Goto Tables.
Lesson 5 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
CSI 3120, Syntactic analysis, page 1 Syntactic Analysis and Parsing Based on A. V. Aho, R. Sethi and J. D. Ullman Compilers: Principles, Techniques and.
Chapter 5: Bottom-Up Parsing (Shift-Reduce)
Syntax Analysis - LR(0) Parsing Compiler Design Lecture (02/04/98) Computer Science Rensselaer Polytechnic.
Announcements/Reading
1 Syntax Analysis Part II Chapter 4 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2005.
Bernd Fischer RW713: Compiler and Software Language Engineering.
Bottom Up Parsing CS 671 January 31, CS 671 – Spring Where Are We? Finished Top-Down Parsing Starting Bottom-Up Parsing Lexical Analysis.
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.
Lecture 5: LR Parsing CS 540 George Mason University.
Compilers: Bottom-up/6 1 Compiler Structures Objective – –describe bottom-up (LR) parsing using shift- reduce and parse tables – –explain how LR.
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture Ahmed Ezzat.
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 
1 Chapter 6 Bottom-Up Parsing. 2 Bottom-up Parsing A bottom-up parsing corresponds to the construction of a parse tree for an input tokens beginning at.
Conflicts in Simple LR parsers A SLR Parser does not use any lookahead The SLR parsing method fails if knowing the stack’s top state and next input token.
Chapter 8. LR Syntactic Analysis Sung-Dong Kim, Dept. of Computer Engineering, Hansung University.
LR Parsing. LR Parsers The most powerful shift-reduce parsing (yet efficient) is: LR(k) parsing. left to right right-mostk lookhead scanning derivation(k.
Lec04-bottomupparser 4/13/2018 LR Parsing.
The role of parser Lexical Analyzer Parser Rest of Front End Symbol
UNIT - 3 SYNTAX ANALYSIS - II
Compiler Construction
Fall Compiler Principles Lecture 4: Parsing part 3
LALR Parsing Canonical sets of LR(1) items
Canonical LR Parsing Tables
Syntax Analysis Part II
Subject Name:COMPILER DESIGN Subject Code:10CS63
Syntax Analysis - LR(1) and LALR(1) Parsing
LR Parsing – The Tables Lecture 11 Wed, Feb 16, 2005.
Lecture 9 SLR Parse Table Construction
Compiler SLR Parser.
Chap. 3 BOTTOM-UP PARSING
Lecture 11 LR Parse Table Construction
Presentation transcript:

Lesson 9 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg

2 Outline Creating SLR parsing tables –LR(0) items –Canonical LR(0) collections –LR(0) automata

CREATING SLR PARSING TABLES 3

Repetition: Shift-reduce parsing StackInputAction $ id * id $sh. id $ id* id $red. by F → id $ F* id $red. by T → F $ T* id $sh. * $ T *id $sh. id $ T * id$red. by F → id $ T * F$red. by T → T * F $ T$red. by E → T $ E$accept 4 id * id ⇐ F * id ⇐ T * id ⇐ T * F ⇐ T ⇐ E

(1)E → E + T (2)E → T (3)T → T * F (4)T → F (5)F → ( E ) (6)F → id 5 State ACTIONGOTO id+*()$ETF 0s5s4123 1s6acc 2r2s7r2 3r4 4s5s4823 5r6 6s5s493 7s5s410 8s6s11 9r1s7r1 10r3 11r5

Repetition: LR parsing StackInputAction $ 0id * id $sh. 5 $ 0 5 id * id $red. by (6) F → id $ 0 3 F * id $red. by (4) T → F $ 0 2 T * id $sh. 7 $ 0 2 T 7 * id $sh. 5 $ 0 2 T 7 * 5 id $red. by (6) F → id $ 0 2 T 7 * 10 F $red. by (3) T → T * F $ 0 2 T $red. by (2) E → T $ 0 1 E $accept 6

LR(0) items One state represents: symbol + context –The set of handles the parser might be in the process of reading in that state –The progress so far in reading those handles LR(0) item = handle with a “dot” –Example: E → E + T 7

LR(0) items Grammar: decl→ type id ; | type id [ num ] ; type → int | float | class class → id 8

LR(0) items Set of items I 1 : decl→ type id ; | type id [ num ] ; type → int | float | class class → id 9

Computing CLOSURE CLOSURE(I) J = I repeat foreach item in J if it has the form A → αBβ add all B → γ to J until no more items were added in an iteration return J 10

The GOTO function GOTO(I 1, int) = I 2 : type → int GOTO(I 1, float) = I 3 : type → float GOTO(I 1, id) = I 4 : class → id 11

The GOTO function GOTO(I 1, type) = I 5 : decl→ type id ; | type id [ num ] ; GOTO(I 1, class) = I 6 type → class 12

Computing GOTO GOTO(I, X) J = Ø foreach item A → αXβ in I add A → αXβ to J return CLOSURE(J) 13

Reductions State: type → int Pop one state –I 1 appears on the stack Push type and go to GOTO(I 1, type) 14

LR(0) automata Based on the canonical LR(0) collection States represent sets of items Transitions defined by the GOTO function 15

LR(0) automata Grammar: S → A B S → A c A → A a A → a B → b B→ ε Augmented grammar: S' → S S → A B S → A c A → A a A → a B → b B→ ε 16

LR(0) automata 17 I 0 : S‘ → S S → A B S → A c A → A a A → a I 7 : S‘ → S I 2 : S → A B S → A c A → A a B → b B → I 1 : A → a I 6 : S → A B I 5 : B → b I 4 : A → A a I 3 : S → A c S a A c a b B

Computing the canonical collection of sets of items items(G') C = CLOSURE({ S' → S }) repeat foreach item I in C foreach grammar symbol X if GOTO(I, X) is non-empty, add it to C until no more sets were added in an iteration return C 18

Exercise (1) Compute a)CLOSURE({ F → id }) b)CLOSURE({ S' → E }) c)CLOSURE({ E → T E’ }) S' → E E → T E' E' → + T E’ | ε T → F T' T' → * F T’ | ε F → ( E ) | id 19

Exercise (2) Let I be the following set of items: E → T E' E' → + T E' E' → Compute GOTO(I, X) for all relevant X. S' → E E → T E' E' → + T E’ | ε T → F T' T' → * F T’ | ε F → ( E ) | id 20

Constructing the parsing table 1.Enumerate the productions: (0) S' → S (1) S → A B (2) S → A c (3) A → A a (4) A → a (5) B → b (6) B→ ε 2.Compute FOLLOW: FOLLOW(S') = { $ } FOLLOW(S) = { $ } FOLLOW(A) = { a, b, c, $ } FOLLOW(B) = { $ } 21

Constructing the parsing table table(C) foreach set of item I i in C if I i has an item [A → αaβ], where a is a terminal set ACTION[i, a] to a “shift j”, where j is the state representing I j = GOTO(I i, a) if I i has an item [A → αBβ] set GOTO[i, B] to j, where j is the state representing I j = GOTO(I i, B) if I i has an item [A → α] set ACTION[i, a] to “red. by. A → α” for all a in FOLLOW(A) if I i has the item [S' → S] set ACTION[i, $] to “accept” 22

Constructing the parsing table 23 I 0 : S‘ → S S → A B S → A c A → A a A → a I 7 : S‘ → S I 2 : S → A B S → A c A → A a B → b B → I 1 : A → a I 6 : S → A B I 5 : B → b I 4 : A → A a I 3 : S → A c S a A c a b B

The complete parsing table 24 States ACTIONGOTO abc$SAB 0s172 1r4 2s4s5s3r66 3r2 4r3 5r5 6r1 7acc

Exercise (3) Construct a parsing table for the grammar here to the right. Recall: you need to 1.compute the canonical collection of sets of items. 2.compute FOLLOW for all nonterminals in the grammar. 3.insert actions into the parsing table. (0) S' → S (1) S → h B e (2) B → B A (3) B → ε (4) A → x (5) A → t 25

Shift/reduce conflicts Grammar: stmt→ if ( expr ) stmt | if ( expr ) stmt else stmt | other Problematic configuration: StackInput $ … if ( expr ) stmtelse … $ 26

Reduce/reduce conflicts Grammar: stmt → id ( param_list ) stmt → expr := expr param_list → param_list, param param_list → param param → id expr → id ( expr_list ) expr → id expr_list → expr_list, expr expr_list → expr Problematic configuration: StackInput $ … id ( id, id ) … 27

Conclusion –LR(0) items –Canonical LR(0) collections –LR(0) automata 28

Next time Parser generator tools Syntax-directed definitions/translation Abstract syntax trees 29