First and Follow
The construction of a predictive parser is aided by two functions associated with a grammar G. These functions, FIRST and FOLLOW, allow us to fill in the entries of a predictive parsing table for G, whenever possible. Sets of tokens yielded by the FOLLOW function can also be used as synchronizing tokens during panic-mode error recovery.
1. FIRST(a)
First(a) = set of terminals that start a string of terminals derived from a.
Apply following rules until no terminal or Ξ΅ can be added
1. If t β T, then First( t ) = { t }.
2. If X β N and X β Ξ΅ exists (nullable), then add Ξ΅ to First( X ).
3. If X β N and X β Y1Y2Y3β¦ Ym, where Y1, Y2, Y3, ... Ym are nonβterminals, then: for each, I from 1 to m if Y1 β¦ Yiβ1 are all nullable (or if i = 1) First( X ) = First( X ) βͺ First( Yi )
2. FOLLOW (a)
Apply the following rules until no terminal or e can be added
1. $ β Follow( S ), where S is the start symbol.
2. Look at the occurrence of a nonβterminal on the right-hand side of a production which is followed by something If A β a B b, then First( b ) β {e} β Follow( B )
3. Look at N on the RHS that is not followed by anything, if (A β a B) or (A β a B b and Ξ΅ ο First( b )), then Follow( A ) β Follow( B )
PRACTICE PROBLEMS BASED ON CALCULATING FIRST AND FOLLOW-
Problem-01:
Calculate the first and follow functions for the given grammar-
S β aBDh
B β cC
C β bC / β
D β EF
E β g / β
F β f / β
Solution-
The first and follow functions are as follows-
First Functions-
- First(S) = { a }
- First(B) = { c }
- First(C) = { b , β }
- First(D) = { First(E) β β } βͺ First(F) = { g , f , β }
- First(E) = { g , β }
- First(F) = { f , β }
Follow Functions-
- Follow(S) = { $ }
- Follow(B) = { First(D) β β } βͺ First(h) = { g , f , h }
- Follow(C) = Follow(B) = { g , f , h }
- Follow(D) = First(h) = { h }
- Follow(E) = { First(F) β β } βͺ Follow(D) = { f , h }
- Follow(F) = Follow(D) = { h }
Problem-02:
Calculate the first and follow functions for the given grammar-
S β A
A β aB / Ad
B β b
C β g
Solution-
We have-
- The given grammar is left recursive.
- So, we first remove left recursion from the given grammar.
After eliminating left recursion, we get the following grammar-
S β A
A β aBAβ
Aβ β dAβ / β
B β b
C β g
Now, the first and follow functions are as follows-
First Functions-
- First(S) = First(A) = { a }
- First(A) = { a }
- First(Aβ) = { d , β }
- First(B) = { b }
- First(C) = { g }
Follow Functions-
- Follow(S) = { $ }
- Follow(A) = Follow(S) = { $ }
- Follow(Aβ) = Follow(A) = { $ }
- Follow(B) = { First(Aβ) β β } βͺ Follow(A) = { d , $ }
- Follow(C) = NA
Problem-03:
Calculate the first and follow functions for the given grammar-
S β (L) / a
L β SLβ
Lβ β ,SLβ / β
Solution-
The first and follow functions are as follows-
First Functions-
- First(S) = { ( , a }
- First(L) = First(S) = { ( , a }
- First(Lβ) = { , , β }
Follow Functions-
- Follow(S) = { $ } βͺ { First(Lβ) β β } βͺ Follow(L) βͺ Follow(Lβ) = { $ , , , ) }
- Follow(L) = { ) }
- Follow(Lβ) = Follow(L) = { ) }
Problem-04:
Calculate the first and follow functions for the given grammar-
S β AaAb / BbBa
A β β
B β β
Solution-
The first and follow functions are as follows-
First Functions-
- First(S) = { First(A) β β } βͺ First(a) βͺ { First(B) β β } βͺ First(b) = { a , b }
- First(A) = { β }
- First(B) = { β }
Follow Functions-
- Follow(S) = { $ }
- Follow(A) = First(a) βͺ First(b) = { a , b }
- Follow(B) = First(b) βͺ First(a) = { a , b }
Problem-05:
Calculate the first and follow functions for the given grammar-
E β E + T / T
T β T x F / F
F β (E) / id
Solution-
We have-
- The given grammar is left recursive.
- So, we first remove left recursion from the given grammar.
After eliminating left recursion, we get the following grammar-
E β TEβ
Eβ β + TEβ / β
T β FTβ
Tβ β x FTβ / β
F β (E) / id
Now, the first and follow functions are as follows-
First Functions-
- First(E) = First(T) = First(F) = { ( , id }
- First(Eβ) = { + , β }
- First(T) = First(F) = { ( , id }
- First(Tβ) = { x , β }
- First(F) = { ( , id }
Follow Functions-
- Follow(E) = { $ , ) }
- Follow(Eβ) = Follow(E) = { $ , ) }
- Follow(T) = { First(Eβ) β β } βͺ Follow(E) βͺ Follow(Eβ) = { + , $ , ) }
- Follow(Tβ) = Follow(T) = { + , $ , ) }
- Follow(F) = { First(Tβ) β β } βͺ Follow(T) βͺ Follow(Tβ) = { x , + , $ , ) }
Problem-06:
Calculate the first and follow functions for the given grammar-
S β ACB / CbB / Ba
A β da / BC
B β g / β
C β h / β
Solution-
The first and follow functions are as follows-
First Functions-
- First(S) = { First(A) β β } βͺ { First(C) β β } βͺ First(B) βͺ First(b) βͺ { First(B) β β } βͺ First(a) = { d , g , h , β , b , a }
- First(A) = First(d) βͺ { First(B) β β } βͺ First(C) = { d , g , h , β }
- First(B) = { g , β }
- First(C) = { h , β }
Follow Functions-
Follow(S) = { $ }
- Follow(A) = { First(C) β β } βͺ { First(B) β β } βͺ Follow(S) = { h , g , $ }
- Follow(B) = Follow(S) βͺ First(a) βͺ { First(C) β β } βͺ Follow(A) = { $ , a , h , g }
- Follow(C) = { First(B) β β } βͺ Follow(S) βͺ First(b) βͺ Follow(A) = { g , $ , b , h }
Labels: CD Unit-2, Compiler Design
Discussion & Queries (<$I18NNumComments$>):
<$CommentPager$>
- <$I18NAtCommentTimeWithPermalink$>, <$I18NCommentAuthorSaid$>
<$CommentPager$>
<$BlogCommentBody$>
<$BlogCommentDeleteIcon$>