Regular Expression: Operations On Regular Language

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 33

Regular Expression

o The language accepted by finite automata can be easily described by simple


expressions called Regular Expressions. It is the most effective way to represent
any language.
o The languages accepted by some regular expression are referred to as Regular
languages.
o A regular expression can also be described as a sequence of pattern that defines
a string.
o Regular expressions are used to match character combinations in strings. String
searching algorithm used this pattern to find the operations on a string.

For instance:

In a regular expression, x* means zero or more occurrence of x. It can generate {e, x,


xx, xxx, xxxx, .....}

In a regular expression, x+ means one or more occurrence of x. It can generate {x, xx,
xxx, xxxx, .....}

Operations on Regular Language


The various operations on regular language are:

Union: If L and M are two regular languages then their union L U M is also a union.

1. 1. L U M = {s | s is in L or s is in M}  

Intersection: If L and M are two regular languages then their intersection is also an
intersection.

1. 1. L ⋂ M = {st | s is in L and t is in M}  

Kleen closure: If L is a regular language then its Kleen closure L1* will also be a
regular language.

1. 1. L* = Zero or more occurrence of language L.  

Example 1:
Write the regular expression for the language accepting all combinations of a's, over
the set ∑ = {a}

Solution:

All combinations of a's means a may be zero, single, double and so on. If a is appearing
zero times, that means a null string. That is we expect the set of {ε, a, aa, aaa, ....}.
So we give a regular expression for this as:

1. R = a*  

That is Kleen closure of a.

Example 2:
Write the regular expression for the language accepting all combinations of a's except
the null string, over the set ∑ = {a}

Solution:

The regular expression has to be built for the language

1. L = {a, aa, aaa, ....}  

This set indicates that there is no null string. So we can denote regular expression as:

R = a+

Example 3:
Write the regular expression for the language accepting all the string containing any
number of a's and b's.

Solution:

The regular expression will be:

1. r.e. = (a + b)*  

This will give the set as L = {ε, a, aa, b, bb, ab, ba, aba, bab, .....}, any combination of
a and b.

The (a + b)* shows any combination with a and b even a null string.
Examples of Regular Expression
Example 1:
Write the regular expression for the language accepting all the string which are starting
with 1 and ending with 0, over ∑ = {0, 1}.

Solution:

In a regular expression, the first symbol should be 1, and the last symbol should be 0.
The r.e. is as follows:

1. R = 1 (0+1)* 0  

Example 2:
Write the regular expression for the language starting and ending with a and having
any having any combination of b's in between.

Solution:

The regular expression will be:

1. R = a b* b  

Example 3:
Write the regular expression for the language starting with a but not having consecutive
b's.

Solution: The regular expression has to be built for the language:

1. L = {a, aba, aab, aba, aaa, abab, .....}  

The regular expression for the above language is:

1. R = {a + ab}*  

Example 4:
Write the regular expression for the language accepting all the string in which any
number of a's is followed by any number of b's is followed by any number of c's.
Solution: As we know, any number of a's means a* any number of b's means b*, any
number of c's means c*. Since as given in problem statement, b's appear after a's and
c's appear after b's. So the regular expression could be:

1. R = a* b* c*  

Example 5:
Write the regular expression for the language over ∑ = {0} having even length of the
string.

Solution:

The regular expression has to be built for the language:

1. L = {ε, 00, 0000, 000000, ......}  

The regular expression for the above language is:

1. R = (00)*  

Example 6:
Write the regular expression for the language having a string which should have atleast
one 0 and alteast one 1.

Solution:

The regular expression will be:

1. R = [(0 + 1)* 0 (0 + 1)* 1 (0 + 1)*] + [(0 + 1)* 1 (0 + 1)* 0 (0 + 1)*]  

Example 7:
Describe the language denoted by following regular expression

1. r.e. = (b* (aaa)* b*)*  

Solution:

The language can be predicted from the regular expression by finding the meaning of it.
We will first split the regular expression as:

r.e. = (any combination of b's) (aaa)* (any combination of b's)


L = {The language consists of the string in which a's appear triples, there is no
restriction on the number of b's}

Example 8:
Write the regular expression for the language L over ∑ = {0, 1} such that all the string
do not contain the substring 01.

Solution:

The Language is as follows:

1. L = {ε, 0, 1, 00, 11, 10, 100, .....}  

The regular expression for the above language is as follows:

1. R = (1* 0*)  

Example 9:
Write the regular expression for the language containing the string over {0, 1} in which
there are at least two occurrences of 1's between any two occurrences of 1's between
any two occurrences of 0's.

Solution: At least two 1's between two occurrences of 0's can be denoted by
(0111*0)*.

Similarly, if there is no occurrence of 0's, then any number of 1's are also allowed.
Hence the r.e. for required language is:

1. R = (1 + (0111*0))*  

Example 10:
Write the regular expression for the language containing the string in which every 0 is
immediately followed by 11.

Solution:

The regular expectation will be:

1. R = (011 + 1)*  The equivalent DFA will be:

2. R = (011 + 1)*  
Conversion of RE to FA
To convert the RE to FA, we are going to use a method called the subset method. This
method is used to obtain FA from the given regular expression. This method is given
below:

Step 1: Design a transition diagram for given regular expression, using NFA with ε
moves.

Step 2: Convert this NFA with ε to NFA without ε.

Step 3: Convert the obtained NFA to equivalent DFA.

Example 1:
Design a FA from given regular expression 10 + (0 + 11)0* 1.

Solution: First we will construct the transition diagram for a given regular expression.

Step 1:

Step 2:

Step 3:
Step 4:

Step 5:
Now we have got NFA without ε. Now we will convert it into required DFA for that, we
will first write a transition table for this NFA.

State 0 1

→q0 q3 {q1, q2}

q1 qf ϕ

q2 ϕ q3

q3 q3 qf

*qf ϕ ϕ

State 0 1

→[q0] [q3] [q1, q2]

[q1] [qf] ϕ
[q2] ϕ [q3]

[q3] [q3] [qf]

[q1, q2] [qf] [qf]

*[qf] ϕ ϕ

The equivalent DFA will be:

Example 2:
Design a NFA from given regular expression 1 (1* 01* 01*)*.

Solution: The NFA for the given regular expression is as follows:

Step 1:

Step 2:

Step 3:
Example 3:
Construct the FA for regular expression 0*1 + 10.

Solution:

We will first construct FA for R = 0*1 + 10 as follows:

Step 1:

Step 2:
Step 3:
Step 4:

Arden's Theorem
The Arden's Theorem is useful for checking the equivalence of two regular expressions
as well as in the conversion of DFA to a regular expression.

Let us see its use in the conversion of DFA to a regular expression.

Following algorithm is used to build the regular expression form given DFA.

1. Let q1 be the initial state.

2. There are q2, q3, q4 ....qn number of states. The final state may be some qj where
j<= n.

3. Let αji represents the transition from qj to qi.

4. Calculate qi such that

qi = αji * qj

If qj is a start state then we have:

qi = αji * qj + ε

5. Similarly, compute the final state which ultimately gives the regular expression 'r'.

Example:
Construct the regular expression for the given DFA

Solution:
Let us write down the equations

q1 = q1 0 + ε

Since q1 is the start state, so ε will be added, and the input 0 is coming to q1 from q1
hence we write
State = source state of input × input coming to it

Similarly,

q2 = q1 1 + q2 1
q3 = q2 0 + q3 (0+1)

Since the final states are q1 and q2, we are interested in solving q1 and q2 only. Let us
see q1 first

q1 = q1 0 + ε

We can re-write it as

q1 = ε + q1 0

Which is similar to R = Q + RP, and gets reduced to R = OP*.

Assuming R = q1, Q = ε, P = 0

We get

q1 = ε.(0)*
q1 = 0* (ε.R*= R*)

Substituting the value into q2, we will get

q2 = 0* 1 + q2 1
q2 = 0* 1 (1)* (R = Q + RP → Q P*)

The regular expression is given by

r = q1 + q2
= 0* + 0* 1.1*
r = 0* + 0* 1+ (1.1* = 1+)
Moore Machine
Moore machine is a finite state machine in which the next state is decided by the
current state and current input symbol. The output symbol at a given time depends
only on the present state of the machine. Moore machine can be described by 6 tuples
(Q, q0, ∑, O, δ, λ) where,

1. Q: finite set of states  
2. q0: initial state of machine  
3. ∑: finite set of input symbols  
4. O: output alphabet  
5. δ: transition function where Q × ∑ → Q  
6. λ: output function where Q → O  

Example 1:
The state diagram for Moore Machine is

Transition table for Moore Machine is:


In the above Moore machine, the output is represented with each input state separated
by /. The output length for a Moore machine is greater than input by 1.

Input: 010

Transition: δ (q0,0) => δ(q1,1) => δ(q1,0) => q2

Output: 1110(1 for q0, 1 for q1, again 1 for q1, 0 for q2)

Example 2:
Design a Moore machine to generate 1's complement of a given binary number.

Solution: To generate 1's complement of a given binary number the simple logic is
that if the input is 0 then the output will be 1 and if the input is 1 then the output will
be 0. That means there are three states. One state is start state. The second state is
for taking 0's as input and produces output as 1. The third state is for taking 1's as
input and producing output as 0.

Hence the Moore machine will be,


For instance, take one binary number 1011 then

Input 1 0 1 1

State q0 q2 q1 q2 q2

Output 0 0 1 0 0

Thus we get 00100 as 1's complement of 1011, we can neglect the initial 0 and the
output which we get is 0100 which is 1's complement of 1011. The transaction table is
as follows:
Thus Moore machine M = (Q, q0, ∑, O, δ, λ); where Q = {q0, q1, q2}, ∑ = {0, 1}, O =
{0, 1}. the transition table shows the δ and λ functions.

Example 3:
Design a Moore machine for a binary input sequence such that if it has a substring 101,
the machine output A, if the input has substring 110, it outputs B otherwise it outputs
C.

Solution: For designing such a machine, we will check two conditions, and those are
101 and 110. If we get 101, the output will be A, and if we recognize 110, the output
will be B. For other strings, the output will be C.

The partial diagram will be:


Now we will insert the possibilities of 0's and 1's for each state. Thus the Moore
machine becomes:

Example 4:
Construct a Moore machine that determines whether an input string contains an even
or odd number of 1's. The machine should give 1 as output if an even number of 1's are
in the string and 0 otherwise.
Solution:

The Moore machine will be:

This is the required Moore machine. In this machine, state q1 accepts an odd number of
1's and state q0 accepts even number of 1's. There is no restriction on a number of
zeros. Hence for 0 input, self-loop can be applied on both the states.

Example 5:
Design a Moore machine with the input alphabet {0, 1} and output alphabet {Y, N}
which produces Y as output if input sequence contains 1010 as a substring otherwise, it
produces N as output.

Solution:

The Moore machine will be:

Mealy Machine
A Mealy machine is a machine in which output symbol depends upon the present input
symbol and present state of the machine. In the Mealy machine, the output is
represented with each input symbol for each state separated by /. The Mealy machine
can be described by 6 tuples (Q, q0, ∑, O, δ, λ') where

1. Q: finite set of states  
2. q0: initial state of machine  
3. ∑: finite set of input alphabet  
4. O: output alphabet  
5. δ: transition function where Q × ∑ → Q  
6. λ': output function where Q × ∑ →O  

Example 1:
Design a Mealy machine for a binary input sequence such that if it has a substring 101,
the machine output A, if the input has substring 110, it outputs B otherwise it outputs
C.

Solution: For designing such a machine, we will check two conditions, and those are
101 and 110. If we get 101, the output will be A. If we recognize 110, the output will
be B. For other strings the output will be C.

The partial diagram will be:

Now we will insert the possibilities of 0's and 1's for each state. Thus the Mealy machine
becomes:
Example 2:
Design a mealy machine that scans sequence of input of 0 and 1 and generates output
'A' if the input string terminates in 00, output 'B' if the string terminates in 11, and
output 'C' otherwise.

Solution: The mealy machine will be:

Conversio
n from Mealy machine to Moore Machine
In Moore machine, the output is associated with every state, and in Mealy machine, the
output is given along the edge with input symbol. To convert Moore machine to Mealy
machine, state output symbols are distributed to input symbol paths. But while
converting the Mealy machine to Moore machine, we will create a separate state for
every new output symbol and according to incoming and outgoing edges are
distributed.

The following steps are used for converting Mealy machine to the Moore machine:

Step 1: For each state(Qi), calculate the number of different outputs that are available
in the transition table of the Mealy machine.

Step 2: Copy state Qi, if all the outputs of Qi are the same. Break qi into n states as
Qin, if it has n distinct outputs where n = 0, 1, 2....

Step 3: If the output of initial state is 0, insert a new initial state at the starting which
gives 1 output.

Example 1:
Convert the following Mealy machine into equivalent Moore machine.

Solution:

Transition table for above Mealy machine is as follows:


o For state q1, there is only one incident edge with output 0. So, we don't need to
split this state in Moore machine.
o For state q2, there is 2 incident edge with output 0 and 1. So, we will split this
state into two states q20( state with output 0) and q21(with output 1).
o For state q3, there is 2 incident edge with output 0 and 1. So, we will split this
state into two states q30( state with output 0) and q31( state with output 1).
o For state q4, there is only one incident edge with output 0. So, we don't need to
split this state in Moore machine.

Transition table for Moore machine will be:


Transition diagram for Moore machine will be:
Example 2:
Convert the following Mealy machine into equivalent Moore machine.

Solution:

Transition table for above Mealy machine is as follows:


The state q1 has only one output. The state q2 and q3 have both output 0 and 1. So we
will create two states for these states. For q2, two states will be q20(with output 0) and
q21(with output 1). Similarly, for q3 two states will be q30(with output 0) and q31(with
output 1).

Transition table for Moore machine will be:

Transition diagram for Moore machine will be:

Con
version from Moore machine to Mealy Machine
In the Moore machine, the output is associated with every state, and in the mealy
machine, the output is given along the edge with input symbol. The equivalence of the
Moore machine and Mealy machine means both the machines generate the same output
string for same input string.

We cannot directly convert Moore machine to its equivalent Mealy machine because the
length of the Moore machine is one longer than the Mealy machine for the given input.
To convert Moore machine to Mealy machine, state output symbols are distributed into
input symbol paths. We are going to use the following method to convert the Moore
machine to Mealy machine.

Method for conversion of Moore machine to Mealy


machine
Let M = (Q, ∑, δ, λ, q0) be a Moore machine. The equivalent Mealy machine can be
represented by M' = (Q, ∑, δ, λ', q0). The output function λ' can be obtained as:

1. λ' (q, a) = λ(δ(q, a))  

Example 1:
Convert the following Moore machine into its equivalent Mealy machine.

Solution:

The transition table of given Moore machine is as follows:

Q a b Output(λ)

q0 q0 q1 0

q1 q0 q1 1
The equivalent Mealy machine can be obtained as follows:

1. λ' (q0, a) = λ(δ(q0, a))  
2.                 = λ(q0)  
3.                 = 0  
4.   
5. λ' (q0, b) = λ(δ(q0, b))  
6.                 = λ(q1)  
7.                 = 1  

The λ for state q1 is as follows:

1. λ' (q1, a) = λ(δ(q1, a))  
2.                 = λ(q0)  
3.                 = 0  
4.   
5. λ' (q1, b) = λ(δ(q1, b))  
6.                 = λ(q1)  
7.                 = 1  

Hence the transition table for the Mealy machine can be drawn as follows:

The equivalent Mealy machine will be,


Note: The length of output sequence is 'n+1' in Moore machine and is 'n' in the Mealy
machine.

Example 2:
Convert the given Moore machine into its equivalent Mealy machine.

Solution:

The transition table of given Moore machine is as follows:

Q a b Output(λ)

q0 q1 q0 0

q1 q1 q2 0

q2 q1 q0 1

The equivalent Mealy machine can be obtained as follows:


1. λ' (q0, a) = λ(δ(q0, a))  
2.                 = λ(q1)  
3.                 = 0  
4.   
5. λ' (q0, b) = λ(δ(q0, b))  
6.                 = λ(q0)  
7.                 = 0  

The λ for state q1 is as follows:

1. λ' (q1, a) = λ(δ(q1, a))  
2.                 = λ(q1)  
3.                 = 0  
4.   
5. λ' (q1, b) = λ(δ(q1, b))  
6.                 = λ(q2)  
7.                 = 1  

The λ for state q2 is as follows:

1. λ' (q2, a) = λ(δ(q2, a))  
2.                 = λ(q1)  
3.                 = 0  
4.   
5. λ' (q2, b) = λ(δ(q2, b))  
6.                 = λ(q0)  
7.                 = 0  

Hence the transition table for the Mealy machine can be drawn as follows:
The equivalent Mealy machine will be,

Example 3:
Convert the given Moore machine into its equivalent Mealy machine.

a b Output(λ)

q0 q0 q1 0

q1 q2 q0 1

q2 q1 q2 2
Solution:

The transaction diagram for the given problem can be drawn as:

The equivalent Mealy machine can be obtained as follows:

1. λ' (q0, a) = λ(δ(q0, a))  
2.                 = λ(q0)  
3.                 = 0  
4.   
5. λ' (q0, b) = λ(δ(q0, b))  
6.                 = λ(q1)  
7.                 = 1  

The λ for state q1 is as follows:

1. λ' (q1, a) = λ(δ(q1, a))  
2.                 = λ(q2)  
3.                 = 2  
4.   
5. λ' (q1, b) = λ(δ(q1, b))  
6.                 = λ(q0)  
7.                 = 0  

The λ for state q2 is as follows:

1. λ' (q2, a) = λ(δ(q2, a))  
2.                 = λ(q1)  
3.                 = 1  
4.   
5. λ' (q2, b) = λ(δ(q2, b))  
6.                 = λ(q2)  
7.                 = 2  

Hence the transition table for the Mealy machine can be drawn as follows:

The equivalent Mealy machine will be,

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy