Download presentation
Presentation is loading. Please wait.
Published byVivien Hall Modified over 9 years ago
1
Nathan Brunelle Department of Computer Science University of Virginia www.cs.virginia.edu/~njb2b/theory Theory of Computation CS3102 – Spring 2014 A tale of computers, math, problem solving, life, love and tragic death
2
Midterm Take home!+ Date: After Spring break! Released: March 19 5pm Due: March 23 7pm Rules: Open book, open notes, open Wikipedia (no other web sites, no use of friends) You may download any time during the window, but turn in the exam within 24 hours of download You have 6 contiguous hours to work on the exam It will be near impossible to complete unless you’ve already done many problem set problems
3
Context-Free Grammars Basic idea: set of production rules induces a language Finite set of variables: V = {V 1, V 2,..., V k } Finite set of terminals: Σ = {t 1, t 2,..., t j } Finite set of productions: R Start symbol: S Productions: V i where V i V and (V Σ)* Applying V i to V i yields: Note: productions do not depend on “context” - hence the name “context free”!
4
Ambiguity Def: A grammar is ambiguous if some string in its language has two non-isomorphic derivations. Theorem: Some context-free grammars are ambiguous. Ex: G 1 :S SS | a | Derivation 1: S SS aa Derivation 2: S SS SSS aa Def: A context-free language is inherently ambiguous if every context-free grammar for it is ambiguous. Theorem: Some context-free languages are inherently ambiguous (i.e., no non-ambiguous CFG exists). Ex: {a i b j c k | i=j or j=k} Prove this for EC! Fault of the grammar Fault of the language!
5
Example : design a context-free grammar that generates all valid regular expressions. Idea: embedd the RE rules in a grammar. G:S a for each a L S (S) | SS | S* | S+S S S* (S)* (S+S)* (a+b)* S SS SSSS abS*b aba*a Q: Is G ambiguous?
6
Pushdown Automata Basic idea: a pushdown automaton is a finite automaton that can optionally write to an unbounded stack. Finite set of states: Q = {q 0, q 1, q 3,..., q k } Input alphabet: Stack alphabet: Transition function: : Q ( { }) 2 Q Initial state:q 0 Q Final states:F Q Pushdown automaton is M=(Q, , , , q 0, F) Note: pushdown automata are non-deterministic! (why?) q0q0 qiqi qjqj q1q1 qkqk
7
Pushdown Automata A pushdown automaton can use its stack as an unbounded but access-controlled (last-in/first-out or LIFO) storage. A PDA accesses its stack using “push” and “pop” Stack & input alphabets may differ. Input read head only goes 1-way. Acceptance can be by final state or by empty-stack. Note: a PDA can be made deterministic by restricting its transition function to unique next moves: : Q ( { }) Q M 1010011 Input a b a stack
8
Pushdown Automata a‘s ε, ↓$ end b‘s a, ↓a start b, ↑a ε, ↑$ b, ↑a $ a a stack aaabbb Input a
9
Pushdown Automata a‘s ε, ↓$ end b‘s a, ↓a b, ↓b start ε ε, ↑$ $ a b stack abbbba Input b a, ↑ a b, ↑ b (note I only show the accepting path)
10
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 →…
11
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|ε|…
12
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 ×
13
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
14
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
15
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!
16
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!
17
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!
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.