Context-Free Languages
COS2601 - Theoretical Computer Science II · Pushdown Automata Theory
Context-Free Languages
Context-free languages (CFLs) are a class of formal languages that can be generated by context-free grammars (CFGs). They are essential in computer science, particularly in the fields of programming languages and compilers. This topic covers the Pumping Lemma for context-free languages, a crucial theorem that provides a method to prove that certain languages are not context-free.
The Pumping Lemma for Context-Free Languages
The Pumping Lemma states that for any context-free language L, there exists a number p (the pumping length) such that any string w in L with a length of at least p can be divided into five parts: u, v, x, y, and z. These parts must satisfy the following conditions:
- The string w can be expressed as w = uvxyz.
- The lengths of v and y must be greater than zero: length(v) > 0 and length(y) > 0.
- The length of vxy must be less than or equal to p: length(vxy) ≤ p.
- For any integer n ≥ 0, the string uv^nxy^nz must also be in L.
Understanding the Parts of the Pumping Lemma
To understand the Pumping Lemma better, let us break down the string w into the parts u, v, x, y, and z:
- u: the substring of w generated before the first occurrence of the nonterminal that leads to the repetition.
- v: the substring generated by the first occurrence of the nonterminal that leads to the repetition.
- x: the substring that comes from the nonterminal that is not repeated.
- y: the substring generated by the second occurrence of the nonterminal that leads to the repetition.
- z: the substring of w generated after the last occurrence of the nonterminal.
Let us visualize this with an example. Consider a string w = a^n b^n for n ≥ 1 (this language consists of strings with equal numbers of a's followed by b's). For a sufficiently large n, we can derive a string w using a context-free grammar:
S -> aSb | ε
For n = 3, a possible string w is aaabbb. We can choose p = 3, and we can decompose w as follows:
- u = aa
- v = a
- x = empty string
- y = b
- z = bb
Thus, we have w = uvxyz = aaabbb.
Pumping the Parts
According to the Pumping Lemma, we can now pump the parts v and y. For example, if we let n = 2, we get:
uv^2xy^2z = aabbb = aaa bbb
This string is also in the language L. Similarly, if n = 3, we have:
uv^3xy^3z = aaabbb = aaaa bbb
Both strings are valid in the language. The Pumping Lemma thus shows that we can repeat certain segments of a string in a context-free language, and the resulting string will still belong to the language.
Using the Pumping Lemma to Prove Non-Context-Freeness
The Pumping Lemma is often used to prove that certain languages are not context-free. To do this, we assume that a language L is context-free and then show that it contradicts the conditions set by the Pumping Lemma.
Consider the language L = { a^n b^n c^n | n ≥ 1 }. We will show that this language is not context-free using the Pumping Lemma.
Assume L is context-free. Then there exists a pumping length p. Take the string w = a^p b^p c^p, which is in L and has a length greater than p. According to the Pumping Lemma, we can write w = uvxyz, where:
- length(v) > 0
- length(y) > 0
- length(vxy) ≤ p
Since v and y cannot span different types of characters (a's, b's, or c's), they must consist of only one type of character. Let’s assume:
- v consists of a's
- y consists of b's
Then, when we pump v and y, we get:
uv^2xy^2z = a^(p+k) b^(p+k) c^p
for some integer k > 0. This string is not in L because the number of a's and b's is not equal to the number of c's. Thus, we have a contradiction, which implies that L is not context-free.
Examples of Non-Context-Free Languages
1. The language L = { a^n b^n | n ≥ 1 } is context-free.
2. The language L = { a^n b^n c^n | n ≥ 1 } is not context-free, as shown above.
3. The language L = { ww | w ∈ {a, b}* } is also not context-free. This language consists of strings that are the concatenation of a string with itself.
Conclusion
The Pumping Lemma is a powerful tool for understanding context-free languages and proving that certain languages are not context-free. By analysing the structure of strings and their components, you can determine the properties of the languages generated by context-free grammars.
Check your understanding
- What are the five parts into which a string must be divided according to the Pumping Lemma?
- Give an example of a context-free language and explain why it is context-free.
- Explain why the language L = { a^n b^n c^n | n ≥ 1 } is not context-free.
- How can the Pumping Lemma be used to prove that a language is not context-free?