Pergunta

I am doing an exercise to create FIRST and FOLLOW sets for a grammar. I think what I did is correct but the answer is slightly different from mine. So need help from someone to verify this. Thank you. The grammar is :

    P -> L
    L -> I X
    X -> ; L | EPSILON
    I -> A | C | W
    A -> id := E
    C -> if E then L O endif
    O -> else L | EPSILON
    W -> while E do L end
    E -> E2 R
    R -> Op1 E2 R | EPSILON
    E2 -> T S
    S -> Op2 E2 | EPSILON
    T -> c | id
    Op1 -> < | = | !=
    Op2 -> + | -

EPSILON is the real 'epsilon' And here is my answer the FIRST set for X:

    FIRST(; L) = {;}
    FOLLOW(X) = {$, else, end, endif}

But the answer given is:

    FIRST(; L) = {;}
    FOLLOW(X) = {$, else, end, endif, then}

Can someone please verify which one is the correct answer?Thanks :)

Foi útil?

Solução

I don't see how then could be in FOLLOW(X). I get the same answer as you do.

The only thing that can precede then in that grammar is an E and E cannot end with L. Furthermore, FOLLOW(E) includes do as well as then, so if a followset includes FOLLOW(E) it would have to include both of those tokens.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top