CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture 5 + 6 Ahmed Ezzat.

Slides:



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

Compiler construction in4020 – lecture 4 Koen Langendoen Delft University of Technology The Netherlands.
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.
Joey Paquet, 2000, 2002, 2008, Lecture 7 Bottom-Up Parsing II.
Review: LR(k) parsers a1 … a2 … an $ LR parsing program Action goto Sm xm … s1 x1 s0 output input stack Parsing table.
CSE 5317/4305 L4: Parsing #21 Parsing #2 Leonidas Fegaras.
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
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
Formal Aspects Term 2, Week4 LECTURE: LR “Shift-Reduce” Parsers: The JavaCup Parser-Generator CREATES LR “Shift-Reduce” Parsers, they are very commonly.
Lecture #8, Feb. 7, 2007 Shift-reduce parsing,
Parsing V Introduction to LR(1) Parsers. from Cooper & Torczon2 LR(1) Parsers LR(1) parsers are table-driven, shift-reduce parsers that use a limited.
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.
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.
Bottom-up parsing Goal of parser : build a derivation
LALR Parsing Canonical sets of LR(1) items
Syntax and Semantics Structure of programming languages.
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.
CS 321 Programming Languages and Compilers Bottom Up Parsing.
Review 1.Lexical Analysis 2.Syntax Analysis 3.Semantic Analysis 4.Code Generation 5.Code Optimization.
11 Outline  6.0 Introduction  6.1 Shift-Reduce Parsers  6.2 LR Parsers  6.3 LR(1) Parsing  6.4 SLR(1)Parsing  6.5 LALR(1)  6.6 Calling Semantic.
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 
Syntax and Semantics Structure of programming languages.
Prof. Necula CS 164 Lecture 8-91 Bottom-Up Parsing LR Parsing. Parser Generators. Lecture 6.
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.
COMP 3438 – Part II-Lecture 6 Syntax Analysis III Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ.
Parsing V LR(1) Parsers. LR(1) Parsers LR(1) parsers are table-driven, shift-reduce parsers that use a limited right context (1 token) for handle recognition.
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.
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.
Eliminating Left-Recursion Where some of a nonterminal’s productions are left-recursive, top-down parsing is not possible “Immediate” left-recursion can.
COMPILER CONSTRUCTION
Syntax and Semantics Structure of programming languages.
Programming Languages Translator
Unit-3 Bottom-Up-Parsing.
UNIT - 3 SYNTAX ANALYSIS - II
Chapter 4 Syntax Analysis.
CS 404 Introduction to Compiler Design
Compiler Construction
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
Canonical LR Parsing Tables
Syntax Analysis Part II
Subject Name:COMPILER DESIGN Subject Code:10CS63
Syntax Analysis - LR(1) and LALR(1) Parsing
Parsing #2 Leonidas Fegaras.
Compiler SLR Parser.
Parsing #2 Leonidas Fegaras.
Kanat Bolazar February 16, 2010
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:

CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture Ahmed Ezzat

CS 404Ahmed Ezzat 2 Review: Bottom-up Parsing Start with strings of terminals Builds up from leaves of parse tree Apply production backwards (reduction) When reach start symbol and exhausted input, done Shift-reduce is one common type of bottom- up parsing

CS 404Ahmed Ezzat 3 Review: Shift-reduce Parsing Use context-free grammar (may not be LL1) Use stack to keep track of tokens seen so far Hard to do manually, but best with Yacc Basic idea – Shift next symbol onto stack – When stack top contains a good right-hand-side of a production, reduce by a rule

CS 404Ahmed Ezzat 4 Construct LR Parsing Table Construct the set of states s0, s1, …, sn Fill Action Table: Action[S,a], a is terminal Fill Goto Table: Goto[S,X], X is non-terminal

CS 404Ahmed Ezzat 5 LR(0) Item Sets An LR(0) item (or item for short) of a grammar G is a production of G with a dot at some position of the right side. For example, A->.XYZ, or A->X.YZ Dot represents how parser has gotten in recognizing the production Items are grouped into sets that give the states of the SLR parser

CS 404Ahmed Ezzat 6 Transition Between Item Sets Parser goes from one state to another based on symbols processed A->X.YZ ->Y->A->XY.Z When a state has a dot at an end of an item, we can reduce by a production rule

CS 404Ahmed Ezzat 7 Function: Closure Find all items that we expect to see at some stage of parsing For a set of items I – Every item in I is in closure(I) – If A->α.Bβ is in closure(I), and B->γ is a production, then add B->. γ to closure(I) – Repeat until no more new items can be added (called a fixed point)

CS 404Ahmed Ezzat 8 Function Goto Goto(I,X), I is a set of items, X is a symbol, terminal or non-terminal Goto(I,X) is the closure of the set of all items [A->αX.β] such that [A->α.Xβ] is in I. (move dot forward over X) Goto(I,X) tells the parser which state to shift (for terminals) or goto (for non-terminals).

CS 404Ahmed Ezzat 9 Construct the SLR Parsing Table Augment the grammar and ass S’->S, S’ is not the new start symbol Let C = { I0=closure(S’->.S)} Repeat until no more items generated – Add each non-empty goto(I,X) to C Each Ii is a state Si in the parsing table

CS 404Ahmed Ezzat 10 Example LR(0) Item Sets StateItem Set XGoto(I,X) I0:E'  EEI1 E  E+TEI1 E  TTI2 T  (E)(I3 T  ididI4 I1: E'  EAccept (dot at end of E’ rule) E  E+T+I5 I2: E  TReduce 2 (dot at end) I3: T  (E)EI6 E  E+TEI6 E  TTI2 T  (E)(I3 T  ididI4 (0) E’  E (1) E  E + T (2) E  T (3) T  (E) (4) T  id

CS 404Ahmed Ezzat 11 Example LR(0) Item Sets State(Item Set) XGoto(I,X) I4:T  idReduce 4 (dot at end) I5: E  E+TTI7 T  (E)(I3 T  ididI4 I6: T  (E))I8 E  E + T+I5 I7: E  E+TReduce 1 (dot at end) I8: T  (E)Reduce 3 (dot at end)

CS 404Ahmed Ezzat 12 Example SLR Parsing Table State ActionGoto id+()$ET 0S4 S S5 accept 2R2R2R2R2R2 3S4 S3 62 4R4R4R4R4R4 5S4 S3 7 6 S5 S8 7R1R1R1R1R1 8R3R3R3R3R3 (1) E  E + T (2) E  T (3) T  (E) (4) T  id

CS 404Ahmed Ezzat 13 More on Grammars SLR(1) – we just learned LR(1) – Split items to resolve shift/reduce error – Table too large LALR – Merge similar LR(1) states, smaller table – Handles more language than SLR(1)

CS 404Ahmed Ezzat 14 Error Repair and Recovery In the presence of an error, a compiler can (crash) Detect Report Repair (guess) Recover (continue)

CS 404Ahmed Ezzat 15 Error Handling Goals Detect errors as soon as possible Report errors clearly and accurately Recover soon enough to continue processing (find more than one error) Don’t significantly delay compilation of correct programs

CS 404Ahmed Ezzat 16 Error Detection In lexical analysis – Invalid characters, incomplete strings, file ending without EOF In syntax analysis – Input violates grammar, cannot continue parsing – But when did the error begin? – Rule: can only report when error occur (when the parsing table entry is empty or error)

CS 404Ahmed Ezzat 17 Error Reporting Include context information of an error – Source file location – Tokens before and after – Name of current routine or module – Report what was found and what was expected

CS 404Ahmed Ezzat 18 Error Repair Fix code – Example: add “end of comment” – Example: add “;” Replace Keyword – Example: duoble -> double (expecting a keyword, got an id)

CS 404Ahmed Ezzat 19 Error Recovery Global correction – If X not in L(G), find a similar X’ in L(G) Panic mode – Throw away incoming token until a “good” one is found. For example, until }, ; or EOL – May miss some errors

CS 404Ahmed Ezzat 20 Error Recovery (cont.) Phrase level recovery – Replace incoming token with a “correct” one Use error productions – Have grammar rules that match common error patterns, then have corresponding actions