1 CS21 Decidability and Tractability Lecture 7 January 23 2019 January 23 2019 CS21 Lecture 7 2 Outline equivalence of NPDAs and CFGs non contextfree languages deterministic PDAs ID: 758586
Download Presentation The PPT/PDF document "January 23, 2019 CS21 Lecture 7" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
Slide1
January 22, 2020
CS21 Lecture 7
1
CS21 Decidability and Tractability
Lecture
7
January
22
,
2020Slide2
January 22, 2020
CS21 Lecture 7
2
Outline
proof of the CFL Pumping Lemma
d
eterministic
PDAs
deciding
CFLs
Turing
Machines and
variantsSlide3
January 22, 2020
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 asw = uvxyz such thatfor every i 0, uvixyiz L , and |vy| > 0, and|vxy| p.
Slide4
January 22, 2020
CS21 Lecture 7
4
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
A
a
b
a
a
b
b
b
a
b
b
a
b
b
b
long path
some non-terminal must repeat on long pathSlide5
January 22, 2020
CS21 Lecture 7
5
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
ASlide6
January 22, 2020
CS21 Lecture 7
6
CFL Pumping Lemma
Schematic proof:
u
v
x
y
z
S
A
A
u
z
S
A
u
z
S
A
xSlide7
January 22, 2020
CS21 Lecture 7
7
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 ≤ b
h
(so length > b
h
implies height > h)Slide8
January 22, 2020
CS21 Lecture 7
8
CFL Pumping Lemma
let m be the # of nonterminals in the grammar
to ensure path of length at least m+2, require
|w| ≥
p
= b
m+2
since |w| > b
m+1
, any parse tree for w has height > m+1
let T be the
smallest parse tree for wlongest root-leaf path must consist of ≥ m+1 non-terminals and 1 terminal.Slide9
January 22, 2020
CS21 Lecture 7
9
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 ≤ b
m+2
= p leavesSlide10
January 22, 2020
CS21 Lecture 7
10
Deterministic PDA
A NPDA is a 6-tuple
(Q,
Σ
,
δ
, q
0
, F)
where:
δ:Q x (Σ {ε}) x ( {ε}) → P (Q x ( {ε})) is a function called the transition functionA deterministic PDA has only one option at every step:
for every state
q
Q
, a
Σ
, and
t
,
exactly 1 element in δ(q, a, t), orexactly 1 element in δ(q, ε
, t), and δ(q, a, t) empty for all a Σ Slide11
January 22, 2020
CS21 Lecture 7
11
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)Slide12
January 22, 2020
CS21 Lecture 7
12
Example deterministic PDA
L = {0
n
1
n
: n
0
}
(
unpictured
transitions go to a “reject” state and stay there)
ε
,
ε
→
$
■
, $
→
ε
1, 0
→
ε
0,
ε
→
0
1, 0 → ε
Σ = {0, 1} = {0, 1, $}
Slide13
January 22, 2020
CS21 Lecture 7
13
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 stringSlide14
January 22, 2020
CS21 Lecture 7
14
Example of problem
0,
ε
→
ε
0,
ε
→
ε
1,
ε
→
ε
1,
ε
→
ε
ε
,
ε
→
$
Language of this DPDA
is
0*
■
,
ε
→ ε
■
, ε → ε
ε
,
ε
→ $Slide15
January 22, 2020
CS21 Lecture 7
15
Example of problem
0,
ε
→
ε
0,
ε
→
ε
1,
ε
→
ε
1,
ε
→
ε
ε
,
ε
→
$
Language of this DPDA
is
{}
■
,
ε
→ ε
■
, ε → ε
ε
,
ε
→ $Slide16
January 22, 2020
CS21 Lecture 7
16
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 loopsSlide17
January 22, 2020
CS21 Lecture 7
17
Deterministic PDA
step 1: keep track of when we have read to end of input
■, ?
→
?
q
0
q
1
q
3
q
2
■, ?
→
?
q
0
’
q
1
’
q
3
’
q
2
’Slide18
January 22, 2020
CS21 Lecture 7
18
Deterministic PDA
step 1: keep track of when we have read to end of input
■, ?
→
?
q
0
q
1
q
3
q
2
■, ?
→
?
q
0
’
q
1
’
q
3
’
q
2
’
for accept state q’: replace outgoing “
ε
, ? → ?” transition with self-loop with same label Slide19
January 22, 2020
CS21 Lecture 7
19
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) Slide20
January 22, 2020
CS21 Lecture 7
20
Deterministic PDA
step 2: eliminate infinite loops
on input x, if infinite loop, then:
stack height
time
i
0
i
1
i
2
i
3
infinite
sequence i
0
< i
1
< i
2
< … such that for all k, stack height never decreases below ht(i
k
) after time i
kSlide21
January 22, 2020
CS21 Lecture 7
21
Deterministic PDA
step 2: eliminate infinite loops
infinite seq. i
0
< i
1
< … such that for all k, stack height never decreases below ht(i
k
) after time i
k
infinite subsequence j
0< j1< j2< … such that same transition is applied at each time jk
p
ε
, t
→
s
never see any stack symbol below t from j
k
on
we are in a periodic, deterministic sequence of stack operations
independent of the input Slide22
January 22, 2020
CS21 Lecture 7
22
Deterministic PDA
step 2: eliminate infinite loops
infinite subsequence j
0
< j
1
< j
2
< … such that same transition is applied at each time j
k
safe to replace:
p
ε
, t
→
s
r’
r
a, t
→
t (for all a, t)
ε
, t
→
t (for all t)
■
, t
→
t (for all t)
p’
ε
, t
→
s
ε
, t
→
s
or
ε
, t
→
s Slide23
January 22, 2020
CS21 Lecture 7
23
Deterministic PDA
finishing up…
have a machine M with no infinite loops
therefore it always reads to end of input
either enters an accept state q’, or enters “reject” state r’
now, can swap: make r’ unique accept state to get a machine recognizing complement of L Slide24
January 22, 2020
CS21 Lecture 7
24
Chomsky Normal Form
Useful to deal only with CFGs in a simple
normal form
Most common:
Chomsky Normal Form (CNF)Definition: every production has formA
→ BC or S → ε
or
A → a
where
A, B, C
are any non-terminals (and
B, C are not S) and a is any terminal.Slide25
January 22, 2020
CS21 Lecture 7
25
Chomsky Normal Form
Theorem
: Every CFL is generated by a CFG in Chomsky Normal Form.
Proof
: exercise or in book…
Slide26
January 22, 2020
CS21 Lecture 7
26
Deciding CFLs
Useful to have an
efficient algorithm
to decide whether string x is in given CFL
e.g. programming language often described by CFG. Determine if string is valid program.
If CFL recognized by
deterministic PDA
, just simulate the PDA.
but not all CFLs are (homework)…
Can simulate NPDA, but this takes
exponential time
in the worst case.Slide27
January 22, 2020
CS21 Lecture 7
27
Deciding CFLs
Convert CFG into
Chomsky Normal Form
parse
tree for string
x
generated by nonterminal
A
:
A
B
C
x
If
A
k
x (k > 1) then t
here must be a way to split x:
x =
yz
A → BC is a production and
B
i
y and C
j
z for
i, j < k
Slide28
January 22, 2020
CS21 Lecture 7
28
Deciding CFLs
An algorithm:
IsGenerated(x, A)
if |x| = 1, then return YES if A → x is a production, else return NO
for all n-1 ways of splitting x = yz
for all ≤ m productions of form A → BC
if IsGenerated(y, B) and IsGenerated(z, C), return YES
return NO
worst case running time?