Regular Expressions

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 35

Regular Expressions

Regular Expressions
Regular expressions
describe regular languages

Example:

(a b c) *
describes the language

a, bc * , a, bc, aa, abc, bca,...

Recursive Definition
Primitive regular expressions:
Given regular expressions

r1

, ,
and

r2

r1 r2
r1 r2
r1 *

r1

Are regular expressions

Examples
A regular expression:

a b c * (c )

Not a regular expression:

a b

Languages of Regular Expressions

L r

: language of regular expression

Example

L (a b c) * , a, bc, aa, abc, bca,...

Definition
For primitive regular expressions:

L
L
L a a

Definition (continued)
For regular expressions r1 and r2

L r1 r2 L r1 L r2
L r1 r2 L r1 L r2
L r1 * L r1 *
L r1 L r1

Example

Regular expression: a b a *

L a b a * L a b L a *
L a b L a *
L a L b L a *
a b a *
a, b , a, aa, aaa,...
a, aa, aaa,..., b, ba, baa,...

Example
Regular expression

r a b * a bb

L r a, bb, aa, abb, ba, bbb,...

Example
Regular expression

L r {a b

r aa * bb * b

2n 2m

b : n, m 0}

Example
Regular expression

r (0 1) * 00 (0 1) *

L(r ) = { all strings containing substring 00 }

Example
Regular expression

r (1 01) * (0 )

L(r ) = { all strings without substring 00 }

Equivalent Regular Expressions


Definition:
Regular expressions
are equivalent if

r1

and

r2

L(r1 ) L(r2 )

Example

= { all strings without substring 00 }

r1 (1 01) * (0 )
r2 (1* 011*) * (0 ) 1* (0 )
L(r1 ) L(r2 ) L

r1

and r2
are equivalent
regular expressions

Regular Expressions
and
Regular Languages

Theorem
Languages
Generated by
Regular Expressions

Regular
Languages

Proof:
Languages
Generated by
Regular Expressions

Regular
Languages

Languages
Generated by
Regular Expressions

Regular
Languages

Proof - Part 1
Languages
Generated by
Regular Expressions

Regular
Languages

For any regular expression r


the language L (r ) is regular
Proof by induction on the size of

Induction Basis
Primitive Regular Expressions:
Corresponding
NFAs

, ,

L( M1 ) L()

L( M 2 ) {} L( )
a

L( M 3 ) {a} L(a )

regular
languages

Inductive Hypothesis
Suppose
that for regular expressions r1 and r2 ,
L(r1 ) and L(r2 ) are regular languages

Inductive Step
We will prove:

L r1 r2
L r1 r2
L r1 *
L r1

Are regular
Languages

By definition of regular expressions:

L r1 r2 L r1 L r2
L r1 r2 L r1 L r2
L r1 * L r1 *
L r1 L r1

By inductive hypothesis we know:


L(r1 ) and L(r2 ) are regular languages

We also know:
Regular languages are closed under:

Union
Concatenation
Star

L r1 L r2
L r1 L r2

L r1 *

Therefore:

L r1 r2 L r1 L r2
L r1 r2 L r1 L r2

Are regular
languages

L r1 * L r1 *
L((r1 )) L(r1 )

is trivially a regular language


(by induction hypothesis)
End of Proof-Part 1

Using the regular closure of these operations,


we can construct recursively the NFA M
that accepts L(M ) L(r )
Example: r r1 r2

L(M1 ) L(r1 )

L(M2 ) L(r2 )

L(M ) L(r )

Proof - Part 2
Languages
Generated by
Regular Expressions

Regular
Languages

For any regular language L there is


a regular expression r with L ( r )
We will convert an NFA that accepts
to a regular expression

Since L is regular, there is a


NFA M that accepts it

L( M ) L

Take it with a single final state

From M construct the equivalent


Generalized Transition Graph
in which transition labels are regular expressions

Example:

Corresponding
Generalized transition graph

M
a

c
a, b

c
ab

Another Example:

q0

q1 a, b

q2

Transition labels
are regular
expressions

q0

a
b

q1 a b q2

Reducing the states:

q0

q1 a b q2

Transition labels
are regular
expressions

bb * a
q0

b
bb * (a b)

q2

Resulting Regular Expression:

bb * a
q0

b
bb * (a b)

q2

r (bb * a ) * bb * (a b)b *
L(r ) L( M ) L

In General
e
Removing a state:
d
qi

c
qj

q
a

ae * d

ce * b
ce * d

qi

ae * b

qj

By repeating the process until


two states are left, the resulting graph is
Initial graph

Resulting graph

r1

q0

r3

r2

r4
qf

The resulting regular expression:

r r1 * r2 (r4 r3r1 * r2 ) *
L(r ) L( M ) L
End of Proof-Part 2

Standard Representations
of Regular Languages
Regular Languages

DFAs
NFAs

Regular
Expressions

When we say:

We mean:

We are given
a Regular Language

Language L is in a standard
representation

(DFA, NFA, or Regular Expression)

You might also like