Context-Free Grammars & Languages
![Question 85, GATE 2006: Using grammar S→AC|CB, C→aCb|ε, A→aA|a, B→Bb|b, count the production applications in a derivation from S of a^l b^m, with l,m≥0 and l≠m. GFG prints the same expression in A and D; GateForum D instead has +3.
Options:
(A) max(l,m)+2
(B) l+m+2
(C) l+m+3
(D) max(l,m)+2 [GFG]; max(l,m)+3 [GateForum]](../../assets/questions/CS-2006-Q85-1.webp)
![Question 85, GATE 2006: Using grammar S→AC|CB, C→aCb|ε, A→aA|a, B→Bb|b, count the production applications in a derivation from S of a^l b^m, with l,m≥0 and l≠m. GFG prints the same expression in A and D; GateForum D instead has +3.
Options:
(A) max(l,m)+2
(B) l+m+2
(C) l+m+3
(D) max(l,m)+2 [GFG]; max(l,m)+3 [GateForum]](../../assets/questions/CS-2006-Q85-2.webp)
Answer & solution
Answer: max(l,m)+2 (options A and D are printed identically) (no official key was published)
1. For l>m use S→AC (1 step), expand C by C→aCb exactly m times and then C→ε (m+1 steps), and derive al−m from A using l−m productions (l−m−1 recursive steps and one terminal step). Total=1+(m+1)+(l−m)=l+2.
2. For m>l the CB branch gives m+2. Thus the length is max(l,m)+2, regardless of the order in which independent nonterminals are expanded.
3. Both A and D visibly print this same expression, so no unique option letter is supported.
4. Teaching check: Count production applications, including C→ε and the initial S choice. For l=3,m=1, use S→AC, A→aA→aa, C→aCb→ab: five applications, equal to max(3,1)+2. Interleaving A and C expansions does not alter the number of productions used.