Reputation: 1848
According to the rules from this paper :
- If A is the start nonterminal, put EOF in FOLLOW(A)
Find the productions with A on the right-hand-side:- For each production X → αAβ, put FIRST(β) − {EPSILON } in FOLLOW(A)
- If EPSILON is in FIRST(β) then put FOLLOW(X) into FOLLOW(A)
- For each production X → αA, put FOLLOW(X) into FOLLOW(A)
I have next piece in my grammar:
...
A -> C B
B -> , A
C -> EPSILON
C -> =
B -> ;
...
When I try to calculate FOLLOW(B) according to the rule 4 I have to calculate FOLLOW(A) and vice versa. So I have StackOverflowException because of self-recursion.
What should I do?
Upvotes: 3
Views: 3094
Reputation: 9224
You don't use recursion. You iterate, calculating the FOLLOW
set based on what was calculated on the previous iteration:
If A is the start nonterminal, put EOF (a new terminal indicating the end of input) in F[0](A).
For each production X → αAβ, put FIRST(β) − {EPSILON} in F[n](A),
and if EPSILON is in FIRST(β) then put F[n-1](X) into F[n](A)
For each production X → αA, put F[n-1](X) into F[n](A).
Stop when F[n](*) == F[n-1](*)
FOLLOW(*) == F[n](*)
Upvotes: 4