CS3012coursework08soln

advertisement
UNIVERSITY OF ABERDEEN
SESSION 2008-2009
Coursework in CS3012 (Formal Languages)
October, 2008
Answer ALL questions.
Each question is worth 25 marks; the marks for each part of a question are shown in brackets.
Submit one paper copy. Please don't submit plastic folders or other fancy stuff. Just submit some papers stapled together.
1. (a)
The following is Finite State Automaton A:
1
1
0
1
2
0
3
0
(i) What are the first eight strings of L(A) if going by lexical order?
0,010,0010,01010,001010,010010,0010010,0101010
(2)
(ii) What are the first four strings of L(A) if going by dictionary order?
0,0010,0010010,0010010010
(2)
(iii) Convert the finite state automaton A to a regular expression.
(You can do it by the algorithm or by inspection)
CS3012 (Formal Languages)
i
01

remove b
a
1
0
0
1
i

b
0

f
0
c
f
a
0
1
00
c
simplify
01
i

remove c
01
0
f
a
00+0
i

0
a
f
1
(00+0)1
c
remove a
simplify
i

(01 + (00+0)1)*0
0
a
f
f
i
01 + (00+0)1
Alternative (01+001)*0
(6)
(iv) Write a regular grammar which defines the same language as finite state automaton A.
Remember: a grammar is regular if each production is of the form: N -> t, or N -> tB, or N -> .
S->0A
S->0B
A->1S
B->0A
B->1S
B->lambda
(3)
(b) Write a regular expression for each of the following languages:
(i) The language of all strings over {0, 1} which do not contain 00 as a substring
(ii) The language of all strings over {0, 1} which do not contain 101 as a substring
(i)
(1+01)*(+0)
(ii)
0*(1+000*)*0*
(3)
(c)
Create a Mealy Machine which takes in a binary number and outputs the quotient of the number
divided by three. For example: 14 divided by 3 gives quotient 4 and remainder 2; convert this to
binary: 1110 divided by 11 gives quotient 100 and remainder 10; this means that if the input to the
machine is 1110, then its output should be 100.
Binary numbers are 0, 1 :
T = {0, 1}
States represent the possible remainders when dividing by 5:
Remainders expressed in base 2: Q = {0, 1, 10}
Initial state is when remainder is 0:
I = {0}
2
{0, 1, 2, 3, 4}
CS3012 (Formal Languages)
Final state is when remainder is 0:
If at state 0 and get a 0:
If at state 0 and get a 1:
If at state 1 and get a 0:
If at state 1 and get a 1:
If at state 10 and get a 0:
If at state 10 and get a 1:
F = {0}
00/11= 0/3 = 0 r 0
01/11= 1/3 = 0 r 1
10/11= 2/3 = 0 r 10
11/11= 3/3 = 1 r 0
100/11= 4/3 = 1 r 1
101/11= 5/3 = 1 r 10
E = {0, 0/0, 0}
E = {0, 1/0, 1}
E = {1, 0/0, 10}
E = {1, 1/1, 0}
E = {10, 0/1, 1}
E = {10, 1/1, 10}
1/0
0
0/0
1
1/1
0/0
0/1
10
1/1
(9)
(25)
3
CS3012 (Formal Languages)
2. (a) Make truth tables for the following relations: (the first one is done for you)
(i) A implies B
(ii) B implies A
(iii) A is true if B is true
(iv) if A is true then B is true
(v) A is true only if B is true
(vi) A is true iff B is true
(vii) A is false iff B is true
(viii) for A to be true, it is sufficient that B be true (i.e. B is a sufficient condition to ensure that A is true)
(ix) for A to be true, it is necessary (but not necessarily sufficient) that B be true
A
0
0
1
1
B
0
1
0
1
i
1
1
0
1
ii
1
0
1
1
iii
1
0
1
1
iv
1
1
0
1
v
1
1
0
1
vi
1
0
0
1
vii
0
1
1
0
viii
1
0
1
1
ix
1
1
0
1
(8)
(b)
Use the algorithm to transform the following NDFSA into a DFSA

2
a
1
a
4
b
a
b
3
I
'
F'
{I}
E'
({1},a,{2,3})
S
Q'
X
t
{1}
{1}
{1}
a {2,3}
{2,3}
{2,3}
{4}
({2,3},a{4})
{4}
{4}
{3,4}
({2,3},b,{3,4})
{3,4}
{3,4}
({4},a{4})
S'
b {}
{2,3}
a {4}
b {3,4}
{4}
a {4}
b {}
({3,4},a,{4})
{3,4}
({3,4},b,{3,4})
a {4}
b {3,4}
4
CS3012 (Formal Languages)
4
a
a
a
1
a
2,3
b
3,4
b
(8)
(c)
3.
(a)
Create a finite state machine which accepts strings of decimal numbers which are divisible by 5.
Just need two states, numbers end in 0 or 5
(9)
Write a context free grammar for the language {aibjak: i = j + k},
and show a derivation tree for the string: aaaaabbaaa
S -> aSa | T
T -> aTb | 
S
a
S
a
a
S
a
a
S
a
T
a
T
b
a
T
b

(5)
(b)
(i)
Show that the grammar below is ambiguous.
S -> S * S
S -> S + S
S -> num
a+a*a
S =>S + S => a + S => a + S * S => a + a * S => a + a * a
S => S * S => S + S * S => a + S * S => a + a * S => a + a * a
(or show by derivation trees)
(ii)
Write an unambiguous grammar to define the same language. Make sure that addition and multiplication
have the usual precedence (i.e. 2+3*4=14, not 20).
The grammar you’ve seen in notes:
E -> E + T
E -> T
T -> T * F
T -> F
F -> num
(5)
5
CS3012 (Formal Languages)
(c)
Consider the grammar
S -> aS | aSbS | 
(i)
Show a derivation for aab
S => aS => aaSbS => aaSb => aab
(ii)
Show that the grammar is ambiguous.
S => aSbS => aSb => aaSb => aab
it has two right derivations, therefore it is ambiguous
(iii)
Give an unambiguous grammar defining the same language as the grammar above.
S -> aS | aTbS | 
T -> aTbT | 
(1)
(1)
(5)
(d)
R -> R * | ( R ) | R + R | R R | letter
is a grammar which recognises regular expressions.
Show a derivation tree for (ab + c)*
where a,b,and c are recognised as letter.
Show that the grammar is ambiguous.
Show why this may be a problem.
Give an unambiguous grammar which recognises regular expressions, and which produces
derivation trees which correspond to the way in which regular expressions are interpreted.
R -> R + R1
R1 -> R1 R2
R2 -> R3 * |
R3 -> ( R ) |
| R1
| R2
R3
letter
(8)
(25)
4.
(a)
Describe the languages defined by the following FSAs. Your description should be a relatively concise
statement describing a pattern, and not a blow-by-blow account of each state and arc.
(i)
(ii)
0,1
1
0
0
0
Strings of 0 and 1 starting with 0.
Strings of 0 and 1, with no adjacent 1’s and no 1 at the end.
(2)
(b)
List the first 8 strings (by lexical order) in the language defined by the regular expression (b+a*b)(a*b + ba)*
b,ab,bb,aab,abb,bab,bba,bbb
(2)
(c)
Write regular expressions for the languages of all strings over {0,1}:
(iii)
that do not begin or end with 00 or 11;
(01+10)(0+1)* (01+10) + (0+1)
(ii) strings which are either empty or which must start and finish with 1
(and could have anything else in the middle);
Λ + 1(0+1)*1 + 1
(iii) that contain an odd number of 1s and an even number of 0s.
Do it by making a diagram first and converting to RE
6
CS3012 (Formal Languages)
(7)
(d)
Is the language L = { aibj | i < 3j } regular? (proof required)
Note that you can pump the b's here, so that doesn't help. Try a proof by contradiction.
Suppose it is regular. Then there is a FSA, A, s.t. L(A) = { aibj | i < 3j }
Let A have n states where n > 3. Choose w  L(A) s.t. w = a3n-1bn. Consider a path accepting w. The a3n-1
part of w must visit 3n states. Therefore, at least one state must be visited more than once, say q.
Let y be the substring corresponding to the subpath from the start to q (y may be empty), let u be the
substring corresponding to the subpath from q back to q, and let z be the substring corresponding to
the subpath from q to the end.
Now, yuz is accepted. But so must yuuz (because we could take another run around the loop).
But u = ai, for some i  1, so now we have at least 3n a's in yuuz, so it is no longer true that i < 3j, so yuuz cannot be
accepted. Contradiction.
(e)
(8)
For the grammar specified below in the normal notation, classify each of the following strings as being in or
not in the language defined by the grammar, and give the derivation tree for any strings which are in the
language.
Strings:
tvvtvrv yes
vbetvv
no
tvvvttvbe no
tvtvtvtvtvvvbe yes
Grammar:
S -> DS | A
D -> tX
X -> vX | v
A -> vbe | rv
(6)
(25)
7
Download