• Home
  • Textbooks
  • Computability, Complexity and Languages: Fundamentals of Theoretical Computer Science
  • Context-Free Languages

Computability, Complexity and Languages: Fundamentals of Theoretical Computer Science

Martin Davis, Elaine J. Weyuker

Chapter 9

Context-Free Languages - all with Video Answers

Educators


Chapter Questions

01:26

Problem 1

Show that $\left\{a^{[i]} \mid i\right.$ is a prime number $\}$ is not context-free.

Patrick Burns
Patrick Burns
Numerade Educator

Problem 1

Give a context-free grammar for generating valid FORTRAN arithmetic expressions over the alphabet $\{a, b,+,-, *, /, \uparrow,()$,$\} , where variable$ names are elements of $\{a, b\}^*$ of length at least 1 . Is the grammar ambiguous? What are the implications of this?

Check back soon!

Problem 1

Let $L=\left\{a^{[n]} b^{[m]} a^{[n]} \mid m, n>0\right\} \cup\left\{a^{[n]} c^{[n]} \mid n>0\right\}$.
(a) Show that $L$ is context-free.
(b) Construct a pushdown automaton which accepts $L$.

Check back soon!

Problem 1

Write a context-free grammar $\Gamma$ such that
$$
L(\Gamma)=\left\{a^{[i]} b^{[j]} c^{[k]} \mid i=j \vee j=k\right\} .
$$
This language is an example of an inherently ambiguous language, i.e., a language such that every grammar which generates it is ambiguous. Explain why this language is inherently ambiguous.

Check back soon!

Problem 1

Find a context-free grammar generating the set of arithmetic statements of FORTRAN (or BASIC or PL/I or Pascal).

Check back soon!
06:46

Problem 1

(a) Find context-grammars $\Gamma_1, \Gamma_2$ such that
$$
\begin{aligned}
& L\left(\Gamma_1\right)=\left\{a^{[i]} b^{[j]} \mid i \geq j>0\right\} \\
& L\left(\Gamma_2\right)=\left\{a^{[2 i]} b^{[i]} \mid i>0\right\} .
\end{aligned}
$$
(b) Find Chomsky normal form grammars which generate the same languages.

Chris Trentman
Chris Trentman
Numerade Educator

Problem 1

(a) Write a context-free grammar to generate all and only regular expressions over the alphabet $\{a, b\}$.
(b) Can a regular grammar generate this language? Support your

Check back soon!

Problem 2

Let $T=\{\downarrow, p, q\}$ be the set of terminals for the grammar $\Gamma$ :
$$
S \rightarrow p, \quad S \rightarrow q, \quad S \rightarrow \downarrow S S .
$$
Find a Chomsky normal form grammar which generates $L(\Gamma)$.

Check back soon!

Problem 2

Show that $\left\{a^{\left[i^2\right]} \mid i>0\right\}$ is not context-free.

Check back soon!

Problem 2

A grammar $\Gamma$ is self-embedding if there is a variable $X$ such that
$$
X \dot{\vec{\Gamma}} v X w, \quad \text { where } \quad v, w \in(\mathscr{V} \cup T)^*-\{0\} .
$$
Let $L$ be a context-free language. Prove that $L$ is regular if and only if there is a non-self-embedding context-free grammar $\Gamma$ such that $L(\Gamma)=L$.

Check back soon!

Problem 2

Give an unambiguous context-free grammar which generates the same language as the ambiguous grammar
$$
\begin{aligned}
& S \rightarrow a B \\
& S \rightarrow A b \\
& A \rightarrow a A B \\
& B \rightarrow A B b \\
& A \rightarrow a \\
& B \rightarrow b .
\end{aligned}
$$

Check back soon!

Problem 2

Let us call a generalized pushdown automaton a device which functions just like a pushdown automaton except that it can write any finite sequence of symbols on the stack in a single step. Show that for every generalized pushdown automaton $\mathscr{M}$, there is a pushdown automaton $\tilde{\mathscr{M}}$ such that $L(\mathscr{M})=L(\bar{M})$

Check back soon!
01:58

Problem 2

Consider the grammar $\Gamma$ with start symbol $S$ and productions
$$
\begin{array}{lll}
S \rightarrow X X Y Y & X \rightarrow X X & Y \rightarrow Y Y \\
X \rightarrow a & Y \rightarrow b . &
\end{array}
$$
Show that $\Gamma$ generates the same language as the grammar of Fig. 1.1.

Adriano Chikande
Adriano Chikande
Numerade Educator
06:46

Problem 3

A context-free grammar is said to be in Greibach normal form if every production of the grammar is of the form
$$
X \rightarrow a Y_1 Y_2 \cdots Y_k, \quad k \geq 0,
$$
where $a \in T$ and $X, Y_1, Y_2, \ldots, Y_k \in \mathscr{V}$. Show that there is an algorithm that transforms any positive context-free grammar into one in Greibach normal form that generates the same language.

Chris Trentman
Chris Trentman
Numerade Educator

Problem 3

Show that a context-free language on a one-letter alphabet is regular.

Check back soon!
04:47

Problem 3

Let
$$
P=\left\{\frac{u_i}{c_i} \mid i=1,2, \ldots, k\right\}
$$
be a set of dominoes on the alphabet $A$. Let $B=\left\{c_1, \ldots, c_k\right\}$ be an alphabet such that $A \cap B=\varnothing$. Let $c \notin A \cup B$. Let
$$
\begin{aligned}
R & =\left\{y c y^R \mid y \in A^* B^*\right\}, \\
L_1 & =\left\{u_{i_1} u_{i_2} \cdots u_{i_n} c_{i_n} c_{i_{n-1}} \cdots c_{i_2} c_{i_1}\right\}, \\
L_2 & =\left\{v_{i_1} v_{i_2} \cdots v_{i_n} c_{i_n} c_{i_{n-1}} \cdots c_{i_2} c_{i_1}\right\}, \\
S_p & =\left\{y c z^R \mid y \in L_1, z \in L_2\right\} .
\end{aligned}
$$
Recall that by Theorem 6.5 , the Post correspondence problem $P$ has a solution if and only if $L_1 \cap L_2 \neq \varnothing$.
(a) Show that the Post correspondence problem $P$ has no solution if and only if $R \cap S_p=\varnothing$.
(b) Show that $(A \cup B \cup\{c\})^*-R$ and $(A \cup B \cup\{c\})^*-S_P$ are both context-free. (Hint: Construct push-down automata.)
(c) From (a) and (b) show how to conclude that there is no algorithm that can determine for a given context-free grammar $\Gamma$ with terminals $T$ whether $L(\Gamma) \cup\{0\}=T^*$.
(d) Now show that there is no algorithm that can determine for a given context-free grammar $\Gamma_1$ and regular grammar $\Gamma_2$ whether
(i) $L\left(\Gamma_1\right)=L\left(\Gamma_2\right)$,
(ii) $L\left(\Gamma_1\right) \supseteq L\left(\Gamma_2\right)$.

Chris Trentman
Chris Trentman
Numerade Educator

Problem 4

Show that there is an algorithm that transforms any positive contextfree grammar into a grammar that generates the same language for which every production is of the form
$$
\begin{aligned}
& A \rightarrow a, \\
& A \rightarrow a B,
\end{aligned}
$$
or
$$
A \rightarrow a B C,
$$
$A, B, C \in \mathscr{V}, a \in T$

Check back soon!