Post on 18-Jan-2018
description
January 20, 2016 CS21 Lecture 7 1
CS21 Decidability and Tractability
Lecture 7January 20, 2016
January 20, 2016 CS21 Lecture 7 2
Outline
• non context-free languages
• deterministic PDAs• deciding CFLs
January 20, 2016 CS21 Lecture 7 3
Pumping Lemma for CFLs
CFL Pumping Lemma: Let L be a CFL. There exists an integer p (“pumping length”) for which every w L with |w| p can be written as
w = uvxyz such that1. for every i 0, uvixyiz L , and 2. |vy| > 0, and3. |vxy| p.
January 20, 2016 CS21 Lecture 7 4
CFL Pumping Lemma Example
Theorem: the following language is not context-free:
L = {anbncn : n ≥ 0}.• Proof:
– let p be the pumping length for L– choose w = apbpcp
w = aaaa…abbbb…bcccc…c– w = uvxyz, with |vy| > 0 and |vxy| p.
January 20, 2016 CS21 Lecture 7 5
CFL Pumping Lemma Example
– possibilities:
w = aaaa…aaabbb…bbcccc…c
(if v, y each contain only one type of symbol, then pumping on them produces a string not in the language)
u v x y z
January 20, 2016 CS21 Lecture 7 6
CFL Pumping Lemma Example
– possibilities:
w = aaaa…abbbb…bccccc…c
(if v or y contain more than one type of symbol, then pumping on them might produce a string with equal numbers of a’s, b’s, and c’s – if vy contains equal numbers of a’s, b’s, and c’s. But they will be out of order.)
u v x y z
January 20, 2016 CS21 Lecture 7 7
CFL Pumping Lemma Example
Theorem: the following language is not context-free:
L = {xx : x {0,1}*}.• Proof:
– let p be the pumping length for L– try w = 0p10p1– can this be pumped?
January 20, 2016 CS21 Lecture 7 8
CFL Pumping Lemma Example
L = {xx : x {0,1}*}.– try w = 02p12p02p12p
– w = uvxyz, with |vy| > 0 and |vxy| p.– case: vxy in first half.
• then uv2xy2z = 0??...?1??...?– case: vxy in second half.
• then uv2xy2z = ??...?0??...?1– case: vxy straddles midpoint
• then uv0xy0z = uxz = 02p1i0j12p with i ≠ 2p or j ≠ 2p
January 20, 2016 CS21 Lecture 7 9
CFL Pumping Lemma
Proof: consider a parse tree for a very long string w L: S
A B C. . .
A D S. . . C S A A B. . .
A C
S S
A D D C
B A
B Aa
b
a a
b
b b
a b
b a
b b b
long path
some non-terminal must repeat on long path
January 20, 2016 CS21 Lecture 7 10
CFL Pumping Lemma
• Schematic proof:
u v x y z
S
A
A u v y z
S
A
A
u v y z
S
A
A
v x y
A
January 20, 2016 CS21 Lecture 7 11
CFL Pumping Lemma
• Schematic proof:
u v x y z
S
A
A u z
S
A
u z
S
A
x
January 20, 2016 CS21 Lecture 7 12
CFL Pumping Lemma
– how large should pumping length p be?– need to ensure other conditions:
|vy| > 0 |vxy| ≤ p
– b = max # symbols on rhs of any production (assume b ≥ 2)
– if parse tree has height ≤ h, then string generated has length ≤ bh (so length > bh implies height > h)
January 20, 2016 CS21 Lecture 7 13
CFL Pumping Lemma
– let m be the # of nonterminals in the grammar– to ensure path of length at least m+2, require
|w| ≥ p = bm+2
– since |w| > bm+1, any parse tree for w has height > m+1
– let T be the smallest parse tree for w– longest root-leaf path must consist of ≥ m+1
non-terminals and 1 terminal.
January 20, 2016 CS21 Lecture 7 14
CFL Pumping Lemma– must be a repeated non-
terminal A on long path– select a repetition among the
lowest m+1 non-terminals on path.
– pictures show that for every i 0, uvixyiz L u v x y z
S
A
A
– is |vy| > 0 ?• smallest parse tree T ensures
– is |vxy| ≤ p?• red path has length ≤ m+2, so ≤ bm+2 = p leaves
January 20, 2016 CS21 Lecture 7 15
Deterministic PDA
• A NPDA is a 6-tuple (Q, Σ, , δ, q0, F) where:– δ:Q x (Σ {ε}) x ( {ε}) → (Q x ( {ε}))
is a function called the transition function• A deterministic PDA has only one option at
every step:– for every state q Q, a Σ, and t , exactly
1 element in δ(q, a, t), or– exactly 1 element in δ(q, ε, t), and δ(q, a, t)
empty for all a Σ
January 20, 2016 CS21 Lecture 7 16
Deterministic PDA
• A technical detail: we will give our deterministic machine the ability to detect end of input string– add special symbol ■ to alphabet– require input tape to contain x■
• language recognized by a deterministic PDA is called a deterministic CFL (DCFL)
January 20, 2016 CS21 Lecture 7 17
Example deterministic PDA
L = {0n1n : n 0}(unpictured transitions go to a “reject” state and stay there)
ε, ε → $
■, $ → ε
1, 0 → ε
0, ε → 0
1, 0 → ε
Σ = {0, 1}
= {0, 1, $}
January 20, 2016 CS21 Lecture 7 18
Deterministic PDA
Theorem: DCFLs are closed under complement
(complement of L in Σ* is (Σ* - L) )Proof attempt:
– swap accept/non-accept states– problem: might enter infinite loop before
reading entire string– machine for complement must accept in these
cases, and read to end of string
January 20, 2016 CS21 Lecture 7 19
Example of problem
0, ε → ε0, ε → ε
1, ε → ε
1, ε → εε, ε → $
Language of this DPDA is 0*
■, ε → ε
■, ε → ε
ε, ε → $
January 20, 2016 CS21 Lecture 7 20
Example of problem
0, ε → ε0, ε → ε
1, ε → ε
1, ε → εε, ε → $
Language of this DPDA is {}
■, ε → ε
■, ε → ε
ε, ε → $
January 20, 2016 CS21 Lecture 7 21
Deterministic PDA
Proof:– convert machine into “normal form”
• always reads to end of input• always enters either an accept state or single
distinguished “reject” state– step 1: keep track of when we have read to
end of input– step 2: eliminate infinite loops
January 20, 2016 CS21 Lecture 7 22
Deterministic PDA
step 1: keep track of when we have read to end of input
■, ? → ?
q0 q1
q3
q2
■, ? → ?
q0’ q1’
q3’q2’
January 20, 2016 CS21 Lecture 7 23
Deterministic PDA
step 1: keep track of when we have read to end of input
■, ? → ?q0 q1
q3
q2
■, ? → ?
q0’ q1’
q3’q2’
for accept state q’: replace outgoing “ε, ? → ?” transition with self-loop with same label
January 20, 2016 CS21 Lecture 7 24
Deterministic PDA
step 2: eliminate infinite loops
– add new “reject” states
r’r
a, t →t (for all a, t) ε, t → t (for all t)
■, t → t (for all t)
January 20, 2016 CS21 Lecture 7 25
Deterministic PDA
step 2: eliminate infinite loops– on input x, if infinite loop, then:
stack height
time i0 i1 i2 i3 infinite sequence i0< i1< i2< … such
that for all k, stack height never decreases below ht(ik) after time ik