RE2FA(After Lesson 13)

download RE2FA(After Lesson 13)

of 66

Transcript of RE2FA(After Lesson 13)

  • 8/9/2019 RE2FA(After Lesson 13)

    1/66

    1

    RERE Finite AutomataFinite AutomataRERE Finite AutomataFinite Automata

    Can we build a finiteautomaton for every regular

    expression? Yes, build FA inductively

    based on the definition ofRegular Expression

  • 8/9/2019 RE2FA(After Lesson 13)

    2/66

    2

    NFANFANFANFA

    Nondeterministic FiniteAutomaton (NFA)

    Can have multipletransitions for one input

    in a given state Can have I - moves

  • 8/9/2019 RE2FA(After Lesson 13)

    3/66

    3

    Epsilon MovesEpsilon MovesEpsilon MovesEpsilon Moves moves

    machine can move from stateA to state B without consuminginput

    I

    A B

  • 8/9/2019 RE2FA(After Lesson 13)

    4/66

    4

    NFANFANFANFAoperation of the automaton is notcompletely defined by input

    1

    1

    0

    On input 11, automaton could bein either state

    A B C

  • 8/9/2019 RE2FA(After Lesson 13)

    5/66

    5

    Execution of FAExecution of FAExecution of FAExecution of FA

    A NFA can choose

    Whether to make I-moves.

    Which of multiple

    transitions to take for a

    single input.

  • 8/9/2019 RE2FA(After Lesson 13)

    6/66

    6

    Acceptance ofNFAAcceptance ofNFAAcceptance ofNFAAcceptance ofNFA

    NFA can get into multiple states Rule: NFA accepts if it can get

    in a final state

    1

    1

    0

    A B C

    0

  • 8/9/2019 RE2FA(After Lesson 13)

    7/66

    7

    DFA and NFADFA and NFADFA and NFADFA and NFA

    Deterministic Finite Automata

    (DFA)

    One transition per input perstate.

    No I - moves

  • 8/9/2019 RE2FA(After Lesson 13)

    8/66

    8

    Execution of FAExecution of FAExecution of FAExecution of FA

    A DFA

    can take only one path

    through the state graph.

    Completely determined by

    input.

  • 8/9/2019 RE2FA(After Lesson 13)

    9/66

    9

    NFA vs DFANFA vs DFANFA vs DFANFA vs DFA

    NFAs and DFAs recognize

    the same set of languages

    (regular languages)

    DFAs are easierto

    implement table driven.

  • 8/9/2019 RE2FA(After Lesson 13)

    10/66

    10

    NFA vs DFANFA vs DFANFA vs DFANFA vs DFA

    For a given language, the

    NFA can be simpler than

    the DFA.

    DFA can be exponentially

    largerthan NFA.

  • 8/9/2019 RE2FA(After Lesson 13)

    11/66

    11

    NFA vs DFANFA vs DFANFA vs DFANFA vs DFA

    NFAs are the key to

    automating RE DFA

    construction.

  • 8/9/2019 RE2FA(After Lesson 13)

    12/66

    12

    RERE NFA ConstructionNFA ConstructionRERE NFA ConstructionNFA Construction

    hompsons construction

    ( 1968)

    Build an NFA for each REterm.

    Combine NFAs withI-moves.

  • 8/9/2019 RE2FA(After Lesson 13)

    13/66

    13

    RERE NFA ConstructionNFA ConstructionRERE NFA ConstructionNFA Construction

    Key idea:

    NFA pattern for each

    symbol and each operator.

    Join them with I-moves in

    precedence order.

  • 8/9/2019 RE2FA(After Lesson 13)

    14/66

    14

    RERE NFA ConstructionNFA ConstructionRERE NFA ConstructionNFA Construction

    I

    s0

    as1

    NFA fora

    s0

    as1

    NFA forab

    s3

    bs4

  • 8/9/2019 RE2FA(After Lesson 13)

    15/66

    15

    RERE NFA ConstructionNFA ConstructionRERE NFA ConstructionNFA Construction

    s0

    a s1

    NFA fora

  • 8/9/2019 RE2FA(After Lesson 13)

    16/66

    16

    RERE NFA ConstructionNFA ConstructionRERE NFA ConstructionNFA Construction

    s0

    a s1

    NFA fora

    s3

    bs4NFA forb

  • 8/9/2019 RE2FA(After Lesson 13)

    17/66

    17

    RERE NFA ConstructionNFA ConstructionRERE NFA ConstructionNFA Construction

    s0

    a s1

    NFA fora

    s0

    as1

    s3

    bs4

    s3

    bs4NFA forb

  • 8/9/2019 RE2FA(After Lesson 13)

    18/66

    18

    RERE NFA ConstructionNFA ConstructionRERE NFA ConstructionNFA Construction

    I

    s0

    a s1

    NFA fora

    s0

    as1

    NFA forab

    s3

    bs4

    s3

    bs4NFA forb

  • 8/9/2019 RE2FA(After Lesson 13)

    19/66

    19

    RERE NFA ConstructionNFA ConstructionRERE NFA ConstructionNFA Construction

    I

    s0 s5

    s1

    as2

    NFA fora | b

    s3

    bs4

    I

    I

    I

  • 8/9/2019 RE2FA(After Lesson 13)

    20/66

    20

    RERE NFA ConstructionNFA ConstructionRERE NFA ConstructionNFA Construction

    s1

    as2

    NFA fora

  • 8/9/2019 RE2FA(After Lesson 13)

    21/66

    21

    RERE NFA ConstructionNFA ConstructionRERE NFA ConstructionNFA Construction

    s1

    as2

    s3

    bs4

    NFA fora and b

  • 8/9/2019 RE2FA(After Lesson 13)

    22/66

    22

    RERE NFA ConstructionNFA ConstructionRERE NFA ConstructionNFA Construction

    I

    s0 s5

    s1

    as2

    NFA fora | b

    s3

    bs4

    I

    I

    I

  • 8/9/2019 RE2FA(After Lesson 13)

    23/66

    23

    RERE NFA ConstructionNFA ConstructionRERE NFA ConstructionNFA Construction

    Is0 s4s1 a s2

    NFA fora*

    I

    I

    I

  • 8/9/2019 RE2FA(After Lesson 13)

    24/66

    24

    RERE NFA ConstructionNFA ConstructionRERE NFA ConstructionNFA Construction

    s1 a s2

    NFA fora

  • 8/9/2019 RE2FA(After Lesson 13)

    25/66

    25

    RERE NFA ConstructionNFA ConstructionRERE NFA ConstructionNFA Construction

    Is0 s4s1 a s2

    NFA fora*

    I

    I

    I

  • 8/9/2019 RE2FA(After Lesson 13)

    26/66

    26

    Example REExample RE NFANFAExample REExample RE NFANFA

    NFA fora ( b|c )* I

    s3

    s9

    s4 s5

    s6 s7

    s8s0 s1 s2a II I

    I

    I I

    Ib

    c

    I

  • 8/9/2019 RE2FA(After Lesson 13)

    27/66

    27

    Example REExample RE NFANFAExample REExample RE NFANFA

    building NFA fora ( b|c )*

    s0 s1a

  • 8/9/2019 RE2FA(After Lesson 13)

    28/66

    28

    Example REExample RE NFANFAExample REExample RE NFANFA

    NFA fora, b and c

    s4 s5

    s6 s7

    s0 s1a

    b

    c

  • 8/9/2019 RE2FA(After Lesson 13)

    29/66

    29

    Example REExample RE NFANFAExample REExample RE NFANFA

    NFA fora and b|c

    s3

    s4 s5

    s6 s7

    s8s0 s1a

    I

    I I

    Ib

    c

  • 8/9/2019 RE2FA(After Lesson 13)

    30/66

    30

    Example REExample RE NFANFAExample REExample RE NFANFA

    NFA fora and ( b|c )*I

    s3

    s9

    s4 s5

    s6 s7

    s8s0 s1 s2a II

    I

    I I

    Ib

    c

    I

  • 8/9/2019 RE2FA(After Lesson 13)

    31/66

    31

    Example REExample RE NFANFAExample REExample RE NFANFA

    NFA fora ( b|c )* I

    s3

    s9

    s4 s5

    s6 s7

    s8s0 s1 s2a II I

    I

    I I

    Ib

    c

    I

  • 8/9/2019 RE2FA(After Lesson 13)

    32/66

    32

    NFANFA DFA ConstructionDFA ConstructionNFANFA DFA ConstructionDFA Construction

    he algorithm is called subsetconstruction.

    In the transition table of anNFA, each entry is a set ofstates.

    In DFA, each entry is a singlestate

  • 8/9/2019 RE2FA(After Lesson 13)

    33/66

    33

    NFANFA DFA ConstructionDFA ConstructionNFANFA DFA ConstructionDFA Construction

    he general idea behind

    NFA-to-DFA construction is

    that each DFA state

    corresponds to a set of

    NFA states.

  • 8/9/2019 RE2FA(After Lesson 13)

    34/66

    34

    NFANFA DFA ConstructionDFA ConstructionNFANFA DFA ConstructionDFA Construction

    he DFA uses its state to

    keep track of all possible

    states the NFA can be in

    after reading each input

    symbol.

  • 8/9/2019 RE2FA(After Lesson 13)

    35/66

    35

    NFANFA DFA ConstructionDFA ConstructionNFANFA DFA ConstructionDFA Construction

    We will use the followingoperations.

    I-closure(T):set ofNFA states reachable

    from some NFA states

    in Ton I-transitions alone.

  • 8/9/2019 RE2FA(After Lesson 13)

    36/66

    36

    NFANFA DFA ConstructionDFA ConstructionNFANFA DFA ConstructionDFA Construction

    move(T,a):

    set ofNFA states to which

    there is a transition on inputa from some NFA state s in

    set of states T.

  • 8/9/2019 RE2FA(After Lesson 13)

    37/66

    37

    NFANFA DFA ConstructionDFA ConstructionNFANFA DFA ConstructionDFA Construction

    Before it sees the first input

    symbol, NFA can be in

    any of the state in the setI-closure(s

    0), where s

    0is the

    start state of the NFA.

  • 8/9/2019 RE2FA(After Lesson 13)

    38/66

    38

    NFANFA DFA ConstructionDFA ConstructionNFANFA DFA ConstructionDFA Construction

    Suppose that exactly the

    states in set Tare

    reachable from s0 on agiven sequence of input

    symbols.

  • 8/9/2019 RE2FA(After Lesson 13)

    39/66

    39

    NFANFA DFA ConstructionDFA ConstructionNFANFA DFA ConstructionDFA Construction

    Let a be the next input

    symbol.

    On seeing a, the NFA can

    move to any of the states in

    the set move(T,a).

  • 8/9/2019 RE2FA(After Lesson 13)

    40/66

    40

    NFANFA DFA ConstructionDFA ConstructionNFANFA DFA ConstructionDFA Construction

    Let a be the next input

    symbol.

    On seeing a, the NFA can

    move to any of the states in

    the set move(T,a).

  • 8/9/2019 RE2FA(After Lesson 13)

    41/66

    41

    NFANFA DFA ConstructionDFA ConstructionNFANFA DFA ConstructionDFA Construction

    When we allow for

    I-transitions, NFA can be in

    any of the states inI-closure(move(T,a))

    after seeing a.

  • 8/9/2019 RE2FA(After Lesson 13)

    42/66

    42

    Subset ConstructionSubset ConstructionSubset ConstructionSubset Construction

    Algorithm:

    Input:

    NFA N with state set S,

    alphabet 7, start state

    s0, final states F

  • 8/9/2019 RE2FA(After Lesson 13)

    43/66

    43

    Subset ConstructionSubset ConstructionSubset ConstructionSubset Construction

    Output:DFA D with state set S,

    alphabet 7, start statess0 = I-closure(s0), final

    states F, transitiontable: S x 7 S

  • 8/9/2019 RE2FA(After Lesson 13)

    44/66

    44

    Subset Construction ExampleSubset Construction ExampleSubset Construction ExampleSubset Construction Example

    NFA for(a | b )*abb

    0I

    I

    I I

    I

    a

    b

    I

    I

    1

    2 3

    4 5

    6 8 9

    I

    7

    a b b

    10

  • 8/9/2019 RE2FA(After Lesson 13)

    45/66

    45

    he start state of equivalentDFA is I-closure(0), which isA = {0,1,2,4,7}

    I

    0I

    I

    I I

    I

    a

    bI

    1

    2 3

    4 5

    6 8 9I

    7a b b

    10

    I

  • 8/9/2019 RE2FA(After Lesson 13)

    46/66

    46

    A = {0,1,2,4,7}, these areexactly the states reachablefrom state 0 via I-transition.

    I

    0I

    I

    I I

    I

    a

    bI

    1

    2 3

    4 5

    6 8 9I

    7a b b

    10

    I

  • 8/9/2019 RE2FA(After Lesson 13)

    47/66

    47

    he input symbol alphabethere is {a,b}.

    I

    0I

    I

    I I

    I

    a

    b

    I

    1

    2 3

    4 5

    6 8 9I

    7a b b

    10

    I

  • 8/9/2019 RE2FA(After Lesson 13)

    48/66

    48

    he algorithm tells us to markA and then compute

    I-closure(move(A,a))

    I

    0I

    I

    I I

    I

    a

    b

    I

    1

    2 3

    4 5

    6 8 9I

    7a b b

    10

    I

  • 8/9/2019 RE2FA(After Lesson 13)

    49/66

    49

    move(A,a)), is the set of statesofNFA that have transition ona from members ofA.

    I

    0I

    I

    I I

    I

    a

    bI

    1

    2 3

    4 5

    6 8 9I

    7a b b

    10

    I

  • 8/9/2019 RE2FA(After Lesson 13)

    50/66

    50

    Only 2 and 7 have suchtransition, to 3 and 8.

    I

    0I

    I

    I I

    I

    a

    b

    I

    1

    23

    4 5

    6 8 9I

    7a b b

    10

    I

  • 8/9/2019 RE2FA(After Lesson 13)

    51/66

    51

    So,

    I

    -closure(move(A

    ,a)) =I-closure({3,8}) ={1,2,3,4,6,7,8}

    I

    0I

    I

    I I

    I

    a

    b

    I

    1

    2 3

    4 5

    6 8 9I

    7a b b

    10

    I

  • 8/9/2019 RE2FA(After Lesson 13)

    52/66

    52

    Let B = {1,2,3,4,6,7,8}. hus Dtran[A,a] = B

    I

    0I

    I

    I I

    I

    a

    b

    I

    1

    2 3

    4 5

    6 8 9I

    7a b b

    10

    I

  • 8/9/2019 RE2FA(After Lesson 13)

    53/66

    53

    For input b, among states in A,only 4 has transition on b to 5

    I

    0I

    I

    I I

    I

    a

    b

    I

    1

    2 3

    4 5

    6 8 9I

    7a b b

    10

    I

  • 8/9/2019 RE2FA(After Lesson 13)

    54/66

    54

    C = I-closure({5})= {1,2,4,5,6,7}

    hus, Dtran[A,b] = C

    I

    0I

    I

    I I

    I

    a

    bI

    1

    2 3

    4 5

    6 8 9I

    7a b b

    10

    I

  • 8/9/2019 RE2FA(After Lesson 13)

    55/66

    55

    We continue this process withthe unmarked sets B and C

    I

    0I

    I

    I I

    I

    a

    bI

    1

    2 3

    4 5

    6 8 9I

    7a b b

    10

    I

  • 8/9/2019 RE2FA(After Lesson 13)

    56/66

    56

    i.e., I-closure(move(B,a)),I-closure(move(B,b)),

    I

    0I

    I

    I I

    I

    a

    bI

    1

    2 3

    4 5

    6 8 9I

    7a b b

    10

    I

  • 8/9/2019 RE2FA(After Lesson 13)

    57/66

    57

    I-closure(move(C,a)) andI-closure(move(C,b))

    0I

    I

    I I

    I

    a

    bI

    1

    2 3

    4 5

    6 8 9I

    7a b b

    10

    I

  • 8/9/2019 RE2FA(After Lesson 13)

    58/66

    58

    Until all sets and states ofDFAare marked.

    I

    0I

    I

    I I

    I

    a

    b

    I

    1

    2 3

    4 5

    6 8 9I

    7a b b

    10

    I

  • 8/9/2019 RE2FA(After Lesson 13)

    59/66

    59

    Eventually, the 5 sets are:A={0,1,2,4,7}

    B={1,2,3,4,6,7,8}C={1,2,4,5,6,7}

    D={1,2,4,5,6,7,9}E={1,2,4,5,6,7,10}

    Subset ConstructionSubset ConstructionSubset ConstructionSubset Construction

  • 8/9/2019 RE2FA(After Lesson 13)

    60/66

    60

    A is start state

    A={0,1,2,4,7} D={1,2,4,5,6,7,9}B={1,2,3,4,6,7,8} E={1,2,4,5,6,7,10}

    C={1,2,4,5,6,7}

    I

    0I

    I

    I I

    I

    a

    b

    I

    1

    2 3

    4 5

    6 8 9I

    7a b b

    10

    I

  • 8/9/2019 RE2FA(After Lesson 13)

    61/66

    61

    E is accepting state

    A={0,1,2,4,7} D={1,2,4,5,6,7,9}B={1,2,3,4,6,7,8} E={1,2,4,5,6,7,10}

    C={1,2,4,5,6,7}

    0I

    I

    I I

    I

    a

    b

    I

    1

    2 3

    4 5

    6 8 9I

    7a b b

    10

    I

  • 8/9/2019 RE2FA(After Lesson 13)

    62/66

    62

    Resulting DFAResulting DFAResulting DFAResulting DFA

    DFA for(a | b )*abb

    A

    a

    b a

    b bEB D

    C

    a

    b

    ba

    a

  • 8/9/2019 RE2FA(After Lesson 13)

    63/66

    63

    Resulting DFAResulting DFAResulting DFAResulting DFA

    A

    a

    b a

    b bEB D

    C

    a

    b

    ba

    a

    a a aa b b

  • 8/9/2019 RE2FA(After Lesson 13)

    64/66

    64

    Final Transition TableFinal Transition TableFinal Transition TableFinal Transition Table

    StateInput symbola b

    A B C

    B B D

    C B C

    D B E

    E B C

  • 8/9/2019 RE2FA(After Lesson 13)

    65/66

    65

    DFA MinimizationDFA MinimizationDFA MinimizationDFA Minimization

    DFA for(a | b )*abb

    A

    a

    b a

    b bEB D

    C

    a

    b

    ba

    a

  • 8/9/2019 RE2FA(After Lesson 13)

    66/66

    66

    DFA MinimizationDFA MinimizationDFA MinimizationDFA Minimization

    MinimizedDFA for(a | b )*abb

    A,C

    ab

    b bEB D

    a

    b

    a

    a