0% found this document useful (0 votes)
13 views10 pages

Problems

The document contains exercises related to formal languages and automata theory, covering topics such as string manipulations, grammars, finite automata, and machines with output. It includes tasks for proving properties of strings, constructing regular expressions and grammars, and designing finite automata and Mealy machines. The exercises aim to deepen understanding of theoretical concepts in computer science related to formal languages and automata.

Uploaded by

jaisarush43
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views10 pages

Problems

The document contains exercises related to formal languages and automata theory, covering topics such as string manipulations, grammars, finite automata, and machines with output. It includes tasks for proving properties of strings, constructing regular expressions and grammars, and designing finite automata and Mealy machines. The exercises aim to deepen understanding of theoretical concepts in computer science related to formal languages and automata.

Uploaded by

jaisarush43
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Exercises in Formal Languages and Automata Theory

1 String Manipulations
1. The reversal of a string w over Σ, denoted by w R , is defined inductively as follows.

(i) If |w| = 0, then w R = w = ε


(ii) If |w| = n + 1 > 0, then w = ua for some a ∈ Σ, and w R = auR

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) {w : for some u ∈ ΣΣ, w = uuR u}


(b) {w : ww = www}
(c) {w : www = uu, for some u}

4. Under what circumstances is L+ = L∗ − {ε}?

5. Which of the following are true? Explain

(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 . . .

(a) ending in 00.


(b) with at least two 0’s.
(c) with exactly two 0’s.
(d) with three consecutive 0’s.
(e) with no more than three 0’s.
(f) such that the 9th symbol from the right end is 1.
(g) every 00 is followed immediately by a 1.
(h) the left most symbol differs from the right most one.

7. Construct a regular grammar which generates the language

{vwv : v, w ∈ {a, b}∗ , |v| = 2}.

8. The star height h(α) of a regular expression α is defined inductively as follows.

(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.

(a) ((abc)∗ ab)∗


(b) (a(ab∗ c)∗ )∗
(c) (c(a∗ b)∗ )∗
(d) (a∗ + b∗ + ab)∗
(e) (abb∗ a)∗

9. A regular expression is said to be in disjunctive normal form if it is of the form α1 +


α2 + . . . + αn form some n ≥ 1, where none of the αi contains an occurrence of +.
Show that every regular expression is represented by one in disjunctive normal form.

10. Prove that if u, v ∈ A∗ , |u| = |v| and uv = vu, then u = v.

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.

12. Observe that Σ∗ is a monoid with respect to concatenation. Define τ : Σ∗ −→ Σ∗ by


τ (w) = w R . Whether τ is a homomorphism?
(Let M1 and M2 be two monoids written multiplicatively with identities e1 and e2 ,
respectively. A mapping f : M1 −→ M2 is said to be a homomorphism if f (e1 ) = e2
and f (xy) = f (x)f (y) for all x, y ∈ M1 .)

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}.

3. Consider the grammar G = (S, N, T, P ), where

N = {S, A}
T = {a, b}
P = {S → AA, A → AAA, A → a, A → bA, A → Ab}.

Give at least four distinct derivations for the string babbab.

4. Construct a context-free grammar that generates all regular expressions over {a, b}.

5. Let G = (S, N, T, P ), where N = {S}, T = {a, b}, and P = {S → aSb, S → aSa, S →


bSa, S → bSb, S → ε}. Show that L(G) is regular.

6. Give context-free grammars generating the following sets.

(a) The set of all strings of balanced parentheses.


(b) The set of all strings over alphabet {a, b} with exactly twice as many a’s as b’s
(c) The set of all strings over alphabet {a, b} which are not of the form ww for some
string w.

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.

(a) {w ∈ {a, b}∗ | |w|a = |w|b}


(b) {w ∈ {a, b}∗ | 2|w|a ≤ |w|b ≤ 3|w|a}

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}

9. Let L = {an bn | n ≥ 0}. Prove that

(a) L2 is context-free.
(b) For any k ≥ 1, Lk is context-free.
(c) Lc and L∗ are context-free.

10. Show that the following languages are context-free.

(a) {uvwv R | u, v, w ∈ {a, b}+ , |u| = |w| = 2}


(b) {w1 cw2 | w1 , w2 ∈ {a, b}+ , w1 6= w2R }

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

(a) closed with respect to unions


(b) closed with respect to complementation
(c) not closed with respect to concatenation
(d) not closed with respect to kleene star

5. Consider the toy shown in the following figure. A marble is dropped in at A or B.


Levers x1 , x2 , and x3 cause the marble to fall either to left or right. Whenever a
marble encounters a lever, it causes the lever to change state, so that the next marble
encounter the lever will take the opposite branch.

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.

7. Let Q, Σ, δ be as in a DFA. For x ∈ Σ∗ define fx : Q −→ Q by fx (q) = δ(q, x) for all


q ∈ Q. Now, for x, y ∈ Σ∗ we say x ∼ y ⇐⇒ fx = fy . Observe ∼ is an equivalence
relation. Obtain the equivalence classes of the following transition diagram. Check
whether each class is regular.
a a, b
b
8. Convert the following NFA with ε-transitions to DFA and hence obtained the regular
expression accepted by the DFA.
s
1 0
ε
0 1
p q r
1
9. Find the regular expressions accepted by the following DFAs.

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.

14. A correct multiplication of two numbers can also be represented by a string in T ∗ .


Show that the set of strings in T ∗ that represents correct multiplication is not a regular
language.

15. Check whether the following languages are regular.

(a) {an | n is prime}


(b) {ww | w ∈ {a, b}∗ }
(c) {ww R | w ∈ {a, b}∗ }, where w R is reversal of w.

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 .

4 Machines with Output


1. Design a Mealy machine that prints out the 10 s complement of an input bit string, i.e.
the machine shall produce a bit string that has a 1 wherever the input string has a 0
and a 0 wherever the input has a 1.
2. Design a residue modulo 4 machine, described as follows, as a Mealy machine. Given a
natural number in decimal representation as input it has to produce a natural number
of length same as given number whose ith digit is residue of modulo 4 of the number in
the input till ith th digit. More precisely, if a1 a2 . . . an is input then the output is given
by b1 b2 . . . bn , where bi ≡ a1 a2 . . . ai mod 4, for 1 ≤ i ≤ n. For instance, if 23563045 is
input then 23303201 should be the output.
And then find its Moore version also.
3. Design a residue modulo 5 machine as described in question 2 but the input is given
in ternary format, i.e., with the symbols 0, 1, and 2.
4. Model the toy given in question 5 of tutorial sheet 3 as a Mealy machine whose output
is the sequence of C’s and D’s out of which successive marbles fall.
5. A stamp machine M has a capacity of 6 stamps. Define it as a Mealy machine such
that the state q means ‘there are q stamps in M ’. The inputs are ‘no coin is inserted’,
‘a correct coin is inserted’, ‘a wrong coin is inserted’ and the outputs are ‘no output’,
‘a stamp is output’, ‘a coin is output’.
6. Give Mealy and Moore machines for the following process with the input from (0 + 1)∗:
if the input ends in 101, output a; it is b, if the input ends in 110; otherwise output c.
7. Construct a Mealy machine which prints 11 for each occurrence of an a in the input
from {a, b, c}, otherwise a 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 .)

9. Construct a Mealy/Moore machine for each of the following Boolean operations on


bits.
(a) AND (b) OR (c) NOT (d) NAND (e) NOR

10. Given two Mealy machines, say M1 and M2 , for each of the following operations
construct a Mealy machine:

(a) Parallel composition of M1 and M2 denoted by M1 k M2 .


(M1 k M2 emits the output (y1 , y2) for the input x, where, for i = 1, 2, yi is the
output of x in the machine Mi .)
(b) Series composition of M1 and M2 denoted by M1 M2 .
(M1 M2 shall emit the output z for the input x with the property that if y is the
output of x in M1 , then z should be the output of y in M2 .)

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.

(a) Show that ∼k is an equivalence relation.


(b) p ∼k q ⇐⇒ p ∼n q for all n ≤ k.
(c) p ∼k+1 q ⇐⇒ p ∼k q, and δ(p, a) ∼k δ(q, a) for all a ∈ Σ.

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.

14. Q, Σ, δ be as in an Automaton A . For x ∈ Σ∗ define fx : Q −→ Q by fx (q) = δ(q, x)


for all q ∈ Q.

(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

4. Transform the following grammar into CNF and GNF.


S → abAB, A → bAB | ε, B → BAa | A | ε

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

7. Show that {ww R | w ∈ {a, b}∗ } is not inherently ambiguous.


8. Show that the linear languages are closed under
(a) union
(b) homomorphism
(c) intersection with regular set
9. Let h : Σ∗1 −→ Σ∗2 be a homomorphism. Show that if L ⊆ Σ∗1 is regular then so is
h(L) ⊆ Σ∗2 .
10. Quotient of languages L1 and L2 , denoted by L1 /L2 , is defined as
L1 /L2 = {x | ∃y ∈ L2 such that xy ∈ L1 }.
Show that the class of regular languages is closed under quotient with arbitrary lan-
guages.
11. Let Σ = {a, b}. Consider the homomorphism ϕ : Σ∗ −→ Z4 induced by the assignment
a 7→ 1 and b 7→ 3, where Z4 is a monoid with addition.
(a) Compute ϕ(a2 b), ϕ(b2 ab), and ϕ(bab).
(b) Give a general formula for ϕ(w) for w ∈ Σ∗ .
(c) Describe the language ϕ−1 ({0}).

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

You might also like