Uploaded by lovelylala00000

Assg2

advertisement
Solutions to Assignment 2
April 2, 2001
Some of these solutions have ideas taken from the solutions whi h I re eived. So don't be
surprised if your solution is here.
Des ribe the languages denoted by the following expressions: The des ription should
be simple and in plain English. So a solution like \one or more zeroes followed by a 1 followed by
one or more zeroes followed by ....." will not onsidered as a orre t solution.
a) 0(0j1) 0: All strings beginning and ending with a zero of length at least 2. (Need to ex lude
the 0 string)
b) ((j0)1 ) : ((j0)1 ) = (1 j01 ) = (1 j0) = (1j0) . Language a epted is the set of all
strings.
) (0j1) 0(0j1)(0j1): The set of all strings where the third digit from the right is a 0.
d) 0 10 10 10 : Set of all strings with exa tly three 1's.
e) (00j11) ((01j10)(00j11) (01j10)(00j11) ) : Set of all strings with even number of 0's and even
number of 1's
Write regular de nitions for the following languages: Some of you have given ontext
free grammars for these languages. The main di eren e between regular de nitions and ontext
free grammars, is that re ursion is not allowed in regular de nitions. Suppose you are de ning a
regular de nition like:
a
b
z
...
Every right hand side an use only the basi symbols and any others you have de ned before.
So you annot use z or even b to de ne . However, you an use a and any symbols of the base
alphabet to de ne .
Another ommon mistake was to give regular expressions whi h do not en ompass the whole language or allow strings outside the language. One solution whi h I got for part (e) was \djd00dj : : : jd99d00 ,
where d is the solution to part (d). This on the fa e of it may seem orre t. But it allows for strings
like 011110, whi h have too many repetitions.
a) All strings of letters that ontain the ve vowels in order.
1
{ (aj : : : jz )
{ 1 a e i o u
b) All strings of letters in whi h the letters are in as ending order.
{ 2
a b : : : z ) Comments onsisting of a string surrounded by /* and */ without an intervening */ unless it appears inside the quotes \ and ". This allows for weird strings like /*ab /*/,
/*\ab /"/ab/*/. Anything inside the /* and */ an be broken into a sequen e of blo ks.
Ea h blo k is either a normal blo k or a quoted blo k. A quoted blo k is any string whatsoever en losed in quotes. A normal blo k is any string whi h does not have a quote and also
does not have the substring */.
{ all printable letters
{ q the double quote hara ter
{o
/*
{
*/
{ A ; q i.e. every letter ex ept '*' and the double quote
{ B = i.e. every letter ex ept '/'
{ C B i.e. all two letter strings starting with '*' and ending with a letter from B
{
(AjC ) i.e. strings over without */ as a substring
{ 3 o ( jq q)
d) All digit strings with no two equal onse utive digits. Let denote the regular expression
for all non-empty digit strings with no equal onse utive digits, if the digits were only from
the set f0; 1; : : : ; ig. Then
{ 1 (01) (0j010) j (10) (1j101)
{ +1
j[ ?(d ) d ?℄, where d = k+1'st digit.
{ 4
9?
e) All strings with at most one repeated digit. Note that every su h string if broken at the
repeating digit, will break into two pie es without any repeating digit. Conversely the onatenation of two strings without any repeating digit, an have at most one repetition (Sin e
the empty string is part of 4 , one letter strings do not ause problems).
i
k
{ 5
k
k
k
k
4 4
f) Set of all strings with even number of 0's and odd number of 1's.
{ a (00j11)
{ b (01j10)
{ Æ a (ba ba ) , even number of b's, hen e even number of 0's and 1's (Why?).
{
a bÆ, odd number of b's, hen e odd number of 0's and odd number of 1's. Also
need to he k that it aptures all strings with odd number of 0's and odd number of 1's.
Hint: Consider the shortest pre x of the string whi h has an odd number of 1's and odd
number of 0's, and argue that this pre x must end with either a 01 or a 10.
2
1Æj0
h) All binary strings whi h do not ontain the substring 011. Equivalently but for the rst blo k
of 1's every 1 must be pre eded by a 0, i.e. all strings over the alphabet 0,01.
{ 8 1 (0j01) = 1 (01?) = 1 (0+ 1?)
i) All binary strings that do not ontain the subsequen e 011. Equivalently all strings whi h
have at most one 1 after the rst 0.
{ 9 1 0 1?0 = 1 0 (10 j)
{ 6
Prove that L = fan bn : n 0g is not regular Proof: If r is a regular expression whi h does
not make use of the \*" operator, then it an be expanded out in full, and then it be omes a bun h
of strings seperated by \j". In this ase the language a epted by r is nite. But L is in nite. So
if L were regular and r is the regular expression for it, then r must ontain a subterm s for some
regular expression s. Now let w be a string, a epted by r and break it up into three pie es x; y; z
so that y orresponds to s part, and y is not the empty string. Now, sin e xyz is satis ed by the
regular expression, so is xyyz . Sin e every string of L has all its a's before its b's, y annot have
both a and b in it. So y onsists of only a's or only b's. In either ase xyyz does not have equal
number of a's and b's, sin e w = xyz had an equal number of a's and b's.
3
Download