I've been trying to find the "first set" of this context-free grammar. I've come up with 2 answers but I'm unsure if they're correct. I would be grateful if someone could explain how to generate the first set of this grammar.
Both answers are written in different ways because the sources I've read have explained it with different syntax.
The grammar in question:
E1 -> E2+E1|E2
E2 -> num*E2|num
My first answer:
| A -> α | FIRST(α) |
|:----------- |------------:|
| E1 -> E2+E1 | {num, num} |
| E1 -> E2 | {num, num} |
| E2 -> num*E2 | {num} |
| E2 -> num | {num} |
My second answer:
FIRST(E1) = {num}
FIRST(E2) = {num}
The most commonly-used definition of a FIRST set is that FIRST(S) is the set of terminal symbols that can appear at the start of some derivation starting at S, plus ε if the empty string can also be derived.
Here, notice that FIRST(S1) = FIRST(S2) because any derivation starting from S1 must necessarily start with something derived from S2, and S2 can't derive the empty string. Then, FIRST(S2) = {num} since every production of S2 starts with {num}.
So yes, FIRST(S1) = FIRST(S2) = {num}.
The particular means by which you were computing FIRST(S1) and FIRST(S2) - that is, looking at the productions and seeing what produced what - is a good one.