Documente Academic
Documente Profesional
Documente Cultură
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)={}
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')={+,}
FIRST(T)={}
FIRST(T')={*,}
FIRST(F)={}
FOLLOW(E)={$}
FOLLOW(E')={$}
FOLLOW(F)={}
FIRST(E')={+,}
FIRST(T)={}
FIRST(T')={*,}
FOLLOW(E)={$}
FIRST(F)={'(',id}
FOLLOW(E')={$}
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