Languages and Finite Automata

Document Sample
Languages and Finite Automata Powered By Docstoc
					            Pushdown Automata
                  PDAs




Fall 2004          COMP 335     1
             Pushdown Automaton -- PDA
               Input String

                                    Stack


            States




Fall 2004                COMP 335           2
              Initial Stack Symbol


            Stack                   Stack


stack
             $                        z     top
head

                      bottom
                    special symbol
Fall 2004                COMP 335                 3
                     The States

            Input          Pop            Push
            symbol         symbol         symbol



                          a, b  c
                     q1              q2




Fall 2004                 COMP 335                 4
                               a, b  c
                          q1               q2


input
               a                                 a   

       stack
            b       top                         c
            h                  Replace          h
            e                                   e
            $                                   $
Fall 2004                       COMP 335                    5
                               a,   c
                          q1               q2


input
               a                                 a   

       stack                                    c
            b       top                         b
            h                   Push            h
            e                                   e
            $                                   $
Fall 2004                       COMP 335                    6
                               a, b  
                          q1               q2


input
               a                                 a   

       stack
            b       top
            h                    Pop            h
            e                                   e
            $                                   $
Fall 2004                       COMP 335                    7
                               a,   
                          q1                q2


input
               a                                  a   

       stack
            b       top                          b
            h                  No Change         h
            e                                    e
            $                                    $
Fall 2004                        COMP 335                    8
                     A Possible Transition

                               a, $  
                          q1               q2
input
               a                             a   

      stack                                 empty
            $       top         Pop




Fall 2004                       COMP 335                9
                   A Bad Transition

                          a, b  c
                     q1               q2
input
            a   

     Empty stack
                                       HALT

            The automaton Halts in state   q1
            and Rejects the input string
Fall 2004                  COMP 335             10
                   A Bad Transition

                          a,   c
                     q1               q2
input
            a   

     Empty stack
                                       HALT

            The automaton Halts in state   q1
            and Rejects the input string
Fall 2004                  COMP 335             11
        No transition is allowed to be followed
        when the stack is empty

                        x, y  z
                  q1                 q2


                       Empty stack




Fall 2004                 COMP 335                12
                      A Good Transition

                               a, $  b
                          q1               q2
input
               a                                 a   

      stack
            $       top         Pop             b



Fall 2004                       COMP 335                    13
                   Non-Determinism


                     q2
      a,b  c

   q1                                      , b  c
                                      q1                q2

    a,b  d                              transition
                     q3

            These are allowed transitions in a
            Non-deterministic PDA (NPDA)
Fall 2004                  COMP 335                          14
            NPDA: Non-Deterministic PDA


       Example:




                  a,   a          b, a  


     q0 ,    q b, a   q , $  $ q
                  1          2          3
Fall 2004                COMP 335              15
Execution Example: Time 0
    Input
        a a a b b b
                                           $
                                          Stack


current      a,   a          b, a  
state

    q0 ,    q b, a   q , $  $ q
                 1          2          3
Fall 2004           COMP 335                      16
                    Time 1
    Input
        a a a b b b
                                           $
                                          Stack


             a,   a          b, a  


     q0 ,    q b, a   q , $  $ q
                  1          2          3
Fall 2004           COMP 335                      17
                    Time 2
   Input
        a a a b b b                        a
                                           $
                                          Stack

             a,   a          b, a  


     q0 ,    q b, a   q , $  $ q
                  1          2          3
Fall 2004           COMP 335                      18
                    Time 3
   Input                                   a
        a a a b b b                        a
                                           $
                                          Stack


             a,   a          b, a  


     q0 ,    q b, a   q , $  $ q
                  1          2          3
Fall 2004           COMP 335                      19
                    Time 4
                                           a
   Input
                                           a
        a a a b b b                        a
                                           $
                                          Stack

             a,   a          b, a  


     q0 ,    q b, a   q , $  $ q
                  1          2          3
Fall 2004           COMP 335                      20
                    Time 5
                                           a
   Input
                                           a
        a a a b b b                        a
                                           $
                                          Stack

             a,   a          b, a  


     q0 ,    q b, a   q , $  $ q
                  1          2          3
Fall 2004           COMP 335                      21
                    Time 6
   Input                                    a
        a a a b b b                         a
                                            $
                                          Stack

             a,   a          b, a  


     q0 ,    q b, a   q , $  $ q
                  1          2          3
Fall 2004           COMP 335                      22
                    Time 7
   Input
        a a a b b b                        a
                                           $
                                          Stack


             a,   a          b, a  


     q0 ,    q b, a   q , $  $ q
                  1          2          3
Fall 2004           COMP 335                      23
                    Time 8
   Input
        a a a b b b
                                           $
                                          Stack


             a,   a          b, a  
                                            accept

     q0 ,    q b, a   q , $  $ q
                  1          2          3
Fall 2004           COMP 335                      24
    A string is accepted if there is
    a computation such that:


            All the input is consumed
                        AND
            The last state is a final state


    At the end of the computation,
    we do not care about the stack contents
Fall 2004              COMP 335               25
            The input string aaabbb
            is accepted by the NPDA:



                a,   a          b, a  


    q0 ,    q b, a   q , $  $ q
                 1          2          3
Fall 2004              COMP 335              26
 In general,

               L  {a b : n  0}
                     n n


  is the language accepted by the NPDA:



               a,   a          b, a  


    q0 ,    q b, a   q , $  $ q
                 1          2          3
Fall 2004             COMP 335              27
                 NPDA -- Another example

                        NPDA        M

                      L( M )  {ww }    R


    a,   a                 a, a  
   b,   b                  b, b  


            q0    ,           q1    , $  $   q2
Fall 2004                    COMP 335                   28
Execution Example:          Time 0
     Input
            a b b a
                                                    $
                                                  Stack
    a,   a                a, a  
   b,   b                 b, b  


            q0   ,           q1    , $  $     q2
Fall 2004                   COMP 335                      29
                            Time 1
     Input
            a b b a
                                                    a
                                                    $
                                                  Stack
    a,   a                a, a  
   b,   b                 b, b  


            q0   ,           q1    , $  $     q2
Fall 2004                   COMP 335                      30
                            Time 2
     Input
                                                    b
            a b b a
                                                    a
                                                    $
                                                  Stack
    a,   a                a, a  
   b,   b                 b, b  


            q0   ,           q1    , $  $     q2
Fall 2004                   COMP 335                      31
                            Time 3
     Input
                                                    b
            a b b a
                        Guess the middle            a
                        of string                   $
                                                  Stack
    a,   a                a, a  
   b,   b                 b, b  


            q0   ,           q1    , $  $     q2
Fall 2004                   COMP 335                      32
                            Time 4
     Input
                                                    b
            a b b a
                                                    a
                                                    $
                                                  Stack
    a,   a                a, a  
   b,   b                 b, b  


            q0   ,           q1    , $  $     q2
Fall 2004                   COMP 335                      33
                            Time 5
     Input
            a b b a
                                                    a
                                                    $
                                                  Stack
    a,   a                a, a  
   b,   b                 b, b  


            q0   ,           q1    , $  $     q2
Fall 2004                   COMP 335                      34
                            Time 6
     Input
            a b b a
                                                    $
                                                  Stack
    a,   a                a, a  
   b,   b                 b, b  
                                                  accept
            q0   ,           q1    , $  $     q2
Fall 2004                   COMP 335                      35
Rejection Example:          Time 0
     Input
            a b b b
                                                    $
                                                  Stack
    a,   a                a, a  
   b,   b                 b, b  


            q0   ,           q1    , $  $     q2
Fall 2004                   COMP 335                      36
                            Time 1
     Input
            a b b b
                                                    a
                                                    $
                                                  Stack
    a,   a                a, a  
   b,   b                 b, b  


            q0   ,           q1    , $  $     q2
Fall 2004                   COMP 335                      37
                            Time 2
     Input
                                                    b
            a b b b
                                                    a
                                                    $
                                                  Stack
    a,   a                a, a  
   b,   b                 b, b  


            q0   ,           q1    , $  $     q2
Fall 2004                   COMP 335                      38
                            Time 3
     Input
                                                    b
            a b b b
                        Guess the middle            a
                        of string                   $
                                                  Stack
    a,   a                a, a  
   b,   b                 b, b  


            q0   ,           q1    , $  $     q2
Fall 2004                   COMP 335                      39
                            Time 4
     Input
                                                    b
            a b b b
                                                    a
                                                    $
                                                  Stack
    a,   a                a, a  
   b,   b                 b, b  


            q0   ,           q1    , $  $     q2
Fall 2004                   COMP 335                      40
                           Time 5
     Input          There is no possible transition.

            a b b b         Input is not            a
                            consumed
                                                    $
                                                  Stack
    a,   a                a, a  
   b,   b                 b, b  


            q0   ,           q1    , $  $     q2
Fall 2004                   COMP 335                      41
Another computation on the same string:
     Input                  Time 0
            a b b b
                                                    $
                                                  Stack
    a,   a                a, a  
   b,   b                 b, b  


            q0   ,           q1    , $  $     q2
Fall 2004                   COMP 335                      42
                            Time 1
     Input
            a b b b
                                                    a
                                                    $
                                                  Stack
    a,   a                a, a  
   b,   b                 b, b  


            q0   ,           q1    , $  $     q2
Fall 2004                   COMP 335                      43
                            Time 2
     Input
                                                    b
            a b b b
                                                    a
                                                    $
                                                  Stack
    a,   a                a, a  
   b,   b                 b, b  


            q0   ,           q1    , $  $     q2
Fall 2004                   COMP 335                      44
                            Time 3
     Input                                          b
                                                    b
            a b b b
                                                    a
                                                    $
                                                  Stack
    a,   a                a, a  
   b,   b                 b, b  


            q0   ,           q1    , $  $     q2
Fall 2004                   COMP 335                      45
                            Time 4                  b
     Input                                          b
                                                    b
            a b b b
                                                    a
                                                    $
                                                  Stack
    a,   a                a, a  
   b,   b                 b, b  


            q0   ,           q1    , $  $     q2
Fall 2004                   COMP 335                      46
                             Time 5                  b
     Input                                           b
                            No final state           b
            a b b b         is reached               a
                                                     $
                                                   Stack
    a,   a                 a, a  
   b,   b                  b, b  


            q0   ,            q1    , $  $     q2
Fall 2004                    COMP 335                      47
                 There is no computation
                 that accepts string abbb

                      abbb L(M )


    a,   a                a, a  
   b,   b                 b, b  


            q0   ,           q1    , $  $   q2
Fall 2004                   COMP 335                   48
    A string is rejected if there is
    no computation such that:


            All the input is consumed
                        AND
            The last state is a final state


    At the end of the computation,
    we do not care about the stack contents
Fall 2004              COMP 335               49
    In other words, a string is rejected
    if in every computation with this string:

            The input cannot be consumed
                              OR
            The input is consumed and the last
            state is not a final state
                              OR
            The stack head moves below the
            bottom of the stack

Fall 2004                 COMP 335               50
            NPDA – Another Example
     L( M )  {a b : n  m  1}
               n    m

                   m 1 m                  m k
             {a      b : m  1}  {a             b : m, k  0}
                                                   m




                                                       b, a  
                            a,   a                   b, $  



                              q0        b, $            q1
                                        b, a  
Fall 2004                    COMP 335                             51
                 Pushing Strings

            Input           Pop            Push
            symbol          symbol         string



                          a, b  w
                     q1               q2




Fall 2004                  COMP 335                 52
Example:
                               a, b  cdf
                          q1                q2

input
                a                                    a


                                                 c       pushed
      stack                                      d
                    top                                  string
            b                                    f
            h                   Push             h
            e                                    e
            $                                    $
Fall 2004                        COMP 335                    53
                Another NPDA example
                             NPDA      M
                  L( M )  {w : na ( w)  nb ( w) }


            a, $  0$        b, $  1$
            a, 0  00        b, 1  11
            a, 1           b, 0  


                        q1
                                   , $  $           q2
Fall 2004                       COMP 335                   54
Execution Example:               Time 0
   Input
        a b      b a a b

                                                   $
            a, $  0$        b, $  1$
                                              Stack
            a, 0  00        b, 1  11
            a, 1           b, 0  
       current
       state                       , $  $
                        q1                    q2
Fall 2004                       COMP 335               55
                                 Time 1
   Input
        a b      b a a b
                                                   0
                                                   $
            a, $  0$        b, $  1$        Stack
            a, 0  00        b, 1  11
            a, 1           b, 0  


                        q1
                                   , $  $   q2
Fall 2004                       COMP 335               56
                                 Time 3
   Input
        a b      b b a a
                                                   0
                                                   $
            a, $  0$        b, $  1$        Stack
            a, 0  00        b, 1  11
            a, 1           b, 0  


                        q1
                                   , $  $   q2
Fall 2004                       COMP 335               57
                                 Time 4
   Input
        a b      b b a a
                                                   1
                                                   $
            a, $  0$        b, $  1$        Stack
            a, 0  00        b, 1  11
            a, 1           b, 0  


                        q1
                                   , $  $   q2
Fall 2004                       COMP 335               58
                                 Time 5
   Input
        a b      b b a a                           1
                                                   1
                                                   $
            a, $  0$        b, $  1$        Stack
            a, 0  00        b, 1  11
            a, 1           b, 0  


                        q1
                                   , $  $   q2
Fall 2004                       COMP 335               59
                                 Time 6
   Input
        a b      b b a a                           1
                                                   1
                                                   $
            a, $  0$        b, $  1$        Stack
            a, 0  00        b, 1  11
            a, 1           b, 0  


                        q1
                                   , $  $   q2
Fall 2004                       COMP 335               60
                                 Time 7
   Input
        a b      b b a a
                                                   1
                                                   $
            a, $  0$        b, $  1$        Stack
            a, 0  00        b, 1  11
            a, 1           b, 0  


                        q1
                                   , $  $   q2
Fall 2004                       COMP 335               61
                                 Time 8
   Input
        a b      b b a a

                                                    $
            a, $  0$        b, $  1$
                                               Stack
            a, 0  00        b, 1  11
            a, 1           b, 0  
                                              accept
                        q1
                                   , $  $    q2
Fall 2004                       COMP 335                62
            Formalities for NPDAs




Fall 2004           COMP 335        63
                    a, b  w
               q1                 q2

Transition function:


             (q1, a, b)  {(q2 , w)}

Fall 2004              COMP 335         64
                  a, b  w           q2

                 q1

                  a, b  u
                                     q3

Transition function:

             (q1 , a, b)  {( q2 , w), (q3 , u )}

Fall 2004                 COMP 335                   65
                 Formal Definition
     Non-Deterministic Pushdown Automaton
                    NPDA
              M  (Q, Σ, Γ, δ, q0 , z, F )   Final
     States                                  states

    Input                                    Stack
    alphabet        Transition Initial       end
           Stack
                    function   state         symbol
           alphabet
Fall 2004                COMP 335                66
            Instantaneous Description (ID)


                      (q, v, u)

            Current                   Current
                       Remaining
            state                     stack
                       unread input
                                      contents



Fall 2004                COMP 335                67
Example:           Instantaneous Description (ID)
                         (q1 , bbb, aaa$)
                                                                   a
Time 4:                Input                                       a
                        a a a b b b                                a
                                                                   $
                                                                 Stack
                       a, / a              b,a / 


     q0
             , / 
                         q1       b,a /     q2        ,$ / $     q3
Fall 2004                        COMP 335                              68
Example:     Instantaneous Description (ID)
                      (q2 , bb, aa$)
                                               a
Time 5:       Input                            a
               a a a b b b                     a
                                               $
                                             Stack
             a,   a             b, a  


     q0 ,    q b, a   q , $  $ q
                  1          2          3
Fall 2004              COMP 335                    69
  We write:



            (q1 , bbb, aaa$)  (q2 , bb, aa$)
                  Time 4              Time 5




Fall 2004                  COMP 335             70
A computation:

(q0 , aaabbb,$)  (q1, aaabbb,$) 
(q1, aabbb, a$)  (q1, abbb, aa$)  (q1, bbb, aaa$) 
(q2 , bb, aa$)  (q2 , b, a$)  (q2 ,  ,$)  (q3 ,  ,$)


                 a,   a             b, a  


      q0 ,    q b, a   q , $  $ q
                   1          2          3

 Fall 2004                 COMP 335                         71
(q0 , aaabbb,$)  (q1, aaabbb,$) 
(q1, aabbb, a$)  (q1, abbb, aa$)  (q1, bbb, aaa$) 
(q2 , bb, aa$)  (q2 , b, a$)  (q2 ,  ,$)  (q3 ,  ,$)


       For convenience we write:

                                
             (q0 , aaabbb ,$)  (q3 ,  ,$)

 Fall 2004                 COMP 335                         72
                    Formal Definition

   Language        L(M )       of NPDA M :


                                           *
     L( M )  {w : w   , (q0 , w, z )  M (q f ,  , u ), u   }
                           *                                    *




            Initial ID                               Final ID



Fall 2004                       COMP 335                              73
Example:                          
             (q0 , aaabbb,$)  (q3 ,  ,$)


                     aaabbb L(M )

 NPDA M :

             a,   a           b, a  


     q0 ,    q b, a   q , $  $ q
                  1          2          3

Fall 2004            COMP 335                74
                                
              (q0 , a b ,$)  (q3 ,  ,$)
                    n n



                        a b  L(M )
                         n n


NPDA M :

             a,   a           b, a  


     q0 ,    q b, a   q , $  $ q
                  1          2          3

Fall 2004            COMP 335               75
      Therefore:   L( M )  {a b : n  0}
                                  n n




NPDA M :

               a,   a          b, a  


     q0 ,    q b, a   q , $  $ q
                  1          2          3

Fall 2004             COMP 335              76

				
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
views:5
posted:6/15/2012
language:
pages:76