Module 2 How to design Computer Language Huma

  • Slides: 24
Download presentation
Module 2 How to design Computer Language Huma Ayub Software Construction Lecture 8

Module 2 How to design Computer Language Huma Ayub Software Construction Lecture 8

Previous Lecture Types of languages How a Language for Computers is developed Alphabets Words

Previous Lecture Types of languages How a Language for Computers is developed Alphabets Words Vs String Valid/In-valid alphabets Length of string Reverse of String Definiation of different languages defined over alphabets

Kleene Star Closure(*) l What is Closure Property ? ? l Given Σ, then

Kleene Star Closure(*) l What is Closure Property ? ? l Given Σ, then the Kleene Star Closure of the alphabet Σ, denoted by Σ*, is the collection of all strings defined over Σ, including Λ. l l l l l It is to be noted that Kleene Star Closure can be defined over any set of strings. Examples If Σ = {x} Then Σ* = {Λ, x, xxx, xxxx, …. } If Σ = {0, 1} Then Σ* = {Λ, 0, 1, 00, 01, 10, 11, …. } If Σ = {aa. B, c} Then Σ* = {Λ, aa. B, c, aa. B, aa. Bc, caa. B, cc, …. } Note Languages generated by Kleene Star Closure of set of strings, are infinite languages. (By infinite language, it is supposed that the language contains infinite many words, each of finite length).

PLUS Operation (+) l l l Plus Operation is same as Kleene Star Closure

PLUS Operation (+) l l l Plus Operation is same as Kleene Star Closure except that it does not generate Λ (null string), automatically. Example If Σ = {0, 1} Then Σ+ = {0, 1, 00, 01, 10, 11, …. } If Σ = {aab, c} Then Σ+ = {aab, c, aabaab, aabc, caab, cc, …. }

l l l l l Recursive definition of languages The following three steps are

l l l l l Recursive definition of languages The following three steps are used in recursive definition Some basic words are specified in the language. Rules for constructing more words are defined in the language. No strings except those constructed in above, are allowed to be in the language. Examples Defining language of INTEGER Step 1: 1 is in INTEGER. Step 2: If x is in INTEGER then x+1 and x-1 are also in INTEGER. Step 3: No strings except those constructed in above, are allowed to be in INTEGER.

Example Recursive l Defining language of EVEN l Step 1: 2 is in EVEN.

Example Recursive l Defining language of EVEN l Step 1: 2 is in EVEN. l Step 2: If x is in EVEN then x+2 and x-2 are also in EVEN. l Step 3: No strings except those constructed in above, are allowed to be in EVEN.

Example Recursive l l l l Defining the language factorial Step 1: As 0!=1,

Example Recursive l l l l Defining the language factorial Step 1: As 0!=1, so 1 is in factorial. Step 2: n!=n*(n-1)! is in factorial. Step 3: No strings except those constructed in above, are allowed to be in factorial. Defining the language PALINDROME, defined over Σ = {a, b} Step 1: a and b are in PALINDROME Step 2: if x is palindrome, then s(x)Rev(s) and xx will also be palindrome, where s belongs to Σ* Step 3: No strings except those constructed in above, are allowed to be in palindrome

Example Recursive l l l l l Defining the language {a nb n },

Example Recursive l l l l l Defining the language {a nb n }, n=1, 2, 3, … , of strings defined over Σ={a, b} Step 1: ab is in {a nbn} Step 2: if x is in {a nbn}, then axb is in {a nbn} Step 3: No strings except those constructed in above, are allowed to be in {an bn. } Defining the language L, of strings ending in a , defined over Σ={a, b} Step 1: a is in L Step 2: if x is in L then s(x) is also in L, where s belongs to Σ* Step 3: No strings except those constructed in above, are allowed to be in L note: Ending character is right most

Example Recursive l l l l l Defining the language L, of strings beginning

Example Recursive l l l l l Defining the language L, of strings beginning and ending in same letters , defined over Σ={a, b} Step 1: a and b are in L Step 2: (a)s(a) and (b)s(b) are also in L, where s belongs to Σ* Step 3: No strings except those constructed in above, are allowed to be in L Defining the language L, of strings containing aa or bb , defined over Σ={a, b} Step 1: aa and bb are in L Step 2: s(aa)s and s(bb)s are also in L, where s belongs to Σ* Step 3: No strings except those constructed in above, are allowed to be in L. Note : consisting[ fixed elements ] vs containing of [at least means aa, bb or both]

Regular Expression

Regular Expression

Regular Expression As discussed earlier that a* generates Λ, a, aaa, … and a+

Regular Expression As discussed earlier that a* generates Λ, a, aaa, … and a+ generates a, aaa, aaaa, …, so the language l L 1= {Λ, a, aaa, …} and L 2 = {a, aaa, aaaa, …} can simply be expressed by a* and a+, respectively. l a* and a+ are called the regular expressions (RE) for L 1 and L 2 respectively. l Note a+, aa* and a*a generate L 2. l

Recursive definition of Regular Expression(RE) l Step 1: Every letter of Σ including Λ

Recursive definition of Regular Expression(RE) l Step 1: Every letter of Σ including Λ is a regular expression. l Step 2: If r 1 and r 2 are regular expressions then l l r 1 * r 2 l r 1 + r 2 l r 1* and l r 2* l are also regular expressions. l Step 3: Nothing else is a regular expression. l

l l l Method 3 (Regular Expressions) Consider the language L={Λ, x, xxx, …}

l l l Method 3 (Regular Expressions) Consider the language L={Λ, x, xxx, …} of strings, defined over Σ = {x}. We can write this language as the Kleene star closure of alphabet Σ or L=Σ*={x}*. This language can also be expressed by the regular expression x*. Similarly the language L={x, xxx, …}, defined over Σ = {x}, can be expressed by the regular expression x+. Now consider another language L, consisting of all possible strings, defined over Σ = {a, b}. This language can also be expressed by the regular expression (a + b)*.

l l l Now consider another language L, of strings having exactly double a,

l l l Now consider another language L, of strings having exactly double a, defined over Σ = {a, b}, then it’s regular expression may be b*aab*. (cover end, mid and start) Why not (a+b)*aa (a+b)* Now consider another language L, of even length, defined over Σ = {a, b}, then it’s regular expression may be ((a+b))*. Now consider another language L, of odd length, defined over Σ = {a, b}, ? ? ? ?

Answer Then it’s regular expression may be l (a+b)(a+b))* or ((a+b))*(a+b).

Answer Then it’s regular expression may be l (a+b)(a+b))* or ((a+b))*(a+b).

Remark l It may be noted that a language may be expressed by more

Remark l It may be noted that a language may be expressed by more than one regular expression, while given a regular expression there exist a unique language generated by that regular expression.

Example Consider the language, defined over Σ = {a , b} of words having

Example Consider the language, defined over Σ = {a , b} of words having at least one a, may be expressed by a regular expression (a+b)*a(a+b)*. l Consider the language, defined over Σ ={a, b}, of words starting with double a and ending in double b then its l regular expression may be aa(a+b)*bb l l

l Consider the language, defined over Σ ={a, b} of words starting with a

l Consider the language, defined over Σ ={a, b} of words starting with a and ending in b OR l starting with b and ending in a, then its regular expression may be a(a+b)*b+b(a+b)*a Consider the language, defined over Σ = {a, b} of words having at least one a and one b, may be expressed by a l regular expression (a+b)*a(a+b)*b(a+b)*+ (a+b)*b(a+b)*a(a+b)*. l

The Language EVEN-EVEN l l l l l An important example Language of strings,

The Language EVEN-EVEN l l l l l An important example Language of strings, defined over Σ={a, b} having even number of a’s and even number of b’s. i. e. EVEN-EVEN = {Λ, aa, bb, aaaa, aabb, abab, abba, baab, baba, bbaa, bbbb, …}, its regular expression can be written as ((aa+bb)+(ab+ba)(aa+bb)*(ab+ba))* Note It is important to be clear about the difference of the following regular expressions r 1 = a*+b* r 2 = (a+b)* Here r 1 does not generate any string of concatenation of a and b, while r 2 generates such strings.

Equivalent Regular Expressions l l l Definition Two regular expressions are said to be

Equivalent Regular Expressions l l l Definition Two regular expressions are said to be equivalent if they generate the same language. Example Consider the following regular expressions r 1 = (a + b)* (aa + bb) r 2 = (a + b)*aa + ( a + b)*bb then both regular expressions define the language of strings ending in aa or bb. Note If r 1 = (aa + bb) and r 2 = ( a + b) then r 1+r 2 = (aa + bb) + (a + b) r 1 r 2 = (aa + bb) (a + b) = (aaa + aab + bba + bbb) (r 1)* = (aa + bb)*

Regular Languages The language generated by any regular expression is called a regular language.

Regular Languages The language generated by any regular expression is called a regular language. l It is to be noted that if r 1, r 2 are regular expressions, corresponding to the languages L 1 and L 2 l then the languages generated by r 1+ r 2, r 1 r 2( or r 2 r 1) and r 1*( or r 2*) are also regular languages. l

Regular Languages l l l Note It is to be noted that if L

Regular Languages l l l Note It is to be noted that if L 1 and L 2 are expressed by r 1 and r 2, respectively then the language expressed by r 1+ r 2, is the language L 1 + L 2 or L 1 ∪ L 2 r 1 r 2, , is the language L 1 L 2, of strings obtained by prefixing every string of L 1 with every string of L 2 r 1*, is the language L 1*, of strings obtained by concatenating the strings of L, including the null string.

All finite languages are regular l l l l l Example Consider the language

All finite languages are regular l l l l l Example Consider the language L, defined over Σ = {a, b}, of strings of length 2, starting with a, then L = {aa, ab}, may be expressed by the regular expression aa+ab. Hence L, by definition, is a regular language. Note It may be noted that if a language contains even thousand words, its RE may be expressed, placing ‘ + ’ between all the words. Here the special structure of RE is not important. Consider the language L = {aaa, aab, aba, abb, baa, bab, bba, bbb}, that may be expressed by a RE aaa+aab+aba+abb+baa+bab+bba+bbb, which is equivalent to (a+b)(a+b).

Quiz

Quiz