0% found this document useful (0 votes)
116 views

Regular Expressions

The regular expression is: (a + b)*c? SOLUTION: The regular expression is: (a + b)*c? This regular expression generates all strings over the alphabet {a, b, c} such that a b is never followed by a c. The expression (a + b)* allows any number of a's and b's (including none) to appear, and the c? allows an optional single c at the end, ensuring that c never follows a b.

Uploaded by

Shut Up
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
116 views

Regular Expressions

The regular expression is: (a + b)*c? SOLUTION: The regular expression is: (a + b)*c? This regular expression generates all strings over the alphabet {a, b, c} such that a b is never followed by a c. The expression (a + b)* allows any number of a's and b's (including none) to appear, and the c? allows an optional single c at the end, ensuring that c never follows a b.

Uploaded by

Shut Up
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

ASSIGNMENT # 2

SUBMITTED BY: IQRA REHMAT


ROLL NO # BC-01 (B) U/18
SUBMITTED TO: DR.FOUZIA
JABEEN
SUBJECT: THEORY OF
AUTOMATA
SUBMISSION DATE: 10
November 2020, Tuesday
DEPARTMENT: BS CS 5TH
INSTITUTE: SBBWUP

1
Table of Contents
ASSIGNMENT # 1.........................................................................................................................................1
Question#1.................................................................................................................................................3
QUESTION # 2:...........................................................................................................................................4
QUESTION # 3:...........................................................................................................................................5
QUESTION # 4:...........................................................................................................................................6
QUESTION # 5:...........................................................................................................................................7
QUESTION # 6:...........................................................................................................................................8
QUESTION # 7:...........................................................................................................................................9
SOLUTION:..............................................................................................................................................9
QUESTION # 8:.........................................................................................................................................10
SOLTION:..............................................................................................................................................10
QUESTION # 9:.........................................................................................................................................11
SOLUTION:............................................................................................................................................11
QUESTION # 10:.......................................................................................................................................12
SOLUTION:............................................................................................................................................12
QUESTION # 11:.......................................................................................................................................13
SOLUTION:............................................................................................................................................13
QUESTION # 12:.......................................................................................................................................14
SOLUTION:............................................................................................................................................14

2
Question#1
Find the shortest string that is not in the language
represented by the regular expression a*(ab)*b*.

SOLUTION:
 It can easily be seen that , a, b, which are strings in the
language with length 1 or less. Of the strings with length 2
aa, bb and ab are in the language. However, ba is not in it.
Thus the answer is ba.

3
QUESTION # 2:
  For the two regular expressions given below,
(a) find a string corresponding to r2 but not to r1 and
(b) find a string corresponding to both r1 and r2.

        r1 = a* + b*     r2 = ab* + ba* + b*a + (a*b)*

SOLUTION:
Any string consisting of only a's or only b's and the empty string are
in r1. So we need to find strings of r2 which contain at least one a and
at least one b. For example ab and ba are such strings.
(b) A string corresponding to r1 consists of only a's or only b's or the
empty string. The only strings corresponding to r2 which consist of
only a's or b's are a, b and the strings consisting of only b's
(from (a*b)*).

4
QUESTION # 3:
Let r1 and r2
 Be arbitrary regular expressions over some alphabet. Find a simple
(the shortest and with the smallest nesting of * and +) regular
expression which is equal to each of the following regular
expressions.

(a)  (r1 + r2 + r1r2 + r2r1)*


(b) (r1 (r1 + r2)*) +

SOLUTION:
 One general strategy to approach this type of question is to try to
see whether or not they are equal to simple regular expressions that
are familiar to us such as a, a*, a+, (a + b)*, (a + b)+ etc.
(a) Since (r1 + r2)* represents all strings consisting of strings
of r1 and/or r2 , r1r2 + r2r1 in the given regular expression is
redundant, that is, they do not produce any strings that are not
represented by (r1 + r2)*. Thus (r1 + r2 + r1r2 + r2r1)* is reduced
to (r1 + r2)*
(b) (r1 (r1 + r2)*) + means that all the strings represented by it must
consist of one or more strings of (r1 (r1 + r2)*). However, the strings
of (r1 (r1 + r2)*) start with a string of r1 followed by any number of
strings taken arbitrarily from r1 and/or r2. Thus anything that comes
after the first r1 in (r1 (r1 + r2)*) + is represented by (r1 + r2)*.
Hence (r1 (r1 + r2)*) also represents the strings of (r1 (r1 + r2)*) +,
and conversely (r1 (r1 + r2)*) + represents the strings represented
by (r1 (r1 + r2)*). Hence (r1 (r1 + r2)*) + is reduced to (r1 (r1 + r2)*).

5
QUESTION # 4:
 Find a regular expression corresponding to the language of all
strings over the alphabet {a, b} that contain exactly two a's.

SOLUTION: 
A string in this language must have at least two a's. Since any string
of b's can be placed in front of the first a, behind the second a and
between the two a's, and since an arbitrasry string of b's can be
represented by the regular expression b*, b*a b*a b* is a regular
expression for this language.

6
QUESTION # 5:
Find a regular expression corresponding to the language of all
strings over the alphabet {a, b} that contain no more than one
occurrence of the string aa.

SOLUTION: 
If there is one substring aa in a string of the language, then that aa
can be followed by any number of b. If an a comes after that aa, then
that a must be preceded by b because otherwise there are two
occurrences of aa. Hence any string that follows aa is represented
by (b + ba)*. On the other hand if an a precedes the aa, then it must
be followed by b. Hence a string preceding the aa can be represented
by (b + ab)*. Hence if a string of the language contains aa then it
corresponds to the regular expression (b + ab)*aa (b + ba)*.
If there is no aa but at least one a exists in a string of the language,
then applying the same argument as for aa to a, (b + ab)*a (b +
ba)* is obtained as a regular expression corresponding to such
strings.
If there may not be any a in a string of the language, then applying
the same argument as for aa to , (b + ab)*(b + ba)* is obtained as a
regular expression corresponding to such strings.
Altogether (b + ab)*(  + a + aa) (b + ba)* is a regular expression
for the language.

7
QUESTION # 6:
 Find a regular expression corresponding to the language of strings
of even lengths over the alphabet of {a, b}.

SOLUTION:
 Since any string of even length can be expressed as the
concatenation of strings of length 2 and since the strings of length 2
are aa, ab, ba, bb, a regular expression corresponding to the
language is ( aa + ab + ba + bb )*
Note that 0 is an even number. Hence the string   is in this
language.

8
QUESTION # 7:
Describe as simply as possible in English the language
corresponding to the regular expression a*b (a*ba*b)*a*.
SOLUTION:

 A string in the language can start and end with a or b, it has at


leastone b, and after the first b all the b's in the string appear in
pairs. Any number of as can appear any place in the string. Thus,
simply put, it is the set of strings over the alphabet {a, b} that
contain an odd number of b's

9
QUESTION # 8:
Create a regular expression that generates the language of all
strings over the alphabet {a, b, c} of the form:

a
ac
acab
acabab
acababab
acabababab
etc...

In other words, an a, optionally followed by c, optionally followed by


1 or more occurrences of ab.

Note that the empty string is not in this language.

SOLTION:
The regular expression is: ac*(ab)*

10
QUESTION # 9:
Create a regular expression that generates the language over the
alphabet {a, b} of all strings in which each b is preceded by at
least one a.

Examples of strings in the language:

ε
a
aaaa
ab
aba
abaabaaaab

Examples of strings not in the language:

b
abb
bba

SOLUTION:
The regular expression is: a*b*a*b*

11
QUESTION # 10:
Create a regular expression that generates the language over the
alphabet {a, b} of all strings containing an even number of bs.

Examples of strings in the language:

ε
a
aa
aaa
abba
bb
bab
abbaa
aababbb

Examples of strings not in the language:

b
abbb
babab
abbbabab
bbaaba

SOLUTION:
The regular expression is: a*(ba*ba*)*

12
QUESTION # 11:
Create a regular expression that generates the language over the
alphabet {a, b, c} of all strings in which a b is never followed by a c.

Examples of strings in the language:

ε
ab
abba
abac
cab
cba
bac
aac
c

Examples of strings not in the language:

bc
abc
bca
aabcab

The key is to construct the regular expression to grow the generated


string in a way that,

 it never adds a chunk that contains bc (obviously)


 if the current string ends in b, it never adds a chunk that begins
with c

13
SOLUTION:
The regular expression is: (c+a)*(ab+b)*(ba+ac)*

QUESTION # 12:
Create a regular expression that generates the language over the
alphabet {a, b} of all strings in which

n mod 3 = 2
Where n is the length of the string. In other words, all strings in the
language have a length of 2, or 5, or 8, or 11, etc.

Examples of strings in the language:

ab
bb
baabb
aaaaa
abbbbbba

Examples of strings not in the language:

ε
a
abb
baaa
babbba
SOLUTION:
The regular expression is:

(a+b)(a+b). ((a+b)(a+b)(a+b))*

14
15

You might also like