Search code examples
compiler-constructionset

First and Follow Set of a given CFG


I have this CFG and there is a loop in it then how to make first and follow set of looped cfg as first sets are easy but having problem in follow sets

S -> iEtSS'|a
S' -> es
E -> b

Solution

  • I fixed the question as you might be asking answer for this question.

    S -> iEtSS'|a<br>
    S' -> eS|eplison<br>
    E -> b 
    

    First

    First(S) -> {i, a}<br>
    First(S') -> {e, eplison}<br>
    First(E) -> {b}
    

    Follow

    Follow(S) -> {$, First(S')} -> {$, e}<br>
    Follow(S') -> {Follow(S)} -> {$, e}<br>
    Follow(E) -> {t}
    

    Even if there is a loop it possibly can't have more than above non terminal in Follow set.

    You may have seen loop as

    Follow(S) -> {$, First(S'), Follow(S)}
    ->Follow(S) is because First(S') has eplison.