Compiler Design First and follow

2.2k views Asked by At

I have i doubt in a problem,trying to calculate FOLLOW(S) which needs FOLLOW(A) in it,but as A production is after S production,we have not calculated FOLLOW(A) yet.So,should we add FOLLOW(A) too inf FOLLOW(S)???

enter image description here

1

There are 1 answers

6
Am_I_Helpful On

To compute FOLLOW(S) for any non-terminal S, apply the followwing rules until nothing can be added to any FOLLOW set.

  1. Place $ in FOLLOW(S), where S is the start symbol.

  2. If there is a production S -> CBD, then everything in FIRST(D) except epsilon is in FOLLOW(B).

  3. If there is a proudction S -> CB, or a production S -> CBD, where FIRST(D) contains epsilon, then everything in FOLLOW(S) is in FOLLOW(B).

Trying to calculate FOLLOW(S) which needs FOLLOW(A) in it,but as A production is after S production,we have not calculated FOLLOW(A) yet.So,should we add FOLLOW(A) too inf FOLLOW(S)???

SO, as you can see from the given grammar,

  1. as per rule 1, the follow(S) will contains $.

  2. Also, as per rule 2, S appears in bodies only followed by D(non-terminal), thus, everything except epsilon that is in FIRST(D) must be in FOLLOW(S). So, FOLLOW(S) must contain a.

  3. Also,as per rule 3, considering the production A->ASD |epsilon and FIRST(D) contains epsilon, we should get FOLLOW(S) = FOLLOW(A).

So, yes you need to calculate FOLLOW(A) before FOLLOW(S).

And, FOLLOW(A) = everything in FIRST(S) except epsilon + b = {a,b}.

  1. Also, in C-production, S is followed by b, so FOLLOW(S) must contain f.

Hence, FOLLOW(S) = {a,b,f,$}.

I don't know how you come to calculate that FOLLOW(A) is required for calculating FOLLOW(S). It is not the case. Please again go through the rules 1 to 3 mentioned in the starting of this answer.