CH4.1 CSE244 SLR Parsing Aggelos Kiayias Computer Science & Engineering Department The University of Connecticut 371 Fairfield Road, Box U-155 Storrs,

Slides:



Advertisements
Similar presentations
CH4.1 CSE244 More on LR Parsing Aggelos Kiayias Computer Science & Engineering Department The University of Connecticut 191 Auditorium Road, Box U-155.
Advertisements

Compiler Theory – 08/10(ppt) By Anindhya Sankhla 11CS30004 Group : G 29.
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 – The Items Lecture 10 Mon, Feb 14, 2005.
LR Parsing Table Costruction
Bhaskar Bagchi (11CS10058) Lecture Slides( 9 th Sept. 2013)
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.
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.
Section 4.8 Aggelos Kiayias Computer Science & Engineering Department
Lecture #8, Feb. 7, 2007 Shift-reduce parsing,
CS 536 Spring Bottom-Up Parsing: Algorithms, part 1 LR(0), SLR Lecture 12.
CH4.1 CSE244 Sections 4.5,4.6 Aggelos Kiayias Computer Science & Engineering Department The University of Connecticut 371 Fairfield Road, Box U-155 Storrs,
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.
CH4.1 CSE244 Introduction to LR Parsing Aggelos Kiayias Computer Science & Engineering Department The University of Connecticut 371 Fairfield Road, Box.
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.
SLR PARSING TECHNIQUES Submitted By: Abhijeet Mohapatra 04CS1019.
CS 321 Programming Languages and Compilers Bottom Up Parsing.
CSc 453 Syntax Analysis (Parsing) Saumya Debray The University of Arizona Tucson.
 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.
CH4.1 CSE244 Sections 4.5,4.6 Aggelos Kiayias Computer Science & Engineering Department The University of Connecticut 371 Fairfield Road, Box U-155 Storrs,
CMSC 331, Some material © 1998 by Addison Wesley Longman, Inc. 1 Chapter 4 Chapter 4 Bottom Up Parsing.
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.
Spring 2014Jim Hogg - UW - CSE - P501E-1 LR ~ Bottom-Up ~ Shift-Reduce - concluded Dotted Items Building the Handles DFA Building Action & Goto Tables.
Chapter 5: Bottom-Up Parsing (Shift-Reduce)
1 Syntax Analysis Part II Chapter 4 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2005.
LR Parser: LR parsing is a bottom up syntax analysis technique that can be applied to a large class of context free grammars. L is for left –to –right.
Bernd Fischer RW713: Compiler and Software Language Engineering.
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.
Bottom-Up Parsing Algorithms LR(k) parsing L: scan input Left to right R: produce Rightmost derivation k tokens of lookahead LR(0) zero tokens of look-ahead.
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.
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.
CH4.1 CSE244 Midterm Subjects Aggelos Kiayias Computer Science & Engineering Department The University of Connecticut 371 Fairfield Road, Box U-155 Storrs,
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.
Introduction to LR Parsing
Bottom-Up Parsing.
lec04-bottomupparser June 6, 2018 Bottom-Up Parsing
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
Chapter 4. Syntax Analysis (2)
Compiler SLR Parser.
Kanat Bolazar February 16, 2010
Announcements HW2 due on Tuesday Fall 18 CSCI 4430, A Milanova.
Compiler Construction
Chapter 4. Syntax Analysis (2)
Chap. 3 BOTTOM-UP PARSING
Items and Itemsets An itemset is merely a set of items
Presentation transcript:

CH4.1 CSE244 SLR Parsing Aggelos Kiayias Computer Science & Engineering Department The University of Connecticut 371 Fairfield Road, Box U-155 Storrs, CT

CH4.2 CSE244Items  SLR (Simple LR parsing)  DEF A LR(0) item is a production with a “marker.” E.g.  DEF A LR(0) item is a production with a “marker.” E.g. S  aA.Be intuition: it indicates how much of a certain production we have seen already (up to the point of the marker)   CENTRAL IDEA OF SLR PARSING: construct a DFA that recognizes viable prefixes of the grammar.   Intuition: Shift/Reduce actions can be decided based on this DFA (what we have seen so far & what are our next options).   Use “LR(0) Items” for the creation of this DFA.

CH4.3 CSE244 Basic Operations  Augmented Grammar:  Augmented Grammar: E’  E E  E + T | T T  T * F | F F  ( E ) | id CLOSURE OPERATION of a set of Items: Function closure(I) {J=I; repeat for each A .B  in J and each produtcion B  of G such that B .  is not in J: ADD B .  to J until … no more items can be added to J return J } EXAMPLE consider I={ E’ .E } E  E + T | T T  T * F | F F  ( E ) | id

CH4.4 CSE244 GOTO function  Definition. Goto(I,X) = closure of the set of all items  Definition. Goto(I,X) = closure of the set of all items A  X.  where A .X  belongs to I   Intuitively: Goto(I,X) set of all items that “reachable” from the items of I once X has been “seen.”   E.g. consider I={E’  E., E  E.+T} and compute Goto(I,+) Goto(I,+) = { E  E+.T, T .T * F, T .F, F .( E ), F .id }

CH4.5 CSE244 The Canonical Collections of Items for G Procedure Items(G’:augmented grammar) {C:={ closure [S’ . S] } repeat for each set of items I in C and each grammar symbol X such that goto(I,X) is not empty and not in C do add goto(I,X) to C until no more sets of items can be added to C } E’  E E  E + T | T T  T * F | F F  ( E ) | id I 0 E’ .E E .E + T E .T T .T * F T .F F .( E ) F .id I 1 E’  E. E  E. + T I 2 E  T. T  T. * F … I 11

CH4.6 CSE244 The DFA For Viable Prefixes  States = Canonical Collection of Sets of Items  Transitions defined by the Goto Function.  All states final except I 0 I2I2 I1I1 + E I0I0 T* I3I3 I7I7 F I3I3 … … Look p. 226 Intuition: Imagine an NFA with states all the items in the grammar and transitions to be of the form: “A .X  ” goes to “ A  X.  ” with an arrow labeled “X” Then the closure used in the Goto functions Essentially transforms this NFA into the DFA above

CH4.7 CSE244Example   S’  S   S  aABe   A  Abc   A  b   B  d Start with I 0 = closure(S’ .S)

CH4.8 CSE244 2 nd Example E’  E E  E + T | T T  T * F | F F  ( E ) | id

CH4.9 CSE244 Relation to Parsing   An item A   1.  2 is valid for a viable prefix  1 if we have a rightmost derivation that yields  Aw which in one step yields  1  2 w   An item will be valid for many viable prefixes.   Whether a certain item is valid for a certain viable prefix it helps on our decision whether to shift or reduce when   1 is on the stack.  If  2  looks like we still need to shift.  If  2 =  it looks like we should reduce A   1  It could be that two valid items may tell us different things.

CH4.10 CSE244 Valid Items for Viable Prefixes  E+T* is a viable prefix (and the DFA will be at state I 7 after reading it)  Indeed:  Indeed: E’=>E=>E+T=>E+T*F is a rightmost derivation, T*F is the handle of E+T*F, thus E+T*F is a viable prefix, thus E+T* is also.  Examine state I 7 … it contains  Examine state I 7 … it contains T  T*.F F .(E) F .id   i.e., precisely the items valid for E+T*: E’=>E=>E+T=>E+T*F E’=>E=>E+T=>E+T*F=>E+T*(E) E’=>E=>E+T=>E+T*F=>E+T*id   There are no other valid items for for the viable prefix E+T*

CH4.11 CSE244 SLR Parsing Table Construction Input: the augmented grammar G’ Output: The SLR Parsing table functions ACTION & GOTO 1. 1.Construct C={I 0,..,I n } the collections of LR(0) items for G’ 2. 2.“State i” is constructed from I i If [A .a  ] is in I i and goto(I i,a)=I k then we set ACTION[i,a] to be “shift k”(a is a terminal) If [A .] is in I i then we set ACTION[i,a] to reduce “A  ” for all a in Follow(A) --- (note: A is not S’) If [S’  S.] is in I i then we set ACTION[i,$] = accept 3. The goto transitions for state i are constructed as follows for all A, if goto(I i,A)=I k then goto[i,A]=k all A, if goto(I i,A)=I k then goto[i,A]=k 4. All entries not defined by rules (2) and (3) are made “error” 5. The initial state of the parser is the one constructed from the set of items I 0

CH4.12 CSE244Example. I 0 E’ .E E .E + T E .T T .T * F T .F F .( E ) F .id I 1 E’  E. E  E. + T I 2 E  T. T  T. * F Goto(I 0, E)=I 1 Goto(I 0,T)=I 2 Goto(I 0,( )=I 4 I 4 F  (.E) E .E + T E .T T .T * F T .F F .( E ) F .id Since F .( E ) is in I 0 And Goto(I 0,( )=I 4 we set ACTION(0, ( )=s4 Since E’  E. is in I 1 We set ACTION(1,$)=acc Since E  T. is in I 2 and Follow(E)={$,+,) } We set ACTION(2,$)=r E  T ACTION(2,+)=r E  T ACTION(2,))=r E  T Follow(T)=Follow(F)={ ), +, *, $ }

CH4.13 CSE244 3 rd example – SLR Table Construction S  AB | a A  aA | b B  a

CH4.14 CSE244Conflicts  Shift/Reduce  Reduce/Reduce Sometimes unambiguous grammars produce multiply defined labels (s/r, r/r conflicts)in the SLR table.

CH4.15 CSE244 Conflict Example S’  S S  L = R | R L  * R | id R  L

CH4.16 CSE244 Conflict Example S’  S S  L = R | R L  * R | id R  L I 0 = { S’ .S, S .L = R, S .R, L .* R, L . id, R .L } I 1 = { S’  S. } I 2 = { S  L. = R, R  L. } I 3 = { S  R. } I 4 = { L  *.R, R .L, L .* R, L . id } I 5 = { L  id. } I 6 = { S  L =. R, R .L, L .* R, L . id } I 7 = { L  *R. } I 8 = { R  L. } I 9 = { S  L = R. } action[2, = ] ?s 6 (because of S  L. = R ) r R  L (because of R  L. and = follows R )

CH4.17 CSE244 But Why?  Let’s consider a string that will exhibit the conflict.  Let’s consider a string that will exhibit the conflict. id=id  What is the correct move? (recall: grammar is non- ambig.)  Ris not a right sentential form!!!  R =id is not a right sentential form!!!  Even though in general might follow R … but it does not in this case.  Even though in general = might follow R … but it does not in this case.  …Actually it does only when R is preceded by  …Actually it does only when R is preceded by *  SLR finds a conflict because using Follow + LR(0) items as the guide to find when to reduce is not the best method. $0 id=id$s5 $0id5 =id$ r L  id $0L2 =id$ conflict…