SLR PARSING TECHNIQUES Submitted By: Abhijeet Mohapatra 04CS1019.

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 Theory – 08/10(ppt) By Anindhya Sankhla 11CS30004 Group : G 29.
Compiler Designs and Constructions
Compilation (Semester A, 2013/14) Lecture 6a: Syntax (Bottom–up parsing) Noam Rinetzky 1 Slides credit: Roman Manevich, Mooly Sagiv, Eran Yahav.
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.
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
CS 536 Spring Bottom-Up Parsing: Algorithms, part 1 LR(0), SLR Lecture 12.
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.
Shift/Reduce and LR(1) Professor Yihjia Tsai Tamkang University.
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.
LESSON 24.
410/510 1 of 21 Week 2 – Lecture 1 Bottom Up (Shift reduce, LR parsing) SLR, LR(0) parsing SLR parsing table Compiler Construction.
LR(k) Parsing CPSC 388 Ellen Walker Hiram College.
Compiler Principle and Technology Prof. Dongming LU Mar. 21th, 2014.
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 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.
Chapter 5: Bottom-Up Parsing (Shift-Reduce)
Syntax Analysis - LR(0) Parsing Compiler Design Lecture (02/04/98) Computer Science Rensselaer Polytechnic.
1 Syntax Analysis Part II Chapter 4 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2005.
10/10/2002© 2002 Hal Perkins & UW CSED-1 CSE 582 – Compilers LR Parsing Hal Perkins Autumn 2002.
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.
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.
COMPILER CONSTRUCTION
Lec04-bottomupparser 4/13/2018 LR Parsing.
Programming Languages Translator
UNIT - 3 SYNTAX ANALYSIS - II
CS 488 Spring 2012 Lecture 4 Bapa Rao Cal State L.A.
Table-driven parsing Parsing performed by a finite state machine.
Syntactic Analysis and Parsing
CS 404 Introduction to Compiler Design
Compiler Construction
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
LR Parsing – The Tables Lecture 11 Wed, Feb 16, 2005.
Syntax Analysis — Parser
Lecture 9 SLR Parse Table Construction
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Chapter 4. Syntax Analysis (2)
Compiler SLR Parser.
Compiler Construction
Compiler Construction
Compiler Construction
Chapter 4. Syntax Analysis (2)
Chap. 3 BOTTOM-UP PARSING
CS 44 – Jan. 31 Parsing Running a parse machine √
Presentation transcript:

SLR PARSING TECHNIQUES Submitted By: Abhijeet Mohapatra 04CS1019.

A Simple LR parser or SLR parser is an LR parser for which the parsing tables are generated as for an LR(0) parser except that it only performs a reduction with a grammar rule A → w if the next symbol on the input stream is in the follow set of A

CONSTRUCTION OF SLR – PARSING TABLES INPUT: Grammar G with production rules. INPUT: Grammar G with production rules. CENTRAL IDEA CENTRAL IDEA To construct from the input grammar, a DFA to recognize viable prefixes. This is done by grouping items into sets which can be visualized as subset construction from the states of a NFA recognizing the viable prefixes.

Item: An LR (0) item or simply, an item of a grammar G is a production of G with a dot ‘.’ at some position of the right side. For example, the production A  XYZ yields four items, Item: An LR (0) item or simply, an item of a grammar G is a production of G with a dot ‘.’ at some position of the right side. For example, the production A  XYZ yields four items, A .XYZ A .XYZ A  X.YZ A  X.YZ A  XY.Z A  XY.Z A  XYZ. A  XYZ. A production rule of the form A   yields only one item A .. Intuitively, an item shows how much of a production we have seen till the current point in the parsing procedure. A production rule of the form A   yields only one item A .. Intuitively, an item shows how much of a production we have seen till the current point in the parsing procedure.

 Augmented Grammar G’: This equals G  {S’  S} where S is the start state of G. The start state of G’ = S’. This is done to signal to the parser when the parsing should stop to announce acceptance of input.

Closure Operation: If I[] is a set of items for a grammar G, Closure Operation: If I[] is a set of items for a grammar G, closure(I) = I  {B .  | (A  .B   closure(I))  ((B   )  grammar G}

Goto Operation: For a set of items I, and grammar symbol X, Goto Operation: For a set of items I, and grammar symbol X, goto(I, X) = { Closure (all items containing A   X.  ) such that A  .X  is in I} = set of items that are valid for the viable prefix  X, where I is valid for some viable prefix . = set of items that are valid for the viable prefix  X, where I is valid for some viable prefix .

Kernel and Non-Kernel items: Kernel items include the set of items that do not have the dot at leftmost end, along with S’ .S Kernel and Non-Kernel items: Kernel items include the set of items that do not have the dot at leftmost end, along with S’ .S Non-kernel items are the items which have the dot at leftmost end. Non-kernel items are the items which have the dot at leftmost end.

void items (grammar G){ set C; set C; int i, j, n; int i, j, n; C = {closure ({[S’ .S]})}; C = {closure ({[S’ .S]})}; do{ do{ n = 0; n = 0; for (i = 0; i < size(C); i++) for (i = 0; i < size(C); i++) for (j = 0; G.symbol[j] != 0; j++) if (size(goto(C[i],X)) > 0 && !iselement(goto(C[i],X),C) ) if (size(goto(C[i],X)) > 0 && !iselement(goto(C[i],X),C) ) { C = union(C, goto(C[i],X)); n++;} { C = union(C, goto(C[i],X)); n++;} } while (n != 0); } while (n != 0); } // size of items in grammar G.

Example: Grammar G: S’  S S  aABe A  Abc A  b B  d

Set of items generated:- I0 = {[S’ .S], [S .aABe]} I1 = {S’  S.} I2 = {S  a.ABe, A .Abc, A .b} I3 = {S  aA.Be, A  A.bc, B .d} I4 = {A  b.} I5 = {S  aAB.e} I6 = {A  Ab.c} I7 = {B  d.} I8 = {S  aABe.} I9 = {A  Abc.}

The table for parsing action and goto function for the given grammar is:- Where 1. rj means reduce by production numbered j, 2. acc means accept 3. si means shift and stack state i. 4. blank means error.

GOTO GRAPH Transition Rules: If there is a transition from, If there is a transition from, A->α. Xβ to A-> αX. β A->α. Xβ to A-> αX. β then, the transition in the GOTO Graph is labeled X then, the transition in the GOTO Graph is labeled X If there is a transition If there is a transition A-> α. Bβ to B->.γ A-> α. Bβ to B->.γ then, the transition is labeled ε, and since we take ε- closure of all items, these productions lie in the same item as that of A->α. Bβ to A-> αB. β. then, the transition is labeled ε, and since we take ε- closure of all items, these productions lie in the same item as that of A->α. Bβ to A-> αB. β.

RULES FOR FILLING THE SLR PARSE TABLE The Rules for filling the action entries in the table are: 1.If [ A-> α.a β ] is in Ii and goto ( Ii, a ) = Ij, then set action [i,a] to “shift j “.Here a must be a terminal. 2. If [A-> α.] is in Ii, then set action[i, a ] to “reduce A -> α “ for all a in FOLLOW (A); here A may not be in S’. 3. If [ S’-> S.] is in Ii, then set action [I, $] to accept. If any conflicting actions are generated by the given rules, then we say the grammar is not in SLR or LR (0) and we fail to produce a parser. We then fill the goto entries. The goto transitions for the state i are constructed for all the non-terminals A using the Rule: If goto( Ii, A ) = Ij, then goto [i, A ] = j.

GOTO GRAPH FOR GRAMMAR G