Sunteți pe pagina 1din 4

FirstAndFollowSets

IhavefoundFirstandFollowSetsverydifficulttofollow,soIhavetriedtorewrutetherulesfor
createingthemsothattheyareeasiertounderstand.

RulesforFirstSets
1.IfXisaterminalthenFirst(X)isjustX!
2.IfthereisaProductionX>thenaddtofirst(X)
3.IfthereisaProductionX>Y1Y2..Ykthenaddfirst(Y1Y2..Yk)tofirst(X)
4.First(Y1Y2..Yk)iseither
1.First(Y1)(ifFirst(Y1)doesn'tcontain)
2.OR(ifFirst(Y1)doescontainthenFirst(Y1Y2..Yk)iseverythinginFirst(Y1)
<exceptfor>aswellaseverythinginFirst(Y2..Yk)
3.IfFirst(Y1)First(Y2)..First(Yk)allcontainthenaddtoFirst(Y1Y2..Yk)aswell.

RulesforFollowSets
1.Firstput$(theendofinputmarker)inFollow(S)(Sisthestartsymbol)
2.IfthereisaproductionAB,(where canbeawholestring)theneverythingin
FIRST()exceptforisplacedinFOLLOW(B).
3.IfthereisaproductionAB,theneverythinginFOLLOW(A)isinFOLLOW(B)
4.IfthereisproductionABwhereFIRST()containstheneverythinginFOLLOW(A)
isinFOLLOW(B)

HerearesomeExamplesforyoutofollowalongwith.
TheGrammar
ETE'
E'+TE'
E'
TFT'
T'*FT'
T'
F(E)
Fid
FirstSets
FollowSets
WeWanttomakeFirstsetssofirstwelist WeWanttomakeFollowsetssofirstwelistthesetswe
thesetsweneed
need

FIRST(E)={}

FOLLOW(E)={}

FIRST(E')={}

FOLLOW(E')={}

FIRST(T)={}

FOLLOW(T)={}

FIRST(T')={}

FOLLOW(T')={}

FIRST(F)={}

FOLLOW(F)={}

FirstWeapplyrule2toT'andE' TheFirstthingwedoisAdd$tothestartSymbol'E'
FIRST(E)={}

FOLLOW(E)={$}

FIRST(E')={}

FOLLOW(E')={}

FIRST(T)={}

FOLLOW(T)={}

FIRST(T')={}

FOLLOW(T')={}

FIRST(F)={}

FOLLOW(F)={}

FirstWeapplyrule3toT'*FT'thisrule Next we apply rule 2 to E' +TE' This says that


tells us that we can add everything in everything in First(E') except for should be in
Follow(T)
First(*FT')intoFirst(T')
Since First(*) useing rule 1 is * we can FOLLOW(E)={$}
add*toFirst(T')
FOLLOW(E')={}
FIRST(E)={}
FOLLOW(T)={+}
FIRST(E')={+,}
FOLLOW(T')={}
FIRST(T)={}
FIRST(T')={*,}
FIRST(F)={}

FOLLOW(F)={}
Next we apply rule 3 to E TE' This says that we
shouldaddeverythinginFollow(E)intoFollow(E')

FOLLOW(E)={$}
FirstWeapplyrule3toT'*FT'thisrule
tells us that we can add everything in
FOLLOW(E')={$}
First(*FT')intoFirst(T')
FOLLOW(T)={+}
Since First(*) useing rule 1 is * we can
add*toFirst(T')
FOLLOW(T')={}
FIRST(E)={}

FOLLOW(F)={}

FIRST(E')={+,}

Next we apply rule 3 to T FT' This says that we


shouldaddeverythinginFollow(T)intoFollow(T')

FIRST(T)={}
FIRST(T')={*,}
FIRST(F)={}

FOLLOW(E)={$}
FOLLOW(E')={$}

Two more productions begin with FOLLOW(T)={+}


terminalsF(E)andFidIfweapply
FOLLOW(T')={+}
rule3totheseweget...
FIRST(E)={}

FOLLOW(F)={}

FIRST(E')={+,}
FIRST(T)={}

Now we apply rule 2 to T' *FT' This says that


everything in First(T') except for should be in
Follow(F)

FIRST(T')={*,}

FOLLOW(E)={$}

FIRST(F)={'(',id}

FOLLOW(E')={$}

Next we apply rule 3 to T FT' once FOLLOW(T)={+}


againthistellsusthatwecanaddFirst(FT')
FOLLOW(T')={+}
toFirst(T)
FOLLOW(F)={*}
SinceFirst(F)doesn'tcontain that means
thatFirst(FT')isjustFirst(F)
Now we apply rule 2 to F (E) This says that
everythinginFirst(')')shouldbeinFollow(E)
FIRST(E)={}
FOLLOW(E)={$,)}
FIRST(E')={+,}
FOLLOW(E')={$}
FIRST(T)={'(',id}
FOLLOW(T)={+}
FIRST(T')={*,}
FOLLOW(T')={+}
FIRST(F)={'(',id}
FOLLOW(F)={*}
Lastly we apply rule 3 to E TE' once
againthistellsusthatwecanaddFirst(TE') Next we apply rule 3 to E TE' This says that we
toFirst(E)
shouldaddeverythinginFollow(E)intoFollow(E')
SinceFirst(T)doesn'tcontain thatmeans FOLLOW(E)={$,)}
thatFirst(TE')isjustFirst(T)
FOLLOW(E')={$,)}
FIRST(E)={'(',id}
FOLLOW(T)={+}
FIRST(E')={+,}
FIRST(T)={'(',id}
FIRST(T')={*,}
FIRST(F)={'(',id}

FOLLOW(T')={+}
FOLLOW(F)={*}
Next we apply rule 4 to E' +TE' This says that we
should add everything in Follow(E') into Follow(T)
(becauseFirst(E')contains

Doinganythingelsedoesn'tchangethesets
FOLLOW(E)={$,)}
sowearedone!
FOLLOW(E')={$,)}
FOLLOW(T)={+,$,)}

FOLLOW(T')={+}
FOLLOW(F)={*}
Next we apply rule 3 to T FT' This says that we
shouldaddeverythinginFollow(T)intoFollow(T')
FOLLOW(E)={$,)}
FOLLOW(E')={$,)}
FOLLOW(T)={+,$,)}
FOLLOW(T')={+,$,)}
FOLLOW(F)={*}
Finalyweapplyrule4toT'*FT'Thissaysthatwe
shouldaddeverythinginFollow(T')intoFollow(F)
FOLLOW(E)={$,)}
FOLLOW(E')={$,)}
FOLLOW(T)={+,$,)}
FOLLOW(T')={+,$,)}
FOLLOW(F)={*,+,$,)}
IfIhavemadeanymistakesonthispagepleaseemailmeandcorrectthem.Imaynothavegoneaboutthisin
thefastestway,buttheansweristheonlythingthatcounts.TheanswersshouldbecorrectasIgotthemfrom
thewebsitebellow.
ThisGrammarwasborrowedfromhttp://es.udmercy.edu/~daimikj/html/CSC541/CSC541first.htm

S-ar putea să vă placă și