Chapter-3 Regular Languages and Regular Expressions: 1 Formal Language and Automata Theory 03/14/21
Chapter-3 Regular Languages and Regular Expressions: 1 Formal Language and Automata Theory 03/14/21
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
Complement L1
Regular
Languages
Intersection L1 L2
q0 b q1 a,b q2
q0 b q1 a,b q2
L1 L2 L1 L2
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
DFA
w
w L
L
DFA
L
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 )
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
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?
3 pigeonholes
...........
m pigeonholes nm
...........
n pigeons
m pigeonholes
There is a pigeonhole
nm with at least 2 pigeons
...........
and
DFAs
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
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:
b
b b
a a b
q1 q2 q3 q4
03/14/21
a a 36
Formal Language and Automata theory
In general:
walk of w
...... q ......
03/14/21 Formal Language and Automata theory 37
The Pumping Lemma
m
states
.........
walk w
03/14/21 Formal Language and Automata theory 40
If string w has length | w | m number
of states
...... 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
• 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
is not regular
Since L is infinite
we can apply the Pumping Lemma
m m
Example: pick wa b
03/14/21 Formal Language and Automata theory 52
m m
Write: a b xyz
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 za 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
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
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
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
M2
a
L2 ba b
M2
L2 {ba}
b a
03/14/21 Formal Language and Automata theory 69
Concatenation
M1 M2
n
L1 {a b}
a L2 {ba}
b b a
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
• Example:
( a b c ) *
r1 r2
r1 r2
Are regular expressions
r1 *
r1
03/14/21 Formal Language and Automata theory 79
Examples
• Example
L (a b c) * , a, bc, aa, abc, bca,...
L
L
L a a
03/14/21 Formal Language and Automata theory 82
Definition (continued)
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
• Regular expression r aa * bb * b
L r {a b
2n 2m
b : n, m 0}
• Regular expression r (0 1) * 00 (0 1) *
• Definition:
• Regular expressions
r1 r
and 2
L(r1 ) L(r2 )
• are equivalent if
Languages
Generated by
Regular Expressions
Regular
Languages
Languages
Generated by Regular
Languages
Regular Expressions
Languages
Generated by Regular
Languages
Regular Expressions
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
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
L( M ) L
Example:
M
a c a c
a, b ab
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