Professional Documents
Culture Documents
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...
How can we prove that a language L
is not regular?
b
b b
a b b
q1 q2 q3 q4
a a
In walks of strings: a no state
aa is repeated
• aab
b
b b
a a b
q1 q2 q3 q4
a a
In walks of strings: aabb a state
bbaa is repeated
•
abbabb
abbbabbabb...
b
b b
a a b
q1 q2 q3 q4
a a
In walks of strings: aabb a state
bbaa is repeated
•
abbabb
abbbabbabb...
b
b b
a a b
q1 q2 q3 q4
a a
If string w has length | w | 4 :
•
Then the transitions of string w
are more than the states of the DFA
a a b
q1 q2 q3 q4
a a
In general, for any DFA:
walk of w
...... q ......
Repeated state
The Pumping Lemma
Take an infinite regular language L
m
states
Take string w with w L
.........
walk w
If string w has length | w | m number
of states
of DFA
then, from the pigeonhole principle:
...... q ......
walk w
Let q be the first state repeated
...... q ......
walk w
Write w x y z
...... q ......
x z
Observations: length | x y | m number
of states
length | y | 1 of DFA
...... q ......
x z
Observation: The string xz
is accepted
...... q ......
x z
Observation: The string xyyz
is accepted
...... q ......
x z
Observation: The string xyyyz
is accepted
...... q ......
x z
i
In General: The string xy z
is accepted i 0, 1, 2, ...
...... q ......
x z
In General: i 0, 1, 2, ...
xy z L
i
...... q ......
x z
In other words, we described:
• with | x y | m and | y | 1
i i 0, 1, 2, ...
• such that: xy z L
Theorem: The language
n n
L {a b : n 0}
is not regular
L
Since is infinite
we can apply the Pumping Lemma
n n
L {a b : n 0}
m m
We pick wa b
m m
Write: a b xyz
i
From the Pumping Lemma: xy z L
i 0, 1, 2, ...
2
Thus: xy z L
m m k
x y za b y a , k 1
2
From the Pumping Lemma: xy z L
mk m
2
xy z a...aa...aa...aa...ab...b L
x y y z
m k m
Thus: a b L
m k m
a b L
For all k > = 1
n n
BUT: L {a b : n 0}
m k m
a b L
CONTRADICTION!!!
Therefore: Our assumption that LL
is a regular language is
not true
Regular languages
R
Non-regular languages L {ww : w *}
Regular languages
Theorem: The language
R {a, b}
L {ww : w *}
is not regular
Since L is infinite
we can apply the Pumping Lemma
R
L {ww : w *}
length | w| m
m m m m
We pick wa b b a
m m m m
Write a b b a xyz
m m m m
xyz a...aa...a...ab...bb...ba...a
x y z
k
Thus: y a , k 1
m m m m k
x y za b b a y a , k 1
i
From the Pumping Lemma: xy z L
i 0, 1, 2, ...
2
Thus: xy z L
m m m m k
x y za b b a y a , k 1
2
From the Pumping Lemma: xy z L
m+k m m m
2
xy z = a...aa...aa...a...ab...bb...ba...a ∈ L
x y y z
m k m m m
Thus: a b b a L
m k m m m
a b b a L k 1
R
BUT: L {ww : w *}
m k m m m
a b b a L
CONTRADICTION!!!
Therefore: Our assumption that L
is a regular language is not true
Regular languages
Theorem: The language
n l n l
L {a b c : n, l 0}
is not regular
Since L is infinite
we can apply the Pumping Lemma
n l n l
L {a b c : n, l 0}
length | w| m
m m 2m
We pick wa b c
m m 2m
Write a b c xyz
m m 2m
xyz a...aa...aa...ab...bc...cc...c
x y z
k
Thus: y a , k 1
m m 2m k
x y za b c y a , k 1
i
From the Pumping Lemma: xy z L
i 0, 1, 2, ...
Thus:
x y z xz L
0
m m 2m k
x y za b c y a , k 1
mk m 2m
Thus: a b c L
mk m 2m
a b c L k 1
n l n l
BUT: L {a b c : n, l 0}
mk m 2m
a b c L
CONTRADICTION!!!
Therefore: Our assumption that L
is a regular language is not true
Regular languages
Example
L ={ w | w =0n where n is a perfect square}
n! 1 2 (n 1) n
Since L is infinite
we can apply the Pumping Lemma
n!
L {a : n 0}
m!
We pick wa
m!
Write a xyz
i
From the Pumping Lemma: xy z L
i 0, 1, 2, ...
2
Thus: xy z L
m! k
x y za y a , 1 k m
2
From the Pumping Lemma: xy z L
mk m!m
2
xy z a...aa...aa...aa...aa...aa...a L
x y y z
m! k
Thus: a L
m! k
a L 1 k m
n!
Since: L {a : n 0}
m! k (m 1)!
n!
BUT: L {a : n 0}
m! k
a L
CONTRADICTION!!!
Therefore: Our assumption that L
is a regular language is not true
b
1
3
a
a
0
b 4 b 5
2
b
a a
a,b
Equivalent finite automata
a,b b
0 1
2
a
a ,b
Unreachable states
’ (ij…k, a) = lm…n
4- The initial state q0’ is that state of M’
whose label includes the q0.initial state
of M
1 0,1
0
1
0 0
0 4
2 1
1 0
1
3
Equivalent FA
0 0,1
0,1
0 123 1
4
0
1
0
0 1 1
1
2
0
0
XX0,1
1
0,1
XX0
Example
minimize
4
0 1
0 0,1
0 1 2
0 3
1
0,1
5
0,1
Minimal state FA
0,1
0
01 35
1
Example 0
4
0
2
0 1 0
1
1 5
1 1
3 0 0
6
1
1 0
7
1
Min state FA
1
0
1 0
124 357
6
0
L1= L3 = 1
L5 = * 01
L2= 0
* L7 = * 11
L4 = 00
L6 = l = *10
Lex
Lex: a lexical analyzer
• A Lex program recognizes strings
Lex program
Regular expressions
“+”
“-” /* operators */
“=“
“if”
/* keywords */
“then”
Lex program
Regular expressions
(0|1|2|3|4|5|6|7|8|9)+ /* integers */
(a|b|..|z|A|B|...|Z)+ /* identifiers */
Internal Structure of Lex
Lex
Regular Minimal
NFA DFA
expressions DFA
Integer
1234 test Identifier
var 566 78 Identifier
9800 + Integer
Integer
temp
Integer
Error in line: 3
Identifier