How to determine the FIRST set of E in this grammar?
I wonder how to determine the FIRST
set of E with grammar:
E -> XYE | e
X -> x
Y -> y
Ca开发者_如何学编程n anyone give me some direction?
Well, assuming that you're starting with E, then either the first terminal is x via the E→XYE production (since X always produces x) or it is e via the E→e production. So First(E) = {x,e}.
That seems pretty straightforward...
Treat rules of the form A -> ...x... | ...y .... as two rules A -> ...x... and B -> ...y...
Form a set S initially containing rules of form E-> ....
then
Set a set P to empty.
Set a set F to empty.
Repeat until S is empty
Choose element of S, and call it R
If R is in P, remove R from S
Elsif R is of the form A -> b ...
then { add b to F,
add R to P,
remove R from S}
Else (R is the form A -> B ...)
then { place all rules of form B -> ... into S
remove R from S}
End
WHen the loop terminates, F contains the tokens which are the First(F).
This does not take into account empty productions.
精彩评论