CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 2 Mälardalen University 2010 1
Content Languages, g Alphabets and Strings Strings & String Operations Languages & Language Operations Regular Expressions Finite Automata, FA Deterministic Finite Automata, DFA 2
Languages, Alphabets and Strings 3
Languages A language is a set of strings A String is a sequence of letters defined d over an alphabet: Σ = { a, b, c, K, z An alphabet is a set of symbols 4
Alphabets and Strings We will use small alphabets: Σ = { a,b, a ab abba bb baba Strings aaabbbaabab u = abb v = bbbaaa w = abba 5
Operations on Strings 6
w v String Operations = a a L x = abba 1 2 1 2 a n = b b Lb y = bbbaaa m Concatenation (sammanfogning) wv = a a 1 2 L a b b 1 2 Lb n b m xy = abbabbbaaa bbb 7
Reverse (reversering) w = a a L ababaaabbb 1 2 a n w R = a nl a 2 a 1 bbbaaababa Example: Longest odd length palindrome in a natural language: g saippuakauppias (Finnish: soap sailsman) 8
String Length w = a 1 Length: a 2 L a n w = n Examples: abba = 4 aa = 2 a =1 9
Recursive Definition of Length For any letter: a = 1 wa wa For any string : = w + 1 Example: abba = abb + 1 = ab + 1 + 1 = a + 1+ 1+ 1 = 1 + 1+ 1+ 1= 4 10
Length of Concatenation uv = u + v Example: u = aab, u = 3 v = abaab, v = 5 uv = aababaab = 8 uv = u + v = 3 + 5 = 8 11
Proof of Concatenation Length Claim: uv = u + v Proof: By induction on the length v Induction basis: v = 1 From definition of length: uv = u + 1 = u + v 12
uv = u + Inductive hypothesis: h + v for v n Inductive step: we will prove uv = u + v for v = n +1 13
Inductive Step Write wa, where v = w = n, a = 1 From definition of length: uv = uwa = uw + 1 wa = w +1 From inductive hypothesis: uw = u + w Thus: uv = u + w + 1 = u + wa = u + v END OF PROOF 14
Empty String A string with no letters: (Also denoted as ε) ) λ Observations: λ = 0 λw = wλ = w λ abba = abbaλ = abba = { { λ 15
Substring of a string: Substring (delsträng) a subsequence of consecutive characters String Substring abbab abbab abbab abbab b ab abba b bbab 16
Prefixes abbab Prefix and Suffix Suffixes λ abbab w = uv a ab abb bbab bab ab prefix suffix abba b abbab λ 17
n w = ww... w Repetition n (String w repeated n times) Example: ( abba ) = abbaabba 2 Definition: 0 w = λ 0 ( abba) = λ 18
The (Kleene* star) Operation Σ * the set of all possible strings from alphabet Σ Σ = { a, b { λ, a, b, aa, ab, ba, bb, aaa, aab,k Σ * = aab [* Kleene is pronounced "clay-knee ] http://en.wikipedia.org/wiki/kleene_star 19
The + (Kleene plus) Operation + Σ : the set of all possible strings from the alphabet Σ except λ Σ = { a,b { λ Σ* =, a, b, aa, ab, ba, bb, aaa, aab,k Σ + = Σ* λ { a, b, aa, ab, ba, bb, aaa, aab,k Σ + = 20
Σ = { oj, fy, usch Example { Σ* = λ, oj, fy, usch, ojoj, fyfy,uschusch, ojfy, ojusch K Σ + = Σ* { λ Σ + = oj, fy, usch, ojoj, fyfy,uschusch, ojfy, ojusch K 21
Operations on Languages 22
Language A language is any subset of Σ* Example: Σ = { a,b, Σ* = { λ, a, b, aa, ab, ba, bb, aaa,k Languages: { λ { a, aa, aab λ {, abba, baba, aa, ab, aaaaaa 23
Example An infinite language L = { a b : n 0 n n λ ab aabb aaaaabbbbb L abb L 24
Operations on Languages The usual set operations { a, ab, aaaa { bb, ab = { a, ab, { a, ab, aaaa { bb, ab = { ab { a, ab, aaaa { bb, ab = { a, aaaa bb, aaaa Complement: L = Σ* LL { λ, a, b, aa, ab, ba, bb, aaa,,k Σ* = aab { a, ba = { λ, b, aa, ab, bb, aaa,k 25
Definition: L R Reverse R = { w : w L Examples: R { ab, aab, baba = { ba, baa, abab L n n = { a b : n 0 L R n n = { b a : n 0 26
Concatenation o Definition: L L = { xy : x L y L 1 2 1, 2 Example { a, ab, ba { b, aa = { ab, aaa, abb, abaa, bab, baaa 27
Repeat Definition: L = 1LL2 L 3L b 3 n { a, b = { a, b { a, b { a, b n = { aaa, aab, aba, abb, baa, bab, bba, bbb Special case: 0 L = { λ { 0 a, bba, aaa = { λ 28
Example L n n = { a b : n 0 L 2 n n m m = { a b a b : n, m 0 2 L aabbaaabbb bbb L 29
Star-Closure (Kleene *) Definition: 0 1 L* = L L L 2 L Example: { a, bb * λ, a, bb, = aa, abb, bba, bbbb, aaa aabb abba,,, abbbb, K 30
Positive Closure Definition L + 1 2 = L U L { λ U L = L* { λ a,bb, +, bb = aa, abb, bba, bbbb { a aaa, aabb, abba, abbbb,, K 31
Regular Expressions 32
Regular Expressions: Recursive Definition iti Primitive regular expressions:, Given regular expressions r 1 and r 2 r + 1 r 2 r 1 r 1 * ( ) r 1 r r 2 λ, λ α are Regular Expressions 33
Examples A regular expression: ( a + b c )* ( c + ) Not a regular expression: ( a + b + ) 34
Building Regular Expressions Σ = { a, b, c Zero or more. a* means "zero or more a's." To say "zero or more ab's," that is, {, ab, abab, ababab,..., you need to say (ab)*. ab* denotes {a, ab, abb, abbb, abbbb,... 35
Building Regular Expressions Σ = { a, b, c One or more. Since a* means "zero or more a's", you can use aa* (or equivalently, a*a) to mean "one or more a's. Similarly, to describe "one or more ab's," that is, {ab, abab, ababab,..., you can use ab(ab)*. 36
Building Regular Expressions Σ = { a, b, c Any string at all. To describe any string at all (with = {a, b, c), you can use (a+b+c)*. Any nonempty string. This can be written as any character from followed by any string at all: (a+b+c)(a+b+c)*. 37
Building Regular Expressions Σ = Any string not containing... { a, b, c To describe any string at all that doesn't contain an a (with = {a, b, c), you can use (b+c)*. Any string containing exactly one... To describe any string that contains exactly one a, put "any string not containing an a," on either side of the a, like this: (b+c)*a(b+c)*. ( ) 38
Languages of Regular Expressions L( r) language of regular expression r Example L (( a + b c)* ) = { λ, a, bc, aa, abc, bca,... 39
Definition For primitive regular expressions: L( L ) = L ( λ ) = { λ L ( a) = { a 40
Definition (continued) For regular expressions r1 and r 2 L ( + ) = ( ) ( ) r r L r L r 1 2 1 2 ( ) ( ) ( ) L = r r L r L r 1 2 1 2 L ( r *) = ( L( r ))* 1 1 (( )) ( ) L = r L r 1 1 41
Example Regular expression: ( a + b ) a * L (( a + b) a *) = L (( a + b) ) L( a *) ( a b) L( a *) = L + = = ( L( L a) L( b) ) ( L( a) )** ( { a { b ) { ( a )* = { a, b { λ, a, aa, aaa,... = { a, aa, aaa,..., b, ba, baa,... 42
Regular expression Example ( a b) ( a bb) r = + * + ( r ) { a, bb, aa, abb, ba, bbb,... L = 43
Example Regular expression = ( aa ) ( bb ) b r = * * L 2n 2 m ( r ) = { a b b : n, m 0 44
Example Σ = { 0,1 L (r) = { all strings with at least two consecutive 0 Regular expression r = ( 0 + 1)*00 (0 + 1)* 45
Example Σ = { 0,1 L (r) = { all strings without two consecutive 0 Regular expression r1 = (1 + 01) * (0 + λ) (consists of repeating 1 s and 01 s). 46
Example L = { all strings without two consecutive 0 Equivalent solution: r 2 = (1*011*)*(0 (0 + λ ) + 1*(0 + λ ) (In order not to get 00 in a string, after each h0 there must be an 1, which means that strings of the form 1...101...1 are repeated. That is the first parenthesis. To take into account strings that end with 0, and those consisting of 1 s solely, the rest of the expression is added.) 47
Equivalent Regular Expressions Definition: Regular expressions r1 and r r 2 are equivalent if L ( r1 ) = L( r2 ) 48
Example L = { all strings without two consecutive 0 r1 = (1 + 01) *(0 + λ) r2 = (1*011*)*(0 *)*( + λ ) + 1*(0 + λ ) ( 1 2 L 1 and 2 are equivalent L r ) = L ( r ) = r r 2 regular expressions. 49