The following CFG S → aB | bA A → a | aS | bAA B → b | bS | aBBgenerates…
2012
The following CFG
S → aB | bA
A → a | aS | bAA
B → b | bS | aBBgenerates strings of terminals that have
Answer: C. equal number of a’s and b’s — Concept: the language of a context-free grammar is identified by finding a quantity that every production preserves and then arguing by induction on the…
- A.
odd number of a’s and odd number of b’s
- B.
even number of a’s and even number of b’s
- C.
equal number of a’s and b’s
- D.
not equal number of a’s and b’s
Attempted by 6 students.
Show answer & explanation
Correct answer: C
Concept: the language of a context-free grammar is identified by finding a quantity that every production preserves and then arguing by induction on the number of derivation steps. Over a two-letter alphabet the natural quantity is the difference d(w) = (number of a’s in w) − (number of b’s in w), because each production changes it by a fixed amount that can be read straight off the right-hand side.
Application: give each non-terminal a claimed value of d — d = +1 for A, d = −1 for B, and d = 0 for S — and then check every production for consistency with that assignment by adding up the contributions of the symbols on its right-hand side.
A → acontributes one a and no b, so d = +1.A → aScontributes +1 from the leading a and 0 fromS, so d = +1.A → bAAcontributes −1 from the leading b and +1 from each of the twoAs, so d = −1 + 1 + 1 = +1.B → bcontributes one b and no a, so d = −1.B → bScontributes −1 from the leading b and 0 fromS, so d = −1.B → aBBcontributes +1 from the leading a and −1 from each of the twoBs, so d = +1 − 1 − 1 = −1.S → aBcontributes +1 from the leading a and −1 fromB, so d = 0.S → bAcontributes −1 from the leading b and +1 fromA, so d = 0.
Every production respects the assignment, so induction on the number of derivation steps gives d(w) = 0 for every terminal string w derivable from S: such a string always carries as many a’s as b’s.
Non-terminal | Strings it derives | Count signature |
|---|---|---|
|
| equally many a’s and b’s |
|
| one more a than b |
|
| one more b than a |
The converse holds as well, which is what makes the description exact rather than merely necessary. A non-empty string with d = 0 either begins with a and leaves a remainder of d = −1, which B supplies, or begins with b and leaves a remainder of d = +1, which A supplies. A string with d = −1 beginning with b leaves a remainder of d = 0, matching B → bS; one beginning with a leaves a remainder of d = −2, and since the running difference moves one step at a time that remainder can always be cut into two consecutive pieces of d = −1 each, matching B → aBB. The reasoning for A is the mirror image, and only the empty string is left out, because no production deletes a non-terminal.
Cross-check by deriving concrete strings:
S ⇒ aB ⇒ ab— one a and one b.S ⇒ bA ⇒ ba— one a and one b.S ⇒ aB ⇒ aaBB ⇒ aabB ⇒ aabb— two a’s and two b’s.S ⇒ aB ⇒ abS ⇒ abaB ⇒ abab— two a’s and two b’s.
Contrast with the other descriptions on offer: ab carries an odd count of each letter while aabb carries an even count of each, and both are derivable, so no single parity condition covers the whole language; and because d(w) = 0 always holds, a string whose two counts differ can never be derived. The language is therefore exactly the set of non-empty strings over {a, b} that contain equally many a’s and b’s.