Our Discord hit 10K members! 🎉 Meet students and ask top educators your questions.Join Here!

## Educators

### Problem 1

Exercises $1-3$ refer to the grammar with start symbol sentence, set of terminals $T=\{\text {the}, \text { sleepy, happy, tortoise, hare, }$ passes, runs, quickly, slowly $\}$ , set of nonterminals $N=\{\text { noun }$ phrase, transitive verb phrase, intransitive verb phrase, $\mathrm{~ a r t i c l e , ~ a d j e c t i v e , ~ n o u n , ~ v e r b , ~ a d v e r b \} , ~ a n d ~ p r o d u c t i o n s : ~}$
$$\begin{array}{l}{\text { sentence } \rightarrow \text { noun phrase transitive verb phrase }} \\ {\text { noun phrase }} \\ {\text { sentence } \rightarrow \text { noun phrase intransitive verb phrase }} \\ {\text { noun phrase } \rightarrow \text { article adjective noun }} \\ {\text { noun phrase } \rightarrow \text { article noun }}\end{array}$$
$$\begin{array}{l}{\text { transitive verb phrase } \rightarrow \text { transitive verb }} \\ {\text { intransitive verb phrase } \rightarrow \text { intransitive verb adverb }} \\ {\text { intransitive verb phrase } \rightarrow \text { intransitive verb }} \\ {\text { article } \rightarrow \text { the }}\end{array}$$
$$\begin{array}{l}{\text { adjective } \rightarrow \text { sleepy }} \\ {\text { adjective } \rightarrow \text { happy }} \\ {\text { noun } \rightarrow \text { tortoise }} \\ {\text { noun } \rightarrow \text { hare }} \\ {\text { transitive verb } \rightarrow \text { passes }} \\ {\text { intransitive verb } \rightarrow \text { runs }} \\ {\text { adverb } \rightarrow \text { quickly }} \\ {\text { adverb } \rightarrow \text { slowly }}\end{array}$$
Use the set of productions to show that each of these sentences is a valid sentence.
a) the happy hare runs
b) the sleepy tortoise runs quickly
c) the tortoise passes the hare
d) the sleepy hare passes the happy tortoise

Trang H.

### Problem 2

Exercises $1-3$ refer to the grammar with start symbol sentence, set of terminals $T=\{\text {the}, \text { sleepy, happy, tortoise, hare, }$ passes, runs, quickly, slowly $\}$ , set of nonterminals $N=\{\text { noun }$ phrase, transitive verb phrase, intransitive verb phrase, $\mathrm{~ a r t i c l e , ~ a d j e c t i v e , ~ n o u n , ~ v e r b , ~ a d v e r b \} , ~ a n d ~ p r o d u c t i o n s : ~}$
$$\begin{array}{l}{\text { sentence } \rightarrow \text { noun phrase transitive verb phrase }} \\ {\text { noun phrase }} \\ {\text { sentence } \rightarrow \text { noun phrase intransitive verb phrase }} \\ {\text { noun phrase } \rightarrow \text { article adjective noun }} \\ {\text { noun phrase } \rightarrow \text { article noun }}\end{array}$$
$$\begin{array}{l}{\text { transitive verb phrase } \rightarrow \text { transitive verb }} \\ {\text { intransitive verb phrase } \rightarrow \text { intransitive verb adverb }} \\ {\text { intransitive verb phrase } \rightarrow \text { intransitive verb }} \\ {\text { article } \rightarrow \text { the }}\end{array}$$
$$\begin{array}{l}{\text { adjective } \rightarrow \text { sleepy }} \\ {\text { adjective } \rightarrow \text { happy }} \\ {\text { noun } \rightarrow \text { tortoise }} \\ {\text { noun } \rightarrow \text { hare }} \\ {\text { transitive verb } \rightarrow \text { passes }} \\ {\text { intransitive verb } \rightarrow \text { runs }} \\ {\text { adverb } \rightarrow \text { quickly }} \\ {\text { adverb } \rightarrow \text { slowly }}\end{array}$$
Find five other valid sentences, besides those given in Exercise $1 .$

Trang H.

### Problem 3

Exercises $1-3$ refer to the grammar with start symbol sentence, set of terminals $T=\{\text {the}, \text { sleepy, happy, tortoise, hare, }$ passes, runs, quickly, slowly $\}$ , set of nonterminals $N=\{\text { noun }$ phrase, transitive verb phrase, intransitive verb phrase, $\mathrm{~ a r t i c l e , ~ a d j e c t i v e , ~ n o u n , ~ v e r b , ~ a d v e r b \} , ~ a n d ~ p r o d u c t i o n s : ~}$
$$\begin{array}{l}{\text { sentence } \rightarrow \text { noun phrase transitive verb phrase }} \\ {\text { noun phrase }} \\ {\text { sentence } \rightarrow \text { noun phrase intransitive verb phrase }} \\ {\text { noun phrase } \rightarrow \text { article adjective noun }} \\ {\text { noun phrase } \rightarrow \text { article noun }}\end{array}$$
$$\begin{array}{l}{\text { transitive verb phrase } \rightarrow \text { transitive verb }} \\ {\text { intransitive verb phrase } \rightarrow \text { intransitive verb adverb }} \\ {\text { intransitive verb phrase } \rightarrow \text { intransitive verb }} \\ {\text { article } \rightarrow \text { the }}\end{array}$$
$$\begin{array}{l}{\text { adjective } \rightarrow \text { sleepy }} \\ {\text { adjective } \rightarrow \text { happy }} \\ {\text { noun } \rightarrow \text { tortoise }} \\ {\text { noun } \rightarrow \text { hare }} \\ {\text { transitive verb } \rightarrow \text { passes }} \\ {\text { intransitive verb } \rightarrow \text { runs }} \\ {\text { adverb } \rightarrow \text { quickly }} \\ {\text { adverb } \rightarrow \text { slowly }}\end{array}$$
Show that the hare runs the sleepy tortoise is not a valid sentence.

Chris T.

### Problem 4

Let $G=(V, T, S, P)$ be the phrase-structure grammar with $V=\{0,1, A, S\}, T=\{0,1\},$ and set of productions $P$ consisting of $S \rightarrow 1 S, \quad S \rightarrow 00 A, \quad A \rightarrow 0 A$ and $A \rightarrow 0$
a) Show that 111000 belongs to the language generated by $G .$
b) Show that 11001 does not belong to the language generated by $G .$
c) What is the language generated by $G ?$

Chris T.

### Problem 5

Let $G=(V, T, S, P)$ be the phrase-structure grammar with $V=\{0,1, A, B, S\}, T=\{0,1\},$ and set of productions $P$ consisting of $S \rightarrow 0 A, S \rightarrow 1 A, A \rightarrow 0 B, B \rightarrow 1 A,$ $B \rightarrow 1$.
a) Show that 10101 belongs to the language generated by $G$ .
b) Show that 10110 does not belong to the language generated by $G .$
c) What is the language generated by $G ?$

Chris T.

### Problem 6

Let $V=\{S, A, B, a, b\}$ and $T=\{a, b\} .$ Find the language generated by the grammar $(V, T, S, P)$ when the set $P$ of productions consists of
$$\begin{array}{l}{\text { a) } S \rightarrow A B, A \rightarrow a b, B \rightarrow b b} \\ {\text { b) } S \rightarrow A B, S \rightarrow a A, A \rightarrow a, B \rightarrow b a} \\ {\text { c) } S \rightarrow A B, S \rightarrow A A, A \rightarrow a B, A \rightarrow a b, B \rightarrow b} \\ {\text { d) } S \rightarrow A A, S \rightarrow B, A \rightarrow a a A, A \rightarrow a a, B \rightarrow b B, B \rightarrow b} \\ {\text { e) } S \rightarrow A B, A \rightarrow a A b, B \rightarrow b B a, A \rightarrow \lambda, B \rightarrow \lambda}\end{array}$$

Chris T.

### Problem 7

Construct a derivation of $0^{3} 1^{3}$ using the grammar given in Example $5 .$

Trang H.

### Problem 8

Show that the grammar given in Example 5 generates the set $\left\{0^{n} 1^{n} | n=0,1,2, \ldots\right\}$

Chris T.

### Problem 9

a) Construct a derivation of $0^{2} 1^{4}$ using the grammar $G_{1}$ in Example $6 .$
b) Construct a derivation of $0^{2} 1^{4}$ using the grammar $G_{2}$ in Example $6 .$

Trang H.

### Problem 10

a) Show that the grammar $G_{1}$ given in Example 6 generates the set $\left\{0^{m} 1^{n} | m, n=0,1,2, \ldots\right\} .$
b) Show that the grammar $G_{2}$ in Example 6 generates the same set.

Chris T.

### Problem 11

Construct a derivation of $0^{2} 1^{2} 2^{2}$ in the grammar given in Example $7 .$

Trang H.

### Problem 12

Show that the grammar given in Example 7 generates the set $\left\{0^{n} 1^{n} 2^{n} | n=0,1,2, \ldots\right\}$

Chris T.

### Problem 13

Find a phrase-structure grammar for each of these languages.
a) the set consisting of the bit strings $0,1,$ and 11
b) the set of bit strings containing only 1 $\mathrm{s}$
c) the set of bit strings that start with 0 and end with 1
d) the set of bit strings that consist of a 0 followed by an even number of 1 $\mathrm{s}$

Chris T.

### Problem 14

Find a phrase-structure grammar for each of these languages.
a) the set consisting of the bit strings $10,01,$ and 101
b) the set of bit strings that start with 00 and end with one or more 1 $\mathrm{s}$
c) the set of bit strings consisting of an even number of 1 s followed by a final 0
d) the set of bit strings that have neither two consecutive 0 s nor two consecutive 1 $\mathrm{s}$

Heather E.

### Problem 15

Find a phrase-structure grammar for each of these languages.
a) the set of all bit strings containing an even number of 0 s and no 1 $\mathrm{s}$
b) the set of all bit strings made up of a 1 followed by an odd number of 0s
c) the set of all bit strings containing an even number of 0s and an even number of 1s
d) the set of all strings containing 10 or more 0s and no 1s
e) the set of all strings containing more 0s than 1s
f ) the set of all strings containing an equal number of 0s and 1s
g) the set of all strings containing an unequal number of 0s and 1s

Check back soon!

### Problem 16

Construct phrase-structure grammars to generate each of these sets.
$\begin{array}{ll}{\text { a) }\left\{1^{n} | n \geq 0\right\}} & {\text { b) }\left\{10^{n} | n \geq 0\right\}} \\ {\text { c) }\left\{(11)^{n} | n \geq 0\right\}}\end{array}$

Chris T.

### Problem 17

Construct phrase-structure grammars to generate each of these sets.
$$\begin{array}{ll}{\text { a) }\left\{0^{n} | n \geq 0\right\}} & {\text { b) }\left\{1^{n} 0 | n \geq 0\right\}} \\ {\text { c) }\left\{(000)^{n} | n \geq 0\right\}}\end{array}$$

Chris T.

### Problem 18

Construct phrase-structure grammars to generate each of these sets.
a) $\left\{01^{2 n} | n \geq 0\right\}$
b) $\left\{0^{n} 1^{2 n} | n \geq 0\right\}$
c) $\left\{0^{n} 1^{m} 0^{n} | m \geq 0 \text { and } n \geq 0\right\}$

Chris T.

### Problem 19

Let $V=\{S, A, B, a, b\}$ and $T=\{a, b\} .$ Determine whether $G=(V, T, S, P)$ is a type 0 grammar but not a type 1 grammar, a type 1 grammar but not a type 2 grammar, or a type 2 grammar but not a type 3 grammar if $P,$ the set of productions, is
a) $S \rightarrow a A B, A \rightarrow B b, B \rightarrow \lambda$
b) $S \rightarrow a A, A \rightarrow a, A \rightarrow b$
c) $S \rightarrow A B a, A B \rightarrow a$
d) $S \rightarrow A B A, A \rightarrow a B, B \rightarrow a b$
e) $S \rightarrow b A, A \rightarrow B, B \rightarrow a$
f) $S \rightarrow a A, a A \rightarrow B, B \rightarrow a A, A \rightarrow b$
g) $S \rightarrow b A, A \rightarrow b, S \rightarrow \lambda$
h) $S \rightarrow A B, B \rightarrow a A b, a A b \rightarrow b$
i) $S \rightarrow a A, A \rightarrow b B, B \rightarrow b, B \rightarrow \lambda$
j) $S \rightarrow A, A \rightarrow B, B \rightarrow \lambda$

Chris T.

### Problem 20

A palindrome is a string that reads the same backward as it does forward, that is, a string $w,$ where $w=w^{R}$ , where $w^{R}$ is the reversal of the string $w$ . Find a context-free grammar that generates the set of all palindromes over the alphabet $\{0,1\}$ .

Chris T.

### Problem 21

Let $G_{1}$ and $G_{2}$ be context-free grammars, generating the languages $L\left(G_{1}\right)$ and $L\left(G_{2}\right),$ respectively. Show that there is a context-free grammar generating each of these sets.
$$\begin{array}{ll}{\text { a) } L\left(G_{1}\right) \cup L\left(G_{2}\right)} & {\text { b) } L\left(G_{1}\right) L\left(G_{2}\right)} \\ {\text { c) } L\left(G_{1}\right)^{*}}\end{array}$$

Chris T.

### Problem 22

Find the strings constructed using the derivation trees shown here.
Graph cannot copy

Chris T.

### Problem 23

Construct derivation trees for the sentences in Exercise $1 .$

Trang H.

### Problem 24

Let $G$ be the grammar with $V=\{a, b, c, S\} ; T=$ $\{a, b, c\} ;$ starting symbol $S ;$ and productions $S \rightarrow$ $a b S, S \rightarrow b c S, S \rightarrow b b S, S \rightarrow a,$ and $S \rightarrow c b .$ Construct derivation trees for
$$\begin{array}{l}{\text { a) bcbba. } \quad \text { b) bbbcbba. }} \\ {\text { c) } b c a b b b b b c b \text { . }}\end{array}$$

Trang H.

### Problem 25

Use top-down parsing to determine whether each of the following strings belongs to the language generated by the grammar in Example $12 .$
$$\begin{array}{ll}{\text { a) } b a b a} & {\text { b) } a b a b} \\ {\text { c) } c b a b a} & {\text { d) } b b b c b a}\end{array}$$

Trang H.

### Problem 26

Use bottom-up parsing to determine whether the strings in Exercise 25 belong to the language generated by the grammar in Example $12 .$

Trang H.

### Problem 27

Construct a derivation tree for $-109$ using the grammar given in Example $15 .$

Chris T.

### Problem 28

a) Explain what the productions are in a grammar if the Backus-Naur form for productions is as follows:
\begin{aligned}\langle\text {expression}\rangle :=&(\langle\text {expression}\rangle) | \\ &\langle\text {expression}\rangle+\langle\text {expression}\rangle | \\ &\langle\text {variable}\rangle *\langle\text {expression}\rangle | \\\langle\text {variable}\rangle & := x | y \end{aligned}
b) Find a derivation tree for $(x * y)+x$ in this grammar.

Check back soon!

### Problem 29

a) Construct a phrase-structure grammar that generates all signed decimal numbers, consisting of a sign, either $+$ or $-;$ a nonnegative integer; and a decimal fraction that is either the empty string or a decimal point followed by a positive integer, where initial zeros in an integer are allowed.
b) Give the Backus-Naur form of this grammar.
c) Construct a derivation tree for $-31.4$ in this grammar.

Chris T.

### Problem 30

a) Construct a phrase-structure grammar for the set of all fractions of the form $a / b,$ where $a$ is a signed integer in decimal notation and $b$ is a positive integer.
b) What is the Backus-Naur form for this grammar?
c) Construct a derivation tree for $+311 / 17$ in this grammar.

Chris T.

### Problem 31

Give production rules in Backus-Naur form for an identifier if it can consist of
a) one or more lowercase letters.
b) at least three but no more than six lowercase letters.
c) one to six uppercase or lowercase letters beginning with an uppercase letter.
d) a lowercase letter, followed by a digit or an underscore, followed by three or four alphanumeric characters (lower or uppercase letters and digits).

Chris T.

### Problem 32

Give production rules in Backus-Naur form for the name of a person if this name consists of a first name, which is a string of letters, where only the first letter is uppercase; a middle initial; and a last name, which can be any string of letters.

Chris T.

### Problem 33

Give production rules in Backus-Naur form that generate all identifiers in the C programming language. In $\mathrm{C}$ an identifier starts with a letter or an underscore $(-)$ that is followed by one or more lowercase letters, uppercase letters, underscores, and digits.

Chris T.

### Problem 34

Describe the set of strings defined by each of these sets of productions in EBNF.
$$\begin{array}{ll}{\text { a) }} & {\text { string } : :=L+D ? L+} \\ {} & {L :=a|b| c} \\ {} & {D : :=0 | 1}\end{array}$$
$$\begin{array}{l}{\text { b) string } : :=\operatorname{sign} D+| D+} \\ {\quad \operatorname{sign} : :=+|-}\end{array}$$
$$D : :=0|1| 2|3| 4|5| 6|7| 8 | 9$$
$$\begin{array}{ll}{\text { c) }} & {\text { string } \therefore=L *(D+) ? L *} \\ {} & {L : :=x | y} \\ {} & {D : :=0 | 1}\end{array}$$

Chris T.

### Problem 35

Give production rules in extended Backus-Naur form that generate all decimal numerals consisting of an optional sign, a nonnegative integer, and a decimal fraction that is either the empty string or a decimal point followed by an optional positive integer optionally preceded by some number of zeros.

Chris T.

### Problem 36

Give production rules in extended Backus-Naur form that generate a sandwich if a sandwich consists of a lower slice of bread; mustard or mayonnaise; optional lettuce; an optional slice of tomato; one or more slices of either turkey, chicken, or roast beef (in any combination); optionally some number of slices of cheese; and a top slice of bread.

Chris T.

### Problem 37

Give production rules in extended Backus-Naur form for identifiers in the C programming language (see Exercise 33 ).

Chris T.

### Problem 38

Describe how productions for a grammar in extended Backus-Naur form can be translated into a set of productions for the grammar in Backus-Naur form.

Chris T.

### Problem 39

For each of these strings, determine whether it is generated by the grammar given for postfix notation. If it is, find the steps used to generate the string
$$\begin{array}{ll}{\text { a) } a b c *+} & {\text { b) } x y++} \\ {\text { c) } x y-z *} & {\text { d) } w x y z-* /} \\ {\text { e) } a d e-*}\end{array}$$

Chris T.

### Problem 40

Use Backus-Naur form to describe the syntax of expressions in infix notation, where the set of operators
and identifiers is the same as in the BNF for postfix expressions given in the preamble to Exercise $39,$ but parentheses must surround expressions being used as factors.

Chris T.

### Problem 41

For each of these strings, determine whether it is generated by the grammar for infix expressions from Exercise $40 .$ If it is, find the steps used to generate the string.
$$\begin{array}{ll}{\text { a) } x+y+z} & {\text { b) } a / b+c / d} \\ {\text { c) } m *(n+p)} & {\text { d) }+m-n+p-q} \\ {\text { e) }(m+n) *(p-q)} & {}\end{array}$$

Chris T.
Let $G$ be a grammar and let $R$ be the relation containing the ordered pair $\left(w_{0}, w_{1}\right)$ if and only if $w_{1}$ is directly derivable from $w_{0}$ in $G .$ What is the reflexive transitive closure of $R ?$