1
votes

I am doing exam questions for compilers. The question is to find the first and follow sets of the following grammar:

S → uBDz
B → Bv | w
D → EF
E → y | e
F → x | e

This is what I got when I calculated the First set:

First
S  u,v,w,y,x,z,e
B  v,w
D  y,x,e
E  y,e
F  x,e

My lecturer already did a solution but I can't seem to understand where he got the answer:

   First     Follow
S  u         $
B  w         y,x,z,y
D  y,x,e     z
E  y,e       x,z
F  x,e       z

PS. e = epsilon

PSS. this is the example I followed https://www.youtube.com/watch?v=dDoo5BF9T4E&t=787s

1
I think there is a typo here. The follow set for B should be v, x, y, z.ggorlen

1 Answers

1
votes

A symbol is in the FIRST set of a non-terminal if the expansion of that non-terminal can start with that symbol. That's the definition of a FIRST set, and if your algorithm does not produce that result, the algorithm is wrong.

For example, the only production for S in your example grammar is:

S → uBDz

That means that every expansion of S must start with u. There is no way to get around that fact. It's impossible to skip over the u and continue the production with some other string. So it's easy to see that FIRST(S) is precisely {u}.

Similarly, B has two productions

B → Bv
B → w

That means that B can produce:

w
Bv → wv
Bv → Bvv → wvv
Bv → Bvvv → wvvv

and so on. The expansions can contain as many v as you want, but the first symbol is always w. So FIRST(B) is precisely {w}.