Problems
Problems
1 String Manipulations
1. The reversal of a string w over Σ, denoted by w R , is defined inductively as follows.
Prove that
(a) (w R )R = w for w ∈ Σ∗ .
(b) (w i )R = (w R )i for w ∈ Σ∗ and i ≥ 0.
(c) If v is a substring of w, then v R is a substring of w R .
2. Let Σ = {a1 , a2 , . . . a26 } = {a, b, . . . z}, the Roman alphabet. Define the binary relation
< on Σ∗ such that x < y if and only if x would precede y in a standard dictionary.
3. Give some examples of strings in, and not in, the following sets, where Σ = {a, b}
(a) baa ∈ a∗ b∗ a∗ b∗
(b) b∗ a∗ ∩ a∗ b∗ = a∗ ∪ b∗
(c) a∗ b∗ ∩ c∗ d∗ = ∅
(d) abcd ∈ (a(cd)∗ b)∗
1
6. Give a regular expression and a regular grammar for each of the following languages
over the alphabet {0, 1}: The set of all strings . . .
(i) h(φ) = 0
(ii) h(a) = 0 for each a ∈ Σ
(iii) h(α + β) = h(αβ) = the maximum of h(α) and h(β)
(iv) h(α∗ ) = h(α) + 1
In the following, find a regular expression, in each case, which represents the same
language and has star height as small as possible.
11. Let x and y be two nonempty strings. Show that the following statements are equiva-
lent:
2
(a) x and y commute, i.e. xy = yx;
(b) there exist m, n > 0 such that xm = y n ;
(c) x and y are powers of one and the same word.
2 Grammars
1. Let G1 and G2 be two regular grammars which generate L1 and L2 , respectively. Con-
struct a regular grammar which generates L1 L2 .
2. Find a regular grammar generating {w ∈ {a, b}∗ | w does not have a substring aa}.
N = {S, A}
T = {a, b}
P = {S → AA, A → AAA, A → a, A → bA, A → Ab}.
4. Construct a context-free grammar that generates all regular expressions over {a, b}.
7. Obtain a context-free grammar for each of the following languages. Here |w|c denotes
the number of c0 s in the string w.
3
8. Use closure under union to show the following are context-free languages.
(a) {am bn | m 6= n}
(b) {a, b}∗ \ {an bn | n ≥ 0}
(c) {w ∈ {a, b}∗ | w = w R }
(d) {am bn cp dq | n = q or m ≤ p or m + n = p + q}
(a) L2 is context-free.
(b) For any k ≥ 1, Lk is context-free.
(c) Lc and L∗ are context-free.
3 Finite Automata
1. Show that the set of strings over Σ = {a, b} whose lengths are divisible by 4 is regular.
2. Show that the set of all natural numbers which are multiple of 5 is regular, where
a number is represented in unary notation. For instance, 6 is denoted by IIIIII by
choosing Σ = {I}.
3. Find whether the set of all natural numbers which are multiples of 3, given in decimal
representation, is regular. Here Σ = {0, 1, 2, . . . , 9}
4. A language L is said to be definite if there is some k such that, for any string w,
whether w ∈ L depends only on the last k symbols of w. Show that the class of
definite languages are
4
(a) Model this toy by a finite automaton. Denote a marble at A by a 0-input and
a marble at B by a 1-input. A sequence of inputs is accepted if the last marble
comes out at D.
(b) Describe the set accepted by the finite automaton.
6. First note that any finite language is regular. Prove that a language L is finite if and
only if in any DFA M such that L(M) = L, there exists no loop or a cycle in any
path from initial to any final state.
5
b 1
p a r s
b
a a 0 0
0
b 1 1
q p q r
0, 1
10. Show that the set of strings over Σ = {a} whose lengths are in arithmetic progression
is regular.
More generally,
11. Let L ⊆ {a}∗ . If {|w| | w ∈ L} is union of finitely many arithmetic progressions then
show that L is regular.
12. State the converse of the problem 11, and observe whether it is true.
13. Let D = {0, 1} and let T = D × D × D. A correct addition of two numbers in binary
notation can be pictured as a string in T ∗ if we think of the symbols in T as vertical
columns. For example,
1011001
+ 0010011
−−−−−
1101100
would be pictured as the following string.
1 0 0 1 1 0 1
1 1 0 0 1 0 0
0 0 1 1 0 1 1
Show that the set of all strings in T ∗ that represent correct addition is a regular
language.
6
(d) {w w̄ | w ∈ {a, b}∗ }, where w̄ stands for w with each occurrence of a replaced by
b, and vice versa.
(e) {0m 1n 0m+n | m ≥ 1 and n ≥ 1}
16. Consider the regular grammar G = (S, N, T, P ), where
N = {S, A, B}
T = {a, b}
P = {S → abA, S → B, S → baB, S → ε, A → bS, B → aS, A → b}.
Construct a nondeterministic finite automaton M such that L(M) = L(G).
17. Show that for every regular grammar G there exists another regular grammar G 0 , with
L(G) = L(G 0 ), such that every string w ∈ L(G 0 ) has a unique derivation in G 0 .
7
8. Construct a Mealy machine which increments the given binary input by 1.
(Here you may consider the reversal of the number as input and output the reversal
of the desired number. That is, if a1 a2 . . . an is the binary number to be incremented,
then you may give an . . . a2 a1 0 as input and if b1 b2 . . . bn bn+1 = 1 + a1 a2 . . . an then the
output will be bn+1 bn . . . b2 b1 .)
10. Given two Mealy machines, say M1 and M2 , for each of the following operations
construct a Mealy machine:
11. Design a Mealy machine which can perform subtraction over two binary numbers.
12. Let M = (Q, Σ, ∆, δ, λ) be a Mealy machine, and k ∈ N, the set of natural numbers.
Two states p, q ∈ Q are said to be k-equivalent denoted by p ∼k q, if λ(p, x) = λ(q, x)
for all x ∈ Σ∗ with |x| = k.
13. Explore on flip-flops and construct a Mealy machine for each flip-flop. For example,
Set-Reset flip-flop (SR flip-flop), Trigger flip-flop, Identity-Reset flip-flop (IR flip-flop),
D flip-flop.
(a) Show that ({fx | x ∈ Σ∗ }, ) is monoid, where fx fy (q) = fy (fx (q)). This monoid
is called as monoid of automaton, denoted by MA .
(b) Define a relation on Σ∗ by x ∼ y ⇐⇒ fx = fy . Show that ∼ is a congruence
relation on Σ∗ , and then show that Σ∗ / ∼ is a monoid.
(c) Show that Σ∗ / ∼ is isomorphic to MA .
8
5 Properties and CFG
1. Eliminate useless symbols, ε-productions, and unit productions of the following gram-
mars wherever is required.
(a) S → a | aA | B | C
A → aB | ε
B → Aa
C → cCD
D → ddd
(b) S → AaB | aaB
A → ε
B → bbA | ε
2. A context-free grammar is said to be in Chomsky normal form (CNF) if all its pro-
ductions are of the form A → BC or A → a, where A, B, and C are nonterminals and
a is a terminal. Show that any context-free language without ε can be generated by a
grammar which is in CNF.
Convert the following grammars into CNF.
(a) S → aSb|ab
(b) S → aSaA|A, A → abA|b
3. A context-free grammar is said to be in Greibach normal form (GNF) if all its produc-
tions are of the form A → aα, where A is a nonterminal, a is a terminal, and α is a
(possibly empty) string of nonterminals. Show that any context-free language without
ε can be generated by a grammar which is in GNF.
Convert the following grammars into GNF.
(a) S → aSb|ab
(b) S → ab|aS|aaS
(c) S → ABb|a, A → aaA|B, B → bAb
5. The grammar
E → E + E | E ∗ E | (E) | id
generates the set of arithmetic expressions with +, ∗, parentheses, and id. Check
whether the grammar is ambiguous. If so construct an equivalent unambiguous gram-
mar.
9
6. Show that the following grammar is ambiguous. Then construct an equivalent unam-
biguous grammar.
S → AB | aaB, A → a | Aa, B → b
6 Pushdown Automata
1. Construct a push down automaton that accepts the language generated by the following
grammar.
S → aAA, A → aS | bS | a.
2. Construct a push down automaton for each of the following languages.
(a) {am bn | n ≤ m ≤ 2n}
(b) {an bm | n ≤ m ≤ 3n}
(c) {an bn+m cm | n ≥ 0, m ≥ 1}
3. Convert the push down automata of question 2 to context-free grammars.
4. Convert the context-free grammars which are obtained in question 7 to push down
automata.
10