Professional Documents
Culture Documents
Motivation: Introduction To Theoretical Computer Science Finite Automata
Motivation: Introduction To Theoretical Computer Science Finite Automata
• NP-hardness = what cannot be efficiently • Software for verifying finite state systems,
computed. such as communication protocols.
1 2
Structural Representations
5 6
Σ∗ = Σ0 ∪ Σ1 ∪ Σ2 ∪ · · ·
Also: Languages:
Σ+ = Σ1 ∪ Σ2 ∪ Σ3 ∪ · · · If Σ is an alphabet, and L ⊆ Σ∗
then L is a language
Σ∗ = Σ+ ∪ {}
Examples of languages:
Concatenation: If x and y are strings, then
xy is the string obtained by placing a copy of
y immediately after a copy of x • The set of legal English words
1 3 4
4. The store can redeem the money (=cash redeem transfer
the check)
Start Start
11 12
Completed protocols:
13 14
δ 0 1
• Q is a finite set of states → q0 q2 q0
?q1 q1 q1
• Σ is a finite alphabet (=input symbols) q2 q2 q1
15 16
An FA accepts a string w = a1a2 . . . an if there
is a path in the transition diagram that
• The transition function δ can be extended
to δ̂ that operates on states and strings (as
1. Begins at a start state opposed to states and symbols)
Basis: δ̂(q, ) = q
2. Ends at an accepting state
17 18
0 x1 x3
0
q2 1 q3
1
x2
δ 0 1
? → q0 q2 q1
q1 q3 q0 C D
q2 q0 q3
q3 q1 q2
19 20
A state is represented as sequence of three bits Nondeterministic Finite Automata
followed by r or a (previous input rejected or
accepted) A NFA can be in several states at once, or,
viewded another way, it can “guess” which
For instance, 010a, means state to go to next
left, right, left, accepted
Example: An automaton that accepts all and
Tabular representation of DFA for the toy only strings ending in 01.
0, 1
A B
Start q0 0 q1 1 q2
→ 000r 100r 011r
?000a 100r 011r
?001a 101r 000a
010r 110r 001a Here is what happens when the NFA processes
?010a 110r 001a the input 00101
011r 111r 010a q0 q0 q0 q0 q0 q0
100r 010r 111r
?100a 010r 111r
q1 q1 q1
101r 011r 100a
(stuck)
?101a 011r 100a
110r 000a 101a q2 q2
?110a 000a 101a (stuck)
111r 001a 110a 0 0 1 0 1
21 22
• Q is a finite set of states ({q0, q1, q2}, {0, 1}, δ, q0, {q2})
23 24
Let’s prove formally that the NFA
Extended transition function δ̂.
0, 1
Basis: δ̂(q, ) = {q} Start 0 1
q0 q1 q2
Induction:
[ accepts the language {x01 : x ∈ Σ∗}. We’ll do
δ̂(q, xa) = δ(p, a)
p∈δ̂(q,x)
a mutual induction on the three statements
below
25 26
D = (QD , Σ, δD , {q0}, FD )
such that
L(D) = L(N )
.
27 28
The details of the subset construction:
29 30
31 32
Induction:
def
δ̂D ({q0}, xa) = δD (δ̂D ({q0}, x), a)
Theorem 2.11: Let D be the “subset” DFA
of an NFA N . Then L(D) = L(N ). i.h.
= δD (δ̂N (q0, x), a)
Proof: First we show on an induction on |w|
cst [
that = δN (p, a)
p∈δ̂N (q0 ,x)
δ̂D ({q0}, w) = δ̂N (q0, w)
def
= δ̂N (q0, xa)
Basis: w = . The claim follows from def.
33 34
Exponential Blow-Up
Theorem 2.12: A language L is accepted by There is an NFA N with n + 1 states that has
some DFA if and only if L is accepted by some no equivalent DFA with fewer than 2n states
NFA. 0, 1
1 0, 1 0, 1 0, 1 0, 1
Proof: The “if” part is Theorem 2.11. q0 q1 q2 qn
Start
1a2 . . . an
0b2 . . . bn 1. An optional + or - sign
a1 . . . ai−11ai+1 . . . an
b1 . . . bi−10bi+1 . . . bn One of the strings (2) are (4) are optional
37 38
39 40
• Inductive definition of δ̂ for -NFA’s
1 b δ̂(q, ) = ECLOSE(q)
ε
4 5 7 Induction:
a ε
For instance,
[
δ̂(q, xa) = ECLOSE(p)
p∈δ(δ̂(q,x),a)
ECLOSE(1) = {1, 2, 3, 4, 6}
Let’s compute on the blackboard in class
δ̂(q0, 5.6) for the NFA on slide 38
41 42
0,1,...,9 0,1,...,9
• QD = {S : S ⊆ QE and S = ECLOSE(S)}
+,-
{q0, q } {q } {q , q } {q2, q3, q5}
• qD = ECLOSE(q0) 1 1 0,1,...,9 1 4 .
. 0,1,...,9
• FD = {S : S ∈ QD and S ∩ FE 6= ∅} .
Start {q2} {q , q5}
3
• δD (S, a) = 0,1,...,9
[
{ECLOSE(p) : p ∈ δ(t, a) for some t ∈ S}
0,1,...,9
43 44
Induction:
[
Theorem 2.22: A language L is accepted by δ̂E (q0, xa) = ECLOSE(p)
some -NFA E if and only if L is accepted by p∈δE (δ̂E (q0 ,x),a)
some DFA.
[
= ECLOSE(p)
Proof: We use D constructed as above and p∈δD (δ̂D (qD ,x),a)
show by induction that δ̂D (q0, w) = δ̂E (qD , w)
[
= ECLOSE(p)
p∈δ̂D (qD ,xa)
Basis: δ̂E (q0, ) = ECLOSE(q0) = qD = δ̂(qD , )
= δ̂D (qD , xa)
45 46
Operations on languages
Regular expressions
Union:
A FA (NFA or DFA) is a “blueprint” for con-
tructing a machine recognizing a regular lan-
L ∪ M = {w : w ∈ L or w ∈ M }
guage.
Concatenation:
A regular expression is a “user-friendly,” declar-
ative way of describing a regular language.
L.M = {w : w = xy, x ∈ L, y ∈ M }
∞
[
L∗ = Li
2. UNIX Lex (Lexical analyzer generator) and
i=0
Flex (Fast Lex) tools.
Question: What are ∅0, ∅i, and ∅∗
47 48
Building regex’s
Example: Regex for
Inductive definition of regex’s:
L = {w ∈ {0, 1}∗ : 0 and 1 alternate in w}
Basis: is a regex and ∅ is a regex.
L() = {}, and L(∅) = ∅.
(k)
• Let Rij be a regex describing the set of
RE DFA
labels of all paths in A from state i to state
j going through intermediate states {1, . . . , k}
only.
To show FA’s equivalent to regex’s we need to
establish that i
j
k
1. For every DFA A we can find (construct,
in this case) a regex R, s.t. L(R) = L(A).
51 52
(k)
Rij will be defined inductively. Note that
M Induction:
L R1j (n) = L(A)
j∈F
(k)
Rij
Basis: k = 0, i.e. no intermediate states.
=
• Case 1: i 6= j
(k−1)
Rij
M +
(0)
Rij = a
(k−1) (k−1) ∗ (k−1)
{a∈Σ:δ(i,a)=j} Rik Rkk Rkj
• Case 2: i = j
i k k k k j
M In R (k-1) In R (k-1)
(0)
a
ik Zero or more strings in R (k-1) kj
Rii = + kk
{a∈Σ:δ(i,a)=i}
53 54
• R + RS ∗ = RS ∗
(0)
R11 +1 • ∅R = R∅ = ∅ (Annihilation)
(0)
R12 0
• ∅ + R = R + ∅ = R (Identity)
(0)
R21 ∅
(0)
R22 +0+1
55 56
Simplified
(0)
R11 +1 (1)
R11 1∗
(0)
R12 0 (1)
R12 1∗ 0
(0)
R21 ∅ (1)
R21 ∅
(0)
R22 +0+1 (1)
R22 +0+1
(1) (0) (0) (0) ∗ (0)
Rij = Rij + Ri1 R11 R1j (2) (1) (1) (1) ∗ (1)
Rij = Rij + Ri2 R22 R2j
57 58
By direct substitution
(2) Observations
R11 1∗ + 1∗0( + 0 + 1)∗∅
(2)
R12 1∗0 + 1∗0( + 0 + 1)∗( + 0 + 1)
(k)
(2) There are n3 expressions Rij
R21 ∅ + ( + 0 + 1)( + 0 + 1)∗∅
(2)
R22 + 0 + 1 + ( + 0 + 1)( + 0 + 1)∗( + 0 + 1) Each inductive step grows the expression 4-fold
Simplified
(n)
(2) Rij could have size 4n
R11 1∗
(2)
R12 1∗0(0 + 1)∗ (k) (k−1)
For all {i, j} ⊆ {1, . . . , n}, Rij uses Rkk
(2)
R21 ∅ so we have to write n2 times the regex Rkk
(k−1)
(2)
R22 (0 + 1)∗
We need a more efficient approach:
The final regex for A is the state elimination technique
(2)
R12 = 1∗0(0 + 1)∗
59 60
The state elimination technique
Let’s label the edges with regex’s instead of Now, let’s eliminate state s.
symbols
R 11 + Q 1 S* P1
R 1m q1 p1
R 1m + Q 1 S* Pm
R 11
q1 p1
Q1
S P1
R k1 + Q k S* P1
s
qk pm
Pm R km + Q k S* Pm
Qk
qk pm
R km For each accepting state q eliminate from the
original automaton all states exept q0 and q.
R k1
61 62
R U
S
Start Example: A, where L(A) = {W : w = x1b, or w =
x1bc, x ∈ {0, 1}∗, {b, c} ⊆ {0, 1}}
T
0,1
that corresponds to the regex Eq = (R+SU ∗T )∗SU ∗ Start 1 0,1 0,1
A B C D
or with Aq looking like
0+1
63 64
0+1 From
Start 1 0+1 0+1
0+1
A B C D
Start 1( 0 + 1) 0+1
A C D
Let’s eliminate state B
Start 1( 0 + 1)
A C
Then we eliminate state C and obtain AD
65 66
ε ε
Proof: By structural induction: S
(a)
Basis: Automata for , ∅, and a.
ε ε
R S
(a)
(b)
ε
(b) ε ε
R
ε
(c) (c)
67 68
Example: We convert (0 + 1)∗1(0 + 1)
ε ε
1 • L ∪ M = M ∪ L.
(a)
ε
0
Union is commutative.
ε ε
ε ε
ε ε ε • (L ∪ M ) ∪ N = L ∪ (M ∪ N ).
1
69 70
• L(M ∪ N ) = LM ∪ LN .
• ∅L = L∅ = ∅. Union is idempotent.
71 72
Algebraic Laws for regex’s
• (L∗)∗ = L∗. Closure is idempotent Evidently e.g. L((0 + 1)1) = L(01 + 11)
⇐⇒ w ∈ L∗ 1. Prove it by hand.
73 74
75 76
For example: Suppose the alphabet is {1, 2}.
Theorem 3.13: Fix a “freezing” substitution Let E(E1, E2) be (E1 + E2)E1, and let E1 be 1,
♠ = {E1/a1, E2/a2, . . . , Em/am}. and E2 be 2. Then
Let E(E1, E2, . . . , Em) be a generalized regex. w ∈ L(E(E1, E2)) = L((E1 + E2)E1) =
Then for any regex’s E1, E2, . . . , Em,
({1} ∪ {2}){1} = {11, 21}
w ∈ L(E(E1, E2, . . . , Em)) if and only if
if and only if there are strings wi ∈ L(Ei), s.t.
∃w1 ∈ L(E1) = {1}, ∃w2 ∈ L(E2) = {2} : w = wj1 wj2
w = wj1 wj2 · · · wjk and
and
aj1 aj2 ∈ L(E(a1, a2))) = L((a1+a2)a1) = {a1a1, a2a1}
aj1 aj2 · · · ajk ∈ L(E(a1, a2, . . . , am)) if and only if
j1 = j2 = 1, or j1 = 1, and j2 = 2
77 78
Induction:
Case 1: E = F + G.
Case 3: E = F∗.
Proof:
w ∈ L(F(E1, . . . Em))
81 82
Proof:
w ∈ L(F(E1, . . . Em))
83 84
The Pumping Lemma Informally
Properties of Regular Languages
Suppose L01 = {0n1n : n ≥ 1} were regular.
• Pumping Lemma. Every regular language
satisfies the pumping lemma. If somebody Then it would be recognized by some DFA A,
presents you with fake regular language, use with, say, k states.
the pumping lemma to show a contradiction.
Let A read 0k . On the way it will travel as
• Closure properties. Building automata from follows:
components through operations, e.g. given L
and M we can build an automaton for L ∩ M . p0
0 p1
• Decision properties. Computational analysis 00 p2
of automata, e.g. are two automata equiva- ... ...
lent. 0k pk
85 86
Theorem 4.1.
Now you can fool A:
The Pumping Lemma for Regular Languages.
87 88
Now w = xyz, where
1. x = a1a2 . . . ai
Proof: Suppose L is regular
3. z = aj+1aj+2 . . . am
Let w = a1a2 . . . am ∈ L, m > n.
y=
ai+1 . . . aj
Let pi = δ̂(q0, a1a2 . . . ai).
x= z=
Start a1 . . . ai aj+1 . . . am
⇒ ∃ i < j : pi = pj p0 pi
89 90
• Inverse homomorphism:
h−1(L) = {w ∈ Σ : h(w) ∈ L, h : Σ → ∆ is a homom.}
93 94
Example:
1 0
1 0
We shall shall also give a nice direct proof, the
Start Cartesian construction from the e-commerce
0 1
{q0} {q0, q1} {q0, q2} example.
AM = (QM , Σ, δM , qM , FM )
AL
We assume w.l.o.g. that both automata are
deterministic.
Start AND Accept
97 98
Formally 1
Start 0 0,1
AL∩M = (QL × QM , Σ, δL∩M , (qL, qM ), FL × FM ), p q
where (a)
0
δL∩M ((p, q), a) = (δL(p, a), δM (q, a)) 0,1
Start 1
r s
on |w| that 1
Start 1
δ̂L∩M ((qL, qM ), w) = δ̂L(qL, w), δ̂M (qM , w) pr ps
0 0
The claim then follows.
0,1
qr qs
Question: Why? 1
0
(c)
99 100
Theorem 4.11. If L is a regular language,
then so is LR .
101 102
Homomorphisms
Proof 2: Let L be described by a regex E.
We shall construct a regex E R , such that
L(E R ) = (L(E))R . A homomorphism on Σ is a function h : Σ → Θ∗,
where Σ and Θ are alphabets.
We proceed by a structural induction on E.
Let w = a1a2 · · · an ∈ Σ∗. Then
Basis: If E is , ∅, or a, then E R = E.
h(w) = h(a1)h(a2) · · · h(an)
Induction: and
1. E = F + G. Then E R = F R + GR h(L) = {h(w) : w ∈ L}
2. E = F.G. Then E R = GR .F R
Example: Let h : {0, 1} → {a, b}∗ be defined by
3. E = F ∗. Then E R = (F R )∗ h(0) = ab, and h(1) = . Now h(0011) = abab.
L(E R ) = (L(E))R
103 104
Theorem 4.14: h(L) is regular, whenever L
is. Inverse Homomorphism
Proof:
Let h : Σ → Θ∗ be a homom. Let L ⊆ Θ∗, and
Let L = L(E) for a regex E. We claim that define
L(h(E)) = h(L).
h−1(L) = {w ∈ Σ∗ : h(w) ∈ L}
Basis: If E is or ∅. Then h(E) = E, and
L(h(E)) = L(E) = h(L(E)).
Induction:
(a)
= h(L(E)).h(L(F )) = h(L(E).L(F ))
/ L((ba)∗). There
Let h(w) ∈ L, and suppose w ∈ It will be shown by induction on |w| in the tu-
are four cases to consider. torial that γ̂(q0, w) = δ̂(q0, h(w))
107 108
Decision Properties
From NFA’s to DFA’s
109 110
All we need to do is to put set brackets around L(A) 6= ∅ for FA A if and only if a final state
the states. Total O(n) steps. is reachable from the start state in A. Total
O(n2) steps.
From FA to regex
Alternatively, we can inspect a regex E and tell
We need to compute n3 entries of size up to if L(E) = ∅. We use the following method:
4n. Total is O(n34n).
E = F + G. Now L(E) is empty if and only if
The FA is allowed to be a NFA. If we first both L(F ) and L(G) are empty.
wanted to convert the NFA to a DFA, the total
time would be doubly exponential E = F.G. Now L(E) is empty if and only if
either L(F ) or L(G) is empty.
From regex to FA’s We can build an expres-
E = F ∗. Now L(E) is never empty, since ∈
sion tree for the regex in n steps.
L(E).
We can construct the automaton in n steps.
E = . Now L(E) is not empty.
Eliminating -transitions takes O(n3) steps.
E = a. Now L(E) is not empty.
If you want a DFA, you might need an expo-
nential number of steps. E = ∅. Now L(E) is empty.
111 112
Example 4.17
Inverse homomorphism
L2
convert E to an -NFA with 2s states. Then we
Add condition that first state is the start state
Homomorphism
113 114
• M L1
115 116
• L2 L3
Define
M
E2 = [paq][rbs]
• L1 L2 [paq]∈T ∗ , [rbs]∈T ∗ , q6=r
Define Let
M
E1 = [q0ap] L3 = L2 \ T ∗.L(E2).T ∗
a∈Σ, δ(q0 ,a)=p
pn ∈ F
117 118
• L3 L4
Define • L4 L
M
Eq = [ras]
We only need to get rid of the state compo-
[ras]∈T ∗ , r6=q, s6=q
nents in the words of L4.
119 120
Equivalence and Minimization of Automata
Example:
0 1
• If p ≡ q we say that p and q are equivalent 1 1 0
E F G H
1 0
• If p 6≡ q we say that p and q are distinguish-
able 0
121 122
0 1
Induction: If ∃a ∈ Σ : δ(p, a) 6≡ δ(q, a),
E
1
F
1
G
0
H
then p 6≡ q.
1 0
Example:
0
Applying the table filling algo to DFA A:
δ̂(A, ) = A ∈
/ F, δ̂(E, ) = E ∈
/F
B x
δ̂(A, 1) = F = δ̂(E, 1) C x x
D x x x
Therefore δ̂(A, 1x) = δ̂(E, 1x) = δ̂(F, x) E x x x
F x x x x
δ̂(A, 00) = G = δ̂(E, 00)
G x x x x x x
A B C D E F G
Conclusion: A ≡ E.
123 124
Theorem 4.20: If p and q are not distin-
guished by the TF-algo, then p ≡ q.
2. The TF-algo does not distinguish between But then the TF-algo would distinguish p from
p and q. q in the inductive part.
Let w = a1a2 · · · an be the shortest string that Thus there are no bad pairs and the theorem
identifies a bad pair {p, q}. is true.
125 126
Example:
Testing Equivalence of Regular Languages 0 1
Start 1
A B
Let L and M be reg langs (each given in some 0
0
form).
Start 0
C D
To test if L = M 1 0
1
We can use the TF-algo to minimize a DFA Proof: Suppose to the contrary that p 6≡ r.
by merging all equivalent states. IOW, replace Then ∃w such that δ̂(p, w) ∈ F and δ̂(r, w) 6∈ F ,
each state p by p/≡ . or vice versa.
Example: The DFA on slide 119 has equiva- OTH, δ̂(q, w) is either accpeting or not.
lence classes {{A, E}, {B, H}, {C}, {D, F }, {G}}.
Case 1: δ̂(q, w) is accepting. Then q 6≡ r.
The “union” DFA on slide 125 has equivalence
classes {{A, C, D}, {B, E}}. Case 1: δ̂(q, w) is not accepting. Then p 6≡ q.
Note: In order for p/≡ to be an equivalence The vice versa case is proved symmetrically
class, the relation ≡ has to be an equivalence
relation (reflexive, symmetric, and transitive). Therefore it must be that p ≡ r.
129 130
0 1
Start 0 1 0
A B C D
0
In order for B to be well defined we have to
show that
to obtain
If p ≡ q then δ(p, a) ≡ δ(q, a)
1
If δ(p, a) 6≡ δ(q, a), then the TF-algo would con-
0
clude p 6≡ q, so B is indeed well defined. Note G
1
D,F
1
also that F/≡ contains all and only the accept-
Start
ing states of A. A,E 0 0
1
B,H C
1
0
131 132
NOTE: We cannot apply the TF-algo to NFA’s. Why the Minimized DFA Can’t Be Beaten
C
Then run the TF-algo on B “union” C.
133 134
Proof of claim: There are no inaccessible states, • Contex-Free Languages (CFL’s) played a cen-
so p = δ̂(q0B , a1a2 · · · ak ), for some string a1a2 · · · ak . tral role natural languages since the 1950’s,
Now q = δ̂(q0C , a1a2 · · · ak ), and p ≡ q. and in compilers since the 1960’s.
Since C has fewer states than B, there must be • Context-Free Grammars (CFG’s) are the ba-
two states r and s of B such that r ≡ t ≡ s, for sis of BNF-syntax.
some state t of C. But then r ≡ s (why?)
which is a contradiction, since B was con- • Today CFL’s are increasingly important for
structed by the TF-algo. XML and their DTD’s.
139 140
Example: (simple) expressions in a typical prog
lang. Operators are + and *, and arguments Derivations using grammars
are identfiers, i.e. strings in
L((a + b)(a + b + 0 + 1)∗) • Recursive inference, using productions from
body to head
The expressions are defined by the grammar
G = ({E, I}, T, P, E) • Derivations, using productions from head to
where T = {+, ∗, (, ), a, b, 0, 1} and P is the fol- body.
lowing set of productions:
Example of recursive inference:
1. E →I
2. E →E+E String Lang Prod String(s) used
3. E →E∗E (i) a I 5 -
4. E → (E) (ii) b I 6 -
(iii) b0 I 9 (ii)
5. I →a (iv) b00 I 9 (iii)
6. I →b (v) a E 1 (i)
7. I → Ia (vi) b00 E 1 (iv)
8. I → Ib (vii) a + b00 E 2 (v), (vi)
(viii) (a + b00) E 4 (vii)
9. I → I0
(ix) a ∗ (a + b00) E 3 (v), (viii)
10. I → I1
141 142
or, if G is understood
αAβ ⇒ αγβ
Note: At each step we might have several rules
and say that αAβ derives αγβ. to choose from, e.g.
I ∗ E ⇒ a ∗ E ⇒ a ∗ (E), versus
∗
We define ⇒ to be the reflexive and transitive I ∗ E ⇒ I ∗ (E) ⇒ a ∗ (E).
closure of ⇒, IOW:
Note: Not all choices lead to successful deriva-
∗
Basis: Let α ∈ (V ∪ T )∗. Then α ⇒ α. tions of a particular string, for instance
∗ ∗ E ⇒E+E
Induction: If α ⇒ β, and β ⇒ γ, then α ⇒ γ.
won’t lead to a derivation of a ∗ (a + b00).
143 144
Leftmost and Rightmost Derivations The Language of a Grammar
Leftmost derivation ⇒ Always replace the left- If G(V, T, P, S) is a CFG, then the language of
lm
most variable by one of its rule-bodies. G is
∗
L(G) = {w ∈ T ∗ : S ⇒ w}
Rightmost derivation ⇒ Always replace the G
rm
rightmost variable by one of its rule-bodies. i.e. the set of strings over T ∗ derivable from
the start symbol.
Leftmost: The derivation on the previous slide.
If G is a CFG, we call L(G) a context-free lan-
Rightmost: guage.
E ⇒E∗E ⇒
rm rm
Example: L(Gpal ) is a context-free language.
E∗(E) ⇒ E∗(E+E) ⇒ E∗(E+I) ⇒ E∗(E+I0)
rm rm rm
Theorem 5.7:
⇒ E ∗ (E + I00) ⇒ E ∗ (E + b00) ⇒ E ∗ (I + b00)
rm rm rm
L(Gpal ) = {w ∈ {0, 1}∗ : w = wR }
⇒ E ∗ (a + b00) ⇒ I ∗ (a + b00) ⇒ a ∗ (a + b00)
rm rm rm
145 146
Thus w ∈ L(Gpal ). or
∗
w = 1x1 ⇐ 1P 1 ⇐ P
The case for w = 1x1 is similar. where the second derivation is done in n steps.
Let G = (V, T, P, S) be a CFG, and α ∈ (V ∪T )∗. E ⇒ E ∗E ⇒ E ∗(E) ⇒ E ∗(E +E) ⇒ E ∗(I +E)
If This derivation is neither leftmost, nor right-
∗ most
S⇒α
we say that α is a sentential form.
Example: a ∗ E is a left-sentential form, since
149 150
Parse Trees
Rightmost
Derivation derivation Recursive w
inference
157 158
w1 w2 ... wk
This is why CFG’s are called context-free.
159 160
Theorem 5.14: Let G = (V, T, P, S) be a Induction: Height is n + 1. The tree must
CFG, and suppose there is a parse tree with look like
∗
root labelled A and yield w. Then A ⇒ w in G.
lm A
A
Then w = w1w2 · · · wk , where
w 1. If Xi ∈ T , then wi = Xi.
∗
2. If Xi ∈ V , then Xi ⇒ wi in G by the IH.
Consequently A → w ∈ P , and A ⇒ w. lm
lm
161 162
A ⇒ X1Xi+2 · · · Xk .
lm
w1w2 · · · wi−1XiXi+1 · · · Xk ⇒
lm
Induction: Make the IH that
∗ w1w2 · · · wi−1α1Xi+1 · · · Xk ⇒
A ⇒ w1w2 · · · wi−1XiXi+1 · · · Xk . lm
lm
w1w2 · · · wi−1α2Xi+1 · · · Xk ⇒
lm
(Case 1:) Xi ∈ T . Do nothing, since Xi = wi
gives us ···
∗
A ⇒ w1w2 · · · wiXi+1 · · · Xk .
lm w1w2 · · · wi−1wiXi+1 · · · Xk
163 164
Example: Let’s construct the leftmost deriva-
For the derivation corresponding to the whole
tion for the tree
E
tree we start with E ⇒ E ∗ E and expand the
lm
E * E first E with the first derivation and the second
I ( E ) E with the second derivation:
a E + E
I I
E⇒
lm
a I 0 E∗E ⇒
lm
I 0 I ∗E ⇒
lm
b
a∗E ⇒
lm
Suppose we have inductively constructed the
a ∗ (E) ⇒
leftmost derivation lm
a ∗ (E + E) ⇒
E⇒I⇒a lm
lm lm
a ∗ (I + E) ⇒
lm
∗
Observation: Suppose that A ⇒ X1X2 · · · Xk ⇒ w.
∗
Then w = w1w2 · · · wk , where Xi ⇒ wi
Theorem 5.18: Let G = (V, T, P, S) be a
∗ ∗
The factor wi can be extracted from A ⇒ w by CFG. Suppose A ⇒ w, and that w is a string
G
looking at the expansion of Xi only. of terminals. Then we can infer that w is in
the language of variable A.
Example: E ⇒ a ∗ b + a, and
E ⇒ |{z} ∗ |{z}
E |{z} + |{z}
E |{z} E Proof: We do an induction on the length of
X1 X2 X3 X4 X5 ∗
the derivation A ⇒ w.
G
In the grammar
∗
Induction: Suppose A ⇒ w in n + 1 steps. 1. E →I
G
Write the derivation as 2. E →E+E
∗ 3. E →E∗E
A ⇒ X1X2 · · · Xk ⇒ w
G G 4. E → (E)
The as noted on the previous slide we can ···
∗
break w as w1w2 · · · wk where Xi ⇒ wi. Fur- the sentential form E + E ∗ E has two deriva-
G
∗ tions:
thermore, Xi ⇒ wi can use at most n steps.
G
E ⇒E+E ⇒E+E∗E
Now we have a production A → X1X2 · · · Xk , and
E ⇒E∗E ⇒E+E∗E
and we know by the IH that we can infer wi to
be in the language of Xi. This gives us two parse trees:
E E
Therefore we can infer w1w2 · · · wk to be in the
language of A. E + E E * E
E * E E + E
(a) (b)
169 170
The mere existence of several derivations is not Definition: Let G = (V, T, P, S) be a CFG. We
dangerous, it is the existence of several parse say that G is ambiguous is there is a string in
trees that ruins a grammar. T ∗ that has more than one parse tree.
Example: In the same grammar If every string in L(G) has at most one parse
5. I → a tree, G is said to be unambiguous.
6. I → b
Example: The terminal string a + a ∗ a has two
7. I → Ia
parse trees:
8. I → Ib
9. I → I0 E E
10. I → I1
E + E E * E
the string a + b has several derivations, e.g.
and
a I I I I a
E ⇒E+E ⇒E+I ⇒I +I ⇒I +b⇒a+b
a a a a
However, their parse trees are the same, and
the structure of a + b is unambiguous. (a) (b)
171 172
Removing Ambiguity From Grammars Solution: We introduce more variables, each
representing expressions of same “binding strength.”
Good news: Sometimes we can remove ambi-
guity “by hand”
1. A factor is an expresson that cannot be
Bad news: There is no algorithm to do it broken apart by an adjacent * or +. Our
factors are
More bad news: Some CFL’s have only am-
biguous CFG’s (a) Identifiers
173 174
T T * F
• An expression is a sequence
F F I t1 + t2 + · · · + tn−1 + tn
of terms ti. It can only be parsed with
I I a
t1 + t2 + · · · + tn−1 as an expression and tn as
a term.
a a
175 176
Leftmost derivations and Ambiguity
T E + E E * E
T * F I E * E E + E I
T * F a I I I I a
.
. .
T a a a a
(a) (b)
T * F
give rise to two derivations:
F E ⇒E+E ⇒I +E ⇒a+E ⇒a+E∗E
lm lm lm lm
⇒a+I ∗E ⇒a+a∗E ⇒a+a∗I ⇒a+a∗a
lm lm lm lm
and
E ⇒ E ∗E ⇒ E +E ∗E ⇒ I +E ∗E ⇒ a+E ∗E
lm lm lm lm
⇒a+I ∗E ⇒a+a∗E ⇒a+a∗I ⇒a+a∗a
lm lm lm lm
177 178
In General:
Sketch of Proof: (Only If.) If the two parse
• One parse tree, but many derivations trees differ, they have a node a which dif-
ferent productions, say A → X1X2 · · · Xk and
• Many leftmost derivation implies many parse B → Y1Y2 · · · Ym. The corresponding leftmost
trees. derivations will use derivations based on these
two different productions and will thus be dis-
• Many rightmost derivation implies many parse tinct.
trees.
(If.) Let’s look at how we construct a parse
Theorem 5.29: For any CFG G, a terminal tree from a leftmost derivation. It should now
string w has two distinct parse trees if and only be clear that two distinct derivations gives rise
if w has two distinct leftmost derivations from to two different parse trees.
the start symbol.
179 180
Inherent Ambiguity
Let’s look at parsing the string aabbccdd.
A CFL L is inherently ambiguous if all gram-
mars for L are ambiguous.
S S
Example: Consider L = A B C
A grammar for L is a b c d a D d
S → AB | C
b D c
A → aAb | ab
B → cBd | cd b c
C → aCd | aDd
(a) (b)
D → bDc | bc
181 182
Pushdown Automata
S ⇒ C ⇒ aCd ⇒ aaDdd ⇒ aabDcdd ⇒ aabbccdd 3. Replaces the top of the stack by any string
lm lm lm lm lm
(does nothing, pops the stack, or pushes a
string onto the stack)
It can be shown that every grammar for L be-
haves like the one above. The language L is
inherently ambiguous. Finite
Input state Accept/reject
control
Stack
183 184
Example: Let’s consider
185 186
0 , Z 0 /0 Z 0
A PDA is a seven-tuple: 1 , Z 0 /1 Z 0
0 , 0 /0 0
0 , 1 /0 1
P = (Q, Σ, Γ, δ, q0, Z0, F ), 1 , 0 /1 0 0, 0/ ε
1 , 1 /1 1 1, 1/ ε
where
Start
q0 q q2
• Q is a finite set of states, ε, Z 0 / Z 0
1
ε , Z 0 /Z 0
ε, 0 / 0
• Σ is a finite input alphabet, ε, 1 / 1
187 188
Instantaneous Descriptions
∗
We define ` to be the reflexive-transitive clo-
sure of `.
189 190
191 192
Acceptance by final state
Theorem 6.5: ∀w ∈ Σ∗, β ∈ Γ∗ :
∗ ∗ Let P = (Q, Σ, Γ, δ, q0, Z0, F ) be a PDA. The
(q, x, α) ` (p, y, β) ⇒ (q, xw, αγ) ` (p, yw, βγ). language accepted by P by final state is
∗
L(P ) = {w : (q0, w, Z0) ` (q, , α), q ∈ F }.
Proof: Induction on the length of the sequence
to the left.
Example: The PDA on slide 183 accepts ex-
Note: If γ = we have proerty 1, and if w = actly Lwwr .
we have property 2.
Let P be the machine. We prove that L(P ) =
Note2: The reverse of the theorem is false. Lwwr .
193 194
We’ll show by induction on |x| that (q0, a1a2 . . . an, α) ` (q0, a2 . . . an, a1α) ` . . . `
∗ (q1, an, a1α) ` (q1, , α).
(q0, x, α) ` (q1, , α) ⇒ x = wwR .
Thus a1 = an and
Basis: If x = then x is a palindrome. ∗
(q0, a2 . . . an, a1α) ` (q1, an, a1α).
Induction: Suppose x = a1a2 . . . an, where n > 0, By Theorem 6.6 we can remove an. Therefore
and the IH holds for shorter strings. ∗
(q0, a2 . . . an−1, a1α) ` (q1, , a1α).
Ther are two moves for the PDA from ID (q0, x, α): Then, by the IH a2 . . . an−1 = yy R . Then x =
a1yy R an is a palindrome.
195 196
From Empty Stack to Final State
ε, X 0 / ε
ε, X 0 / ε
197 198
Since δN ⊂ δF we have
Start
∗ q
(q0, w, Z0X0) `
F
(q, , X0).
We conclude that
Formally,
∗
(p0, w, X0) `
F
(q0, w, Z0X0) `
F
(q, , X0) `
F
(pf , , ).
PN = ({q}, {i, e}, {Z}, δN , q, Z),
where δN (q, i, Z) = {(q, ZZ)},
(⊆direction.) By inspecting the diagram.
and δN (q, e, Z) = {(q, )}.
199 200
From Final State to Empty Stack
Start ε, X 0/ZX 0 ε, X 0 / ε
p q r ε, any/ ε ε, any/ ε
Start ε, X 0 / Z 0X 0 PF
p0 q0 p
ε, any/ ε
201 202
A language is
We have to show that N (PN ) = L(PF ).
generated by a CFG
(⊆-direction.) By inspecting the diagram.
if and only if it is
(⊇-direction.) Let w ∈ L(PF ). Then
∗
(q0, w, Z0) `
F
(q, , α), accepted by a PDA by empty stack
for some q ∈ F, α ∈ Γ∗. Since δF ⊂ δN , and
Theorem 6.5 says that X0 can be slid under if and only if it is
the stack, we get
∗ accepted by a PDA by final state
(q0, w, Z0X0) `
N
(q, , αX0).
Then PN can compute: PDA by PDA by
Grammar
∗ ∗ empty stack final state
(p0, w, X0) `
N
(q0, w, Z0X0) `
N
(q, , αX0) `
N
(p, , ).
Let xAα ⇒ xβα. This corresponds to the PDA δ(q, , A) = {(q, β) : A → β ∈ Q},
lm
first having consumed x and having Aα on the for A ∈ V , and
stack, and then on it pops A and pushes β.
δ(q, a, a) = {(q, )},
More fomally, let y, s.t. w = xy. Then the PDA for a ∈ T .
goes non-deterministically from configuration
(q, y, Aα) to configuration (q, y, βα). Example: On blackboard in class.
205 206
209 210
e, Z/ ε
i, Z/ZZ
213 214
Example: Let P = ({p, q}, {0, 1}, {X, Z0}, δ, q, Z0), We get G = (V, {0, 1}, R, S), where
where δ is given by
V = {[pXp], [pXq], [pZ0p], [pZ0q], S}
and the productions in R are
1. δ(q, 1, Z0) = {(q, XZ0)}
S → [qZ0q]|[qZ0p]
2. δ(q, 1, X) = {(q, XX)}
From rule (1):
[qXq] → 1[qXq][qXq]
6. δ(p, 0, Z0) = {(q, Z0)}
[qXq] → 1[qXp][pXq]
[qXp] → 1[qXq][qXp]
to a CFG. [qXp] → 1[qXp][pXp]
215 216
From rule (3):
Proof:
From rule (4):
217 218
219 220
(⊇-direction.) We shall show by an induction
∗
on the length of the derivation ⇒ that
∗ ∗
(♥) If [qXp] ⇒ w then (q, w, X) ` (p, , )
From Theorem 6.5 we get
∗
(ri−1, wiwi+1 · · · wk , YiYi+1 · · · Yk ) `
Basis: One step. Then we have a production (ri, wi+1 · · · wk , Yi+1 · · · Yk )
[qXp] → w. From the construction of G it
follows that (p, ) ∈ δ(q, a, X), where w = a. Since this holds for all i ∈ {1, . . . , k}, we get
∗
But then (q, w, X) ` (p, , ). (q, aw1w2 · · · wk , X) `
∗
∗
(r0, w1w2 · · · wk , Y1Y2 · · · Yk ) `
Induction: Length of ⇒ is n > 1, and ♥ holds ∗
(r1, w2 · · · wk , Y2 · · · Yk ) `
for lengths < n. Then we must have ∗
(r2, w3 · · · wk , Y3 · · · Yk ) `
[qXrk ] ⇒ a[r0Y1r1][r1Y2r2] · · · [rk−1Yk rk ] ⇒ w
∗ (p, , ).
∗
We can break w into aw2 · · · wk such that [ri−1Yiri] ⇒
wi. From the IH we get
∗
(ri−1, wi, Yi) ` (ri, , )
221 222
A PDA P = (Q, Σ, Γ, δ, q0, Z0, F ) is determinis- Theorem 6.17: If L is regular, then L = L(P )
tic iff for some DPDA P .
1. δ(q, a, X) is always empty or a singleton.
Proof: Since L is regular there is a DFA A s.t.
2. If δ(q, a, X) is nonempty, then δ(q, , X) must
L = L(A). Let
be empty.
A = (Q, Σ, δA, q0, F )
Example: Let us define
We define the DPDA
Lwcwr = {wcwR : w ∈ {0, 1}∗}
P = (Q, Σ, {Z0}, δP , q0, Z0, F ),
Then Lwcwr is recognized by the following DPDA
where
0 , Z 0 /0 Z 0
1 , Z 0 /1 Z 0 δP (q, a, Z0) = {(δA(q, a), Z0)},
0 , 0 /0 0
0 , 1 /0 1 for all p, q ∈ Q, and a ∈ Σ.
1 , 0 /1 0 0, 0/ ε
1 , 1 /1 1 1, 1/ ε
An easy induction (do it!) on |w| gives
Start ∗
q0 q q2 (q0, w, Z0) ` (p, , Z0) ⇔ δˆA(q0, w) = p
1
c , Z 0 /Z 0 ε , Z 0 /Z 0
c, 0/ 0
c, 1/ 1 The theorem then follows (why?)
223 224
• We have seen that Regular⊆ L(DPDA).
What about DPDA’s that accept by null stack?
• Lwcwr ∈ L(DPDA)\ Regular
They can recognize only CFL’s with the prefix
• Are there languages in CFL\L(DPDA).
property.
Yes, for example Lwwr .
A language L has the prefix property if there
are no two distinct strings in L, such that one • What about DPDA’s and Ambiguous Gram-
is a prefix of the other. mars?
Example: Lwcwr has the prefix property. Lwwr has unamb. grammar S → 0S0|1S1|
but is not L(DPDA).
Example: {0}∗ does not have the prefix prop-
erty. For the converse we have
∗
1. Eliminate useless symbols, those that do • A symbol X is generating if X ⇒ w, for some
∗ G
not appear in any derivation S ⇒ w, for w ∈ T∗
start symbol S and terminal w.
∗
• A symbol X is reachable if S ⇒ αXβ, for
G
2. Eliminate -productions, that is, produc- some {α, β} ⊆ (V ∪ T )∗
tions of the form A → .
It turns out that if we eliminate non-generating
3. Eliminate unit productions, that is, produc- symbols first, and then non-reachable ones, we
tions of the form A → B, where A and B will be left with only useful symbols.
are variables.
229 230
Example: Let G be
S → AB|a, A → b
S and A are generating, B is not. If we elimi- Theorem 7.2: Let G = (V, T, P, S) be a CFG
nate B we have to eliminate S → AB, leaving such that L(G) 6= ∅. Let G1 = (V1, T1, P1, S)
the grammar be the grammar obtained by
S → a, A → b
Now only S is reachable. Eliminating A and b 1. Eliminating all nongenerating symbols and
leaves us with the productions they occur in. Let the new
S→a grammar be G2 = (V2, T2, P2, S).
with language {a}.
The generating symbols g(G) are computed by We’ll show in class on an induction on the
stage in which a symbol X is added to g(G)
the following closure algorithm:
that X is indeed generating.
Based on the second production we add {b} to We’ll compute n(G), the set of nullable sym-
r(G) and that’s it. bols of a grammar G = (V, T, P, S) as follows:
239 240
Theorem 7.9: L(G1) = L(G) \ {}.
∗
A⇒α⇒w
in G.
241 242
∗
⊇-direction: Let A ⇒ w, and w 6= . We’ll show
G
by induction of the length of the derivation
∗
that A ⇒ w in G1.
∗
Now X1X2 · · · Xk ⇒ w (Why?)
G
243 244
Eliminating Unit Productions
We’ll expand rule E → T and get rules
E → F, E → T ∗ F
A→B
We then expand E → F and get
is a unit production, whenever A and B are
variables. E → I|(E)|T ∗ F
Finally we expand E → I and get
Unit productions can be eliminated.
E → a | b | Ia | Ib | I0 | I1 | (E) | T ∗ F
245 246
∗
Given G = (V, T, P, S) we can construct G1 =
(A, B) is a unit pair if A ⇒ B using unit pro- (V, T, P1, S) that doesn’t have unit productions,
ductions only. and such that L(G1) = L(G) by setting
2. Eliminate unit productions To achieve this, start with any grammar for
the CFL, and
249 250
• For step 2, for every terminal a that appears Illustration of the effect of step 3
in a body of length ≥ 2, create a new variable,
say A, and replace a by A in all bodies.
Then add a new rule A → a. A
B1 C1
A → B1C1
C1 → B2C2
A
···
B1 B2 . . . Bk
Ck−3 → Bk−2Ck−2
Ck−2 → Bk−1Bk (b)
251 252
Example of CNF conversion For step 3, we replace
E → E + T | T ∗ F | (E) | a | b | Ia | Ib | I0 | I1 E → T M F, T → T M F by
T → T ∗ F | (E)a | b | Ia | Ib | I0 | I1 E → T C2, T → T C2, C2 → M F
F → (E) a | b | Ia | Ib | I0 | I1
I → a | b | Ia | Ib | I0 | I1
E → LER, T → LER, F → LER by
E → LC3, T → LC3, F → LC3, C3 → ER
For step 2, we need the rules
A → a, B → b, Z → 0, O → 1
P → +, M → ∗, L → (, R →) The final CNF grammar is
and by replacing we get the grammar
E → EC1 | T C2 | LC3 | a | b | IA | IB | IZ | IO
E → EP T | T M F | LER | a | b | IA | IB | IZ | IO T → T C2 | LC3 | a | b | IA | IB | IZ | IO
T → T M F | LER | a | b | IA | IB | IZ | IO F → LC3 | a | b | IA | IB | IZ | IO
F → LER | a | b | IA | IB | IZ | IO I → a | b | IA | IB | IZ | IO
I → a | b | IA | IB | IZ | IO C1 → P T, C2 → M F, C3 → ER
A → a, B → b, Z → 0, O → 1 A → a, B → b, Z → 0, O → 1
P → +, M → ∗, L → (, R →) P → +, M → ∗, L → (, R →)
253 254
1. |vwx| ≤ n.
Proof: Induction on n.
2. vx 6=
Basis: n = 1. Then the tree consists of a root
3. uv iwxiy ∈ L, for all i ≥ 0.
and a leaf, and the production must be of the
form S → a. Thus |w| = |a| = 1 = 20 = 2n−1. S
A1
Aj
A2
.
.
u v w x y
. z
Ak
a
257 258
Since the longest path in the subtree rooted Closure Properties of CFL’s
at Ai is at most m + 1, the previous theorem
gives us |vwx| ≤ 2m = n. Consider a mapping
S
∗
s : Σ → 2∆
A
A
(a)
where Σ and ∆ are finite alphabets. Let w ∈ Σ∗,
u v w x y
where w = a1a2 . . . an, and define
S
s(w) = s(a1).s(a2). · · · .s(an)
A
(b) and, for L ⊆ Σ∗,
w
[
u y s(L) = s(w).
S
w∈L
A
Such a mapping s is called a substitution.
(c)
A
u v A x y
v w x
259 267
Proof: We start with grammars
G = (V, Σ, P, S)
Example: Σ = {0, 1}, ∆ = {a, b},
for L, and
s(0) = {anbn : n ≥ 1}, s(1) = {aa, bb}.
Ga = (Va, Ta, Pa, Sa)
Let w = 01. Then s(w) = s(0).s(1) = for each s(a). We then construct
{anbnaa : n ≥ 1} ∪ {anbn+2 : n ≥ 1}. G0 = (V 0, T 0, P 0, S)
where
Let L = {0}∗. Then s(L) = (s(0))∗ =
S
{an1 bn1 an2 bn2 · · · ank bnk : k ≥ 0, ni ≥ 1}. V 0 = ( a∈Σ Va) ∪ V
S
Theorem 7.23: Let L be a CFL over Σ, and s T0 = a∈Σ Ta
a substitution, such that s(a) is a CFL, ∀a ∈ Σ.
S
Then s(L) is a CFL. P 0 = a∈Σ Pa plus the productions of P
with each a in a body replaced with sym-
bol Sa.
268 269
x1 x2 xn
Sa Sa Sa
1 2 n
Now delete the dangling subtrees. Then you
have yield
x1 x2 xn
Sa1 Sa2 . . . San
where a1a2 . . . an ∈ L(G). Now w is also equal
Thus we can generate Sa1 Sa2 . . . San in G0 and to s(a1a2 . . . an), which is in s(L).
form there we generate x1x2 . . . xn = w. Thus
w ∈ L(G0).
270 271
Applications of the Substitution Theorem
P R = {A → αR : A → α ∈ P }
(ii) : Here we choose L = {12} and s as before.
Then L1.L2 = s(L). Show at home by inductions on the lengths of
the derivations in G (for one direction) and in
(iii) : Suppose L1 is CF. Let L = {1}∗, s(1) = GR (for the other direction) that (L(G))R =
L1. Now L∗1 = s(L). Similar proof for +. L(GR ).
a 7→ {h(a)}
Then h(L) = s(L).
272 273
CF ∩ Regular = CF
CFL’s are not closed under ∩ Theorem 7.27: If L is CR, and R regular,
then L ∩ R is CF.
Let L1 = {0n1n2i : n ≥ 1, i ≥ 1}. The L1 is CF
Proof: Let L be accepted by PDA
with grammar
S → AB P = (QP , Σ, Γ, δP , qP , Z0, FP )
A → 0A1|01 by final state, and let R be accepted by DFA
B → 2B|2
A = (QA, Σ, δA, qA, FA)
Also, L2 = {0i1n2n : n ≥ 1, i ≥ 1} is CF with We’ll construct a PDA for L ∩ R according to
grammar the picture
S → AB
A → 0A|0 FA
B → 1B2|12 state
274 275
Theorem 7.29: Let L, L1, L2 be CFL’s and
R regular. Then
Formally, define
1. L \ R is CF
P 0 = (QP × QA, , Σ, Γ, δ, (qP , qA), Z0, FP × FA)
where 2. L̄ is not necessarily CF
n o
δ (q, p), a, X = (r, δ̂A(p, a)), γ : (r, γ) ∈ δP (q, a, X) 3. L1 \ L2 is not necessarily CF
∗
Prove at home by an induction `, both for P Proof:
and for P 0 that
∗ 1. R̄ is regular, L ∩ R̄ is regular, and L ∩ R̄ =
(qP , w, Z0) `
P
(q, , γ), and δ̂(qA, w) ∈ FA L \ R.
276 277
Stack ∗
• (q0, h(w), Z0) ` (p, , γ) in P if and only if
∗
((q0, ), w, Z0) ` ((p, ), , γ) in P 0.
278 279
Converting between CFA’s and PDA’s
We’ll look at the following: • n is the total size of the input CFG or PDA.
280 281
[qXrk ] → a[rY1r1] · · · [rk−1Yk rk ] • Total size (and time to calculate) the gram-
mar is therefore O(n3).
282 283
Converting into CNF
Good news:
Bad news:
1. Computing r(G) and g(G) and eliminating
useless symbols takes time O(n). This will
be shown shortly • Eliminating the nullable symbols can make
the new grammar have size O(2n)
(slides 229,232,234)
(slide 236)
2. Size of u(G) and the resulting grammar
with productions P1 is O(n2) The bad news are avoidable:
(slides 244,245)
Break bodies first before eliminating nullable
symbols
3. Arranging that bodies consist of only vari-
ables is O(n)
• Conversion into CNF is O(n2)
(slide 248)
284 285
L(G) is non-empty if the start symbol S is gen- Creation and initialzation of the links and counts
erating. is O(n)
A naive implementation on g(G) takes time When a count goes to zero, we have to
O(n2).
1. Finding the head variable A, checkin if it
g(G) can be computed in time O(n) as follows: already is “yes” in the array, and if not,
queueing it is O(1) per production. Total
Generating?
O(n)
Count
A ?
B yes A B c D B 3
2. Following links for A, and decreasing the
counters. Takes time O(n).
C B A 2
286 287
CYK-algo for membership testing
2n − 1 internal nodes.
X 15
Generate all binary parse trees of G with 2n − 1
X 14 X 25
internal nodes.
X 13 X 24 X 35
Check if any parse tree generates w
X 12 X 23 X 34 X 45
X 11 X 22 X 33 X 44 X 55
a1 a2 a3 a4 a5
288 289
Example:
To fill the table we work row-by-row, upwards
G has productions
The first row is computed in the basis, the S → AB|BC
subsequent ones in the induction.
A → BA|a
B → CC|b
Basis: Xii == {A : A → ai is in G}
C → AB|a
Induction:
{S,A,C}
We wish to compute Xij , which is in row j − i + 1.
- {S,A,C}
A ∈ Xij , if
∗ - {B} {B}
A ⇒ aiai + 1 · · · aj , if
for some k < j, and A → BC, we have {S,A} {B} {S,C} {S,A}
∗ ∗
B ⇒ aiai+1 · · · ak , and C ⇒ ak+1ak+2 · · · aj , if
{B} {A,C} {A,C} {B} {A,C}
B ∈ Xik , and C ∈ Xkj
b a a b a
290 291
To compute Xij we need to compare at most
n pairs of previously computed sets:
Preview of undecidable CFL problems
(Xii, Xi=1,j ), (Xi,i+1, Xi+2,j ), . . . , (Xi,j−1, Xjj )
The following are undecidable:
as suggested below
For each Xij we need to compare at most n 5. Is a given CFL universal (equal to Σ∗)?
pairs (Xik , Xk+1,j ).
main()
{
printf(‘‘hello, world\n’’);
}
294 295
What about the program in Fig. 8.2 in the
textbook? The hypothetical “Hello, world” tester H
It will print hello, world, for input n if and only Suppose the following program H exists.
if the equation
xn + y n = z n I Hello-world
tester
yes
296 297
yes
P H2
hello, world
... B B X1 X2 Xi Xn B B ...
1. Change state
• If H2 prints yes it should have printed
hello, world.
• If H2 prints hello, world it should have 2. Write a tape symbol in the cell scanned
printed yes.
• Thus H2 cannot exist.
3. Move the tape head one cell left or right
• Consequently H cannot exist either.
298 299
A (deterministic) Turing Machine is a 7-tuple Instantaneous Description
M = (Q, Σ, Γ, δ, q0, B, F ),
where A TM changes configuration after each move.
• F ⊆ Q is the set of final states. 3. The tape head is scanning the ith symbol
300 301
We’ll use ` to indicate a move by M from a M = ({q0 , q1 , q2 , q3 , q4 }, {0, 1}, {0, 1, X, Y, B}, δ, q0 , B, {q4 })
M
configuration to another. where δ is given by the following table
0 1 X Y B
• Suppose δ(q, Xi) = (p, Y, L). Then → q0 (q1 , X, R) (q3 , Y, R)
q1 (q1 , 0, R) (q2 , Y, L) (q1 , Y, R)
q2 (q2 , 0, L) (q0 , X, R) (q2 , Y, L)
q3 (q3 , Y, R) (q4 , B, R)
X1X2 · · · Xi−1qXiXi+1 · · · Xn `
M ?q4
X1X2 · · · pXi−1Y Xi+1 · · · Xn
We can represent M by the following transition
• If δ(q, Xi) = (p, Y, R), we have diagram
Y/ Y
X1X2 · · · Xi−1qXiXi+1 · · · Xn `
M 0/ 0 Y/ Y
X1X2 · · · Xi−1Y pXi+1 · · · Xn 0/ 0
Start 0/ X 1/ Y
q0 q1 q2
We denote the reflexive-transitive closure of `
M
∗
with `
M
. X/ X
Y/ Y
∗
L(M ) = {w ∈ Σ∗ : q0w `
M
αpβ, p ∈ F, α, β ∈ Γ∗}
Y/ Y
302 303
A TM with “output” Programming Techniques for TM’s
The following TM computes Although TM’s seem very simple, they are as
· powerful as any computer.
m − n = max(m − n, 0)
0 1 B
→ q0 (q1 , B, R) (q5 , B, R) Lots of “features” can be simulated with a
q1 (q1 , 0, R) (q2 , 1, R)
q2 (q1 , 1, L) (q2 , 1, R) (q4 , B, L) “standard” machine.
q3 (q3 , 0, L) (q3 , 1, L) (q0 , B, R)
q4 (q4 , 0, L) (q4 , B, L) (q6 , 0, R)
q5 (q5 , B, R) (q5 , B, R) (q6 , B, R)
?q6 • Storage in State
304 305
• Subroutines
• Multiple Tracks for {wcw : w ∈ {0, 1}∗}
The TM computes 0m10n1 7→ 0m·n
State q 0/0
Start B/ B
q0 q9
Storage A B C
0/ B
0/0
Track 1 X 1/1 Copy 0/0 1/1
q6 q1 q5 q7 q8
Track 2 Y
Track 3 Z
0/0 B /B
1/ B 1/ B
q12 q11 q10
0/ B
M = (Q, Σ, Γ, δ, [q1, B], [B, B], {[q0, B]})
Here is the “Copy” TM
where
1/1 1/1
Q = {q1, q2, . . . , q9} × {0, 1, B} 0/0 0/0
Start 0/ X B /0
q1 q2 q3
X /0
306 307
Variations of the basic TM Theorem: Every language accepted by a multi-
tape TM M is RE
• Multitape TM’s. Input on first tape.
Proof idea: Simulate M by multitrack TM N
... ... X
A1 A2 Ai Aj
X
... ...
B1 B2 Bi Bj
... ...
308 309
• Nondeterministic TM’s
Proof idea: Suppose at each state MN has k We want to prove undecidable Lu which is the
choiches for each symbol.
language of pairs (M, w) such that:
Finite
control
1. M is a TM (encoded in binary) with input
alphabet {0, 1}.
Queue x ...
ID1 * ID2 * ID3 * ID4 *
of ID’s
Scratch
tape
2. w ∈ {0, 1}∗.
310 311
The landscape of languages (problems) Encoding TM’s
Ld .
• Enumerate strings: w ∈ {0, 1}∗ is treated as
Lu .
binary integer 1w. The ith string is denoted wi.
RE Recursive
but = w1, 0 = w2, 1 = w3, 00 = w4, 01 = w5, . . .
not
recursive
Not RE
To enccode M = (Q, {0, 1}, Γ, δ, q1, B, F )
312 313
Proof:
w M
Accept Accept Lu = {(enc(M ), enc(w)) : w ∈ L(M )}
Reject Reject
TM U where L(U ) = Lu
Proof: Input M w
w
State of M 000 . . . 0BB . . .
Accept Reject
M2
Scratch
316 317
Operation of U :
318 319
Reductions for proving lower bounds
Theorem: Lu is RE but not recursive. Find an algorithm that reduces a known hard
problem P1 to P2.
Proof: Lu is RE since L(U ) = Lu.
yes
Suppose Lu were recursive. Then Lu is also yes
• wi ∈ L(M 0 ) ⇒ wi 111wi ∈ Lu ⇒ wi ∈
/ L(Mi ) ⇒ wi ∈ Ld
Proof: by contradiction. If there were an al-
/ L(M 0 ) ⇒ wi 111wi ∈ Lu ⇒ wi ∈ L(Mi ) ⇒ wi ∈
• wi ∈ / Ld gorithm for P2 you could also solve P1 by first
reducing P1 to P2 and then running the algo-
rithm for P2.
320 321
w ∈ L(M ) ⇔ L(M 0) 6= ∅
Le = {enc(M ) : L(M ) = ∅}
M’
324 325
Suppose first ∅ ∈
/ P.
Proof of Rice’s Theorem:
Let L ∈ P and ML be a TM such that L(ML) = L.
x
ML Since every TM accepts an RE language we
M’
have
LP = LP
We have reduced Lu to LP .
The instance (A, B) has a solution if there ex- This PCP instance has no solution.
ists indices i1, i2, . . . , im, such that
wi1 wi2 · · · wim = xi1 xi2 · · · xim Suppose i1, i2, . . . , im is a solution:
Example: w2 011
If i1 = 2 we cannot match x2= 11
List A List B
i wi xi
1 1 111 101
2 10111 10 w3
3 10 0 If i1 = 3 we cannot match x3= 011
Solution: i1 = 2, i2 = 1, i3 = 1, i4 = 3 gives w1 10
Therefore i1 = 1 and a partial solution is x1= 101
w2w1w1w3 = x2x1x1x3 = 101111110
w1 w2 10011
If i2 = 2 we cannot match x1x2=10111 The modifeid PCP A, B has a solution if there
exists indices i1, i2, . . . , im, such that
w1 w3 10101
Only i2 = 3 is possible giving x1x3=101011 w1wi1 , wi2 , . . . , wim = x1xi1 , xi2 , . . . xim
w1 w3 w3 10101101101 w1 1
Only i4 = 3 is possible giving x1x3x3=101011011011 Any solution would have to begin with x1=111
w1 w2 110111
If i2 = 2 we cannot match x1x2=11110
Conclusion: The first list can never catch up 110
w1 w2
with the second If i2 = 3 we cannot match x1x2=1110
w1 w2 11
If i2 = 1 we have to match x1x2=111111
We are back to “square one.”
330 331
We reduce MPCP to PCP
Example:
Let MPCP be A = w1, w2, . . . , wk , B = x1, x2, . . . , xk
Let MPCP be
We construct PCP A0 = y0, y1, . . . , yk+1,
B 0 = z0, z1, . . . , zk+1 as follows: List A List B
i wi xi
1 1 111
y0 = ∗y1 and z0 = z1 2 10111 10
3 10 0
List A List B
If xi = b1b2 . . . bp then zi = ∗b1 ∗ b2 . . . ∗ bp i yi zi
0 *1* *1*1*1
1 1* *1*1*1
yk+1 = $ and zk+1 = ∗$ 2 1*0*1*1*1* *1*0
3 1*0* *
4 $ *$
Now (A, B) has a solution iff (A0, B 0) has one.
332 333
List A List B
Partial solutions will consist of strings of the X X
form # #
#α1#α2#α3 · · ·
3. ∀q ∈ Q \ F, ∀p ∈ Q, ∀X, Y, Z ∈ Γ
where α1 is the initial configuartion of M on w,
and αi ` αi+1.
List A List B
qX Yp if δ(q, X) = (p, Y, R)
The string from list B will always be one ID
ZqX pZY if δ(q, X) = (p, Y, L), Z ∈ Γ
ahead of A until M accepts w and A can catch up. q# Y p# if δ(q, B) = (p, Y, R)
Zq# pZY # if δ(q, B) = (p, Y, L), Z ∈ Γ
334 335
Example: Lu instance: (M, 01)
336 337