Follow Examples in Compiler
In this class, We discuss Follow Examples in Compiler.
For Complete YouTube Video: Click Here
The reader should have prior knowledge of ‘follow’ conditions. Click Here.
To find the ‘Follow’ symbols, We need to find the First symbols.
How to find the First Symbols? Click Here.
Example 1:
S – ABCDE
A – a | ε
B – b | ε
C – c
D – d | ε
E – e | ε
First Symbols for the grammar
First(S) = {a,b,c}
First(A) = {a, ε}
First(B) = {b, ε}
First(C) = {c}
First(D) = {d,ε}
First(E) = { e, ε}
Follow Symbols for the grammar
Follow(S) = {$}
For the first non-terminal, we add dollar as follow symbol.
Are there any other ‘follow’ symbols for S? No
If we find the symbol S on the right side of the grammar productions, we find ‘follow’ symbols.
In our example grammar, no production has the symbol S on the right side.
Follow(A) = First(B)
First(B) = {b, ε}
Place epsilon in place of B. We get First(C) as Follow(A).
Finally, Follow(A) = {First(B) – {ε}} U {First(C)} From our fourth condition.
Follow(A) = {b,c}
Follow(B) = First(C) = {c}
Follow(C) = {First(D) U First(E) U Follow(S) } – {ε}
First(D) had epsilon so we take First(E)
First(E) had epsilon so we take Follow(S).
Finally, remove epsilon from the ‘follow’ symbols.
Follow(C) = {d, e,$, ε} – {v} = {d, e, $}
Follow(D) = {e, $}
Follow(E) = {$}
Example 2:
Expression Grammar First and Follow.
E – TE’
E’ – +TE’ | ε
T – FT’
T’ – *FT’ | ε
F – id | (E)
First symbols for Expression Grammar
First(E) = First(T) = First(F) = {id, (}
First(E’) = {+, ε}v
First(T’) = {*, ε}
Follow symbols for expression grammar.
Follow(E) = {$, )}
Follow(E’) = {$, )}
Follow(T) = {+, $, )}
Follow(T’) = {+, $, )}
Follow(F) = {*, +, $, )}