Syntax Analysis - LR(0) Parsing 66.648 Compiler Design Lecture (02/04/98) Computer Science Rensselaer Polytechnic.

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
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.
Mooly Sagiv and Roman Manevich School of Computer Science
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)
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 Bottom Up Parsing. 2 Bottom-Up Parsing l Bottom-up parsing is more general than top-down parsing »And just as efficient »Builds on ideas in top-down.
Pertemuan 12, 13, 14 Bottom-Up Parsing
1 CMPSC 160 Translation of Programming Languages Fall 2002 slides derived from Tevfik Bultan, Keith Cooper, and Linda Torczon Lecture-Module #10 Parsing.
Formal Aspects Term 2, Week4 LECTURE: LR “Shift-Reduce” Parsers: The JavaCup Parser-Generator CREATES LR “Shift-Reduce” Parsers, they are very commonly.
Parsing VI The LR(1) Table Construction. LR(k) items The LR(1) table construction algorithm uses LR(1) items to represent valid configurations of an LR(1)
More SLR /LR(1) Professor Yihjia Tsai Tamkang University.
Lecture #8, Feb. 7, 2007 Shift-reduce parsing,
CS 536 Spring Bottom-Up Parsing: Algorithms, part 1 LR(0), SLR Lecture 12.
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.
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 CIS 461 Compiler Design & Construction Fall 2012 slides derived from Tevfik Bultan, Keith Cooper, and Linda Torczon Lecture-Module #12 Parsing 4.
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
 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.
Syntax and Semantics Structure of programming languages.
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.
1 Natural Language Processing Lecture 11 Efficient Parsing Reading: James Allen NLU (Chapter 6)
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.
Review 1.Lexical Analysis 2.Syntax Analysis 3.Semantic Analysis 4.Code Generation 5.Code Optimization.
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 
Lesson 9 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
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.
Syntax Analysis - Parsing Compiler Design Lecture (01/28/98) Computer Science Rensselaer Polytechnic.
10/10/2002© 2002 Hal Perkins & UW CSED-1 CSE 582 – Compilers LR Parsing Hal Perkins Autumn 2002.
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.
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.
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture Ahmed Ezzat.
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.
Programming Languages Translator
UNIT - 3 SYNTAX ANALYSIS - II
Table-driven parsing Parsing performed by a finite state machine.
Compiler Construction
Fall Compiler Principles Lecture 4: Parsing part 3
Bottom-Up Syntax Analysis
Syntax Analysis Part II
Subject Name:COMPILER DESIGN Subject Code:10CS63
Syntax Analysis - LR(1) and LALR(1) Parsing
Chapter 4. Syntax Analysis (2)
Syntax Analysis - Parsing
Kanat Bolazar February 16, 2010
Announcements HW2 due on Tuesday Fall 18 CSCI 4430, A Milanova.
Chapter 4. Syntax Analysis (2)
Chap. 3 BOTTOM-UP PARSING
Presentation transcript:

Syntax Analysis - LR(0) Parsing Compiler Design Lecture (02/04/98) Computer Science Rensselaer Polytechnic

Lecture Outline LR(0) Parsing Algorithm LR(0) Parsing Algorithm Parse Tables Parse Tables Examples Examples Administration Administration

LR(k) Parsing Algorithms This is an efficient class of Bottom-up parsing algorithms. The other bottom-up parsers include operator precedence parsers. The name LR(k) means: L - Left-to-right scanning of the input R - Constructing rightmost derivation in reverse k - number of input symbols to select a parser action

Yet Another Example Consider a grammar to generate all palindromes. Consider a grammar to generate all palindromes. 1) S--> P 2) P --> a Pa 3) P --> b P b 4) P --> c LR parsers work with an augmented grammar in which the start symbol never appears in the right side of a production. In a given grammar, if the start symbol appears in the RHS, we can add a production S’ --> S (S’ is the new start symbol and S was the old start symbol)

STACKINPUT BUFFERACTION $abcba$shift $abcba$reduce $abPba$shift $abPba$reduce $aPa$shift $aPa$reduce $P$reduce $S$accept Example Cont...

Qn: How to select parser actions (namely shift, reduce, accept and error)? Ans: 1) By constructing a DFA that encodes all parser states, and transitions on terminals and nonterminals. The transitions on terminals are the parser actions( also called the action table) and transitions on nonterminals resulting in a new state (also called the goto table). 2) Keeping a stack to simulate the PDA. This stack maintains the list of states. LR(0) Parsers

LR(0) Items and Closure LR(0) parser state needs to capture how much of a given production we have scanned. LR(0) parser (like a FSA) needs to know how much the production (on the rhs) we have scanned so far. For example: in the production: P --> a P a An LR(0) item is a production with a mark/dot on the RHS. SO the items for this production will be P-->. a P a, P --> a. P a, P --> a P. a, P--> aPa.

Items and Closure Contd Intuitively, there is a derivation (or we have seen the input symbols) to the left of dot. Two kinds of items, kernel items and nonkernel items - Kernel and nonkernel items. Kernel Items - Includes initial item S’ -->.S and all items in which dot does not appear at the left most position. Nonkernel Items- All other items which have dots at the leftmost position.

Closure of Items Let I be the set of items. Then Closure (I) consists of the set of items that are constructed as follows: 1) Every item I is also in the Closure(I) - reflexive 2 If A --. alpha. B beta is in Closure(I), and B--> gamma is production, then add the item B-->.gamma also in the Closure(I), if it is not already a member. Repeat this until no more items can be added.

Intuition Closure represents an equivalent state - all the possible ways that you could have reached that state. Example: I = { S-->.P} Closure (I) = { S-->.P,P-->.aPa,P-->.bPb,P-->.c} In Arithmetic Expression: S’-->.E closure(I)={ }

GOTO Operation Let I be the set of items and let X be a grammar symbol (nonterminal/terminal). Then GOTO(I,X) = Closure({A--> alpha X.beta| A--> alpha. X beta is in I}) It is a new set of items moving a dot over X. Intuitively, we have seen either an input symbol (terminal symbol) or seen a derivation starting with that nonterminal.

Canonical set of Items (states) Enumerate possible states for an LR(0) parser. Each state is a canonical set of items. Algorithm: 1) Start with a canonical set, Closure({S’-->.S}) 2) If I is a canonical set and X is a grammar symbol such that I’=goto(I,X) is nonempty, then make I’ a new canonical set (if it is not already a canonical set). Keep repeating this until no more canonical sets can be created. The algorithm terminates!!.

Example S0: S-->.P, P -->.a P a, P-->.bP b, P-->.c S1: S--> P. S2: P --> a.Pa, P-->.aPa,P-->.bPb,P-->.c S3:P--> b.P b, P-->.aPa,P-->.bPb,P-->.c S4: P--> c. S5: P--> aP.a S6:P--> bP.b S7: P--> aPa. S8: P--> bP b.

Finite State Machine Draw the FSA. The major difference is that transitions can be both terminal and nonterminal symbols.

Key Idea in Canonical states If a state contains an item of the form A--> beta., then state prompts a reduce action (provided the correct symbols follow). If a state contains A--> alpha. delta, then the state prompts the parser to perform a shift action (of course on the right symbols). If a state contains S’--> S. and there are no more input symbols left, then the parser is prompted to accept. Else an error message is prompted.

Prasing Table state Input symbolgoto a b c $P 0 s2 s3 s acc 2. s2 s3 s s2 s3 s r3 r3 5. s7 6. s8 7. r1 r1 r1 r1 8. r2 r2 r2 r2

Parsing Table Contd si means shift the input symbol and goto state I. rj means reduce by jth production. Note that we are not storing all the items in the state in our table. example: abcba$ if we go thru, parsing algorithm, we get

Example Contd Stateinputaction $S0abcba$shift $S0aS2bcba$shift $S0aS2bS3cba$shist $S0aS2bS3cS4ba$reduce

Shift/Reduce Conflicts An LR(0) state contains aconflict if its canonical set has two items that recommend conflicting actions. shift/reduce conflict - when one item prompts a shift action, the other prompts a reduce action. reduce/reduce conflict - when two items prompt for reduce actions by different production. A grammar is said be to be LR(0) grammar, if the table does not have any conflicts.

LALR Grammar Programming languages cannot be generated by LR(0) grammar. We usually have a look ahead symbol, to deteremine what kind of action parser will be prompted for. These lookaheads refine states and actions.

Comments and Feedback Project 2 will be in the web by Friday (this). Please keep reading chapter 4 and understand the material. Work out as many exercises as you can.