You are on page 1of 28

Chapter 3

REGULAR EXPRESSION

CHAPTER-3 REGULAR LANGUAGES


Regular Expressions
2

Regular expressions were mathematical tool designed to


represent regular languages.

Built from a set of primitives and operations.

This representation involves a combination of strings of symbols


from some alphabet Σ, parentheses and the operators +, ⋅, and *.

A regular expression is obtained from the symbol {a, b, c}, empty


string ∈, and empty-set ∅ perform the operations +, ⋅ and *
(union, concatenation and Kleene star).
Regular Expressions
3

Examples
0 + 1 represents the set {0, 1}
1 represents the set {1}
(0 +1)1 represents the set {01, 11}
(a+b)⋅(b+c) represents the set {ab, bb, ac, bc}
(0 + 1)* = ∈+ (0 + 1) + (0 + 1) (0 + 1) ..........= Σ*
(0 + 1 )+ =(0 +1) (0 +1)*= Σ+ =Σ*- {ε}
Building Regular Expressions
4

Assume that Σ = {a,b,c}


Zero or more: a* means zero or more a’s,

To say zero or more ab’s, i.e.,{λ, ab,abab........,} you need to say (ab)*.

One or more: Since a* means zero or more a’s, you can use aa*
(or equivalently a*a) to mean one or more a’s.

Similarly to describe ‘one or more ab’s”, that is {ab, abab,


ababab, .........}, you can use ab (ab)*.

Zero or one: It can be described as an optional ‘a’ with (a + λ).


Languages defined by Regular Expressions

5
Hierarchy of evaluation of regular expression
6

 We follow the following order when evaluate a regular


expressions;
 Parenthesis

 Kleene Closure
 Concatenation

 Union
Regular Languages
7

 Regular Languages are


Language accepted by a DFA.

 A language 𝐿 is regular if there exists a DFA 𝐷 such


that 𝐿 = 𝐿(𝐷).
Regular Languages
8

   Σ be an alphabet. The class of “regular languages” over Σ is


 Let
defined inductively as follows:
a) ∅ is a regular language
b) For each ∈ Σ, {σ}is a regular language
c) For any natural number n ≥ 2, if L1, L2, L3, .... ,Ln are regular
languages, then so is L1∪L2 ∪......∪ Ln.
d) For any natural number n ≥ 2, if L1 L2,...... Ln are regular
languages, then so is L1. L2. .......Ln.
e) If L is a regular language, then so is L*.
(f) Nothing else is a regular language unless its construction
follows from rules (a) to (e).
Examples
9

i. ∅ is a regular language (by rule (a))

ii. L = {a, ab} is a language over Σ = {a, b}because, both {a} and {b} are

regular languages by rule (b).

{a} o {b}={ab} is a regular language. By rule (d)

{a }∪{ab } = L is a regular language. Using rule (c),

iii. The language over the alphabet {0,1} where strings contain an even

number of 0’s can be constructed by

(1*((01*)(01*))*) or simply 1*(01* 01*)*.


Exercises
10

Solution
s:
Regular Expressions to NFA
11

For any x in Σ, the regular expression denotes the language {x}.


The NFA (with a single start state and a single final state) as
shown below, represents exactly that language.

The regular expression λ denotes the language {λ}that is the


language containing only the empty string.
Regular Expressions to NFA
12

The regular expression ∅ denotes the language ∅; no


strings belong to this language, not even the empty
string.

For juxtaposition, strings in L(r1) followed by strings in


L(r2) , we chain the NFAs together
Regular Expressions to NFA
13

The “+” denotes “or” in a regular expression, we would use


an NFA with a choice of paths.

The star (*) denotes zero or more applications of the regular

expression, hence a loop has to be set up in the NFA.


Example :Construct An NFA for the regular expression (0+1)*.
14

 Solution: NFA is constructed step by step by breaking the RE.


 r3= (r1+r2)
 r=r3*, where r1=0, r2=1
Example
15

Represent the following sets by regular expression


a.{∧, ab}
b.{1,11,111....}
c.{ab, a, b, bb}
Solution
d.The set {∧, ab} is represented by the regular expression ∧ + ab
e.The set{1, 11,111,....,}is got by concatenating 1 and any element
of {1}*.
Therefore 1(1)* represent the given set.
f.The set {ab, a, b, bb} represents the regular expression
ab+ a+ b +bb.
Exercise
16

1. Obtain the regular expressions for the following sets:


1.1. The set of all strings over {a, b} beginning and ending with ‘a’.
ÞThe regular expression for ‘the set of all strings over {a, b}
beginning and ending with ‘a’ is given by: a (a + b)*a

1.2. {b2, b5, b8,. . . . .}


ÞThe regular expression for {b 2 , b 5 , b 8 , ............} is given by:
bb (bbb)*

1.3. {a2n+1 |n > 0}


ÞThe regular expression for {a 2n+1 |n > 0}is given by:

a (aa)+
FINITE AUTOMATA WITH OUTPUT
17

A finite-state
 machine M = (Q , Σ, δ , λ, O , q0 ) consists of

a
 finite set Q of states,
a
 finite input alphabet Σ,

a
 finite output alphabet O,
a
 transition function δ that assigns to each state and input pair of a new
state,
an output function λ that assigns to each state and input pair an output, and
an
 initial state q0.
There are two types of finite state machines that generate
output −
 Mealy Machine
 Moore Machine
 Mealy Machine
 A Mealy Machine is an FSM whose output depends on the
present state as well as the present input.
 It can be described by a 6 tuple (Q, ∑, O, δ, X, q0)
M = (Q , O , ,Σ , δ , λ , q0 ) consists of
 A finite set Q of states,
 A finite input alphabet Σ,
 A finite output alphabet O,
 A transition function δ that assigns to each
state and input pair a new state,
 An output function X that assigns to each
state and input pair an output, and
 An initial state q0.
18
Examples
19

The above Mealy Machine outputs a value for each input. You can
tell input /output.
So for the following input:
000101010
It outputs
000010101
Moore Machine
20

 Moore machine is an FSM whose outputs depend on only the


present state.
 A Moore machine can be described by a 6 tuple(Q, ∑, O, δ, X, q0)
Current State Input Next State Output

A 0 D 0
A 1 B 0
B 0 E 0
B 1 C 0
C 0 F 0
C 1 C 0
D 0 G 0
D 1 E 0
E 0 H 0
E 1 F 0
F 0 I 1
F 1 F 0
G 0 G 0
G 1 H 0
H 0 H 0
H 1 I 1
I 0 I 1
I 1 I 1
Fig Example Moore machine

21
PROPERTIES OF REGULAR SETS (LANGUAGES)
22

There
 are several operations defined on languages:

L1 ∪ L2: strings in either L1 or L2.

L1 ∩ L2 : strings in both L1 and L2.

L1L2 : strings composed of one string from L1, followed by one

string from L2.

L1*: Zero or more strings from L1 concatenated together

L1-L2 : strings in L1 that are not in L2.

L1R : strings in L1, reversed.


Applying the Pumping Lemma
23
Theorem
Let L be a regular language. Then there exists a constant ‘c’ such that for every
string w in L −
|w| ≥ c
We can break w into three strings, 
w = xyz, such that −
|y| > 0
|xy| ≤ c

For all k ≥ 0, the string xykz is also in L.


The “pumping lemma” for regular languages is another way of showing that a given
infinite language is not regular.
Applications of Pumping
24
Lemma
 Method to prove that a language L is not regular

 At first, we have to assume that L is regular.

 So, the pumping lemma should hold for L.

 Use the pumping lemma to obtain a contradiction −

 Select w such that |w| ≥ c
 Select y such that |y| ≥ 1
 Select x such that |xy| ≤ c
 Assign the remaining string to z.
 Select k such that the resulting string is not in L.
 Hence L is not regular.
Problem

25

Prove that L = {aibi | i ≥ 0} is not regular.


Solution: At first, assume that L is regular and n is the number of states.
Let w = anbn. Thus |w| = 2n ≥ n.
By pumping lemma, let w = xyz, where |xy|≤ n.

Let x = ap, y = aq, and z = arbn, where p + q + r = n.

p ≠ 0, q ≠ 0, r ≠ 0. Thus |y|≠ 0
Let k = 2. Then xy2z = apa2qarbn.
Number of as = (p + 2q + r) = (p + q + r) + q = n + q
Hence, xy2z = an+q bn. Since q ≠ 0, xy2z is not of the form anbn.
Thus, xy2z is not in L. Hence L is not regular.
Complement of a DFA

26

 If (Q, ∑, δ, q0, F) be a DFA that accepts a language L, then the

complement of the DFA can be obtained by swapping its


accepting states with its non-accepting states and vice versa.
 We will take an example and elaborate this below −

This DFA accepts the language


L = {a, aa, aaa , ............. }
over the alphabet
∑ = {a, b}
So, RE = a+.
Con..
27

Now we will swap its accepting states with its non-accepting states
and vice versa and will get the following −

This DFA accepts the language


Ľ = { ε , b, ab ,bb,ba, ............... } over the alphabet
∑ = {a, b}
Exercise
28

1. Let Σ = {a, b}.


 Let L = { w ∈ Σ* | w contains at most one a }.
 Which of the following is a regular expression for L?
A. Σ*aΣ*
B. b*ab* ∪ b*
C. b*(a ∪ ε)b*
D. b*a*b* ∪ b*
E. b*(a* ∪ ε)b*
F. None of the above, or two or more of the above.
2. Construct An NFA for the regular expression 1(11)*.

You might also like