BU CS 332 – Theory of Computation
Lecture 7:
• More on CFGs
• Pushdown Automata
Reading:
Sipser Ch 2.1-2.3
Mark Bun
February 12, 2020
Context-Free Grammar (Formal)
A CFG is a 4-tuple 𝐺 = 𝑉, Σ, 𝑅, 𝑆
• 𝑉 is a finite set of variables
• Σ is a finite set of terminal symbols (disjoint from 𝑉)
• 𝑅 is a finite set of production rules of the form 𝐴 → 𝑤, where 𝐴 ∈ 𝑉 and 𝑤 ∈ (𝑉 ∪ Σ)∗
• 𝑆 ∈ 𝑉 is the start symbol
2/11/2020 CS332 - Theory of Computation 2
Context-Free Grammar
Example Grammar 𝐺 Parse Tree
𝑆 → 0𝑆1𝐴 | 1𝑆0𝐴 | 휀
𝐴 → # | 휀
Derivation
2/11/2020 CS332 - Theory of Computation 3
Context-Free Languages
2/11/2020 CS332 - Theory of Computation 4
𝐿 is a context-free language if it is the
language of some CFG
Questions about CFLs
1. Which languages are notcontext-free?
2. How do we recognize whether 𝑤 ∈ 𝐿?
3. What are the closure properties of CFLs?
Pumping Lemma for context-free languages
2/11/2020 CS332 - Theory of Computation 5
Let 𝐿 be a context-free language.
Then there exists a “pumping length” 𝑝 such that
1. |𝑣𝑦| > 0
2. |𝑣𝑥𝑦| ≤ 𝑝
3. 𝑢𝑣𝑖𝑥𝑦𝑖𝑧 𝐿 for all 𝑖 ≥ 0
For every 𝑤 ∈ 𝐿 where |𝑤| ≥ 𝑝,𝑤 can be split into five parts 𝑤 = 𝑢𝑣𝑥𝑦𝑧 where:
Pumping Lemma example
2/11/2020 CS332 - Theory of Computation 6
Claim: 𝐿 = 𝑎𝑛𝑏𝑛𝑐𝑛 𝑛 ≥ 0} is not context-free
Proof: Assume 𝐿 is context-free with pumping length 𝑝
1. Find 𝑤 ∈ 𝐿 with 𝑤 ≥ 𝑝2. Show that 𝑤 cannot be pumped
If 𝑤 = 𝑢𝑣𝑥𝑦𝑧 with |𝑣𝑦| > 0,|𝑣𝑥𝑦| ≤ 𝑝, then…
Case 1: 𝑣, 𝑦 both contain only one kind of symbol
Case 2: Either 𝑣 or 𝑦 contains two kinds of symbols
Pumping Lemma example
2/11/2020 CS332 - Theory of Computation 7
Claim: 𝐿 = 𝑎𝑛𝑏𝑛𝑐𝑛 𝑛 ≥ 0} is not context-free
Proof: Assume 𝐿 is context-free with pumping length 𝑝
1. Find 𝑤 ∈ 𝐿 with 𝑤 ≥ 𝑝2. Show that 𝑤 cannot be pumped
If 𝑤 = 𝑢𝑣𝑥𝑦𝑧 with |𝑣𝑦| > 0,|𝑣𝑥𝑦| ≤ 𝑝, then…
Case 1: 𝑣, 𝑦 both contain only one kind of symbol
Pumping Lemma example
2/11/2020 CS332 - Theory of Computation 8
Claim: 𝐿 = 𝑎𝑛𝑏𝑛𝑐𝑛 𝑛 ≥ 0} is not context-free
Proof: Assume 𝐿 is context-free with pumping length 𝑝
1. Find 𝑤 ∈ 𝐿 with 𝑤 ≥ 𝑝2. Show that 𝑤 cannot be pumped
If 𝑤 = 𝑢𝑣𝑥𝑦𝑧 with |𝑣𝑦| > 0,|𝑣𝑥𝑦| ≤ 𝑝, then…
Case 2: Either 𝑣 or 𝑦 contains two kinds of symbols
Pumping Lemma: Proof idea
Let 𝐿 be a context-free language. If 𝑤 ∈ 𝐿 is long enough, then every parse tree for 𝑤 has a repeated variable.
2/11/2020 CS332 - Theory of Computation 9
Pumping Lemma Proof
What does “long enough” mean? (How do we choose the pumping length 𝑝?)
• Let 𝐺 be a CFG for 𝐿
• Suppose the right-hand side of every rule in 𝐺 uses at most 𝑏 symbols
• Let 𝑝 = 𝑏 𝑉 +1
Claim: If 𝑤 ∈ 𝐿 with 𝑤 ≥ 𝑝, then the smallest parse tree for 𝑤 has height at least 𝑉 + 1
2/11/2020 CS332 - Theory of Computation 10
Pumping Lemma Proof
Claim: If 𝑤 ∈ 𝐿 with 𝑤 ≥ 𝑝, then the smallest parse tree for 𝑤 has height at least 𝑉 + 1
• By the pigeonhole principle, there is a path down the parse tree with a repeated variable 𝑅
• Choose two such occurrences within the bottom 𝑉 + 1 levels
2/12/2020 CS332 - Theory of Computation 11
Context-Free Languages
2/11/2020 CS332 - Theory of Computation 12
𝐿 is a context-free language if it is the
language of some CFG
Questions about CFLs
1. Which languages are notcontext-free?
2. How do we recognize whether 𝑤 ∈ 𝐿?
3. What are the closure properties of CFLs?
Pushdown Automata
2/11/2020 CS332 - Theory of Computation 13
2/11/2020 CS332 - Theory of Computation 14
Regular Expressions : Finite Automata ::
Context-Free Languages : ???
Pushdown Automata
2/11/2020 CS332 - Theory of Computation 15
Input 𝑎 𝑏 𝑎 𝑎
Finite control
…
Input 𝑎 𝑏 𝑎 𝑎
Finite control
…
𝑥
𝑥
𝑦 Memory: Infinite Stack
Pushdown Automata (PDAs):Machine with unbounded structured memory in the form of a stack
Finite Automata (FAs):Machine with a finiteamount of unstructured memory
Pushdown Automaton (the idea)
• Nondeterministic finite automaton + stack
• Stack has unlimited size, but machine can only manipulate (push, pop, read) symbol at the top
2/11/2020 CS332 - Theory of Computation 16
Input 𝑎 𝑏 𝑎 𝑎
Finite control
…
𝑥
𝑥
𝑦 Memory: Infinite Stack
Transitions of the form:
𝑎, 𝑥 → 𝑥′𝑝 𝑞
Example: Even Palindromes
𝐿 = {𝑤𝑤𝑅|𝑤 ∈ 0,1 ∗}
2/11/2020 CS332 - Theory of Computation 17
Input 𝑎 𝑏 𝑏 𝑏 𝑏 𝑎
Finite control
…
Memory: Infinite Stack
Example: Even Palindromes
𝐿 = {𝑤𝑤𝑅|𝑤 ∈ 0,1 ∗}
2/11/2020 CS332 - Theory of Computation 18
Input 𝑎 𝑏 𝑏 𝑏 𝑏 𝑎
Finite control
…
Memory: Infinite Stack
Algorithmic Description1. Place the marker $ on the stack2. Nondeterministically, either
a) Read a character and push it to the stack, orb) Go to the next step
3. Nondeterministically, eithera) Pop the stack if it matches the next character orb) Go to the next step
4. Accept if the top of the stack is $
Example: Even Palindromes
2/11/2020 CS332 - Theory of Computation 19
휀, 휀 → $ 𝑎, 휀 → 𝑎
휀, 휀 → 휀
𝑎, 𝑎 → 휀휀, $ → 휀
𝑞0 𝑝
𝑞𝑞𝑓
𝑏, 휀 → 𝑏
𝑏, 𝑏 → 휀
Pushdown Automaton (formal)
A PDA is a 6-tuple (sorry) 𝑀 = (𝑄, Σ, Γ, 𝛿, 𝑞0, 𝐹)
• 𝑄 is a finite set of states
• Σ is the input alphabet
• Γ is the stack alphabet
• 𝛿 : 𝑄 × Σ𝜀 × Γ𝜀 → 𝑃(𝑄 × Γ𝜀) is the transition function
• 𝑞0 is the start state
• 𝐹 is the set of final states
2/11/2020 CS332 - Theory of Computation 20
𝑀 accepts a string 𝑤 if, starting from 𝑞0 and an empty stack, there exists a path to an accept state that can be followed by reading all of 𝑤.
Example: Even Palindromes
2/12/2020 CS332 - Theory of Computation 21
휀, 휀 → $ 𝑎, 휀 → 𝑎
휀, 휀 → 휀
𝑎, 𝑎 → 휀휀, $ → 휀
𝑞0 𝑝
𝑞𝑞𝑓
𝑏, 휀 → 𝑏
𝑏, 𝑏 → 휀
𝑄 =Σ =Γ =𝐹 =
𝛿 : 𝑄 × Σ𝜀 × Γ𝜀 → 𝑃 𝑄 × Γ𝜀
𝛿 𝑝, 𝑏, 휀 =𝛿 𝑞, 𝑎, 𝑎 =𝛿 𝑞, 𝑎, 𝑏 =
Example𝐿 = {𝑤|𝑤 has an equal number of 𝑎’s and 𝑏’s}
2/11/2020 CS332 - Theory of Computation 22
Example𝐿 = {𝑤|𝑤 has an equal number of 𝑎’s and 𝑏’s}
2/11/2020 CS332 - Theory of Computation 23
CFGs vs. PDAs
The language 𝐿(𝑀) of a PDA 𝑀 is the set of all strings it accepts.
Theorem (next time): The class of languages recognized by PDAs is exactly the context-free languages.
2/11/2020 CS332 - Theory of Computation 24
Context-Free Languages
2/11/2020 CS332 - Theory of Computation 25
𝐿 is a context-free language if it is the
language of some CFG
Questions about CFLs
1. Which languages are notcontext-free?
2. How do we recognize whether 𝑤 ∈ 𝐿?
3. What are the closure properties of CFLs?
Closure Properties
• The class of CFLs is closed under the regular operations union, concatenation, star
• Beware: It is not closed under intersection or complement(Find a counterexample!)
2/11/2020 CS332 - Theory of Computation 26
Closure under union (Proof 1)
Let 𝐴 be a CFL recognized by PDA 𝑀𝐴 and let 𝐵 be a CFL recognized by PDA 𝑀𝐵
Goal: Construct a PDA recognizing 𝐴 ∪ 𝐵
2/11/2020 CS332 - Theory of Computation 27
Closure under union (Proof 2)
Let 𝐴 be a CFL generated by CFG 𝐺𝐴 and let 𝐵 be a CFL recognized by CFG 𝐺𝐵Goal: Construct a CFG 𝐺 recognizing 𝐴 ∪ 𝐵
𝐺𝐴 = (𝑉𝐴, Σ𝐴, 𝑅𝐴, 𝑆𝐴)
𝐺𝐵 = (𝑉𝐵 , Σ𝐵 , 𝑅𝐵 , 𝑆𝐵)
Relabel variables so 𝑉𝐴 and 𝑉𝐵 are disjoint
Let 𝐺 = (𝑉, Σ , 𝑅 , 𝑆 )
2/12/2020 CS332 - Theory of Computation 28