Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Pushdown Stack Automata Zeph Grunschlag. 2 Agenda Pushdown Automata Stacks and recursiveness Formal Definition.

Similar presentations


Presentation on theme: "1 Pushdown Stack Automata Zeph Grunschlag. 2 Agenda Pushdown Automata Stacks and recursiveness Formal Definition."— Presentation transcript:

1 1 Pushdown Stack Automata Zeph Grunschlag

2 2 Agenda Pushdown Automata Stacks and recursiveness Formal Definition

3 3 Recursive Algorithms and Stacks General Principle in Computer Science: Any recursive algorithm can be turned into a non- recursive one using a stack and a while-loop which exits only when stack is empty. EG: JVM keeps stack of activation records generated at each method call. Consider: long factorial(int n){ if (n<=0) return 1; return n*factorial(n-1); } What does the JVM do for factorial(5) ?

4 4 Recursive Algorithms and Stacks long factorial(int n){ if (n<=0) return 1; return n*factorial(n-1); } Compute 5!

5 5 Recursive Algorithms and Stacks long factorial(int n){ if (n<=0) return 1; return n*factorial(n-1); } f(5)= 5·f(4)

6 6 Recursive Algorithms and Stacks long factorial(int n){ if (n<=0) return 1; return n*factorial(n-1); } f(4)= 4·f(3) f(5)= 5·f(4)

7 7 Recursive Algorithms and Stacks long factorial(int n){ if (n<=0) return 1; return n*factorial(n-1); } f(3)= 3·f(2) f(4)= 4·f(3) f(5)= 5·f(4)

8 8 Recursive Algorithms and Stacks long factorial(int n){ if (n<=0) return 1; return n*factorial(n-1); } f(2)= 2·f(1) f(3)= 3·f(2) f(4)= 4·f(3) f(5)= 5·f(4)

9 9 Recursive Algorithms and Stacks long factorial(int n){ if (n<=0) return 1; return n*factorial(n-1); } f(1)= 1·f(0) f(2)= 2·f(1) f(3)= 3·f(2) f(4)= 4·f(3) f(5)= 5·f(4)

10 10 Recursive Algorithms and Stacks long factorial(int n){ if (n<=0) return 1; return n*factorial(n-1); } f(0)= 1  f(1)= 1·f(0) f(2)= 2·f(1) f(3)= 3·f(2) f(4)= 4·f(3) f(5)= 5·f(4)

11 11 Recursive Algorithms and Stacks long factorial(int n){ if (n<=0) return 1; return n*factorial(n-1); } 1·1= 1  f(2)= 2·f(1) f(3)= 3·f(2) f(4)= 4·f(3) f(5)= 5·f(4)

12 12 Recursive Algorithms and Stacks long factorial(int n){ if (n<=0) return 1; return n*factorial(n-1); } 2·1= 2  f(3)= 3·f(2) f(4)= 4·f(3) f(5)= 5·f(4)

13 13 Recursive Algorithms and Stacks long factorial(int n){ if (n<=0) return 1; return n*factorial(n-1); } 3·2= 6  f(4)= 4·f(3) f(5)= 5·f(4)

14 14 Recursive Algorithms and Stacks long factorial(int n){ if (n<=0) return 1; return n*factorial(n-1); } 4·6= 24  f(5)= 5·f(4)

15 15 Recursive Algorithms and Stacks long factorial(int n){ if (n<=0) return 1; return n*factorial(n-1); } 5·24= 120 

16 16 Recursive Algorithms and Stacks long factorial(int n){ if (n<=0) return 1; return n*factorial(n-1); } Return 5! = 120

17 17 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

18 18 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?

19 19 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?

20 20 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.

21 21 From CFG’s to Stack Machines (1) PUSH (3) POP read a or b ? Push it ACCEPT (2) read # ? (ignore stack) read == peek ? Pop Else: CRASH! empty stack?

22 22 From CFG’s to Stack Machines (1) PUSH (3) POP read a or b ? Push it ACCEPT (2) read # ? (ignore stack) read == peek ? Pop Else: CRASH! empty stack? Input: aaab#baa

23 23 From CFG’s to Stack Machines (1) PUSH (3) POP read a or b ? Push it ACCEPT (2) read # ? (ignore stack) read == peek ? Pop Else: CRASH! empty stack? a Input: aaab#baa

24 24 From CFG’s to Stack Machines (1) PUSH (3) POP read a or b ? Push it ACCEPT (2) read # ? (ignore stack) read == peek ? Pop Else: CRASH! empty stack? aa Input: aaab#baa

25 25 From CFG’s to Stack Machines (1) PUSH (3) POP read a or b ? Push it ACCEPT (2) read # ? (ignore stack) read == peek ? Pop Else: CRASH! empty stack? aaa Input: aaab#baa

26 26 From CFG’s to Stack Machines (1) PUSH (3) POP read a or b ? Push it ACCEPT (2) read # ? (ignore stack) read == peek ? Pop Else: CRASH! empty stack? baaa Input: aaab#baa

27 27 From CFG’s to Stack Machines (1) PUSH (3) POP read a or b ? Push it ACCEPT (2) read # ? (ignore stack) read == peek ? Pop Else: CRASH! empty stack? baaa Input: aaab#baa

28 28 From CFG’s to Stack Machines (1) PUSH (3) POP read a or b ? Push it ACCEPT (2) read # ? (ignore stack) read == peek ? Pop Else: CRASH! empty stack? aaa Input: aaab#baa

29 29 From CFG’s to Stack Machines (1) PUSH (3) POP read a or b ? Push it ACCEPT (2) read # ? (ignore stack) read == peek ? Pop Else: CRASH! empty stack? aa Input: aaab#baa

30 30 From CFG’s to Stack Machines (1) PUSH (3) POP read a or b ? Push it ACCEPT (2) read # ? (ignore stack) read == peek ? Pop Else: CRASH! empty stack? a Input: aaab#baa REJECT (nonempty stack)

31 31 From CFG’s to Stack Machines (1) PUSH (3) POP read a or b ? Push it ACCEPT (2) read # ? (ignore stack) read == peek ? Pop Else: CRASH! empty stack? aa Input: aaab#baaa

32 32 From CFG’s to Stack Machines (1) PUSH (3) POP read a or b ? Push it ACCEPT (2) read # ? (ignore stack) read == peek ? Pop Else: CRASH! empty stack? a Input: aaab#baaa

33 33 From CFG’s to Stack Machines (1) PUSH (3) POP read a or b ? Push it ACCEPT (2) read # ? (ignore stack) read == peek ? Pop Else: CRASH! empty stack? Input: aaab#baaa Pause input

34 34 From CFG’s to Stack Machines (1) PUSH (3) POP read a or b ? Push it ACCEPT (2) read # ? (ignore stack) read == peek ? Pop Else: CRASH! empty stack? Input: aaab#baaa ACCEPT

35 35 From CFG’s to Stack Machines (1) PUSH (3) POP read a or b ? Push it ACCEPT (2) read # ? (ignore stack) read == peek ? Pop Else: CRASH! empty stack? aa Input: aaab#baaaa

36 36 From CFG’s to Stack Machines (1) PUSH (3) POP read a or b ? Push it ACCEPT (2) read # ? (ignore stack) read == peek ? Pop Else: CRASH! empty stack? a Input: aaab#baaaa

37 37 From CFG’s to Stack Machines (1) PUSH (3) POP read a or b ? Push it ACCEPT (2) read # ? (ignore stack) read == peek ? Pop Else: CRASH! empty stack? Input: aaab#baaaa Pause input

38 38 From CFG’s to Stack Machines (1) PUSH (3) POP read a or b ? Push it ACCEPT (2) read # ? (ignore stack) read == peek ? Pop Else: CRASH! empty stack? Input: aaab#baaaa CRASH

39 39 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.

40 40 Sipser’s Version Becomes: (1) PUSH (3) POP read a or b ? Push it ACCEPT (2) read # ? (ignore stack) read == peek ? Pop Else: CRASH! empty stack?   $ a   a b   b #    $   a  a   b  b  

41 41 Sipser’s Version 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 =  : ignore input, don’t read y =  : ignore top of stack and push z z =  : pop y p q x, y  z

42 42 Sipser’s Version   $ a   a b   b #    $   a  a   b  b   push $ to detect empty stack

43 43 Sipser’s Version   $ a   a b   b #    $   a  a   b  b   Input: aaab#baaa

44 44 Sipser’s Version   $ a   a b   b #    $   a  a   b  b   $ Input: aaab#baaa

45 45 Sipser’s Version   $ a   a b   b #    $   a  a   b  b   a$ Input: aaab#baaa

46 46 Sipser’s Version   $ a   a b   b #    $   a  a   b  b   aa$ Input: aaab#baaa

47 47 Sipser’s Version   $ a   a b   b #    $   a  a   b  b   aaa$ Input: aaab#baaa

48 48 Sipser’s Version   $ a   a b   b #    $   a  a   b  b   baaa$ Input: aaab#baaa

49 49 Sipser’s Version   $ a   a b   b #    $   a  a   b  b   baaa$ Input: aaab#baaa

50 50 Sipser’s Version   $ a   a b   b #    $   a  a   b  b   aaa$ Input: aaab#baaa

51 51 Sipser’s Version   $ a   a b   b #    $   a  a   b  b   aa$ Input: aaab#baaa

52 52 Sipser’s Version   $ a   a b   b #    $   a  a   b  b   a$ Input: aaab#baaa

53 53 Sipser’s Version   $ a   a b   b #    $   a  a   b  b   $ Input: aaab#baaa

54 54 Sipser’s Version   $ a   a b   b #    $   a  a   b  b   Input: aaab#baaa ACCEPT!

55 55 PDA Formal Definition DEF: A pushdown automaton (PDA) is a 6-tuple M = (Q, , , , q 0, F ). Q, , and q 0, are the same as for an FA.  is the tape alphabet.  is as follows: So given a state p, an input letter x and a tape letter y,  p,x,y  gives all (q,z) where q is a target state and z a stack replacement for y.

56 56 PDA Formal Definition Q: What is  p,x,y  in each case?   0,a,b    0, ,    1,a,    3, ,  01 2   $ 3 a   a b   b b   $  $   a  a   b  b   a   

57 57 PDA Formal Definition A:   0,a,b    0, ,  = {(1,$)}   1,a,  = {(0,  ),(1,a)}   3, ,  =  01 2   $ 3 a   a b   b b   $  $   a  a   b  b   a   

58 58 PDA Exercise (Sipser 2.6.a) Draw the PDA acceptor for L = { x  {a,b}* | n a (x) = 2n b (x) } NOTE: The empty string is in L.

59 59 PDA Exercise The idea is to use the stack to keep count of the number of a’s and/or b’s needed to get a valid string. If we have a surplus of b’s thus far, we should have corresponding number of a’s (two for every b) on the stack. On the other hand, if we have a surplus of a’s we cannot put b’s on the stack since we can’t split symbols. So instead, put two “negative” a-symbols, where a negative a will be denoted by capital A. Let’s see how this looks:

60 60 PDA Ex.   $ a  $  $ a  A  A  $   push $ so can detect empty stack accept if stack empty a- surplus push A   A a  a   b-surplus a’s cancel b  $  $ b  a  a b-surplus add 2 a’s per b   a a- surplus read b: erase 2 A’s if can b  A    A    $  $

61 61 PDA Example. No-Bubbles   $ a  $  $ a  A  A  $     A a  a   b  $  $ b  a  a   a b  A    A    $  $


Download ppt "1 Pushdown Stack Automata Zeph Grunschlag. 2 Agenda Pushdown Automata Stacks and recursiveness Formal Definition."

Similar presentations


Ads by Google