You are on page 1of 23

2/17/2020

Lecture 03
2nd Semester 2019-2020

• Regular Expressions
• Properties of Regular Languages

1 2nd 2019-2020 Computation

1. Regular Expressions

2 2nd 2019-2020 Computation

Regular Expressions
Regular expressions
describe regular languages

(a  b  c) *
Example:
a, bc*   , a, bc, aa, abc, bca,...
describes the language

3 2nd 2019-2020 Computation

theory of computation 1
2/17/2020

Recursive Definition
Primitive regular expressions: ,  , 

Given regular expressions r1 and r2

r1  r2
r1  r2
Are regular expressions
r1 *
r1 
4 2nd 2019-2020 Computation

Examples
A regular expression: a  b  c  * (c  )

Not a regular expression: a  b  

5 2nd 2019-2020 Computation

Languages of Regular Expressions

Lr  : language of regular expression r

Example

L(a  b  c) *   , a, bc, aa, abc, bca,...

6 2nd 2019-2020 Computation

theory of computation 2
2/17/2020

Definition
For primitive regular expressions:

L   

L    

La   a
7 2nd 2019-2020 Computation

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 
8 2nd 2019-2020 Computation

Example a  b   a *
Regular expression:
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,...
9 2nd 2019-2020 Computation

theory of computation 3
2/17/2020

Example

Regular expression r  a  b  * a  bb 

Lr   a, bb, aa, abb, ba, bbb,...

10 2nd 2019-2020 Computation

Example

Regular expression r  aa  * bb  * b

Lr   {a 2nb 2mb : n, m  0}

11 2nd 2019-2020 Computation

Example

Regular expression r  (0  1) * 00 (0  1) *

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

12 2nd 2019-2020 Computation

theory of computation 4
2/17/2020

Example

Regular expression r  (1  01) * (0   )

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

13 2nd 2019-2020 Computation

Equivalent Regular Expressions

Definition:
Regular expressions r1 and r2

are equivalent if

L(r1)  L(r2 )

14 2nd 2019-2020 Computation

Example
L = { all strings without substring 00 }

r1  (1  01) * (0   )
r2  (1* 011*) * (0   )  1* (0   )

r1 and r2
L(r1)  L(r2 )  L
are equivalent
15
regular expressions
2nd 2019-2020 Computation

theory of computation 5
2/17/2020

Regular Expressions
and
Regular Languages

16 2nd 2019-2020 Computation

Theorem
Languages
Generated by
Regular Expressions
 Regular
Languages

17 2nd 2019-2020 Computation

Proof:

Languages
Generated by  Regular
Languages
Regular Expressions

Languages
Generated by  Regular
Languages
Regular Expressions
18 2nd 2019-2020 Computation

theory of computation 6
2/17/2020

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


19 2nd 2019-2020 Computation

Induction Basis
,  , 
Corresponding Primitive Regular Expressions:
NFAs
L( M1 )    L()

regular
L( M 2 )  {}  L( )
languages
a
L( M 3 )  {a}  L(a)
20 2nd 2019-2020 Computation

Inductive Hypothesis

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

21 2nd 2019-2020 Computation

theory of computation 7
2/17/2020

Inductive Step
Lr1  r2  We will prove:

Lr1  r2 
Are regular
Languages
Lr1 *

22
Lr1 
2nd 2019-2020 Computation

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 
23 2nd 2019-2020 Computation

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  *
24 2nd 2019-2020 Computation

theory of computation 8
2/17/2020

Therefore:

Lr1  r2   Lr1   Lr2 

Are regular
Lr1  r2   Lr1  Lr2 
languages

Lr1 *   Lr1  *

L((r1 ))  L(r1 ) is trivially a regular language


(by induction hypothesis)
25
End of Proof-Part 1
2nd 2019-2020 Computation

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(M )  L(r )


L(M2 )  L(r2 ) 

26 2nd 2019-2020 Computation

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

We will convert an NFA that accepts L


to a regular expression
27 2nd 2019-2020 Computation

theory of computation 9
2/17/2020

Since L is regular, there is a


NFA M that accepts it

L( M )  L

Take it with a single accept state


28 2nd 2019-2020 Computation

From M construct the equivalent


Generalized Transition Graph
in which transition labels are regular expressions

Example: Corresponding
M Generalized transition graph

a c a c
a, b ab
29 2nd 2019-2020 Computation

b b
Another Example:
a
q0 q1 a, b q2
b

Transition labels b b
are regular a
expressions q0 q1 a  b q2
b
30 2nd 2019-2020 Computation

theory of computation 10
2/17/2020

b b
Reducing the states:
a
q0 q1 a  b q2
b

Transition labels
are regular bb * a b
expressions
q0 bb * (a  b) q2
31 2nd 2019-2020 Computation

Resulting Regular Expression:

bb * a b

q0 bb * (a  b) q2

r  (bb * a ) * bb * (a  b )  b *

L( r )  L( M )  L
32 2nd 2019-2020 Computation

In Generale
Removing a state: d c
qi q qj
a b

ae * d ce * b
ce * d
qi qj
33
ae * b
2nd 2019-2020 Computation

theory of computation 11
2/17/2020

By repeating the process until


two states are left, the resulting graph is
Initial graph Resulting graph
r1 r4
r3
q0 qf
r2
The resulting regular expression:
r  r1 * r2 (r4  r3r1 * r2 ) *
L( r )  L( M )  L
34 End of Proof-Part 2
2nd 2019-2020 Computation

Standard Representations
of Regular Languages
Regular Languages

DFAs

Regular
NFAs
Expressions

35 2nd 2019-2020 Computation

Properties of
Regular Languages

36 2nd 2019-2020 Computation

theory of computation 12
2/17/2020

For regular languages L1 and L2


we will prove that:

Union: L1  L2
Concatenation: L1L2
Star: Are regular
L1 *
Languages
Reversal: L1R
Complement: L1
37
Intersection: L1  L2 2nd 2019-2020 Computation

We say: Regular languages are closed under

Union: L1  L2

Concatenation: L1L2

Star: L1 *
Reversal: L1R

Complement: L1

Intersection: L1  L2
38 2nd 2019-2020 Computation

A useful transformation: use one accept state

NFA
a
b 2 accept states
a
b

Equivalent
a
NFA 1 accept state
a b

b 

39 2nd 2019-2020 Computation

theory of computation 13
2/17/2020

In General
NFA

Equivalent NFA
 Single
 accepting

state
40 2nd 2019-2020 Computation

Extreme case

NFA without accepting state

Add an accepting state


without transitions

41 2nd 2019-2020 Computation

Take two languages

Regular language L1 Regular language L2

LM1   L1 LM 2   L2

NFA M1 NFA M2

Single accepting state Single accepting state


42 2nd 2019-2020 Computation

theory of computation 14
2/17/2020

Example

M1
n0
a
L1  {a nb} b

M2
L2  ba b a

43 2nd 2019-2020 Computation

L1  L2 Union
M1 NFA for

 M2

44 2nd 2019-2020 Computation

Example
n
NFA for L1  L2  {a b}  {ba}

L1  {a nb}
a
b

 L2  {ba}
b a
45 2nd 2019-2020 Computation

theory of computation 15
2/17/2020

Concatenation
L1L2
NFA for

M1 M2

46 2nd 2019-2020 Computation

Example

L1L2  {a nb}{ba}  {a nbba}


NFA for

L1  {a nb}
a
L2  {ba}
b  b a

47 2nd 2019-2020 Computation

Star Operation L1 *

w  w1w2  wk NFA for

wi  L1 M1
  L1 *
 


48 2nd 2019-2020 Computation

theory of computation 16
2/17/2020

Example

NFA for L1*  {a nb} *


L1  {a nb}
a
 b 

49

2nd 2019-2020 Computation

Reverse
NFA for L1R
L1 M1 M1

1. Reverse all transitions

2. Make initial state accepting state


50
and vice versa 2nd 2019-2020 Computation

Example

M1
a
L1  {a nb} b

M1
a
L1R n
 {ba } b

51 2nd 2019-2020 Computation

theory of computation 17
2/17/2020

Complement

L1 M1 L1 M1

1. Take the DFA that accepts L1

2. Make accepting states non-final,


and vice-versa
52 2nd 2019-2020 Computation

Example

M1
a a, b
n b a, b
L1  {a b}

M1
a, b
L1  {a, b} * {a nb} a
b a, b
53 2nd 2019-2020 Computation

Intersection

L1 regular
We show L1  L2
L2 regular regular

54 2nd 2019-2020 Computation

theory of computation 18
2/17/2020

DeMorgan’s Law: L1  L2  L1  L2

L1 , L2 regular

L1 , L2 regular

L1  L2 regular

L1  L2 regular
L1  L2 regular
55 2nd 2019-2020 Computation

Example

L1  {a nb} regular
L1  L2  {ab}
L2  {ab, ba} regular regular

56 2nd 2019-2020 Computation

Another Proof for Intersection Closure

Machine M1 Machine M2
DFA for L1 DFA for L2

Construct a new DFA M that accepts L1  L2

M simulates in parallel M1 and M 2


57 2nd 2019-2020 Computation

theory of computation 19
2/17/2020

States in M

qi , p j

State in M1 State in M2

58 2nd 2019-2020 Computation

DFA M1 DFA M2

q1 a q2 p1 a p2
transition transition

DFA M

q1, p1 a q2 , p2
New transition
59 2nd 2019-2020 Computation

DFA M1 DFA M2

q0 p0
initial state initial state

DFA M

q0 , p0
New initial state
60 2nd 2019-2020 Computation

theory of computation 20
2/17/2020

DFA M1 DFA M2

qi pj pk

accept state accept states

DFA M
qi , p j qi , pk

New accept states

Both constituents must be accepting states


61 2nd 2019-2020 Computation

Example:

L1  {a nb} n0 L2  {abm } m0

M1 M2
a b
q0 b q1 p0 a p1
a, b b a
q2 p2
a, b a, b
62 2nd 2019-2020 Computation

Automaton for intersection

L  {a n b }  {ab m }  {ab }
a, b

q0 , p0 a q0 , p1 b q1, p1 a q2 , p2

b a b a
q1, p2 b q0 , p2 q2 , p1

a b
a, b
63 2nd 2019-2020 Computation

theory of computation 21
2/17/2020

M simulates in parallel M1 and M 2

M accepts string w if and only if:

M1 accepts string w
and M 2 accepts string w

L ( M )  L ( M1 )  L ( M 2 )
64 2nd 2019-2020 Computation

When we say: We are given


a Regular Language L

We mean: Language L is in a standard


representation

(DFA, NFA, or Regular Expression)

65 2nd 2019-2020 Computation

Non-regular languages
(Pumping Lemma)

66 2nd 2019-2020 Computation

theory of computation 22
2/17/2020

{a nb n : n  0}
Non-regular languages
{vv R : v {a, b}*}

Regular languages
a *b b*c  a
b  c ( a  b) *
etc...
67 2nd 2019-2020 Computation

How can we prove that a language L


is not regular?

Prove that there is no DFA or NFA or RE


that accepts L

Difficulty: this is not easy to prove


(since there is an infinite number of them)

Solution: use the Pumping Lemma !!!


68 2nd 2019-2020 Computation

theory of computation 23

You might also like