Suppose I have the following CFG.
A -> B | Cx | EPSILON
B -> C | yA
C -> B | w | z
Now if I try to find
FIRST(C) = FIRST(B) U FIRST(w) U FIRST(z)
= FIRST(C) U FIRST(yA) U {w, z}
That is, I'm going in a loop. Thus I assume I have to convert it into a form which has immediate left recursion, which I can do as follows.
A -> B | Cx | EPSILON
B -> C | yA
C -> C | yA | w | z
Now if I try to calculate FIRST sets, I think I can get it done as follows.
FIRST(C) = FIRST(C) U FIRST(yA) U FIRST(w) U FIRST(z)
= { y, w, z } // I ignore FIRST(C)
FIRST(B) = FIRST(C) U FIRST(yA)
= { y, w, z }
FIRST(A) = FIRST(B) U FIRST(Cx) U FIRST(EPSILON)
= { y, w, z, EPSILON }
Am I correct there?
But even if I'm right there, I still run into a problem when I try to calculate FOLLOW sets from this grammar.
FOLLOW(A) = { $ } U FOLLOW(B) U FOLLOW(C)
I get FOLLOW(B) from 2nd rule and FOLLOW(C) from 3rd rule. But now to calculate FOLLOW(B), I need FOLLOW(A) (from 1st grammar rule) so again I'm stuck in a loop.
Any help? Thanks in advance!