0% found this document useful (0 votes)
35 views6 pages

Cs1622-First and Follow

This document defines the First and Follow sets for a context-free grammar. It begins by showing the grammar rules and then calculates the First sets using three rules: (1) a terminal symbol's First set is just that symbol, (2) if a nonterminal can derive the empty string ε then ε is added to its First set, and (3) the First set of a nonterminal is the union of the First sets of its immediate successors. It then calculates the Follow sets using three similar rules: (1) the Follow set of the start symbol contains $, (2) if a nonterminal B immediately follows A then First(B)-ε is added to Follow(A), and (3) if A

Uploaded by

AhashanAlamSojib
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views6 pages

Cs1622-First and Follow

This document defines the First and Follow sets for a context-free grammar. It begins by showing the grammar rules and then calculates the First sets using three rules: (1) a terminal symbol's First set is just that symbol, (2) if a nonterminal can derive the empty string ε then ε is added to its First set, and (3) the First set of a nonterminal is the union of the First sets of its immediate successors. It then calculates the Follow sets using three similar rules: (1) the Follow set of the start symbol contains $, (2) if a nonterminal B immediately follows A then First(B)-ε is added to Follow(A), and (3) if A

Uploaded by

AhashanAlamSojib
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

First

and Follow Sets


Grammar
ETX
X+E
X
TintY
T(E)
Y*T
Y

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

),$,+
),$
),$,+
),$

You might also like