Consider the following languages: I. \(\{a^mb^nc^pd^q \mid m+p=n+q, \text{…
2018
Consider the following languages:
I. \(\{a^mb^nc^pd^q \mid m+p=n+q, \text{ where } m, n, p, q \geq 0 \}\)
II. \(\{a^mb^nc^pd^q \mid m=n \text{ and }p=q, \text{ where } m, n, p, q \geq 0 \}\)
III. \(\{a^mb^nc^pd^q \mid m=n =p \text{ and } p \neq q, \text{ where } m, n, p, q \geq 0 \}\)
IV. \(\{a^mb^nc^pd^q \mid mn =p+q, \text{ where } m, n, p, q \geq 0 \}\)
Which of the languages above are context-free?
- A.
I and IV only
- B.
I and II only
- C.
II and III only
- D.
II and IV only
Attempted by 91 students.
Show answer & explanation
Correct answer: B
Final answer: I and II are context-free.
I is context-free. Sketch: write the condition as m + p = n + q, and generate a central part that enforces m = n and p = q, then allow matched a...d pairs around it or matched b...c pairs between the middle blocks. One convenient grammar (union of two CFL constructions) is:
S -> X | Y
X -> a X d | A B
Y -> A R B
A -> a A b | ε (generates a^n b^n)
B -> c B d | ε (generates c^p d^p)
R -> b R c | ε (generates b^t c^t)
This grammar respects the ordering a^* b^* c^* d^* and generates exactly the strings where m+p = n+q by either adding matched a...d pairs around a central A B (case s ≥ 0) or inserting matched b...c pairs between A and B (case s ≤ 0).
II is context-free. It factors as {a^m b^m} concatenated with {c^p d^p}, i.e. strings of the form a^m b^m c^p d^p, and concatenation of two context-free languages is context-free.
III is not context-free. Intersecting III with the regular language a* b* c* (i.e. force q=0) yields {a^n b^n c^n | n>0}, which is a well-known non-context-free language. Since CFLs are closed under intersection with regular languages, III cannot be context-free.
IV is not context-free. Restrict to the subcase q = 0; then p = m n and we get the language {a^m b^n c^{mn} | m,n ≥ 0}, which is known to be non-context-free (one can prove this by the pumping lemma or other standard arguments). Thus IV cannot be context-free.
Therefore the context-free languages among I–IV are exactly I and II.
A video solution is available for this question — log in and enroll to watch it.