Cs1622-First and Follow
Cs1622-First and Follow
First Set
First(a)=setofterminalsthatstartstringofterminalsderivedfroma.
Applyfollowingrulesuntilnoterminalorcanbeadded
1. IftT,thenFirst(t)={t}.
2. IfXNandXexists(nullable),thenaddtoFirst(X).
3. IfXNandXY1Y2Y3Ym,whereY1,Y2,Y3,...Ymarenonterminals,then:
foreachifrom1tom
ifY1Yi1areallnullable(orifi=1)
First(X)=First(X)First(Yi)
By rule 1:
TheFirstofaterminalisthatterminal.
Symbol
(
)
+
*
int
Y
X
T
E
First
(
)
+
*
int
By rule 2:
IfXNandXexists(nullable),thenaddtoFirst(X).
X
Y
Symbol
(
)
+
*
int
Y
X
T
E
First
(
)
+
*
int
By rule 3:
IfXNandXY1Y2Y3Ym,whereY1,Y2,Y3,...Ymarenonterminals,then:
foreachifrom1tom
ifY1Yi1areallnullable(orifi=1)
First(X)=First(X)First(Yi)
ETX
First(E)=First(E)First(T)
WeneedFirst(T)first.
First(T)={int,(}
TintY
T(E)
becauseFirst(int)=int
becauseFirst(()=(
NowwecangobackanddoFirst(E)
First(E)={int,(}
WedontconsiderthefirstofXbecauseTisnotnullable.
First(X)={,+}
X+E
X
First(+)=+
handledatstep2
First(Y)={,*}
Y*T
First(*)=*
Y
handledatstep2
Putitalltogether:
Symbol
(
)
+
*
int
Y
X
T
E
First
(
)
+
*
int
,*
,+
int,(
int,(
Done.
Follow Set
Applyfollowingrulesuntilnoterminalorecanbeadded
1. $Follow(S),whereSisthestartsymbol.
2. Lookattheoccurrenceofanonterminalontherighthandsideofaproductionwhichis
followedbysomething
IfAaBb,thenFirst(b){e}Follow(B)
3. LookatNontheRHSthatisnotfollowedbyanything,
if(AaB)or(AaBbandFirst(b)),
thenFollow(A)Follow(B)
By step 1:
$Follow(S),whereSisthestartsymbol.
a.)Follow(E)={$}
Eisourstartsymbol
Thetablesofar:
Symbol
(
)
+
*
int
Y
X
T
E
First
(
)
+
*
int
,*
,+
int,(
int,(
Follow
N/A
By step 2:
Lookattheoccurrenceofanonterminalontherighthandsideofaproductionwhichisfollowedby
something:
IfAaBb,thenFirst(b){}Follow(B)
Thissubsetnotationcanbeconfusing.ItssayingherethattheFollow(B)containsatleasttheFirst(b)
(withexcluded)asasubset.TheremightbemorethingsinFollow(B)becauseotherstepsaddedthem
inorothergrammarruleswereanalyzed.
ETX
a.)Follow(T)contains(atleast)theFirst(X)={,+}{}={+}
T(E)
b.)Follow(E)contains(atleast)theFirst())={)}
SonowFollow(E)is{),$}(Fromstep1a)
Thetablesofar:
Symbol
First
Follow
(
(
)
)
+
+
N/A
*
*
int
int
Y
,*
X
,+
T
int,(
+
E
int,(
),$
By step 3:
LookatNontheRHSthatisnotfollowedbyanything,
if(AaB)or(AaBbandFirst(b)),
thenFollow(A)Follow(B)
Again,becarefulwiththenotation.ItssayingherethattheFollow(B)containsatleasttheFollow(A)
asasubset.TheremightbemorethingsinFollow(B)becauseotherstepsaddedtheminorother
grammarruleswereanalyzed.
ETX
a.)Follow(X)contains(atleast)Follow(E)={),$}
(fromstep2b)
First(X)so:
b.)Follow(T)contains(atleast)Follow(E)={),$,+} (fromstep2aand2b)
X+E
c.)Follow(E)contains(atleast)Follow(X)={),$}
(fromstep3a)
TintY
d.)Follow(Y)contains(atleast)Follow(T)={),$,+} (fromstep3b)
Y*T
e.)Follow(T)contains(atleast)Follow(Y)={),$}
(fromstep3d)
Wedothiswholeprocessagainuntilnomoreadditionshappen:
ETX
f.)Follow(X)contains(atleast)Follow(E)={),$}
(nochange)
First(X)so:
g.)Follow(T)contains(atleast)Follow(E)={),$,+} (nochange)
X+E
h.)Follow(E)contains(atleast)Follow(X)={),$}
(nochange)
TintY
i.)Follow(Y)contains(atleast)Follow(T)={),$,+}
(nochange)
Y*T
j.)Follow(T)contains(atleast)Follow(Y)={),$}
(nochange)
Done.
Results
Symbol
(
)
+
*
int
Y
X
T
E
First
(
)
+
*
int
,*
,+
int,(
int,(
Follow
N/A
),$,+
),$
),$,+
),$