CSci 4011 INHERENT LIMITATIONS OF COMPUTER PROGRAMS THE PUMPING LEMMA Let L be a regular language with |L| = Then there exists a.
Download ReportTranscript CSci 4011 INHERENT LIMITATIONS OF COMPUTER PROGRAMS THE PUMPING LEMMA Let L be a regular language with |L| = Then there exists a.
Slide 1
CSci 4011
INHERENT LIMITATIONS
OF COMPUTER PROGRAMS
Slide 2
THE PUMPING LEMMA
Let L be a regular language with |L| =
Then there exists a length P such that
∀w L, if |w| ≥ P
then there exist xyz = w where:
1. |y| > 0
2. |xy| ≤ P
3. xyiz L for any i ≥ 0
Slide 3
PUMPING DOWN
C = { 0i1j | i > j ≥ 0}
Assume … pumping length P
Find a w ∈ C longer than P
0P+11P
Show that … w cannot be pumped:
P+1
P
w= 00…0011…11
If w = xyz with |xy| ≤ P then
y = 0J for
y some J ≥ 1.
0z = xz = 0P+1-J1P C
Then
xy
xz = 0…0011…11
xyyz = 00…00011…11
> P+1
P
≤P
P
Slide 4
CHOOSING WISELY
Let BALANCED = { w | w has an equal # of 1s and 0s }
Assume … there is a P
Find a w ∈ BALANCED longer than P
(01)P
0P1P
Show that w cannot be pumped:
If w = xyz with |xy| ≤ P then
y = 0J for some J>0.
Then xyyz = 0P+J1P BALANCED
Slide 5
REUSING A PROOF
Pumping a language can be lots of work…
Let’s try to reuse that work!
{0n1n : n ≥ 0} = BALANCED ∩ 0*1*
If BALANCED is regular then so is {0n1n: n ≥ 0}
Slide 6
USING CLOSURE
Prove: A is not regular
A
∩
C
=
(regular)
B
(Not regular)
or any of {¬, ∪, ∩, ・, R, *}
If A is regular, then A ∩ C (= B) is regular.
But B is not regular so neither is A.
Slide 7
Prove A = {0i1j : i j} is not regular
using B = {0n1n : n ≥ 0}
¬A = B ∪{ strings that mix 0s and 1s }
B = ¬A ∩ 0*1*
Slide 8
PUMPING
NON-REGULAR LANGUAGES
Let F = {aibjck | i, j, k ≥ 0 and i=1 ⇒ j=k}
F has pumping length 2!
i=0
i=1
i=2
F ∩ ab*c* = {abncn | n ≥ 0}
A non-regular language may still satisfy the pumping lemma.
Slide 9
4011 SO FAR…
MODEL OF A PROBLEM: LANGUAGE
MODEL OF A PROGRAM: DFA
EQUIVALENT MODELS: NFA, REGEXP
PROBLEMS THAT A DFA CAN’T SOLVE
ARE WE DONE?
Slide 10
NONE OF THESE ARE REGULAR
Σ = {0, 1}, L = { 0n1n | n ≥ 0 }
Σ = {a, b, c, …, z}, L = { w | w = wR }
Σ = { (, ) }, L = { balanced strings of parens }
We can write a SCHEME or JAVA program for any of them!
Slide 11
PUSHDOWN AUTOMATA
FINITE
STATE
CONTROL
STACK
(Last in,
first out)
INPUT
Slide 12
string
pop
ε,ε → $
0011
push
11
0011
011
0,ε → 0
1,0 → ε
ε,$ → ε
STACK
$
0 0
$ $
1
1,0 → ε
Slide 13
string
pop
ε,ε → $
001
push
1
001
01
0,ε → 0
1,0 → ε
ε,$ → ε
STACK
1,0 → ε
$
0 0
$ $
PDA to recognize L = { 0n1n | n ≥ 0 }
Slide 14
Definition: A (non-deterministic) PDA is a tuple
P = (Q, Σ, Γ, , q0, F), where:
Q is a finite set of states
Σ is the input alphabet
Γ is the stack alphabet
: Q Σε Γε → 𝒫(Q Γε)
q0 Q is the start state
F Q is the set of accept states
𝒫(Q) is the set of subsets of Q and Σε = Σ {ε}
Slide 15
q0
ε,ε → $
0,ε → 0
q1
1,0 → ε
q3
Q = {q0, q1, q2, q3}
ε,$ → ε
1,0 → ε
q2
Σ = {0,1}
Γ = {$,0,1}
: Q Σε Γε → 𝒫(Q Γε)
(q1,1,0) = { (q2,ε) }
(q2,1,1) =
Slide 16
EVEN-LENGTH PALINDROMES
Σ = {a, b, c, …, z}
q0
ε,ε → $
q1
,ε →
ε,ε → ε
q3
ε,$ → ε
q2
, → ε
Slide 17
Build a PDA to recognize
L = { aibjck | i, j, k ≥ 0 and (i = j or i = k) }
c,ε → ε
b,a → ε
q0
ε,$ → ε
q2
q3
ε,ε → $
q1
ε,ε → ε
a,ε → a
q4
b,ε → ε
ε,ε → ε
q5
ε,$ → ε
c,a → ε
q6
Slide 18
Build a PDA to recognize…
L = { x0y | |x| = |y| }
Slide 19
CONTEXT-FREE GRAMMARS
start variable
A → 0A1
A→B
B→#
variables
production
rules
terminals
A 0A1 00A11 00B11 00#11
Slide 20
VALLEY GIRL GRAMMAR
→
→
→ LIKE
→ UMM
→ YOU KNOW
→ ε
→ GAG ME WITH A SPOON
→ AS IF
→ WHATEVER
→ LOSER
Slide 21
VALLEY GIRL GRAMMAR
→ |
→ LIKE | UMM
→ YOU KNOW | ε
→ GAG ME WITH A SPOON | AS IF | WHATEVER
| LOSER
Slide 22
CONTEXT-FREE GRAMMARS
A context-free grammar (CFG) is a tuple G = (V,
Σ, R, S), where:
V is a finite set of variables
Σ is a finite set of terminals (disjoint from V)
R is set of production rules of the form A → W,
where A V and W (VΣ)*
S V is the start variable
G = { {S}, {0,1}, R, S }
R = { S → 0S1, S → ε }
L(G) = { 0n1n | n ≥ 0 }
Slide 23
CONTEXT-FREE GRAMMARS
start variable
A → 0A1
A→B
B→#
variables
production
rules
terminals
A 0A1 00A11 00B11 00#11
uVw yields uvw if (V → v) ∈ R.
A derives 00#11 in 4 steps.
Slide 24
WRITE A CFG FOR EVEN-LENGTH
PALINDROMES
S → S for all Σ
S→ε
Slide 25
WRITE A CFG FOR THE EMPTY SET
G = { {S}, Σ, , S }
Slide 26
GIVE A CFG FOR…
L3 = { strings of balanced parens }
L4 = { aibjck | i, j, k ≥ 0 and (i = j or j = k) }
Slide 27
VERY INTERESTING CFGs…
http://java.sun.com/docs/books/jls/third_edition/html/j3TOC.html
http://unununium.org/darcs/stackless-python/Grammar/Grammar
CSci 4011
INHERENT LIMITATIONS
OF COMPUTER PROGRAMS
Slide 2
THE PUMPING LEMMA
Let L be a regular language with |L| =
Then there exists a length P such that
∀w L, if |w| ≥ P
then there exist xyz = w where:
1. |y| > 0
2. |xy| ≤ P
3. xyiz L for any i ≥ 0
Slide 3
PUMPING DOWN
C = { 0i1j | i > j ≥ 0}
Assume … pumping length P
Find a w ∈ C longer than P
0P+11P
Show that … w cannot be pumped:
P+1
P
w= 00…0011…11
If w = xyz with |xy| ≤ P then
y = 0J for
y some J ≥ 1.
0z = xz = 0P+1-J1P C
Then
xy
xz = 0…0011…11
xyyz = 00…00011…11
> P+1
P
≤P
P
Slide 4
CHOOSING WISELY
Let BALANCED = { w | w has an equal # of 1s and 0s }
Assume … there is a P
Find a w ∈ BALANCED longer than P
(01)P
0P1P
Show that w cannot be pumped:
If w = xyz with |xy| ≤ P then
y = 0J for some J>0.
Then xyyz = 0P+J1P BALANCED
Slide 5
REUSING A PROOF
Pumping a language can be lots of work…
Let’s try to reuse that work!
{0n1n : n ≥ 0} = BALANCED ∩ 0*1*
If BALANCED is regular then so is {0n1n: n ≥ 0}
Slide 6
USING CLOSURE
Prove: A is not regular
A
∩
C
=
(regular)
B
(Not regular)
or any of {¬, ∪, ∩, ・, R, *}
If A is regular, then A ∩ C (= B) is regular.
But B is not regular so neither is A.
Slide 7
Prove A = {0i1j : i j} is not regular
using B = {0n1n : n ≥ 0}
¬A = B ∪{ strings that mix 0s and 1s }
B = ¬A ∩ 0*1*
Slide 8
PUMPING
NON-REGULAR LANGUAGES
Let F = {aibjck | i, j, k ≥ 0 and i=1 ⇒ j=k}
F has pumping length 2!
i=0
i=1
i=2
F ∩ ab*c* = {abncn | n ≥ 0}
A non-regular language may still satisfy the pumping lemma.
Slide 9
4011 SO FAR…
MODEL OF A PROBLEM: LANGUAGE
MODEL OF A PROGRAM: DFA
EQUIVALENT MODELS: NFA, REGEXP
PROBLEMS THAT A DFA CAN’T SOLVE
ARE WE DONE?
Slide 10
NONE OF THESE ARE REGULAR
Σ = {0, 1}, L = { 0n1n | n ≥ 0 }
Σ = {a, b, c, …, z}, L = { w | w = wR }
Σ = { (, ) }, L = { balanced strings of parens }
We can write a SCHEME or JAVA program for any of them!
Slide 11
PUSHDOWN AUTOMATA
FINITE
STATE
CONTROL
STACK
(Last in,
first out)
INPUT
Slide 12
string
pop
ε,ε → $
0011
push
11
0011
011
0,ε → 0
1,0 → ε
ε,$ → ε
STACK
$
0 0
$ $
1
1,0 → ε
Slide 13
string
pop
ε,ε → $
001
push
1
001
01
0,ε → 0
1,0 → ε
ε,$ → ε
STACK
1,0 → ε
$
0 0
$ $
PDA to recognize L = { 0n1n | n ≥ 0 }
Slide 14
Definition: A (non-deterministic) PDA is a tuple
P = (Q, Σ, Γ, , q0, F), where:
Q is a finite set of states
Σ is the input alphabet
Γ is the stack alphabet
: Q Σε Γε → 𝒫(Q Γε)
q0 Q is the start state
F Q is the set of accept states
𝒫(Q) is the set of subsets of Q and Σε = Σ {ε}
Slide 15
q0
ε,ε → $
0,ε → 0
q1
1,0 → ε
q3
Q = {q0, q1, q2, q3}
ε,$ → ε
1,0 → ε
q2
Σ = {0,1}
Γ = {$,0,1}
: Q Σε Γε → 𝒫(Q Γε)
(q1,1,0) = { (q2,ε) }
(q2,1,1) =
Slide 16
EVEN-LENGTH PALINDROMES
Σ = {a, b, c, …, z}
q0
ε,ε → $
q1
,ε →
ε,ε → ε
q3
ε,$ → ε
q2
, → ε
Slide 17
Build a PDA to recognize
L = { aibjck | i, j, k ≥ 0 and (i = j or i = k) }
c,ε → ε
b,a → ε
q0
ε,$ → ε
q2
q3
ε,ε → $
q1
ε,ε → ε
a,ε → a
q4
b,ε → ε
ε,ε → ε
q5
ε,$ → ε
c,a → ε
q6
Slide 18
Build a PDA to recognize…
L = { x0y | |x| = |y| }
Slide 19
CONTEXT-FREE GRAMMARS
start variable
A → 0A1
A→B
B→#
variables
production
rules
terminals
A 0A1 00A11 00B11 00#11
Slide 20
VALLEY GIRL GRAMMAR
Slide 21
VALLEY GIRL GRAMMAR
| LOSER
Slide 22
CONTEXT-FREE GRAMMARS
A context-free grammar (CFG) is a tuple G = (V,
Σ, R, S), where:
V is a finite set of variables
Σ is a finite set of terminals (disjoint from V)
R is set of production rules of the form A → W,
where A V and W (VΣ)*
S V is the start variable
G = { {S}, {0,1}, R, S }
R = { S → 0S1, S → ε }
L(G) = { 0n1n | n ≥ 0 }
Slide 23
CONTEXT-FREE GRAMMARS
start variable
A → 0A1
A→B
B→#
variables
production
rules
terminals
A 0A1 00A11 00B11 00#11
uVw yields uvw if (V → v) ∈ R.
A derives 00#11 in 4 steps.
Slide 24
WRITE A CFG FOR EVEN-LENGTH
PALINDROMES
S → S for all Σ
S→ε
Slide 25
WRITE A CFG FOR THE EMPTY SET
G = { {S}, Σ, , S }
Slide 26
GIVE A CFG FOR…
L3 = { strings of balanced parens }
L4 = { aibjck | i, j, k ≥ 0 and (i = j or j = k) }
Slide 27
VERY INTERESTING CFGs…
http://java.sun.com/docs/books/jls/third_edition/html/j3TOC.html
http://unununium.org/darcs/stackless-python/Grammar/Grammar