1 May 22, 2015 1 May 22, 2015May 22, 2015May 22, 2015 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University Azusa Pacific University,

Slides:



Advertisements
Similar presentations
Lesson 8 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Advertisements

Compiler Designs and Constructions
Compiler Principles Fall Compiler Principles Lecture 4: Parsing part 3 Roman Manevich Ben-Gurion University.
 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.
LR Parsing Table Costruction
Bhaskar Bagchi (11CS10058) Lecture Slides( 9 th Sept. 2013)
6/12/2015Prof. Hilfinger CS164 Lecture 111 Bottom-Up Parsing Lecture (From slides by G. Necula & R. Bodik)
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
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.
1 CMPSC 160 Translation of Programming Languages Fall 2002 slides derived from Tevfik Bultan, Keith Cooper, and Linda Torczon Lecture-Module #10 Parsing.
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.
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.
Shift/Reduce and LR(1) Professor Yihjia Tsai Tamkang University.
Bottom-up parsing Goal of parser : build a derivation
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.
LESSON 24.
410/510 1 of 21 Week 2 – Lecture 1 Bottom Up (Shift reduce, LR parsing) SLR, LR(0) parsing SLR parsing table Compiler Construction.
1 October 2, October 2, 2015October 2, 2015October 2, 2015 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University Azusa.
SLR PARSING TECHNIQUES Submitted By: Abhijeet Mohapatra 04CS1019.
LR(k) Parsing CPSC 388 Ellen Walker Hiram College.
Parsing Jaruloj Chongstitvatana Department of Mathematics and Computer Science Chulalongkorn University.
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.
1 Compiler Construction Syntax Analysis Top-down parsing.
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 
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.
–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.
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.
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.
Announcements/Reading
Lec04-bottomupparser 4/13/2018 LR Parsing.
Compiler design Bottom-up parsing Concepts
UNIT - 3 SYNTAX ANALYSIS - II
Compiler Construction
Fall Compiler Principles Lecture 4: Parsing part 3
LALR Parsing Canonical sets of LR(1) items
Bottom-Up Syntax Analysis
Canonical LR Parsing Tables
Syntax Analysis Part II
Subject Name:COMPILER DESIGN Subject Code:10CS63
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Chapter 4. Syntax Analysis (2)
Compiler SLR Parser.
Parsing Bottom-Up LR Table Construction.
Parsing Bottom-Up LR Table Construction.
Chap. 3 BOTTOM-UP PARSING
Presentation transcript:

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, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction

2 Parsing Universal (any C-F grammar) –C–Cocke-Younger-Kasimi –E–Earley Top-down (C-F grammar with restrictions) –R–Recursive descent (predictive parsing) –L–LL (Left-to-right, Leftmost derivation) methods Bottom-up (C-F grammar with restrictions) –O–Operator precedence parsing –L–LR (Left-to-right, Rightmost derivation) methods SLR, canonical LR, LALR May 22, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction

3 May 22, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction Got it with following questions Bottom Up Parsing –L–Left to right, rightmost –R–Reduce from leaves to root –T–Terminal is gradually eaten SLR & LALR –S–Simple extension of LR(0) –L–Lookahead LR –A–Avoid unnecessary conflicts Summary –L–LL, LR, SLR, LALR –G–Grammar types –G–Goto state transition

4 SLR Grammars SLR (Simple LR): a simple extension of LR(0) shift- reduce parsing SLR eliminates some conflicts by populating the parsing table with reductions A  on symbols in FOLLOW(A) S  E E  id + E E  id State I 0 : S  E E  id + E E  id State I 2 : E  id+ E E  id goto(I 0,id)goto(I 3,+) FOLLOW(E)={$} thus reduce on $ Shift on + May 22, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction

5 SLR Parsing Table s2 acc s3r3 s2 r2 id+$ E S  E 2. E  id + E 3. E  id FOLLOW(E)={$} thus reduce on $ Shift on + Reductions do not fill entire rows Otherwise the same as LR(0) May 22, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction

6 SLR Parsing An LR(0) state is a set of LR(0) items An LR(0) item is a production with a (dot) in the right-hand side Build the LR(0) DFA by –Closure operation to construct LR(0) items –Goto operation to determine transitions Construct the SLR parsing table from the DFA LR parser program uses the SLR parsing table to determine shift/reduce operations May 22, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction

7 Constructing SLR Parsing Tables 1.Augment the grammar with S’  S 2.Construct the set C={I 0,I 1,…,I n } of LR(0) items 3.If [A a  ]  I i and goto(I i,a)=I j then set action[i,a]=shift j 4.If [A  ]  I i then set action[i,a]=reduce A  for all a  FOLLOW(A) (apply only if A  S’) 5.If [S’  S] is in I i then set action[i,$]=accept 6.If goto(I i,A)=I j then set goto[i,A]=j 7.Repeat 3-6 until no more entries added 8.The initial state i is the I i holding item [S’ S] May 22, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction

8 LR(0) Items of a Grammar An LR(0) item of a grammar G is a production of G with a at some position of the right-hand side Thus, a production A  X Y Z has four items: [A  X Y Z] [A  X Y Z] [A  X Y Z] [A  X Y Z ] Note that production A   has one item [A  ] May 22, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction

9 Constructing the set of LR(0) Items of a Grammar 1.The grammar is augmented with a new start symbol S’ and production S’  S 2.Initially, set C = closure({[S’ S]}) (this is the start state of the DFA) 3.For each set of items I  C and each grammar symbol X  (N  T) such that goto(I,X)  C and goto(I,X)  , add the set of items goto(I,X) to C 4.Repeat 3 until no more sets can be added to C May 22, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction

10 The Closure Operation for LR(0) Items 1.Start with closure(I) = I 2.If [A B  ]  closure(I) then for each production B  in the grammar, add the item [B   ] to I if not already in I 3.Repeat 2 until no new items can be added May 22, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction

11 The Closure Operation (Example) Grammar: E  E + T | T T  T * F | F F  ( E ) F  id { [E’  E] } closure({[E’  E]}) = { [E’  E] [E  E + T] [E  T] } { [E’  E] [E  E + T] [E  T] [T  T * F] [T  F] } { [E’  E] [E  E + T] [E  T] [T  T * F] [T  F] [F  ( E )] [F  id] } Add [E   ] Add [T   ] Add [F   ] May 22, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction

12 The Goto Operation for LR(0) Items 1.For each item [A X  ]  I, add the set of items closure({[A  X  ]}) to goto(I,X) if not already there 2.Repeat step 1 until no more items can be added to goto(I,X) 3.Intuitively, goto(I,X) is the set of items that are valid for the viable prefix  X when I is the set of items that are valid for  May 22, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction

13 The Goto Operation (Example 1) Suppose I = Then goto(I,E) = closure({[E’  E, E  E + T]}) = { [E’  E ] [E  E + T] } Grammar: E  E + T | T T  T * F | F F  ( E ) F  id { [E’  E] [E  E + T] [E  T] [T  T * F] [T  F] [F  ( E )] [F  id] } May 22, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction

14 The Goto Operation (Example 2) Suppose I = { [E’  E ], [E  E + T] } Then goto(I,+) = closure({[E  E + T]}) = { [E  E + T] [T  T * F] [T  F] [F  ( E )] [F  id] } Grammar: E  E + T | T T  T * F | F F  ( E ) F  id May 22, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction

15 Example SLR Grammar and LR(0) Items Augmented grammar: 1. C’  C 2. C  A B 3. A  a 4. B  a I 0 = closure({[C’  C]}) I 1 = goto(I 0,C) = closure({[C’  C]}) … May 22, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction

16 Example SLR Parsing Table May 22, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction

17 SLR and Ambiguity Every SLR grammar is unambiguous, but not every unambiguous grammar is SLR Consider for example the unambiguous grammar S  L = R | R L  * R | id R  L May 22, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction

18 LR(1) Grammars SLR too simple LR(1) parsing uses lookahead to avoid unnecessary conflicts in parsing table LR(1) item = LR(0) item + lookahead LR(0) item: [A   ] LR(1) item: [A  , a] May 22, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction

19 Should not reduce on =, because no right-sentential form begins with R= SLR Versus LR(1) Split the SLR states by adding LR(1) lookahead Unambiguous grammar 1.S  L = R 2. | R 3.L  * R 4. | id 5.R  L May 22, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction

20 LR(1) Items An LR(1) item [A  , a] contains a lookahead terminal a, meaning  already on top of the stack, expect to see  a For items of the form [A , a] the lookahead a is used to reduce A  only if the next input is a For items of the form [A  , a] with  the lookahead has no effect May 22, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction

21 The Closure Operation for LR(1) Items 1.Start with closure(I) = I 2.If [A B , a]  closure(I) then for each production B  in the grammar and each terminal b  FIRST(  a), add the item [B  , b] to I if not already in I 3.Repeat 2 until no new items can be added May 22, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction

22 The Goto Operation for LR(1) Items 1.For each item [A X , a]  I, add the set of items closure({[A  X , a]}) to goto(I,X) if not already there 2.Repeat step 1 until no more items can be added to goto(I,X) May 22, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction

23 Constructing the set of LR(1) Items of a Grammar 1.Augment the grammar with a new start symbol S’ and production S’  S 2.Initially, set C = closure({[S’ S, $]}) (this is the start state of the DFA) 3.For each set of items I  C and each grammar symbol X  (N  T) such that goto(I,X)  C and goto(I,X)  , add the set of items goto(I,X) to C 4.Repeat 3 until no more sets can be added to C May 22, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction

24 Example Grammar and LR(1) Items Unambiguous LR(1) grammar: S  L = R | R L  * R | id R  L Augment with S’  S LR(1) items (next slide) May 22, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction

25 May 22, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction

26 Constructing Canonical LR(1) Parsing Tables 1.Augment the grammar with S’  S 2.Construct the set C={I 0,I 1,…,I n } of LR(1) items 3.If [A a , b]  I i and goto(I i,a)=I j then set action[i,a]=shift j 4.If [A , a]  I i then set action[i,a]=reduce A  (apply only if A  S’) 5.If [S’  S, $] is in I i then set action[i,$]=accept 6.If goto(I i,A)=I j then set goto[i,A]=j 7.Repeat 3-6 until no more entries added 8.The initial state i is the I i holding item [S’ S,$] May 22, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction

27 Example LR(1) Parsing Table Grammar: 1. S’  S 2. S  L = R 3. S  R 4. L  * R 5. L  id 6. R  L May 22, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction

28 LALR(1) Grammars LR(1) parsing tables have many states LALR(1) parsing (Look-Ahead LR) combines LR(1) states to reduce table size Less powerful than LR(1) –Will not introduce shift-reduce conflicts, because shifts do not use lookaheads –May introduce reduce-reduce conflicts, but seldom do so for grammars of programming languages May 22, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction

29 Constructing LALR(1) Parsing Tables 1.Construct sets of LR(1) items 2.Combine LR(1) sets with sets of items that share the same first part May 22, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction

30 Example LALR(1) Grammar Unambiguous LR(1) grammar: S  L = R | R L  * R | id R  L Augment with S’  S LALR(1) items (next slide) May 22, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction

31 May 22, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction

32 Example LALR(1) Parsing Table Grammar: 1. S’  S 2. S  L = R 3. S  R 4. L  * R 5. L  id 6. R  L May 22, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction

33 LL, SLR, LR, LALR Summary LL parse tables computed using FIRST/FOLLOW –Nonterminals  terminals  productions –Computed using FIRST/FOLLOW LR parsing tables computed using closure/goto –LR states  terminals  shift/reduce actions –LR states  nonterminals  goto state transitions A grammar is –LL(1) if its LL(1) parse table has no conflicts –SLR if its SLR parse table has no conflicts –LALR(1) if its LALR(1) parse table has no conflicts –LR(1) if its LR(1) parse table has no conflicts May 22, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction

34 LL, SLR, LR, LALR Grammars May 22, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction

35 May 22, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction Got it with following questions Bottom Up Parsing –L–Left to right, rightmost –R–Reduce from leaves to root –T–Terminal is gradually eaten SLR & LALR –S–Simple extension of LR(0) –L–Lookahead LR –A–Avoid unnecessary conflicts Summary –L–LL, LR, SLR, LALR –G–Grammar types –G–Goto state transition

36 Thank you very much! Questions? May 22, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction