Prepared By: NITIN GOYAL - 04CS1026 JITENDRA BINDAL - 04CS1027

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 Designs and Constructions
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.
Compiler Construction Sohail Aslam Lecture Finite Automaton of Items Then for every item A →  X  we must add an  -transition for every production.
CS 31003: Compilers  Difference between SLR and LR(1)  Construction of LR(1) parsing table  LALR parser Bandi Sumanth 11CS30006 Date : 9/10/2013.
LR Parsing Table Costruction
Bhaskar Bagchi (11CS10058) Lecture Slides( 9 th Sept. 2013)
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.
CH4.1 CSE244 More on LR Parsing Aggelos Kiayias Computer Science & Engineering Department The University of Connecticut 371 Fairfield Road, Unit 1155 Storrs,
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
LESSON 24.
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.
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 
Ambiguity in Grammar By Dipendra Pratap Singh 04CS1032.
Chapter 5: Bottom-Up Parsing (Shift-Reduce)
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.
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.
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.
March 1, 2009 Dr. Muhammed Al-Mulhem 1 ICS 482 Natural Language Processing SLR Parsing Muhammed Al-Mulhem March 1, 2009.
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.
Overview of Previous Lesson(s) Over View  Structure of the LR Parsing Table  Consists of two parts: a parsing-action function ACTION and a goto function.
Announcements/Reading
Lec04-bottomupparser 4/13/2018 LR Parsing.
SLR Parser Problems.
Bottom-up Parsing.
Introduction to LR Parsing
Bottom-Up Parsing.
Compiler Baojian Hua LR Parsing Compiler Baojian Hua
The role of parser Lexical Analyzer Parser Rest of Front End Symbol
Unit-3 Bottom-Up-Parsing.
UNIT - 3 SYNTAX ANALYSIS - II
Bottom-up Parsing.
Table-driven parsing Parsing performed by a finite state machine.
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
Simple, efficient;limitated
Canonical LR Parsing Tables
Syntax Analysis Part II
Subject Name:COMPILER DESIGN Subject Code:10CS63
Syntax Analysis - LR(1) and LALR(1) Parsing
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
LALR Parsing Adapted from Notes by Profs Aiken and Necula (UCB) and
Compiler Construction
Chapter 4. Syntax Analysis (2)
Compiler SLR Parser.
Bottom-up Parsing.
Prepared by VISHWA DEEPAK 04CS1029
Syntax Analysis - 3 Chapter 4.
Kanat Bolazar February 16, 2010
USING AMBIGUOUS GRAMMARS
Chapter 4. Syntax Analysis (2)
Parsing Bottom-Up LR Table Construction.
Parsing Bottom-Up LR Table Construction.
Chap. 3 BOTTOM-UP PARSING
Lecture 11 LR Parse Table Construction
COP 4620 / 5625 Programming Language Translation / Compiler Writing Fall 2003 Lecture 7, 10/09/2003 Prof. Roy Levow.
Compiler design Bottom-up parsing: Canonical LR and LALR
Presentation transcript:

Prepared By: NITIN GOYAL - 04CS1026 JITENDRA BINDAL - 04CS1027 LR(1) & LALR PARSER Prepared By: NITIN GOYAL - 04CS1026 JITENDRA BINDAL - 04CS1027

INTRODUCTION The LALR ( Look Ahead-LR ) parsing technique is between SLR and Canonical LR, both in terms of power of parsing grammars and ease of implementation. This method is often used in practice because the tables obtained by it are considerably smaller than the Canonical LR tables, yet most common syntactic constructs of programming languages can be expressed conveniently by an LALR grammar. The same is almost true for SLR grammars, but there are a few constructs that can not be handled by SLR techniques.

CONSTRUCTING LALR PARSING TABLES A core is a set of LR (0) (SLR) items for the grammar, and an LR (1) (Canonical LR) grammar may produce more than two sets of items with the same core. The core does not contain any look ahead information. Example: Let s1 and s2 are two states in a Canonical LR grammar. S1 – {C ->c.C, c/d; C -> .cC, c/d; C -> .d, c/d} S2 – {C ->c.C, $; C -> .cC, $; C -> .d, $} These two states have the same core consisting of only the production rules without any look ahead information.

CONSTRUCTION IDEA: Construct the set of LR (1) items. Merge the sets with common core together as one set, if no conflict (shift-shift or shift-reduce) arises. If a conflict arises it implies that the grammar is not LALR. The parsing table is constructed from the collection of merged sets of items using the same algorithm for LR (1) parsing.

ALGORITHM: Input: An augmented grammar G’. Output: The LALR parsing table actions and goto for G’.

Method: Construct C= {I0, I1, I2,…, In}, the collection of sets of LR(1) items. For each core present in among the set of LR(1) items , find all sets having the core, and replace these sets by their union. Parsing action table is constructed as for Canonical LR. The goto table is constructed by taking the union of all sets of items having the same core. If J is the union of one or more sets of LR (1) items, that is, J=I1 U I2 U … U Ik, then the cores of goto(I1,X), goto(I2,X),…, goto(Ik, X) are the same as all of them have same core. Let K be the union of all sets of items having same core as goto(I1, X). Then goto(J,X)=K.

EXAMPLE GRAMMAR: S’ -> S S -> CC C -> cC C -> d

SET OF ITEMS: I0 : S’ -> .S, $ S -> .CC, $ C -> .c C, c /d C -> .d, c /d I1: S’ -> S., $ I2: S -> C.C, $ C -> .Cc, $ C -> .d, $ I3: C -> c. C, c /d C -> .Cc, c /d I4: C -> d., c /d I5: S -> CC., $ I6: C -> c.C, $ C -> .cC, $ C -> .d, $ I7: C -> d., $ I8: C -> cC., c /d I9: C -> cC., $

The goto graph:

CANONICAL PARSING TABLE

LALR PARSER Merge the Cores: 3 & 6 4 & 7 8 & 9

LALR PARSING TABLE

SHIFT-REDUCE CONFLICT COMPARISON OF LR (1) AND LALR: If LR (1) has shift-reduce conflict then LALR will also have it. If LR (1) does not have shift-reduce conflict LALR will also not have it. Any shift-reduce conflict which can be removed by LR (1) can also be removed by LALR. For cases where there are no common cores SLR and LALR produce same parsing tables.

SHIFT-REDUCE CONFLICT LR(1) Parser- How Does a shift-Reduce conflict arises? IA : { E->α..βc, F->γ. } & FOLLOW(F)={β} For Eg. I3 : {E->L.=R , R-> L. } & Follow(E)={=} For Solving this Problem by LALR Parser: IA={E->α.βc,ω1 ; F->γ.,δ1} IB={E->α.βc,ω2 ; F->γ.,δ2} IAB={E->α.βc,ω1|ω2 ; F->γ.,δ1|δ2}

SHIFT-REDUCE CONFLICT COMPARISON OF SLR AND LALR: If SLR has shift-reduce conflict then LALR may or may not remove it. SLR and LALR tables for a grammar always have same number of states. Hence, LALR parsing is the most suitable for parsing general programming languages.The table size is quite small as compared to LR (1) , and by carefully designing the grammar it can be made free of conflicts. For example, in a language like Pascal LALR table will have few hundred states, but a Canonical LR will have thousands of states. So it is more convenient to use an LALR parsing.

REDUCE-REDUCE CONFLICT The Reduce-Reduce conflicts still might just remain .This claim may be better comprehended if we take the example of the following grammar: IA={ A->γ1.,δ1 ; ->γ2.,δ2} If δ1≠δ2 then NO reduce-reduce conflict. (mutual exclusive sets) IB={ A->γ1.,δ3 ; B->γ2.,δ4} IAB={ A->γ1.,δ1|δ3 ; B->γ2.,δ2|δ4} Conflicts arises when δ1=δ4 or δ2=δ3.

Example of R-R Conflict S'-> S S -> aAd S -> bBd S -> aBe S -> bAe A -> c B -> c

Generating the LR (1) items for the above grammar I0 : S’-> .S , $ S-> . aAd, $ S-> . bBd, $ S-> . aBe, $ S-> . bAe, $ I1: S’-> S ., $ I2: S-> a . Ad, $ S-> a . Be, $ A-> .c, d B->.c, e I3: S-> b . Bd, $ S-> b . Ae, $ A->.c, e B->.c,d I4: S->aA.d, $

Generating the LR (1) items for the above grammar I5: S-> aB.e,$ I6: A->c. , d ; B->c. , e I7: S->bB.d, $ I8: S->bA.e, $ I9: B->c. ,d ; A->c. , e I10: S->aAd. , $ I11: S->aBe., $ I12: S->bBd., $ I13: S->aBe., $ The underlined items are of our interest. We see that when we make the Parsing table for LR (1), we will get something like this…

The LR (1) Parsing Table

The LALR Parsing table LR(1) Parsing table on reduction to the LALR parsing table

Conclusion So, we find that the LALR gains reduce-reduce conflict whereas the corresponding LR (1) counterpart was void of it. This is a proof enough that LALR is less potent than LR (1). But, since we have already proved that the LALR is void of shift-reduce conflicts (given that the corresponding LR(1) is devoid of the same), whereas SLR (or LR (0)) is not necessarily void of shift-reduce conflict, the LALR grammar is more potent than the SLR grammar

Conclusion SHIFT-REDUCE CONFLICT present in SLR  (Some of them are solved in….) LR (1)  (All those solved are preserved in…) LALR So, we have answered all the queries on LALR that we raised intuitively.

THE END Visit: http://www.facweb.iitkgp.ernet.in/~niloy/COURSE/Autumn2006/Compiler/main.html#Lecture