formal-languagescontext-free-language

Is the language L = { a^n b^j : j ≤ n ≤ 2j − 1 } context-free?


Is the language L = { anbj : j ≤ n ≤ 2j − 1 } context-free? I have no idea how to start with this exercise. I am familiar with the pumping lemma, but I have no idea how I should use it here. It seems to me that this language is context-free and that something could be done with the stack to realise it, but I do not know how. So if someone could give me a hint to start, I would highly appreciate it.

I took this exercise from the book 'Formal languages and automata', fifth edition, by Peter Linz, exercise 8.1.14

After some help in the comments, I thought of the following grammar:

S-> aSb|aaaSbb|λ


Solution

  • I claim the language L is generated by the following context-free grammar G, with starting symbol S and an additional non-terminal T:

    1. SaaSb
    2. SaTb
    3. TaTb
    4. T ↦ ε (the empty string)

    Showing this grammar is a valid presentation of the language will prove the language is context-free.

    Proof. (L ⊆ G). We notice that any n such that jn ≤ 2j − 1 can be written as a sum n = j + k, with 0 ≤ k < j. We also notice the case j = 0 is impossible, because the condition then implies 0 ≤ −1. To generate a string aj+kbj with 0 ≤ k < j, 1 ≤ j, we apply:

    1. rule (0) k times, obtaining a2kSbk; then
    2. rule (1) once, obtaining a2k+1Tbk+1; then
    3. rule (2) j − (k + 1) times, obtaining aj+kTbj, and finally
    4. rule (3) once, obtaining the desired string aj+kbj.

    Therefore every string of L is generated by G.

    (G ⊆ L; sketch). By induction over grammar rules. Observe that every string G can generate has the form anZbj where n, j ∈ ℕ, and Z is a non-terminal or the empty string. Then observe that:

    The only way to eliminate non-terminals is to pass rule (3), and that can be only applied after rule (1). Therefore every string generated by G belongs to L.

    As such, L and the language generated by G are the same language. Because G is a context-free grammar, the language L is context-free. ∎