CS 301 Lecture 13 Closure properties of context-free languages - - PowerPoint PPT Presentation

cs 301
SMART_READER_LITE
LIVE PREVIEW

CS 301 Lecture 13 Closure properties of context-free languages - - PowerPoint PPT Presentation

CS 301 Lecture 13 Closure properties of context-free languages Stephen Checkoway March 5, 2018 1 / 18 CFLs and PDAs Theorem Every context-free language can be recognized by some PDA. Proof idea. 1 Use the PDAs stack to perform a


slide-1
SLIDE 1

CS 301

Lecture 13 – Closure properties of context-free languages Stephen Checkoway March 5, 2018

1 / 18

slide-2
SLIDE 2

CFLs and PDAs

Theorem

Every context-free language can be recognized by some PDA.

Proof idea.

1 Use the PDA’s stack to perform a left-most derivation of a word in the language 2 Match the PDA’s input symbols against the stack, popping each one 3 Accept if stack is empty when there’s no more input

2 / 18

slide-3
SLIDE 3

What we’d like to do

Consider the language A = {w ∣ w ∈ {a, b}∗ and w is not a palindrome} What CFG generates that language?

3 / 18

slide-4
SLIDE 4

What we’d like to do

Consider the language A = {w ∣ w ∈ {a, b}∗ and w is not a palindrome} What CFG generates that language? S → aSa ∣ bSb ∣ aTb ∣ bTa T → aT ∣ bT ∣ ε

3 / 18

slide-5
SLIDE 5

What we’d like to do

Consider the language A = {w ∣ w ∈ {a, b}∗ and w is not a palindrome} What CFG generates that language? S → aSa ∣ bSb ∣ aTb ∣ bTa T → aT ∣ bT ∣ ε A left-most derivation of the string abaaa is S ⇒ aSa ⇒ abTaa ⇒ abaTaa ⇒ abaaa. We want to start by pushing S on the stack, then performing the derivation step by step so that abaaa ends on the stack, and then match the input

3 / 18

slide-6
SLIDE 6

What we’d like to do

Consider the language A = {w ∣ w ∈ {a, b}∗ and w is not a palindrome} What CFG generates that language? S → aSa ∣ bSb ∣ aTb ∣ bTa T → aT ∣ bT ∣ ε A left-most derivation of the string abaaa is S ⇒ aSa ⇒ abTaa ⇒ abaTaa ⇒ abaaa. We want to start by pushing S on the stack, then performing the derivation step by step so that abaaa ends on the stack, and then match the input There are two complications

1 The first step in the derivation S ⇒ aSa requires popping one symbol and

pushing three

2 We can only replace symbols at the top of the stack

3 / 18

slide-7
SLIDE 7

Pushing multiple symbols

We would like to write a transition like q r ε, S → aTb but δ ∶ Q × Σε × Γε → P(Q × Γε) doesn’t allow that

4 / 18

slide-8
SLIDE 8

Pushing multiple symbols

We would like to write a transition like q r ε, S → aTb but δ ∶ Q × Σε × Γε → P(Q × Γε) doesn’t allow that Instead, use multiple transitions q r ε, S → b ε, ε → T ε, ε → a Note that the symbols are pushed on in reverse order

4 / 18

slide-9
SLIDE 9

We can only replace symbols at the top of the stack

Rather than first deriving the whole string on the stack and then matching the input,

  • If the top of the stack is a terminal, match it to the next input symbol

t, t → ε for each t ∈ Σ

  • If the top of the stack is a variable, replace it with the RHS of a corresponding rule

5 / 18

slide-10
SLIDE 10

We can only replace symbols at the top of the stack

Rather than first deriving the whole string on the stack and then matching the input,

  • If the top of the stack is a terminal, match it to the next input symbol

t, t → ε for each t ∈ Σ

  • If the top of the stack is a variable, replace it with the RHS of a corresponding rule

In fact, we only need four main states plus any additional states necessary to push mul- tiple symbols The qloop state is where all the real work happens q0 q1 qloop qa ε, ε → $ ε, ε → S ε, $ → ε ... ...

5 / 18

slide-11
SLIDE 11

Example

S → aSa ∣ bSb ∣ aTb ∣ bTa T → aT ∣ bT ∣ ε

1 For each t ∈ Σ, add the transition

t, t → ε from qloop to qloop

2 For each rule A → u1u2⋯un for

ui ∈ V ∪ Σ, add n − 1 new states (if n > 1) and transitions to pop A and push u1, u2, . . . , un on in reverse order q0 q1 qloop qa ε, ε → $ ε, ε → S ε, $ → ε a, a → ε b, b → ε ε, S → aSa ε, S → bSb ε, S → aTb ε, S → bTa ε, T → aT ε, T → bT ε, T → ε [The rules on the right need 10 extra states to make this a proper PDA]

6 / 18

slide-12
SLIDE 12

Running the PDA on some input

Consider running the PDA on the input

  • abaaa. The stack is shown on the right after

each step

1 push $;

$ q0 q1 qloop qa ε, ε → $ ε, ε → S ε, $ → ε a, a → ε b, b → ε ε, S → aSa ε, S → bSb ε, S → aTb ε, S → bTa ε, T → aT ε, T → bT ε, T → ε

7 / 18

slide-13
SLIDE 13

Running the PDA on some input

Consider running the PDA on the input

  • abaaa. The stack is shown on the right after

each step

1 push $;

$

2 push S;

S$ q0 q1 qloop qa ε, ε → $ ε, ε → S ε, $ → ε a, a → ε b, b → ε ε, S → aSa ε, S → bSb ε, S → aTb ε, S → bTa ε, T → aT ε, T → bT ε, T → ε

7 / 18

slide-14
SLIDE 14

Running the PDA on some input

Consider running the PDA on the input

  • abaaa. The stack is shown on the right after

each step

1 push $;

$

2 push S;

S$

3 pop S, push aSa;

aSa$ q0 q1 qloop qa ε, ε → $ ε, ε → S ε, $ → ε a, a → ε b, b → ε ε, S → aSa ε, S → bSb ε, S → aTb ε, S → bTa ε, T → aT ε, T → bT ε, T → ε

7 / 18

slide-15
SLIDE 15

Running the PDA on some input

Consider running the PDA on the input

  • abaaa. The stack is shown on the right after

each step

1 push $;

$

2 push S;

S$

3 pop S, push aSa;

aSa$

4 read and pop a;

Sa$ q0 q1 qloop qa ε, ε → $ ε, ε → S ε, $ → ε a, a → ε b, b → ε ε, S → aSa ε, S → bSb ε, S → aTb ε, S → bTa ε, T → aT ε, T → bT ε, T → ε

7 / 18

slide-16
SLIDE 16

Running the PDA on some input

Consider running the PDA on the input

  • abaaa. The stack is shown on the right after

each step

1 push $;

$

2 push S;

S$

3 pop S, push aSa;

aSa$

4 read and pop a;

Sa$

5 pop S, push bTa;

bTaa$ q0 q1 qloop qa ε, ε → $ ε, ε → S ε, $ → ε a, a → ε b, b → ε ε, S → aSa ε, S → bSb ε, S → aTb ε, S → bTa ε, T → aT ε, T → bT ε, T → ε

7 / 18

slide-17
SLIDE 17

Running the PDA on some input

Consider running the PDA on the input

  • abaaa. The stack is shown on the right after

each step

1 push $;

$

2 push S;

S$

3 pop S, push aSa;

aSa$

4 read and pop a;

Sa$

5 pop S, push bTa;

bTaa$

6 read and pop b;

Taa$ q0 q1 qloop qa ε, ε → $ ε, ε → S ε, $ → ε a, a → ε b, b → ε ε, S → aSa ε, S → bSb ε, S → aTb ε, S → bTa ε, T → aT ε, T → bT ε, T → ε

7 / 18

slide-18
SLIDE 18

Running the PDA on some input

Consider running the PDA on the input

  • abaaa. The stack is shown on the right after

each step

1 push $;

$

2 push S;

S$

3 pop S, push aSa;

aSa$

4 read and pop a;

Sa$

5 pop S, push bTa;

bTaa$

6 read and pop b;

Taa$

7 pop T, push aT;

aTaa$ q0 q1 qloop qa ε, ε → $ ε, ε → S ε, $ → ε a, a → ε b, b → ε ε, S → aSa ε, S → bSb ε, S → aTb ε, S → bTa ε, T → aT ε, T → bT ε, T → ε

7 / 18

slide-19
SLIDE 19

Running the PDA on some input

Consider running the PDA on the input

  • abaaa. The stack is shown on the right after

each step

1 push $;

$

2 push S;

S$

3 pop S, push aSa;

aSa$

4 read and pop a;

Sa$

5 pop S, push bTa;

bTaa$

6 read and pop b;

Taa$

7 pop T, push aT;

aTaa$

8 read and pop a;

Taa$ q0 q1 qloop qa ε, ε → $ ε, ε → S ε, $ → ε a, a → ε b, b → ε ε, S → aSa ε, S → bSb ε, S → aTb ε, S → bTa ε, T → aT ε, T → bT ε, T → ε

7 / 18

slide-20
SLIDE 20

Running the PDA on some input

Consider running the PDA on the input

  • abaaa. The stack is shown on the right after

each step

1 push $;

$

2 push S;

S$

3 pop S, push aSa;

aSa$

4 read and pop a;

Sa$

5 pop S, push bTa;

bTaa$

6 read and pop b;

Taa$

7 pop T, push aT;

aTaa$

8 read and pop a;

Taa$

9 pop T, push ε;

aa$ q0 q1 qloop qa ε, ε → $ ε, ε → S ε, $ → ε a, a → ε b, b → ε ε, S → aSa ε, S → bSb ε, S → aTb ε, S → bTa ε, T → aT ε, T → bT ε, T → ε

7 / 18

slide-21
SLIDE 21

Running the PDA on some input

Consider running the PDA on the input

  • abaaa. The stack is shown on the right after

each step

1 push $;

$

2 push S;

S$

3 pop S, push aSa;

aSa$

4 read and pop a;

Sa$

5 pop S, push bTa;

bTaa$

6 read and pop b;

Taa$

7 pop T, push aT;

aTaa$

8 read and pop a;

Taa$

9 pop T, push ε;

aa$

10 read and pop a;

a$ q0 q1 qloop qa ε, ε → $ ε, ε → S ε, $ → ε a, a → ε b, b → ε ε, S → aSa ε, S → bSb ε, S → aTb ε, S → bTa ε, T → aT ε, T → bT ε, T → ε

7 / 18

slide-22
SLIDE 22

Running the PDA on some input

Consider running the PDA on the input

  • abaaa. The stack is shown on the right after

each step

1 push $;

$

2 push S;

S$

3 pop S, push aSa;

aSa$

4 read and pop a;

Sa$

5 pop S, push bTa;

bTaa$

6 read and pop b;

Taa$

7 pop T, push aT;

aTaa$

8 read and pop a;

Taa$

9 pop T, push ε;

aa$

10 read and pop a;

a$

11 read and pop a;

$ q0 q1 qloop qa ε, ε → $ ε, ε → S ε, $ → ε a, a → ε b, b → ε ε, S → aSa ε, S → bSb ε, S → aTb ε, S → bTa ε, T → aT ε, T → bT ε, T → ε

7 / 18

slide-23
SLIDE 23

Running the PDA on some input

Consider running the PDA on the input

  • abaaa. The stack is shown on the right after

each step

1 push $;

$

2 push S;

S$

3 pop S, push aSa;

aSa$

4 read and pop a;

Sa$

5 pop S, push bTa;

bTaa$

6 read and pop b;

Taa$

7 pop T, push aT;

aTaa$

8 read and pop a;

Taa$

9 pop T, push ε;

aa$

10 read and pop a;

a$

11 read and pop a;

$

12 pop $ and accept;

ε q0 q1 qloop qa ε, ε → $ ε, ε → S ε, $ → ε a, a → ε b, b → ε ε, S → aSa ε, S → bSb ε, S → aTb ε, S → bTa ε, T → aT ε, T → bT ε, T → ε

7 / 18

slide-24
SLIDE 24

Proving that every CFL is recognized by a PDA

Proof.

Let A be a CFL generated by a CFG G = (V, Σ, R, S).

8 / 18

slide-25
SLIDE 25

Proving that every CFL is recognized by a PDA

Proof.

Let A be a CFL generated by a CFG G = (V, Σ, R, S). Construct the PDA M = (Q, Σ, Γ, δ, q0, {qa}) with states Q = {q0, q1, qloop, qa} ∪ E where E are the extra states we need for each rule and Γ = V ∪ Σ ∪ {$}.

8 / 18

slide-26
SLIDE 26

Proving that every CFL is recognized by a PDA

Proof.

Let A be a CFL generated by a CFG G = (V, Σ, R, S). Construct the PDA M = (Q, Σ, Γ, δ, q0, {qa}) with states Q = {q0, q1, qloop, qa} ∪ E where E are the extra states we need for each rule and Γ = V ∪ Σ ∪ {$}. Start with then transitions ε, ε → $ from q0 to q1, ε, ε → S from q1 to qloop, and ε, $ → ε from qloop to qa

8 / 18

slide-27
SLIDE 27

Proving that every CFL is recognized by a PDA

Proof.

Let A be a CFL generated by a CFG G = (V, Σ, R, S). Construct the PDA M = (Q, Σ, Γ, δ, q0, {qa}) with states Q = {q0, q1, qloop, qa} ∪ E where E are the extra states we need for each rule and Γ = V ∪ Σ ∪ {$}. Start with then transitions ε, ε → $ from q0 to q1, ε, ε → S from q1 to qloop, and ε, $ → ε from qloop to qa For each t ∈ Σ, add the transition t, t → ε from qloop to qloop.

8 / 18

slide-28
SLIDE 28

Proving that every CFL is recognized by a PDA

Proof.

Let A be a CFL generated by a CFG G = (V, Σ, R, S). Construct the PDA M = (Q, Σ, Γ, δ, q0, {qa}) with states Q = {q0, q1, qloop, qa} ∪ E where E are the extra states we need for each rule and Γ = V ∪ Σ ∪ {$}. Start with then transitions ε, ε → $ from q0 to q1, ε, ε → S from q1 to qloop, and ε, $ → ε from qloop to qa For each t ∈ Σ, add the transition t, t → ε from qloop to qloop. For each rule A → u add the states and transitions necessary to pop A and push u in reverse order from qloop to qloop.

8 / 18

slide-29
SLIDE 29

Proof continued

Consider running M on input w = w1w2⋯wn for wi ∈ Σ. The first time M enters state qloop, the stack is S$ and no input has been read. Every subsequent time it enters qloop, the input read so far concatenated with the stack is a step in some left-most derivation of w (followed by a $). I.e., if k symbols have been read from the input and the stack is s, then w1w2⋯wks is a step in the derivation of w

9 / 18

slide-30
SLIDE 30

Returning to the example

S ⇒ aSa ⇒ abTaa ⇒ abaTaa ⇒ abaaa State Action Input read Stack q0 push $ ε $ q0 q1 qloop qa ε, ε → $ ε, ε → S ε, $ → ε a, a → ε b, b → ε ε, S → aSa ε, S → bSb ε, S → aTb ε, S → bTa ε, T → aT ε, T → bT ε, T → ε

10 / 18

slide-31
SLIDE 31

Returning to the example

S ⇒ aSa ⇒ abTaa ⇒ abaTaa ⇒ abaaa State Action Input read Stack q0 push $ ε $ q1 push S ε S$ q0 q1 qloop qa ε, ε → $ ε, ε → S ε, $ → ε a, a → ε b, b → ε ε, S → aSa ε, S → bSb ε, S → aTb ε, S → bTa ε, T → aT ε, T → bT ε, T → ε

10 / 18

slide-32
SLIDE 32

Returning to the example

S ⇒ aSa ⇒ abTaa ⇒ abaTaa ⇒ abaaa State Action Input read Stack q0 push $ ε $ q1 push S ε S$ qloop pop S, push aSa ε aSa$ q0 q1 qloop qa ε, ε → $ ε, ε → S ε, $ → ε a, a → ε b, b → ε ε, S → aSa ε, S → bSb ε, S → aTb ε, S → bTa ε, T → aT ε, T → bT ε, T → ε

10 / 18

slide-33
SLIDE 33

Returning to the example

S ⇒ aSa ⇒ abTaa ⇒ abaTaa ⇒ abaaa State Action Input read Stack q0 push $ ε $ q1 push S ε S$ qloop pop S, push aSa ε aSa$ qloop read and pop a a Sa$ q0 q1 qloop qa ε, ε → $ ε, ε → S ε, $ → ε a, a → ε b, b → ε ε, S → aSa ε, S → bSb ε, S → aTb ε, S → bTa ε, T → aT ε, T → bT ε, T → ε

10 / 18

slide-34
SLIDE 34

Returning to the example

S ⇒ aSa ⇒ abTaa ⇒ abaTaa ⇒ abaaa State Action Input read Stack q0 push $ ε $ q1 push S ε S$ qloop pop S, push aSa ε aSa$ qloop read and pop a a Sa$ qloop pop S, push bTa a bTaa$ q0 q1 qloop qa ε, ε → $ ε, ε → S ε, $ → ε a, a → ε b, b → ε ε, S → aSa ε, S → bSb ε, S → aTb ε, S → bTa ε, T → aT ε, T → bT ε, T → ε

10 / 18

slide-35
SLIDE 35

Returning to the example

S ⇒ aSa ⇒ abTaa ⇒ abaTaa ⇒ abaaa State Action Input read Stack q0 push $ ε $ q1 push S ε S$ qloop pop S, push aSa ε aSa$ qloop read and pop a a Sa$ qloop pop S, push bTa a bTaa$ qloop read and pop b ab Taa$ q0 q1 qloop qa ε, ε → $ ε, ε → S ε, $ → ε a, a → ε b, b → ε ε, S → aSa ε, S → bSb ε, S → aTb ε, S → bTa ε, T → aT ε, T → bT ε, T → ε

10 / 18

slide-36
SLIDE 36

Returning to the example

S ⇒ aSa ⇒ abTaa ⇒ abaTaa ⇒ abaaa State Action Input read Stack q0 push $ ε $ q1 push S ε S$ qloop pop S, push aSa ε aSa$ qloop read and pop a a Sa$ qloop pop S, push bTa a bTaa$ qloop read and pop b ab Taa$ qloop pop T, push aT ab aTaa$ q0 q1 qloop qa ε, ε → $ ε, ε → S ε, $ → ε a, a → ε b, b → ε ε, S → aSa ε, S → bSb ε, S → aTb ε, S → bTa ε, T → aT ε, T → bT ε, T → ε

10 / 18

slide-37
SLIDE 37

Returning to the example

S ⇒ aSa ⇒ abTaa ⇒ abaTaa ⇒ abaaa State Action Input read Stack q0 push $ ε $ q1 push S ε S$ qloop pop S, push aSa ε aSa$ qloop read and pop a a Sa$ qloop pop S, push bTa a bTaa$ qloop read and pop b ab Taa$ qloop pop T, push aT ab aTaa$ qloop read and pop a aba Taa$ q0 q1 qloop qa ε, ε → $ ε, ε → S ε, $ → ε a, a → ε b, b → ε ε, S → aSa ε, S → bSb ε, S → aTb ε, S → bTa ε, T → aT ε, T → bT ε, T → ε

10 / 18

slide-38
SLIDE 38

Returning to the example

S ⇒ aSa ⇒ abTaa ⇒ abaTaa ⇒ abaaa State Action Input read Stack q0 push $ ε $ q1 push S ε S$ qloop pop S, push aSa ε aSa$ qloop read and pop a a Sa$ qloop pop S, push bTa a bTaa$ qloop read and pop b ab Taa$ qloop pop T, push aT ab aTaa$ qloop read and pop a aba Taa$ qloop pop T, push ε aba aa$ q0 q1 qloop qa ε, ε → $ ε, ε → S ε, $ → ε a, a → ε b, b → ε ε, S → aSa ε, S → bSb ε, S → aTb ε, S → bTa ε, T → aT ε, T → bT ε, T → ε

10 / 18

slide-39
SLIDE 39

Returning to the example

S ⇒ aSa ⇒ abTaa ⇒ abaTaa ⇒ abaaa State Action Input read Stack q0 push $ ε $ q1 push S ε S$ qloop pop S, push aSa ε aSa$ qloop read and pop a a Sa$ qloop pop S, push bTa a bTaa$ qloop read and pop b ab Taa$ qloop pop T, push aT ab aTaa$ qloop read and pop a aba Taa$ qloop pop T, push ε aba aa$ qloop read and pop a abaa a$ q0 q1 qloop qa ε, ε → $ ε, ε → S ε, $ → ε a, a → ε b, b → ε ε, S → aSa ε, S → bSb ε, S → aTb ε, S → bTa ε, T → aT ε, T → bT ε, T → ε

10 / 18

slide-40
SLIDE 40

Returning to the example

S ⇒ aSa ⇒ abTaa ⇒ abaTaa ⇒ abaaa State Action Input read Stack q0 push $ ε $ q1 push S ε S$ qloop pop S, push aSa ε aSa$ qloop read and pop a a Sa$ qloop pop S, push bTa a bTaa$ qloop read and pop b ab Taa$ qloop pop T, push aT ab aTaa$ qloop read and pop a aba Taa$ qloop pop T, push ε aba aa$ qloop read and pop a abaa a$ qloop read and pop a abaaa $ q0 q1 qloop qa ε, ε → $ ε, ε → S ε, $ → ε a, a → ε b, b → ε ε, S → aSa ε, S → bSb ε, S → aTb ε, S → bTa ε, T → aT ε, T → bT ε, T → ε

10 / 18

slide-41
SLIDE 41

Returning to the example

S ⇒ aSa ⇒ abTaa ⇒ abaTaa ⇒ abaaa State Action Input read Stack q0 push $ ε $ q1 push S ε S$ qloop pop S, push aSa ε aSa$ qloop read and pop a a Sa$ qloop pop S, push bTa a bTaa$ qloop read and pop b ab Taa$ qloop pop T, push aT ab aTaa$ qloop read and pop a aba Taa$ qloop pop T, push ε aba aa$ qloop read and pop a abaa a$ qloop read and pop a abaaa $ qloop pop $ abaaa ε q0 q1 qloop qa ε, ε → $ ε, ε → S ε, $ → ε a, a → ε b, b → ε ε, S → aSa ε, S → bSb ε, S → aTb ε, S → bTa ε, T → aT ε, T → bT ε, T → ε

10 / 18

slide-42
SLIDE 42

Returning to the example

S ⇒ aSa ⇒ abTaa ⇒ abaTaa ⇒ abaaa State Action Input read Stack q0 push $ ε $ q1 push S ε S$ qloop pop S, push aSa ε aSa$ qloop read and pop a a Sa$ qloop pop S, push bTa a bTaa$ qloop read and pop b ab Taa$ qloop pop T, push aT ab aTaa$ qloop read and pop a aba Taa$ qloop pop T, push ε aba aa$ qloop read and pop a abaa a$ qloop read and pop a abaaa $ qloop pop $ abaaa ε qa accept abaaa ε q0 q1 qloop qa ε, ε → $ ε, ε → S ε, $ → ε a, a → ε b, b → ε ε, S → aSa ε, S → bSb ε, S → aTb ε, S → bTa ε, T → aT ε, T → bT ε, T → ε

10 / 18

slide-43
SLIDE 43

Back from example

Consider running M on input w = w1w2⋯wn for wi ∈ Σ. The first time M enters state qloop, the stack is S$ and no input has been read. Every subsequent time it enters qloop, the input read so far concatenated with the stack is a step in some left-most derivation of w (followed by a $). I.e., if k symbols have been read from the input and the stack is s, then w1w2⋯wks is a step in the derivation of w

11 / 18

slide-44
SLIDE 44

Back from example

Consider running M on input w = w1w2⋯wn for wi ∈ Σ. The first time M enters state qloop, the stack is S$ and no input has been read. Every subsequent time it enters qloop, the input read so far concatenated with the stack is a step in some left-most derivation of w (followed by a $). I.e., if k symbols have been read from the input and the stack is s, then w1w2⋯wks is a step in the derivation of w M accepts w once the derivation is complete and all terminals have been matched. Therefore, each string accepted by M is in A.

11 / 18

slide-45
SLIDE 45

Back from example

Consider running M on input w = w1w2⋯wn for wi ∈ Σ. The first time M enters state qloop, the stack is S$ and no input has been read. Every subsequent time it enters qloop, the input read so far concatenated with the stack is a step in some left-most derivation of w (followed by a $). I.e., if k symbols have been read from the input and the stack is s, then w1w2⋯wks is a step in the derivation of w M accepts w once the derivation is complete and all terminals have been matched. Therefore, each string accepted by M is in A. For each w ∈ A, there is some left-most derivation of w by G. By construction, M performs the derivation on the stack while matching leading terminals. Thus L(M) = A.

11 / 18

slide-46
SLIDE 46

Going the other direction

Theorem

If a language is recognized by a PDA, then it is context-free.

Proof idea.

12 / 18

slide-47
SLIDE 47

Going the other direction

Theorem

If a language is recognized by a PDA, then it is context-free.

Proof idea.

1 First, convert the PDA to one that

  • has a single accepting state qa;
  • empties its stack before accepting; and
  • either pushes a symbol or pops a symbol, but not both, on each transition

12 / 18

slide-48
SLIDE 48

Going the other direction

Theorem

If a language is recognized by a PDA, then it is context-free.

Proof idea.

1 First, convert the PDA to one that

  • has a single accepting state qa;
  • empties its stack before accepting; and
  • either pushes a symbol or pops a symbol, but not both, on each transition

2 Next, construct a CFG that

  • has variables that are pairs of states ⟨q, r⟩ from the PDA;
  • has start variable ⟨q0, qa⟩;
  • has rules ⟨q, q⟩ → ε for each q ∈ Q;
  • has rules ⟨p, r⟩ → ⟨p, q⟩⟨q, r⟩ for each p, q, r ∈ Q; and
  • has rules ⟨p, q⟩ → a⟨r, s⟩b for p, q, r, s ∈ Q and a, b ∈ Σε if (r, u) ∈ δ(p, a, ε) and

(q, ε) ∈ δ(s, b, u)

12 / 18

slide-49
SLIDE 49

Going the other direction

Theorem

If a language is recognized by a PDA, then it is context-free.

Proof idea.

1 First, convert the PDA to one that

  • has a single accepting state qa;
  • empties its stack before accepting; and
  • either pushes a symbol or pops a symbol, but not both, on each transition

2 Next, construct a CFG that

  • has variables that are pairs of states ⟨q, r⟩ from the PDA;
  • has start variable ⟨q0, qa⟩;
  • has rules ⟨q, q⟩ → ε for each q ∈ Q;
  • has rules ⟨p, r⟩ → ⟨p, q⟩⟨q, r⟩ for each p, q, r ∈ Q; and
  • has rules ⟨p, q⟩ → a⟨r, s⟩b for p, q, r, s ∈ Q and a, b ∈ Σε if (r, u) ∈ δ(p, a, ε) and

(q, ε) ∈ δ(s, b, u)

3 Prove (by induction) that each variable ⟨q, r⟩ has the property ⟨q, r⟩ ∗

⇒ x ∈ Σ∗ iff starting M in state q with an empty stack and running on input x causes M to move to state r and end with an empty stack

12 / 18

slide-50
SLIDE 50

Going the other direction

Theorem

If a language is recognized by a PDA, then it is context-free.

Proof idea.

1 First, convert the PDA to one that

  • has a single accepting state qa;
  • empties its stack before accepting; and
  • either pushes a symbol or pops a symbol, but not both, on each transition

2 Next, construct a CFG that

  • has variables that are pairs of states ⟨q, r⟩ from the PDA;
  • has start variable ⟨q0, qa⟩;
  • has rules ⟨q, q⟩ → ε for each q ∈ Q;
  • has rules ⟨p, r⟩ → ⟨p, q⟩⟨q, r⟩ for each p, q, r ∈ Q; and
  • has rules ⟨p, q⟩ → a⟨r, s⟩b for p, q, r, s ∈ Q and a, b ∈ Σε if (r, u) ∈ δ(p, a, ε) and

(q, ε) ∈ δ(s, b, u)

3 Prove (by induction) that each variable ⟨q, r⟩ has the property ⟨q, r⟩ ∗

⇒ x ∈ Σ∗ iff starting M in state q with an empty stack and running on input x causes M to move to state r and end with an empty stack

4 Conclude that ⟨q0, qa⟩ ∗

⇒ w iff w ∈ L(M)

12 / 18

slide-51
SLIDE 51

Closure properties of CFLs

The class of context-free languages is closed under

  • Union
  • Concatenation
  • Kleene star
  • Prefix
  • Suffix
  • Reversal
  • Intersection with a regular language
  • Quotient by a string
  • Quotient by a regular language

We proved closure under union, concatenation, Kleene star, and Prefix previously

13 / 18

slide-52
SLIDE 52

Reversal

Theorem

Context-free languages are closed under reversal.

  • Proof. Let B be a context-free language generated by a CFG G = (V, Σ, R, S).

Construct CFG G′ = (V, Σ, R′, S) where R′ = {A → uR ∣ A → u is a rule in R}.

14 / 18

slide-53
SLIDE 53

Reversal

Theorem

Context-free languages are closed under reversal.

  • Proof. Let B be a context-free language generated by a CFG G = (V, Σ, R, S).

Construct CFG G′ = (V, Σ, R′, S) where R′ = {A → uR ∣ A → u is a rule in R}. To prove that L(G′) = BR, we want to show that for each variable A ∈ V and u ∈ (V ∪ Σ)∗, A

⇒G u in n steps iff A

⇒G′ uR in n steps. Let’s write

k

⇒ to mean

⇒ in exactly k steps.

14 / 18

slide-54
SLIDE 54

Proof continued

Base case n = 0. If A ⇒G u, then u = uR = A so A ⇒G′ uR, and vice versa.

15 / 18

slide-55
SLIDE 55

Proof continued

Base case n = 0. If A ⇒G u, then u = uR = A so A ⇒G′ uR, and vice versa. Inductive step. Assume that for all n > 0, A ∈ V , and u ∈ (V ∪ Σ)∗, A

n−1

⇒ G u iff A

n−1

⇒ G′ uR.

15 / 18

slide-56
SLIDE 56

Proof continued

Base case n = 0. If A ⇒G u, then u = uR = A so A ⇒G′ uR, and vice versa. Inductive step. Assume that for all n > 0, A ∈ V , and u ∈ (V ∪ Σ)∗, A

n−1

⇒ G u iff A

n−1

⇒ G′ uR. If A

n

⇒G u, then there is some C ∈ V and x, y, z ∈ (V ∪ Σ)∗ such that u = xyz, A

n−1

⇒ G xCz, and C ⇒G y.

15 / 18

slide-57
SLIDE 57

Proof continued

Base case n = 0. If A ⇒G u, then u = uR = A so A ⇒G′ uR, and vice versa. Inductive step. Assume that for all n > 0, A ∈ V , and u ∈ (V ∪ Σ)∗, A

n−1

⇒ G u iff A

n−1

⇒ G′ uR. If A

n

⇒G u, then there is some C ∈ V and x, y, z ∈ (V ∪ Σ)∗ such that u = xyz, A

n−1

⇒ G xCz, and C ⇒G y. By the inductive hypothesis A

n−1

⇒ G′ zRCxR and by construction C ⇒G′ yR. Thus A

n

⇒G′ zRyRxR = (xyz)R = uR. Swapping G and G′ shows the converse.

15 / 18

slide-58
SLIDE 58

Proof continued

Base case n = 0. If A ⇒G u, then u = uR = A so A ⇒G′ uR, and vice versa. Inductive step. Assume that for all n > 0, A ∈ V , and u ∈ (V ∪ Σ)∗, A

n−1

⇒ G u iff A

n−1

⇒ G′ uR. If A

n

⇒G u, then there is some C ∈ V and x, y, z ∈ (V ∪ Σ)∗ such that u = xyz, A

n−1

⇒ G xCz, and C ⇒G y. By the inductive hypothesis A

n−1

⇒ G′ zRCxR and by construction C ⇒G′ yR. Thus A

n

⇒G′ zRyRxR = (xyz)R = uR. Swapping G and G′ shows the converse. Thus, A

n

⇒G u iff A

n

⇒G′ uR.

15 / 18

slide-59
SLIDE 59

Proof continued

Base case n = 0. If A ⇒G u, then u = uR = A so A ⇒G′ uR, and vice versa. Inductive step. Assume that for all n > 0, A ∈ V , and u ∈ (V ∪ Σ)∗, A

n−1

⇒ G u iff A

n−1

⇒ G′ uR. If A

n

⇒G u, then there is some C ∈ V and x, y, z ∈ (V ∪ Σ)∗ such that u = xyz, A

n−1

⇒ G xCz, and C ⇒G y. By the inductive hypothesis A

n−1

⇒ G′ zRCxR and by construction C ⇒G′ yR. Thus A

n

⇒G′ zRyRxR = (xyz)R = uR. Swapping G and G′ shows the converse. Thus, A

n

⇒G u iff A

n

⇒G′ uR. Therefore, for w ∈ B, S

⇒G w iff S

⇒G′ wR so L(G′) = BR.

15 / 18

slide-60
SLIDE 60

Suffix

Theorem

Context free languages are closed under Suffix.

Proof.

Since Suffix(A) = Prefix(AR)R and CFLs are closed under reversal and Prefix, CFLs are closed under Suffix.

16 / 18

slide-61
SLIDE 61

Intersection of a CFL and a regular language

Theorem

The intersection of a CFL and a regular language is context-free.

Proof.

Let A be a CFL recognized by the PDA M1 = (Q1, Σ, Γ, δ1, q1, F1) and B be a regular language recognized by the NFA M2 = (Q2, Σ, δ2, q2, F2).

17 / 18

slide-62
SLIDE 62

Intersection of a CFL and a regular language

Theorem

The intersection of a CFL and a regular language is context-free.

Proof.

Let A be a CFL recognized by the PDA M1 = (Q1, Σ, Γ, δ1, q1, F1) and B be a regular language recognized by the NFA M2 = (Q2, Σ, δ2, q2, F2). Construct the PDA M = (Q, Σ, Γ, δ, q0, F) where Q = Q1 × Q2 q0 = (q1, q2) F = F1 × F2 δ((q, r), a, b) = {((s, t), c) ∣ (s, c) ∈ δ1(q, a, b) and t ∈ δ2(r, a)} for a ∈ Σε, b, c ∈ Γε

17 / 18

slide-63
SLIDE 63

Intersection of a CFL and a regular language

Theorem

The intersection of a CFL and a regular language is context-free.

Proof.

Let A be a CFL recognized by the PDA M1 = (Q1, Σ, Γ, δ1, q1, F1) and B be a regular language recognized by the NFA M2 = (Q2, Σ, δ2, q2, F2). Construct the PDA M = (Q, Σ, Γ, δ, q0, F) where Q = Q1 × Q2 q0 = (q1, q2) F = F1 × F2 δ((q, r), a, b) = {((s, t), c) ∣ (s, c) ∈ δ1(q, a, b) and t ∈ δ2(r, a)} for a ∈ Σε, b, c ∈ Γε As M runs on input w, its stack and the first element of its state change according to δ1 whereas the second element of its state changes according to δ2. M accepts w iff M1 accepts w and M2 accepts w. Therefore, L(M) = A ∩ B.

17 / 18

slide-64
SLIDE 64

What about intersection with another CFL?

Are context-free languages closed under intersection?

18 / 18

slide-65
SLIDE 65

What about intersection with another CFL?

Are context-free languages closed under intersection? Consider Σ = {a, b, c} and A = {ambmcn ∣ m, n ≥ 0} B = {ambncn ∣ m, n ≥ 0} Both B and C are context-free. Is A ∩ B = {anbncn ∣ n ≥ 0}? How can we keep track of how many as and bs we’ve seen to ensure we get the same number of cs using a PDA? How about trying to generate such strings with a CFG?

18 / 18

slide-66
SLIDE 66

What about intersection with another CFL?

Are context-free languages closed under intersection? Consider Σ = {a, b, c} and A = {ambmcn ∣ m, n ≥ 0} B = {ambncn ∣ m, n ≥ 0} Both B and C are context-free. Is A ∩ B = {anbncn ∣ n ≥ 0}? How can we keep track of how many as and bs we’ve seen to ensure we get the same number of cs using a PDA? How about trying to generate such strings with a CFG? Next time, we’ll see that B ∩ C is not context-free!

18 / 18