HW6 Solutions. Tommy Schmitz. February 25, 2014

HW6 Solutions Tommy Schmitz February 25, 2014 1 Exercise 5.6 The first PDA accepts strings whose first and last letter are equal, i.e. a(a + b)∗ a ...
Author: Alfred Wade
0 downloads 3 Views 152KB Size
HW6 Solutions Tommy Schmitz February 25, 2014

1

Exercise 5.6

The first PDA accepts strings whose first and last letter are equal, i.e. a(a + b)∗ a + b(a + b)∗ b. Its diagram is below. a, a/a b, a/a a, b/b b, b/b

start

q0

a, Z0 /aZ0 b, Z0 /bZ0

q1

a, a/a b, b/b

q2

The second PDA accepts odd-length strings whose middle symbol is c and whose other letters are as and bs. In other words, it accepts the language {xcy | x, y ∈ {a, b}∗ and |x| = |y|}. Its diagram is below. a, Z0 /XZ0 b, Z0 /XZ0 a, X/XX a, X/Λ b, X/Λ b, X/XX c, X/X c, Z0 /Z0 Λ, Z0 /Z0 q0 q1 start

1

q2

2 2.1

Exercise 5.12 Informal answer

If a PDA never removes any symbols from the stack, then we can convert it into an equivalent NFA. For example, consider the PDA from exercise 5.6: a, a/a b, a/a a, b/b b, b/b

q0

start

a, Z0 /aZ0 b, Z0 /bZ0

q1

a, a/a b, b/b

q2

The equivalent NFA is: start

(q0 , Z0 )

(q1 , Z0 )

a

(q0 , a)

b

(q2 , Z0 )

a, b

(q1 , a)

a

(q2 , a)

a, b

(q0 , b)

2.2

(q1 , b)

b

(q2 , b)

The real answer

Let M = (Q, Σ, Γ, q0 , Z0 , A, δ) be a PDA in which no symbols are ever removed from the stack (this means that none of the transitions replace the 2

top-of-stack symbol with Λ). I will create an NFA M 0 = (Q0 , Σ, q00 , A0 , δ 0 ) accepting the same language as M ; it is defined as follows. • Define Q0 = Q × Γ to be the set of states in M 0 . So the number of states is |Q| · |Γ|. • Define q00 = (q0 , Z0 ) to be the start state of M 0 . • Define A0 = A × Γ to be the final states of M 0 . So the new final states just correspond to the old final states. σ

• We want (q1 , γ1 ) → − (q2 , γ2 ) to be a transition in M 0 if and only if σ,γ1 /γ2 α

q1 −−−−−→ q2 is a transition in M (for some α ∈ Γ∗ ). We can achieve this with the definition:  δ 0 (q1 , γ1 ), σ = {(q2 , γ2 ) | (q2 , γ2 α) ∈ δ(q1 , σ, γ1 ) for some α ∈ Γ∗ } . Notice that every transition in M will have a corresponding transition in M 0 because none of M ’s transitions replace the top-of-stack symbol with Λ. It remains to prove that L(M 0 ) = L(M ). But this is obvious because a path in one machine corresponds precisely to a path in the other machine. Therefore, there will be an accepting path in M if and only if there is an accepting path in M 0 .

3

Exercise 5.18(d)

The accepting states State q0 q0 q1 q1 q2 q2 q3 q4 q4 q5 q5

are q0 , q3 , q6 , and the transition table is Input Stack New state New stack a Z0 q1 aZ0 b Z0 q4 bZ0 a a q1 aa b a q2 Λ b a q2 Λ Λ Z0 q3 Z0 b Z0 q4 bZ0 b b q4 bb q5 Λ a b a b q5 Λ Λ Z0 q6 Z0 . 3

The diagram: a, a/aa

start

q0

a, Z0 /aZ0

q1

b, a/Λ b, a/Λ

Λ, Z0 /Z0

b, Z0 /bZ0 b, b/bb

q2

q4

b, Z0 /bZ0

q3

a, b/Λ a, b/Λ

q5

Λ, Z0 /Z0

q6

The PDA is clearly deterministic because each state has no more than two outward transitions, and in those cases, the choice can be disambiguated by either the input symbol or the stack symbol. Furthermore, the PDA accepts the desired language because: • The strings reaching q0 are {Λ}. The stack contents will be just Z0 at this time. • The strings reaching q1 are {ai | i ≥ 1}. The stack contents will be ai Z0 if the input string is ai . • The strings reaching q2 are {ai bj | 1 ≤ j ≤ i}. The stack contents will be ai−j Z0 at this time. • The strings reaching q3 are {an bn | n ≥ 1}. The stack contents will be just Z0 again. • The strings reaching q4 are {ai bj | i < j}. The stack contents will be bj−i Z0 . • The strings reaching q5 are {an bn+m ai | n ≥ 0 and 1 ≤ i ≤ m}. The stack contents will be bm−i Z0 . • The strings reaching q6 are {an bn+m am | n ≥ 0 and m ≥ 1}. The stack contents will be just Z0 . 4

4 4.1

Exercise 5.19 Concatenation

Let M1 and M2 be PDAs accepting L1 and L2 , respectively. We can assume without loss of generality that their stack alphabets are disjoint. The construction is much like the one for NFAs. To create a PDA M 0 accepting L1 L2 , simply draw transitions labeled Λ, Λ/Z2 from each of the final states of M1 to the start state of M2 (where Z2 is the initial stack symbol of M2 ). The start state and initial stack symbol of M 0 should be those of M1 ; the final states of M 0 should be those of M2 . This works because paths through the new machine must correspond to paths in M1 followed by paths in M2 . An accepting path in M 0 can be split into an accepting path in M1 followed by an accepting path in M2 ; conversely, an accepting path in M1 can be combined with an accepting path in M2 to form an accepting path in M 0 . It is interesting to note that when M 0 reaches the second part of the string, there may be stack symbols from M1 underneath the starting stack symbol for M2 . Thankfully, the behavior of M2 cannot be affected by any symbols underneath its start symbol because we have assumed that none of them will belong to its stack alphabet.

4.2

Kleene star

Let M be a PDA accepting a language L. Again, the construction is much like the one for NFAs. To create a PDA M 0 accepting L∗ , we add a new state to M . The new state becomes the new start state and also the only final state. Add a transition labeled Λ, Λ/Z0 Z00 from the new state to the old start state (where Z0 is the initial stack symbol of M and Z00 is some symbol not in the stack alphabet of M ). Add transitions labeled Λ, Λ/Λ from the old final states to the new state. The initial stack symbol of M 0 is unimportant, but presumably it ought to be Z00 . This works because an accepting path in M 0 can be split into some number of accepting paths through M , and conversely any number of accepting paths through M can be combined into a single accepting path through M 0 . Again, when M 0 transitions into the states of M , there may be more symbols underneath the Z0 . Again though, the behavior of M cannot be 5

affected by the symbols underneath because the top-most one is guaranteed (by our construction) to be Z00 , which is assumed not to belong to the stack alphabet of M .

5

Exercise 5.27

5.1

Part a

1. Λ is balanced. 2. If x and y are balanced, then xy and [x] and {x} are balanced.

5.2

Part b State q0 q0 q1 q1 q1 q1 q1 q1 q1

Input [ { [ [ { { ] } Λ

Stack Z0 Z0 [ { [ { [ { Z0

New state q1 q1 q1 q1 q1 q1 q1 q1 q0

[, Z0 /[Z0 {, Z0 /{Z0 start

q0

q1 Λ, Z0 /Z0

New stack [Z0 {Z0 [[ [{ {[ {{ Λ Λ Z0 [, [/[[ [, {/[{ {, [/{[ {, {/{{ ], [/Λ }, {/Λ

The PDA is deterministic because the outward transitions of each state can be uniquely determined by the input symbol and the top-of-stack symbol. The PDA pushes left-brackets onto the stack when it sees them and it ensures that the style (curly or square) of an incoming right-bracket matches the style of the bracket on top of the stack. The PDA sits in a final state if and only if the stack is empty (aside from Z0 ). 6

6

CYK Exercise

It can be helpful to first “invert” the grammar: a ← A, C b←B AA ← A AB ← A, S AC ← C, S BC ← B, S BB ← B CC ← C Next, make the table. There are two equivalent formats: 1 2 3 4 5

b a b a a B AC B AC AC BS AS BS ACS B ACS BS BS ACS BS a b aa ab ba aba baa bab baba abaa babaa

AC B ACS AS BS ACS BS B BS ACS BS

Since S appears in the last cell of the table, we know that babaa is generated by the grammar.

7

7

Exercise 5.11

7.1

Informal answer

It is possible to convert any PDA into a new PDA whose stack alphabet consists of only 3 symbols (in fact, 2 is also sufficient). The trick is to use multiple stack symbols to encode the old stack symbols. (The new PDA will need more states and transitions so that it can “encode” and “decode” the stack.) 7.1.1

Example

Consider the following stack with six different symbols: ABCDECABZ0 . We can transform this into an “equivalent” stack with only three different symbols: XY XXY XXXY XXXXY XXXXXY XXXY XY XXY Z0 . The number of Xs encodes the original stack symbol; the Y s serve to separate the Xs from each other. 7.1.2

Full example

Let’s convert this PDA a, Z0 /CZ0 a, A/C a, B/AB b, B/BB b, C/Λ start

q0

q1 Λ, B/B

by encoding A → Y , B → XY , C → XXY .

8

a, Z0 /XXY Z0 a, Y /XXY start

(q0 , A)

Λ, X/Λ

(q1 , A)

a, Y /Y XY b, Y /XY XY

Λ, X/Λ

(q0 , B)

(q1 , B)

Λ, X/Λ

Λ, Y /XY

Λ, X/Λ b, Y /Λ

(q0 , C)

7.2

(q1 , C)

The real answer

Let M = (Q, Σ, Γ, q0 , Z0 , A, δ) be a PDA. I will create a new PDA M 0 = (Q0 , Σ, Γ0 , q00 , Z0 , A0 , δ 0 ) accepting the same language as M , but with only three stack symbols. It will be convenient to give names to the members of Γ, so let us write Γ = {γ0 , γ1 , . . . , γm , Z0 }. We will also want to define the “stack encoding” function f : f (Λ) = Λ f (γi α) = X i Y f (α) f (Z0 α) = Z0 f (α). M 0 is defined as follows: 9

• Define Q0 = Q×(Γ−{Z0 }) to be the set of states in M 0 . So the number of states in M 0 is |Q| · (|Γ| − 1). • Define Γ0 = {X, Y, Z0 } to be the stack alphabet of M 0 . • Define q00 = (q0 , γ0 ) to be the start state of M 0 . • Define A0 = A × (Γ − {Z0 }) to be the final states of M 0 . So the new final states just correspond to the old final states. σ,Z0 /α

σ,Z0 /f (α)

σ,γi /α

σ,Y /f (α)

• If q1 −−−−→ q2 is a transition in M , then (q1 , γ0 ) −−−−−→ (q2 , γ0 ) is a transition in M 0 . Let us call these transitions of type 1. • If q1 −−−→ q2 is a transition in M , then (q1 , γi ) −−−−−→ (q2 , γ0 ) is a transition in M 0 . Let us call these transitions of type 2. Λ,X/Λ

• M 0 also has the transition (q, γi ) −−−→ (q, γi+1 ) for all q ∈ Q and i < m. Let us call these transitions of type 3. It remains to prove that L(M 0 ) = L(M ). To do so, I must show that accepting paths in M 0 correspond to accepting paths in M . The hand-waving in the next paragraph achieves this. Given a path in either machine, the transitions of type 1 can be translated into a corresponding transition in the other machine. For other transitions, σ,γi /α

we see that a single transition q1 −−−→ q2 in M can be translated into i transitions of type 3 followed by one transition of type 2 in M 0 . Thanks to the careful construction of M 0 , the reverse translation is also possible (converting a block of transitions of type 3 and one transition of type 2 into a single transition in M ).

10