Computer Sciences > GATE 2026 SET-1 > Context Free Grammar
Consider the following context free grammar:

S → abaABAbba
A → aaBBAb | bBabaa
B → aBb | ab

L(G) is the language generated by the grammar. For a string s ∈ L(G), let n1(s) be the number of a's in s and n2(s) be the number of b's in s. Which of the following is/are correct?
A
For every string s ∈ L(G), n1(s) ≥ n2(s)
B
There is a string s ∈ L(G), n1(s) ≥ 2n2(s)
C
For every string s ∈ L(G), n1(s) < n2(s)
D
For every string s ∈ L(G), n1(s) ≤ 2n2(s)

Correct : a,d

The correct answers are A and D.
The key insight is to compute the fixed difference d = n1(s) - n2(s) for all strings in L(G) by analyzing each production rule.
For B → ab: n1 - n2 = 1 - 1 = 0. So dB = 0.
For A → aaBBAb: n1 - n2 = 2 + 2×0 - 1 + 1 = 2. So dA = 2. (Both A-rules give 2 — consistent.)
For S → abaABAbba: n1 - n2 = (4-3) + dA + dB + dA = 1 + 2 + 0 + 2 = 5.
So for every string s ∈ L(G): n1(s) = n2(s) + 5.
A (n1 ≥ n2): Always true since n1 = n2 + 5 > n2.
B (n1 ≥ 2n2 for some s): Would need n2 ≤ 5, but the minimum n2 in any generated string is large (many b"s come from A and B productions), so this is false.
C (n1 < n2): Always false since n1 > n2.
D (n1 ≤ 2n2): n2+5 ≤ 2n2 ⇒ n2 ≥ 5. Since all strings have n2 well above 5, this always holds.

Similar Questions

Consider the following grammar where 𝑆 is the start symbol, and 𝑎 and 𝑏 are terminal symbols.S &rarr; aSbS | bS | &epsilon;w which of the following statements...
#1478 MSQ
A palindrome is a word that reads the same forwards and backwards. In a game of words, a player has the following two plates painted with letters. From...
#1 MCQ
Which number does not belong in the series below? 2, 5, 10, 17, 26, 37, 50, 64
#4 MCQ

Related Topics

GATE CS 2026 GATE CS 2026 Set-1 Q15 Context Free Grammar n1 n2 Count a's b's CFG Grammar String Properties Formal Languages GATE Theory of Computation CFG Language Analysis GATE CS 2026 Solved String Count CFG GATE Previous Year Questions

Unique Visitor Count

Total Unique Visitors

Loading......