You are on page 1of 38

Regular Expressions

1
Regular Expressions
Regular expressions
describe regular languages

Example: (a + b ⋅ c) *

describes the language


{ a, bc} * = { λ , a, bc, aa, abc, bca,...}

2
Recursive Definition
Primitive regular expressions: ∅, λ , α

Given regular expressions r1 and r2

r1 + r2
r1 ⋅ r2
Are regular expressions
r1 *
( r1 )
3
Examples

A regular expression: ( a + b ⋅ c ) * ⋅(c + ∅)

Not a regular expression: ( a + b +)

4
Languages of Regular Expressions

L( r ) : language of regular expression r

Example
L( (a + b ⋅ c) *) = { λ , a, bc, aa, abc, bca,...}

5
Definition

For primitive regular expressions:

L( ∅ ) = ∅

L( λ ) = { λ }

L( a ) = { a}
6
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 )
7
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,...}
8
Example

Regular expression r = ( a + b ) * ( a + bb )

L( r ) = { a, bb, aa, abb, ba, bbb,...}

9
Example

Regular expression r = ( aa ) * ( bb ) * b

L( r ) = {a b
2n 2m
b : n, m ≥ 0}

10
Example

Regular expression r = (0 + 1) * 00 (0 + 1) *

L(r ) = { all strings with at least


two consecutive 0 }

11
Example

Regular expression r = (1 + 01) * (0 + λ )

L(r ) = { all strings without


two consecutive 0 }

12
Equivalent Regular Expressions

Definition:

Regular expressions r1 and r2

are equivalent if L(r1 ) = L(r2 )

13
Example
L = { all strings without
two consecutive 0 }

r1 = (1 + 01) * (0 + λ )
r2 = (1* 011*) * (0 + λ ) + 1* (0 + λ )

r1 and r2
L(r1 ) = L(r2 ) = L
are equivalent
regular expr.
14
Regular Expressions
and
Regular Languages

15
Theorem

Languages
Generated by
Regular Expressions
= Regular
Languages

16
We will show:

Languages
Generated by ⊆ Regular
Languages
Regular Expressions

Languages
Generated by ⊇ Regular
Languages
Regular Expressions

17
Proof - Part 1

Languages
Generated by ⊆ Regular
Languages
Regular Expressions

For any regular expression r


the language L (r ) is regular

Proof by induction on the size of r


18
Induction Basis
Primitive Regular Expressions: ∅, λ , α
NFAs

L( M1 ) = ∅ = L(∅)

regular
L( M 2 ) = {λ} = L(λ )
languages
a
L( M 3 ) = {a} = L(a )

19
Inductive Hypothesis

Assume
for regular expressions r1 and r2
that
L(r1 ) and L(r2 ) are regular languages

20
Inductive Step
We will prove:
L( r1 + r2 )

L( r1 ⋅ r2 )
Are regular
Languages
L( r1 *)

L( ( r1 ) )
21
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 )
22
By inductive hypothesis we know:
L(r1 ) and L(r2 ) are regular languages

We also know:
Regular languages are closed under:
Union L( r1 ) ∪ L( r2 )
Concatenation L( r1 ) L( r2 )
Star ( L( r1 ) ) *
23
Therefore:

L( r1 + r2 ) = L( r1 ) ∪ L( r2 )

Are regular
L( r1 ⋅ r2 ) = L( r1 ) L( r2 )
languages

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

24
And trivially:

L((r1 )) is a regular language

25
Proof - Part 2

Languages
Generated by ⊇ Regular
Languages
Regular Expressions

For any regular language L there is


a regular expression r with L ( r ) = L

Proof by construction of regular expression


26
Standard Representations
of Regular Languages

Regular Languages

FAs

Regular
NFAs
Expressions

27
When we say: We are given
a Regular Language L

We mean: Language L is in a standard


representation

28
Elementary Questions

about

Regular Languages

29
Membership Question
Question: Given regular language L
and string w
how can we check if w ∈ L?

Answer: Take the DFA that accepts L


and check if w is accepted

30
DFA
w
w∈ L

DFA
w
w∉ L

31
Question: Given regular language L
how can we check
if L is empty: ( L = ∅) ?

Answer: Take the DFA that accepts L

Check if there is any path from


the initial state to a final state
32
DFA

L≠∅

DFA

L=∅

33
Question: Given regular language L
how can we check
if L is finite?

Answer: Take the DFA that accepts L

Check if there is a walk with cycle


from the initial state to a final state
34
DFA

L is infinite

DFA

L is finite

35
Question: Given regular languages L1 and L2
how can we check if L1 = L2 ?

Answer: Find if ( L1 ∩ L2 ) ∪ ( L1 ∩ L2 ) = ∅

36
( L1 ∩ L2 ) ∪ ( L1 ∩ L2 ) = ∅

L1 ∩ L2 = ∅ and L1 ∩ L2 = ∅

L1 L2 L L2 L1 L1
2
L1 ⊆ L2 L2 ⊆ L1

L1 = L2
37
( L1 ∩ L2 ) ∪ ( L1 ∩ L2 ) ≠ ∅

L1 ∩ L2 ≠ ∅ or L1 ∩ L2 ≠ ∅

L1 L2 L2 L1

L1 ⊄ L2 L2 ⊄ L1

L1 ≠ L2
38

You might also like