CS154
Chris Pollett
Mar 20, 2013
Problem 2. Give a CFG for the language consisting of balanced parentheses. Use the algorithm from class to convert this CFG into a PDA. Show formally how this PDA would accept the string `(()())`.
Answer. A CFG for this language is:
Converting this to a PDA according to the method from class gives:
Given the input string `w=(()())` we need to come up with sequence `w_i`, `r_i` and `s_i` that match the definition of acceptance for a PDA. We list these below
`r_0 = q_(\mbox(start))` and `s_0 = Z`Problem 4. Consider the language `L` consisting of all strings over `{0,1}` of the form `0^{2n}1^{n}0^{n}1^{2n}` for any `n ge 0`. Show using the pumping lemma this language is not context-free.
Answer. Assume this language was CFL. Let `G` be a CFG generating it. Let `p` be the pumping length given by the pumping lemma. Consider the string `w=0^{2p}1^{p}0^{p}1^{2p}`. As `w in L`, by the pumping lemma, `w` can be written in the form `uvxyz` with `|vxy| < p`, `|vy| > 0` and such that `uv^jxy^jz` is in `L` for `j ge 0`. There are two main cases to consider: either (1) `v` or `y` contains both a `0`s and `1`s or (2) `v` is made up of only `0`s or only `1`s and `y` is made up of only `0` or only `1`'s. In case (1) the string `uv^2xy^2z` with have at least one more alternation between 0's and 1's than the string `w` and all other strings in `L` which should have only 3 alternations `0->^(1)1->^(2)0->^(3)1`, so we get a contradiction, despite the pumping lemma saying it should be in the language it is not in the correct format. For case (2), since `|vxy| < p` and `|w| = 6p` it is either within the first `3p` symbols, or within the last `3p` symbols, or within the middle `2p` symbols. By the pumping lemma the string `uv^0xy^0z` if `vxy` is within the first `3p` or last `3p` symbols then the other half will have too many `0`'s and `1`'s to be in the correct format for a string in the language since `|vy| > 0`. On the other hand, if `vxy` is from the middle `2p` symbols, then `uv^0xy^0z = 0^{2p}1^{k}0^{j}1^{2p}` where either `k` or `j` is strictly less than `p` so again the string does not have the correct format to be in `L`. Hence, for all ways we could split the string according to the pumping lemma we get a contradiction. Therefore, our assumption that the language was CFL must be false.
On input `w`: (1) Zig-zag across the tape to the corresponding positions on either side of the # symbol to check whether these positions contain the same symbol. If they do not, or if no # is found do into the reject state. If they have the same symbol change the symbol to a new symbol X. (2) When all the symbols on the left side of the # have been X'd out, check if there are any more symbols to the right of the #. If yes reject; if not accept.