Context-Free Languages
[59/92%]Recall the CFG that we gave in Example 2.4. For convenience, let's rename its variables with single letters as follows.
Give parse trees and derivations for each string.
a
((a))
Use the languages and together with Example 2.36 to show that the class of context-free languages is not closed under intersection.
Use part (a) and DeMorgan's law (Theorem 0.20) to show that the class of context-free languages is not closed under complementation.
ᴬ Answer each part for the following context-free grammar .
What are the variables of ?
What are the terminals of ?
Which is the start variable of ?
Give three strings in .
Give three strings not in .
True or False: .
True or False: aba.
True or False: .
True or False: .
True or False: aba.
True or False: aba.
True or False: .
True or False: .
True or False: .
Give a description in English of .
Give context-free grammars that generate the following languages. In all parts, the alphabet is 0,1.
ᴬ
ᴬ
The empty set
Give informal descriptions and state diagrams of pushdown automata for the languages in Exercise 2.4. In all parts, the alphabet is 0,1.
The empty set
Give context-free grammars generating the following languages.
ᴬ The set of strings over the alphabet with more a's than b's
The complement of the language
ᴬ
ᴬ Give informal English descriptions of PDAs for the languages in Exercise 2.6.
ᴬ Show that the string the girl touches the boy with the flower has two different leftmost derivations in grammar on page 103. Describe in English the two different meanings of this sentence.
Give a context-free grammar that generates the language
Is your grammar ambiguous? Why or why not?
Give an informal description of a pushdown automaton that recognizes the language in Exercise 2.9.
Convert the CFG given in Exercise 2.1 to an equivalent PDA, using the procedure given in Theorem 2.20.
Convert the CFG given in Exercise 2.3 to an equivalent PDA, using the procedure given in Theorem 2.20.
Let be the following grammar. ; and is the set of rules:
Describe in English.
Prove that is not regular.
Convert the following CFG into an equivalent CFG in Chomsky normal form, using the procedure given in Theorem 2.9.
Give a counterexample to show that the following construction fails to prove that the class of context-free languages is closed under star. Let be a CFL that is generated by the CFG . Add the new rule and call the resulting grammar . This grammar is supposed to generate .
Show that the class of context-free languages is closed under the regular operations, union, concatenation, and star.
Use the results of Exercise 2.16 to give another proof that every regular language is context free, by showing how to convert a regular expression directly to an equivalent context-free grammar.
ᴬ
Let be a context-free language and be a regular language. Prove that the language is context free.
Let . Use part (a) to show that is not a CFL.
- Let CFG be the following grammar.
Give a simple description of in English. Use that description to give a CFG for , the complement of .
Let . Show that if is context free and is regular, then is context free.
- Let . Give a CFG generating the language of strings with twice as many a's as b's. Prove that your grammar is correct.
- Let . Show that is a context-free language.
- Let . Show that is a context-free language.
- Let . Show that is a context-free language.
For any language , let . Show that the class of context-free languages is closed under the SUFFIX operation.
Show that if is a CFG in Chomsky normal form, then for any string of length , exactly steps are required for any derivation of .
- Let STMT be the following grammar.
is a natural-looking grammar for a fragment of a programming language, but is ambiguous.
Show that is ambiguous.
Give a new unambiguous grammar for the same language.
- Give unambiguous CFGs for the following languages.
- Show that the language in Exercise 2.9 is inherently ambiguous.
Use the pumping lemma to show that the following languages are not context free.
ᴬ
ᴬ
Let be the language of all palindromes over 0,1 containing equal numbers of 0s and 1s. Show that is not context free.
Let and . Show that is not context free.
- Show that is not context free.
Consider the language , where is the grammar given in Exercise 2.13. The pumping lemma for context-free languages, Theorem 2.34, states the existence of a pumping length for . What is the minimum value of that works in the pumping lemma? Justify your answer.
Let be a CFG in Chomsky normal form that contains variables. Show that if generates some string with a derivation having at least steps, is infinite.
Give an example of a language that is not context free but that acts like a CFL in the pumping lemma. Prove that your example works. (See the analogous example for regular languages in Problem 1.54.)
- Prove the following stronger form of the pumping lemma, wherein both pieces and must be nonempty when the string is broken up. If is a context-free language, then there is a number where, if is any string in of length at least , then may be divided into five pieces, , satisfying the conditions:
for each ,
and , and
.
ᴬ Refer to Problem 1.41 for the definition of the perfect shuffle operation. Show that the class of context-free languages is not closed under perfect shuffle.
Problem 1.41: For languages and , the perfect shuffle of and is the language
Refer to Problem 1.42 for the definition of the shuffle operation. Show that the class of context-free languages is not closed under shuffle.
Problem 1.42: For languages and , the shuffle of and is the language
- Say that a language is prefix-closed if all prefixes of every string in the language are also in the language. Let be an infinite, prefix-closed, context-free language. Show that contains an infinite regular subset.
- Read the definitions of and in Problem 1.40.
Show that the class of CFLs is not closed under NOPREFIX.
Show that the class of CFLs is not closed under NOEXTEND.
- Let . Here . Prove that is not context free.
For strings and , write if the symbols of are a permutation of the symbols of . In other words, if and have the same symbols in the same quantities, but possibly in a different order. For any string , define . For any language , let .
Show that if , then the SCRAMBLE of a regular language is context free.
What happens in part (a) if contains three or more symbols? Prove your answer.
If and are languages, define . Show that if and are regular languages, then is a CFL.
- Let . Prove that is not a CFL.
Consider the following CFG :
Describe and show that is ambiguous. Give an unambiguous grammar where and sketch a proof that is unambiguous.
Let and let be the collection of strings that contain at least one 1 in their second half. In other words, .
Give a PDA that recognizes .
Give a CFG that generates .
Let . Let be the language of all strings that contain a 1 in their middle third. Let be the language of all strings that contain two 1s in their middle third. So and .
Show that is a CFL.
Show that is not a CFL.
- We defined the rotational closure of language to be . Show that the class of CFLs is closed under rotational closure.
- We defined the CUT of language to be . Show that the class of CFLs is not closed under CUT.
Show that every DCFG is an unambiguous CFG.
ᴬ Show that every DCFG generates a prefix-free language.
- Show that the class of DCFLs is not closed under the following operations:
Union
Intersection
Concatenation
Star
Reversal
Let be the following grammar:
Show that . Use a proof by induction on the length of .
Use the -test to show that is a DCFG.
Describe a DPDA that recognizes .
Let be the following grammar that we introduced in Example 2.45. Use the -test to show that is not a DCFG.
- Let where is defined in Problem 2.55. Show that is not a DCFL. (Hint: Assume that is a DCFL and consider its DPDA . Modify so that its input alphabet is . When it first enters an accept state, it pretends that c's are b's in the input from that point on. What language would the modified accept?)
Problem 2.55: is the grammar
- Let . Prove that is not a DCFL.
- Let . Prove that is not a DCFL. (Hint: Suppose that when some DPDA is started in state with symbol on the top of its stack, never pops its stack below , no matter what input string reads from that point on. In that case, the contents of 's stack at that point cannot affect its subsequent behavior, so 's subsequent behavior can depend only on and .)
- If we disallow -rules in CFGs, we can simplify the -test. In the simplified test, we only need to check that each of 's accept states has a single rule. Prove that a CFG without -rules passes the simplified -test iff it is a DCFG.