You are on page 1of 43

PRESENTED BY

SHRADDHA GUPTA
SOPAN SHUKLA
2ND C.S.E.
REGULAR EXPRESSION

Regular expression can define exactly the same languages that the
various forms of automata describe: the regular languages.
Regular expression offer something that automata do not: a
declarative way to express the strings, we want to accept.

The set of regular expressions is defined by the following rules:


Let be a given alphabet. Then
1. , and a are all regular expressions.These are called
primitive regular expressions.
2. If r1 and r2 are regular expressions, so are r1+r2, r1.r2 , r1* and (r1).
3. A string is a regular expression if and only if it can be derived from
the primitive regular expressions by a finite number applications of the
rules in 2.
OPERATORS OF REGULAR EXPRESSION

1. UNION : L1 U L2
It denotes the set of string that are in either L1 or L2 or both.
L1={001,10,111) and L2 ={ ,001} then L1 U L2 = { ,10,001,111}.
2. CONCATENATION: L1.L2
It is a set of strings that can be formed by taking any string in L1
and concatenating it with any strings in L2. It is denoted by ‘.’.
3. STAR: L*
It represents a set of those strings that can be formed by taking
any number of strings from L, possibly with repetitions and
concatenating all of them.
Note : L+ represents the positive closure.
EXAMPLE: L(a*.(a+b))

If r1 and r2 are regular expressions, then


2. L(r1+r2) = L(r1) U L(r2)
3. L(r1.r2) = L(r1).L(r2)
4. L(r1*) = (L(r1))*

L(a*.(a+b)) = L(a*).L(a+b)
(L(a))*.(L(a) U L(b))
{ ,a,aa,aaa, …}{a,b}
{a,aa,aaa,…,b,ab,aab,…}

r1 = (a+b)*(a+bb)
L(r1) = {a,bb,aa,abb,ba,bbb,…}
L(r1) is the set of all strings on {a,b}, terminated by either an a or a bb.
CONSTRUCTION OF FA FOR REGULAR EXPRESSIONS

Regular Regular Set Finite


Expression Automata
{}
{ }
a
a {a}
(r1+r2) R1 U R2

(r1.r2) R1R2
r* R1
Regular language L1 Regular language L2

L( M1 ) = L1 L( M 2 ) = L2

NFA M1 NFA M2

Single accepting state Single accepting state


Example
M1
n≥0
a
n
L1 = {a b} b

M2
a
L2 = { ba} b
Union
• NFA for L1 ∪ L2
M1

λ M2
Example
n
NFA for L1 ∪ L2 = {a b} ∪ {ba}
n
• L1 = {a b}
a
b
λ

λ L2 = {ba}
b a
Concatenation
• NFA for L1L2

M1 M2
λ λ
Example

n n
• NFA for L1L2 = {a b}{ba} = {a bba}

n
L1 = {a b}
a L2 = {ba}
b λ b a λ
Star Operation
• NFA for L1 *
λ
λ ∈ L1 *
M1
λ λ

λ
Example
n w = w1w2  wk
• NFA for L1* = {a b} * wi ∈ L1
λ
n
L1 = {a b}
a
λ b λ

λ
ALGEBRAIC LAWS
• Associativity and Commutativity
(P+Q)+R = P+(Q+R)
P+Q = Q+P
• Identity Law
+L =L+ =L
L = L =L
• Distributed Law
x * (y+z) = x*y + x*z
• Idempotent Law
L+L = L; L*L = L
Union Example

EX: Draw the DFA forx has even length or


L = { x ∈ {0,1}* | ends with 11 }

The solution involved making a table of states


with rows keeping track of parity, and
columns keeping track of the progress
towards achieving the 11 pattern:
Union Example
suffix
ε 1 11
length

0 mod 2 0
1

0 0 0 1 1 1
1
1 mod 2 1
0
0
Union Example
We could have arrived at this result
methodically by noticing that L is the union
of two easier languages:
L = L1 ∪ L2
L1 = { x ∈ {0,1}* | x has even length}
L2 = { x ∈ {0,1}* | x ends with 11 }
Union Example --L1
L1 = { x ∈ {0,1}* | x has even length}

0,1 0,1
Union Example –L2
L2 = { x ∈ {0,1}* | x ends with 11 }
1
1
0

0 1

0
Union Example –L1∪L2
Which together form the union, using the
Cartesian Product Construction:

1
0
0 0 0 1 1 1
1
1
0
0
Union Example
Upshot: To tell if string is in union, just kept track of active
states in both automata. At end, string is in union if it is
accepted by at least one automaton. It would be nice if
could keep track of both sets of states in a single
automaton. 1
1
0

0 1
0,1 0,1
0
Union Example
1. Assign labels to the states in the two
machines

1
1
0
a x y z
0 1
0,1 0,1
0
b
Union Example
2. Keep track of states of both machines by
using spliced states.

1
1
0
a x y z
0 1
0,1 0,1
0
b
Union Example
Current spliced state: (a,x)

1
1
0
a x y z
0 1
0,1 0,1
0
b
1 1 1 0 1 0 1
Union Example
Current spliced state: (b,y)

1
1
0
a x y z
0 1
0,1 0,1
0
b
1 1 1 0 1 0 1
Union Example
Current spliced state: (a,z)

1
1
0
a x y z
0 1
0,1 0,1
0
b
1 1 1 0 1 0 1
Union Example
Current spliced state: (b,z)

1
1
0
a x y z
0 1
0,1 0,1
0
b
1 1 1 0 1 0 1
Union Example
Current spliced state: (a,x)

1
1
0
a x y z
0 1
0,1 0,1
0
b
1 1 1 0 1 0 1
Union Example
Current spliced state: (b,y)

1
1
0
a x y z
0 1
0,1 0,1
0
b
1 1 1 0 1 0 1
Union Example
Current spliced state: (a,z)

1
1
0
a x y z
0 1
0,1 0,1
0
b
1 1 1 0 1 0 1
Union Example
Current spliced state: (b,y)

Rejected! 1
1
0
a x y z
0 1
0,1 0,1
0
b

Rejected! 1 1 1 0 1 0 1
Union Example
Q: What splice states should be
accepting states?

1
1
0
a x y z
0 1
0,1 0,1
0
b
Union Example –Accept States
A: Any splices that included at least
one accept state.
F = {(a,x),(a,y),(a,z),(b,z)}
1
1
0
a x y z
0 1
0,1 0,1
0
b
Back to our Example1
1
0
a x y z
0 1
0,1 0,1

b 0

(a,x) (a,y) 1 (a,z)


Unioner 0
Construction: 0 0 0 1 1 1
1
(b,x) 1 (b,y) (b,z)
0
0
MISCELLANEOUS EXAMPLE

Q. Construct a finite automata


equivalent to the regular expression.
(0+1)*(00+11)(0+1)*
Answer:
(0+1)*(00+11)(0+1)*
q1
q0
• First we draw the state transition as above:
• Then in step 2 we eliminate the concatenations
by introducing new vertices q2 and q3.
• Inter-mediate vertices is shown :

(0+1)*
(00+11)
q0 (0+1)* q1
q2 q3
• Now we eliminate * operation by introducing
two new vertices q5 and q6 and different
moves are shown.
(0+1)

q5 q3 (0+1)
(00+11)

q0 q1
q2
q6
• Now we eliminate concatenations and +
(where concatenation is by different states and
+ i.e.0+1 by (0,1).)

0,1 0
q7 0 0,1

q0 q5 q2 q3 q6 q1
1 q8 1
• After reducing the above operated constructions we
get FA equivalent to (0+1)*(00+11)(0+1)*.
• Here we are using rules:
• For reductions.. 1.Find all edges starting from V2.
• 2.Duplicate all edges starting from V1without
changing the edge labels.
• 3.If V1 is initial state make V2 also initial state and if
V2 final state make V1 also final state.
• Above is construction of FA equivalent to
(0+1)*(00+11)(0+1)*.
• After making the transition and successor
table we get the required DFA.
• This is the required DFA.

1
0
[q0,q9] [q0,q9,q1]
0
0 1 0 1
[q0] 0
1 [q0,q10] [q0,q10,q1]
1
Conclusion
We can now say that regular expressions are
very useful for representing certain sets of
strings in an algebraic fashion. Actually
these describe the languages accepted by
finite state automata. And the operations
used in the expressions are the only tools
for doing that without which it is not
possible.
Thank you.

You might also like