Toc Unit 2 Regular Expression
Toc Unit 2 Regular Expression
UNIT 2
as regular language.
operators.
1. + union operator
2. . Concatenation operators
2 a {a} RE=a
3 a {a ,b} RE=a+b
b
4 {a ,b} RE=a.b
a b
5 a φ RE=φ
Regular Expressions Introduction
5
8 a {a ,b} RE=a+b
b
9 {, a, b, aa, ab, RE=(a+b)*
a, b
ba, aaa,...}
10 a. b
{ab,ba} RE=ab+ba
b. a
Regular Expressions Introduction
6
⚫ To show this:
⚫ From this recursive nature of RE we can conclude that if FAS for two RE R1
and R2 are given and if we can construct composite FAs for
1. R1+R2 2. R1.R2 3. R1*
Then we can construct Fas for any RE.
FA representing RE
12
Sr. Regular Automata
No. Expression
1 RE=
2 RE=a a
3 RE=a+b a
b
4 RE=a.b a b
5 RE=φ
FA representing RE
13
6 RE=a* a
7 RE=aa* or a+
a a
8 RE=a+b a
b
9 RE=(a+b)* a, b
a b
10 RE=ab+ba
b a
FA representing RE
14
Sr. No.
Regular Expression Automata
6 b
RE=a*b* a
7 a
RE=(ab)* q1
q0 b
8 a
RE=a*b b
q0 q1
9 b
RE=ab* a
q0 q1
a c
10 RE=a*bc* b
q0 q1
Examples on RE to Fas conversion
15
Solution:
1. a*b(a+b)*
a a,b
b
q0 q2
Examples on RE to Fas conversion
16
2. (ab)*ab*
ab b
a
q0 q1
Divide transition q2
ab by adding one
a b
more state. b
q0 q1
a
Examples on RE to Fas conversion
17
3. (a+ba)*ba
a+ba
Divide transition
a+ba by adding one
b a more transition.
q0 q1 q1
Divide transition ba b a
by adding one more q0 q1 q1
state.
a
ba
b a
q0 q1 q2
b
a
q3
Examples on RE to Fas conversion
18
3. (a+ba)*ba
a+ba
Divide transition
a+ba by adding
b a one more state.
q0 q1 q1
Divide transition ba b a
by adding one more q0 q1 q1
state.
a
ba
b a
q0 q1 q2
b
a
q3
Examples on RE to Fas conversion
19
5. (0+1(01)*)*
0+1(01)*
Divide transition
1
0+1(01)* by adding
q0 q1 q2 one more transition.
1(01)*
Divide transition
2
1(01)* by adding
one more state. q0 q1 q2
q3 0
1 (01)*
q0 q1 q2 3
0
Examples on RE to Fas conversion
20
5. (0+1(01)*)*
Divide transition
1(01)* by adding
one more state.
q3
q4 q5
1
q0 q1 q2
0
Examples on RE to Fas conversion
21
Ex. 2: Show that P*(QP*)* = (P+ Q)*
Divide transition
P
QP* QP* by adding one
more state.
q0 q1
1
P
Q q2
P
q0 q1
Examples on RE to Fas conversion
22
As all q0, q1, q2 are final state so they can be merged in to single state.
P, Q
q0, q1, q2
Solution: r
q1 -NFA to DFA
Closure of q0= {q0, q1, q2}
Closure of q1= {q0, q1, q2}
Closure of q2= {q0, q1, q2}
q0 So the equivalent DFA can be shown
s as bellow
q2
r, s
X+Y =Y+X
The associative laws holds for union of regular languages.
(X+Y)+Z = (X+Z)+Y
Algebraic Laws of Regular Expressions
29
3. Distributive Law
The left distributive law of concatenation over union hold for
regular language.
Z (X+Y) = ZX + ZY
The right distributive law of concatenation over union hold for
regular language.
(X+Y) Z = XZ + YZ
Algebraic Laws of Regular Expressions
31
L+L=L
Algebraic Laws of Regular Expressions
32
1. Φ + R =R
2. Φ . R =R . Φ =R
R = R . R
5. Φ* =
6. R + R = R
7. PQ + PR = P(Q + R)
8. QP + RP = (Q + R)P
Basic properties of Regular Expressions
35
9. R* R* =R*
10. R R* =R * R
11. (R*)* = R*
R R* =R*
Ex. 2: If S= {a, bb} find the set of all strings in S* with string length less than
or equal to 5. Also for given S, Prove whether following is true or false.
(S*)*=(S+)*
Solution:
String of length 0=
String of length 1= a
String of length 2= aa, bb
String of length 3= aaa, abb, bba
String of length 4= aaaa, bbbb, aabb, bbaa, abba
String of length 5= aaaaa, abbbb, bbabb, bbbba, abbaa, aabba, bbaaa, aaabb
And (S*)*=(S+)* is true as is belonging to both expressions.
Examples on Regular Expressions
38
Ex. 2: Find all regular expressions for following languages over {a, b}.
1. The set of all strings ending in b.
2. The set of all strings ending in ba.
3. The set of all strings ending neither in b nor in ba.
4. The set of all strings ending in ab.
5.The set of all strings ending neither in ab nor in ba.
Solutions:
1. The set of all strings ending in b (a+b)*b
2. The set of all strings ending in ba (a+b)*ba
3. The set of all strings ending neither in b nor in ba. (a+b)*aa+a+
4. The set of all strings ending in ab. (a+b)*ab
5. The set of all strings ending neither in ab nor in ba.
a +b + (a+b)*(aa+bb)
Examples on Regular Expressions
39
Ex. 3: Find all regular expressions for following subset of (0, 1)*.
1. The Language of all strings containing exactly two 0’s.
2. The Language of all strings containing at least two 0’s.
3. The Language of all strings that do not end with 01.
4. The Language of all strings starting with 11.
Solutions:
1. The Language of all strings containing exactly two 0’s.
RE 1*01*01*
2. The Language of all strings containing at least two 0’s.
RE 1*01*0(1+0)*
3. The Language of all strings that do not end with 01.
RE (1+0)*(00+11+10)
4. The Language of all strings starting with 11.
RE 11(0+1)*
Examples on Regular Expressions
40
Solution: 1 q1
1. (11+00)*
11
11+00
1
q0 q0
q0 0
00
q2 0
Examples on Regular Expressions
41
Solution:
2. (111+100)*0
(111+100)*
(111+100)*0
0
(111+100)
(111+100)
0 0
q0 q1
Examples on Regular Expressions
42
Solution:
0 q
2. (111+100)*0 3
q2 0
100 1
0
q0 q0 q1
q1
0
111
1
1
1
Examples on Regular Expressions
43
Solution:
3. 0+ 10* + 01* 0
01* 0
0+ 10* + 01* 0
0
q0 q1 q0 q1
q0 0* 10*
1
0
q0 q1
0 1*
0
q0 q0
Examples on Regular Expressions
44
Solution:
3. 0+ 10* + 01* 0
q2
1 0
0
q0 q1
0
0
q3 q4 q5
1
Examples on Regular Expressions
45
Solution:
4. 10+ (0+11)0* 1 0
0
1
q0 q2 q3 q5
1 0
q1
1
1
q4
Examples on Regular Expressions
46
0,1
0,1
(010+101)
1
q1 q2
0 0,1
0,1
0
q0 q5
1 1
0
q3 q4
Examples on Regular Expressions
47
Ex 5: Find FA for given RE 1. (a+b)* abb 2. (11)* . 010 . (11)*
Solution:
1. (a+b)* abb
(a+b)* abb
a, b
a b b
q0 q1 q2 q3
Examples on Regular Expressions
48
Solution:
2. (11)* . 010 . (11)*
11
11
010
q0 q1 q2 q3
Examples on Regular Expressions
49
Solution:
2. (11)* . 010 . (11)*
q6
q4
1
1
1 1
1
q0 q1 q2 q3
DFA to Regular Expressions
50
a b a. b
RE= ab
DFA to Regular Expressions : By State or Loop
elimination.
52
Example: 2
q2
1
0 10
0
q1 q3 q0 q1
01
1
0
q4
RE= 0(10+01)*
DFA to Regular Expressions : By State or Loop
elimination.
53
Example: 3
q2
1 10
0
q1
q3
01
1
0
q1
RE= (10+01)*
Examples on DFA to RE: By State or Loop elimination
54
Examples 1: Find a Regular expression for the given two State machine.
a
c
b
q0 q1
d
Solution:
Now here nor q0 or q1 can be eliminated as both are final states.
But loop can be eliminated by moving its effect either to state q0 or q1, so let us move
it to q0.
a+bc*d
c
b
q0 q1
Examples on DFA to RE: By State or Loop elimination
55
The above machine has two final states. The machine can be divided in to two machines,
Machine with q0 as final state and machine with q1 as final state.
a+bc*d a+bc*d c
b
q0 q0 q1
So RE= RE1+RE2
RE= (a+bc*d)* + (a+bc*d)* bc*
= (a+bc*d)* ( + bc*)
Examples on DFA to RE: By State or Loop elimination
56
Example 2: Find Regular Expression for given three state machine.
a b
d f c
q0 q1 q2
e
g
Solution: For given machine state q1 is the crossing point o three loops
1. q1 to q1 on input b.
2. Loop between q1 and q0 on input ea*d
3. Loop between q1 and q2 on input fc*g
Now eliminate the loop by moving these loop effects on state q1.
a b+da*e+fc*g
d f c
q0 q1 q2
Examples on DFA to RE: By State or Loop elimination
57
Example 2:
Now eliminate the state q1 can be eliminated.
a d(b+ea*d+fc*g)f c
q0 q1
RE = a* d(b+ea*d+fc*g)f c*
Examples on DFA to RE: By State or Loop elimination
58
Example 3: Find Regular Expression for given three state machine
1. 0,1
0
q0 q1
1
(01)* 0, 1
0
q0 q1
b+ab a, b b+ab a, b
a a aa
q0 q1 q2 q0 q2
After eliminating q1
1 1
q3
10
q1
0 0
q2
q0
1 1
q3
So final required RE= 0(10)* (0+11)+ 1 (01)* (1+00)
01
Examples on DFA to RE: By State or Loop elimination
61
Example 4: Write a RE for the following.
=(0, 1) Odd number of 1’s in String.
=(0, 1) Triple 0 must never appear in Strings.
3. Identities in C language.
4. Obtain plain English language represented by RE.
a. 0* (10* 10*)* 1(0* 10* 1)* 0*
b. 0* (0* 10* 1)* 0*
Solution:
i. DFA for Given Language is
0 0+10*1
0 0
1 1
q0 q0 q1
q1
1
RE=(0+10*1)*10*
Examples on DFA to RE: By State or Loop elimination
62
1 1+01+001
0 0
q0 q1 q2 0 0
q0 q1 q2
1
0
1
Φ
O, 1
RE= 1+01+0011+01+0011+01+001)
RE=(1+01+001)*()
Examples on DFA to RE: By State or Loop elimination
63
Solutions:
a. The set of strings that begin with 110.
RE 110 (1+0)*
1 0 1
q0 q1 q2 q3
0
Examples on DFA to RE: By State or Loop elimination
65
Example 5:
b. The set of all strings not containing 101 as a substring.
0+11*00 1
1 0
q0 q1 q2
Let P, Q and R be regular expressions on . Then if p does not contain then the
following equation
R=Q + RP …………………….(1)
R= QP*. ----------------------(2)
Arden’s Theorem Solved Examples
67
A= A1+B1+
C0
C= B1
Arden’s Theorem Solved Examples
68
Example 1:
So now Equation (1) can be rewritten as
A= A1+(B1+ (B1+
Now compare above equation with first equation of Arden’s theorem. It is of
the form R=Q+RP. With R=A, Q= (B1+ P=1 so it’s solution is given by.
R=QP*;
So A= (B1+ ……………………….
Now substitute the value of A from equitation 4 & the value of C from
equitation 3 in eq. 2
B= (B1+
=1*0+B11*0+B10
=1*0+B(11*0+10)
Arden’s Theorem Solved Examples
69
Example 1:
Now compare above equation with first equation of Arden’s theorem. It is of
the form R=Q+PR. With R=B, Q= 1*0 & P=(11*0+10) so it’s solution is given
by R=QP*;
B= 1*0(11*0+10)* ……………………….(5)
Solution: The Set of equations for regular sets (Incoming Transition to every
state (q1, q2 & q3)) represented as follow.
q1= q10+q30+
q2= q11+ q31+q21
q3= q20
Arden’s Theorem Solved Examples
71
Example 3: Prove that following FA accepts strings with equal number of 0’s and 1’s
such that each prefix has atmost one more 0 than 1’s or at most one more 1 than
0’s.
0 0
q0 q1
1
0
0 1
q2
q3
Solution: As q3 is dead state so can be eliminated. The Set of equations for
regular sets (Incoming Transition to every state (q1, q2 & q0)) represented as
follow.
q0= q11+q20 +
q1= q00
q2= q01
Arden’s Theorem Solved Examples
73
Example 3:
Now substitute q1 and q2 in eq. 1
Example 4: Consider the following transition diagram and prove that the
string recognized are (0 + 0(1+00)*1*) 0 (1+00)* 1 using Arden’s theorem.
0 1
0 0
q1 q2 q3
1
0
Example 4:
So q1=
=
Now put value of q1 from eq. 5 in eq. 4
q2= 0 (1+10)*
Hence Proved.
Arden’s Theorem Solved Examples
77
Example 5:
Now substitute value of q3 in eq. 2
B= A0 + B1 + B1 0
A0 + B(1 + 1 0)
Now compare above equation with first equation of Arden’s theorem. It is of the
form R=Q+RP. so it’s solution is given by R=QP*.
B=A0 (1 + 1 0)*
So substitute the value of C in eq. 1
A= A1+B11 +
Now substitute the value of b from eq. 4 in eq. 5
A= A1+A0 (1 + 1 0)* 11 +
Now compare above equation with first equation of Arden’s theorem. It is of the
form R=Q+RP. so it’s solution is given by R=QP*.
A=
Arden’s Theorem Solved Examples
79
Example 5:
Hence B from Eq. 4 is
B= 0 (1 + 1 0)*
Hence C from Eq. 3 is
C= 0 (1 + 1 0)* 1
Limitations of Finite Automata
80
Pumping Lemma
81
If there exists at least one string made from pumping which is not in L,
then L is surely not regular.
That is, if Pumping Lemma holds, it does not mean that the language is
regular.
Pumping Lemma
82
The Pumping Lemma says that is a language A is regular, then any string
in the language will have a certain property, provided that it is ‘long
enough’ (that is, longer than some length p, which is the pumping length).
Inside any string in A that’s longer than p, we can find a piece that can be
repeated (pumped) as many times as we want, and the result will always
be in A. Moreover, this piece can be found within the first p letter of our
string. That is, given any string s in A longer than p, we can find a
substring in s that can be pumped. We’ll call this substring y. Then
anything before y we’ll call x, and anything after y we’ll call z. Then the
whole string can be rewritten as x – y – z. (Remember that these are
strings and not letters!) By repeating y zero or more times, we get: Xz, xyz,
xyyz, xyyyz, …, xyyyyyyyyyyyyyyyyyz, … What the Pumping Lemma says is
that each of these must be in A.
Pumping Lemma
84
Condition 3: |xy|≤p Since x is the piece before y, this says that all of y
must come from the first p letters of our string s, so that the combined
length of x and y is at most p.
How to use the Pumping Lemma to prove that a language is
not regular?
87
If there exists at least one string made from pumping which is not in L,
then L is surely not regular.
That is, if Pumping Lemma holds, it does not mean that the language is
regular.
How to use the Pumping Lemma to prove that a language is
not regular?
89
Example 1: Using Pumping Lemma Show that the language L={anb2n} is not
regular.
Solution:
Step 1: Let us assume that L is a regular and L is accepted by FA with n states.
Step 2: Let us choose a string represented by given language L and accepted
by FA.
According to language no of occurrences of a should be followed by double
no. of occurrences of b. So let us consider the string |w|
L= anb2n | w |= aabbbb
Now let us write w as xyz with |y|>0 and |xy| <= n
aa bb bb
x y z
Pumping Lemma Examples
91
Example 1:
Now let us pump the y i times write i >0
Let us consider i=2
aa bbbb bb
x yi z
Now let us check xy2 z belongs to L or not.
xy2 z = aabbbbbb
So as per given language no of occurrences of a should be followed by
double no. of occurrences of b is not followed by string after pumping so
which indicates |w| Є L, and hence the given language is not regular.
Pumping Lemma Examples
92
Example 2: Using Pumping Lemma Show that the language L={anbn} is not
regular.
Solution:
Step 1: Let us assume that L is a regular and L is accepted by FA with n states.
Step 2: Step 2: Let us choose a string represented by given language L and
accepted by FA.
According to language no of occurrences of a should be followed by equal no.
of occurrences of b. So let us consider the string |w|
L= anbn
| w |= aaabbb
Now let us write w as xyz with |y|>0 and |xy| <= n
aa abb b
x y z
Pumping Lemma Examples
93
Example 2:
Now let us pump the y i times write i >0
Let us consider i=2
aa abbabb bb
x yi z
Now let us check xy2 z belongs to L or not.
xy2 z = aaabbabbbb
So as per given language no of occurrences of a should be followed by same
no. of occurrences of b is not followed by string after pumping so which
indicates |w| Є L, and hence the given language is not regular.
Pumping Lemma Examples
94
Example 3: Show that the language L={0m1n 0m+n m>=1 and n>=1} is not
regular.
Solution:
Step 1: Let us assume that L is a regular and L is accepted by FA with n states.
Step 2: Step 2: Let us choose a string represented by given language L and
accepted by FA. According to language no of m times occurrences of 0 should
be followed by n times occurrences of 1 followed by m+n times occurrences
of 0 should be followed by n times occurrences of 1 followed by m+n times
occurrences of 0 . So let us consider the string |w|
L= 0m1n 0m+n | w |= 00110000
Now let us write w as xyz with |y|>0 and |xy| <= n
001 10 000
x y z
Pumping Lemma Examples
95
Example 3:
Now let us pump the y i times write i >0
Let us consider i=2
001 0101 000
x yi z
Now let us check xy2 z belongs to L or not.
xy2 z = 0010101000
So as per given language m times occurrences of 0 should be followed by n
times occurrences of 1 followed by m+n times occurrences of 0 should be
followed by n times occurrences of 1 followed by m+n times occurrences of 0
. is not followed by string after pumping so which indicates |w| Є L, and
hence the given language is not regular.
Pumping Lemma Examples
96
Example 4: Using Pumping Lemma Show that the given language is not
regular. L={wwR | w ∈ {0, 1}*}
Solution:
Step 1: Let us assume that L is a regular and L is accepted by FA with n states.
Step 2: Let us choose a string represented by given language L and accepted
by FA.
According to language is set of string of {0 & 1} such that string consist of
string followed by reverse of itself . So let us consider the string |w|
| w |= 1001
Now let us write w as xyz with |y|>0 and |xy| <= n
10 0 1
x y z
Pumping Lemma Examples
97
Example 4:
Now let us pump the y i times write i >0
Let us consider i=2
10 00 1
x yi z
Now let us check xy2 z belongs to L or not.
xy2 z = 10001
So as per given is set of string of {0 & 1} such that string consist of string
followed by reverse of itself . So is not followed by string after pumping so
which indicates |w| Є L, and hence the given language is not regular.
Pumping Lemma Examples
98
Example 5: Using Pumping Lemma Show that the given language is not
regular. L={ww | w ∈ {0, 1}*}
Solution:
Step 1: Let us assume that L is a regular and L is accepted by FA with n states.
Step 2: Step 2: Let us choose a string represented by given language L and
accepted by FA.
According to language is set of string of {0 & 1} such that string consist of
string followed by itself . So let us consider the string |w|
| w |= 1010
Now let us write w as xyz with |y|>0 and |xy| <= n
1 01 0
x y z
Pumping Lemma Examples
99
Example 5:
Now let us pump the y i times write i >0
Let us consider i=2
1 0101 0
x yi z
Now let us check xy2 z belongs to L or not.
xy2 z = 101010
So as per given is set of string of {0 & 1} such that string consist of string
followed by itself . So is not followed by string after pumping so which
indicates |w| Є L, and hence the given language is not regular.
Closure Properties of Regular Language
100
1. Union
2. Intersection
3. Difference
4. Concatenation
5. Kleene Closure
6. Reversal
7. Complementation
Closure Properties of Regular Language
101
and are closes Under union if another FA M3 which acceptes all string
generated by M1 & M2.
1. Regular Expressions in Unix: The grep utility in Unix scans a file for