# First and Follow Algorithm in Compiler Design

While I was studying Compilers,I saw an example in first and follow that illustrated how to 'find the first and follow' non-terminals in a grammer.

But I couldn't get how `FOLLOW(E')=FOLLOW(E)={ \$ ,) }` even though the first and follow algorithm states it like that.

I don't understand why the FOLLOW(E') contains ')'. Does anyone know how this works?

• F -> (E) -> (T E') And that's how you get an `E'` followed by a `)` . – Kilian Foth Nov 14 '14 at 9:07
• @KilianFoth:that's very right.Could you explain how the 3rd statement came in first and follow algorithm because I couldn't get how Follow(A) woud reside in Follow(B).If there is a production \$A\toB\$ how would Follow(A) reside in Follow(B) if B comes only after A.I think only A's follow would be B(First(B)).But follow of B would be only which comes after B.But I can't see A coming after B.Could you help me. – justin Nov 14 '14 at 9:22
• You're confused by two different uses of 'after'. One concerns order of symbols in a string. The other concerns sequence of transformations in time. (And it doesn't help that the temporal sense is itself expressed as symbol order when dealing with production rules.) Try substituting every use of 'after' with 'to the right of' or 'in the next step' in the material to make it less confusing. – Kilian Foth Nov 14 '14 at 9:38
• @KilianFoth:sorry.I have mistyped the comment.Could you explain how the 3rd statement came in first and follow algorithm because I couldn't get how Follow(A) woud reside in Follow(B).If there is a production \$A\toB\$ how would Follow(A) reside in Follow(B) if B comes right of A.I think only A's follow would be B(First(B)).But I think follow of B would be only the non-terminals which comes right of B.But I can't see A to the right of B. – justin Nov 14 '14 at 9:43
• `AB` is completely different from `A -> B`. One is a string, the other is a production rule. And the production rule doesn't state that A comes to the left of B, it says "if you have A you can replace it with B". That's why everything that can follow an `A` can also follow `B`, because every A might be replaced by a `B` at will. – Kilian Foth Nov 14 '14 at 9:53