2
votes

Given a grammar G defined by

A -> Ca
B -> Cb
C -> e|f

Is this grammar LL(1)?

I realize that we could compress this down into a single line, but that's not the point of this question.

Mainly, can an LL(1) grammar have multiple rules that begin with the same non-terminal?

As a follow up question, how do I construct a parse table for the above grammar?

I've worked out the following:

FIRST(A) = {e,f}
FIRST(B) = {e,f}
FIRST(C) = {a,b}

FOLLOW(A) = {}
FOLLOW(B) = {}
FOLLOW(C) = {a,b}

I looked at this post, but didn't understand how they went from the FIRSTs and FOLLOWs to a table.

2

2 Answers

2
votes

The grammar you've given is not LL(1) because there is a FIRST/FIRST conflict between the two productions A → Ca and A → Cb.

In general, grammars with multiple productions for the same nonterminal that begin with the same nonterminal will not be LL(1) because you will get a FIRST/FIRST conflict. There are grammars with this property that are LL(1), though they're essentially degenerate cases. Consider, for example, the following grammar:

A → Ea

A → Eb

E → ε

Here, the nonterminal E only expands out to the empty string ε and therefore, in effect, isn't really there. Therefore, the above grammar is LL(1) because there are no FIRST/FIRST conflicts between the two productions. To see this, here's the parse table:

     a  b  $
A    Ea Eb -
E    ε  ε  -

Hope this helps!

1
votes

I am solve your question in 2 case:

First case if the terminal is {a,b,e,f} enter image description here

Second case if the terminal is {a,b,f} and e is epsilon

enter image description here

so no multipe entry this is LL(1).

For more information: look at this explanation and example below:

enter image description here

enter image description here

Best regards