Lecture 11 (PDAs), 2/27/06
Concepts, definitions (in italics):
Reading:
Skills:
Notes:
Question: design a CFG for the language L = {w in {a,b}* : w contains equal number of a's and b's}.
Consider any string w in
L. Imagine going along the string from left to right keeping a tally
as follows: Start the tally at 0, add 1 for every a, subtract 1 for
every b. Our tally will be 0 at the end, and it may also be 0 in
places along the way. Here is an example for aabbba, where we
show the characters followed by the tally
0 a
1 a 2 b1 b 0 b -1 a 0
Break up w into substrings at points where the tally is 0. For example, aabbba gets broken up into aabb and ba. Note that, since the tally is 0 at the beginning and at the end of each substrings, each of them must contain an equal number of a's and b's. Also, the tally is never 0 in the middle of any substring.
Each of these substrings from step 2 ends in a different character than it starts. This is proved by contradiction: Suppose it starts and ends in the same character. Case 1: it's an a. Then, the tally is 1 after the first a, and it's -1 before the last a. Therefore, it must have been 0 in the middle, which is a contradiction. Case 2 (it's a b) is symmetric.
Therefore, each string in L can be represented as 0 or more of these strings with an equal number of a's and b's that start and end in a different character. The desired grammar is as follows
S -> e | SX
X -> 0S1 | 1S0