Consider the following languages : \(L_1 = \{a^m b^n | m ≠ n\} \\ L_2 = \{a^m…
2017
Consider the following languages :\(L_1 = \{a^m b^n | m ≠ n\} \\ L_2 = \{a^m b^n | m = 2n+1\} \\ L_3 = \{a^m b^n | m ≠ 2n\}\)
Which one of the following statement is correct ?
- A.
Only
\(L_1\)and\(L_2\)are context free languages - B.
Only
\(L_1\)and\(L_3\)are context free languages - C.
Only
\(L_2\)and\(L_3\)are context free languages - D.
\(L_1\),\(L_2\)and\(L_3\)are context free languages
Attempted by 86 students.
Show answer & explanation
Correct answer: D
Answer: All three languages are context-free.
L1 = {a^m b^n | m ≠ n}:
Split L1 as the union of strings with m>n and strings with m<n.
For m>n every string can be written as a^k a^n b^n = a^k (a^n b^n) with k≥1, which is the concatenation of a regular language a^+ and the CFL {a^n b^n}, hence a CFL. Similarly for m<n every string is (a^n b^n) b^+.
Therefore L1 is a union of two CFLs and is context-free.
L2 = {a^m b^n | m = 2n+1}:
A simple context-free grammar that generates exactly this language is
S -> a | aa S b
This produces a (base case) and for each application of the recursive rule adds two a's and one b, yielding a^{2n+1} b^n, so L2 is context-free.
L3 = {a^m b^n | m ≠ 2n}:
Construct a pushdown automaton that on each a pushes two stack symbols and on each b pops one symbol. There are three possible end behaviours:
If after processing all input the stack has leftover symbols, then m>2n, accept.
If during processing some b causes an underflow (attempting to pop from an empty region), move to an accepting 'underflow' state and accept the remainder, which corresponds to m<2n.
If stack is exactly balanced at the end, then m=2n and the PDA should reject; otherwise accept. This PDA construction shows L3 is context-free.
Thus L3 is a CFL.
Conclusion: All three languages are context-free, so the correct choice is that each of L1, L2 and L3 is context-free.