BU CS 332 Theory of Computation€¦ · BU CS 332 –Theory of Computation Lecture 7: •More on...

28
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

Transcript of BU CS 332 Theory of Computation€¦ · BU CS 332 –Theory of Computation Lecture 7: •More on...

Page 1: BU CS 332 Theory of Computation€¦ · 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

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

Page 2: BU CS 332 Theory of Computation€¦ · 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

Page 3: BU CS 332 Theory of Computation€¦ · 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

Example Grammar 𝐺 Parse Tree

𝑆 → 0𝑆1𝐴 | 1𝑆0𝐴 | 휀

𝐴 → # | 휀

Derivation

2/11/2020 CS332 - Theory of Computation 3

Page 4: BU CS 332 Theory of Computation€¦ · 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 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?

Page 5: BU CS 332 Theory of Computation€¦ · 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

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:

Page 6: BU CS 332 Theory of Computation€¦ · 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

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

Page 7: BU CS 332 Theory of Computation€¦ · 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

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

Page 8: BU CS 332 Theory of Computation€¦ · 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

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

Page 9: BU CS 332 Theory of Computation€¦ · 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

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

Page 10: BU CS 332 Theory of Computation€¦ · 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

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

Page 11: BU CS 332 Theory of Computation€¦ · 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

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

Page 12: BU CS 332 Theory of Computation€¦ · 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 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?

Page 13: BU CS 332 Theory of Computation€¦ · 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

Pushdown Automata

2/11/2020 CS332 - Theory of Computation 13

Page 14: BU CS 332 Theory of Computation€¦ · 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

2/11/2020 CS332 - Theory of Computation 14

Regular Expressions : Finite Automata ::

Context-Free Languages : ???

Page 15: BU CS 332 Theory of Computation€¦ · 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

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

Page 16: BU CS 332 Theory of Computation€¦ · 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

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:

𝑎, 𝑥 → 𝑥′𝑝 𝑞

Page 17: BU CS 332 Theory of Computation€¦ · 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

Example: Even Palindromes

𝐿 = {𝑤𝑤𝑅|𝑤 ∈ 0,1 ∗}

2/11/2020 CS332 - Theory of Computation 17

Input 𝑎 𝑏 𝑏 𝑏 𝑏 𝑎

Finite control

Memory: Infinite Stack

Page 18: BU CS 332 Theory of Computation€¦ · 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

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 $

Page 19: BU CS 332 Theory of Computation€¦ · 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

Example: Even Palindromes

2/11/2020 CS332 - Theory of Computation 19

휀, 휀 → $ 𝑎, 휀 → 𝑎

휀, 휀 → 휀

𝑎, 𝑎 → 휀휀, $ → 휀

𝑞0 𝑝

𝑞𝑞𝑓

𝑏, 휀 → 𝑏

𝑏, 𝑏 → 휀

Page 20: BU CS 332 Theory of Computation€¦ · 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

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 𝑤.

Page 21: BU CS 332 Theory of Computation€¦ · 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

Example: Even Palindromes

2/12/2020 CS332 - Theory of Computation 21

휀, 휀 → $ 𝑎, 휀 → 𝑎

휀, 휀 → 휀

𝑎, 𝑎 → 휀휀, $ → 휀

𝑞0 𝑝

𝑞𝑞𝑓

𝑏, 휀 → 𝑏

𝑏, 𝑏 → 휀

𝑄 =Σ =Γ =𝐹 =

𝛿 : 𝑄 × Σ𝜀 × Γ𝜀 → 𝑃 𝑄 × Γ𝜀

𝛿 𝑝, 𝑏, 휀 =𝛿 𝑞, 𝑎, 𝑎 =𝛿 𝑞, 𝑎, 𝑏 =

Page 22: BU CS 332 Theory of Computation€¦ · 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

Example𝐿 = {𝑤|𝑤 has an equal number of 𝑎’s and 𝑏’s}

2/11/2020 CS332 - Theory of Computation 22

Page 23: BU CS 332 Theory of Computation€¦ · 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

Example𝐿 = {𝑤|𝑤 has an equal number of 𝑎’s and 𝑏’s}

2/11/2020 CS332 - Theory of Computation 23

Page 24: BU CS 332 Theory of Computation€¦ · 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

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

Page 25: BU CS 332 Theory of Computation€¦ · 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 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?

Page 26: BU CS 332 Theory of Computation€¦ · 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

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

Page 27: BU CS 332 Theory of Computation€¦ · 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

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

Page 28: BU CS 332 Theory of Computation€¦ · 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

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