CS375: Logic and Theory of Computing Fuhua (Frank) Cheng Department of Computer Science University of Kentucky 1
Tale of Contents: Week 1: Preliminaries (set alger relations, functions) (read Chapters 1-4) Weeks 3-6: Regular Languages, Finite Automata (Chapter 11) Weeks 7-9: Context-Free Languages, Pushdown Automata (Chapters 12) Weeks 10-12: Turing Machines (Chapter 13) 2
Tale of Contents (conti): Weeks 13-14: Propositional Logic (Chapter 6), Predicate Logic (Chapter 7), Computational Logic (Chapter 9), Algeraic Structures (Chapter 10) 3
7. Context-Free Languages & Pushdown Automata- Pushdown Automata Transform an empty-stack PDA to a C-F grammar such that language accepted y the PDA is the same as language generated y the C-F grammar C-F grammar 4
Relationship etween accepting and generating: How would a PDA accept a symol a in an input string? B w = a Must have i x (State j could e the same as state i, and B can not e Ʌ ) j How would a CFG generate a string with a symol a in it? Must have w = a B a w1 (B is a non-terminal, w1 is a string of terminals and/or non-terminals ut do not contain and w1 could e Ʌ) 5
Relationship etween accepting and generating: How would a PDA accept a symol a in an input string as follows? w = a Must have i B x (State j could e the same as state i, and B can not e Ʌ ) j How would a CFG generate a symol a in an input string as follows? Must have w = a B a (B is a non-terminal) 6
Relationship etween accepting and generating: How would a PDA accept a string a c? B w = a c Must have x i j c, B x k (State j could e the same as state i, and B can not e Ʌ ) How would a CFG generate a string a c? w = a c Must have Bik a Bjk (Bik and Bjk are non-terminals, and Bjk could e Ʌ) 7
Transform an empty-stack PDA to a C-F grammar Basic idea: Define non-terminals Bij for the CFG that can generate all strings w that: upon reading w on the input tape of the PDA will take you from state i to state j in the PDA and have a net result of ping B off the stack In essence, B is eventually replaced y w It may take many moves to get there. 8
Transform an empty-stack PDA to a C-F grammar B (B is removed) i w j B ij w (leftmost derivation) 9
Transform an empty-stack PDA to a C-F grammar Type 1: PDA instruction a is the string accepted Grammar Production B ij a B can reach an emptystack status 10
Transform an empty-stack PDA to a C-F grammar Type 1: The string accepted PDA instruction Grammar Production B ij a Example:, 1 2 B 1 2 12 11 B 12 a
Transform an empty-stack PDA to a C-F grammar Type 2: PDA instruction Grammar Production B ik a B jk for each state k B B accepted string is a followed y whatever is accepted etween state j and state k 12
Transform an empty-stack PDA to a C-F grammar PDA instruction Grammar Production B ik ab jk for each state k Example: 1 B nop 2, B 3 c, B 4 B B14 ab24 13 ab 23 13
Transform an empty-stack PDA to a C-F grammar Type 3: PDA instruction Grammar Production B il ac jk B kl for each state k and l B C B B 14
Transform an empty-stack PDA to a C-F grammar PDA instruction Grammar Production B il ac jk B kl for each state k and l Example: B B C B 1 B Push(C) 2, C 3 c, B 4 B13 a C 23 B14 a C23B34 15
Transform an empty-stack PDA to a C-F grammar Type 4: PDA instruction The production that will generate the string accepted y the PDA etween state i and state j Grammar Production S ij for each ( S is start state j symol) Could lead to an empty-stack status 16
Transform an empty-stack PDA to a C-F grammar Type 4: PDA instruction Grammar Production S ij for each state j Example: start 1 nop 2 c, 3 S 13 13 a 23 (Type 2) 17
Example. Transform the following empty-stack PDA into a C-F grammar. Solution: Type 4 The start state 0 with on the stack gives: S 01 18
Example. Transform the following empty-stack PDA into a C-F grammar. Solution: Type 1 The operation (1,,, 1) gives a 11 19
Example. Transform the following empty-stack PDA into a C-F grammar. Solution: Type 1 The operation (1, A,, 1) gives A a 11 20
Example. Transform the following empty-stack PDA into a C-F grammar. Solution: Type 2 The nop operation (0, A, nop, 1) gives A01 aa 11 21
Example. Transform the following empty-stack PDA into a C-F grammar. Solution: Type 3 The push operation (0,, push(a), 0) gives aa 01 0111 22
Empty-stack PDA: C-G Grammar: S 01 11 a A 11 a A 01 aa 11 aa 01 0111 Leftmost derivation S 01 aa 01 11 aaa 11 11 aaa 11 aaaa The language accepted y this PDA has one element only: aaaa 23
Empty-stack PDA: C-G Grammar: S 01 11 a A 11 a A aa 01 aa 11 01 0111 (0, aaa ) (0, aa A) (1, a A) (1, ) Accepted 24 A
Example. Find a grammar for the language L = {w { }* na(w) = n(w)} y (1) constructing an empty-stack PDA to accept L and then (2) transforming it into a C-F grammar. Solution: (1) To accept lamda and to reach empty-stack status To ensure # of a s and # of s are the same 25
Note the following two PDAs are equivalent: Solution 2 of HW6, #4 26
Solution: (1) Consider: aaaaa Accepted Hence, the aove PDA accepts L 27 B A A Stack is empty
Solution: (2) PDA Transformed into C - F grammar : S aa B A aaa Type =? B a B B 28
Solution: (2) PDA Transformed into C - F grammar : S aa B A aaa Type =? B a B B 29
Solution: (2) PDA Transformed into C - F grammar : S A B aa aa a B A B B Type =? 30
Solution: (2) PDA Transformed into C - F grammar : S A B aa aa a B A B B Type =? 31
Solution: (2) PDA Transformed into C - F grammar : S A B aa aa a B A B B University of Kentucky Type =? 3/22/2017 32
Solution: (2) PDA Transformed into C - F grammar : S A B aa aa a B A B B 33 Type =?
Solution: (2) PDA Transformed into C - F grammar : S A B aa aa a B A B B Type =? 34
Solution: (2) PDA Transformed into C - F grammar : S A B aa aa a B A B B Type =? 35
Solution: (2) PDA Transformed into C - F grammar S A B aa aa a B A B B : Simplified CFG : S aas BS A aaa B a BB 36
Solution: (2) Simplified CFG : S aas BS A aaa B a BB Derivation of aaa : S aas aaaas aaas aaaaas aaaas aaas aaa 37
Nondeterministic PDAs are more powerful than deterministic PDAs. An example is to consider the language of even Palindromes (such as: aaaaaa) over { }. A context-free grammar for the language is given y S Λ asa S Left half and right half are symmetric Any PDA to accept the language must make a nondeterministic decision to start comparing the 2nd half of a string with the reverse of the first half. 38
Example: consider the following PDA a a, a, Start 0 1 2,, a,,, nop nop nop,, push ( ), push ( ), a push ( ) This non-deterministic PDA accepts the language of even palindromes over { } 39
Example: consider the following PDA Why is this an non-deterministic PDA? a a, a, Start 0,, a,,, nop nop nop 1 2,, push ( ), push ( ), a push ( ) 40
Example: Does it accept consider even the palindromes following PDA over { }? a a, a, Start 0,, a,,, nop nop nop 1 2,, push ( ), push ( ), a push ( ) Consider (0, a ) If we make a guess here (0, a) (0, a) (0, a) (0, Ʌ a) (1, a) (1, a) (1, a) 41 (1, Ʌ, ) (2, Ʌ, Ʌ)
Example: The PDA may consider guess the the following middle wrong: PDA a a, a, Start 0,, a,,, nop nop nop 1 2,, push ( ), push ( ), a push ( ) Consider (0, a ) If the PDA made a guess here (0, a) (0, a) (0, Ʌ a) (1, a) (1, a) 42 It gets stuck here!
Example: This PDA can consider only accept the following even palindromes: PDA a a, a, Start 0,, a,,, nop nop nop 1 2,, push ( ), push ( ), a push ( ) Consider (0, aa ) (0, a a) (0, aa) (0, aa) (0, aa) (0, aa) (0, Ʌ, aaa) 43 (1, Ʌ, aaa) It gets stuck here!
There is in general no way to translate a nondeterministic PDA (NPDA) into a deterministic one. Indeed, there is no DPDA which recognizes the language of even palindromes. That is why we can say that NPDAs are more powerful than DPDAs. However, we can define a similar language L1 over {, $} which can e recognized y a DPDA: L1 = { w$w R w ϵ { }* } 44
Example: consider the following PDA A DPDA for L1: a a, a, Start 0 $, $, a $,,, nop nop nop 1 2,, push ( ), push ( ), a push ( ) Consider (0, a$ ) Is this PDA indeed deterministic? (0, $ a) (0, $ a) (0, $ a) (1, a) (1, a) (1, a) What do you see here? 45 (1, Ʌ, ) (2, Ʌ, Ʌ)
Note that 1. Final-state acceptance and empty-stack acceptance are equivalent only for NPDAs 2. Final-state acceptance and empty-stack acceptance are not equivalent for DPDAs. For DPDAs, the class of languages defined y final-state acceptance is igger. Why? 46
Recall: The following is a C-F grammar for the language of even palindromes over { } S Λ asa S Using the CFG to PDA algorithm, we can transform the grammar into an empty-stack NPDA as follows: (0,, 0) (0,,,, 0) (0, Λ, S,, 0) (0, Λ, S,, push(a), push(s), push(a), 0) (0, Λ, S,, push(), push(s), push(), 0) 47
Question: Is this empty-stack NPDA (0,, 0); (0,,,, 0); (0, Λ, S,, 0); (0, Λ, S,, push(a), push(s), push(a), 0); (0, Λ, S,, push(), push(s), push(), 0) equivalent to the following final-state NPDA? P1 P2 a a, a, Start 0 1, 2,, a, nop nop nop, push ( ), push ( ), a push ( ) 48
Question: Or, is this empty-stack PDA P1 a, a,, Start 0,, push (, a),, push (,, ) equivalent to the following final-state PDA? P2 a a, a, Start, push ( ) 0, push ( ) 1, 2,, a, nop nop nop, a push ( ) 49
It is easy to see that P1 accepts even palindromes a, a,, Start 0,, push (, a),, push (,, ) Consider (0, a ) (0, Ʌa ) (0, a aa) (0, a) (0, Ʌ a) (0, a) (0, a) (0, Ʌ a) (0, a) (0, a) (0, Ʌ a) (0, a) (0, a) (0, a) (0, Ʌ, Ʌ) Accepted 50 P1
P1 can only accept even palindromes a, a,, Start 0,, push (, a),, push (,, ) Consider (0, aa ) (0, Ʌaa ) (0, aa aa) (0, a a) (0, Ʌa a) (0, a aaa) (0, aa) (0, Ʌ aa) (0, aa) (0, aa) (0, Ʌ a) (0, aa) (0, aa) It gets stuck here! 51
Since P2 accepts and can only accept even palindromes over { }, P1 and P2 are equivalent. The question then is: can P1 and P2 e transformed to a C-F grammar for the language of even palindromes over { }? S Λ asa S 52
How to handle Type 3 PDA instructions for P1: a, a,, Start 0,, push (, a),, push (,, ) Consider the following situation: P1,, push (, a) 0 0 Y a a Y 53 a Y a Y Y
How to handle Type 3 PDA instructions for P1: a, a,, Start 0,, push (, a),, push (,, ) Consider the following situation: P1,, push (, a) 0 0 a, a 0 0 0 Y a a Y 54 a Y a Y Y
How to handle Type 3 PDA instructions for P1: a, a,, Start 0,, push (, a),, push (,, ) Consider the following situation: P1,, push (, a) 0 0 a, a 0 0 0 A A 55
Summarize: CFGs and PDAs have equivalent expressive powers. More formally,... Theorem. For every CFG G, there is a PDA P such that L(G) = L(P). In addition, for every PDA P, there is a CFG G such that L(P) = L(G). Thus, L is CF iff there is a (non-deterministic) PDA P such that L = L(P). CF languages are exactly those languages that are accepted y (non-deterministic) PDAs. 56
Summarize: A CF language is called a deterministic final-state CF language if it can e recognized y a deterministic final-state PDA Even palindromes: not a deterministic final-state CFL a NPDA Start, push ( ) 0, push ( ),, a,,, nop nop nop, a push ( ) 1 2, 57
Summarize: L1 over {, $} is a deterministic final-state CFL: L1 = { w$w R w ϵ { }* } a DPDA Start, push ( ) 0, push ( ) $, $, a $,,, nop nop nop, a push ( ) 1 2, 58
End of Context-Free Language and Pushdown Automata II 59