• Home
  • Textbooks
  • Languages and Machines: An Introduction to the Theory of Computer Science
  • Context-Free Grammars

Languages and Machines: An Introduction to the Theory of Computer Science

Thomas A. Sudkamp

Chapter 3

Context-Free Grammars - all with Video Answers

Educators


Chapter Questions

05:01

Problem 1

Let $\mathrm{G}$ be the grammar
$$
\begin{aligned}
& S \rightarrow a b S c \mid A \\
& A \rightarrow c A d \mid c d .
\end{aligned}
$$
a) Give a derivation of $a b a b c c d d c c$.
b) Build the derivation tree for the derivation in part (a).
c) Use set notation to define $\mathrm{L}(\mathrm{G})$.

Trang Hoang
Trang Hoang
Numerade Educator
05:01

Problem 2

Let $G$ be the grammar
$$
\begin{aligned}
& S \rightarrow A S B \mid \lambda \\
& A \rightarrow a A b \mid \lambda \\
& B \rightarrow b B a \mid b a .
\end{aligned}
$$
a) Give a leftmost derivation of aabbba.
b) Give a rightmost derivation of abaabbbabbaa.
c) Build the derivation tree for the derivations in parts (a) and (b).
d) Use set notation to define $L(G)$.

Trang Hoang
Trang Hoang
Numerade Educator
05:01

Problem 3

Let $\mathrm{G}$ be the grammar
$$
\begin{aligned}
& S \rightarrow S A B \mid \lambda \\
& A \rightarrow a A \mid a \\
& B \rightarrow b B \mid \lambda .
\end{aligned}
$$
a) Give a leftmost derivation of $a b b a a b$.
b) Give two leftmost derivations of $a a$.
c) Build the derivation tree for the derivations in part (b).
d) Give a regular expression for $\mathrm{L}(\mathrm{G})$.

Trang Hoang
Trang Hoang
Numerade Educator
02:56

Problem 4

Let DT be the derivation tree
(FIGURE CANT COPY).
a) Give a leftmost derivation that generates the tree DT.
b) Give a rightmost derivation that generates the tree DT.
c) How many different derivations are there that generate DT?

Chris Trentman
Chris Trentman
Numerade Educator

Problem 5

Give the leftmost and rightmost derivations corresponding to each of the derivation trees given in Figure 3.3.

Check back soon!
02:04

Problem 6

For each of the following context-free grammars, use set notation to define the language generated by the grammar.
a)
$$
\begin{aligned}
& S \rightarrow a a S B \mid \lambda \\
& B \rightarrow b B \mid b
\end{aligned}
$$
b)
$$
\begin{aligned}
& S \rightarrow a S b b \mid A \\
& A \rightarrow c A \mid c
\end{aligned}
$$
c)
$$
\begin{aligned}
& S \rightarrow a b S d c \mid A \\
& A \rightarrow c d A b a \mid \lambda
\end{aligned}
$$
d)
$$
\begin{aligned}
& S \rightarrow a S b \mid A \\
& A \rightarrow c A d \mid c B d \\
& B \rightarrow a B b \mid a b
\end{aligned}
$$
e)
$$
\begin{aligned}
& S \rightarrow a S B \mid a B \\
& B \rightarrow b b \mid b
\end{aligned}
$$

James Kiss
James Kiss
Numerade Educator

Problem 7

Construct a grammar over $\{a, b, c\}$ whose language is $\left\{a^n b^{2 n} c^m \mid n, m>0\right\}$.

Check back soon!

Problem 8

Construct a grammar over $\{a, b, c\}$ whose language is $\left\{a^n b^m c^{2 n+m} \mid n, m>0\right\}$.

Check back soon!

Problem 9

Construct a grammar over $\{a, b, c\}$ whose language is $\left\{a^n b^m c^i \mid 0 \leq n+m \leq i\right\}$.

Check back soon!

Problem 10

Construct a grammar over $\{a, b\}$ whose language is $\left\{a^m b^n \mid 0 \leq n \leq m \leq 3 n\right\}$.

Check back soon!

Problem 11

Construct a grammar over $\{a, b\}$ whose language is $\left\{a^m b^i a^n \mid i=m+n\right\}$.

Check back soon!
08:11

Problem 12

Construct a grammar over $\{a, b\}$ whose language contains precisely the strings with the same number of $a$ 's and $b$ 's.

Chris Trentman
Chris Trentman
Numerade Educator
01:24

Problem 13

Construct a grammar over $\{a, b\}$ whose language contains precisely the strings of odd length that have the same symbol in the first and middle positions.

Adriano Chikande
Adriano Chikande
Numerade Educator

Problem 14

For each of the following regular grammars, give a regular expression for the language generated by the grammar.
a)
$$
\begin{aligned}
& S \rightarrow a A \\
& A \rightarrow a A|b A| b
\end{aligned}
$$
b)
$$
\begin{aligned}
& S \rightarrow a A \\
& A \rightarrow a A \mid b B \\
& B \rightarrow b B \mid \lambda
\end{aligned}
$$
c)
$$
\begin{aligned}
& S \rightarrow a S \mid b A \\
& A \rightarrow b B \\
& B \rightarrow a B \mid \lambda
\end{aligned}
$$
d)
$$
\begin{aligned}
& S \rightarrow a S|b A| \lambda \\
& A \rightarrow a A \mid b S
\end{aligned}
$$

Check back soon!

Problem 15

For each of the languages described in Exercises 12 through 36 in Chapter 2, give a regular grammar $\mathrm{G}$ whose language is the specified set.

Check back soon!

Problem 16

For each of the languages described in Exercises 12 through 36 in Chapter 2, give a regular grammar $\mathrm{G}$ whose language is the specified set.

Check back soon!

Problem 17

For each of the languages described in Exercises 12 through 36 in Chapter 2, give a regular grammar $\mathrm{G}$ whose language is the specified set.

Check back soon!

Problem 18

For each of the languages described in Exercises 12 through 36 in Chapter 2, give a regular grammar $\mathrm{G}$ whose language is the specified set.

Check back soon!

Problem 19

For each of the languages described in Exercises 12 through 36 in Chapter 2, give a regular grammar $\mathrm{G}$ whose language is the specified set.

Check back soon!

Problem 20

For each of the languages described in Exercises 12 through 36 in Chapter 2, give a regular grammar $\mathrm{G}$ whose language is the specified set.

Check back soon!

Problem 21

For each of the languages described in Exercises 12 through 36 in Chapter 2, give a regular grammar $\mathrm{G}$ whose language is the specified set.

Check back soon!

Problem 22

For each of the languages described in Exercises 12 through 36 in Chapter 2, give a regular grammar $\mathrm{G}$ whose language is the specified set.

Check back soon!

Problem 23

For each of the languages described in Exercises 12 through 36 in Chapter 2, give a regular grammar $\mathrm{G}$ whose language is the specified set.

Check back soon!

Problem 24

For each of the languages described in Exercises 12 through 36 in Chapter 2, give a regular grammar $\mathrm{G}$ whose language is the specified set.

Check back soon!

Problem 25

For each of the languages described in Exercises 12 through 36 in Chapter 2, give a regular grammar $\mathrm{G}$ whose language is the specified set.

Check back soon!

Problem 26

For each of the languages described in Exercises 12 through 36 in Chapter 2, give a regular grammar $\mathrm{G}$ whose language is the specified set.

Check back soon!

Problem 27

For each of the languages described in Exercises 12 through 36 in Chapter 2, give a regular grammar $\mathrm{G}$ whose language is the specified set.

Check back soon!

Problem 28

For each of the languages described in Exercises 12 through 36 in Chapter 2, give a regular grammar $\mathrm{G}$ whose language is the specified set.

Check back soon!

Problem 29

For each of the languages described in Exercises 12 through 36 in Chapter 2, give a regular grammar $\mathrm{G}$ whose language is the specified set.

Check back soon!

Problem 30

For each of the languages described in Exercises 12 through 36 in Chapter 2, give a regular grammar $\mathrm{G}$ whose language is the specified set.

Check back soon!

Problem 31

For each of the languages described in Exercises 12 through 36 in Chapter 2, give a regular grammar $\mathrm{G}$ whose language is the specified set.

Check back soon!

Problem 32

For each of the languages described in Exercises 12 through 36 in Chapter 2, give a regular grammar $\mathrm{G}$ whose language is the specified set.

Check back soon!

Problem 33

For each of the languages described in Exercises 12 through 36 in Chapter 2, give a regular grammar $\mathrm{G}$ whose language is the specified set.

Check back soon!

Problem 34

For each of the languages described in Exercises 12 through 36 in Chapter 2, give a regular grammar $\mathrm{G}$ whose language is the specified set.

Check back soon!

Problem 35

For each of the languages described in Exercises 12 through 36 in Chapter 2, give a regular grammar $\mathrm{G}$ whose language is the specified set.

Check back soon!

Problem 36

For each of the languages described in Exercises 12 through 36 in Chapter 2, give a regular grammar $\mathrm{G}$ whose language is the specified set.

Check back soon!

Problem 37

For each of the languages described in Exercises 12 through 36 in Chapter 2, give a regular grammar $\mathrm{G}$ whose language is the specified set.

Check back soon!

Problem 38

For each of the languages described in Exercises 12 through 36 in Chapter 2, give a regular grammar $\mathrm{G}$ whose language is the specified set.

Check back soon!

Problem 39

For each of the languages described in Exercises 12 through 36 in Chapter 2, give a regular grammar $\mathrm{G}$ whose language is the specified set.

Check back soon!
01:09

Problem 40

The grammar in Figure 3.1 generates $\left(b^* a b^* a b^*\right)^{+}$, the set of all strings with a positive, even number of $a$ 's. Prove this.

Adriano Chikande
Adriano Chikande
Numerade Educator
16:24

Problem 41

Prove that the grammar given in Example 3.2.2 generates the prescribed language.

Chris Trentman
Chris Trentman
Numerade Educator

Problem 42

Let $\mathrm{G}$ be the grammar
$$
\begin{aligned}
& S \rightarrow a S b \mid B \\
& B \rightarrow b B \mid b .
\end{aligned}
$$
Prove that $\mathrm{L}(\mathrm{G})=\left\{a^n b^m \mid 0 \leq n<m\right\}$.

Check back soon!
03:09

Problem 43

Let $\mathrm{G}$ be the grammar
$$
\begin{aligned}
& S \rightarrow a S a a \mid B \\
& B \rightarrow b b B d d \mid C \\
& C \rightarrow b d .
\end{aligned}
$$
a) What is $\mathrm{L}(\mathrm{G})$ ?
b) Prove that $L(G)$ is the set given in part (a).

Angelo Rendina
Angelo Rendina
Numerade Educator
01:33

Problem 44

Let $\mathrm{G}$ be the grammar
$$
S \rightarrow a S b S|a S| \lambda .
$$
Prove that every prefix of a string in $\mathrm{L}(\mathrm{G})$ has at least as many $a$ 's as $b$ 's.

Adriano Chikande
Adriano Chikande
Numerade Educator
03:00

Problem 45

Use the definition of Pascal in Appendix III to construct the derivations.
Construct a derivation of the string $x 1 y$ from the variable ?variable).

Angela Guo
Angela Guo
Numerade Educator
03:00

Problem 46

Use the definition of Pascal in Appendix III to construct the derivations.
Construct a derivation of $(x 1 y)$ from $\langle$ expression .

Angela Guo
Angela Guo
Numerade Educator
03:00

Problem 47

Use the definition of Pascal in Appendix III to construct the derivations.
Construct a derivation for the expression $(x * y * 5)$ from the variable $\langle$ expression .

Angela Guo
Angela Guo
Numerade Educator
01:06

Problem 48

Use the definition of Pascal in Appendix III to construct the derivations.
For the not-faint-of-heart: Construct a derivation of $(x+y *(12+z))$ from the variable (expression $\rangle$.

Linh Vu
Linh Vu
Numerade Educator
06:46

Problem 49

Let $G_1$ and $G_2$ be the following grammars:
$$
\begin{array}{rlrl}
\mathrm{G}_1: & S \rightarrow a A B b & \mathrm{G}_2: & S \rightarrow A A B B \\
A & \rightarrow a A \mid a & A & \rightarrow A A \mid a \\
B & \rightarrow b B \mid b & B & \rightarrow B B \mid b .
\end{array}
$$
a) For each variable $X$, show that the right-hand side of every $X$ rule of $\mathrm{G}_1$ is derivable from the corresponding variable $X$ using the rules of $\mathrm{G}_2$. Use this to conclude that $\mathrm{L}\left(\mathrm{G}_1\right) \subseteq \mathrm{L}\left(\mathrm{G}_2\right)$.
b) Prove that $\mathrm{L}\left(\mathrm{G}_1\right)=\mathrm{L}\left(\mathrm{G}_2\right)$.

Chris Trentman
Chris Trentman
Numerade Educator

Problem 50

A right-linear grammar is a context-free grammar each of whose rules has one of the following forms:
i) $A \rightarrow w$
ii) $A \rightarrow w B$
iii) $A \rightarrow \lambda$,
where $w \in \Sigma^*$. Prove that a language $\mathrm{L}$ is generated by a right-linear grammar if, and only if, $\mathrm{L}$ is generated by a regular grammar.

Check back soon!

Problem 51

Try to construct a regular grammar that generates the language $\left\{a^n b^n \mid n \geq 0\right\}$. Explain why none of your attempts succeed.

Check back soon!

Problem 52

Try to construct a context-free grammar that generates the language $\left\{a^n b^n c^n \mid n \geq 0\right\}$. Explain why none of your attempts succeed.

Check back soon!