0% found this document useful (0 votes)
58 views3 pages

First and Follow Sets

The document discusses rules for constructing First and Follow sets for a context-free grammar. It provides 4 rules for constructing First sets by examining terminal symbols and production rules. It also provides 4 rules for constructing Follow sets by examining start symbols, symbols before and after other symbols in production rules, and contents of First sets. The document includes an example grammar and shows the step-by-step construction of the First and Follow sets for that grammar's symbols according to the provided rules.

Uploaded by

palukapoop
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views3 pages

First and Follow Sets

The document discusses rules for constructing First and Follow sets for a context-free grammar. It provides 4 rules for constructing First sets by examining terminal symbols and production rules. It also provides 4 rules for constructing Follow sets by examining start symbols, symbols before and after other symbols in production rules, and contents of First sets. The document includes an example grammar and shows the step-by-step construction of the First and Follow sets for that grammar's symbols according to the provided rules.

Uploaded by

palukapoop
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

First And Follow Sets

I have found First and Follow Sets very difficult to follow, so I have tried to rewrute the rules for createing
them so that they are easier to understand.
Rules for First Sets
1. If X is a terminal then First(X) is just X!
2. If there is a Production X -> ∈ then add∈ to first(X)
3. If there is a Production X -> Y1Y2..Yk then add first(Y1Y2..Yk) to first(X)
4. First(Y1Y2..Yk) is either
1. First(Y1) (if First(Y1) doesn't contain ∈)
2. OR (if First(Y1) does contain ∈) then First (Y1Y2..Yk) is everything in First(Y1) <except
for ∈ > as well as everything in First(Y2..Yk)
3. If First(Y1) First(Y2)..First(Yk) all contain ∈ then add ∈ to First(Y1Y2..Yk) as well.

Rules for Follow Sets


1. First put $ (the end of input marker) in Follow(S) (S is the start symbol)
2. If there is a production A → α Bβ , (where α can be a whole string) then everything in FIRST(β )
except for ∈ is placed in FOLLOW(B).
3. If there is a production A → α B, then everything in FOLLOW(A) is in FOLLOW(B)
4. If there is production A → α Bβ , where FIRST(β ) contains ∈, then everything in FOLLOW(A) is
in FOLLOW(B)
Here are some Examples for you to follow along with.
The Grammar
E → TE'
E' → +TE'
E' → ∈
T → FT'
T' → *FT'
T' → ∈
F → (E)
F → id
First Sets Follow Sets
We Want to make First sets so first we list the sets we We Want to make Follow sets so first we list the sets we need
need FOLLOW(E) = {}
FIRST(E) = {} FOLLOW(E') = {}
FIRST(E') = {} FOLLOW(T) ={}
FIRST(T) = {} FOLLOW(T') = {}
FIRST(T') = {} FOLLOW(F) = {}
FIRST(F) = {} The First thing we do is Add $ to the start Symbol 'E'
First We apply rule 2 to T' → ∈ and E' → ∈ FOLLOW(E) = {$}
FIRST(E) = {} FOLLOW(E') = {}
FIRST(E') = {∈} FOLLOW(T) ={}
FIRST(T) = {} FOLLOW(T') = {}
FIRST(T') = {∈} FOLLOW(F) = {}
FIRST(F) = {} Next we apply rule 2 to E' → +TE' This says that everything in
First We apply rule 3 to T' → *FT' this rule tells us that First(E') except for∈ should be in Follow(T)
we can add everything in First(*FT') into First(T') FOLLOW(E) = {$}
Since First(*) useing rule 1 is * we can add * to First(T') FOLLOW(E') = {}
FIRST(E) = {} FOLLOW(T) ={+}
FIRST(E') = {+,∈} FOLLOW(T') = {}
FIRST(T) = {} FOLLOW(F) = {}
FIRST(T') = {*,∈} Next we apply rule 3 to E → TE' This says that we should add
FIRST(F) = {} everything in Follow(E) into Follow(E')
First We apply rule 3 to T' → *FT' this rule tells us that FOLLOW(E) = {$}
we can add everything in First(*FT') into First(T') FOLLOW(E') = {$}
Since First(*) useing rule 1 is * we can add * to First(T') FOLLOW(T) ={+}
FIRST(E) = {} FOLLOW(T') = {}
FIRST(E') = {+,∈} FOLLOW(F) = {}
FIRST(T) = {} Next we apply rule 3 to T → FT' This says that we should add
FIRST(T') = {*,∈} everything in Follow(T) into Follow(T')
FIRST(F) = {} FOLLOW(E) = {$}
Two more productions begin with terminals F → (E) FOLLOW(E') = {$}
and F → id If we apply rule 3 to these we get... FOLLOW(T) ={+}
FIRST(E) = {} FOLLOW(T') = {+}
FIRST(E') = {+,∈} FOLLOW(F) = {}
FIRST(T) = {} Now we apply rule 2 to T' → *FT' This says that everything in
FIRST(T') = {*,∈} First(T') except for∈ should be in Follow(F)
FIRST(F) = {'(',id} FOLLOW(E) = {$}
Next we apply rule 3 to T → FT' once again this tells us FOLLOW(E') = {$}
that we can add First(FT') to First(T) FOLLOW(T) ={+}
Since First(F) doesn't contain ∈ that means that FOLLOW(T') = {+}
First(FT') is just First(F) FOLLOW(F) = {*}
FIRST(E) = {} Now we apply rule 2 to F → (E) This says that everything in
FIRST(E') = {+,∈} First(')') should be in Follow(E)
FIRST(T) = {'(',id} FOLLOW(E) = {$,)}
FIRST(T') = {*,∈} FOLLOW(E') = {$}
FIRST(F) = {'(',id} FOLLOW(T) ={+}
Lastly we apply rule 3 to E → TE' once again this tells FOLLOW(T') = {+}
us that we can add First(TE') to First(E) FOLLOW(F) = {*}
Since First(T) doesn't contain ∈ that means that Next we apply rule 3 to E → TE' This says that we should add
First(TE') is just First(T) everything in Follow(E) into Follow(E')
FIRST(E) = {'(',id} FOLLOW(E) = {$,)}
FIRST(E') = {+,∈} FOLLOW(E') = {$,)}
FIRST(T) = {'(',id} FOLLOW(T) ={+}
FIRST(T') = {*,∈} FOLLOW(T') = {+}
FIRST(F) = {'(',id} FOLLOW(F) = {*}
Doing anything else doesn't change the sets so we are Next we apply rule 4 to E' → +TE' This says that we should add
done! everything in Follow(E') into Follow(T) (because First(E')
contains∈)
FOLLOW(E) = {$,)}
FOLLOW(E') = {$,)}
FOLLOW(T) ={+,$,)}
FOLLOW(T') = {+}
FOLLOW(F) = {*}
Next we apply rule 3 to T → FT' This says that we should add
everything in Follow(T) into Follow(T')
FOLLOW(E) = {$,)}
FOLLOW(E') = {$,)}
FOLLOW(T) ={+,$,)}
FOLLOW(T') = {+,$,)}
FOLLOW(F) = {*}
Finaly we apply rule 4 to T' → *FT' This says that we should add
everything in Follow(T') into Follow(F)
FOLLOW(E) = {$,)}
FOLLOW(E') = {$,)}
FOLLOW(T) ={+,$,)}
FOLLOW(T') = {+,$,)}
FOLLOW(F) = {*,+,$,)}

You might also like