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

Module 2flat

Regular expressions are used to describe patterns in strings and define languages. They allow operations like union, intersection, and Kleen closure to combine patterns. A regular expression can be converted to a finite automaton using the subset construction method, which involves designing an NFA with epsilon moves, removing epsilon moves, and determinizing the resulting NFA. Deterministic finite automata (DFAs) uniquely transition between states based on input symbols and are used in applications like lexical analysis in compilers.

Uploaded by

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

Module 2flat

Regular expressions are used to describe patterns in strings and define languages. They allow operations like union, intersection, and Kleen closure to combine patterns. A regular expression can be converted to a finite automaton using the subset construction method, which involves designing an NFA with epsilon moves, removing epsilon moves, and determinizing the resulting NFA. Deterministic finite automata (DFAs) uniquely transition between states based on input symbols and are used in applications like lexical analysis in compilers.

Uploaded by

Priya Rana
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 26

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.

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)*

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 ϕ ϕ

The equivalent DFA will be:

State 0 1

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

[q1] [qf] ϕ

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

[q1, q2] [qf] [qf]

*[qf] ϕ ϕ

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:
DFA (Deterministic finite automata)
o DFA refers to deterministic finite automata. Deterministic refers to the uniqueness
of the computation. The finite automata are called deterministic finite automata if
the machine is read an input string one symbol at a time.
o In DFA, there is only one path for specific input from the current state to the next
state.
o DFA does not accept the null move, i.e., the DFA cannot change state without any
input character.
o DFA can contain multiple final states. It is used in Lexical Analysis in Compiler.

In the following diagram, we can see that from state q0 for input a, there is only one
path which is going to q1. Similarly, from q0, there is only one path for input b going to
q2.

Formal Definition of DFA


A DFA is a collection of 5-tuples same as we described in the definition of FA.

1. Q: finite set of states


2. ∑: finite set of the input symbol
3. q0: initial state
4. F: final state
5. δ: Transition function

Transition function can be defined as:

1. δ: Q x ∑→Q

Graphical Representation of DFA


A DFA can be represented by digraphs called state diagram. In which:

1. The state is represented by vertices.


2. The arc labeled with an input character show the transitions.
3. The initial state is marked with an arrow.
4. The final state is denoted by a double circle.

Example 1:

1. Q = {q0, q1, q2}


2. ∑ = {0, 1}
3. q0 = {q0}
4. F = {q2}

Solution:

Transition Diagram:

Transition Table:

Present State Next state for Input 0 Next State of Input 1

→q0 q0 q1
q1 q2 q1

*q2 q2 q2

Example 2:
DFA with ∑ = {0, 1} accepts all starting with 0.

Solution:

Explanation:

o In the above diagram, we can see that on given 0 as input to DFA in state q0 the
DFA changes state to q1 and always go to final state q1 on starting input 0. It can
accept 00, 01, 000, 001....etc. It can't accept any string which starts with 1, because
it will never go to final state on a string starting with 1.

Example 3:
DFA with ∑ = {0, 1} accepts all ending with 0.

Solution:

Explanation:
In the above diagram, we can see that on given 0 as input to DFA in state q0, the DFA
changes state to q1. It can accept any string which ends with 0 like 00, 10, 110, 100....etc.
It can't accept any string which ends with 1, because it will never go to the final state q1
on 1 input, so the string ending with 1, will not be accepted or will be rejected.

Example 1:
Design a FA with ∑ = {0, 1} accepts those string which starts with 1 and ends with 0.

Solution:

The FA will have a start state q0 from which only the edge with input 1 will go to the
next state.

In state q1, if we read 1, we will be in state q1, but if we read 0 at state q1, we will reach
to state q2 which is the final state. In state q2, if we read either 0 or 1, we will go to q2
state or q1 state respectively. Note that if the input ends with 0, it will be in the final
state.

Example 2:
Design a FA with ∑ = {0, 1} accepts the only input 101.

Solution:

In the given solution, we can see that only input 101 will be accepted. Hence, for input
101, there is no other path shown for other input.

Example 3:
Design FA with ∑ = {0, 1} accepts even number of 0's and even number of 1's.

Solution:

This FA will consider four different stages for input 0 and input 1. The stages could be:

Here q0 is a start state and the final state also. Note carefully that a symmetry of 0's and
1's is maintained. We can associate meanings to each state as:

q0: state of even number of 0's and even number of 1's.


q1: state of odd number of 0's and even number of 1's.
q2: state of odd number of 0's and odd number of 1's.
q3: state of even number of 0's and odd number of 1's.

Example 4:
Design FA with ∑ = {0, 1} accepts the set of all strings with three consecutive 0's.

Solution:
The strings that will be generated for this particular languages are 000, 0001, 1000,
10001, .... in which 0 always appears in a clump of 3. The transition graph is as follows:

Note that the sequence of triple zeros is maintained to reach the final state.

Example 5:
Design a DFA L(M) = {w | w ε {0, 1}*} and W is a string that does not contain consecutive
1's.

Solution:

When three consecutive 1's occur the DFA will be:

Here two consecutive 1's or single 1 is acceptable, hence

The stages q0, q1, q2 are the final states. The DFA will generate the strings that do not
contain consecutive 1's like 10, 110, 101,..... etc.

Example 6:
Design a FA with ∑ = {0, 1} accepts the strings with an even number of 0's followed by
single 1.
Solution:

The DFA can be shown by a transition diagram as:

NFA (Non-Deterministic finite


automata)
o NFA stands for non-deterministic finite automata. It is easy to construct an NFA
than DFA for a given regular language.
o The finite automata are called NFA when there exist many paths for specific input
from the current state to the next state.
o Every NFA is not DFA, but each NFA can be translated into DFA.
o NFA is defined in the same way as DFA but with the following two exceptions, it
contains multiple next states, and it contains ε transition.

In the following image, we can see that from state q0 for input a, there are two next
states q1 and q2, similarly, from q0 for input b, the next states are q0 and q1. Thus it is
not fixed or determined that with a particular input where to go next. Hence this FA is
called non-deterministic finite automata.
Formal definition of NFA:
NFA also has five states same as DFA, but with different transition function, as shown
follows:

δ: Q x ∑ →2Q

where,

1. Q: finite set of states


2. ∑: finite set of the input symbol
3. q0: initial state
4. F: final state
5. δ: Transition function

Graphical Representation of an NFA


An NFA can be represented by digraphs called state diagram. In which:

1. The state is represented by vertices.


2. The arc labeled with an input character show the transitions.
3. The initial state is marked with an arrow.
4. The final state is denoted by the double circle.

Example 1:

1. Q = {q0, q1, q2}


2. ∑ = {0, 1}
3. q0 = {q0}
4. F = {q2}

Solution:

Transition diagram:

Transition Table:

Present State Next state for Input 0 Next State of Input 1

→q0 q0, q1 q1

q1 q2 q0

*q2 q2 q1, q2

In the above diagram, we can see that when the current state is q0, on input 0, the next
state will be q0 or q1, and on 1 input the next state will be q1. When the current state is
q1, on input 0 the next state will be q2 and on 1 input, the next state will be q0. When
the current state is q2, on 0 input the next state is q2, and on 1 input the next state will
be q1 or q2.

Example 2:
NFA with ∑ = {0, 1} accepts all strings with 01.

Solution:
Transition Table:

Present State Next state for Input 0 Next State of Input 1

→q0 q1 ε

q1 ε q2

*q2 q2 q2

Example 3:
NFA with ∑ = {0, 1} and accept all string of length atleast 2.

Solution:

Transition Table:

Present State Next state for Input 0 Next State of Input 1

→q0 q1 q1

q1 q2 q2

*q2 ε ε

Example 1:
Design a NFA for the transition table as given below:

Present State 0 1

→q0 q0, q1 q0, q2

q1 q3 ε

q2 q2, q3 q3

→q3 q3 q3

Solution:

The transition diagram can be drawn by using the mapping function as given in the
table.
Here,

1. δ(q0, 0) = {q0, q1}


2. δ(q0, 1) = {q0, q2}
3. Then, δ(q1, 0) = {q3}
4. Then, δ(q2, 0) = {q2, q3}
5. δ(q2, 1) = {q3}
6. Then, δ(q3, 0) = {q3}
7. δ(q3, 1) = {q3}

Example 2:
Design an NFA with ∑ = {0, 1} accepts all string ending with 01.

Solution:

Hence, NFA would be:

Example 3:
Design an NFA with ∑ = {0, 1} in which double '1' is followed by double '0'.

Solution:

The FA with double 1 is as follows:


It should be immediately followed by double 0.

Then,

Now before double 1, there can be any string of 0 and 1. Similarly, after double 0, there
can be any string of 0 and 1.

Hence the NFA becomes:

Now considering the string 01100011

1. q0 → q1 → q2 → q3 → q4 → q4 → q4 → q4

Example 4:
Design an NFA in which all the string contain a substring 1110.

Solution:

The language consists of all the string containing substring 1010. The partial transition
diagram can be:
Now as 1010 could be the substring. Hence we will add the inputs 0's and 1's so that the
substring 1010 of the language can be maintained. Hence the NFA becomes:

Transition table for the above transition diagram can be given below:

Present State 0 1

→q1 q1 q1, q2

q2 q3

q3 q4

q4 q5

*q5 q5 q5

Consider a string 111010,

1. δ(q1, 111010) = δ(q1, 1100)


2. = δ(q1, 100)
3. = δ(q2, 00)

Got stuck! As there is no path from q2 for input symbol 0. We can process string 111010
in another way.

1. δ(q1, 111010) = δ(q2, 1100)


2. = δ(q3, 100)
3. = δ(q4, 00)
4. = δ(q5, 0)
5. = δ(q5, ε)
As state q5 is the accept state. We get the complete scanned, and we reached to the
final state.

Example 5:
Design an NFA with ∑ = {0, 1} accepts all string in which the third symbol from the right
end is always 0.

Solution:

Thus we get the third symbol from the right end as '0' always. The NFA can be:

The above image is an NFA because in state q0 with input 0, we can either go to state
q0 or q1

You might also like