regular expression2

Upload: sharf-ali

Post on 08-Apr-2018

226 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/7/2019 regular expression2

    1/12

    CHAPTERCHAPTER 44

    REGULARREGULAREXPRESSIONSEXPRESSIONS

    ::

  • 8/7/2019 regular expression2

    2/12

    Regular Expressions and LanguagesRegular Expressions and Languages A regular expression is a notation to representA regular expression is a notation to represent

    languages, i.e. a set of strings, where the set is eitherlanguages, i.e. a set of strings, where the set is eitherfinite or contains strings that are generated usingfinite or contains strings that are generated using simplesimplerecursive rules. The languages represented by regularrecursive rules. The languages represented by regular

    expressions are called regular languagesexpressions are called regular languages..

    Regular expressions:Regular expressions: To simplify the notations of regular languages, andTo simplify the notations of regular languages, and

    drawing analogy to arithmetic expressions used indrawing analogy to arithmetic expressions used inalgebra, we could replace the union symbolalgebra, we could replace the union symbol with thewith the

    plus sign +, drop the bracesplus sign +, drop the braces {{ andand }} for sets, and usefor sets, and useparenthesesparentheses (( andand )) for groupingfor grouping

  • 8/7/2019 regular expression2

    3/12

    Some examples of Regular expressionsSome examples of Regular expressionsare as followsare as follows

    The set of strings overThe set of strings over {{aa,, bb}} that have at least twothat have at least twooccurrences of symboloccurrences of symbol aa::

    (a + b)* a(a + b)* a(a + b)*(a + b)* a(a + b)* a(a + b)* The set of strings over {The set of strings over {aa,, bb}} that contain up tothat contain up to 22

    symbols:symbols:

    ++ aa++ bb ++ aaaa++ abab ++ baba++ bbbb

    The set of strings overThe set of strings over {{aa,, bb}} that begin withthat begin with aa, and, andhave an even number ofhave an even number of bb::

    aa((aa**baba**bb)*)*aa*.*.

    The set of strings overThe set of strings over {{aa,, bb}} that do not contain thethat do not contain thesubstringsubstring aaaa::

    bb*(*(abbabb*)*(*)*(++ aa))

  • 8/7/2019 regular expression2

    4/12

    Laws and rules for manipulating regularLaws and rules for manipulating regularexpressions:expressions:

    LetLet LL,, MM, and, and NN denote regular expressionsdenote regular expressions..(1) (Associative law(1) (Associative law))

    LL((MNMN) = () = (LMLM))NN. (This is true for any sets. (This is true for any sets LL,, MM, and, and NN of strings)of strings)

    (2) (distributive laws)(2) (distributive laws)LL((MM ++ NN) =) = LMLM ++ LNLN and (and (MM ++ NN))LL == MLML ++ NLNL

    . (3). (3) ((LL*)* =*)* = LL*. (Both sides contain all possible strings that are made up of*. (Both sides contain all possible strings that are made up ofstrings ofstrings of LL.).)

    (4)(4) * =* = * =* =. (The Kleene star always contains the empty string. (The Kleene star always contains the empty string ).).

    (5)(5) ((LL*)*)LL == LL((LL*). (Both sides equal*). (Both sides equal LL LL22 LL33 , which is denoted, which is denoted LL+)+)

    (6)(6) ((LL**MM*)* = (*)* = (LL++ MM)*.)*.

  • 8/7/2019 regular expression2

    5/12

    DEFINTIONDEFINTION

    If S and T are of strings of letters (whether they are finiteIf S and T are of strings of letters (whether they are finite

    or infinite sets), we define the product set of strings ofor infinite sets), we define the product set of strings ofletters to beletters to be

    ST= {all combinations of a string from S concatenated withST= {all combinations of a string from S concatenated witha string from T }a string from T }

    EXAMPLESEXAMPLES If S = {a aa aaa} T ={bb bbb}If S = {a aa aaa} T ={bb bbb}

    Then ST = {abb abbb aabb aabbb aaabb aaabbb}Then ST = {abb abbb aabb aabbb aaabb aaabbb}

    (a + bb + bab)(a + ab) = aa + aab + bba + bbab +(a + bb + bab)(a + ab) = aa + aab + bba + bbab +

    baba + bababbaba + babab ((+ x + xx)(y*) = y* + xy* + xxy*+ x + xx)(y*) = y* + xy* + xxy*

  • 8/7/2019 regular expression2

    6/12

    Rules define the language associatedRules define the language associatedwith regular expressionwith regular expression

    RuleRule 11The language associated withThe language associated with is just{is just{}, a one}, a one word language.word language.

    RuleRule 22

    If rIf r11 is a regular expressionis a regular expression associated with the language Lassociated with the language L11 andand rr22 isis

    a regular expression associated with the language La regular expression associated with the language L22 then,then,(i) The regular expression ((i) The regular expression (rr11) () (rr22) is associated with the language) is associated with the language

    LL11 times Ltimes L22

    Language (Language (rr11 rr22) = L) = L11LL22

    (ii) The regular expression(ii) The regular expression rr11 ++ rr22 is associated with the languageis associated with the languageformed by the union of the sets Lformed by the union of the sets L11 and Land L22

    Language (rLanguage (r11 +r+r22) = L) = L11 +L+L22

    (iii) The language associated with(iii) The language associated with the regular expression (the regular expression (rr11)* is L)* is L11* ,the* ,thekleene closure of the set Lkleene closure of the set L11 as a set of wordsas a set of words

    Language (Language (rr11*) = L*) = L11**

  • 8/7/2019 regular expression2

    7/12

    THEOREMTHEOREM

    If L is a finite language (a language with only finitelyIf L is a finite language (a language with only finitely

    many words), then L can be defined by a regularmany words), then L can be defined by a regularexpressionexpression

    PROOFPROOF

    Let L = {wLet L = {w11, . . . w, . . . wnn}. Then each w}. Then each wjj can be described bycan be described by

    a regular expression (write out the letters and show thisa regular expression (write out the letters and show thisis a regular expression). Then take the union.is a regular expression). Then take the union.

    EXAMPLEEXAMPLE

    LetLet

    L = {L = {baa abbba bababa}baa abbba bababa}The regular expression we get from theorem isThe regular expression we get from theorem is

    baa + abbba + babababaa + abbba + bababa

  • 8/7/2019 regular expression2

    8/12

    EXAMPLEEXAMPLE

    We have L = language (bb* + abb* + aabb*) ((aWe have L = language (bb* + abb* + aabb*) ((a

    + aa)bb*)*(b* + a + aa).+ aa)bb*)*(b* + a + aa).We can simplify this expression.We can simplify this expression.

    The first factor can be written (The first factor can be written ( ++ a + aa) bb*a + aa) bb*

    The second factor can be written ((The second factor can be written (( ++ a +a +

    aa)bb*)* Hence, we can writeaa)bb*)* Hence, we can write L = language ( ((L = language ( (( ++ a + aa)bb*)+ (b* + a +a + aa)bb*)+ (b* + a +

    aa))aa))

  • 8/7/2019 regular expression2

    9/12

    PROBLEMSPROBLEMS

    Construct a regular expression defining each of theConstruct a regular expression defining each of thefollowing languages over the alphabetfollowing languages over the alphabet ={a ,b}={a ,b}

    (i)(i) All the words with exactly two aAll the words with exactly two ass

    (ii)(ii) All the words that have at least one a and at leastAll the words that have at least one a and at leastone bone b

    SolutionSolution(i)(i) We use the expression b*ab*ab*We use the expression b*ab*ab*

    which describes such words as aab, baba, andwhich describes such words as aab, baba, andbbbabbbab. To make the word aabbbbabbbab. To make the word aab

    (ii) we use the expression (a + b)*a(a + b)* b(a + b)*(ii) we use the expression (a + b)*a(a + b)* b(a + b)*then requiring thatthen requiring thatanan a precedea precede aa b in the word. Suchb in the word. Such

    words as ba and bbaaaa are not included in this setwords as ba and bbaaaa are not included in this set

  • 8/7/2019 regular expression2

    10/12

    Let us reconsider the regular expressionLet us reconsider the regular expression(a + b)*a(a + b)*b(a + b)*(a + b)*a(a + b)*b(a + b)*

    Show that this is equivalent toShow that this is equivalent to

    (a + b)*ab(a + b)*(a + b)*ab(a + b)*

    SolutionSolutionLanguage ((a +b)*a(a + b)*b(a + b)*)Language ((a +b)*a(a + b)*b(a + b)*)

    = language ((a + b)*ab(a +b)*)= language ((a + b)*ab(a +b)*)

    = all words with at least one a and at least one b= all words with at least one a and at least one b

    Then two regular expression are equivalent because theyThen two regular expression are equivalent because theydescribe the same languagedescribe the same language

  • 8/7/2019 regular expression2

    11/12

    letletrr11,r,r22 andand rr33 be three regular expressions show that thebe three regular expressions show that the

    languagelanguage associated withassociated with (r(r11+r+r22)r)r33 is the same as theis the same as thelanguagelanguage associated withassociated with rr11rr33 + r+ r22rr33.. show thatshow thatrr11(r(r22 + r+ r33)) isisequivalent toequivalent to rr11rr22 + r+ r11rr33.. This will be the same asThis will be the same as provingprovingaa distributive lawdistributive law for regular expressionsfor regular expressions

    SolutionSolution(Distributive law) (r(Distributive law) (r11+r+r22) r) r33 = r= r11rr33 + r+ r22rr33..

    ProofProof

    Let L = language ((rLet L = language ((r11 + r+ r22) r) r33), and let M = language (r), and let M = language (r11rr33 ++

    rr22rr33). Let w). Let w L. Then w begins with either string of rL. Then w begins with either string of r11 ororstring of rstring of r22, and the rest of w consists of string of r, and the rest of w consists of string of r33;;hence, w = rhence, w = r11rr33 or w =ror w =r22rr33 . Thus w. Thus w M. The converse isM. The converse issimilar.similar.

  • 8/7/2019 regular expression2

    12/12

    Show that the following pairs of regular expressionsShow that the following pairs of regular expressionsdefine the same language over the alphabetdefine the same language over the alphabet={a, b}={a, b}

    (i)(i)

    (ab)*a and a(ba)*(ab)*a and a(ba)*(ii)(ii) (a* + b)* and (a + b)*(a* + b)* and (a + b)*(iii) (a* + b*)* and (a + b)*(iii) (a* + b*)* and (a + b)*

    SolutionSolution

    (i) Let S = language ((ab)*a) and let T = language(i) Let S = language ((ab)*a) and let T = language(a(ba)*). Suppose w(a(ba)*). Suppose w S. Then w = (ab)S. Then w = (ab)^n^na for some na for some n N, so we can rewrite w as a(ba)N, so we can rewrite w as a(ba)^n^n. Hence w. Hence w T. TheT. Thereverse inclusion is proven in a similar manner.reverse inclusion is proven in a similar manner.

    (ii) Let S = language ((a* + b)*) and let T = language ((a(ii) Let S = language ((a* + b)*) and let T = language ((a+ b)*). Let w+ b)*). Let w S. Then w = (aS. Then w = (a^n^n11 b)b)^k^k11 . . . Hence w. . . Hence w T. The reverse inclusion is similar.T. The reverse inclusion is similar.

    (iii) Let S = language ((a* + b*)*) and let T = language((a(iii) Let S = language ((a* + b*)*) and let T = language((a+ b)*). Let w+ b)*). Let w S. The argument is the same as for (ii)S. The argument is the same as for (ii)