0% found this document useful (0 votes)
63 views

SLR Table

This document describes how to construct an SLR parse table for a given context-free grammar. It involves two main parts: 1) creating the LR(0) states by applying start, read, and complete operations to items until no new states are generated, and 2) filling in the parse table using the states and grammar rules/symbols according to four construction rules, including shifting for dots in the middle of items and reducing for dots at the end. An example grammar and its resulting 5 states and 5x5 parse table are provided.

Uploaded by

Hector Villa
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
63 views

SLR Table

This document describes how to construct an SLR parse table for a given context-free grammar. It involves two main parts: 1) creating the LR(0) states by applying start, read, and complete operations to items until no new states are generated, and 2) filling in the parse table using the states and grammar rules/symbols according to four construction rules, including shifting for dots in the middle of items and reducing for dots at the end. An example grammar and its resulting 5 states and 5x5 parse table are provided.

Uploaded by

Hector Villa
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

ConstructinganSLRparsetable

ThisdocumentwascreatedbySamJ.FrazierbasedonclasslecturesbyProfessorCarolZander.
ThedocumentwaseditedbyCarolZander.
S

simple
L
lefttorightscanofinput
R
rightmostderivationinreverse

Part1:CreatethesetofLR(0)statesfortheparsetable
Fortherulesinanaugmentedgrammar,G,beginatrulezeroandfollowthestepsbelow:
Statecreationsteps(bigpicturealgorithm)
1. Applythestartoperationand
2. completethestate:
a. UseonereadoperationoneachitemC(nonterminalorterminal)inthecurrentstate
tocreatemorestates.
b. Applythecompleteoperationonthenewstates.
c. Repeatstepsaandbuntilnomorenewstatescanbeformed.

Operationsdefined
A,S,X:nonterminals
w,x,y,z:stringofterminalsand/ornonterminals
C:oneterminaloronenonterminal

start:ifSisasymbolwith[S>w]asaproductionrule,then[S>.w}istheitemassociatedwiththe
startstate.
read:if[A>x.Cz]isaniteminsomestate,then[A>xC.z]isassociatedwithsomeotherstate.
Whenperformingaread,alltheitemswiththedotbeforethesameCareassociatedwiththesame
state.(Notethatthedotisbeforeanything,eitherterminalornonterminal.)
complete:if[A>x.Xy]isanitem,theneveryruleofthegrammarwiththeform[X>.z]mustbe
includedwithinthisstate.Repeataddingitemsuntilnonewitemscanbeadded.(Notethatthedotis
beforeanonterminal.)

Page1of4

ExampleforPart1
ConsidertheaugmentedgrammarG:
0. S>S$
1. S>aSbS
2. S>a
ThesetofLR(0)itemsets,thestates:
State

Item

Notes

I0

[S -> .S$]

startoperation;readonSgoestoI1(state1)

[S -> .aSbS]

completeoperationonSrule;readonagoestoI2(state2)

[S -> .a]

continuecompleteforallrulesS;dittothereadona,tostate2

I1

[S -> S.$]

readonSfromfirstline;Note:neverreadon$
nothingtoreadon;nothingtocomplete

I2

[S -> a.SbS]

fromreadonafromstateI0;readonSgoestoI3(state3)

[S -> a.]

continuefromreadonafromstateI0(seestep2ofstatecreation)
nothingtoreadon;nothingtocomplete

[S -> .aSbS]

completethestatebecauseof.Sinthefirstitem
readonacyclesbacktostate2

[S -> .a]

continuecompleteofallgrammarrulesforS
dittoreadonacyclesbacktostate2

I3

[S -> aS.bS]

fromreadonSfromstateI2
thedotisbeforeanonterminal,nocompleteoperation
readonbgoestoI4(state4)

I4

[S -> aSb.S]

fromreadonbfromstateI3;readonSgoestoI5(state5)

[S -> .aSbS]

completethestatebecauseof.Sinthefirstitem;
note:dotalwaysinfrontforcompletes
readonacyclesbacktostate2

[S -> .a]

continuecomplete;dittoreadonacyclesbacktostate2

[S -> aSbS.]

fromreadonSfromstate5;nothingtoreadon

I5

Page2of4

Part2:Constructtheparsetable
Tocreatetheparsetable,youneedtheFIRSTandFOLLOWsetsforeachnonterminalinyourgrammar.
Also,youneedthecompletedsetofstateitemsfrompart1.
Now,drawannxmtableforyourparsetable,andlabelitappropriately.Thenisequaltothenumber
ofstatesyouhavefrompart1.Youdeterminethembycountingallnonterminalsandallterminalsin
thegrammar.Providearoworcolumnforeachnandmitem.
Labeleachrownwithastatenumber,startingatzero.Labeleachcolumnmwithoneterminalper
column,startingfromlefttoright.Afterlabelingwithalltheterminals,labeltheremainingcolumns
withthenonterminals.
Thegroupofcolumnsontheleft(terminals)istheACTIONside.Thegroupofcolumnsontheright
(nonterminals)istheGOTOside.
Tofillinthetable,youfollowthesefourrules.
Constructionrules
,=anystringofterminalsand/ornonterminals
X,S,S=nonterminals

(Whendotisinmiddle)

1. if[A>.a]IiandreadonaproducesIj thenACTION[i,a]=SHIFTj.
2. if[A>.X]Ii andreadonXproducesIj thenGOTO[i,X]=j.
(Whendotisatend)

3. if[A>.]IithenACTION[i,a]=REDUCEonA>forallaFOLLOW(A).
4. if[S>S.]Ii thenACTION[i,$]=ACCEPT.

Fortheexample,theparsetable:

0
1
2
3
4
5

a
s2

s2

s2

r2
s4

r1

accept
r2

r1

S
1

Page3of4

UsingtheparsetableconstructionrulesfortheaugmentedgrammarG:
0. S>S$
1. S>aSbS
2. S>a
TheFIRSTandFOLLOWstatementsare:
FIRST(S)={a}
FOLLOW(S)={b,$}

State

Item

Notes
(RememberthataSHIFTreferstostate,REDUCEreferstogrammarrule)

I0

[S -> .S$]

readonSgoestostate1;dotinmiddle#2,GOTO[0,S]=1

[S -> .aSbS]
[S -> .a]

readonaforbothofthesegoestostate2;
dotinmiddle#1,ACTION[0,a]=SHIFT2

I1

[S -> S.$]

dotatend#4(onlyoneofthese),ACTION[1,$]=REDUCE2

I2

[S -> a.SbS]

readonSgoestostate3;dotinmiddle#2,GOTO[2,S]=3

[S -> a.]

dotatend#3,ACTION[2,b]=ACTION[2,$]=REDUCE2

[S -> .aSbS]
[S -> .a]

readonaforbothofthesecyclesbacktostate2;
dotinmiddle#1,ACTION[2,a]=SHIFT2

I3

[S -> aS.bS]

readonbgoestostate4;dotinmiddle#1,ACTION[3,b]=SHIFT4

I4

[S -> aSb.S]

readonSgoestostate5;dotinmiddle#2,GOTO[4,S]=5

[S -> .aSbS]
[S -> .a]

readonacyclesforbothofthesecyclesbacktostate2;
dotinmiddle#1,ACTION[4,a]=SHIFT2

[S -> aSbS.]

dotatend#3,ACTION[5,b]=ACTION[5,$]=REDUCE1

I5

0
1
2
3
4
5

a
s2

s2

s2

r2
s4

r1

accept
r2

r1

S
1

Page4of4

You might also like