Question

Grammar:

E -> T D
D -> + T D | EPSILON
T -> F M
M -> * F M | EPSILON
F -> ( E ) | id

The FOLLOW(D) given in the book = { +, $, ) }

How does the + is a follower of D ?

and if it is correct, then * should also be a follower of M, which is not as evident in FOLLOW(M) = { +, $, ) }.

Was it helpful?

Solution

Always follow the rules:

  1. Place $ in FOLLOW(S), where S is the start symbol and $ in the input right endmarker.
  2. If there is a production A -> aBs, where FIRST(s) except e is placed in FOLLOW(B).
  3. If there is a production A -> aB or a production A->aBs where FIRST(s) contains e, then everything in FOLLOW(A) is in FOLLOW(B).

The one important point to note in the above rule is:

Point 3: A -> aB: FOLLOW(A) is in FOLLOW(B) is applicable to EVERY non-terminal on the RHS. Consider this as Rule 3.a.

Now,

FOLLOW(E) = { ), $ }                    // by rule 2
FOLLOW(D) = { ), $ }                    // by rule 3
FOLLOW(T) = FIRST(D) = { +, $, ) }      // by rule 3 and 3.a
FOLLOW(M) = { +, $, ) }                 // by rule 3 and 3.a
FOLLOW(F) = FIRST(M) = {*, $, +, ) }    // by rule 3 and 3.a
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top