CS代考 CS21 Decidability and Tractability

CS21 Decidability and Tractability
Lecture 7 January 19, 2024
CFG example
• Arithmetic expressions over {+,*,(,),a}

Copyright By PowCoder代写 加微信 powcoder

– (a + a) * a –a*a+a+a+a+a
• A CFG generating this language:
* + → () | a
January 19, 2024 CS21 Lecture 7 2
CFG example
• A derivation of the string: a+a*a *
+ * ⇒ a + *
⇒ a + a * ⇒a+a*a
January 19, 2024 CS21 Lecture 7 3
* + → () | a
Parse Trees
• Easier way to picture derivation: parse tree
*
+ a aa
• grammar encodes grouping information; this is captured in the parse tree.
January 19, 2024 CS21 Lecture 7 4
CFGs and parse trees
• Is this a good grammar for arithmetic expressions?
– can group wrong way (+ precedence over *) – different grammar for same language can
force correct precedence/grouping
January 19, 2024 CS21 Lecture 7 5
* + → () | a
Some facts about CFLs • CFLs are closed under
– concatenation – star
(proof?) (proof?) (proof?)
• Every regular language is a CFL – proof?
January 19, 2024 CS21 Lecture 7 6

NPDA, CFG equivalence Theorem: a language L is recognized by a
NPDA iff L is described by a CFG.
Must prove two directions: (proof next lecture!) (⇒) L is recognized by a NPDA implies L is
described by a CFG.
(⇐) L is described by a CFG implies L is
recognized by a NPDA.
January 19, 2024 CS21 Lecture 7 7
NPDA, CFG equivalence
Proof of (⇐): L is described by a CFG implies L is recognized by a NPDA.
0#1 0#1 0#1
q1 q2 q3 idea:
January 19, 2024
000 A## 1 1 1 $$$
CS21 Lecture 7 8
NPDA, CFG equivalence
1. we’dliketonon-deterministicallyguessthe derivation, forming it on the stack
2. thenscantheinput,poppingmatching symbol off the stack at each step
3. acceptifwegettothebottomofthestackat the end of the input.
what is wrong with this approach?
January 19, 2024 CS21 Lecture 7 9
NPDA, CFG equivalence
– only have access to top of stack – combine steps 1 and 2:
• allow to match stack terminals with tape during the process of producing the derivation on the stack
January 19, 2024
0#1 0#1 0#1
CS21 Lecture 7 10
NPDA, CFG equivalence
• informal description of construction: – place $ and start symbol S on the stack – repeat:
• if the top of the stack is a non-terminal A, pick a production with A on the lhs and substitute the rhs for A on the stack
• if the top of the stack is a terminal b, read b from the tape, and pop b from the stack.
• if the top of the stack is $, enter the accept state. January 19, 2024 CS21 Lecture 7 11
NPDA, CFG equivalence
ε, ε → S$ ε,A→w
one transition for
each production
q ε, A → w = w1
January 19, 2024
one transition for
shorthand for: ε, ε → wk-1
ε, A → wk q2
each terminal b
CS21 Lecture 7

NPDA, CFG equivalence Proof of (⇒): L is recognized by a NPDA
implies L is described by a CFG.
– harder direction
– first step: convert NPDA into “normal form”:
• single accept state
• empties stack before accepting
• each transition either pushes or pops a symbol
January 19, 2024 CS21 Lecture 7 13
NPDA, CFG equivalence
– main idea: non-terminal Ap,q generates exactly the strings that take the NPDA from state p (w/ empty stack) to state q (w/ empty stack)
– then Astart, accept generates all of the strings in the language recognized by the NPDA.
January 19, 2024 CS21 Lecture 7 14
NPDA, CFG equivalence
• Two possibilities to get from state p to q:
stack height
January 19, 2024
generated by Ap,r
generated by Ar,q prq
abcabbacacbacbacabacabbabbabaacabbbababaacaccaccccc
string taking NPDA from p to q
CS21 Lecture 7 15
NPDA, CFG equivalence
• NPDA P = (Q, Σ, Γ, δ, start, {accept}) • CFG G:
– non-terminals V = {Ap,q : p, q ∈ Q}
– start variable Astart, accept
– productions:
for every p, r, q ∈Q, add the rule Ap,q → Ap,rAr,q
January 19, 2024 CS21 Lecture 7 16
NPDA, CFG equivalence
• Two possibilities to get from state p to q:
stack height
January 19, 2024
generated by Ar,s
p push d pop d q
abcabbacacbacbacabacabbabbabaacabbbababaacaccaccccc
string taking NPDA from p to q
CS21 Lecture 7 17
NPDA, CFG equivalence
• CFG G: move to state r – non-terminals V = {Ap,q f:ropm, qs∈taQte}s,
– start variable Astart, acceprtead b, pop d,
• NPDA P = (Q, Σ, Γ, δ, start, {accept}) read a, push d,
from state p,
– productions:
move to state q
for every p, r, s, q ∈ Q, d ∈ Γ and a, b ∈ (Σ ∪ {ε}) if (r,d)∈δ(p,a,ε),and
(q, ε) ∈ δ(s, b, d), add the rule Ap,q → aAr,sb
January 19, 2024 CS21 Lecture 7 18

NPDA, CFG equivalence
• NPDA P = (Q, Σ, Γ, δ, start, {accept}) • CFG G:
– non-terminals V = {Ap,q : p, q ∈ Q} – start variable Astart, accept
– productions:
for every p ∈ Q, add the rule Ap,p → ε
January 19, 2024 CS21 Lecture 7 19
NPDA, CFG equivalence • two claims to verify correctness:
1. if Ap,q generates string x, then x can take NPDA P from state p (w/ empty stack) to q (w/ empty stack)
2. ifxcantakeNPDAPfromstatep(w/ empty stack) to q (w/ empty stack), then Ap,q generates string x
January 19, 2024 CS21 Lecture 7 20
NPDA, CFG equivalence
1. if Ap,q generates string x, then x can take NPDA P from state p (w/ empty stack) to q (w/ empty stack)
– induction on length of derivation of x.
– base case: 1 step derivation. must have only terminals on rhs. In G, must be production of form Ap,p → ε.
January 19, 2024 CS21 Lecture 7 21
NPDA, CFG equivalence
1. if Ap,q generates string x, then x can take NPDA P from state p (w/ empty stack) to q (w/ empty stack)
– assume true for derivations of length at most k, prove for length k+1.
– verify case: Ap,q → Ap,rAr,q →k x = yz
– verify case: Ap,q → aAr,sb →k x = ayb
January 19, 2024 CS21 Lecture 7 22
NPDA, CFG equivalence
2. if x can take NPDA P from state p (w/ empty stack) to q (w/ empty stack), then Ap,q generates string x
– induction on # of steps in P’s computation
– base case: 0 steps. starts and ends at same state p. only has time to read empty string ε.
– G contains Ap,p → ε.
January 19, 2024 CS21 Lecture 7 23
NPDA, CFG equivalence
2. if x can take NPDA P from state p (w/ empty stack) to q (w/ empty stack), then Ap,q generates string x
– induction step. assume true for computations of length at most k, prove for length k+1.
– if stack becomes empty sometime in the
middle of the computation (at state r)
• y is read going from state p to r
• z is read going from state r to q
• conclude: Ap,q → Ap,rAr,q →* yz = x
January 19, 2024 CS21 Lecture 7
(Ap,r→* y) (Ar,q→* z)

NPDA, CFG equivalence
2. if x can take NPDA P from state p (w/ empty stack) to q (w/ empty stack), then Ap,q generates string x
– if stack becomes empty only at beginning and end of computation.
• first step: state p to r, read a, push d • go from state r to s, read string y
• last step: state s to q, read b, pop d • conclude: Ap,q → aAr,sb →* ayb = x
January 19, 2024 CS21 Lecture 7
(Ar,s→* y)
NPDA, CFG equivalence
2. if x can take NPDA P from state p (w/ empty stack) to q (w/ empty stack), then Ap,q generates string x
– if stack becomes empty only at beginning and end of computation.
• first step: state p to r, read a, push d • go from state r to s, read string y
• last step: state s to q, read b, pop d • conclude: Ap,q → aAr,sb →* ayb = x
January 19, 2024 CS21 Lecture 7
(Ar,s→* y)
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 that
1. for every i ≥0, uvixyiz ∈L , and 2. |vy|>0,and
3. |vxy|≤p.
January 19, 2024 CS21 Lecture 7 27
CFL Pumping
Theorem: the following language is not context-free:
L = {anbncn : n ≥ 0}.
– 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 19, 2024 CS21 Lecture 7 28
CFL Pumping
– 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)
January 19, 2024 CS21 Lecture 7 29
CFL Pumping
– 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.)
January 19, 2024 CS21 Lecture 7 30

CFL Pumping
Theorem: the following language is not context-free:
L = {xx : x ∈ {0,1}*}.
– let p be the pumping length for L – try w = 0p10p1
– can this be pumped?
January 19, 2024 CS21 Lecture 7 31
CFL Pumping
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
• thenuv0xy0z=uxz=02p1i0j12p withi≠2porj≠2p January 19, 2024 CS21 Lecture 7 32
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 that
1. for every i ≥0, uvixyiz ∈L , and 2. |vy|>0,and
3. |vxy|≤p.
January 19, 2024 CS21 Lecture 7 33
CFL Pumping : consider a parse tree for a very long
string w ∈ L:
A D…SCSA A…B aACbADabDCBA
January 19, 2024 CS21 Lecture 7
SS bb some non-terminal must
repeat on long path
CFL Pumping
• Schematic proof:
Auvyz uvxyz
uvAyz January 19, 2024 CS21 Lecture 7 v x y 35
CFL Pumping Lemma
• Schematic proof:
January 19, 2024 CS21 Lecture 7 36

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 19, 2024 CS21 Lecture 7 37
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 19, 2024 CS21 Lecture 7 38
CFL Pumping Lemma
– must be a repeated non- terminal A on long path
– select a repetition among the
lowest m+1 non-terminals on A
– pictures show that for every i
≥ 0, uvixyiz ∈ L
– is |vy| > 0 ?
• smallest parse tree T ensures
– is |vxy| ≤ p?
• red path has length ≤ m+2, so ≤ bm+2 = p leaves
January 19, 2024 CS21 Lecture 7 39

程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com