context-free-grammar

Context-free grammar for this language


I'm working on an exercise with the language L = { 0i(01)i | i ≥ 0 }.

My intuition is that the language generates strings of equal 0’s and 01’s, so for example the strings: 001, 000010101 and the empty string will be in L. But for example the strings 0, 01, 000101010101 are not in L, is that correctly understood?

We can construct a context-free grammar for it which should be simple, if I have understood the language correctly, I am thinking something like this:

S → 0S01
S → ε

Where ε represents the empty string. Is there missing something in this? Is the understanding correct?


Solution

  • Your grammar S->0S01 | e is correct, and will produce the language you describe. That is: a sequence of '0's followed by the same number of '01's.

    The grammar is context free and also LL(2) i believe.

    It is context free because it is in the form NONTERM->term. It is LL(2) because when parsing S you must look ahead 2 characters to determine whether to return S to 0S01 or e. i.e to chose the correct production.