CS 422 – Eschen Homework 7 Solutions 1) I’m using Z instead of Z0, so that I don’t have type the subscript. (q, 0011, Z) 1 4 (p, 011, Z) (q, 011, XZ) dead 2 4 5 (p, 11, XZ) (q, 11, XXZ) 3 6 (q, 1, XXZ) 5 6 (q, , XXZ) (p, , XXZ) 5 5 (p, , XXZ) (p, ,X Z) 5 (p, , XZ) 5 5 (p, , XZ) (p, , Z) (p, , Z) 5 3 (p, , Z) 5 (p, 1, XZ) 6 (p, , XXZ) 5 (p, , XZ) 5 (p, , Z) (p, 1, Z) 7 dead 7 (p, 1, Z) 6 (p, , XXXZ) 4 (p, 1, ) 5 (p, 1, XZ) (p, 1, XXZ) 3 7 (p, 11, Z) (p, , ) (p, , ) 2) Idea: 1 on the stack represents that one 1 has been read. 0 on the stack represents that two 0s have been read. 1s on the stack wait to be matched with two 0s. 0s on the stack (that represent two 0s on the input) wait to be matched with one 1. PDA for L = { w in {0, 1}* | w has twice as many 0s as 1s } q3 , Z0/Z0 q1 q0 1, 0/ 1, Z0/1Z0 1, 1/11 0, 1/1 0, Z0/Z0 0, 0/0 0, 1/ 0, Z0/0Z0 0, 0/00 1, 0/ 1, Z0/1Z0 1, 1/11 1. q0: If 1 is read, try to match with a 0 on the stack and pop 0; otherwise push 1. If 0 is read, move to q1 without changing the stack. 2. q1: Remembers that one 0 has been read. If 1 is read, try to match with a 0 on the stack and pop 0; otherwise push 1. If 0 is read move to q0, and try to match with a 1 on the stack (this is the second 0 of a pair) and pop 1; otherwise, push 0 (represents two 0s). 3. q3: Transition to an accept state when there are no 0s or 1s on the stack. If the input is also exhausted, the input is accepted. 3) b) more powerful The language L = { 0n 1n 2n | n ≥ 1 } is not context-free, but there is a 2-stack PDA that accepts L. Another example is A = { ww | w is in {0, 1}* }. A is not context-free, but there is a 2-stack PDA that accepts A. For a 2-stack PDA I will label transitions as [input symbol or , top of S1/α, top of S2/β] where S1 and S2 refer to stack 1 and stack 2, respectively, and α and β are the symbols pushed on stack 1 and stack 2, respectively. If one of the stack parts is missing, then the transition is independent of that stack’s top symbol and that stack is unchanged. 2-stack PDA for L = {0n1n2n | n 1} dead (#1s > # 0s) [1, Z0/Z0, --] (#0s > # 1s) [2, 0/0, --] q0 [1, 0/, Z0/1Z0] q1 (#1s = # 0s) q2 [, --, Z0/Z0] q3 [2, Z0/Z0, 1/] [0, Z0/0Z0, --] [1, 0/, 1/11] (pop s1) [2, --, 1/] (pop s2) [0, 0/00, --] 1. 2. 3. 4. 5. q0: Push 0s onto S1 until the first 1 is read. q1: For each 1 read, push a 1 onto S2 and pop 0 from S1. If there are an unequal # of 1s and 0s, move to a dead state. q2: Otherwise, for each 2 read, pop a 1 from S2. q3: Transition into an accept state when there are no more 1s on S2. If the input is also exhausted, the input is accepted. If # 1s < # 2s, enter q3, but the input is not exhausted. If # 1s > # 2s, stops in q2. 2-stack PDA for A = { ww | w is in {0, 1}* }} q0 [, 0/0, --] [, 1/1, --] [, Z0/Z0, --] [0, Z0/0Z0, --] [1, Z0/1Z0, --] [0, 0/00, --] [1, 1/11, --] [0, 1/01, --] [1, 0/10, --] q1 [, 0/, Z0/0Z0] [, 1/, Z0/1Z0] [, 0/, 0/00] [, 0/, 1/01] [, 1/, 0/10] [, 1/, 1/11] [, Z0/Z0, --] q2 [, --, Z0/Z0] q3 [0, --, 0/] [1, --, 1/] 1. q0: Push input symbols onto S1, and nondeterministically make a guess that the last symbol of w has been read. 2. q1: For each symbol on S1, pop S1 and push the symbol onto S2 without consuming any input. 3. When all the symbols except Z0 have been removed from S1, transition to the matching state q2. 4. q2: If the top of S2 is the same as the input symbol, pop S2. 5. q3: When all the symbols except Z0 have been removed from S2, transition to an accepting state. If the input is also exhausted, the input is accepted. 4) There a bit of ambiguity in the statement of this problem. It isn’t clear whether in a single transition the PDA may read/write to each of the stacks and transfer symbols between the two stacks, OR in a single transition the PDA may either read/write to each of the stacks or transfer symbols between the two stacks. In the former case, there is also the question of the order of the operations. I will make the assumption that the latter case holds. This is not a limiting assumption because in this case a sequence of transitions can simulate one of the combined transitions, whatever the order of operations. A transfer transition that transfers a single symbol can be formalized as [input symbol or , top of S1, top of S2, S1 S2 or S2 S1], for e.g. [a, X, Y, S1 S2]. A sequence of such transitions can achieve a stack transfer transition that transfers several symbols. a) as powerful First, a 2-stack* PDA can simulate a 2-stack PDA by never making a stack transfer transition. On the other hand, a 2-stack PDA can simulate a stack transfer transition of a 2-stack* PDA that transfers a single symbol using transitions of the form [a, X/, Y/XY] (transfer from S1 to S2) and [a, X/YX, Y/] (transfer from S2 to S1), where a is an input symbol or . The set of transitions in PDA for A from state q1 to q1 in problem 3) implement transferring the top of S1 to the top of S2, where the operation is independent of the input and stack contents. 5) a) as powerful Assume a Turing machine with a two-way infinite tape. First, a Turing machine can simulate a 2-stack* PDA by partitioning its tape so that S1 grows to the left and S2 grows to the right, and executing the transitions of the 2-stack* PDA. S1 S2 On the other hand, a 2-stack* PDA can simulate the tape of a Turing machine using the two stacks. Initially, the PDA reads all of its input and pushes it onto S1. Now the PDA can move back and forth over this input by transferring symbols from S1 to S2 and from S2 to S1. If the Turing machine writes to the right of the current tape contents, the PDA empties S2 into S1 and pushes onto S1. If the Turing machine writes to the left of the current tape contents, the PDA empties S1 into S2 and pushes onto S2. S1 S2