Top Banner
BU CS 332 – Theory of Computation Lecture 8: Equivalence between PDAs and CFGs Closure Properties Reading: Sipser Ch 2.2 Mark Bun February 18, 2020
27

BU CS 332 –Theory of Computation · 2020-02-18 · BU CS 332 –Theory of Computation Lecture 8: • Equivalence between PDAs and CFGs • Closure Properties Reading: Sipser Ch

Jun 01, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: BU CS 332 –Theory of Computation · 2020-02-18 · BU CS 332 –Theory of Computation Lecture 8: • Equivalence between PDAs and CFGs • Closure Properties Reading: Sipser Ch

BU CS 332 – Theory of Computation

Lecture 8:• Equivalence between PDAs and CFGs• Closure Properties

Reading:Sipser Ch 2.2

Mark BunFebruary 18, 2020

Page 2: BU CS 332 –Theory of Computation · 2020-02-18 · BU CS 332 –Theory of Computation Lecture 8: • Equivalence between PDAs and CFGs • Closure Properties Reading: Sipser Ch

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:𝑎, 𝑥 → 𝑥′

Page 3: BU CS 332 –Theory of Computation · 2020-02-18 · BU CS 332 –Theory of Computation Lecture 8: • Equivalence between PDAs and CFGs • Closure Properties Reading: Sipser Ch

Example: Even Palindromes

2/18/2020 CS332 ‐ Theory of Computation 3

𝜀, 𝜀 → $ 𝑎, 𝜀 → 𝑎

𝜀, 𝜀 → 𝜀

𝑎,𝑎 → 𝜀𝜀, $ → 𝜀

0

𝑓

𝑏, 𝜀 → 𝑏

𝑏, 𝑏 → 𝜀

Page 4: BU CS 332 –Theory of Computation · 2020-02-18 · BU CS 332 –Theory of Computation Lecture 8: • Equivalence between PDAs and CFGs • Closure Properties Reading: Sipser Ch

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  .

Page 5: BU CS 332 –Theory of Computation · 2020-02-18 · BU CS 332 –Theory of Computation Lecture 8: • Equivalence between PDAs and CFGs • Closure Properties Reading: Sipser Ch

Examplehas an equal number of  ’s and  ’s

Algorithmic description

2/18/2020 CS332 ‐ Theory of Computation 5

Page 6: BU CS 332 –Theory of Computation · 2020-02-18 · BU CS 332 –Theory of Computation Lecture 8: • Equivalence between PDAs and CFGs • Closure Properties Reading: Sipser Ch

Examplehas an equal number of  ’s and  ’s

State diagram

2/18/2020 CS332 ‐ Theory of Computation 6

Page 7: BU CS 332 –Theory of Computation · 2020-02-18 · BU CS 332 –Theory of Computation Lecture 8: • Equivalence between PDAs and CFGs • Closure Properties Reading: Sipser Ch

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

Page 8: BU CS 332 –Theory of Computation · 2020-02-18 · BU CS 332 –Theory of Computation Lecture 8: • Equivalence between PDAs and CFGs • Closure Properties Reading: Sipser Ch

CFG ‐> PDA

2/18/2020 CS332 ‐ Theory of Computation 8

Page 9: BU CS 332 –Theory of Computation · 2020-02-18 · BU CS 332 –Theory of Computation Lecture 8: • Equivalence between PDAs and CFGs • Closure Properties Reading: Sipser Ch

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

Page 10: BU CS 332 –Theory of Computation · 2020-02-18 · BU CS 332 –Theory of Computation Lecture 8: • Equivalence between PDAs and CFGs • Closure Properties Reading: Sipser Ch

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

Page 11: BU CS 332 –Theory of Computation · 2020-02-18 · BU CS 332 –Theory of Computation Lecture 8: • Equivalence between PDAs and CFGs • Closure Properties Reading: Sipser Ch

State Diagram

2/18/2020 CS332 ‐ Theory of Computation 11

𝜀,𝐴 → 𝑢 [for every rule 𝐴 → 𝑢]𝜎,𝜎 → 𝜀 [for every terminal 𝐴 → 𝜎]

𝜀, 𝜀 → 𝑆$

𝜀, $ → 𝜀

0

𝑓

𝑙𝑜𝑜𝑝

Page 12: BU CS 332 –Theory of Computation · 2020-02-18 · BU CS 332 –Theory of Computation Lecture 8: • Equivalence between PDAs and CFGs • Closure Properties Reading: Sipser Ch

Example

2/18/2020 CS332 ‐ Theory of Computation 12

𝜀, 𝜀 → 𝑆$

𝜀, $ → 𝜀

0

𝑓

𝑙𝑜𝑜𝑝

Page 13: BU CS 332 –Theory of Computation · 2020-02-18 · BU CS 332 –Theory of Computation Lecture 8: • Equivalence between PDAs and CFGs • Closure Properties Reading: Sipser Ch

Example

2/18/2020 CS332 ‐ Theory of Computation 13

𝜀, $ → 𝜀

0

𝑓

𝑙𝑜𝑜𝑝

Page 14: BU CS 332 –Theory of Computation · 2020-02-18 · BU CS 332 –Theory of Computation Lecture 8: • Equivalence between PDAs and CFGs • Closure Properties Reading: Sipser Ch

PDA ‐> CFG

2/18/2020 CS332 ‐ Theory of Computation 14

Page 15: BU CS 332 –Theory of Computation · 2020-02-18 · BU CS 332 –Theory of Computation Lecture 8: • Equivalence between PDAs and CFGs • Closure Properties Reading: Sipser Ch

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

Page 16: BU CS 332 –Theory of Computation · 2020-02-18 · BU CS 332 –Theory of Computation Lecture 8: • Equivalence between PDAs and CFGs • Closure Properties Reading: Sipser Ch

Simplification Example

2/18/2020 CS332 ‐ Theory of Computation 16

𝜀, 𝜀 → $ 𝑎, 𝜀 → 𝑎

𝜀, 𝜀 → 𝜀

𝑎,𝑎 → 𝜀𝜀, $ → 𝜀

0

𝑓

𝑏, 𝜀 → 𝑏

𝑏, 𝑏 → 𝜀

Page 17: BU CS 332 –Theory of Computation · 2020-02-18 · BU CS 332 –Theory of Computation Lecture 8: • Equivalence between PDAs and CFGs • Closure Properties Reading: Sipser Ch

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

Page 18: BU CS 332 –Theory of Computation · 2020-02-18 · BU CS 332 –Theory of Computation Lecture 8: • Equivalence between PDAs and CFGs • Closure Properties Reading: Sipser Ch

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

𝜀, # → 𝜀

Page 19: BU CS 332 –Theory of Computation · 2020-02-18 · BU CS 332 –Theory of Computation Lecture 8: • Equivalence between PDAs and CFGs • Closure Properties Reading: Sipser Ch

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

Page 20: BU CS 332 –Theory of Computation · 2020-02-18 · BU CS 332 –Theory of Computation Lecture 8: • Equivalence between PDAs and CFGs • Closure Properties Reading: Sipser Ch

1. Stack first empties after reading all of 

2/18/2020 CS332 ‐ Theory of Computation 20

stackheight

inputstring

Add rule        

Page 21: BU CS 332 –Theory of Computation · 2020-02-18 · BU CS 332 –Theory of Computation Lecture 8: • Equivalence between PDAs and CFGs • Closure Properties Reading: Sipser Ch

2. Stack empties before reaching the end of 

2/18/2020 CS332 ‐ Theory of Computation 21

stackheight

inputstring

Add rule        

Page 22: BU CS 332 –Theory of Computation · 2020-02-18 · BU CS 332 –Theory of Computation Lecture 8: • Equivalence between PDAs and CFGs • Closure Properties Reading: Sipser Ch

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

Page 23: BU CS 332 –Theory of Computation · 2020-02-18 · BU CS 332 –Theory of Computation Lecture 8: • Equivalence between PDAs and CFGs • Closure Properties Reading: Sipser Ch

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

Page 24: BU CS 332 –Theory of Computation · 2020-02-18 · BU CS 332 –Theory of Computation Lecture 8: • Equivalence between PDAs and CFGs • Closure Properties Reading: Sipser Ch

Closure Properties

2/18/2020 CS332 ‐ Theory of Computation 24

Page 25: BU CS 332 –Theory of Computation · 2020-02-18 · BU CS 332 –Theory of Computation Lecture 8: • Equivalence between PDAs and CFGs • Closure Properties Reading: Sipser Ch

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

Page 26: BU CS 332 –Theory of Computation · 2020-02-18 · BU CS 332 –Theory of Computation Lecture 8: • Equivalence between PDAs and CFGs • Closure Properties Reading: Sipser Ch

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

Page 27: BU CS 332 –Theory of Computation · 2020-02-18 · BU CS 332 –Theory of Computation Lecture 8: • Equivalence between PDAs and CFGs • Closure Properties Reading: Sipser Ch

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