Download as pdf or txt
Download as pdf or txt
You are on page 1of 29

EXAMS OFFICE

hrs / /9 USE ONLY


.....................................................................................................
University of the Witwatersrand, Johannesburg

Course or topic No(s) COMS3003

Course or topic name(s)


Formal Languages and Automata
Paper Number & title

Examination to be
June 2007
held during the months(s) of

Year of Study

Degrees/Diplomas for which


BSc, BEconSc, BCom
this course is prescribed

Faculty/ies presenting Science; Commerce, Law and Management


candidates

Internal examiner(s)
and telephone extension Dr S. Ewert x76180
number(s)

External examiner(s) Prof. D. Kourie — University of Pretoria

Special materials required None

Course
Time allowance COMS3003 Hours 3
nos

This is an open book exam. 150 marks = 100%. There


Instructions to candidates
are 155 marks available.
* As Internal Examiner/Head of Department, I certify that this question paper is in final form, as
approved by the External Examiner, and is ready for reproduction.

** As Internal Examiner, I certify that this question paper is in final form and is ready for reproduc-
tion.

* applicable to formal examinations as approved by an external examiner.


** applicable to formal tests not requiring approval by external examiner.

Delete whichever not applicable.

Name: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Signature: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

(NOT FOR REPRODUCTION)


School of Computer Science
University of the Witwatersrand, Johannesburg

Exam
Formal Languages and Automata

student number:

June, 2007

Instructions
• Answer all the questions on the question paper.

• Answer all the questions in the space allocated for the answer immediately following the question.
If you run out of space, you may continue on the last page; in that case, remember to give the
number of the question.

Comments
• Throughout this paper, na (x) indicates the total number of occurrences of the symbol a in the
string x.

• Throughout this paper, xr indicates the reverse of the string x.

For examiner’s use only


Question no. Maximum marks Marks obtained
1 1
2 12
3 4
4 24
5 34
6 8
7 13
8 13
9 17
10 24
11 5

Questions: please turn over

Page 1 of 27
COMS3003 Formal Languages and Automata June 2007

Question 1 —
Consider the following statement. ‘If a student has failed course X more than once, he or she may not
register for it again’.
Student Y isn’t registered for course X. Which one of the following statements is correct? Circle the
correct answer.

1. Y has failed course X more than once.

2. Y has failed course X exactly once.

3. Y may or may not have failed course X more than once, but we cannot say for sure given the
information we have.

4. Y has never failed course X.

5. None of the above.

1 mark(s)

[Total marks for Question 1 = 1 mark(s)]

Page 2 of 27
COMS3003 Formal Languages and Automata June 2007

Question 2 —
1. In each of the following you are given two possible answers, labelled empty and non empty,
respectively. Circle either empty or non empty; moreover, if you choose the answer labelled
non empty, complete the sentence.

5 mark(s)

(a) Let L = {x ∈ {a, b}∗ | na (x) = nb (x)}.


Then L∗ \ L is . . .
i. empty: The set is empty.
ii. non empty: The set contains the string .

(b) Let L = {x ∈ {a, b} | na (x) 6= nb (x)}.
Then L∗ \ L is . . .
i. empty: The set is empty.
ii. non empty: The set contains the string .

(c) Let L = {x ∈ {a, b} | x = yy for some string y}.
Then L2 \ {an bn an bn | n ≥ 0} is . . .
i. empty: The set is empty.
ii. non empty: The set contains the string .

2. Let L1 = {ab} and L2 = {ba}.


In each of the following you are given a set and two possible answers, labelled empty and
non empty, respectively. Circle either empty or non empty; moreover, if you choose the an-
swer labelled non empty, complete the sentence.

7 mark(s)

(a) (L1 ∪ L2 )+ \ L+ +

1 ∪ L2
i. empty: The set is empty.
ii. non empty: The set contains the string .
+
(b) L+ +

1 ∪ L2 \ (L1 ∪ L2 )
i. empty: The set is empty.
ii. non empty: The set contains the string .
+
(c) L+ +
2 L1 \ (L2 L1 )
i. empty: The set is empty.
ii. non empty: The set contains the string .
+
(d) (L2 L1 ) \ L+ +
2 L1
i. empty: The set is empty.
ii. non empty: The set contains the string .

[Total marks for Question 2 = 12 mark(s)]

Page 3 of 27
COMS3003 Formal Languages and Automata June 2007

Question 3 —
Consider the following regular expressions. In each case, give a string of length 4 in {0, 1}∗ that is not
in the language corresponding to the given regular expression.

1. (0 + 10)∗ 1∗

2 mark(s)

2. (λ + 0 + 00) (1 + 10 + 100)∗

2 mark(s)

[Total marks for Question 3 = 4 mark(s)]

Page 4 of 27
COMS3003 Formal Languages and Automata June 2007

Question 4 —
1. Let L ⊆ Σ∗ be any language. Let L1 be the set of prefixes of elements of L. We want to investigate
the relationship between the two partitions of Σ∗ corresponding to the equivalence relations IL and
IL1 , respectively.
Complete the following proof.

8 mark(s)

Suppose x and y are distinguishable w.r.t. L1 . Specifically, suppose that for some z,

xz ∈ L1 and yz 6∈ L1 .

Then, for some w, (circle the correct answer)

(a) xzw ∈ L and yzw ∈ L.


(b) xzw ∈ L and yzw 6∈ L.
(c) xzw 6∈ L and yzw ∈ L.
(d) xzw 6∈ L and yzw 6∈ L.

Therefore, if x and y are in different equivalence classes with respect to IL1 , they are in (circle the
correct answer)

(a) the same


(b) overlapping
(c) different
(d) none of the above

equivalence classes with respect to IL .


This means that (circle the correct answer)

(a) the partition determined by IL1 is at least as fine as that determined by IL .


(b) we cannot say anything about the relationship between the two partitions determined by IL
and IL1 .

Therefore,

(a) any equivalence class w.r.t. IL is the union of equivalence classes w.r.t. IL1 .
(b) any equivalence class w.r.t. IL1 is the union of equivalence classes w.r.t. IL .
(c) we cannot say anything about the relationship between the equivalence classes w.r.t. IL and
IL1 .

Page 5 of 27
COMS3003 Formal Languages and Automata June 2007

2. For a certain language L ⊆ {a, b}∗ , IL has exactly four equivalence classes. They are [λ], [a], [ab],
and [b]. It is also true that

(a) the three strings a, aa, and abb are all equivalent, and
(b) the two strings b and aba are equivalent.

Finally,

(a) ab ∈ L,
(b) λ 6∈ L,
(c) a 6∈ L,
(d) b is not even a prefix of any element of L.

Draw an FA with exactly four states that accepts L.

6 mark(s)

Page 6 of 27
COMS3003 Formal Languages and Automata June 2007

3. In each of the following


 i cases, we can prove that L is not regular by showing that any two elements
of the infinite set 0 | i ≥ 0 are distinguishable with respect to L.
Assume in each part 0 ≤ i < j, and find a string that distinguishes 0i and 0j .

(a) L = {0m 1n 0p | p ≥ m + n}

For z = ,
0i z L
and 0j z L.

3 mark(s)

(b) L = {0m 1n 0p | p < m + n}

For z = ,
0i z L
and 0j z L.

3 mark(s)

4. In each of the following cases, give two strings that are indistinguishable with respect to L; no
justification is necessary. If no such strings exist, state it clearly.

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

2 mark(s)

(b) L = {xcxr | x ∈ {a, b}∗ }

2 mark(s)

[Total marks for Question 4 = 24 mark(s)]

Page 7 of 27
COMS3003 Formal Languages and Automata June 2007

Question 5 —
1. Let
M1 = ({A, B, C, D} , {0, 1} , A, {C, D} , δ1 ) ,
and
M2 = ({P, Q} , {0, 1} , P, {Q} , δ2 ) ,
be Finite Automata (FA) recognizing languages L1 and L2 , respectively, where

• δ1 is given in the following diagram:

• and δ2 is given in the following diagram:

Please turn over.

Page 8 of 27
COMS3003 Formal Languages and Automata June 2007

(a) Using M1 and M2 as appropriate, construct the FA

M3 = (Q3 , {0, 1} , q3 , A3 , δ3 )

that accepts
L3 = {x ∈ {0, 1}∗ | x ends in 00} .
In the following, use the notation AQ for the states in Q3 .
i. Give the start state of M3 .

1 mark(s)
ii. Give A3 .

2 mark(s)
iii. Complete the following transition table for δ3 . You may leave out the states that are not
reachable, if such states exist.
4 mark(s)
q δ3 (q, 0) δ3 (q, 1)

Page 9 of 27
COMS3003 Formal Languages and Automata June 2007

iv. Give the states in M3 that are not reachable. If no such state exists, state it clearly.

2 mark(s)
v. Consider the following list:
A. L1 ∩ L2
B. L′1 ∩ L2
C. L1 ∩ L′2
D. L′1 ∩ L′2
E. L1 ∪ L2
F. L′1 ∪ L2
G. L1 ∪ L′2
H. L′1 ∪ L′2
Complete the following statement by giving the label of the correct expression in the list.

L3 =

1 mark(s)

Page 10 of 27
COMS3003 Formal Languages and Automata June 2007

2. Consider the FA
M = ({1, 2, 3, 4, 5, 6, 7} , {a, b} , 1, {4, 6, 7} , δ) ,
where δ is given in the following diagram:

As part of the process of finding a minimum-state FA that accepts the same language as M , com-
plete rows 5 and 7 in the following diagram to indicate the situation after the first pass.

5 mark(s)

2
3
4
5
6
7
q 1 2 3 4 5 6

Page 11 of 27
COMS3003 Formal Languages and Automata June 2007

3. Consider the FA
M = ({1, 2, 3, 4, 5, 6} , {a, b} , 1, {1, 2, 7} , δ) ,
where δ is given in the following diagram:

In the process of finding a minimum-state FA that accepts the same language as M , we obtain the
following diagram after completion of a pass.

2
3 X X
4 X X
5 X X
6 X X
7 X X X X
q 1 2 3 4 5 6

Here X indicates that the pair is marked.


Consider the next pass.

(a) Give one pair that gets marked in this pass.

2 mark(s)
(b) Give one pair that doesn’t get marked in this pass.

2 mark(s)

Page 12 of 27
COMS3003 Formal Languages and Automata June 2007

4. Consider the FA
M = ({1, 2, 3, 4, 5, 6, 7} , {a, b} , 1, {2, 6} , δ) ,
where δ is given in the following diagram:

After the final pass, the diagram looks as follows.

2 X
3 X
4 X X X
5 X X
6 X X X X
7 X X X
q 1 2 3 4 5 6

Here X indicates that the pair is marked. Give the minimum-state FA Mmin for M as a diagram.
In each state of Mmin , indicate clearly which states of M it represents.

8 mark(s)

Page 13 of 27
COMS3003 Formal Languages and Automata June 2007

5. Describe a decision algorithm to answer the following question:


Given an FA M = (Q, Σ, q0 , A, δ) accepting a language L, and a string x, is x a prefix of an
element of L?

7 mark(s)

[Total marks for Question 5 = 34 mark(s)]

Page 14 of 27
COMS3003 Formal Languages and Automata June 2007

Question 6 —
1. Consider the language

L = {x ∈ {a, b}∗ | |x| is odd and the middle symbol of x is b}

We want to prove that L is not regular.


Suppose L is regular. According to the pumping lemma for regular languages, there exists an n
such that for every x ∈ L with |x| ≥ n, x can be written uvw for some u, v and w with |uv| ≤ n,
|v| > 0, and for any m ≥ 0, uv m w ∈ L.
Let n be the integer in the statement of the pumping lemma.
Choose x such that |x| ≥ n.
Complete the proof.

8 mark(s)

x= .
Then uv = .
This implies that w = .
Moreover, v = .
According to the lemma, uv m w ∈ L for every m ≥ 0.
Consider

uv m w = . . .
= ...

For m = , uv m w 6∈ L,
since .
This contradicts the lemma, therefore our assumption that L is regular is wrong.

[Total marks for Question 6 = 8 mark(s)]

Page 15 of 27
COMS3003 Formal Languages and Automata June 2007

Question 7 —
Consider

1. Lab = ai bi | i > 0


2. La = ai | i > 0


3. Lb = bi | i > 0


These languages are generated by the grammars

1. Gab = (Vab , Σ, Sab , Pab ), where Vab = {Sab } and Pab = {Sab → aSab b | ab},

2. Ga = (Va , Σ, Sa , Pa ), where Va = {Sa } and Pa = {Sa → aSa | a},

3. Gb = (Vb , Σ, Sb , Pb ), where Vb = {Sb } and Pb = {Sb → bSb | b},

respectively, where Σ = {a, b}.


Construct a context-free grammar G for generating

L = ai bj | i, j ≥ 1, i 6= j ,


using the given languages and grammars only.

13 mark(s)

Page 16 of 27
COMS3003 Formal Languages and Automata June 2007

[Total marks for Question 7 = 13 mark(s)]

Page 17 of 27
COMS3003 Formal Languages and Automata June 2007

Question 8 —
1. Consider the context-free grammar G = ({S, A, B} , {a, b, c} , S, P ), where P is the set of pro-
ductions

S → AB
A → aAb |
λ
B → bBc |
λ

Find the nullable variables, using the appropriate algorithm. Show all steps.

4 mark(s)

2. Consider the context-free grammar G = ({S, T } , {a, b} , S, P ), where P is the set of productions

S → aSc |
T
T → aT b |
λ

The nullable variables are S and T .


Construct a context-free grammar without λ-productions G1 = ({S, T } , {a, b} , S, P1 ), that gen-
erates L (G) \ {λ}.

4 mark(s)

Page 18 of 27
COMS3003 Formal Languages and Automata June 2007

3. Consider the context-free grammar G = ({S} , {(, )} , S, P ), where P is the set of productions

S → S (S) |
(S) |
S ()

Construct a context-free grammar in Chomsky normal form

G1 = (V, {(, )} , S, P1 )

that generates L (G).

5 mark(s)

[Total marks for Question 8 = 13 mark(s)]

Page 19 of 27
COMS3003 Formal Languages and Automata June 2007

Question 9 —
1. Consider the language
n o
L = ai bj ck | i, j, k ≥ 0, j = i or j = k ,

We want to construct a Pushdown Automaton (PDA)

M = ({q0 , q1 , q2 , q3 , q4 , q5 , q6 , qf } , {a, b, c} , {a, b, Z0 } , q0 , Z0 , {qf } , δ)

that accepts L.
We proceed as follows. The PDA makes an initial choice as to whether it will compare the number
of b’s to the number of a’s or to the number of c’s; q1 and q4 are the states that begin these respective
processes. In q1 , one possibility is that there are no a’s or b’s at all. Otherwise, the PDA saves a’s
on the stack, and when it sees a b, it pops a’s off the stack to match inputs of b. In either case, when
the stack is empty except for Z0 , the PDA goes to q3 , from which it can move to the accepting state
any time. The only other move from q3 is to read a c, leaving the stack alone.
Starting in q4 the moves are similar. The PDA moves to q5 at any time, and the only move it can
make before that is to read an a, leaving the stack alone. From q5 , the PDA pushes b’s onto the
stack, and when it sees a c it starts to remove b’s from the stack to match the c’s in the input. It
enters the accepting state whenever the stack is empty except for Z0 .
Complete the following transition table.

17 mark(s)

Page 20 of 27
COMS3003 Formal Languages and Automata June 2007

Move State Input Stack Move(s)


No. symbol

1 q0 λ Z0

2 q1 λ Z0

3 q1 a Z0

4 q1 a a

5 q1 b a

6 q2 b a

7 q2 λ Z0

8 q3 c Z0

9 q3 λ Z0

10 q4 λ Z0

11 q4 a Z0

12 q4 b Z0

13 q5 b b

14 q5 c b

15 q6 c b

16 q6 λ Z0

[Total marks for Question 9 = 17 mark(s)]

Page 21 of 27
COMS3003 Formal Languages and Automata June 2007

Question 10 —
1. The Pumping Lemma for Context-free Languages (CFLs) states:
Let L be a CFL. Then there is an integer n such that for any

u∈L

with
|u| ≥ n ,
there are strings v, w, x, y, and z satisfying

u = vwxyz

|wy| > 0
|wxy| ≤ n
for any m ≥ 0, vwm xy m z ∈ L

Using the lemma, derive the following statement:


Suppose L is an infinite context-free language. There are integers p and q, with q > 0, such that
for every m ≥ 0, L contains a string of length p + mq.

5 mark(s)

Page 22 of 27
COMS3003 Formal Languages and Automata June 2007

2. In each of the following you are given a language L. You want to use the pumping lemma for
context-free languages given above to show that L is not context-free. Give the word u ∈ L that
you would use to start your proof.

(a) L = ai bi cj | i, j ≥ 0, j ≤ i


2 mark(s)

(b) L = ai b i ∪ ai b 2i ∪ ai bi ci
  

2 mark(s)

Page 23 of 27
COMS3003 Formal Languages and Automata June 2007

3. Consider n o
L = ai bj ck | i, j, k ≥ 0, i < j, i ≤ k ≤ j

We want to prove that L is not context-free.


Complete the proof.

15 mark(s)

Suppose L is context-free. According to the pumping lemma, there is an integer n such that for
any u ∈ L with |u| ≥ n, there are strings v, w, x, y, and z satisfying u = vwxyz, |wy| > 0,
|wxy| ≤ n, and for any m ≥ 0, vwm xy m z ∈ L.
Let n be the integer in the statement of the pumping lemma.
Choose u ∈ L such that |u| ≥ n; we choose

u = an bn+1 cn .

Suppose v, w, x, y, and z are any strings satisfying the conditions above.


If w and y contain

(a) a’s only, then vwm xy m z 6∈ L for


m= ,
since

(b) a’s and b’s only, then vwm xy m z 6∈ L for


m= ,
since

(c) b’s only, then vwm xy m z 6∈ L for


m= ,
since

(d) b’s and c’s only, then vwm xy m z 6∈ L for


m= ,
since

Page 24 of 27
COMS3003 Formal Languages and Automata June 2007

(e) c’s only, then vwm xy m z 6∈ L for


m= ,
since

[Total marks for Question 10 = 24 mark(s)]

Page 25 of 27
COMS3003 Formal Languages and Automata June 2007

Question 11 —
Consider the following theorem:
Let L1 be a context-free language and L2 be a regular language. Then L1 ∩ L2 is context-free.
Use the theorem to prove that the language

L = {an bn | n ≥ 0, n is not a multiple of 5}


is context-free.

5 mark(s)

[Total marks for Question 11 = 5 mark(s)]

Page 26 of 27
COMS3003 Formal Languages and Automata June 2007

End of question paper

Page 27 of 27

You might also like