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.

Slides:



Advertisements
Similar presentations
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.
Advertisements

A question from last class: construct the predictive parsing table for this grammar: S->i E t S e S | i E t S | a E -> B.
Lesson 8 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
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.
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:
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
By Neng-Fa Zhou Syntax Analysis lexical analyzer syntax analyzer semantic analyzer source program tokens parse tree parser tree.
Lecture #8, Feb. 7, 2007 Shift-reduce 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.
CS 1352 Principles of Compiler Design — Unit - 2 — — Syntax Analysis—
– 1 – CSCE 531 Spring 2006 Lecture 8 Bottom Up Parsing Topics Overview Bottom-Up Parsing Handles Shift-reduce parsing Operator precedence parsing Readings:
1 Syntactic Analysis and Parsing (Based on: Compilers, Principles, Techniques and Tools, by Aho, Sethi and Ullman, 1986)
1 Introduction to Bottom Up Parser By Debi Prasad Behera, Lecturer, Dept of CSEA, Silicon Institute of Technology, Bhubaneswar.
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.
Bottom-Up Parsing CS308 Compiler Theory1. 2 Bottom-Up Parsing A bottom-up parser creates the parse tree of the given input starting from leaves towards.
Compiler 2 nd Phase Syntax Analysis Unit-2 : Syntax Analysis1.
lec04-bottomupparser April 23, 2017 Bottom-Up Parsing
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.
BİL 744 Derleyici Gerçekleştirimi (Compiler Design)1 Bottom-Up Parsing A bottom-up parser creates the parse tree of the given input starting from leaves.
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.
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.
12/11/2015Unit-2 : Syntax Analysis1 CS 1352 Principles of Compiler Design — Unit - 2 — — Syntax Analysis—
1 Syntax Analysis Part II Chapter 4 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2005.
Chapter 4 Syntax Analysis. Outline Role of parser Context free grammars Top down parsing Bottom up parsing Parser generators.
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.
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.
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.
S YNTAX A NALYSIS O R P ARSING Lecture 08. B OTTOM -U P P ARSING A bottom-up parser creates the parse tree of the given input starting from leaves towards.
Lec04-bottomupparser 4/13/2018 LR Parsing.
Compiler design Bottom-up parsing Concepts
The role of parser Lexical Analyzer Parser Rest of Front End Symbol
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
Syntax Analysis source program lexical analyzer tokens syntax analyzer
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Chapter 4. Syntax Analysis (2)
Compiler SLR Parser.
Announcements HW2 due on Tuesday Fall 18 CSCI 4430, A Milanova.
Chapter 4. Syntax Analysis (2)
Chap. 3 BOTTOM-UP PARSING
Presentation transcript:

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 (k is omitted  it is 1)  LR parsing is attractive because:  LR parsing is most general non-backtracking shift-reduce parsing, yet it is still efficient.  The class of grammars that can be parsed using LR methods is a proper superset of the class of grammars that can be parsed with predictive parsers. LL(1)-Grammars  LR(1)-Grammars  An LR-parser can detect a syntactic error as soon as it is possible to do so a left-to-right scan of the input.

2 LR Parsers  LR-Parsers  covers wide range of grammars.  SLR – simple LR parser  LR – most general LR parser  LALR – intermediate LR parser (look-head LR parser)  SLR, LR and LALR work same (they use the same algorithm), only their parsing tables are different.

3 LR Parsing Algorithm SmSmSmSm XmXmXmXm S m- 1 X m- 1.. S1S1S1S1 X1X1X1X1 S0S0S0S0 a1a1a1a aiaiaiai anananan$ Action Table terminals and $ terminals and $s t four different a actions tes Goto Table non-terminal non-terminals t each item is a a state number tes LR Parsing Algorithm stack input output

4 A Configuration of LR Parsing Algorithm  A configuration of a LR parsing is: ( S o X 1 S 1... X m S m, a i a i+1... a n $ ) StackRest of Input  S m and a i decides the parser action by consulting the parsing action table. (Initial Stack contains just S o )  A configuration of a LR parsing represents the right sentential form: X 1... X m a i a i+1... a n $

5 Actions of A LR-Parser 1. shift s -- shifts the next input symbol and the state s onto the stack ( S o X 1 S 1... X m S m, a i a i+1... a n $ )  ( S o X 1 S 1... X m S m a i s, a i+1... a n $ ) 2. reduce A  (or reduce n, where n is a production number)  pop 2*|  | (=r) items from the stack;  then push A and s where s=goto[s m-r,A] ( S o X 1 S 1... X m S m, a i a i+1... a n $ )  ( S o X 1 S 1... X m-r S m-r A s, a i... a n $ )  Output is the reducing production reduce A  3. Accept – Parsing successfully completed 4. Error -- Parser detected an error (an empty entry in the action table)

6 Reduce Action  pop 2*|  | (=r) items from the stack; let us assume that  = Y 1 Y 2...Y r  then push A and s where s=goto[s m-r,A] ( S o X 1 S 1... X m-r S m-r Y 1 S m-r...Y r S m, a i a i+1... a n $ )  ( S o X 1 S 1... X m-r S m-r A s, a i... a n $ )  ( S o X 1 S 1... X m-r S m-r A s, a i... a n $ )  In fact, Y 1 Y 2...Y r is a handle. X 1... X m-r A a i... a n $  X 1... X m Y 1...Y r a i a i+1... a n $

7 (SLR) Parsing Tables for Expression Grammar stateid+*()$ETF 0s5s4123 1s6acc 2r2s7r2r2 3r4r4r4r4 4s5s4823 5r6r6r6r6 6s5s493 7s5s410 8s6s11 9r1s7r1r1 10r3r3r3r3 11r5r5r5r5 Action TableGoto Table 1) E  E+T 2) E  T 3) T  T*F 4) T  F 5) F  (E) 6) F  id

8 Actions of A (S)LR-Parser -- Example stackinputactionoutput 0id*id+id$shift 5 0id5*id+id$reduce by F  id F  id 0F3*id+id$reduce by T  F T  F 0T2*id+id$shift 7 0T2*7id+id$shift 5 0T2*7id5+id$reduce by F  id F  id 0T2*7F10+id$ reduce by T  T*FT  T*F 0T2+id$reduce by E  T E  T 0E1+id$shift 6 0E1+6id$shift 5 0E1+6id5$reduce by F  id F  id 0E1+6F3$reduce by T  F T  F 0E1+6T9$reduce by E  E+TE  E+T 0E1$accept

9 Constructing SLR Parsing Tables – LR(0) Item  An LR(0) item of a grammar G is a production of G a dot at the some position of the right side.  Ex:A  aBb Possible LR(0) Items:A . aBb (four different possibility) A  a. Bb (four different possibility) A  a. Bb A  aB. b A  aB. b A  aBb. A  aBb.  Sets of LR(0) items will be the states of action and goto table of the SLR parser.  A collection of sets of LR(0) items (the canonical LR(0) collection) is the basis for constructing SLR parsers.  Augmented Grammar: G’ is G with a new production rule S’  S where S’ is the new starting symbol.

10 The Closure Operation  If I is a set of LR(0) items for a grammar G, then closure(I) is the set of LR(0) items constructed from I by the two rules: 1. Initially, every LR(0) item in I is added to closure(I). 2. If A  . B  is in closure(I) and B  is a production rule of G; then B .  will be in the closure(I). We will apply this rule until no more new LR(0) items can be added to closure(I).

11 The Closure Operation -- Example E’  E closure({E’ . E}) = E  E+T { E’ . Ekernel items E  TE . E+T T  T*FE . T T  FT . T*F F  (E)T . F F  idF . (E) F . id }

12 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 }

13 Construction of The Canonical LR(0) Collection  To create the SLR parsing tables for a grammar G, we will create the canonical LR(0) collection of the grammar G’.  Algorithm: C is { closure({S’ . S}) } repeat the followings until no more set of LR(0) items can be added to C. for each I in C and each grammar symbol X if goto(I,X) is not empty and not in C add goto(I,X) to C  goto function is a DFA on the sets in C.

14 The Canonical LR(0) Collection -- Example I 0 : E’ .EI 1 : E’  E.I 6 : E  E+.T I 9 : E  E+T. E .E+T E  E.+T T .T*F T  T.*F E .E+T E  E.+T T .T*F T  T.*F E .T T .F E .T T .F T .T*FI 2 : E  T. F .(E) I 10 : T  T*F. T .T*FI 2 : E  T. F .(E) I 10 : T  T*F. T .F T  T.*F F .id T .F T  T.*F F .id F .(E) F .(E) F .id I 3 : T  F.I 7 : T  T*.FI 11 : F  (E). F .id I 3 : T  F.I 7 : T  T*.FI 11 : F  (E). F .(E) F .(E) I 4 : F  (.E) F .id E .E+T E .E+T E .T I 8 : F  (E.) E .T I 8 : F  (E.) T .T*F E  E.+T T .T*F E  E.+T T .F T .F F .(E) F .(E) F .id F .id I 5 : F  id.

15 Transition Diagram (DFA) of Goto Function I0I0 I1I2I3I4I5I1I2I3I4I5 I 6 I 7 I 8 to I 2 to I 3 to I 4 I 9 to I 3 to I 4 to I 5 I 10 to I 4 to I 5 I 11 to I 6 to I 7 id ( F * E E + T T T ) F F F ( ( * (

16 Constructing SLR Parsing Table (of an augumented grammar G’) 1. Construct the canonical collection of sets of LR(0) items for G’. C  {I 0,...,I n } 2. Create the parsing action table as follows If a is a terminal, A .a  in I i and goto(I i,a)=I j then action[i,a] is shift j. If A . is in I i, then action[i,a] is reduce A  for all a in FOLLOW(A) where A  S’. If S’  S. is in I i, then action[i,$] is accept. If any conflicting actions generated by these rules, the grammar is not SLR(1). 3. Create the parsing goto table for all non-terminals A, if goto(I i,A)=I j then goto[i,A]=j 4. All entries not defined by (2) and (3) are errors. 5. Initial state of the parser contains S’ .S

17 Parsing Tables of Expression Grammar stateid+*()$ETF 0s5s4123 1s6acc 2r2s7r2r2 3r4r4r4r4 4s5s4823 5r6r6r6r6 6s5s493 7s5s410 8s6s11 9r1s7r1r1 10r3r3r3r3 11r5r5r5r5 Action TableGoto Table 1-2 E  E + T | T 3-4 T  T * F | F 5-6 T  ( E ) | id

18 SLR(1) Grammar  An LR parser using SLR(1) parsing tables for a grammar G is called as the SLR(1) parser for G.  If a grammar G has an SLR(1) parsing table, it is called SLR(1) grammar (or SLR grammar in short).  Every SLR grammar is unambiguous, but every unambiguous grammar is not a SLR grammar.

19 shift/reduce and reduce/reduce conflicts  If a state does not know whether it will make a shift operation or reduction for a terminal, we say that there is a shift/reduce conflict.  If a state does not know whether it will make a reduction operation using the production rule i or j for a terminal, we say that there is a reduce/reduce conflict.  If the SLR parsing table of a grammar G has a conflict, we say that that grammar is not SLR grammar.

20 Conflict Example S  L=R I 0 : S’ .S I 1 :S’  S. I 6 :S  L=.R S  R S .L=RR .L L  *R S .R I 2 :S  L.=RL .*R L  id L .*RR  L.L .id R  L L .id R .L I 3 :S  R. R .L I 3 :S  R. I 4 :L  *.R I 7 :L  *R. I 4 :L  *.R I 7 :L  *R. ProblemR .L ProblemR .L FOLLOW(R)={=,$}L .*R I 8 :R  L. = shift 6L .id reduce by R  L shift/reduce conflict I 5 :L  id. I 9 : S  L=R.

21 Conflict Example2 S  AaAb I 0 :S’ .S S  BbBaS .AaAb A   S .BbBa B   A . B . ProblemFOLLOW(A)={a,b}FOLLOW(B)={a,b} areduce by A   breduce by A   reduce by B   reduce by B   reduce/reduce conflictreduce/reduce conflict