Formal Languages With Visualizations

Chapter 8 Properties of Context-free Languages

| About   中文版   «  7.3. Deterministic Pushdown Automata   ::   Contents   ::   9.1. Models of Computation  »

8.1. Properties of Context-Free Languages

8.1.1. Properties of Context-Free Languages

Note

We will not cover Theorem 8.2

Which of the following languages are CFL?

  • \(L = \{a^nb^nc^j \mid 0 < n\le j\}\)

    Note

    NOT CFL

  • \(L = \{a^nb^ja^nb^j \mid n>0, j>0\}\)

    Note

    NOT CFL

  • \(L = \{a^nb^ja^kb^p \mid n+j \le k+p, n>0, j>0, k>0, p>0 \}\)

    Note

    CFL

  • \(L = \{a^nb^ja^jb^n \mid n>0, j>0\}\)

    Note

    CFL

Pumping Lemma for Regular Languages

Let \(L\) be a regular language, then there is a constant \(m\) such that \(w\in L\), \(|w|\ge m\), \(w = xyz\) such that

  • \(|xy| \le m\)

  • \(|y| \ge 1\)

  • for all \(i\ge 0, xy^iz \in L\)

We were able to show that \(a^nb^n\) is not a regular language.

Pumping Lemma for CFL’s

Let \(L\) be any infinite CFL. Then there is a constant \(m\) depending only on \(L\), such that for every string \(w\) in \(L\), with \(|w| \ge m\), we may partition \(w = uvxyz\) such that:

\(|vxy| \le m\), (limit on size of substring)
\(|vy| \ge 1\), (\(v\) and \(y\) not both empty)
For all \(i \ge 0, uv^ixy^iz \in L\)

Proof: (sketch)

There is a CFG \(G\) such that \(L = L(G)\).

Consider the parse tree of a long string in \(L\).

For any long string, some nonterminal \(N\) must appear twice in the path.

lt8ptree1
\(N \stackrel{*}{\Rightarrow} vNy\) and \(N \stackrel{*}{\Rightarrow} x\).
\(S \stackrel{*}{\Rightarrow} uNz \stackrel{*}{\Rightarrow} uvNyz \stackrel{*}{\Rightarrow} uvxyz\)
By repeating the \(v\) and \(y\) subtrees, get \(N \stackrel{*}{\Rightarrow} v^iNy^i \stackrel{*}{\Rightarrow} v^ixy^i\).
lt8ptree2

QED

Proof: (by contradiction)

Assume \(L\) is a CFL and apply the pumping lemma.
Let \(m\) be the constant in the pumping lemma and consider \(w = a^mb^mc^m\). Note \(|w|\ge m\).
Show there is no division of \(w\) into \(uvxyz\) such that \(|vy|\ge 1\), \(|vxy|\le m\), and \(uv^ixy^iz \in L\) for \(i = 0, 1, 2, \ldots\).

Case 1: Neither \(v\) nor \(y\) can contain 2 or more distinct symbols. If \(v\) contains \(a\) ‘s and \(b\) ‘s, then \(uv^2xy^2z \notin L\) since there will be \(b\) ‘s before \(a\) ‘s.
Thus, \(v\) and \(y\) can be only \(a\) ‘s, \(b\) ‘s, or \(c\) ‘s (not mixed).

Case 2: \(v = a^{t_1}\), then \(y = a^{t_2}\) or \(b^{t_3} (|vxy| \le m)\)
If \(y = a^{t_2}\), then \(uv^2xy^2z = a^{m+t_1+t_2}b^mc^m \notin L\) since \(t_1 + t_2 > 0, n(a) > n(b)\) (number of \(a\) ‘s is greater than number of \(b\) ‘s)
If \(y = b^{t_3}\), then \(uv^2xy^2z = a^{m+t_1}b^{m+t_3}c^m \notin L\) since \(t_1 + t_3 > 0\), either \(n(a) > n(c)\) or \(n(b) > n(c)\).

Case 3: \(v = b^{t_1}\), then \(y = b^{t_2}\) or \(c^{t_3}\).
If \(y = b^{t_2}\), then \(uv^2xy^2z = a^mb^{m+t_1+t_2}c^m \notin L\) since \(t_1 + t_2 > 0, n(b) > n(a)\).
If \(y = c^{t_3}\), then \(uv^2xy^2z = a^mb^{m+t_1}c^{m+t_3} \notin L\) since \(t_1 + t_3 > 0\), either \(n(b) > n(a)\) or \(n(c) > n(a)\).

Case 4: \(v = c^{t_1}\), then \(y = c^{t_2}\).
Then, \(uv^2xy^2z = a^mb^mc^{m+t_1+t_2} \notin L\) since \(t_1 + t_2 > 0, n(c) > n(a)\).

Thus, there is no breakdown of \(w\) into \(uvxyz\) such that \(|vy| \ge 1\), \(|vxy| \le m\) and for all \(i\ge 0\), \(uv^ixy^iz\) is in \(L\).
This is a contradiction, thus, \(L\) is not a CFL. Q.E.D.

8.1.1.1. (Chap 8.2) Closure Properties of CFLs

Example: \(L=\{a^nb^n | n>0\}\), \(L\circ L = \{a^nb^na^mb^m | n>0, m>0 \}\)

Note

Example for theorem below:

\(L_1 = \{a^nb^ma^n | m> 0, n>0 \}\)
\(L_2 = \{w | w \in{\Sigma}^{*}\) and \(w\) has an even number of b’s}, \(\Sigma = \{a,b\}\),
\(L_1 \cup L_2 = \{a^nb^mb^ma^n\}\) is a CFL.

Questions about CFL:

1. Decide if CFL is empty?
Know how to get rid of useless variables and productions, if there is anything left, then CFL is not empty.
2. Decide if CFL is infinite?
Get rid of useless variables and productions, \(\lambda\)-rules, and unit productions. Then if there is a variable that repeats \(A \stackrel{*}{\Rightarrow} xAy\), then \(L\) is infinite.

Note

What type of language is a grammar that has this property? How do we recognize it automatically?

The following “adversary game” is similar to the one used to help prove that a grammar is not regular.

   «  7.3. Deterministic Pushdown Automata   ::   Contents   ::   9.1. Models of Computation  »

Close Window