Вы находитесь на странице: 1из 1

FIRSTandFOLLOW(Neededforbuildingaparsetable)

ThesenoteswerecollectedbySamJ.Frazierbasedonthetwosourceslistedbelow.Thenoteswere
editedbyCarolZander.
a. Aho,A.etal.Compilers:Principles,Techniques,&Tools.2nded.Pearson:Boston,2007.
b. Zander,Carol.ClassLecture.UniversityofWashington,Bothell.Oct.26&28,2009.

InformalDefinition
ForthenonterminalX,FIRST(X)isthesetofterminalsinagrammarthatbeginanystringthatXcan
generate.Inotherwords,ifyouuseXtogenerateastring,whatterminalsstartthestring.
ForthenonterminalX,FOLLOW(X)isthesetofterminalsthatmayappearafterXinthegrammar.
Inotherwords,whengeneratingastring,whatterminalscouldfollowX.

FormalDefinition
,

FIRST()

FOLLOW(A)

and

(Aisinthemiddleofarule)

(Aisattheendofarule)

WhenAisinthemiddleofarule,itisfairlyclearthatwhatfollowsAiswhatisthefirstthingof .
Forthesecondpart,whenAisattheendofarule,hereisanexample:
B > ... A
X > ... BC
Y > ... Bd

UsingthefirstruleandplugginginforB,putstheAjustbeforetheC.
Ditto,putstheAjustbeforethed.BoththeCanddfollowB.

Example
Considerthisgrammar,G:
0. S>S$
1. S>aSbS
2. S>a
OurFIRSTandFOLLOWstatementsare:

FIRST(S)={a}
FOLLOW(S)={b,$}

Вам также может понравиться