Section 2.1

Exercises

[17/94%]
Show
LaTeX
Problem 2.1

Recall the CFG G4G_{4} that we gave in Example 2.4. For convenience, let's rename its variables with single letters as follows.

E→E+T∣TT→T×F∣FF→(E)∣a \begin{aligned} & E \rightarrow E+T \mid T \\ & T \rightarrow T \times F \mid F \\ & F \rightarrow (E) \mid \mathrm{a} \end{aligned}

Give parse trees and derivations for each string.

?
(a)

a

(b)

a+a\mathrm{a}+\mathrm{a}

(c)

a+a+aa+a+a

(d)

((a))

Problem 2.2
?
(a)

Use the languages A={am bncn∣m,n≥0}A=\left\{ \mathrm{a}^{m} \mathrm{~ b}^{n} \mathrm{c}^{n} \mid m, n \geq 0\right\} and B={an bncm∣m,n≥0}B=\left\{ \mathrm{a}^{n} \mathrm{~ b}^{n} \mathrm{c}^{m} \mid m, n \geq 0\right\} together with Example 2.36 to show that the class of context-free languages is not closed under intersection.

(b)

Use part (a) and DeMorgan's law (Theorem 0.20) to show that the class of context-free languages is not closed under complementation.

Problem 2.3

ᴬ Answer each part for the following context-free grammar GG.

R→XRX∣SS→aT b∣bTaT→XTX∣X∣εX→a∣b \begin{aligned} R & \rightarrow X R X \mid S \\ S & \rightarrow \mathrm{a} T \mathrm{~ b} \mid \mathrm{b} T \mathrm{a} \\ T & \rightarrow X T X \mid X \mid \varepsilon \\ X & \rightarrow \mathrm{a} \mid \mathrm{b} \end{aligned}
?
(a)

What are the variables of GG?

(b)

What are the terminals of GG?

(c)

Which is the start variable of GG?

(d)

Give three strings in L(G)L(G).

(e)

Give three strings not in L(G)L(G).

(f)

True or False: T⇒abaT \Rightarrow \mathrm{aba}.

(g)

True or False: T⇒∗T \stackrel{*}{\Rightarrow } aba.

(h)

True or False: T⇒TT \Rightarrow T.

(i)

True or False: T⇒∗TT \stackrel{*}{\Rightarrow } T.

(j)

True or False: XXX⇒∗X X X \stackrel{*}{\Rightarrow } aba.

(k)

True or False: X⇒∗X \stackrel{*}{\Rightarrow } aba.

(l)

True or False: T→∗XXT \xrightarrow {*} X X.

(m)

True or False: T⇒∗XXXT \stackrel{*}{\Rightarrow } X X X.

(n)

True or False: S⇒∗εS \stackrel{*}{\Rightarrow } \varepsilon.

(o)

Give a description in English of L(G)L(G).

Problem 2.4

Give context-free grammars that generate the following languages. In all parts, the alphabet Σ\Sigma is 0,1.

?
(a)

ᴬ {w∣w contains at least three 1 s }\left\{ w \mid w\text{ contains at least three 1 s }\right\}

(b)

{w∣w starts and ends with the same symbol}\left\{ w \mid w\text{ starts and ends with the same symbol}\right\}

(c)

{w∣the length of w is odd}\left\{ w \mid \text{the length of } w \text{ is odd}\right\}

(d)

ᴬ {w∣ the length of w is odd and its middle symbol is a 0}\left\{ w \mid \text{ the length of }w\text{ is odd and its middle symbol is a 0}\right\}

(e)

{w∣w=wR, that is, w is a palindrome}\left\{ w \mid w=w^{\mathcal{R}}, \text{ that is, } w \text{ is a palindrome}\right\}

(f)

The empty set

Problem 2.5

Give informal descriptions and state diagrams of pushdown automata for the languages in Exercise 2.4. In all parts, the alphabet Σ\Sigma is 0,1.

?
(a)

{w∣w contains at least three 1 s }\left\{ w \mid w\text{ contains at least three 1 s }\right\}

(b)

{w∣w starts and ends with the same symbol}\left\{ w \mid w\text{ starts and ends with the same symbol}\right\}

(c)

{w∣the length of w is odd}\left\{ w \mid \text{the length of } w \text{ is odd}\right\}

(d)

{w∣ the length of w is odd and its middle symbol is a 0}\left\{ w \mid \text{ the length of }w\text{ is odd and its middle symbol is a 0}\right\}

(e)

{w∣w=wR, that is, w is a palindrome}\left\{ w \mid w=w^{\mathcal{R}}, \text{ that is, } w \text{ is a palindrome}\right\}

(f)

The empty set

Problem 2.6

Give context-free grammars generating the following languages.

?
(a)

ᴬ The set of strings over the alphabet {a,b}\left\{ \mathbf{a}, \mathbf{b}\right\} with more a's than b's

(b)

The complement of the language {an bn∣n≥0}\left\{ \mathrm{a}^{n} \mathrm{~ b}^{n} \mid n \geq 0\right\}

(c)

ᴬ {w#x∣wR is a substring of x for w,x∈{0,1}∗}\left\{ w \# x \mid w^{\mathcal{R}} \text{ is a substring of } x \text{ for } w, x \in \left\{ 0,1\right\}^{*}\right\}

(d)

{x1#x2#⋯#xk∣k≥1, each xi∈{a,b}∗, and for some i and j,xi=xjR}\left\{ x_{1} \# x_{2} \# \cdots \# x_{k} \mid k \geq 1, \text{ each } x_{i} \in \left\{ \mathrm{a}, \mathrm{b}\right\}^{*}, \text{ and for some } i \text{ and } j, x_{i}=x_{j}^{\mathcal{R}}\right\}

Problem 2.7

ᴬ Give informal English descriptions of PDAs for the languages in Exercise 2.6.

?
Problem 2.8

ᴬ Show that the string the girl touches the boy with the flower has two different leftmost derivations in grammar G2G_{2} on page 103. Describe in English the two different meanings of this sentence.

?
Problem 2.9

Give a context-free grammar that generates the language

A={ai bjck∣i=j or j=k where i,j,k≥0}. A=\left\{ \mathrm{a}^{i} \mathrm{~ b}^{j} \mathrm{c}^{k} \mid i=j \text{ or } j=k \text{ where } i, j, k \geq 0\right\} .

Is your grammar ambiguous? Why or why not?

?
Problem 2.10

Give an informal description of a pushdown automaton that recognizes the language AA in Exercise 2.9.

?
Problem 2.11

Convert the CFG G4G_{4} given in Exercise 2.1 to an equivalent PDA, using the procedure given in Theorem 2.20.

?
Problem 2.12

Convert the CFG GG given in Exercise 2.3 to an equivalent PDA, using the procedure given in Theorem 2.20.

?
Problem 2.13

Let G=(V,Σ,R,S)G=(V, \Sigma , R, S) be the following grammar. V={S,T,U};Σ={0,#}V=\left\{ S, T, U\right\} ; \Sigma =\left\{ 0, \# \right\}; and RR is the set of rules:

S→TT∣UT→0T∣T0∣#U→0U00∣# \begin{aligned} S & \rightarrow T T \mid U \\ T & \rightarrow 0 T \mid T 0 \mid \# \\ U & \rightarrow 0 U 00 \mid \# \end{aligned}
?
(a)

Describe L(G)L(G) in English.

(b)

Prove that L(G)L(G) is not regular.

Problem 2.14

Convert the following CFG into an equivalent CFG in Chomsky normal form, using the procedure given in Theorem 2.9.

A→BAB∣B∣εB→00∣ε \begin{aligned} & A \rightarrow B A B \mid B \mid \varepsilon \\ & B \rightarrow 00 \mid \varepsilon \end{aligned}
?
Problem 2.15

Give a counterexample to show that the following construction fails to prove that the class of context-free languages is closed under star. Let AA be a CFL that is generated by the CFG G=(V,Σ,R,S)G=(V, \Sigma , R, S). Add the new rule S→SSS \rightarrow S S and call the resulting grammar G′G^{\prime }. This grammar is supposed to generate A∗A^{*}.

?
Problem 2.16

Show that the class of context-free languages is closed under the regular operations, union, concatenation, and star.

?
Problem 2.17

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.

?