MELJUN CORTES Automata Theory 4

Embed Size (px)

Citation preview

  • 8/13/2019 MELJUN CORTES Automata Theory 4

    1/20

    CSC 3130: Automata theory and formal languages

    Properties of regular languages

    MELJUN P. CORTES MBA MPA BSCS ACS

    MELJUN CORTES

  • 8/13/2019 MELJUN CORTES Automata Theory 4

    2/20

    Operations that preserve regularity

    In the last lecture we saw three operations thatpreserve regularity:

    Union: If L, Lare regular languages, so is L L

    Concatenation: If L, Lare regular languages, so is LL

    Star: If Lis a regular language, so is L*

    Exercise: If Lis regular, is L4also regular?

    Answer: Yes, because

    L4= ((LL)L)L

  • 8/13/2019 MELJUN CORTES Automata Theory 4

    3/20

    Example

    The language Lof strings that end in 101isregular

    How about the language Lof strings that do not

    end in 101?

    (0+1)*101

  • 8/13/2019 MELJUN CORTES Automata Theory 4

    4/20

  • 8/13/2019 MELJUN CORTES Automata Theory 4

    5/20

    Complement

    The complementLof a language Lis the set ofall strings (over S) that are not in L

    Examples (S= {0, 1}) L1= all strings that end in 101

    L1=all strings that do not endin 101

    = all strings end in 000, , 111or have length 0, 1, or

    2

    L2= 1* = {e, 1, 11, 111, }

    L2= all strings that contain at least one 0

    = (0 + 1)*0(0 + 1)*

  • 8/13/2019 MELJUN CORTES Automata Theory 4

    6/20

    Closure under complement

    If Lis a regular language, is Lalso regular? Previous examples indicate answer should be

    yes

    TheoremIf Lis a regular language, so is L.

  • 8/13/2019 MELJUN CORTES Automata Theory 4

    7/20

    Proof of closure under complement

    To prove this in general, we can use any of theequivalent definitionsfor regular languages:

    In this proof DFA definitionwill be mostconvenient

    We will assume L is accepted by a DFA, and show the

    same forL

    regularexpression

    DFANFA

  • 8/13/2019 MELJUN CORTES Automata Theory 4

    8/20

    Proof of closure under complement

    Suppose Lis regular, then it is accepted by aDFA M

    Now consider the DFA Mwith the accepting and

    rejecting states ofMreversed

  • 8/13/2019 MELJUN CORTES Automata Theory 4

    9/20

    Proof of closure under complement

    Now for every input xS*:Maccepts x

    After processing x,

    Mends inan

    accepting state

    After processing x, Mends in

    anrejecting state

    Mrejects x

    Language of Mis L

    Lis regular

  • 8/13/2019 MELJUN CORTES Automata Theory 4

    10/20

    A warning

    NFA for language of strings ending in 101

    Give NFA that accepts strings that do notend in101

    1 0 1

    0, 1

    q0 q1 q2 q3

    1 0 1

    0, 1

    q0

    q1

    q2

    q3wrong!

  • 8/13/2019 MELJUN CORTES Automata Theory 4

    11/20

    Intersection

    The intersection LLis the set of strings thatare in both Land L

    Examples:

    If L, Lare regular, is LLalso regular?

    L= (0 + 1)*111 L = 1* LL = ?

    L= (0 + 1)*101 L = 1* LL = ?

  • 8/13/2019 MELJUN CORTES Automata Theory 4

    12/20

    Closure under intersection

    Theorem

    Proof:

    If L andLare regular languages, so is L

    L.

    Lregular Lregular

    Lregular Lregular

    L Lregular

    But L L = LL

    LL regular

    LL regular.

  • 8/13/2019 MELJUN CORTES Automata Theory 4

    13/20

    Reversal

    The reversalwR

    of a string wis wwrittenbackwards

    The reversalLRof a languageLis the language

    obtained by reversing all its strings

    w= cave wR= evac

    L= {push, pop} LR= {hsup, pop}

  • 8/13/2019 MELJUN CORTES Automata Theory 4

    14/20

    Reversal of regular languages

    L= all strings that end in 101is regular

    How about LR?

    This is the language of all strings beginningin 101

    Yes, because it is represented by

    (0+1)*101

    101(0+1)*

  • 8/13/2019 MELJUN CORTES Automata Theory 4

    15/20

    Closure under reversal

    Theorem

    Proof

    We will use the representation of regular languages by

    regular expressions

    If Lis a regular language, so is LR.

    regular

    expressionDFANFA

  • 8/13/2019 MELJUN CORTES Automata Theory 4

    16/20

    Proof of closure under reversal

    If L is regular, then there is a regular expressionEthat describes it

    We will give a systematic way of reversingE

    Recall that a regular expression can be of thefollowing types:

    Special expressions and e

    Alphabet symbols a, b,

    The union, concatenation, or star of simplerexpressions

    In each of these cases we show how to do a

    reversal

  • 8/13/2019 MELJUN CORTES Automata Theory 4

    17/20

    Proof of closure under reversal

    regular expressionE

    e

    a (alphabet symbol)

    E1+ E2

    reversalER

    E1E2

    E1*

    e

    a

    E1R+ E2

    R

    E2RE1

    R

    (E1R)*

  • 8/13/2019 MELJUN CORTES Automata Theory 4

    18/20

    Applications: text search

    Text search: grep Looks for occurrences of a regular expressionin a file

    Syntax of regular expressions in grep

    [atr]means the set {a, t, r}

    [b-e]means {b, c, d, e} (in ASCII/Unicode ordering)

    |means +(union), *means star

    ? means zero or one: R?is e+ R

    +meansone or more

    : R+is RR*

    {n}means ncopies of: R{5}is RRRRR

  • 8/13/2019 MELJUN CORTES Automata Theory 4

    19/20

    Regular expressions in grep

    Say we have a file w.txt

    Want to know all sunny days> grep sunny w.txt

    Any cloudy days in April 08?> grep 4/[0-9]*/08 ([0-9]|C| )* cloudy w.txt

    Any consecutive string of 7 sunny days?> grep (([0-9]|/|C| )* sunny\n){7} w.txt

    1/1/08 14C rain\n

    1/2/08 17C sunny\n

    1/3/08 18C sunny\n

  • 8/13/2019 MELJUN CORTES Automata Theory 4

    20/20

    Implementation of grep

    One way to implement grepis to convert theregular expression into a DFA and then run the

    DFA on this text file

    Two issues that arise:

    grep looks for patterns inside text, while the DFA

    processes the input as a whole

    DFA only produces yes/noanswer, while grepoutputs linesthat contain given pattern

    How would you resolve these issues?