0% found this document useful (0 votes)
125 views122 pages

Chapter-3 Regular Languages and Regular Expressions: 1 Formal Language and Automata Theory 03/14/21

The document discusses regular languages and properties of regular languages. It states that a language defined by a regular expression is a regular language. It then lists properties of regular languages, stating that regular languages are closed under union, concatenation, star operation, and reverse. It provides proofs that regular languages are also closed under complement and intersection. The document asks elementary questions about regular languages, such as how to check membership, emptiness, finiteness, and equality of two regular languages. It contrasts regular and non-regular languages, introducing the pumping lemma as a way to prove that a language is not regular. Finally, it explains the pigeonhole principle and how it applies to walks of strings in a deterministic finite automaton.

Uploaded by

Mehari Temesgen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
125 views122 pages

Chapter-3 Regular Languages and Regular Expressions: 1 Formal Language and Automata Theory 03/14/21

The document discusses regular languages and properties of regular languages. It states that a language defined by a regular expression is a regular language. It then lists properties of regular languages, stating that regular languages are closed under union, concatenation, star operation, and reverse. It provides proofs that regular languages are also closed under complement and intersection. The document asks elementary questions about regular languages, such as how to check membership, emptiness, finiteness, and equality of two regular languages. It contrasts regular and non-regular languages, introducing the pumping lemma as a way to prove that a language is not regular. Finally, it explains the pigeonhole principle and how it applies to walks of strings in a deterministic finite automaton.

Uploaded by

Mehari Temesgen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 122

Chapter-3

Regular Languages and


Regular Expressions

03/14/21 Formal Language and Automata theory 1


Regular Languages
• A language defined by a regular expression
is a regular language.

03/14/21 Formal Language and Automata theory 2


Properties
of
Regular Languages

03/14/21 Formal Language and Automata theory 3


Regular languages are closed under:

Union

Concatenation

Star operation

Reverse
03/14/21 Formal Language and Automata theory 4
Namely, for regular languages L1 and L2 :

Union L1  L2

Concatenation L1L2
Regular
 Languages
Star operation L1
R
Reverse L1
03/14/21 Formal Language and Automata theory 5
We will prove
Regular languages are closed under:

Complement

Intersection

03/14/21 Formal Language and Automata theory 6


Namely, for regular languages L1 and L2 :

Complement L1
Regular
Languages
Intersection L1  L2

03/14/21 Formal Language and Automata theory 7


Complement
Theorem: For regular language L
the complement L is regular

Proof: Take DFA that accepts L and make


• nonfinal states final
• final states nonfinal
Resulting DFA accepts L
03/14/21 Formal Language and Automata theory 8
Example: L  L( a * b)
a a, b

q0 b q1 a,b q2

L  L(a *  a * b(a  b)(a  b)*)


a a, b

q0 b q1 a,b q2

03/14/21 Formal Language and Automata theory 9


Intersection
Theorem: For regular languages L1 and L2
the intersection L1  L2 is regular

Proof: Apply DeMorgan’s Law:

L1  L2  L1  L2

03/14/21 Formal Language and Automata theory 10


L1 , L2 regular

L1 , L2 regular

L1  L2 regular

L1  L2 regular

L1  L2 regular
03/14/21 Formal Language and Automata theory 11
Elementary Questions

about

Regular Languages

03/14/21 Formal Language and Automata theory 12


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

03/14/21 Formal Language and Automata theory 13


DFA
w
w L

DFA
w
w L

03/14/21 Formal Language and Automata theory 14


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 a path from


the initial state to a final state
03/14/21 Formal Language and Automata theory 15
DFA

L

DFA

L

03/14/21 Formal Language and Automata theory 16


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
03/14/21 Formal Language and Automata theory 17
DFA

L is infinite

DFA

L is finite
03/14/21 Formal Language and Automata theory 18
Question: Given regular languages L1 and L2
how can we check if L1  L2 ?

Answer: Find if ( L1  L2 )  ( L1  L2 )  

03/14/21 Formal Language and Automata theory 19


( L1  L2 )  ( L1  L2 )  

L1  L2   and L1  L2  

L1 L2 L L2 L1 L1
2
L1  L2 L2  L1

L1  L2
03/14/21 Formal Language and Automata theory 20
( L1  L2 )  ( L1  L2 )  

L1  L2   or L1  L2  

L1 L2 L2 L1

L1  L2 L2  L1

L1  L2
03/14/21 Formal Language and Automata theory 21
Non-regular languages

03/14/21 Formal Language and Automata theory 22


n n
{a b : n  0}
Non-regular languages
R
{ww : w {a, b}*}

Regular languages
a *b b*c  a
b  c ( a  b) *
etc...
03/14/21 Formal Language and Automata theory 23
How can we prove that a language L
is not regular?

Prove that there is no DFA that accepts L

Problem: this is not easy to prove

Solution: the Pumping Lemma !!!


03/14/21 Formal Language and Automata theory 24
The Pigeonhole Principle

03/14/21 Formal Language and Automata theory 25


4 pigeons

3 pigeonholes

03/14/21 Formal Language and Automata theory 26


A pigeonhole must
contain at least two pigeons

03/14/21 Formal Language and Automata theory 27


n pigeons

...........

m pigeonholes nm

...........

03/14/21 Formal Language and Automata theory 28


The Pigeonhole Principle

n pigeons
m pigeonholes
There is a pigeonhole
nm with at least 2 pigeons

...........

03/14/21 Formal Language and Automata theory 29


The Pigeonhole Principle

and

DFAs

03/14/21 Formal Language and Automata theory 30


DFA with 4 states

b
b b

a b b
q1 q2 q3 q4
a a
03/14/21 Formal Language and Automata theory 31
In walks of strings: a no state
aa is repeated

aab

b
b b

a a b
q1 q2 q3 q4
a a
03/14/21 Formal Language and Automata theory 32
In walks of strings: aabb a state
bbaa is repeated

abbabb
abbbabbabb...
b
b b

a a b
q1 q2 q3 q4
a a
03/14/21 Formal Language and Automata theory 33
If the walk of string w has length | w |  4

then a state is repeated

b
b b

a a b
q1 q2 q3 q4
a a
03/14/21 Formal Language and Automata theory 34
Pigeonhole principle for any DFA:

If in a walk of a string w
transitions  states of DFA
then a state is repeated

b
b b

a a b
q1 q2 q3 q4
03/14/21
a
Formal Language and Automata theory a 35
In other words for a string w:

a transitions are pigeons

q states are pigeonholes

b
b b

a a b
q1 q2 q3 q4
03/14/21
a a 36
Formal Language and Automata theory
In general:

A string w has length  number of states

A state q must be repeated in the walk w

walk of w
...... q ......
03/14/21 Formal Language and Automata theory 37
The Pumping Lemma

03/14/21 Formal Language and Automata theory 38


Take an infinite regular language L

DFA that accepts L

m
states

03/14/21 Formal Language and Automata theory 39


Take string w with w L

There is a walk with label w:

.........
walk w
03/14/21 Formal Language and Automata theory 40
If string w has length | w |  m number
of states

then, from the pigeonhole principle:

a state q is repeated in the walk w

...... q ......
walk w
03/14/21 Formal Language and Automata theory 41
Write w x y z

...... q ......

03/14/21 x
Formal Language and Automata theory z 42
Observations: length | x y |  m number
of states
length | y | 1

...... q ......

03/14/21 x
Formal Language and Automata theory z 43
Observation: The string x z is accepted

...... q ......

03/14/21 x
Formal Language and Automata theory z 44
Observation: The string xyyz
is accepted

...... q ......

03/14/21 x
Formal Language and Automata theory z 45
Observation: The string xyyyz
is accepted

...... q ......

03/14/21 x
Formal Language and Automata theory z 46
i
In General: The string xy z
is accepted i  0, 1, 2, ...

...... q ......

03/14/21 x
Formal Language and Automata theory z 47
The Pumping Lemma:
• Given a infinite regular language L
• there exists an integer m

• for any string w L with length | w |  m


• we can write w x y z

• with | x y |  m and | y |  1
i i  0, 1, 2, ...
• such that: xy z  L
03/14/21 Formal Language and Automata theory 48
Applications

of

the Pumping Lemma

03/14/21 Formal Language and Automata theory 49


Theorem: The language L  {a b : n  0} n n

is not regular

Proof: Use the Pumping Lemma

03/14/21 Formal Language and Automata theory 50


n n
L  {a b : n  0}

Assume for contradiction


that L is a regular language

Since L is infinite
we can apply the Pumping Lemma

03/14/21 Formal Language and Automata theory 51


n n
L  {a b : n  0}

Let m be the integer in the Pumping Lemma

Pick a string w such that: w  L


length | w| m

m m
Example: pick wa b
03/14/21 Formal Language and Automata theory 52
m m
Write: a b xyz

From the Pumping Lemma


it must be that: length | x y |  m, | y | 1

m m
m m
Therefore: a b  a...aa...a...ab...b
x y z
k
03/14/21
y  a , k 1
Formal Language and Automata theory 53
m m k
We have: x y za b y  a , k 1

i
From the Pumping Lemma: xy z  L
i  0, 1, 2, ...

2
Thus: xy z  L
2 m k m
xy z  xyyz  a b  L
03/14/21 Formal Language and Automata theory 54
m k m
Therefore: a b L

n n
BUT: L  {a b : n  0}

m k m
a b L

CONTRADICTION!!!
03/14/21 Formal Language and Automata theory 55
Therefore: Our assumption that L
is a regular language is not true

Conclusion: L is not a regular language

03/14/21 Formal Language and Automata theory 56


n n
Non-regular languages {a b : n  0}

Regular languages
a *b b*c  a
b  c ( a  b) *
etc...
03/14/21 Formal Language and Automata theory 57
Single Final State
for NFAs and DFAs

03/14/21 Formal Language and Automata theory 58


Observation

• Any Finite Automaton (NFA or DFA)

• can be converted to an equivalent NFA

• with a single final state

03/14/21 Formal Language and Automata theory 59


Example
a
NFA
a b

a Equivalent NFA

a b

b 

03/14/21 Formal Language and Automata theory 60
In General
NFA

Equivalent NFA

 Single
 final state
03/14/21 Formal Language and Automata theory 61
Extreme Case

NFA without final state

Add a final state


Without transitions

03/14/21 Formal Language and Automata theory 62


Some Properties of
Regular Languages

03/14/21 Formal Language and Automata theory 63


Properties
For regular languages L1 and L2
we will prove that:

Union: L1  L2

Are regular
Concatenation: L1L2
Languages

Star: L1 *
03/14/21 Formal Language and Automata theory 64
We Say:
Regular languages are closed under

Union: L1  L2

Concatenation: L1L2

Star: L1 *
03/14/21 Formal Language and Automata theory 65
Regular language L1 Regular language L2

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

NFA M1 NFA M2

Single final state Single final state


03/14/21 Formal Language and Automata theory 66
Example
M1
a
n
L1  {a b} b

M2
a
L2   ba b

03/14/21 Formal Language and Automata theory 67


Union
• NFA for L1  L2
M1

 

 M2 

03/14/21 Formal Language and Automata theory 68


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

 L2  {ba} 
b a
03/14/21 Formal Language and Automata theory 69
Concatenation

• NFA for L1L2

M1 M2
 

03/14/21 Formal Language and Automata theory 70


Example

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

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

03/14/21 Formal Language and Automata theory 71


Star Operation
• NFA for L1 *

  L1 *
M1
 


03/14/21 Formal Language and Automata theory 72
Example

n
• NFA forL1*  {a b} *

n
L1  {a b}
a
 b 

03/14/21

Formal Language and Automata theory 73
Regular Expressions

03/14/21 Formal Language and Automata theory 74


Conti…

03/14/21 Formal Language and Automata theory 75


A Regular Expression can be recursively
defined as follows:
1. ε is a Regular Expression indicates the
language containing an empty string. (L (ε) =
{ε})
2. φ is a Regular Expression denoting an
empty language. (L (φ) = { })
3. x is a Regular Expression where L(x)={x}

03/14/21 Formal Language and Automata theory 76


Conti…
4. If X is a Regular Expression denoting the
language L(X) and Y is a Regular Expression
denoting the language L(Y), then
(a) X + Y is a Regular Expression
corresponding to the language L(X) U L(Y)
where L(X+Y) = L(X) U L(Y).
(b) X . Y is a Regular Expression
corresponding to the language L(X) . L(Y) where
L(X.Y)= L(X) . L(Y)
(c) R* is a Regular Expression corresponding
to the language L(R*) where L(R*) = (L(R))*
5. If we apply any of the rules several times
from 1 to 4, they are Regular Expressions.
03/14/21 Formal Language and Automata theory 77
Regular Expressions
• Regular expressions
• describe regular languages

• Example:
( a  b  c ) *

• describes the language


 a, bc *    , a, bc, aa, abc, bca,...
03/14/21 Formal Language and Automata theory 78
Recursive Definition
Primitive regular expressions: ,  , 

Given regular expressions r1 and r2

r1  r2
r1  r2
Are regular expressions
r1 *
 r1 
03/14/21 Formal Language and Automata theory 79
Examples

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

Not a regular expression:  a  b 

03/14/21 Formal Language and Automata theory 80


Languages of Regular Expressions

•  r
L : language of regular expression r

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

03/14/21 Formal Language and Automata theory 81


Definition

• For primitive regular expressions:

L    

L      

L a    a
03/14/21 Formal Language and Automata theory 82
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 
03/14/21 Formal Language and Automata theory 83
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,...
03/14/21 Formal Language and Automata theory 84
Example

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

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

03/14/21 Formal Language and Automata theory 85


Example

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

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

03/14/21 Formal Language and Automata theory 86


Example

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

L(r ) = { all strings with at least


two consecutive 0 }

03/14/21 Formal Language and Automata theory 87


Example

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

L(r ) = { all strings without


two consecutive 0 }

03/14/21 Formal Language and Automata theory 88


03/14/21 Formal Language and Automata theory 89
03/14/21 Formal Language and Automata theory 90
More Examples on Regular Expressions
• Regular Expressions for:
A. The set of all string whose length is exactly two over the
alphabet ∑={a,b}.
Ans: RE=(a+b)(a+b)
B. The set of all string whose length is atleast two over the
alphabet ∑={a,b}.
Ans: RE=(a+b)(a+b) (a+b)*
C. The set of all string whose length is atmost two over the
alphabet ∑={a,b}.
Ans: RE=(a+b+↋)(a+b+↋)
D. The set of all string whose length is even over the alphabet
∑={a,b}.
Ans: RE=((a+b)(a+b))*
03/14/21 Formal Language and Automata theory 91
Conti…
E. The set of all string whose length is odd over the
alphabet ∑={a,b}.
Ans: RE=((a+b)(a+b))* (a+b)
F. The set of all string whose length is divisible by three
over the alphabet ∑={a,b}.
Ans: RE=((a+b)(a+b) (a+b))*
And for 2mod3 is: RE=((a+b)(a+b) (a+b))*(a+b)(a+b)
G. The set of all string in which number of a is exactly two
over the alphabet ∑={a,b}.
Ans: RE=(b)*a (b)*a(b)*
H. The set of all string in which number of a is atleast two
over the alphabet ∑={a,b}.
Ans: RE=(b)*a (b)*a(a+b)*

03/14/21 Formal Language and Automata theory 92


Conti…
I. The set of all string which starts with a over the
alphabet ∑={a,b}.
Ans: RE=a(a+b)*
J. The set of all string which ends with a over the alphabet
∑={a,b}.
Ans: RE=(a+b)*a
K. The set of all string which contains a at the middle over
the alphabet ∑={a,b}.
Ans: RE=(a+b)*a(a+b)*

03/14/21 Formal Language and Automata theory 93


Conti…
L. The set of all string which starts and ends with
different symbols over the alphabet ∑={a,b}.
Ans: RE=a(a+b)*b+b(a+b)*a
M. The set of all string which starts and ends with
the same symbols over the alphabet ∑={a,b}.
Ans: RE=a(a+b)*a+b(a+b)*b+a+b+↋

03/14/21 Formal Language and Automata theory 94


Equivalent Regular Expressions

• Definition:

• Regular expressions
r1 r
and 2

L(r1 )  L(r2 )
• are equivalent if

03/14/21 Formal Language and Automata theory 95


Regular Expressions
and
Regular Languages

03/14/21 Formal Language and Automata theory 96


Theorem

Languages
Generated by
Regular Expressions
 Regular
Languages

03/14/21 Formal Language and Automata theory 97


Theorem - Part 1

Languages
Generated by  Regular
Languages
Regular Expressions

1. For any regular expression r


the language L(r ) is regular

03/14/21 Formal Language and Automata theory 98


Theorem - Part 2

Languages
Generated by  Regular
Languages
Regular Expressions

2. For any regular language L there is


a regular expression r with L( r )  L

03/14/21 Formal Language and Automata theory 99


Proof - Part 1

1. For any regular expression r


the language L(r ) is regular

Proof by induction on the size of r

03/14/21 Formal Language and Automata theory 100


Induction Basis
• Primitive Regular Expressions: ,  , 
NFAs

L( M1 )    L()

regular
L( M 2 )  {}  L( )
languages

L( M 3 )  {a}  L(a )
03/14/21 Formal Language and Automata theory 101
Inductive Hypothesis

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

03/14/21 Formal Language and Automata theory 102


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

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

03/14/21
L  r1  
Formal Language and Automata theory 103
• 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 
03/14/21 Formal Language and Automata theory 104
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   *
03/14/21 Formal Language and Automata theory 105
• Therefore:

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

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

L r1 *   L r1   *
03/14/21 Formal Language and Automata theory 106
• And trivially:


L((r1 )) is a regular language

03/14/21 Formal Language and Automata theory 107


Proof – Part 2

2. For any regular language L there is


a regular expression r with L( r )  L

Proof by construction of regular expression

03/14/21 Formal Language and Automata theory 108


• Since L is regular take the
• NFA M that accepts it

L( M )  L

Single final state


03/14/21 Formal Language and Automata theory 109
• From M construct the equivalent
• Generalized Transition Graph
– transition labels
– are regular expressions

Example:
M
a c a c
a, b ab
03/14/21 Formal Language and Automata theory 110
b b
• Another Example:
a
q0 q1 a, b q2
b

b b
a
q0 q1 a  b q2
b
03/14/21 Formal Language and Automata theory 111
b b
• Reducing the states:
a
q0 q1 a  b q2
b

bb * a b

q0 bb * (a  b) q2
03/14/21 Formal Language and Automata theory 112
• Resulting Regular Expression:

bb * a b

q0 bb * (a  b) q2

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

L(r )  L( M )  L
03/14/21 Formal Language and Automata theory 113
In General
• Removing states: e
d c
qi q qj
a b

ae * d ce * b
ce * d
qi qj
03/14/21
ae * b
Formal Language and Automata theory 114
• Obtaining the final regular expression:
r1 r4
r3
q0 qf
r2

r  r1 * r2 (r4  r3r1 * r2 ) *

L(r )  L( M )  L
03/14/21 Formal Language and Automata theory 115
Regular Grammar

03/14/21 Formal Language and Automata theory 116


03/14/21 Formal Language and Automata theory 117
03/14/21 Formal Language and Automata theory 118
Conti…

03/14/21 Formal Language and Automata theory 119


Conti…

03/14/21 Formal Language and Automata theory 120


Conti…

03/14/21 Formal Language and Automata theory 121


re e
- Th
ter
ha p
f C
d o
En

03/14/21 Formal Language and Automata theory 122

You might also like