L11 by yaofenji

VIEWS: 3 PAGES: 45

									Pushdown Stack Automata
Zeph Grunschlag




                          1
Agenda
 Pushdown Automata
    Stacks and recursiveness
    Formal Definition




                                2
From CFG’s to Stack Machines
CFG’s naturally define recursive procedure:
boolean derives(strings x, y)
1. if (x==y) return true
2. for(all uy)
     if derives(x,u) return true
3. return false //no successful branch
EG:       S  # | aSa | bSb

                                         17
From CFG’s to Stack Machines
By general principles, can carry out any
  recursive computation on a stack. Can
  do it on a restricted version of an
  activation record stack, called a
  “Pushdown (Stack) Automaton” or PDA
  for short.
Q: What is the language generated by
 S  # | aSa | bSb ?

                                      18
From CFG’s to Stack Machines
A: Palindromes in {a,b,#}* containing
  exactly one #-symbol.
Q: Using a stack, how can we recognize
  such strings?




                                     19
From CFG’s to Stack Machines
A: Use a three phase process:
1. Push mode: Before reading “#”, push
   everything on the stack.
2. Reading “#” switches modes.
3. Pop mode: Read remaining symbols
   making sure that each new read symbol is
   identical to symbol popped from stack.
Accept if able to empty stack completely.
   Otherwise reject, and reject if could not pop
   somewhere.
                                              20
   From CFG’s to Stack Machines
                          read == peek ?
read a or b ?                   Pop
   Push it                 Else: CRASH!

                      (2)                  empty
       (1)         read # ?       (3)      stack?
                (ignore stack)                      ACCEPT
      PUSH                        POP




                                                      21
   From CFG’s to Stack Machines
                          read == peek ?
read a or b ?                   Pop
   Push it                 Else: CRASH!

                      (2)                  empty
       (1)         read # ?       (3)      stack?
                (ignore stack)                      ACCEPT
      PUSH                        POP


    Input:
    aaab#baa
                                                      22
   From CFG’s to Stack Machines
                          read == peek ?
read a or b ?                   Pop
   Push it                 Else: CRASH!

                      (2)                  empty
       (1)         read # ?       (3)      stack?
                (ignore stack)                      ACCEPT
      PUSH                        POP


    Input:
                                                a
    aaab#baa
                                                      23
   From CFG’s to Stack Machines
                          read == peek ?
read a or b ?                   Pop
   Push it                 Else: CRASH!

                      (2)                  empty
       (1)         read # ?       (3)      stack?
                (ignore stack)                      ACCEPT
      PUSH                        POP


    Input:
                                            a a
    aaab#baa
                                                      24
   From CFG’s to Stack Machines
                          read == peek ?
read a or b ?                   Pop
   Push it                 Else: CRASH!

                      (2)                   empty
       (1)         read # ?       (3)       stack?
                (ignore stack)                       ACCEPT
      PUSH                        POP


    Input:
                                           a a a
    aaab#baa
                                                       25
   From CFG’s to Stack Machines
                          read == peek ?
read a or b ?                   Pop
   Push it                 Else: CRASH!

                      (2)                  empty
       (1)         read # ?       (3)      stack?
                (ignore stack)                      ACCEPT
      PUSH                        POP


    Input:
                                     b a a a
    aaab#baa
                                                      26
   From CFG’s to Stack Machines
                          read == peek ?
read a or b ?                   Pop
   Push it                 Else: CRASH!

                      (2)                  empty
       (1)         read # ?       (3)      stack?
                (ignore stack)                      ACCEPT
      PUSH                        POP


    Input:
                                     b a a a
    aaab#baa
                                                      27
   From CFG’s to Stack Machines
                          read == peek ?
read a or b ?                   Pop
   Push it                 Else: CRASH!

                      (2)                   empty
       (1)         read # ?       (3)       stack?
                (ignore stack)                       ACCEPT
      PUSH                        POP


    Input:
                                           a a a
    aaab#baa
                                                       28
   From CFG’s to Stack Machines
                          read == peek ?
read a or b ?                   Pop
   Push it                 Else: CRASH!

                      (2)                  empty
       (1)         read # ?       (3)      stack?
                (ignore stack)                      ACCEPT
      PUSH                        POP


    Input:
                                            a a
    aaab#baa
                                                      29
   From CFG’s to Stack Machines
                          read == peek ?
read a or b ?                   Pop
   Push it                 Else: CRASH!

                      (2)                  empty
       (1)         read # ?       (3)      stack?
                (ignore stack)                      ACCEPT
      PUSH                        POP


    Input:
                                               a
    aaab#baa
                         REJECT (nonempty stack)      30
   From CFG’s to Stack Machines
                          read == peek ?
read a or b ?                   Pop
   Push it                 Else: CRASH!

                      (2)                  empty
       (1)         read # ?       (3)      stack?
                (ignore stack)                      ACCEPT
      PUSH                        POP


    Input:
                                            a a
    aaab#baaa
                                                      31
   From CFG’s to Stack Machines
                          read == peek ?
read a or b ?                   Pop
   Push it                 Else: CRASH!

                      (2)                  empty
       (1)         read # ?       (3)      stack?
                (ignore stack)                      ACCEPT
      PUSH                        POP


    Input:
                                               a
    aaab#baaa
                                                      32
   From CFG’s to Stack Machines
                          read == peek ?
read a or b ?                   Pop
   Push it                 Else: CRASH!

                      (2)                  empty
       (1)         read # ?       (3)      stack?
                (ignore stack)                      ACCEPT
      PUSH                        POP


    Input:
    aaab#baaa
                         Pause input                  33
   From CFG’s to Stack Machines
                          read == peek ?
read a or b ?                   Pop
   Push it                 Else: CRASH!

                      (2)                  empty
       (1)         read # ?       (3)      stack?
                (ignore stack)                      ACCEPT
      PUSH                        POP


    Input:
    aaab#baaa
                         ACCEPT                       34
   From CFG’s to Stack Machines
                          read == peek ?
read a or b ?                   Pop
   Push it                 Else: CRASH!

                      (2)                  empty
       (1)         read # ?       (3)      stack?
                (ignore stack)                      ACCEPT
      PUSH                        POP


    Input:
                                            a a
    aaab#baaaa
                                                      35
   From CFG’s to Stack Machines
                          read == peek ?
read a or b ?                   Pop
   Push it                 Else: CRASH!

                      (2)                  empty
       (1)         read # ?       (3)      stack?
                (ignore stack)                      ACCEPT
      PUSH                        POP


    Input:
                                               a
    aaab#baaaa
                                                      36
   From CFG’s to Stack Machines
                          read == peek ?
read a or b ?                   Pop
   Push it                 Else: CRASH!

                      (2)                  empty
       (1)         read # ?       (3)      stack?
                (ignore stack)                      ACCEPT
      PUSH                        POP


    Input:
    aaab#baaaa
                         Pause input                  37
   From CFG’s to Stack Machines
                          read == peek ?
read a or b ?                   Pop
   Push it                 Else: CRASH!

                      (2)                  empty
       (1)         read # ?       (3)      stack?
                (ignore stack)                      ACCEPT
      PUSH                        POP


    Input:
    aaab#baaaa
                         CRASH                        38
         PDA’s à la Sipser
To aid analysis, theoretical stack machines
  restrict the allowable operations. Each text-
  book author has his own version. Sipser’s
  machines are especially simple:
   Push/Pop rolled into a single operation:
  replace top stack symbol
   No intrinsic way to test for empty stack
   Epsilon’s used to increase functionality,
  result in default nondeterministic machines.

                                                 39
        Sipser’s Version
                                      read == peek ?
     read a or b ?                          Pop
        Push it                        Else: CRASH!
                           (2)
                        read # ?                 empty
            (1)      (ignore stack)    (3)       stack?
                                                          ACCEPT
           PUSH                        POP


Becomes:        a , ea               a , ae
                b , eb               b , be

    e , e$            #, ee              e , $e



                                                           40
         Sipser’s Version
              x, y z
          p             q

Meaning of labeling convention:
If at p and next input x and top stack y,
then go to q and replace y by z on stack.
   x = e: ignore input, don’t read
   y = e: ignore top of stack and push z
   z = e: pop y
                                       41
         Sipser’s Version
               a , ea      a , ae
               b , eb      b , be

     e , e$       #, ee       e , $e



push $ to
 detect
 empty
  stack

                                          42
       Sipser’s Version
             a , ea      a , ae
             b , eb      b , be

   e , e$       #, ee       e , $e

Input:
aaab#baaa



                                        43
       Sipser’s Version
             a , ea      a , ae
             b , eb      b , be

   e , e$       #, ee       e , $e

Input:
                                    $
aaab#baaa



                                        44
       Sipser’s Version
             a , ea      a , ae
             b , eb      b , be

   e , e$       #, ee       e , $e

Input:
                              a $
aaab#baaa



                                        45
       Sipser’s Version
             a , ea      a , ae
             b , eb      b , be

   e , e$       #, ee       e , $e

Input:
                           a a $
aaab#baaa



                                        46
       Sipser’s Version
             a , ea      a , ae
             b , eb      b , be

   e , e$       #, ee       e , $e

Input:
                       a a a $
aaab#baaa



                                        47
       Sipser’s Version
             a , ea      a , ae
             b , eb      b , be

   e , e$       #, ee       e , $e

Input:
                   b a a a $
aaab#baaa



                                        48
       Sipser’s Version
             a , ea      a , ae
             b , eb      b , be

   e , e$       #, ee       e , $e

Input:
                   b a a a $
aaab#baaa



                                        49
       Sipser’s Version
             a , ea      a , ae
             b , eb      b , be

   e , e$       #, ee       e , $e

Input:
                       a a a $
aaab#baaa



                                        50
       Sipser’s Version
             a , ea      a , ae
             b , eb      b , be

   e , e$       #, ee       e , $e

Input:
                           a a $
aaab#baaa



                                        51
       Sipser’s Version
             a , ea      a , ae
             b , eb      b , be

   e , e$       #, ee       e , $e

Input:
                              a $
aaab#baaa



                                        52
       Sipser’s Version
             a , ea      a , ae
             b , eb      b , be

   e , e$       #, ee       e , $e

Input:
                                    $
aaab#baaa



                                        53
       Sipser’s Version
             a , ea      a , ae
             b , eb      b , be

   e , e$       #, ee       e , $e

Input:
aaab#baaa
              ACCEPT!


                                        54
              PDA
         Formal Definition
DEF: A pushdown automaton (PDA) is
 a 6-tuple M = (Q, S, G, d, q0, F ). Q, S, and
  q0, are the same as for an FA. G is the tape
  alphabet. d is as follows:
       δ : Q  Σ ε  Gε  P (Q  Gε )
So given a state p, an input letter x and a tape
  letter y, d(p,x,y) gives all (q,z) where q is a
  target state and z a stack replacement for y.

                                                55
                   PDA
              Formal Definition

                      a , ea    a , ae
                      b , eb    b , be
           a , e e
     0                 1          2             3
          e , e$       b, e$        e , $e
Q:       What is d(p,x,y) in each case?
1.       d(0,a,b)
2.       d(0,e,e)
3.       d(1,a,e)
4.       d(3,e,e)                                   56
                   PDA
              Formal Definition

                      a , ea       a , ae
                      b , eb       b , be
           a , e e
     0                 1             2             3
          e , e$          b, e$        e , $e
A:
1.       d(0,a,b) = 
2.       d(0,e,e) = {(1,$)}
3.       d(1,a,e) = {(0,e),(1,a)}
4.       d(3,e,e) =                                   57
          PDA Exercise
(Sipser 2.6.a) Draw the PDA acceptor for
L = { x  {a,b}* | na(x) = 2nb(x) }
NOTE: The empty string is in L.




                                       58
          PDA Example.
           No-Bubbles
          e, eA    a, $ $
a , ae             a, AA

                   e, Ae                e, $$
                               b, Ae
e , e$                        e , $e
                      e, ea
     b, $ $
     b, a a
                     e, ea

                                           61

								
To top