push down automata (pda)bchor/cm05/compute4.pdf · push down automata (pda) nondeterminism adds...
TRANSCRIPT
Computational Models - Lecture 4
Push Down Automata (PDA)
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.1
Computational Models - Lecture 4
Push Down Automata (PDA)
Nondeterminismadds powerto PDAs
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.1
Computational Models - Lecture 4
Push Down Automata (PDA)
Nondeterminismadds powerto PDAs
Equivalenceof CFGs and PDAs
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.1
Computational Models - Lecture 4
Push Down Automata (PDA)
Nondeterminismadds powerto PDAs
Equivalenceof CFGs and PDAs
Pumping Lemmafor context free languages
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.1
Computational Models - Lecture 4
Push Down Automata (PDA)
Nondeterminismadds powerto PDAs
Equivalenceof CFGs and PDAs
Pumping Lemmafor context free languages
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.1
Computational Models - Lecture 4
Push Down Automata (PDA)
Nondeterminismadds powerto PDAs
Equivalenceof CFGs and PDAs
Pumping Lemmafor context free languages
Sipser’s book, 2.2 & 2.3
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.1
even if youcan’t hear me
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.2
even if youcan’t hear me
There will be a mid-termexam on Friday, Dec. 3
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.2
even if youcan’t hear me
There will be a mid-termexam on Friday, Dec. 3Material includes first five lectures
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.2
even if youcan’t hear me
There will be a mid-termexam on Friday, Dec. 3Material includes first five lectures
Can use4 pages, double sided, any size font youcan read, and also your heads (optionally)
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.2
String Generators and String Acceptors
Regular expressions arestring generators– theytell us how to generate all strings in a languageL
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.3
String Generators and String Acceptors
Regular expressions arestring generators– theytell us how to generate all strings in a languageL
Finite Automata (DFA, NFA) arestring acceptors– they tell us if a specific stringw is inL
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.3
String Generators and String Acceptors
Regular expressions arestring generators– theytell us how to generate all strings in a languageL
Finite Automata (DFA, NFA) arestring acceptors– they tell us if a specific stringw is inL
CFGs arestring generators
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.3
String Generators and String Acceptors
Regular expressions arestring generators– theytell us how to generate all strings in a languageL
Finite Automata (DFA, NFA) arestring acceptors– they tell us if a specific stringw is inL
CFGs arestring generators
Are therestring acceptors forContext-Freelanguages?
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.3
String Generators and String Acceptors
Regular expressions arestring generators– theytell us how to generate all strings in a languageL
Finite Automata (DFA, NFA) arestring acceptors– they tell us if a specific stringw is inL
CFGs arestring generators
Are therestring acceptors forContext-Freelanguages?
YES! Push-down automata
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.3
A Finite Automaton
011011001
read unread
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.4
A PushDown Automaton
01101
read unread
pop
push
10
abba
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.5
An Example
Recall that the language{0n1n | n ≥ 0} is not regular.Consider the following PDA:
Read input symbols
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.6
An Example
Recall that the language{0n1n | n ≥ 0} is not regular.Consider the following PDA:
Read input symbols
For each0, push it on the stack
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.6
An Example
Recall that the language{0n1n | n ≥ 0} is not regular.Consider the following PDA:
Read input symbols
For each0, push it on the stack
As soon as a1 is seen, pop a0 for each1 read
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.6
An Example
Recall that the language{0n1n | n ≥ 0} is not regular.Consider the following PDA:
Read input symbols
For each0, push it on the stack
As soon as a1 is seen, pop a0 for each1 read
Acceptif stack isemptywhenlast symbol read
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.6
An Example
Recall that the language{0n1n | n ≥ 0} is not regular.Consider the following PDA:
Read input symbols
For each0, push it on the stack
As soon as a1 is seen, pop a0 for each1 read
Acceptif stack isemptywhenlast symbol read
Rejectif
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.6
An Example
Recall that the language{0n1n | n ≥ 0} is not regular.Consider the following PDA:
Read input symbols
For each0, push it on the stack
As soon as a1 is seen, pop a0 for each1 read
Acceptif stack isemptywhenlast symbol read
RejectifStack isnon-emptywhenlast symbol read
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.6
An Example
Recall that the language{0n1n | n ≥ 0} is not regular.Consider the following PDA:
Read input symbols
For each0, push it on the stack
As soon as a1 is seen, pop a0 for each1 read
Acceptif stack isemptywhenlast symbol read
RejectifStack isnon-emptywhenlast symbol readStack isemptybut input symbol(s) still exist,
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.6
An Example
Recall that the language{0n1n | n ≥ 0} is not regular.Consider the following PDA:
Read input symbols
For each0, push it on the stack
As soon as a1 is seen, pop a0 for each1 read
Acceptif stack isemptywhenlast symbol read
RejectifStack isnon-emptywhenlast symbol readStack isemptybut input symbol(s) still exist,0 is read after1.
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.6
PDA Configuration
A Configurationof a Push-Down Automata is atriplet
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.7
PDA Configuration
A Configurationof a Push-Down Automata is atriplet
(<state>,<remaining input string>,<stack>)
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.7
PDA Configuration
A Configurationof a Push-Down Automata is atriplet
(<state>,<remaining input string>,<stack>)
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.7
PDA Configuration
A Configurationof a Push-Down Automata is atriplet
(<state>,<remaining input string>,<stack>)
A configuration is like asnapshotof PDAprogress
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.7
PDA Configuration
A Configurationof a Push-Down Automata is atriplet
(<state>,<remaining input string>,<stack>)
A configuration is like asnapshotof PDAprogress
A PDA computationis a sequence of successiveconfigurations, starting fromstart configuration
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.7
PDA Configuration
A Configurationof a Push-Down Automata is atriplet
(<state>,<remaining input string>,<stack>)
A configuration is like asnapshotof PDAprogress
A PDA computationis a sequence of successiveconfigurations, starting fromstart configuration
The string describing the stack has top of thestack on the left (technical item)
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.7
Comparing PDA and Finite Automata
PDA may be deterministic or non-deterministic.
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.8
Comparing PDA and Finite Automata
PDA may be deterministic or non-deterministic.
Unlike finite automata, non-determinism addspower.
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.8
Comparing PDA and Finite Automata
PDA may be deterministic or non-deterministic.
Unlike finite automata, non-determinism addspower.
There are some languages acceptedonly bynon-deterministicPDAs.
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.8
Comparing PDA and Finite Automata
PDA may be deterministic or non-deterministic.
Unlike finite automata, non-determinism addspower.
There are some languages acceptedonly bynon-deterministicPDAs.
Transition functionδ looks different than DFA or NFA
cases, reflectingstackfunctionality.
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.8
The Transition FunctionDenote input alphabet byΣ and stack alphabet byΓ.
thedomainof the transition functionδ is
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.9
The Transition FunctionDenote input alphabet byΣ and stack alphabet byΓ.
thedomainof the transition functionδ iscurrent state:Q
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.9
The Transition FunctionDenote input alphabet byΣ and stack alphabet byΓ.
thedomainof the transition functionδ iscurrent state:Qnext input, if any:Σε (=Σ ∪ {ε})
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.9
The Transition FunctionDenote input alphabet byΣ and stack alphabet byΓ.
thedomainof the transition functionδ iscurrent state:Qnext input, if any:Σε (=Σ ∪ {ε})stack symbol popped, if any:Γε(=Γ ∪ {ε})
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.9
The Transition FunctionDenote input alphabet byΣ and stack alphabet byΓ.
thedomainof the transition functionδ iscurrent state:Qnext input, if any:Σε (=Σ ∪ {ε})stack symbol popped, if any:Γε(=Γ ∪ {ε})
and itsrangeis
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.9
The Transition FunctionDenote input alphabet byΣ and stack alphabet byΓ.
thedomainof the transition functionδ iscurrent state:Qnext input, if any:Σε (=Σ ∪ {ε})stack symbol popped, if any:Γε(=Γ ∪ {ε})
and itsrangeisnew state:Q
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.9
The Transition FunctionDenote input alphabet byΣ and stack alphabet byΓ.
thedomainof the transition functionδ iscurrent state:Qnext input, if any:Σε (=Σ ∪ {ε})stack symbol popped, if any:Γε(=Γ ∪ {ε})
and itsrangeisnew state:Qstack symbol pushed, if any:Γε
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.9
The Transition FunctionDenote input alphabet byΣ and stack alphabet byΓ.
thedomainof the transition functionδ iscurrent state:Qnext input, if any:Σε (=Σ ∪ {ε})stack symbol popped, if any:Γε(=Γ ∪ {ε})
and itsrangeisnew state:Qstack symbol pushed, if any:Γε
non-determinism:P(two components above)
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.9
The Transition FunctionDenote input alphabet byΣ and stack alphabet byΓ.
thedomainof the transition functionδ iscurrent state:Qnext input, if any:Σε (=Σ ∪ {ε})stack symbol popped, if any:Γε(=Γ ∪ {ε})
and itsrangeisnew state:Qstack symbol pushed, if any:Γε
non-determinism:P(two components above)
δ : Q× Σε × Γε → P(Q× Γε)
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.9
Formal DefinitionsA pushdown automaton(PDA) is a 6-tuple(Q,Σ,Γ, δ, q0, F ), where
Q is a finite set called thestates,
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.10
Formal DefinitionsA pushdown automaton(PDA) is a 6-tuple(Q,Σ,Γ, δ, q0, F ), where
Q is a finite set called thestates,
Σ is a finite set called theinput alphabet,
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.10
Formal DefinitionsA pushdown automaton(PDA) is a 6-tuple(Q,Σ,Γ, δ, q0, F ), where
Q is a finite set called thestates,
Σ is a finite set called theinput alphabet,
Γ is a finite set called thestack alphabet,
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.10
Formal DefinitionsA pushdown automaton(PDA) is a 6-tuple(Q,Σ,Γ, δ, q0, F ), where
Q is a finite set called thestates,
Σ is a finite set called theinput alphabet,
Γ is a finite set called thestack alphabet,
δ : Q× Σε × Γε → P(Q× Γε) is thetransitionfunction,
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.10
Formal DefinitionsA pushdown automaton(PDA) is a 6-tuple(Q,Σ,Γ, δ, q0, F ), where
Q is a finite set called thestates,
Σ is a finite set called theinput alphabet,
Γ is a finite set called thestack alphabet,
δ : Q× Σε × Γε → P(Q× Γε) is thetransitionfunction,
q0 ∈ Q is thestart state, and
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.10
Formal DefinitionsA pushdown automaton(PDA) is a 6-tuple(Q,Σ,Γ, δ, q0, F ), where
Q is a finite set called thestates,
Σ is a finite set called theinput alphabet,
Γ is a finite set called thestack alphabet,
δ : Q× Σε × Γε → P(Q× Γε) is thetransitionfunction,
q0 ∈ Q is thestart state, and
F ⊆ Q is the set ofaccept states.
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.10
ConventionsIt will be convenient to be able to know when thestack is empty, but there isno built-in mechanismto do that.
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.11
ConventionsIt will be convenient to be able to know when thestack is empty, but there isno built-in mechanismto do that.
Solution:
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.11
ConventionsIt will be convenient to be able to know when thestack is empty, but there isno built-in mechanismto do that.
Solution:Start by pushing$ onto stack.
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.11
ConventionsIt will be convenient to be able to know when thestack is empty, but there isno built-in mechanismto do that.
Solution:Start by pushing$ onto stack.When you see it again, stack is empty.
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.11
ConventionsIt will be convenient to be able to know when thestack is empty, but there isno built-in mechanismto do that.
Solution:Start by pushing$ onto stack.When you see it again, stack is empty.
Question: When is input string exhausted?
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.11
ConventionsIt will be convenient to be able to know when thestack is empty, but there isno built-in mechanismto do that.
Solution:Start by pushing$ onto stack.When you see it again, stack is empty.
Question: When is input string exhausted?It doesn’t matter. No modifications needed!
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.11
ConventionsIt will be convenient to be able to know when thestack is empty, but there isno built-in mechanismto do that.
Solution:Start by pushing$ onto stack.When you see it again, stack is empty.
Question: When is input string exhausted?It doesn’t matter. No modifications needed!’Caus accepting state is effective only if inputstring was exhausted!
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.11
NotationTransitiona, b→ c means
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.12
NotationTransitiona, b→ c means
reada from input
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.12
NotationTransitiona, b→ c means
reada from inputpopb from stack
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.12
NotationTransitiona, b→ c means
reada from inputpopb from stackpushc onto stack
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.12
NotationTransitiona, b→ c means
reada from inputpopb from stackpushc onto stack
Meaning ofε transitions:
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.12
NotationTransitiona, b→ c means
reada from inputpopb from stackpushc onto stack
Meaning ofε transitions:
if a = ε, don’t read inputs
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.12
NotationTransitiona, b→ c means
reada from inputpopb from stackpushc onto stack
Meaning ofε transitions:
if a = ε, don’t read inputsif b = ε, don’t pop any symbols
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.12
NotationTransitiona, b→ c means
reada from inputpopb from stackpushc onto stack
Meaning ofε transitions:
if a = ε, don’t read inputsif b = ε, don’t pop any symbolsif c = ε, don’t push any symbols
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.12
Example
The PDA
ε,ε $1q q2
q3q4
0,ε $
1,0 ε
1,0 εε,$ ε
accepts{0n1n|n ≥ 1}.
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.13
Example
The PDA
ε,ε $1q q2
q3q4
0,ε $
1,0 ε
1,0 εε,$ ε
accepts{0n1n|n ≥ 1}.
(does it also accept the empty string?)
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.13
Another Example
A PDA that accepts{
aibjck|i, j, k > 0 andi = j or i = k}
Informally:
read and pusha’s
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.14
Another Example
A PDA that accepts{
aibjck|i, j, k > 0 andi = j or i = k}
Informally:
read and pusha’s
either pop and match withb’s
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.14
Another Example
A PDA that accepts{
aibjck|i, j, k > 0 andi = j or i = k}
Informally:
read and pusha’s
either pop and match withb’s
or else pop and match withc’s
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.14
Another Example
A PDA that accepts{
aibjck|i, j, k > 0 andi = j or i = k}
Informally:
read and pusha’s
either pop and match withb’s
or else pop and match withc’s
anon-deterministicchoice!
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.14
Another Example (cont.)
ε,ε $
1q
q2
q3 q4ε,$ ε
q5 q0 q7ε,ε
ε
εb,a εc, ε
εa, a εb, ε c,a ε
ε,ε ε ε,ε ε ε,ε ε
A PDA that accepts{
aibjck|i, j, k > 0 andi = j or i = k}
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.15
Another Example (cont.)
A PDA that accepts{
aibjck|i, j, k > 0 andi = j or i = k}
Note: non-determinism is essential here!
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.16
Another Example (cont.)
A PDA that accepts{
aibjck|i, j, k > 0 andi = j or i = k}
Note: non-determinism is essential here!
Unlike finite automata, non-determinismdoes add power.
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.16
Another Example (cont.)
A PDA that accepts{
aibjck|i, j, k > 0 andi = j or i = k}
Note: non-determinism is essential here!
Unlike finite automata, non-determinismdoes add power.
Let us try to think how a proof could go,
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.16
Another Example (cont.)
A PDA that accepts{
aibjck|i, j, k > 0 andi = j or i = k}
Note: non-determinism is essential here!
Unlike finite automata, non-determinismdoes add power.
Let us try to think how a proof could go,...
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.16
Another Example (cont.)
A PDA that accepts{
aibjck|i, j, k > 0 andi = j or i = k}
Note: non-determinism is essential here!
Unlike finite automata, non-determinismdoes add power.
Let us try to think how a proof could go,...and realize it does not seem trivial or immediate.
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.16
NonDeterministm Essential for PDAsTheorem: LetM be a PDA that accepts
L = {xnyn|n ≥ 0} ∪{
xny2n | n ≥ 0}
.
ThenM is non-deterministic.Proof: (www.cs.may.ie/∼jpower/Courses/parsing/node38.html)
Suppose, by way ofcontradiction,M is deterministic.
Create two copies of this PDA, denotededM1
andM2.
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.17
NonDeterministm Essential for PDAsTheorem: LetM be a PDA that accepts
L = {xnyn|n ≥ 0} ∪{
xny2n | n ≥ 0}
.
ThenM is non-deterministic.Proof: (www.cs.may.ie/∼jpower/Courses/parsing/node38.html)
Suppose, by way ofcontradiction,M is deterministic.
Create two copies of this PDA, denotededM1
andM2.
Say two states“cousins”if they are copies of thesame state in the original PDA.
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.17
NonDeterministm Essential for PDAsTheorem: LetM be a PDA that accepts
L = {xnyn|n ≥ 0} ∪{
xny2n | n ≥ 0}
.
ThenM is non-deterministic.Proof: (www.cs.may.ie/∼jpower/Courses/parsing/node38.html)
Suppose, by way ofcontradiction,M is deterministic.
Create two copies of this PDA, denotededM1
andM2.
Say two states“cousins”if they are copies of thesame state in the original PDA.
We now modify these copies to make them intoone PDA,M0, over the alphabet{x, y, z}.
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.17
NonDeterministm Essential (cont.)
States of the newM0 are those ofM1 unionM2.
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.18
NonDeterministm Essential (cont.)
States of the newM0 are those ofM1 unionM2.
Start state of the newM0 is the start state ofM1.
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.18
NonDeterministm Essential (cont.)
States of the newM0 are those ofM1 unionM2.
Start state of the newM0 is the start state ofM1.
The accepting states of the newM0 are theaccepting states ofM2.
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.18
NonDeterministm Essential (cont.)
States of the newM0 are those ofM1 unionM2.
Start state of the newM0 is the start state ofM1.
The accepting states of the newM0 are theaccepting states ofM2.
For bothM1 andM2, add a newz transitionforevery existingy transition.
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.18
NonDeterministm Essential (cont.)
States of the newM0 are those ofM1 unionM2.
Start state of the newM0 is the start state ofM1.
The accepting states of the newM0 are theaccepting states ofM2.
For bothM1 andM2, add a newz transitionforevery existingy transition.
Now changeany transition(x, y, or z) originatingfrom an red accept state inM1 so it goes to its“cousin” destination inM2.
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.18
NonDeterministm Essential (cont.)
What languageM0 recognizes? Consider itsactions on an input of the formxkykzk for somefixedk.
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.19
NonDeterministm Essential (cont.)
What languageM0 recognizes? Consider itsactions on an input of the formxkykzk for somefixedk.
Initially M0 moves from the start state to anaccept state ofM1 while consumingxkyk.
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.19
NonDeterministm Essential (cont.)
What languageM0 recognizes? Consider itsactions on an input of the formxkykzk for somefixedk.
Initially M0 moves from the start state to anaccept state ofM1 while consumingxkyk.
Since no prefix ofxkyk is inL,M0 doesnotgothrough any accept state earlier.
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.19
NonDeterministm Essential (cont.)
What languageM0 recognizes? Consider itsactions on an input of the formxkykzk for somefixedk.
Initially M0 moves from the start state to anaccept state ofM1 while consumingxkyk.
Since no prefix ofxkyk is inL,M0 doesnotgothrough any accept state earlier.
BecauseM0 is deterministic, it could reach noother state while consumingxkyk.
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.19
NonDeterministm Essential (cont.)
What languageM0 recognizes? Consider itsactions on an input of the formxkykzk for somefixedk.
Initially M0 moves from the start state to anaccept state ofM1 while consumingxkyk.
Since no prefix ofxkyk is inL,M0 doesnotgothrough any accept state earlier.
BecauseM0 is deterministic, it could reach noother state while consumingxkyk.
BecauseM1 acceptsL, it would go on to acceptanotheryk block. Therefore, by construction,M0
will move onzk to its “M2” half, and accept.Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.19
Conclusion of ProofWe can conclude (after a few more arguments)thatM0 accepts the language{xnynzn|n ≥ 0}.Contradiction. ♣
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.20
Conclusion of ProofWe can conclude (after a few more arguments)thatM0 accepts the language{xnynzn|n ≥ 0}.Contradiction. ♣
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.20
Conclusion of ProofWe can conclude (after a few more arguments)thatM0 accepts the language{xnynzn|n ≥ 0}.Contradiction. ♣
Contradiction? What contradiction?What the $%&# does this contradict?
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.20
Conclusion of ProofWe can conclude (after a few more arguments)thatM0 accepts the language{xnynzn|n ≥ 0}.Contradiction. ♣
Contradiction? What contradiction?What the $%&# does this contradict?
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.20
Conclusion of ProofWe can conclude (after a few more arguments)thatM0 accepts the language{xnynzn|n ≥ 0}.Contradiction. ♣
Contradiction? What contradiction?What the $%&# does this contradict?
And while in the mode of thinking about theproof, where would such argument fail if theoriginalM werenon-deterministic?
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.20
And Yet Another PDA Example
A palindromehas the formwwR.
“Madam I’m Adam”
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.21
And Yet Another PDA Example
A palindromehas the formwwR.
“Madam I’m Adam”
“Dennis and Edna sinned”
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.21
And Yet Another PDA Example
A palindromehas the formwwR.
“Madam I’m Adam”
“Dennis and Edna sinned”
“Red rum, sir, is murder”
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.21
And Yet Another PDA Example
A palindromehas the formwwR.
“Madam I’m Adam”
“Dennis and Edna sinned”
“Red rum, sir, is murder”
“In girum imus nocte et consumimur igni”
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.21
And Yet Another PDA Example
A palindromehas the formwwR.
“Madam I’m Adam”
“Dennis and Edna sinned”
“Red rum, sir, is murder”
“In girum imus nocte et consumimur igni”
“νιψoν ανoµηµατα µη µoναν oψιν”
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.21
Yet Another Example (cont.)
This PDA
ε,ε $1q q2
q3q4 ε,$ ε
0,ε 0ε1, 1
ε,ε ε
ε1,1ε0,0
accepts binary palindromes.
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.22
PDA Languages
The Push-Down Automata Languages,LPDA, is theset of all languages that can be described by somePDA:
LPDA = {L : ∃ PDAM ∧ L[M ] = L}
We already knowLPDA ) LDFA (why)?
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.23
PDA Languages
The Push-Down Automata Languages,LPDA, is theset of all languages that can be described by somePDA:
LPDA = {L : ∃ PDAM ∧ L[M ] = L}
We already knowLPDA ) LDFA –since every DFA is just a PDA thatignores the stack.
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.24
PDA Languages
The Push-Down Automata Languages,LPDA, is theset of all languages that can be described by somePDA:
LPDA = {L : ∃ PDAM ∧ L[M ] = L}
We already knowLPDA ) LDFA –since every DFA is just a PDA thatignores the stack.
LCFG ⊆ LPDA ?
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.24
PDA Languages
The Push-Down Automata Languages,LPDA, is theset of all languages that can be described by somePDA:
LPDA = {L : ∃ PDAM ∧ L[M ] = L}
We already knowLPDA ) LDFA –since every DFA is just a PDA thatignores the stack.
LCFG ⊆ LPDA ?
LPDA ⊆ LCFG ?
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.24
Equivalence Theorem
Theorem: A language is context free if and only ifsome pushdown automaton accepts it.
This time, both the “if” part and the “only if” part arenon-trivial.
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.25
LCFG ⊆ LPDA
Given any CFGG, can we create a PDAM such thatL[M ] = L[G]?
Idea: We will first use the stack to derive theinput string, and then check to make sure ourderivation matches the input
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.26
If PartTheorem: If a language is context free, then somepushdown automaton accepts it.
LetL be a context-free language.
We knowL has a context-free grammarG.
on inputw, the PDAP should figure out if thereis a derivation ofw usingG.
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.27
If PartTheorem: If a language is context free, then somepushdown automaton accepts it.
LetL be a context-free language.
We knowL has a context-free grammarG.
on inputw, the PDAP should figure out if thereis a derivation ofw usingG.
Question: How doesP figure out which substitutionto make?
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.27
If PartTheorem: If a language is context free, then somepushdown automaton accepts it.
LetL be a context-free language.
We knowL has a context-free grammarG.
on inputw, the PDAP should figure out if thereis a derivation ofw usingG.
Question: How doesP figure out which substitutionto make?
Answer: It guesses.
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.27
CFL Implies PDA
Informally:
P pushes start variableS on stack
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.28
CFL Implies PDA
Informally:
P pushes start variableS on stack
keeps making substitutions
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.28
CFL Implies PDA
Informally:
P pushes start variableS on stack
keeps making substitutions
when only terminals remain . . .
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.28
CFL Implies PDA
Informally:
P pushes start variableS on stack
keeps making substitutions
when only terminals remain . . .
tests whetherderived stringequalsinput
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.28
CFL Implies PDA (cont.)
Where do we keep the intermediate string?
read unread
pop
push
A1A0$
011001
intermediate string: A1A001
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.29
CFL Implies PDA (cont.)
Where do we keep the intermediate string?
read unread
pop
push
A1A0$
011001
intermediate string: A1A001
can’t put it all on the stack
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.29
CFL Implies PDA (cont.)
Where do we keep the intermediate string?
read unread
pop
push
A1A0$
011001
intermediate string: A1A001
can’t put it all on the stack
only strings whose first letter is a variable arekept on stack
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.29
CFL Implies PDA (cont.)
Informal description:
pushS$ on stack
if top of stack is variableA, non-deterministicallyselect rule and substitute.
if top of stack is terminala, read next input andcompare. If they differ, reject.
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.30
CFL Implies PDA (cont.)
Need shorthand to push entire string onto stack.
(r, w) ∈ δ(q, a, s)
Easy to do by introducing intermediate states.
q
r
q
r
a,s xyz
a,s z
ε,ε y
ε,ε x
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.31
CFL Implies PDA (cont.)
States ofP are
start stateqsaccept stateqaloop stateqℓ
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.32
Transition FunctionInitialize stack
δ(qs, ε, ε) = {qℓ, S$}
Top of stack is variable
δ(qℓ, ε, A) = {(qℓ, w)| whereA→ w is a rule}
Top of stack is terminal
δ(qℓ, a, a) = {(qℓ, ε)}
End of Stack
δ(qℓ, ε, $) = {(qa, ε)}
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.33
Transition Function
ε,$ ε
qs
qa
ql
ε,ε S$
εa,aε,A w
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.34
Example
S → aTb|b
T → Ta|ε
qs
ql
ε,ε S$
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.35
Example
S → aTb|b
T → Ta|ε
Rules forS
qs
qa
ql
ε,ε S$Sε, b
Tε,ε
ε,ε a
Sε, b
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.36
Example
S → aTb|b
T → Ta|ε
Rules forT
qs
qa
ql
ε,ε S$Sε, b
Tε,ε
ε,ε a
ε, aT ε,ε a
Sε, bε,T ε
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.37
Example
S → aTb|b
T → Ta|ε
Rules for terminalsqs
qa
ql
ε,ε S$
Sε, b
Tε,ε
ε,ε a
ε, aT ε,ε a
Sε, bε,T ε
εaaεbb
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.38
Example
S → aTb|b
T → Ta|εTermination:
qs
qa
ql
ε,ε S$
Sε, b
Tε,ε
ε,ε a
ε, aT ε,ε a
Sε, bε,T ε
εaaεbbε, ε$
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.39
Only If Part
Theorem: If a PDA accepts a language,L, thenL iscontext-free.
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.40
Only If Part
Theorem: If a PDA accepts a language,L, thenL iscontext-free.
Given a pushdown automataP , we want to design acontext-free grammarG that “simulates” it:
The strings generated byG should be exactly those that
causeP to go from thestart stateto anaccepting state.
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.40
Only If Part
Theorem: If a PDA accepts a language,L, thenL iscontext-free.
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.41
Only If Part
Theorem: If a PDA accepts a language,L, thenL iscontext-free.
Proof: After constructing the grammarG , shouldprove it generates exactly the same language acceptedby the PDA.This is done by induction on the length of anycomputation ofP on any input stringx.
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.41
Only If Part
Theorem: If a PDA accepts a language,L, thenL iscontext-free.
Proof: After constructing the grammarG , shouldprove it generates exactly the same language acceptedby the PDA.This is done by induction on the length of anycomputation ofP on any input stringx.The induction argument is a bit lengthy and tedious,and we’ll skip it.
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.41
Only If Part
Theorem: If a PDA accepts a language,L, thenL iscontext-free.
Proof: After constructing the grammarG , shouldprove it generates exactly the same language acceptedby the PDA.This is done by induction on the length of anycomputation ofP on any input stringx.The induction argument is a bit lengthy and tedious,and we’ll skip it.
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.41
Only If Part
Theorem: If a PDA accepts a language,L, thenL iscontext-free.
Proof: After constructing the grammarG , shouldprove it generates exactly the same language acceptedby the PDA.This is done by induction on the length of anycomputation ofP on any input stringx.The induction argument is a bit lengthy and tedious,and we’ll skip it.
Diehards are welcome to consult pp. 106–114 inSipser’s book, and/or last year’s slides (fall 2003/4).
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.41
Corollary
Every regular language is context-free.
context freelanguages
regularlanguages
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.42
Non-Context-Free Languages
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.43
Non-Context-Free Languages
pumping lemmafor finite automata is our(almost) only tool for showing that languagesarenot regular.
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.43
Non-Context-Free Languages
pumping lemmafor finite automata is our(almost) only tool for showing that languagesarenot regular.
we will now do the same for context-freelanguages.
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.43
Non-Context-Free Languages
pumping lemmafor finite automata is our(almost) only tool for showing that languagesarenot regular.
we will now do the same for context-freelanguages.
it’s slightly more complicated . . .
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.43
Pumping Lemma for CFL
Also known as theuvxyz Theorem.
Theorem: If A is a CFL , there is anℓ (criticallength), such that ifs ∈ A and|s| ≥ ℓ, thens = uvxyz where
for everyi ≥ 0, uvixyiz ∈ A
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.44
Pumping Lemma for CFL
Also known as theuvxyz Theorem.
Theorem: If A is a CFL , there is anℓ (criticallength), such that ifs ∈ A and|s| ≥ ℓ, thens = uvxyz where
for everyi ≥ 0, uvixyiz ∈ A
|vy| > 0, (non-triviality)
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.44
Pumping Lemma for CFL
Also known as theuvxyz Theorem.
Theorem: If A is a CFL , there is anℓ (criticallength), such that ifs ∈ A and|s| ≥ ℓ, thens = uvxyz where
for everyi ≥ 0, uvixyiz ∈ A
|vy| > 0, (non-triviality)
|vxy| ≤ ℓ.
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.44
Basic IntuitionLetA be a CFL andG its CFG.
Let s be a “very long” string inA.
Thens has a “tall” parse tree.
And some root-to-leaf path must repeat a symbol.ahmmm,. . . why is that so?
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.45
Basic IntuitionLetA be a CFL andG its CFG.
Let s be a “very long” string inA.
Thens has a “tall” parse tree.
And some root-to-leaf path must repeat a symbol.ahmmm,. . . why is that so?
T
R
u v x y z
R
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.45
ProofletG be a CFG for CFLA.
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.46
ProofletG be a CFG for CFLA.
let c be the max number of symbols inright-hand-side of any rule.
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.46
ProofletG be a CFG for CFLA.
let c be the max number of symbols inright-hand-side of any rule.
no node in parse tree has> c children.
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.46
ProofletG be a CFG for CFLA.
let c be the max number of symbols inright-hand-side of any rule.
no node in parse tree has> c children.
at depthd, no more thancd leaves.
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.46
ProofletG be a CFG for CFLA.
let c be the max number of symbols inright-hand-side of any rule.
no node in parse tree has> c children.
at depthd, no more thancd leaves.
let |V | be the number of variables inG.
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.46
ProofletG be a CFG for CFLA.
let c be the max number of symbols inright-hand-side of any rule.
no node in parse tree has> c children.
at depthd, no more thancd leaves.
let |V | be the number of variables inG.
let ℓ = c|V |+2.
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.46
Proof (2)
let s be a string where|s| ≥ ℓ
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.47
Proof (2)
let s be a string where|s| ≥ ℓ
let τ be parse tree fors with fewest nodes
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.47
Proof (2)
let s be a string where|s| ≥ ℓ
let τ be parse tree fors with fewest nodes
τ has height≥ |V | + 2
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.47
Proof (2)
let s be a string where|s| ≥ ℓ
let τ be parse tree fors with fewest nodes
τ has height≥ |V | + 2
some path inτ has length≥ |V | + 2
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.47
Proof (2)
let s be a string where|s| ≥ ℓ
let τ be parse tree fors with fewest nodes
τ has height≥ |V | + 2
some path inτ has length≥ |V | + 2
that pathrepeatsa variableR
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.47
Proof (3)
Split s = uvxyz
T
R
u v x y z
R
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.48
Proof (3)
Split s = uvxyz
T
R
u v x y z
R
each occurrence ofR produces a string
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.48
Proof (3)
Split s = uvxyz
T
R
u v x y z
R
each occurrence ofR produces a string
upper produces stringvxy
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.48
Proof (3)
Split s = uvxyz
T
R
u v x y z
R
each occurrence ofR produces a string
upper produces stringvxy
lower produces stringxSlides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.48
ProofReplacing smaller by larger yieldsuvixyiz, for i > 0.
T
R
u v x y z
R
v yx
R
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.49
Proof (4)
Replacing larger by smaller yieldsuxz.
T
R
u
x
zTogether, they establish:
for i ≥ 0, uvixyiz ∈ A
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.50
Proof (5)
Next condition is:
|vy| > 0
If v andy are bothε, thenT
R
u
x
z
is a parse tree fors with fewer nodes.Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.51
Proof (6)
Final condition is|vxy| ≤ ℓ:T
R
u v x y z
R |V|+2
b |V|+2
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.52
Proof (6)
Final condition is|vxy| ≤ ℓ:T
R
u v x y z
R |V|+2
b |V|+2
the upper occurrence ofR generatesvxy.
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.52
Proof (6)
Final condition is|vxy| ≤ ℓ:T
R
u v x y z
R |V|+2
b |V|+2
the upper occurrence ofR generatesvxy.
can choose symbols such that both occurrences ofR lie in bottom|V | + 1 variables on path.
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.52
Proof (6)
Final condition is|vxy| ≤ ℓ:T
R
u v x y z
R |V|+2
b |V|+2
the upper occurrence ofR generatesvxy.
can choose symbols such that both occurrences ofR lie in bottom|V | + 1 variables on path.
subtree whereR generatesvxy is≤ |V | + 2 high.
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.52
Proof (6)
Final condition is|vxy| ≤ ℓ:T
R
u v x y z
R |V|+2
b |V|+2
the upper occurrence ofR generatesvxy.
can choose symbols such that both occurrences ofR lie in bottom|V | + 1 variables on path.
subtree whereR generatesvxy is≤ |V | + 2 high.
strings by subtree at mostb|V |+2 = ℓ long.Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.52
Example
Theorem: B = {anbncn} is not a CFL.Proof: By contradiction.
Let ℓ be the critical length.
Considers = aℓbℓcℓ.If s = uvxyz, neitherv nory can contain
botha’s andb’s, or
bothb’s andc’s, because
uv2xy2z would have out-of-order symbols.But if v andy contain only one symbol, thenuv2xy2zhas an imbalance!
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.53
Example (2)
The languageC ={
aibjck|0 ≤ i ≤ j ≤ k}
is notcontext-free.Let ℓ be the critical length, ands = aℓbℓcℓ.
Let s = uvxyz
neitherv nory contains contains two distinct symbols,
becauseuv2xy2z would have out-of-order symbols.
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.54
Example (2)
The languageC ={
aibjck|0 ≤ i ≤ j ≤ k}
is notcontext-free.Let ℓ be the critical length, ands = aℓbℓcℓ.
Let s = uvxyz
neitherv nory contains contains two distinct symbols,
becauseuv2xy2z would have out-of-order symbols.
vxy not all b’s (why?)
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.54
Example (2)
The languageC ={
aibjck|0 ≤ i ≤ j ≤ k}
is notcontext-free.Let ℓ be the critical length, ands = aℓbℓcℓ.
Let s = uvxyz
neitherv nory contains contains two distinct symbols,
becauseuv2xy2z would have out-of-order symbols.
vxy not all b’s (why?)
vxy < ℓ, so either
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.54
Example (2)
The languageC ={
aibjck|0 ≤ i ≤ j ≤ k}
is notcontext-free.Let ℓ be the critical length, ands = aℓbℓcℓ.
Let s = uvxyz
neitherv nory contains contains two distinct symbols,
becauseuv2xy2z would have out-of-order symbols.
vxy not all b’s (why?)
vxy < ℓ, so either
v contains onlya’s andy contains onlyb’s, but then
uv2xy2z has too fewc’s.
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.54
Example (2)
The languageC ={
aibjck|0 ≤ i ≤ j ≤ k}
is notcontext-free.Let ℓ be the critical length, ands = aℓbℓcℓ.
Let s = uvxyz
neitherv nory contains contains two distinct symbols,
becauseuv2xy2z would have out-of-order symbols.
vxy not all b’s (why?)
vxy < ℓ, so either
v contains onlya’s andy contains onlyb’s, but then
uv2xy2z has too fewc’s.
v contains onlyb’s andy contains onlyc’s. but then
uv0xy0z has too manya’s.Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.54
Example (3)
The languageD = {ww|w ∈ {0, 1}∗} is notcontext-free.Let s = 0ℓ1ℓ0ℓ1ℓ As before, supposes = uvxyz .
Recall that|vxy| ≤ ℓ.
if vxy is in the first half ofs, uv2xy2z moves a 1 into the
first position in second half.
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.55
Example (3)
The languageD = {ww|w ∈ {0, 1}∗} is notcontext-free.Let s = 0ℓ1ℓ0ℓ1ℓ As before, supposes = uvxyz .
Recall that|vxy| ≤ ℓ.
if vxy is in the first half ofs, uv2xy2z moves a 1 into the
first position in second half.
if vxy is in the second half,uv2xy2z moves a 0 into the last
position in first half.
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.55
Example (3)
The languageD = {ww|w ∈ {0, 1}∗} is notcontext-free.Let s = 0ℓ1ℓ0ℓ1ℓ As before, supposes = uvxyz .
Recall that|vxy| ≤ ℓ.
if vxy is in the first half ofs, uv2xy2z moves a 1 into the
first position in second half.
if vxy is in the second half,uv2xy2z moves a 0 into the last
position in first half.
if vxy straddles the midpoint, then pumpingdown to uxz
yields0ℓ1i0j1ℓ wherei andj cannot both beℓ.
Slides modified by Benny Chor, based on original slides by David Galles, Univ. of San Francisco, and Maurice Herlihy, Brown Univ. – p.55