strings and languages
play

Strings and Languages -- Glynda, the good witch of the North What is - PDF document

It is always best to start at the beginning Strings and Languages -- Glynda, the good witch of the North What is a Language? What is a string? A string over is a finite sequence A language is a set of strings made of of symbols


  1. “It is always best to start at the beginning” Strings and Languages -- Glynda, the good witch of the North What is a Language? What is a string? • A string over Σ is a finite sequence • A language is a set of strings made of of symbols from a given alphabet. (possibly empty) of elements of Σ . • An alphabet is a finite set of symbols (usually • ε denotes the null string , the string with no denoted by Σ ) symbols. – Examples of alphabets: – Example strings over {a, b} • {0, 1} • ε , a, aa, bb, aba, abbba • { α , β , χ , δ , ε , φ , γ , η } • {a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t , u, v, w, x, y, – NOT strings over {a, b} z} • aaaa…., abca • {a} The length of a string Strings and languages • For any alphabet Σ , the set of all strings • The length of a string x , denoted | x |, is the over Σ is denoted as Σ * . number of symbols in the string • A language over Σ is a subset of Σ * – Example: – Example • |abbab| = 5 • {a,b} * = { ε , a, b, aa, bb, ab, ba, aaa, bbb, baa, …} • |a| = 1 – Example Languages over {a,b} • |bbbbbbb| = 7 • { ε , a, b, aa, bb} ∅ • | ε | = 0 • {x ∈ {a,b} * | |x| = 8} {x ∈ {a,b} * | |x| is odd} • {x ∈ {a,b} * | n a (x) = n b (x)} { ε } • {x ∈ {a,b} * | n a (x) =2 and x starts with b}

  2. Concatenation of String Some string related definitions • x is a substring of y if there exists w,z ∈ Σ * • For x , y ∈ Σ * (possibly ε ) such that y = wxz. – xy is the concatenation of x and y . – car is a substring of carnage , descartes , vicar , • x = aba, y = bbb, xy=ababbb car , but not a substring of charity. • For all x , ε x = x ε = x • x is a suffix of y if there exists w ∈ Σ * such – x i for an integer i, indicates concatenation of x, i that y = wx . times • x is a prefix of y if there exists z ∈ Σ * such • x = aba, x 3 = abaabaaba • For all x, x 0 = ε that y = xz . Operations on Languages Concatenation of Languages • If L 1 and L 2 are languages over Σ * • Since languages are simply sets of strings, regular set operations can be applied: – L 1 L 2 = { xy | x ∈ L 1 and y ∈ L 2 } – For languages L 1 and L 2 over Σ * – Example: • L 1 ∪ L 2 = all strings in L 1 or L 2 • L 1 = {hope, fear} • L 1 ∩ L 2 = all strings in both L 1 and L 2 • L 2 = {less, fully} • L 1 – L 2 = strings in L 1 that are not in L 2 • L 1 L 2 = {hopeless, hopefully, fearless, • L’ = Σ * – L fearfully} Concatenation of Languages Kleene Star Operation • If L is a language over Σ * • The set of strings that can be obtained by – L k is the set of strings formed by concatenating any number of elements of a language L is called the Kleene Star, L * concatenating elements of L, k times. ∞ – Example: * = U i = 0 ∪ 1 ∪ 2 ∪ 3 ∪ 4 L L L L L L L ... • L = {aa, bb} = i 0 • L 3 = {aaaaaa, aaaabb, aabbaa, aabbbb, � Note that since, L * contains L 0 , ε is an bbbbbb, bbbbaa, bbaabb, bbaaaa} • L 0 = { ε } element of L *

  3. Kleene Star Operation Specifying Languages • The set of strings that can be obtained by • How do we specify languages? concatenating one or more elements of a language – If language is finite, you can list all of its L is denoted L + strings. + U ∞ = = ∪ ∪ ∪ • L = {a, aa, aba, aca} i 1 2 3 4 L L L L L L ... – Using basic Language operations = i 1 • L= {aa, ab} * ∪ {b}{bb} * – Descriptive: • L = {x | n a (x) = n b (x)} Specifying Languages Recursive Definitions • Next we will define how to specify • Definition is given in terms of itself languages recursively – Example (factorial) 4! = 4 * 3! } • In future classes, we will describe how to = 4 * (3 * 2!) ! { 1 if n = 0 specify languages by defining a mechanism = n = 4 * (3 * (2 * 1!)) − n * ( n 1 )! otherwise for generating the language = 4 * (3 * (2 * (1 * 0!))) = 4 * (3 * (2 * (1 * 1)))) • Any questions? = 24 Recursive Definitions and Languages Recursive Definitions and Languages • Languages can also be described by using a • Example: recursive definition Recursive definition of Σ * – 1. Initial elements are added to your set (BASIS) 2. Additional elements are added to your set by 1. ε ∈ Σ * applying a rule(s) to the elements already in 2. For all x ∈ Σ * and all a ∈ Σ , xa ∈ Σ * your set (INDUCTION) 3. Nothing else is in Σ * unless it can be obtained 3. Complete language is obtained by applying by a finite number of applications of rules 1 step 2 infinitely and 2.

  4. Recursive Definitions and Languages Recursive Definitions and Languages • Let’s iterate through the rules for Σ = {a,b} • Example: – i=0 Σ * = { ε } – Recursive definition of L * – i=1 Σ * = { ε , a, b} – i=2 Σ * = { ε , a, b, aa, ab, ba, bb} 1. ε ∈ L * – i=3 Σ * = { ε , a, b, aa, ab, ba, bb, aaa, aab, aba, 2.For all x ∈ L and all y ∈ L, xy ∈ L * abb, baa, bab, bba, bbb} 3.Nothing else is in L * unless it can be obtained by a finite number of applications of rules 1 and – …and so on 2. Recursive Definitions and Languages Recursive Definitions – another Example • Let’s iterate through the rules for L = {aa,bb} • Example: Palindromes – i=0 L * = { ε } – A palindrome is a string that is the same read – i=1 L * = { ε , aa, bb} left to right or right to left – i=2 L * = { ε , aa, bb, aaaa, aabb, bbbb, bbaa} – First half of a palindrome is a “mirror image” – i=3 L * = { ε , aa, bb, aaaa, aabb, bbbb, bbaa, aaaaaa, of the second half aaaabb, aabbaa, aabbbb, bbbbaa, bbbbbb, …} – Examples: • a, b, aba, abba, babbab. – …and so on Recursive Definitions – another Example Recursive Definitions – another Example • Let’s iterate through the rules for pal over Σ • Recursive definition for palindromes (pal) over Σ = {a,b} 1. ε ∈ pal – i=0 pal = { ε , a, b} 2. For any a ∈ Σ , a ∈ pal – i=1 pal = { ε , a, b, aaa, aba, bab, bbb} 3. For any x ∈ pal and a ∈ Σ , axa ∈ pal – i=2 pal = { ε , a, b, aaa, aba, bab, bbb, aaaaa, aabaa, ababa, abbba, baaab, ababa, bbabb, 4. No string is in pal unless it can be obtained by bbbbb} rules 1-3

  5. Recursive Definitions – yet another Example Recursive Definitions – yet another Example • Example: Fully parenthesized algebraic • Recursive definition for AE expressions (AE) 1.a ∈ AE – Σ = { a, (, ), +, - } 2.For any x, y ∈ AE (x + y) and (x – y) ∈ AE – All expressions where the parens match 3.No string is in pal unless it can be obtained by correctly are in the language rules 1-2 – Examples: • a, (a + a), (a + (a – a)), ( (a + a) – (a + a)), etc. Recursive Definitions – yet another Example Recursive Definitions – a final Example • L = {x ∈ {0,1} * | x = 0 i 1 j and i ≥ j ≥ 0} • Let’s iterate through the rules for AE – i=0 AE = {a} – In English: – i=1 AE = { a, (a+a), (a-a) } • strings over the alphabet {0,1} • each string contains zero or more 0’s followed by a – i=2 AE = {a, (a+a), (a-a) , (a + ( a + a)), zero or more 1’s (a – (a + a)), ( a + ( a – a)), ( a – ( a – a)), ((a • the number of 1’s is greater than or equal to the + a) + a), ( (a + a) – a), …} number of 0’s Recursive Definitions and Languages Recursive Definitions – a final Example L = {x ∈ {0,1} * | x = 0 i 1 j and i ≥ j ≥ 0} • • Questions on Recursive Definition? • A recursive definition ε ∈ L 1. For any x ∈ L, both 0 x and 0 x 1 ∈ L 2. • Functions on strings and languages can also 3. No strings are in L unless it can be obtained using rules 1-2. be defined recursively. Later we will prove that this definition does indeed describe L.

  6. Structural Induction Structural Induction • When dealing with languages, it is • Principles sometime cumbersome to restate the – Suppose problems in terms of an integer. • U is a set, • I is a subset of U (BASIS), • Op is a set of operations on U (INDUCTION). • For languages described using a recursive • L is a subset of U defined recursively as follows: definition, another type of induction, – I ⊆ L structural induction, is useful. – L is closed under each operation in Op – L is the smallest set satisfying 1 & 2 Structural Induction Structural Induction • Then – Recall this recursive definition of a language L – To prove that every element of L has some ε ∈ L 1. property P, it is sufficient to show: 2. For any x ∈ L, both 0 x and 0 x 1 ∈ L 1. Every element of I has property P 3. No strings are in L unless it can be obtained using rules 1-2. 2. The set of elements of L having property P is And: closed under Op A = {x ∈ {0,1} * | x = 0 i 1 j and i ≥ j ≥ 0} Show L ⊆ A by structural induction #2: If x ∈ L has property P, Op(x) also must have property P Structural Induction Structural Induction • Principles • To prove that every element of L has some property P: – Suppose • U is a set U = {a,b} * – Our property is: I = { ε } A = {x ∈ {0,1} * | x = 0 i 1 j and i ≥ j ≥ 0} • I is a subset of U, • Op is a set of ops on U. Op = {0x, 0x1} P(x) is true if x ∈ A. • L is a subset of U defined recursively as follows: – I ⊆ L – L is closed under each operation in Op – L is the smallest set satisfying 1 & 2.

Recommend


More recommend