Similar questions 3 more →
Context-Free Grammars & Languages

Answer & solution
Official answer: A, C, D
Givens and goal
Consider strings ai bj ck d^ℓ with i,j,k,ℓ≥0. Which restrictions yield context-free languages? A i+k=j+ℓ; B i=k and j=ℓ; C i=ℓ and j=k; D i+j=k+ℓ.
Approach
Give stack constructions for the positive choices and a pumping-lemma contradiction for the crossing equalities.
Step-by-step solution
1. For A, scan the string while tracking block order a*b*c*d* in finite control. Maintain the signed difference (#a+#c)−(#b+#d). Store its sign in the finite state and its absolute value as repeated stack symbols. On a or c, add 1; on b or d, subtract 1, popping if this cancels the current sign and pushing otherwise. Accept exactly when the final difference is zero. This is a pushdown automaton, so A is context-free.
2. For D, check block order, push one marker for every a or b, and pop one for every c or d. Reject an attempt to pop an empty counter and accept exactly with zero balance at the end. This enforces i+j=k+ℓ.
3. For C the equalities are nested. The grammar S→aSd|T, T→bTc|ε generates precisely ai bj cj di: outer recursion matches a with d, inner recursion matches b with c. Hence C is context-free.
4. For B suppose a context-free pumping length p exists, and take w=ap bp cp dp. Every decomposition w=uvxyz with |vxy|≤p and |vy|>0 has its pumping window in one block or at most two adjacent blocks. It cannot reach both members of either matching pair a/c or b/d.
5. Pump v and y away, using exponent 0. At least one block count decreases, and its matching nonadjacent block is untouched. The equality i=k or j=ℓ therefore fails. This contradicts the pumping lemma for every allowed decomposition, so B is not context-free.
6. The correct selections are A,C,D. Two independent crossing counters cannot in general be handled by the single last-in-first-out store used for nested matching.