views:

169

answers:

1

If somebody could help me with the rules of FIRST and FOLLOW sets that would be awesome.

The question is calculate the FOLLOW sets for all of the non-terminals in the following grammar

S ::= S b T a E ¦ a T b ¦ c T a c     R ::= E T ¦ a E

T ::= a c E ¦ epsilon                 E ::= R ¦ T a d ¦ epsilon

I have read the rules of creating follow sets and understood the basic examples but I am confused at what I should be doing when I write FIRST(S) for this

any help would be greatly appreciated.

+1  A: 

Are you talking about the FIRST1 sets? You simply build a set of all nonterminals, that may be at the beginning of a derivation.

Consider the right-hand side of S: every derivation of S starts with the S, a, c, so FIRST1(S) = FIRST1(S) union {a, c} = {a, c}.

Larry_Croft
I've never seen them called FIRST1 sets, but that's exactly the definition of FIRST sets that I was taught.
Kaleb Pederson
would FIRST(R) = FIRST(E) union {a} = FIRST(E) union FIRST(T) union {epsilon} = {epsilon, a}and also the FIRST(E) = {epsilon, a} ?
Kaleb Pederson: What i've been taught is that there are FIRSTk sets for every k, so when you're writing a parser with k symbol lookahead, you compute the FIRSTk sets, this way you might resolve ambiguities without refactoring your grammar.stagnetti: I'm not completely sure about the epsilon since it is no terminal in the strict sense, so I'd say FIRST1(R) = {a}. But maybe someone else can help out here.
Larry_Croft
Many thanks guys I reckon I have it solved. Cheers once again for your help.