Compiler Construction

Slides:



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

Lesson 8 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Compiler Designs and Constructions
Compiler Principles Fall Compiler Principles Lecture 4: Parsing part 3 Roman Manevich Ben-Gurion University.
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.
CS 31003: Compilers  Difference between SLR and LR(1)  Construction of LR(1) parsing table  LALR parser Bandi Sumanth 11CS30006 Date : 9/10/2013.
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)
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.
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 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.
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.
 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 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.
1 Syntax Analysis Part II Chapter 4 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2005.
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.
Announcements/Reading
Lec04-bottomupparser 4/13/2018 LR Parsing.
Bottom-up 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 Bottom-Up-Parsing.
UNIT - 3 SYNTAX ANALYSIS - II
Bottom-up Parsing.
Syntactic Analysis and Parsing
Compiler Construction
CS 404 Introduction to Compiler Design
Compiler design Bottom-up parsing: Canonical LR and LALR
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 - LR(1) and LALR(1) Parsing
LR Parsing – The Tables Lecture 11 Wed, Feb 16, 2005.
Parsing #2 Leonidas Fegaras.
Lecture 8 Bottom Up Parsing
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Compiler Design 7. Top-Down Table-Driven Parsing
Compiler SLR Parser.
Kanat Bolazar February 16, 2010
Announcements HW2 due on Tuesday Fall 18 CSCI 4430, A Milanova.
Parsing Bottom-Up LR Table Construction.
Parsing Bottom-Up LR Table Construction.
Chap. 3 BOTTOM-UP PARSING
Lecture 11 LR Parse Table Construction
Compiler design Bottom-up parsing: Canonical LR and LALR
Presentation transcript:

Compiler Construction Chapter 5 Compiler Construction Dr K. V. N. Sunitha

Task: detect the handle and reduce the handle. Bottom Up Parser/SR Parser Task: detect the handle and reduce the handle. Compiler Construction Dr K. V. N. Sunitha

Compiler Construction LR Parsing Program Let ‘X’ be the state on top of the stack and ‘a’ be the symbol pointed by ‘ip’. Repeat for ever if action[X, a]= Si, then (Shift action) Push ‘a’ then state I onto stack. Advance the input pointer to the next symbol. Compiler Construction Dr K. V. N. Sunitha

Compiler Construction if action[X,a]= ri, then (Reduce action) { Take ri i.e ith production from the grammar. Let ri is A → β . Pop 2 * |β| symbols from stack and replace them by nonterminal A. If Xi is the state below the nonterminal A, then push goto[Xi,A] onto the stack. Output the production A → β .} Now continue parsing. if action[X,a]= accept, then (Successful completion action) Compiler Construction Dr K. V. N. Sunitha

Compiler Construction Example w = aabb (1) S → AA (2) A → aA (3) A → b State Action Goto a b $ S A S3 S4 1 2 acc 5 3 r 4 6 4 r 3 8 r 1 r 2 Compiler Construction Dr K. V. N. Sunitha

Compiler Construction LR Grammars LR grammar A grammar is said to be an LR grammar if we can construct a parsing table for it. LR(k) grammar lookahead of up to k input symbols LR(0), SLR(1), LR(1), and LALR(1) grammars Compiler Construction Dr K. V. N. Sunitha

Types of Bottom Up Parsers Compiler Construction Dr K. V. N. Sunitha

Construction of LR Parsing Table Given grammar, take augmented grammar. Create canonical collection of LR items. Draw DFA and prepare table. LR(0) SLR(1) LALR(1) CLR(1) LR(0) ITEMS LR(1) ITEMS Compiler Construction Dr K. V. N. Sunitha

Compiler Construction Augment the grammar with production: Add S’ S to G where S is start symbol and S’ not in V. This rule helps the parser to understand when to stop parsing and announce the successful completion of the process. Compiler Construction Dr K. V. N. Sunitha

Compiler Construction LR(0) Item/Item A production with a dot at some position on the RHS is known as Item A → XYZ produces 4 items. A →.XYZ A → X . YZ A → XY . Z A → XY Z. . Indicates how far right hand side of production is seen by parser. Compiler Construction Dr K. V. N. Sunitha

Compiler Construction Closure (I) Initially add every item from input to output. If A → α•Bβ is in I where B is nonterminal & B → ૪ is the rule for B then add B → •૪ to Closure (I). Repeat this for every newly added item. Compiler Construction Dr K. V. N. Sunitha

Closure of a Set of Items I Example E' →E E → E + T | T T → T * F | F F → id |(E) closure({E' → .E}) = ? Compiler Construction Dr K. V. N. Sunitha

Compiler Construction The Goto Operation Goto function defines the transition in the set of items I on seeing the grammar symbol X. goto(I,X) = closure({A → aX.b |A → a.Xb is in I}) The goto Operation Example I = {E' → .E , E → E . + T} goto(I,+) = ? Compiler Construction Dr K. V. N. Sunitha

Canonical LR(0) Collection of Set of Items C={I0, I1, I2…} The initial item in C is I0 = closure (augmented production with dot at the beginning) for Example in the above grammar, I0= closure(E' → •E ) For each Ii in C and each grammar symbol X in G Repeat While goto (I,X) is not empty and not in C Add goto (I,X) to C Until no more set of items can be added to C. Compiler Construction Dr K. V. N. Sunitha

Compiler Construction LR(0) Parsers No lookahead How to produce table? Consider grammar: S’ S S  (L) S  x L  S L  L , S Compiler Construction Dr K. V. N. Sunitha

Compiler Construction LR(0) Parsers Initial State (S1) S’  . S S  . x S  . (L) If we shift x , we get state (S2) S  x. From S1, if we shift (, we get S3 S  ( . L) L  . L,S L  . S Compiler Construction Dr K. V. N. Sunitha

Compiler Construction S’  . S $ S  . x S  . (L) x S2: S  x . S3: S  ( . L) L  . L,S L  . S S1: S’  S . ( S5: L S . S S4: S (L . ) L  L . , S L ) S6: S  (L) . , S7: L  L , . S S. (L ) S  .x S8: L  L , S . Compiler Construction Dr K. V. N. Sunitha

LR(0) Parsing – Table Construction X For each edge I  J if X is a terminal, put shift entry as SJ at action[I,X] if X is a non-terminal, put goto [I,X] as J For each state I containing S’  S. put accept at state I. For each state containing A  γ. Put reduce A  γ at (I, Z) for every token Z. Compiler Construction Dr K. V. N. Sunitha

LR(0) Parsing Table - Example , $ S L s3 s2 1 Accepting state 2 r2 3 5 4 s6 s7 r3 6 r1 7 8 r4 S  (L) S  x L  S L  L , S Compiler Construction Dr K. V. N. Sunitha

Compiler Construction Parsing Conflicts A  .a r2:S  c. a I1 A  x. S  c. I0 I0 SR conflict RR conflict LR(0) a b $ S1/r2 r2 LR(0) a b $ r1/r2 A conflict produces multiple entries in table. Compiler Construction Dr K. V. N. Sunitha

LR(0) Parsing - Limitation Consider the grammar: S  E E  T + E E  T T  x Compiler Construction Dr K. V. N. Sunitha

Compiler Construction S  E . S0: S . E E  . T + E E  . T T  .x T S4: E  T + . E E  . T + E E  . T T  .x S2: E  T . + E E  T . + S3: T  x. x T E S5: E  T + E . In state S3, should we shift or do we reduce? We need to look ahead (or do we?) Compiler Construction Dr K. V. N. Sunitha

Shift-Reduce Conflict x + $ E T s5 1 2 acc r2 S4,r2 Mutliple entries denote Conflicting actions: Shift vs. Reduce (Other possibilities: Reduce vs. Reduce.) Compiler Construction Dr K. V. N. Sunitha

Compiler Construction SLR Parser Generation Simple LR – slightly better than LR(0) Change the rule adding reductions to the table. For each state containing A  γ. Put reduce A  γ at (I, Z) for each token Z in FOLLOW(A) Reconsider the previous example: S  E$ E  T + E E  T T  x Compiler Construction Dr K. V. N. Sunitha

Compiler Construction S  . E E  . T + E E  . T T  .x E S1: S  E . S2: E  T . + E E  T . T S5: E  T+ E. S3: T  x. x S4: E  T + . E + S3 Compiler Construction Dr K. V. N. Sunitha

Compiler Construction SLR Parsing Table x + $ E T s3 1 2 acc s4 r2 3 r3 4 5 r1 S3: E  T . + E E  T . Conflict resolved in favor of shift Compiler Construction Dr K. V. N. Sunitha

Compiler Construction LR(1) Items Algorithm for constructing parsing table similar to that for LR(0). But LR(1) items include a look-ahead symbol as well: Compiler Construction Dr K. V. N. Sunitha

Compiler Construction LR(1) Parsing Tables LR(1) item – LR(0) item + look ahead terminal [A→a. ,a] – reduce a to A only if the next symbol is a Compiler Construction Dr K. V. N. Sunitha

LR(1) Parsing Table Construction Closure(I) : j=I; repeat for any item (A  α . X β, z) in I for any production X  γ add { (X  . γ, w) } to J until no more items can be added to J. for any w in FIRST(βz) Goto(I, X) = J := {} for any item (A  α . X β , z) in I add (A  α X . β, z) to J return Closure(J) Compiler Construction Dr K. V. N. Sunitha

Compiler Construction Example - S → AA A → aA A → b Construct LR(1) and LALR(1) parsers Compiler Construction Dr K. V. N. Sunitha

Compiler Construction Dr K. V. N. Sunitha

LR(1) Parsing – Table Construction For each edge I  J if X is a terminal, put shift entry as SJ at action[I,X] if X is a non-terminal, put goto [I,X] as J For each state I containing S’  S., $ put accept at state I on $. For each state containing A  γ. ,Z Put reduce A  γ at (I, Z) for token Z. Compiler Construction Dr K. V. N. Sunitha

Compiler Construction LR(1) Table state a b $ S A s3 s4 1 2 acc s6 s7 5 3 8 4 r3 r1 6 9 7 r2 Compiler Construction Dr K. V. N. Sunitha

Compiler Construction LALR(1) Tables If two (or more) states in an LR(1) PDA are identical except for look ahead, then the states can be merged This leads to a compressed table Efficient look ahead may become important for large grammars. But this may introduce conflicts not present in the LR(1) tables Compiler Construction Dr K. V. N. Sunitha

Compiler Construction LALR(1) Table state a b $ S A s36 s47 1 2 acc 5 36 89 47 r3 r1 r2 For a given grammar, no.of states of SLR(1) and LALR(1) is always equal. Compiler Construction Dr K. V. N. Sunitha

Compiler Construction Parsing Strengths LL(0) < LL(1) < LL(2) < … LR(0) < LR(1) < LR(2) <… LR(0) < SLR < LALR(1) < LR(1) LL(k) < LR(k) for any k LL(1) ≠ LR(0) An ambiguous grammar is not LL(k) nor LR(k) for any k. Compiler Construction Dr K. V. N. Sunitha