meljun cortes automata theory 3

Upload: meljun-cortes-mbampa

Post on 04-Jun-2018

229 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/13/2019 MELJUN CORTES Automata Theory 3

    1/32

    CSC 3130: Automata theory and formal languages

    Regular expressions

    MELJUN P. CORTES MBA MPA BSCS ACS

    MELJUN CORTES

  • 8/13/2019 MELJUN CORTES Automata Theory 3

    2/32

  • 8/13/2019 MELJUN CORTES Automata Theory 3

    3/32

    Operations on languages

    The concatenationof languages L1and L2is

    Similarly, we write Lnfor LLL(ntimes)

    The unionof languages L1L2 is the set of all

    strings that are in L1or in L2

    Example:L1= {01, 0}, L2= {e, 1, 11, 111, }.What isL1L2 and L1L2?

    L1L2= {st: s L1, t L2}

  • 8/13/2019 MELJUN CORTES Automata Theory 3

    4/32

    Operations on languages

    The star(Kleene closure) of Lare all stringsmade up of zero or more chunks from L:

    This is always infinite, and always contains e

    Example:L1= {01, 0}, L2= {e, 1, 11, 111, }.What isL1

    *and L2*?

    L*= L0L1L2

  • 8/13/2019 MELJUN CORTES Automata Theory 3

    5/32

    Constructing languages with operations

    Lets fix an alphabet, say S= {0, 1} We can construct languages by starting with

    simple ones, like {0}, {1}and combining them

    {0}({0}{1})*all strings that start with

    0

    ({0}{1}*)({1}{0}*)

    0(0+1)*

    01*+10*

  • 8/13/2019 MELJUN CORTES Automata Theory 3

    6/32

    Regular expressions

    A regular expressionover Sis an expressionformed using the following rules:

    The symbol is a regular expression

    The symbol eis a regular expression

    For every a S, the symbol ais a regular expression

    If Rand Sare regular expressions, so are RS, R+Sand

    R*.

    Definition of regular language

    A language is regularif it is represented

    by a regular expression

  • 8/13/2019 MELJUN CORTES Automata Theory 3

    7/32

    Examples

    1. 01* = {0, 01, 011, 0111, ..}

    2. (01*)(01) = {001, 0101, 01101, 011101, ..}

    3. (0+1)*

    4. (0+1)*01(0+1)*

    5. ((0+1)(0+1)+(0+1)(0+1)(0+1))*

    6. ((0+1)(0+1))*+((0+1)(0+1)(0+1))*

    7. (1+01+001)*(e+0+00)

  • 8/13/2019 MELJUN CORTES Automata Theory 3

    8/32

    Examples

    Construct a RE over S= {0,1}that representsAll strings that have two consecutive 0s.

    All strings exceptthose with two consecutive0s.

    All strings with an even numberof 0s.

    (0+1)*00(0+1)*

    (1*01)*1* + (1*01)*1*0

    (1*01*01*)*

  • 8/13/2019 MELJUN CORTES Automata Theory 3

    9/32

    Main theorem for regular languages

    Theorem

    A language is regularif and only if it is the

    language of some DFA

    DFA NFAregular

    expression

    regular languages

  • 8/13/2019 MELJUN CORTES Automata Theory 3

    10/32

  • 8/13/2019 MELJUN CORTES Automata Theory 3

    11/32

    What is an eNFA?

    An eNFA is an extension of NFA where sometransitions can be labeled by e

    Formally, the transition function of an eNFA is a

    function

    d:Q ( S {e}) subsets ofQ

    The automaton is allowed to follow e-transitions

    without consuming an input symbol

  • 8/13/2019 MELJUN CORTES Automata Theory 3

    12/32

    Example of eNFA

    q0 q1 q2e,b

    a

    a

    eS= {a, b}

    Which of the following is accepted by this eNFA:

    aab, bab, ab, bb, a,e

  • 8/13/2019 MELJUN CORTES Automata Theory 3

    13/32

    M2

    Examples: regular expression eNFA

    R1= 0

    R2= 0 + 1

    R3= (0 + 1)*

    q0 q10

    q0 q1

    e

    ee

    e q2 q30

    q4 q51

    q0 q1eM2

    e

    e

    e

  • 8/13/2019 MELJUN CORTES Automata Theory 3

    14/32

    General method

    regular expr eNFA

    q0

    e q0

    symbol a q0 q1a

    RS q0 q1eMR MS

    ee

  • 8/13/2019 MELJUN CORTES Automata Theory 3

    15/32

    Convention

    When we draw a box around an eNFA: The arrow going inpoints to the start state

    The arrow going outrepresents all transitions going

    out of accepting states

    None of the states inside the box is accepting The labels of the states inside the box are distinctfrom

    all other states in the diagram

  • 8/13/2019 MELJUN CORTES Automata Theory 3

    16/32

    General method continued

    regular expr eNFA

    R +S q0 q1

    eMR

    MSee

    e

    R* q0 q1eMR

    e

    ee

  • 8/13/2019 MELJUN CORTES Automata Theory 3

    17/32

    Road map

    eNFA

    regularexpression

    NFA

    DFA

  • 8/13/2019 MELJUN CORTES Automata Theory 3

    18/32

    Example of eNFA to NFA conversion

    q0 q1 q2e,b a

    a

    eeNFA:

    Transition table of corresponding NFA:

    states

    inputs

    a b

    q0q1

    q2

    {q1, q2}{q0, q1, q2}

    {q0, q1, q2}

    Accepting states of NFA:{q0, q1, q2}

  • 8/13/2019 MELJUN CORTES Automata Theory 3

    19/32

  • 8/13/2019 MELJUN CORTES Automata Theory 3

    20/32

    General method

    To convert an eNFA to an NFA: Statesstay the same

    Start statestays the same

    The NFA has a transitionfrom qito qjlabeled aiff the

    eNFA has a pathfrom qito qjthat contains onetransition labeled aand all other transitions labeled e

    The accepting statesof the NFA are all states that can

    reach some accepting state of eNFA using only e-

    transitions

  • 8/13/2019 MELJUN CORTES Automata Theory 3

    21/32

    Why the conversion works

    In the originale-NFA, when given input a1a2antheautomaton goes through a sequence of states:

    q0q1q2 qm

    Some e-transitions may be in the sequence:

    q0... qi1... qi2 qin

    In the new NFA, each sequence of states of theform:

    qik... qik+1

    will be represented by a single transitionqik qik+1

    because of the way we construct the NFA.

    e e e e e ea1 a

    2

    e eak+1

    ak+1

  • 8/13/2019 MELJUN CORTES Automata Theory 3

    22/32

    Proof that the conversion works

    More formally, we have the following invariantforany k 1:

    We prove this by induction on k

    When k= 0, the eNFA can be in more states,while the NFA must be in q0

    After reading kinput symbols, the set of

    states that the eNFA and NFA can be in

    are exactly the same

  • 8/13/2019 MELJUN CORTES Automata Theory 3

    23/32

    Proof that the conversion works

    When k 1(input is notthe empty string) If eNFA is in an accepting state, so is NFA

    Conversely, if NFA is an accepting state qi, then some

    accepting state of eNFA is reachable from qi, so eNFA

    accepts also

    When k= 0(input is the empty string)

    The eNFA accepts iff one of its accepting states isreachable from q0

    This is true iff q0is an accepting state of the NFA

  • 8/13/2019 MELJUN CORTES Automata Theory 3

    24/32

    From DFA to regular expressions

    eNFA

    regularexpression

    NFA

    DFA

  • 8/13/2019 MELJUN CORTES Automata Theory 3

    25/32

    Example

    Construct a regular expression for this DFA:

    1

    1

    0

    0

    q1 q2

    (0 + 1)*0 + e

  • 8/13/2019 MELJUN CORTES Automata Theory 3

    26/32

    General method

    We have a DFA Mwith states q1, q2,qn We will inductively define regular expressions Rij

    k

    Rijkwill be the set of all strings that take Mfrom

    qito qjwith intermediate statesgoing throughq1, q2,or qkonly.

  • 8/13/2019 MELJUN CORTES Automata Theory 3

    27/32

    Example

    1

    1

    0

    0

    q1 q2

    R110= {e, 0} = e+ 0

    R120= {1} = 1

    R22

    0= {e, 1} = e+ 1

    R111= {e, 0, 00, 000, ...}= 0*

    R121= {1, 01, 001, 0001, ...}= 0*1

  • 8/13/2019 MELJUN CORTES Automata Theory 3

    28/32

  • 8/13/2019 MELJUN CORTES Automata Theory 3

    29/32

  • 8/13/2019 MELJUN CORTES Automata Theory 3

    30/32

    Final step

    Suppose the DFA start state is q1, and theaccepting states are F = {qj1qj2

    qjt}

    Then the regular expression for this DFA is

    R1j1n+ R1j2

    n+ .. + R1jtn

  • 8/13/2019 MELJUN CORTES Automata Theory 3

    31/32

  • 8/13/2019 MELJUN CORTES Automata Theory 3

    32/32

    Example

    Give a RE for the following DFA using thismethod:

    1

    1

    0

    0

    q0 q1