slr parsing techniques submitted by: abhijeet mohapatra 04cs1019

16
SLR PARSING SLR PARSING TECHNIQUES TECHNIQUES Submitted By: Abhijeet Mohapatra 04CS1019.

Upload: suzanna-cameron

Post on 12-Jan-2016

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: SLR PARSING TECHNIQUES Submitted By: Abhijeet Mohapatra 04CS1019

SLR PARSING SLR PARSING TECHNIQUESTECHNIQUES

Submitted By:

Abhijeet Mohapatra

04CS1019.

Page 2: SLR PARSING TECHNIQUES Submitted By: Abhijeet Mohapatra 04CS1019

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

Page 3: SLR PARSING TECHNIQUES Submitted By: Abhijeet Mohapatra 04CS1019

CONSTRUCTION OF SLR – CONSTRUCTION OF SLR – PARSING TABLESPARSING TABLES

INPUT:INPUT: Grammar G with production rules. Grammar G with production rules.

CENTRAL IDEACENTRAL IDEATo construct from the input grammar, a DFA To construct from the input grammar, a DFA to recognize viable prefixes. This is done by to recognize viable prefixes. This is done by grouping grouping items items into sets which can be into sets which can be visualized as subset construction from the visualized as subset construction from the states of a NFA recognizing the viable states of a NFA recognizing the viable prefixes.prefixes.

Page 4: SLR PARSING TECHNIQUES Submitted By: Abhijeet Mohapatra 04CS1019

Item: Item: An An LR (0) item LR (0) item or simply, an item of a or simply, an item of a grammar G is a production of G with a dot ‘.’ at grammar G is a production of G with a dot ‘.’ at some position of the right side. For example, the some position of the right side. For example, the production A production A XYZ yields four items, XYZ yields four items, A A .XYZ .XYZ A A X.YZ X.YZ A A XY.Z XY.Z A A XYZ. XYZ.

A production rule of the form A A production rule of the form A yields only yields only one item A one item A . . Intuitively, an item shows how . . Intuitively, an item shows how much of a production we have seen till the current much of a production we have seen till the current point in the parsing procedure.point in the parsing procedure.

Page 5: SLR PARSING TECHNIQUES Submitted By: Abhijeet Mohapatra 04CS1019

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

Page 6: SLR PARSING TECHNIQUES Submitted By: Abhijeet Mohapatra 04CS1019

Closure Operation:Closure Operation: If I[] is a set of items for If I[] is a set of items for a grammar G, a grammar G,

closure(I) = I closure(I) = I {B {B . . | (A | (A .B.B closure(I)) closure(I)) ((B ((B ) ) grammar G} grammar G}

Page 7: SLR PARSING TECHNIQUES Submitted By: Abhijeet Mohapatra 04CS1019

Goto Operation: Goto Operation: For a set of items I, and For a set of items I, and grammar symbol X,grammar symbol X,

goto(I, X) = { Closure (all items goto(I, X) = { Closure (all items containing A containing A X.X.) such that A ) such that A .X.X is in I} is in I}

= set of items that are = set of items that are valid for the viable prefix valid for the viable prefix X, where I is X, where I is valid for some viable prefix valid for some viable prefix . .

Page 8: SLR PARSING TECHNIQUES Submitted By: Abhijeet Mohapatra 04CS1019

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

Non-kernel items are the items which have the Non-kernel items are the items which have the dot at leftmost end. dot at leftmost end.

Page 9: SLR PARSING TECHNIQUES Submitted By: Abhijeet Mohapatra 04CS1019

void items (grammar G){void items (grammar G){ set C;set C; int i, j, n;int i, j, n; C = {closure ({[S’ C = {closure ({[S’ .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++)for (j = 0; G.symbol[j] != 0; j++) if (size(goto(C[i],X)) > 0 && !if (size(goto(C[i],X)) > 0 && !iselement(goto(C[i],X),C) )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.} // size of items in grammar G.

Page 10: SLR PARSING TECHNIQUES Submitted By: Abhijeet Mohapatra 04CS1019

Example:Example:

Grammar G:Grammar G:

S’ S’ S S

S S aABe aABe

A A Abc Abc

A A b b

B B d d

Page 11: SLR PARSING TECHNIQUES Submitted By: Abhijeet Mohapatra 04CS1019

Set of items generated:-Set of items generated:-

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

Page 12: SLR PARSING TECHNIQUES Submitted By: Abhijeet Mohapatra 04CS1019

The table for parsing action and goto function The table for parsing action and goto function for the given grammar is:-for the given grammar is:-

Where 1. rj means reduce by production numbered j,2. acc means accept3. si means shift and stack state i.4. blank means error.

Page 13: SLR PARSING TECHNIQUES Submitted By: Abhijeet Mohapatra 04CS1019
Page 14: SLR PARSING TECHNIQUES Submitted By: Abhijeet Mohapatra 04CS1019

GOTO GRAPHGOTO GRAPH

Transition Rules: Transition Rules:

If there is a transition from,If there is a transition from,

A->α . Xβ A->α . Xβ to to A-> αX . βA-> αX . β

then, the transition in the GOTO Graph is labeled then, the transition in the GOTO Graph is labeled X X

If there is a transition If there is a transition

A-> α . BβA-> α . Bβ to to B-> .γ B-> .γ

then, the transition is labeledthen, the transition is labeled ε, ε, and since we take and since we take εε--closure of all items, these productions lie in the same closure of all items, these productions lie in the same item as that of item as that of A->α . Bβ A->α . Bβ to to A-> αB . βA-> αB . β. .

Page 15: SLR PARSING TECHNIQUES Submitted By: Abhijeet Mohapatra 04CS1019

RULES FOR FILLING THE SLR RULES FOR FILLING THE SLR PARSE TABLEPARSE TABLE

The Rules for filling the The Rules for filling the action action entries in the table are:entries in the table are:1.If 1.If [ A-> α .a β ][ A-> α .a β ] is in is in Ii Ii and and gotogoto ( Ii , a ) = ( Ii , a ) = Ij Ij, then set , then set action action

[i ,a] to “shift j “.Here [i ,a] to “shift j “.Here aa must be a terminal. must be a terminal.2. If 2. If [A-> α .] [A-> α .] is in is in IiIi, then set , then set actionaction[i , a ] to “reduce A -> α “ [i , a ] to “reduce A -> α “

for all for all aa in FOLLOW (A); here A may not be in S’. in FOLLOW (A); here A may not be in S’.3. If 3. If [ S’-> S.][ S’-> S.] is in is in Ii Ii , then set , then set actionaction [I, $] to accept. [I, $] to accept.If any conflicting actions are generated by the given rules, then 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 we say the grammar is not in SLR or LR (0) and we fail to produce a parser. produce a parser.

We then fill the goto entries.We then fill the goto entries.The goto transitions for the state i are constructed for all the The goto transitions for the state i are constructed for all the

non-terminals A using the Rule:non-terminals A using the Rule:If If goto( Ii, A )goto( Ii, A ) = Ij = Ij , then , then goto [i , A ] = jgoto [i , A ] = j . .

Page 16: SLR PARSING TECHNIQUES Submitted By: Abhijeet Mohapatra 04CS1019

GOTO GRAPH FOR GRAMMAR GGOTO GRAPH FOR GRAMMAR G