CFL Closure Properties, CFL Algorithms, TMs




CS154

Chris Pollett

Mar 20, 2013

Outline

Closure Properties of CFL

CFL are closed under union
Proof idea: Let `G` and `H` with start symbols `S` and `T` respectively, be two CFGs for the CFL's we want to take the union of. Make a new grammar with the same alphabet, with the union of the two grammars productions (after renaming) together with the new rules `S'->S|T` where `S'` is the new start variable.
CFLs are closed under intersection by regular languages
Proof idea: use the Cartesian product construction on a PDA for the CFL together with a DFA for the regular language.
CFLs are not closed under intersection or complementation
Proof: The languages `{a^nb^nc^m | n, m ge 0}` and `{a^mb^nc^n | n, m ge0}` are both context-free. Their intersection is `{a^nb^nc^n | n ge 0}` which is not CFL by the pumping lemma. CFLs are not closed under complementation as using deMorgan rules, intersection can be defined from union and complementation.

Algorithms for CFLs

There is an algorithm, which given a grammar `G` written down formally as a 4-tuple, can decide whether or not `L(G)` is empty.
To do this, the algorithm check if the start variable is useless. If it is we know the language is empty; otherwise it is not.
There is an algorithm to check given G whether or not L(G) is infinite.
To do this, we first eliminate `epsilon`-rules, unit-productions, and useless symbols from `G`. We then construct a graph where `(A,B)` is an edge for two variables `A`, `B` in the graph iff `A->xBy` for some production in `G`. If there is a cycle in this graph then `C=>^star uCv` for some variable `C` in the original grammar. As there are no useless symbols in this grammar, we must have `C=>^star z`, for some string `z` of terminals. Hence, also, `S=>^star sCt =>^star szt, S=>^star sCt =>^star suCvt =>^star suzvt`, etc. Thus, one can argue there is a cycle in the graph iff `G'`s grammar is infinite.

Homework Problems (Sec 1) and (Sec 3)

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:

`S -> epsilon|SS|(S)`

Converting this to a PDA according to the method from class gives:

Balanced Parentheses PDA

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`
`r_1 = q_(\mbox(loop))` and `s_1 = SZ` and `w_1 = epsilon`, using `(q_(\mbox(loop)), S) in delta(q_(\mbox(start)), epsilon, epsilon)`.
`r_2 = q_(4)` and `s_2 = )Z` and `w_2 = epsilon`, using `(``q_4, '``)``'``)`` in delta(q_(\mbox(loop)), epsilon, S)`.
`r_3 = q_5` and `s_3 = S)Z` and `w_3 = epsilon`, using `(q_5, S) in delta(q_4, epsilon, epsilon)`.
`r_4 = q_(\mbox(loop))` and `s_4 = (S)Z` and `w_4 = epsilon`, using `(q_(\mbox(loop)), '``(``'``)`` in delta(q_5, epsilon, epsilon)`.
`r_5 = q_(\mbox(loop))` and `s_5 = S)Z` and `w_5 = '('`, using `(q_(\mbox(loop)), epsilon) in delta(q_(\mbox(loop)), '``(``', '``(``')`.
`r_6 = q_(3)` and `s_6 = S)Z` and `w_6 = epsilon`, using `(q_3, S) in delta(q_(\mbox(loop)), epsilon, S)`.
`r_7 = q_(\mbox(loop))` and `s_7 = SS)Z` and `w_7 = epsilon`, using `(q_(\mbox(loop)), S) in delta(q_3, epsilon, epsilon)`.
`r_8 = q_(4)` and `s_8 = )S)Z` and `w_8 = epsilon`, using `(``q_4, '``)``'``)`` in delta(q_(\mbox(loop)), epsilon, S)`.
`r_9 = q_5` and `s_9 = S)S)Z` and `w_9 = epsilon`, using `(q_5, S) in delta(q_4, epsilon, epsilon)`.
`r_(10) = q_(\mbox(loop))` and `s_(10) = (S)S)Z` and `w_(10) = epsilon`, using `(q_(\mbox(loop)), '``(``'``)`` in delta(q_5, epsilon, epsilon)`.
`r_(11) = q_(\mbox(loop))` and `s_(11) = S)S)Z` and `w_(11) = '('`, using `(q_(\mbox(loop)), epsilon) in delta(q_(\mbox(loop)), '``(``', '``(``')`.
`r_(12) = q_(\mbox(loop))` and `s_(12) = )S)Z` and `w_12 = epsilon`, using `(q_(\mbox(loop)), epsilon) in delta(q_(\mbox(loop)), epsilon, S)`.
`r_(13) = q_(\mbox(loop))` and `s_(13) = S)Z` and `w_(13) = ')'`, using `(q_(\mbox(loop)), epsilon) in delta(q_(\mbox(loop)), '``)``', '``)``')`.
`r_(14) = q_(4)` and `s_(14) = ))Z` and `w_(14) = epsilon`, using `(``q_4, '``)``'``)`` in delta(q_(\mbox(loop)), epsilon, S)`.
`r_(15) = q_5` and `s_(15) = S))Z` and `w_(15) = epsilon`, using `(q_5, S) in delta(q_4, epsilon, epsilon)`.
`r_(16) = q_(\mbox(loop))` and `s_(16) = (S))Z` and `w_(16) = epsilon`, using `(q_(\mbox(loop)), '``(``'``)`` in delta(q_5, epsilon, epsilon)`.
`r_(17) = q_(\mbox(loop))` and `s_(17) = S))Z` and `w_(17) = '('`, using `(q_(\mbox(loop)), epsilon) in delta(q_(\mbox(loop)), '``(``', '``(``')`.
`r_(18) = q_(\mbox(loop))` and `s_(18) = ))Z` and `w_18 = epsilon`, using `(q_(\mbox(loop)), epsilon) in delta(q_(\mbox(loop)), epsilon, S)`.
`r_(19) = q_(\mbox(loop))` and `s_(19) = )Z` and `w_(19) = ')'`, using `(q_(\mbox(loop)), epsilon) in delta(q_(\mbox(loop)), '``)``', '``)``')`.
`r_(20) = q_(\mbox(loop))` and `s_(20) = Z` and `w_(20) = ')'`, using `(q_(\mbox(loop)), epsilon) in delta(q_(\mbox(loop)), '``)``', '``)``')`.
`r_(21) = q_(\mbox(accept))` and `s_(21) = Z` and `w_(21) = epsilon`, using `(q_(\mbox(accept)), Z) in delta(q_(\mbox(loop)), epsilon, 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.

General Models of Computation

A Real-Life Turing Machine

A photo an a Turing Machine implemented in real-life

[YouTube video of above machine in action]

Example

Formal Definition of Turing Machine

Diagrams and Examples

Image of a TM in JFLAP

Configurations, Yields

Accept, Reject, Recognize, Decide