Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grammar question, problem with FIRST

Tags:

grammar

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).
But I stuck on FIRST(B), because it is left-recursive.

So what is FIRST(B)? And FIRST(A)?
My version is:

FIRST(B) → {a, epsilon}
FIRST(A) → {a, b, epsilon}

Is that correct?

like image 929
Little Jeans Avatar asked Jul 15 '26 14:07

Little Jeans


1 Answers

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.

like image 87
Karl Knechtel Avatar answered Jul 22 '26 18:07

Karl Knechtel