Grammar question, problem with FIRST
Consider following grammar:
A → BC
B → Ba | epsilon
C → bD | epsilon
D → …
…
The problem here is that rule B
can derive epsilon
and left-recursive as well.
In order to find FIRST(A)
I am searching FIRST(B)
.
FIRST(B)
, because it is left-recursive.
So what is FIRST(B)
? And FIRST(A)
?
FIRST(B) → {a, epsilon}
FIRST(A) → 开发者_如何学编程{a, b, epsilon}
Is that correct?
Yes, you have it right. A left-recursion does not contribute to FIRST, because when Ba
is matched for B
, the B
in Ba
must start with something that B
can start with - because it's a B
, after all. :)
You could also instead factor out the left-recursion first.
精彩评论