CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 5 School of Innovation, Design and Engineering Mälardalen University 2011 1
Content - More Properties of Regular Languages (RL) - Standard Representations of RL - Elementary Questions about RL - Non-Regular Languages - The Pigeonhole Principle - The Pumping Lemma - Applications of the Pumping Lemma - NFA-DFA repetition 2
More Properties of Regular Languages 3
We have proven Regular languages g are closed under Union Concatenation Star operation Reverse 4
Namely, for regular languages L1 and L2 : Union Concatenation Star operation Reverse L1 L 2 L 1 L 2 L 1 R L 1 Regular Languages 5
We will prove Regular languages are also closed under Complement Intersection 6
Namely, for regular languages L1 and L2 : Complement L1 Intersection L1 L 2 Regular Languages 7
Complement Theorem For regular language L the complement L is regular Proof Take DFA that accepts L and exchange: non-final states final states non-accepting states accepting states Resulting DFA accepts L 8
Example a L = L( a * b) a,b b a, b q0 q1 q2 L = L ( a + a b ( a + b )( a + b ) a ) a, b b a, b q 0 q 1 q 2 9
Intersection Theorem e For regular languages L 1 and L 2 the intersection L1 L 2 is regular Proof Apply DeMorgan s Law: L1 L2 = L1 L 2 10
L 1, L 2 1, regular L 1, L 2 L1 L 2 L1 L 2 L1 L 2 regular regular regular regular 11
Standard Representations of Regular Languages 12
Regular Language Representations Regular Expression Regular Language DFA NFA Regular Grammar 13
Elementary Questions about Regular Languages 14
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 15
DFA w w L DFA w w L 16
Question: Given regular language how can we check L ( L = ) L if is empty:? Answer: Take the DFA that accepts L Check if there is a path from the initial state to a final state 17
DFA L DFA L = 18
Question: Given regular language how can we check L if is finite? L Answer: Take the DFA that accepts Check if there is a walk with cycle from the initial iti state t to a final state t L 19
DFA L is infinite it DFA L is finite 20
Question: L L2 Given regular languages 1 and how can we check if L 1 = L 2? Answer: Find if ( L1 L2 ) ( L1 L2 ) = 21
( 2 L1 L2 ) ( L1 L ) = L 1 L 2 = and L 1 L 2 = L 1 L2 L 2 L 2 1 L L1 L1 L 2 L2 L1 L = 1 L 2 22
( 2 L1 L2 ) ( L1 L ) L 1 L 2 or L 1 L 2 L 1 L L 2 2 L1 L1 L 2 L2 L1 L 1 L 2 23
Non-Regular Languages 24
Non-regular languages??? Regular languages a *b b * c + a b + c ( a + b)* etc... 25
How can we prove that a language L is not regular? a a a λ b λ λ b b 26
How can we prove that a language L is not regular? Prove that there is no DFA that accepts L a a a Problem: this is not easy to prove Solution: the Pumping Lemma! λ λ λ b b L n n = { a b : n 0} b 27
The Pigeonhole Principle i 28
The Pigeonhole Principle 29
4 pigeons 3 pigeonholes 30
A pigeonhole must contain at least two pigeons pg 31
n pigeons... m pigeonholes n > m... 32
The Pigeonhole Principle n pigeons m pigeonholes n > m There is a pigeonhole with at least 2 pigeons... 33
The Pigeonhole Principle and DFAs 34
DFA with 4 states b b b a b q 1 q 2 q 3 q b q 4 a a 35
In walks of strings: b b a aa aab no state is repeated b a a q1 q2 q3 a b a q 4 36
In walks of strings: aabb a state bbaa abbabb abbbabbabb... is repeated b b b a a q1 q2 q3 b q 4 a a 37
w w 4 If the walk of string has length then a state is repeated b b b a a q1 q2 q3 a b a q 4 38
Pigeonhole principle for any DFA: If in a walk of a string transitions then a state is repeated w states of DFA 39
In other words for a string w a q transitions are pigeons states t are pigeonholes 40
In general A string w has length number of states A state q must be repeated in the walk of w walk of w...... q 41
The Pumping Lemma for Regular Languages see also http://www.math.uu.se/~salling/movies/nonregularity.mov 42
Take an infinite regular language g L DFA that accepts L n states 43
Take string w with w L There is a walk with label w... walk w 44
If string w has length w m = n + ( n number of states) then, from the pigeonhole principle: 1 a state q is repeated in the walk w...... q walk w 45
Write w = x y z y...... q x z 46
Lengths: x y m (from pigeon pg principle, p as q is the first repetition in sequence) y 1 (there is a walk in the graph) y... q... x z 47
Observation The string x z is accepted y...... q x z 48
Observation The string x y y z is accepted y...... q x z 49
Observation The string is accepted Th t i x y y y z y... q... x z 50
Generally The string x y i z is accepted i = 0,1, 2,... y...... q x z 51
The Pumping Lemma Given an 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 such hthat: t i x y i z L i = 0,1, 2,... 52
Applications of the Pumping Lemma 53
Theorem The language L n n = { a b : n 0} is not regular Proof Use the Pumping Lemma! 54
L n n = { a b : n 0} Assume to the contrary, that L is a regular language L Since is infinite we can apply the Pumping Lemma 55
L n n = { a b : n 0} Let m be the integer in the Pumping Lemma Pick a string such that: w w L length w m e.g. pick m b m w = a 56
Write: a m b m = x y z From the Pumping Lemma it must be that length x y m, y 1 Therefore: a m m m b m = a... aa... a... ab... b x y z y k = a, k 1 57
We have: x y z = m b m a y k = a, k 1 From the Pumping Lemma: x y i z i = 0,1, 2,... L We can choose i = 0 w = a m k b m L CONTRADICTION! 58
Therefore: Our assumption that L is a regular language is not true Conclusion L is not a regular language END OF PROOF 59
Non-regular languages n n { a b : n 0} Regular languages a *b b * c + a b + c ( a + b)* etc... 60
Theorem The language L R = { ww : w Σ*} Σ = { a, b} is not regular Proof Use the Pumping Lemma! 61
L R = { ww : w Σ*} Assume to the contrary, L that is a regular language L Since is infinite we can apply the Pumping Lemma 62
L R = { ww : w Σ *} Let m be the integer in the Pumping Lemma Pick a string w such that: w L length w m pick w = a m b m b m a m 63
Write a m b m b m a m = x y z From the Pumping Lemma it must be that length x y m, y 1 a m b m b m a m = m m m m a... aa... a... ab... bb... ba... a y k = a, k 1 x y z 64
a m b m b m a m = m m m m a... aa... a... ab... bb... ba... a y k, 1 = a k x y z We can choose i = 0 m k So we get a s as on the left, while m is on the right: CONTRADICTION! 65
Therefore: Our assumption that L is a regular language is not true Conclusion L is not a regular language END OF PROOF 66
n Non-regular languages g n { a b : n 0} { ww R : w Σ*} Regular languages a *b b * c + a b + c ( a + b)* etc... 67
Theorem The language L n = { a b c : n, l is not regular l n+ l 0} Proof Use the Pumping Lemma 68
L n l n+ l = { a b c : n, l 0} Assume to the contrary that L is a regular language L Since is infinite we can apply the Pumping Lemma 69
L n l n+l = { a b c : n, l 0} Let m be the integer in the Pumping Lemma Pick a string such that: w w L length w m Pick m m 2 m 2 w = a b c 70
Write a m b m c 2m = x y z From the Pumping Lemma it must be that length x y m, y 1 a m m 2m b c = m m 2m a... aa... aa... ab... bc... cc... c y k = a, k 1 x y z 71
m m m c b a z y x 2 = We have:, 1 = k a y k From the Pumping Lemma L z y x i... 2, = 0,1, i Thus: L z y x 0 L c b a z x z y x m m k m = = 2 0 72
Therefore: a m k m 2m b c L BUT: L n l n+ l = { a b c : n, l 0} a m k m 2m b c L CONTRADICTION! 73
Therefore: Our assumption that L is a regular language is not true Conclusion L is not a regular language END OF PROOF 74
Non-regular languages g { ww R : w Σ*} n n { a b : n 0} n l n+ l { a b c : n, l 0} Regular languages 75
Theorem The language L n = { a! : n 0} is not regular n! = 1 2L ( n 1 ) n Proof Use the Pumping Lemma Factorial of n, (n!) is the product of all positive integers less than or equal to n. For example, 5! = 5 x 4 x 3 x 2 x 1 = 120 The value of 0! is 1. 76
L n = { a! : n 0} Assume to the contrary that L is a regular language L Since is infinite we can apply the Pumping Lemma 77
L n = { a! : n 0} Let m be the integer in the Pumping Lemma Pick a string ti w such hthat: t w L length w m Pick w = a m!! 78
Write a m! = x y z From the Pumping Lemma it must be that length x y m, y 1 m m!! m! = a m a... aa... aa... aa... aa... a x y z k y = a, 1 k m 79
We have: x y z = m! a k y = a, 1 k m From the Pumping Lemma: x y i z L i = 0,1, 2,... Thus: x y 2 z L x y 2 z = x y y z = a m!+ k L 80
Therefore: a m!+ k L 1 k m Since: L n = { a! : n 0} There is p m! + k = p!! 1 k m There is p 81
However for m > 1 m! +k m!+ m m!+ m! < m! m + m! = m! ( m + 1) = ( m +1)! m! < m! + k < ( m + 1)! m! + k p! for any p 82
Therefore: a m!+ k L BUT: L = { a! : n 0} n and 1 k m a m!+ k L CONTRADICTION! 83
Therefore: Our assumption that L is a regular language is not true Conclusion L is not a regular language END OF PROOF 84
Non-regular languages { ww R : w Σ*} { a b : n 0} n n n l n+ l { a b c : n, l 0} { a n! : n 0} Regular languages 85
Theorem The language L = i { a : i prime } is not regular Proof Use the Pumping Lemma 86
L = i { a : i prime } Assume to the contrary that L is a regular language Since is infinite L we can apply the Pumping Lemma 87
w L length w m From the Pumping Lemma it must be that length x y m, y 1 From the Pumping Lemma: x y i z L i = 0,1, 2,... k+ 1 The length of w = xy z = k > m must be prime for each string of L 88
But, choosing length ( xyz ) = k length( xy k + 1 z) = length( xyy k z) = length( xyz) + length( y k ) = k + k ( length ( y )) = k (1 + length( y)) which is not prime! ( k > m) Thus: w L CONTRADICTION! 89
Therefore: Our assumption that L is a regular language is not true Conclusion L is not a regular language END OF PROOF 90
Non-regular languages { ww R : w Σ*} { a b : n 0} n n n l n+ l { a b c : n, l 0} { a n! : n 0} Regular languages L i = { a : i prime} 91
An alternative variant of proof from slide 53. Theorem The language is not regular L n n = { a b : n 0} Proof Use the Pumping Lemma! 92
L n n = { a b : n 0} Assume to the contrary that L is a regular language L Since is infinite we can apply the Pumping Lemma 93
L n n = { a b : n 0} Let m be the integer in the Pumping Lemma Pick a string w such that: w L length w m Pick m b m w = a 94
Write: a m b m = x y z From the Pumping Lemma it must be that: length x y m, y 1 m m Therefore: a m b m = a... aa... a... ab... b y k = a, k 1 x y z 95
We have: x y z = a m b m y = a, k 1 k From the Pumping Lemma: x y i z L i = 0,1, 2,... Thus: x y 2 z L x y 2 z = x y y z = a m+ k b m L 96
m+k + b m Therefore: a b L BUT: L n n = { a b : n 0} a m+ k b m L CONTRADICTION 97
Therefore: Our assumption that L is a regular language is not true Conclusion: L is not a regular language END OF PROOF 98
Pumping Lemma, in short 99
Observation: Every language of finite size has to be regular (we can easily construct an NFA that accepts every string in the language) Therefore, every non-regular language g has to be of infinite size (contains an infinite number of strings) 100
Suppose you want to prove that An infinite language L is not regular 1. Assume the opposite: L is regular 2. The pumping lemma should hold for L 3. Use the pumping lemma to obtain a contradiction 4. Therefore, L is not regular 101
Explanation of Step 3: How to get a contradiction with pumping lemma w L i. Find a particular string which satisfies the conditions of the pumping p lemma ii. Write w = xyz iii. Show that w i for some i 1 = xy z L iv. This gives a contradiction, since from pumping lemma w = xy i z L 102
Note: It suffices to show that only one string w gives a contradiction L You don t need to obtain contradiction for every w L 103
Extra Examples on Regular Languages 104
FA RE 105
FA RE or 106
NFA DFA Subset Construction Delmängdkonstruktion 107
Convert an NFA to DFA using the subset construction 108
Each state t of the DFAis a set of states t of the NFA. Start by the initial state of the DFA which is the ε-closure of the initial state of the NFA (all the states you reach by ε-transitions from the initial state). CLOSE{0}={0, {0, 1, 3}=S 0 109
Determine the transition function of the DFA on all inputs. Begin with the initial state S 0, and determine the transition on input a. CLOSE{0} = {0, 1, 3} = S 0 δ(s 0, a) = CLOSE{1, 2} 110
The ε-closure of the set {1, 2} is {1, 2, 3} This is a new state in the DFA, call it S 1. CLOSE{0} = {0, 1, 3} = S 0 δ(s 0, a) = CLOSE{1, 2} = {1, 2, 3} = S 1 111
With the initial state S 0, determine the transition on input b. CLOSE{0} = {0, 1, 3} = S 0 δ(s 0, a) = CLOSE{1, 2} = {1, 2, 3} = S 1 δ(s0, b) = CLOSE{3} 112
The ε-closure of the set {3} is {1, 3}. This is a new state in the DFA, call it S 2. CLOSE{0}={0 {0, 1, 3}=S 0 δ(s 0, a) = CLOSE{1, 2} = {1, 2, 3} = S 1 δ(s0, b) = CLOSE{3} = {1, 3} = S 2 113
Determine the transition function of the DFA from state S 1 on inputs a and b. On a there is no where to go in the NFA, so we create a sink ( trap ) state for DFA. δ(s 0, a) = CLOSE{1, 2} = {1, 2, 3} = S 1 δ(s 0, b) = CLOSE{3} = {1, 3} = S 2 δ(s 1, a) = CLOSE{} = = S 3 114
Determine the transition function of the DFA from state S 1 on inputs a and b. On b there is a transition to an existing state S 2. δ(s 0, a) = CLOSE{1, 2} = {1, 2, 3} = S 1 δ(s 0, b) = CLOSE{3} = {1, 3} = S 2 δ(s 1, a) = CLOSE{} = = S 3 δ(s 1, b) = CLOSE{3} = {1, 3} = S2 115
Determine the transition from state S 2 on inputs a and b. δ(s 0, a) = CLOSE{1, 2} = {1, 2, 3} = S 1 δ(s 0, b) = CLOSE{3} = {1, 3} = S 2 δ(s 1, a) = CLOSE{} = = S 3 δ(s 1, b) = CLOSE{3} = {1, 3} = S 2 δ(s 2, a) = CLOSE{} = = S 3 δ(s 2, b) = CLOSE{3} = {1, 3} = S 2 116
Determining the transition from state S 2 on inputs a and b is easy; from the empty set of states there are no transitions in the NFA. In the DFA this is represented by a transition from the empty set back to itself. 117
The final states t of the DFA are determined d from the final states t of the NFA. State t 3 was the only final state in the NFA. (Any set of NFA states containing a final state is a final state in the DFA.) 118
NFA DFA RESULT 119
State elimination 0*1 q 0 λ+10* q 1 0*11 q 2 01 (λ+10*)(0*1)*0*11 q 0 q 2 01 (λ+10*)(0*1)*0*11 1) 0 11 + 01 q 0 q 2
Example 7 Convert the following NFA to DFA. 1 0 q 0 0 s 0 ε ε 1 s r 0 q p ε r p r s p 1 0 1 0 q, r, p 1
Converting the NFA to DFA 0 0 0 s p 0 λ λ q λ 1 r p r s 1 q r p 0 1 q p r s 0 1 0 1
Application: Lex - lexical analyzer Lex RE NFA DFA Minimal DFA The final states of the DFA are associated with actions. 123