BU CS 332 –Theory of Computation · 2020-02-18 · BU CS 332 –Theory of Computation Lecture 8:...
Transcript of BU CS 332 –Theory of Computation · 2020-02-18 · BU CS 332 –Theory of Computation Lecture 8:...
BU CS 332 – Theory of Computation
Lecture 8:• Equivalence between PDAs and CFGs• Closure Properties
Reading:Sipser Ch 2.2
Mark BunFebruary 18, 2020
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/18/2020 CS332 ‐ Theory of Computation 2
Input 𝑎 𝑏 𝑎 𝑎
Finite control
…
𝑥𝑥𝑦 Memory: Infinite Stack
Transitions of the form:𝑎, 𝑥 → 𝑥′
Example: Even Palindromes
2/18/2020 CS332 ‐ Theory of Computation 3
𝜀, 𝜀 → $ 𝑎, 𝜀 → 𝑎
𝜀, 𝜀 → 𝜀
𝑎,𝑎 → 𝜀𝜀, $ → 𝜀
0
𝑓
𝑏, 𝜀 → 𝑏
𝑏, 𝑏 → 𝜀
Pushdown Automaton (formal)A PDA is a 6‐tuple • is a finite set of states• is the input alphabet• is the stack alphabet• : is the transition function• is the start state• is the set of final states
2/18/2020 CS332 ‐ Theory of Computation 4
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 .
Examplehas an equal number of ’s and ’s
Algorithmic description
2/18/2020 CS332 ‐ Theory of Computation 5
Examplehas an equal number of ’s and ’s
State diagram
2/18/2020 CS332 ‐ Theory of Computation 6
CFGs vs. PDAsThe language of a PDA is the set of all strings it accepts.
Theorem: The class of languages recognized by PDAs is exactly the context‐free languages.
Theorem 1: Every CFG has an equivalent PDATheorem 2: Every PDA has an equivalent CFG
2/18/2020 CS332 ‐ Theory of Computation 7
CFG ‐> PDA
2/18/2020 CS332 ‐ Theory of Computation 8
CFG ‐> PDA ConversionSuppose language is generated by CFG =
Goal: Construct a PDA recognizing
Idea: will guess the steps of the CFG derivation of its input , and use its stack to check the derivation
A helpful intermediate abstractionGeneralized PDA: Can push an entire string to the stack in one move
2/18/2020 CS332 ‐ Theory of Computation 9
Algorithmic Description1. Place and the start variable on the stack2. Repeat forever:
a) If the top of the stack holds variable :Nondeterministically guess a rule Replace with on the stack
b) If the top of the stack holds terminal :Pop and verify that it matches the next input char
c) If the top of the stack holds :Accept if the input is empty
2/18/2020 CS332 ‐ Theory of Computation 10
State Diagram
2/18/2020 CS332 ‐ Theory of Computation 11
𝜀,𝐴 → 𝑢 [for every rule 𝐴 → 𝑢]𝜎,𝜎 → 𝜀 [for every terminal 𝐴 → 𝜎]
𝜀, 𝜀 → 𝑆$
𝜀, $ → 𝜀
0
𝑓
𝑙𝑜𝑜𝑝
Example
2/18/2020 CS332 ‐ Theory of Computation 12
𝜀, 𝜀 → 𝑆$
𝜀, $ → 𝜀
0
𝑓
𝑙𝑜𝑜𝑝
Example
2/18/2020 CS332 ‐ Theory of Computation 13
𝜀, $ → 𝜀
0
𝑓
𝑙𝑜𝑜𝑝
PDA ‐> CFG
2/18/2020 CS332 ‐ Theory of Computation 14
PDA ‐> CFG ConversionTheorem 2: Every PDA has an equivalent CFGSuppose is recognized by PDAGoal: Construct a CFG = generating
First simplify so that:1. It has a single accept state 𝑓
2. It empties the stack before accepting3. Every transition either pushes a symbol or pops a
symbol (but not both)
2/18/2020 CS332 ‐ Theory of Computation 15
Simplification Example
2/18/2020 CS332 ‐ Theory of Computation 16
𝜀, 𝜀 → $ 𝑎, 𝜀 → 𝑎
𝜀, 𝜀 → 𝜀
𝑎,𝑎 → 𝜀𝜀, $ → 𝜀
0
𝑓
𝑏, 𝜀 → 𝑏
𝑏, 𝑏 → 𝜀
Conversion IdeaVariables: for every pair of states in PDA Idea: generates all strings that can take from (with an empty stack) to (with an empty stack)
2/18/2020 CS332 ‐ Theory of Computation 17
Example
2/18/2020 CS332 ‐ Theory of Computation 18
𝜀, 𝜀 → $ 𝑎, 𝜀 → 𝑎
𝜀, 𝜀 → #
𝑎,𝑎 → 𝜀𝜀, $ → 𝜀
0 𝑏, 𝜀 → 𝑏
𝑏, 𝑏 → 𝜀
𝜀, # → 𝜀
𝜀, 𝜀 → #
𝜀, 𝜀 → #
What strings should 𝐴 generate?
1
2
34
5
What strings should 𝐴 generate?
What strings should 𝐴 generate?
6
𝜀, # → 𝜀
What rules should define ?
Let be a string generated by
Two cases:1) Stack first empties after reading all of
2) Stack empties before reaching the end of
2/18/2020 CS332 ‐ Theory of Computation 19
stackheight
inputstring 𝑝 𝑞
stackheight
𝑝 𝑞inputstring
1. Stack first empties after reading all of
2/18/2020 CS332 ‐ Theory of Computation 20
stackheight
inputstring
Add rule
2. Stack empties before reaching the end of
2/18/2020 CS332 ‐ Theory of Computation 21
stackheight
inputstring
Add rule
Formal CFG Construction
Three kinds of rules:1. For every :If and ,
include the rule
2. For every include the rule 3. For every include the rule
2/18/2020 CS332 ‐ Theory of Computation 22
Sketch of proof that CFG generates Claim: ∗ if and only if takes from to , beginning and ending with empty stackProof idea:
Induction on number of steps of derivation of from
Induction on number of steps of computation taking from to
2/18/2020 CS332 ‐ Theory of Computation 23
Closure Properties
2/18/2020 CS332 ‐ Theory of Computation 24
Closure Properties
• The class of CFLs is closed underUnionConcatenationStarIntersection with regular languages
• Beware: It is not closed under intersection or complement(Find counterexamples!)
2/18/2020 CS332 ‐ Theory of Computation 25
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/18/2020 CS332 ‐ Theory of Computation 26
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/18/2020 CS332 ‐ Theory of Computation 27