parsing - LL(1) grammar. How to calculate follow set if one has self-recursion rules? -


according rules paper :

  1. if start nonterminal, put eof in follow(a)
    find productions on right-hand-side:
  2. for each production x → αaβ, put first(β) − {epsilon } in follow(a)
  3. if epsilon in first(β) put follow(x) follow(a)
  4. for each production x → αa, put follow(x) follow(a)

i have next piece in grammar:

 ...     -> c b     b -> ,     c -> epsilon     c -> =     b -> ;  ... 

when try calculate follow(b) according rule 4 have calculate follow(a) , vice versa. have stackoverflowexception because of self-recursion.

what should do?

you don't use recursion. iterate, calculating follow set based on calculated on previous iteration:

  1. if start nonterminal, put eof (a new terminal indicating end of input) in f[0](a).

  2. for each production x → αaβ, put first(β) − {epsilon} in f[n](a),

  3. and if epsilon in first(β) put f[n-1](x) f[n](a)

  4. for each production x → αa, put f[n-1](x) f[n](a).

  5. stop when f[n](*) == f[n-1](*)

follow(*) == f[n](*)


Comments