Download presentation

Presentation is loading. Please wait.

Published byHassan Willits Modified about 1 year ago

1
Nathan Brunelle Department of Computer Science University of Virginia Theory of Computation CS3102 – Spring 2014 A tale of computers, math, problem solving, life, love and tragic death

2
Closure Properties of CFLs Theorem: The context-free languages are closed under union. Hint: Derive a new grammar for the union. Let G 1 =(V 1, Σ,R 1, S 1 ), G 2 =(V 2, Σ,R 2, S 2 ) Then G 1+2 =(V 1 +V 2 +{S}, Σ, R 1 + R 2 +{S→ S 1 | S 2 }, S) S→ S 1 | S 2 S 1 →… S 2 →…

3
Closure Properties of CFLs Theorem: The CFLs are closed under Kleene closure. Hint: Derive a new grammar for the Kleene closure. Let G =(V, Σ,R, S) Then G * =(V, Σ,R+ {S → SS|ε}, S) S → SS|ε|…

4
Closure Properties of CFLs Theorem: The CFLs are closed under with regular langs. Hint: Simulate PDA and FA in parallel. Let FSA M fsa =(Q f, , f, q f, F f ), PDA M pda =(Q p, , , p, q p, F p ) Then M ∩ =(Q f ×Q p, , (q f,q p ), F f ×F p ) Q f ×Q p ( { }) 2 Q s.t. q f, q p, f q f, p q p, M pda a b a stack M fsa ×

5
Closure Properties of CFLs Theorem: The CFLs are not closed under intersection. Hint: Find a counter example. L={a n b n c n } is not context free (Involves pumping lemma of CFLs, we’ll get to it) Let L 1 ={a i b j c j }, L 2 ={a i b i c j } L 1 ∩L 1 = L

6
Closure Properties of CFLs Theorem: The CFLs are not closed under complementation. Hint: Use De Morgan’s law. Assume FSORC that CFLs are closed under complementation. Since we know that they are closed under union we can find

7
Decidable PDA / CFG Problems Given an arbitrary pushdown automata M the following problems are decidable (i.e., have algorithms): Q 1 : Is L(M) = Ø ? Q 5 : Is L(G) = Ø ? Q 2 :Is L(M) finite ? Q 6 :Is L(G) finite ? Q 3 :Is L(M) infinite ? Q 7 :Is L(G) infinite ? Q 4 : Is w L(M) ? Q 8 : Is w L(G) ? ≡ (or CFG G) Extra-credit: prove each!

8
Theorem: the following are undecidable (i.e., there exist no algorithms to answer these questions): Q: Is PDA M minimal ? Q: Are PDAs M 1 and M 2 equivalent ? Q: Is CFG G minimal ? Q: Is CFG G ambiguous ? Q: Is L(G 1 ) = L(G 2 ) ? Q: Is L(G 1 ) L(G 2 ) = Ø ? Q: Is CFL L inherently ambiguous ? Undecidable PDA / CFG Problems ≡ Extra-credit: prove each!

9
PDA Enhancements Theorem: 2-way PDAs are more powerful than 1-way PDAs. Hint: Find an example non-CFL accepted by a 2-way PDA. Theorem: 2-stack PDAs are more powerful than 1-stack PDAs. Hint: Find an example non-CFL accepted by a 2-stack PDA. Theorem: 1-queue PDAs are more powerful than 1-stack PDAs. Hint: Find an example non-CFL accepted by a 1-queue PDA. Theorem: 2-head PDAs are more powerful than 1-head PDAs. Hint: Find an example non-CFL accepted by a 2-head PDA. Theorem: Non-determinism increases the power of PDAs. Hint: Find a CFL not accepted by any deterministic PDA. Extra-credit: prove each!

10
PDA = CFG Theorem: If a Language is derivable by some context free grammar then it is accepted by some PDA Proof idea: Construct a PDA which checks if there is a way of reversing the substitution rules of the grammar to produce the start variable. Machine description: 1.Put the symbol ‘$’ onto the stack 2.Put the start symbol onto the stack 3.Repeat the following: If the top of the stack has a variable V, nondeterministically choose a substitution rule. Pop the V and push the rhs of the rule onto the stack If the top is a terminal symbol ‘a’ then check if the next input symbol is also ‘a’. If so then pop the ‘a’, if not then reject. If the top of the stack is ‘$’ then enter the accept state, accept if all input has been read. Loop startend ε, ↓$ Var S ε, ↓S ε, ↑V↓R a, ↑a ε, ↑$

11
PDA = CFG Example: S→aSb|ε Loop startend ε, ↓$ Var S ε, ↓S ε, ↑S ε, ↑$ a, ↑a b, ↑b ε, ↑S↓bSa

12
PDA = CFG Example: S→aSaa|B B→bB|ε Loop startend ε, ↓$ Var S ε, ↓S ε, ↑S↓aaSa ε, ↑$ ε, ↑S↓B a, ↑a b, ↑b ε, ↑B↓Bb ε, ↑B

13
PDA = CFG Theorem: If a Language is accepted by some PDA then it is derivable by some CFG Proof idea: For every pair of states (p,q) in the machine construct a variable. This variable generates all strings that cause the machine to transition from p with an empty stack to q on an empty stack. The variable for (start, accept) is the start variable. Machine restrictions: There is a unique accept state The stack is empty before accepting Each transition performs at most 1 stack operation Construction: There are two cases for states (p,q): The last item popped is the same as the first item pushed, apply the rule V pq →a V rs b where ‘a’ matches the transition out of p to state r, and ‘b’ matches the transition out of state s to state q. The stack was emptied previously in in the path. For the latter we add rule V pq →V pr V rq where r is the state where the stack becomes empty.

14
PDA = CFG Example: V start,start → ε V start,a → a V start,b →ab V start,end → a V a,b a a‘s a end b‘s start b a V a,a → ε V a,b → b V a,end →ba V b,b → ε V b,end →a V end,end → ε

15
PDA = CFG Example: V start,start → ε V start,a → V start,b → V start,end → ε| ε V a,b ε V a,start → V a,a → ε a‘s ε, ↓$ end b‘s a, ↓a start b, ↑a ε, ↑$ b, ↑a ε V a,b → a V a,b b|a V a,a b V a,end → V b,start → V b,a → V b,b → ε V b,end → V end, start → V end,a → V end,b → V end,end → ε

16
PDA = CFG Example: V start,start → ε V start,a → V start,b → V start,end → ε V a,b ε V a,start → V a,a → ε V a,b → a V a,b a| b V a,b b| ε V a,end → V b,start → V b,a → V b,b → ε V b,end → V end, start → V end,a → V end,b → V end,end → ε a‘s ε, ↓$ end b‘s a, ↓a b, ↓b start ε ε, ↑$ a, ↑ a b, ↑ b

17
Parse Tree Describes derivation of a string from a CFG Examples: S→aSaa|B B→BB|b|ε S S S B BB aa aa b b S a for each a L S (S) | SS | S* | S+S S S S SS (a+b)*

18
Pumping Lemma for Context Free Languages If L is a CFL then there is some number p such that for any string s in L where |s|>p we can divide s into 5 substrings s=uvxyz in a way to satisfy all of the following: 1.For each i≥0, 2.|vy|>0 3.|vxy|≤p

19
Pumping Lemma for Context Free Languages 1.For each i≥0, 2.|vy|>0 3.|vxy|≤p Proof: Consider the CFG for L. The grammar has a finite number of production rules. If a string is long enough, some variable must have been reached twice. S R R uvxyz S R u x z S uz R vy R vxy R

20
Pumping Lemma for Context Free Languages 1.For each i≥0, 2.|vy|>0 3.|vxy|≤p Example: Consider: If any of a,b,c are not part of vy then we definitely fail rule 1. Because of rule 3 if v contains the letter ‘a’ then y cannot contain the letter ‘b’

21
Pumping Lemma for Context Free Languages 1.For each i≥0, 2.|vy|>0 3.|vxy|≤p Example:. Consider: If neither a,c or b,d are not part of vy then we definitely fail rule 1. Because of rule 3 if v contains the letter ‘a’ then y cannot contain the letter ‘c’ If v contains the letter ‘b’ then y cannot contain the letter ‘d’

22
Pumping Lemma for Context Free Languages 1.For each i≥0, 2.|vy|>0 3.|vxy|≤p Example: Consider: If I modify the first set of ‘a’s then I must also modify the second set. If v contains one of the first set of ‘a’s then y cannot contain one of the second set due to rule 3. The case for the ‘b’s is similar.

23
Here Be an extra slide

24
Pumping Lemma for Context Free Languages 1.For each i≥0, 2.|vy|>0 3.|vxy|≤p Proof: Consider the CFG for L. The grammar has a finite number of production rules. If a string is long enough, some rule must have been applied twice. S R R uvxyz S R u x z S R R uvxyz R vy

Similar presentations

© 2016 SlidePlayer.com Inc.

All rights reserved.

Ads by Google