Computation Models and String Theory
Computation Models and String Theory
Strings. The set of all possible inputs to a decision problem may be encoded as the set of finite-length
strings over some fixed finite alphabet. All data types – numbers , graphs , trees , programs etc — can
all be encoded naturally as strings. So by using the string data type we need to consider only one data
type with a few basic operations.
1
df df
• a0 = ε an+1 = aan = an a
• Σ⋆ is the set of all (finite - length) string over Σ.
• If Σ = φ then Σ⋆ = {ε} by [Link] Σ 6== φ then Σ⋆ is an infinite set of strings.
• For x, y ∈ Σ⋆ , x.y is the string obtained by juxtaposing y to the right of x. This is the operations
of (con)catenation. Often the ′ .′ is omitted
• (Con)catenation is
(i) associative : x(y z) = (x y)z
(ii) ε is the identity : εx = xε = x.
(iii) |xy| = |x| + |y|. for x = am and y = an we have xy = am an = am+n . for all m, n ≥ 0.
• h Σ⋆ , . , ε i is a monoid.
• For any x ∈ Σ⋆ ,
df df
x0 = ε xn+1 = xn . x = [Link]
• For a ∈ Σ and a ∈ Σ⋆ ,
#a(x) is the number of occurrences of ′ a′ in x.
• ε 4 x for every x ∈ Σ⋆
• x 4 x for every x ∈ Σ⋆
• x 4 y and y 4 x ⇒ x = y for all x, y ∈ Σ⋆
• x 4 y and y 4 z ⇒ x4z
• 4 is a partial order on Σ⋆ .
Problems on strings
1. Consider the set T of trees whose nodes are labelled by letters ′ a′ to ′ z ′ such that more than one
node in a tree may have the same letter labelling it. In particular , consider the following tree. It is
necessary to represent this tree as a string. Now answer the following.
(a) What is Σ ? Give an intuitive expalanation of the letters in the alphabet Σ that you will use
to represent trees such as the above.
2
λ − calculus Register machines P ost system
3. Let L be a set of labels on nodes of a graph. Give a scheme to represent both directed and undi-
rected graphs as strings. Is it possible for the same graph to have different strings representations
?
4. If a structure h A , . i with an associative binary product operations . and closed under . has a left
identity and a right identity then it has a unique element which is both the left identity and the
left right identity.
P ⋆
Sets of string on — The structure h 2Σ , ∪, ∩, • φ, Σ⋆ , {ε} i
3
P
Note : For any a ∈ , a⋆ and a+ will be used as short hand for {a}⋆ and {a}+ respectively.
Similarly for any x ∈ Σ⋆ x⋆ and x+
4
FINITE AUTOMATA & REGULAR SETS
Introduction
Consider a basic model of computation which we know machines could perform even the early years of
the 20th century
• looms which could “read ” patterns from a finite paper tape and could replicate the patterns.
• even more recently , tape recorders and gramophone players which could “read” a finite input and
convert them into sounds.
• even more recently programs that can “read” an input file containing a finite sequence of characters
and take appropriate action.
All the above are concrete examples or instances of the following abstraction.
Diagrammatically
finite length
input sequence
Finite state
control
Definition 3. M = h Q, Σ, δ, q0 , F i a finite-state
5
Simple examples
1. Consider a vending machine which accepts only 5-rupee coins and delivers coffee for each such coin.
We forget about the coffee and specify it as
δ Σ 5
Q
q0 q0
Σ = {5}
Q = {q0 }
F = {q0 }
δ(q0 , 5) = q0
2. Consider a machine that does not accept any other coins (say Re1 and Re2).
Σ = {1, 2, 5}
Q = {q0 , q1 }
F = {q0 }
δ Σ 1 2 5
Q
q0 q1 q1 q0
q1 q1 q1 q1
3. Consider a machine that accepts a nonempty sequence of Re1 coins followed by a nonempty sequence
of Re2 coins but does not accept “anything else”
We abstract out the “anything else” by a new symbol ⊥. So we then have
δ Σ
Q
1 2 ⊥
q0 q1 q⊥ q⊥
q1 q1 q2 q⊥
q2 q⊥ q2 q⊥
q⊥ q⊥ q⊥ q⊥
Σ = {1, 2, ⊥}
Q = {q0 , q1 , q2 , q⊥ }
F = {q2 }
6
Automata may also be represented graphically
2
q1 2
1
q2
⊥
q0
⊥ 1,⊥
final state
q⊥
2
initial state
1,2,⊥
4. Consider a machine that only accepts any sequence conisting of at least 3 one-rupee coins and
assume Σ = {1, 2, 5}
1 1 1
q0 q11 q12 q13
5. Consider a machine that accepts a sequence of coins in which there is at least one sub-sequence of
there consecutive 1s.
2,5
1,2,5
1 1 1
q0 q11 q12 q13
2,5 2,5
6. Consider a machine that accepts a sequence of coins in which there is exactly one sub-sequence of
three consecutive 1s
7
2,5 2,5
1 1 1
q0 q11 q12 q13
2,5 2,5 1
2,5
2,5
1 1 q21
q23 q22
7. Consider a vending machine which accepts exactly coins of denominations {1, 2, 3} which add
up to 4.
Clearly Σ = {1, 2, 5} and the language accepted by the machine is {14 , 121, 112, 211, 22} let the states
of the machine by numbered/named 0, 1, 2, 3, 4, ⊥ where ⊥ stands for any number > 4
2 2,5
1 3
1
1,2,5
5
⊥
1
1 ,5
2 1,2
1
2 4
2
The number on each state indicates the total value of the sequence of coins input so far.
δ ⋆ (q, ε) = q
δ ⋆ (q, xa) = δ(δ ⋆ (q, x), a)
FACTS
8
3. The various languages we have defined through the various automata in the preeding examples are
all regular.
⋆
4. If L ⊆ Σ⋆ is regular and Σ′ ⊇ Σ, then L ⊆ Σ′ is still regular.
Number-theoretic examples. Consider an automation that accepts all bit strings that are multiples
of 2. Assume ε represents the number 0.
Clearly here Σ = {0, 1} and the automation accepts the language {ε} ∪ {x 0 | x ∈ Σ⋆ }
δ
Σ
0 1
1 Q
02 12
02 02 12
0 12 02 12
0
Consider a machine that accepts only multiples of 3. In designing this automation we consider the fol-
lowing. For any x ∈ {0, 1}⋆ let (x)2 denote the integer it represents. Then we have
(x b)2 = 2(x)2 + b
The states of the automation are numbered i ∈ {0, 1, 2} and satisfy the preoperty that for any x ∈ Σ⋆
the automation is in state (x)2 mod 3 after consuming x. Then inductively the transition function
δ : {0, 1, 2} × {0, 1} −→ {0, 1, 2} satisfies the property that
0 δ Σ 0 1
Q
1
1 0 03 03 13
03 13 23 13 23 03
1 0 23 13 23
Question ? How does one design a modulo 6 counter ? there are two possible answers to this question.
9
1. Follow the usual steps and design a 6-state automation.
2. Do a “product construction” to create the 6-state automation from the modulo 2 and modulo 3
counters.
Q3 = Q1 × Q2 = {q1 ′ , q2 ′ | q1 ′ ∈ Q1 , q2 ′ ∈ Q2 }
F3 = F1 × F2
q3 = (q1 , q2 )
δ3 : Q3 × Σ −→ Q3 such that
δ((q1 ′ , q2 ′ ), a) = (δ1 (q1 ′ , a), δ2 (q2 ′ , a))
Example . The product of the automation for the modulo 2 counter and that of the modulo 3 counter
yield an automation that accepts only bit strings that are binary representations of multiples of 6.
Lemma 1. If M3 = M1 × M2 then
δ3⋆ ((q1 ′ , q2 ′ ), ε) = (q1 ′ , q2 ′ )
δ3⋆ ((q1 ′ , q2 ′ ), x a) = δ3 (δ3⋆ ((q1 ′ , q2 ′ ), x), a)
Theorem 1. L(M3 ) = L(M1 ) ∩ L(M2 )
Proof. for all x ∈ Σ⋆
x ∈ L(M3 )
⇔ δ3⋆ ((q1 , q2 ), x) ∈ F3
⇔ (δ1⋆ (q1 , x), (δ2⋆ (q2 , x)) ∈ F1 × F2
⇔ (δ1⋆ (q1 , x) ∈ F1 ∧ (δ2⋆ (q2 , x)) ∈ F2
⇔ x ∈ L(M1 ) ∧ x ∈ L(M2 )
⇔ x ∈ L(M1 ) ∩ L(M2 )
10
Example : A modulo 6 counter .
A direct construction yields.
0
δ 0 1
Σ
0 Q
1
16
06 06 16
06 26 0
0
16 26 36
1
1
0 26 46 56
0 46
1
1
36 06 16
36 56
46 26 36
56 46 56
1
0
δ Σ 0 1
1 0 Q
12 13
02 03 02 23 0 02 03 02 03 12 13
0
1
12 13 02 23 12 03
1
0
0 02 13 02 23 02 13 12 23
1
1
12 03 02 03 12 13
12 03 12 23
02 13 02 23 12 03
1 12 23 02 13 12 23
11
∼
Proof. If A ∈ R , then for some DF AM A = L(M ). Consider the complement DF A M = h Q, Σ, δ, q0 , Q − F i
∼
in which every non-accepting state becomes final and vice-versa. Clearly L(M ) = Σ⋆ − L(M ) =∼ L(M ) =∼ A
Proof. Since R is closed under both ∩ and ∼ it follows that for any A, B ∈ R A ∪ B =∼ (∼ A∩ ∼ B)
and hence A ∪ B is regular.
M = h P, Σ, p0 , δA , F i
N = h Q, Σ, q0 , δB , G i
Tutorial 2
12
0 1
1
0 0 1
0 0
0 1
1
0 1
3. Prove without using the product construction or any of its consequences that every finite language
on Σ is regular.
A=φ q0
If A is not empty then consider the following case A = {ε}. This language is accepted by the
automation.
13
Σ
Σ
q1
q0
A 6= {ε} Intially we consider the case when A 6= φ and ε 6∈ A. If ε ∈ A we make the initial state
final. Define for each ai ∈ Σ , Ai = {x ∈ Σ⋆ | ai x ∈ A}. Also define M AX(A) = max{|x|x ∈ A}.
Basis M AX(A) = 0. Then A = {ε} and we have already constructed the required automation.
IH : Assume for all B M AX(B) < n for some n > 0 it is true that B is regular
M = h Q, Σ, δ, q0 , F i as follows :
[
Q= Qi ∪ {q0 } ∪ {qai | Ai = φ}
1≤i<m
S
where q0 , qa1 , . . . , qam are all mutually distinct and also different from each state in Qi
[
F = Fi and δ is defined as follows :
1≤i≤m
Example. In particular , we would like to “connect (the two DF As) in parallel ” to obtain an automa-
tion which can simulate either of the two behaviours.
14
i) Consider an automation that accepts bit strings which are either multiples of 2 or multiples of 3. The
resulting language is the union of the two languages.
It should be possible to combine the two vending machine to get a single vending machine which delivers
coffee or tea according to the money put in. The only thing required is a mechanism of combining the
two vending machine and deciding as soons as a coin is put it whether to trigger the first machine or the
second one.
Deciding
mechanism
p0 q0
Coffee
machine Tea
pc machine q4
Coffee Tea
A simple way to combine the two machines is as follows. Define the machine P Q with initial state pq0
1) A − B
2) B − A
3) A ∩ B
In the case if 1 and 2 the decision as to which machine to start is [Link] the question of which
machine to run in the case of x ∈ A ∩ B requires certain external decision-making which we abstract out
as non-determinism.
Having abstracted this out , it also requires that we relax the condition of acceptance or rejection as
follows :
x is accepted by the combined machine if it is accepted by at least one of them
x is rejected by the combined machine only if it is rejected by both of them
Hence there should be a simple way of combining machines to produce machines that accepts unions of
languages such that the decisions that accept unions of languages such that the decisions mechananism
is abstracted away and left to the implementor. To this end we define a “nondeterministic” automation
as follows :
Definition 6. A nonderministic finite automation (NFA) is a structure N = h Q, Σ, △, S, F i where
15
φ 6= S ⊆ Q △ : Q × Σ → 2Q , △ is the transition relation. The function △⋆ : 2Q × Σ⋆ → 2Q is the
natural extension of the function △ and is defined by the rules.
df
△⋆ (T, ε) = T
df
△⋆ (T, ax) = △⋆ (△(T, a), x)
where for any set S ⊆ Q [
△(T, a) = △(q, a)
q∈T
Intuitively a NFA accepts a string if there exists a path from one of the start states to one of the final
states and it rejects a string only if there is no path from any start state to any final state. As usual
L (N ) denotes the language accepted by N.
16
[
let Ti = T = △⋆ (△(T, a), x′ )
i∈I [
= △⋆ ( △(q, a), x′ )
q∈T
[ [
= △⋆ ( △(qi , a), x′ )
q∈I qi ∈Ti
[ [
By IH = △ (⋆
△(qi , a), x′ )
i∈I qi ∈Ti
[
= △⋆ (△(Ti , a), x′ )
i∈I
[
= △⋆ (Ti , ax′ )
i∈I
[
= △⋆ (Ti , x)
i∈I
(⇒) The proof of this part requires the construction of a DF A that accepts the same language.
Let N = h QN , Σ, △, SN , FN i be a N F A.
Now construct a DF A
D = h QD , Σ, δ, qDo , FD i as follows .
df
QD = 2QN i.e. each state of the DF A represents a set of N F A state.
δD (qD , a) = △N (qD , a)
[
= △N {qN }, a)
qN ∈qD
qDo = SN
FD = {T ⊆ QN | T ∩ FN 6= φ}
17
Claim 1. D and N accepts the same language.
Proof. For any x ∈ Σ⋆
x ∈ L (D)
⋆
⇔ δD (qDo , x) ∈ FD
⇔ △⋆N (SN , x) ∈ FD
⇔ △⋆N (SN , x) ∩ FN 6= φ
⇔ x ∈ L (N )
End of proof of the subset construction theorem
Fact
Any N F A is also an N F A with ε-moves. Typically △N (T, ε) = φ in the case of a N F A without ε-moves.
Definition 8. Let q ∈ QN be a state of a N F A with ε-moves. The ε − closure of q (denoted ε⋆ (q)) is
the set of states reachable from q only by ε-moves and is defined inductively as follows :
ε0 (q) = q
′′ ′ ′′ ′
εn+1 (q) = n n
[ε (q) ∪ {q ∈ QN | ∃q ∈ ε (q) : q ∈ △n (q , ε)}
⋆ n
ε (q) = ε (q)
n≥0
We denote the fact that q ′ ∈ ε⋆ (q) alternately by q ⇒ q ′ to mean that q ′ is reachable from q by only a
sequence of ε−moves. Note : q ⇒ q holds for any state
For any set T ⊆ QN we exented this by
[
ε⋆ (T ) = ε⋆ (q).
q∈T
18
Theorem 4. Every regular expression represents a regular language.
Proof. By induction on the structure of regular expressions.
Basis. φ denotes the empty language and is accepted by a DF A/N F A with no final states.
ε is accepted by q0 which is a N F A.
a ∈ Σ is accepted by q0 a qf
19
Induction Step
α∪β
ε
ε Nα
q0 qf
ε
ε
Nβ
αβ
ε
Nα Nβ
α⋆
ε
ε ε
Theorem 5. For every N F Aε there exists a N F A without ε-moves which accepts the same language.
Proof. Let Nε = h Qε , Σ, δε , Sε , Fε i be a N F A with ε-moves Construct the N F A N = h Qε , Σ, δ, S, F i
without ε-moves as follows.
For every q ∈ Qε and a ∈ Σ
a a
q −→ q ′ δ iff ∃q1 , q2 : q ⇒ q1 −→ε q2 ⇒ q ′ in Nε .
Claim 2. N and Nε accept the same language.
Proof. x ∈ L (Nε ) with x = a1 . . . an .
a a an
⇔ ∃q0 ∈ S ∃qf ∈ F : q0 ⇒1 ε q1 ⇒2 ε . . . ⇒ ε qf in Nε
a1 a2 an
⇔ ∃q0 ∈ S ∃qf ∈ F : q0 −→ q
ε 1 −→ ε . . . −→ ε qf in Nε
Theorem 6. Every regular language has a regular expression representation. We have already proved
the following viz :
1. Every regular expression represents a regular language where the regular expressions were defined
by the following
0) For every a ∈ Σ , a is a RE and L (a) = {a}.
1) ε and φ are regular expression and L (ε) = {ε}
L (φ) = φ
2) For regular expressions α, β.
α.β is a regular expression representing L (α).L β
α ∪ β is a regular expression representing L (α) ∪ L β
20
3) For any regular expression α,
(α) is a regular expression representing L (α)
α⋆ is a regular expression representing (L (α))⋆
∼ α is a regular expression representing ∼ L (α)
Precedence of Operators
21
By induction on the size of X
a
Basis X = φ. Let a1 , a2 , . . . , ak ∈ Σ be all the letters such that p ⇒i q for 1 ≤ i ≤ k. Then
(
φ df a1 ∪ a2 ∪ · · · ∪ ak . if k>0
if p 6= q αpq =
φ if k = 0.
(
df a1 ∪ a2 ∪ · · · ∪ ak ∪ ε if k>0
else if p = q, αφpq =
ε if k = 0.
Induction step X 6= φ
df
Then αX Y Y Y ⋆ Y
pq = αpq ∪ αpr (α⇒ ) αrq
Hence by this induction process for each start state sıS and final state f ∈ F we have an expression αQ sf
[ Q
which represents the set of all paths from s to f in N . Hence αsf is the regular expression denoting
s∈S
the language accepted by N .
Note. This theorem essentially tells us that the language REΣ of regular expressions over Σ is complete
and that it is always possible to convert a DF A or a N F A into a regular expression.
• is associative
ε is the identity for •
φ is the zero for •
• distributes over + (both left & right)
α⋆ = ε + αα⋆ = ε + α⋆ α
α ≤ β iff α + β = β
22
⋆
2Σ
RLGΣ
RΣ
L
L
REΣ
N F AΣε
EXPRESSION
N F AΣ
(REPRESENTATION)
DF AΣ
MACHINES
(ACCEPTANCE)
which may be obtained as the maximal fixpoint under ⊆ of the composition of monotonic functions • an
⋆
d + on the lattice 2Σ
X = A.X ∪ B
on language over Σ⋆ i.e. A, B ⊆ Σ⋆ abd we need to find the unknown X. We may construct an increasing
sequence of sets Xi , i ≥ 0
φ = X0 ⊆ X1 ⊆ . . . Xi ⊆ Xi+q ⊆ . . .
such that
23
Xi+1 = [Link] ∪ B
[
The maximum solution is the set Xi
i≥0
i.e p ⊆f V × Σ⋆ • (V ∪ {ε}
A derivation of a (right linear) grammar is a sequence of strings in (Σ ∪ V )⋆ starting from S. such that
L (G) = { x ∈ Σ⋆ | S ⇒⋆ x }.
24
Theorem 8. If G is a right linear grammer then L (G) is a regular language.
Proof. Let G = hV, Σ, S0 , P i be a right linear grammar with V = {S0 , S1 , . . . , Sm } with a finite collection
of productions
P = { Si −→ xk Sj | Sj ∈ V ∪ {ε} , Si ∈ V , xk ∈ Σ⋆ , 1 ≤ k ≤ n }
Si −→ a1 . . . al Sj where Sj ∈ V ∪ {ε}
is a transition in △. If Sj 6= ε then Sij = Sj otherwise Sil is a new final state. Clearly for each such
production.
We have the following claim
S0 ⇒0 iff △⋆ (S0 , y) ∈ F
D = h Q, Σ, δ, q0 , F i
G = h V, Σ, q0 , P i where V = Q
with
qi −→ aqj ∈ iff δ(qi , a) = qj
and qi −→ ε ∈ P iff qi ∈ F
25
Proof. Every derivation in G is of the form
q0 ⇒ a1 q1 ⇒ a1 a2 q2 ⇒ . . . ⇒ a1 a2 . . . an qn ⇒ a1 . . . an where qn −→ ε ∈ P . By the construction of the
grammar it follows that δ ⋆ (q0 , a1 . . . an ) = qn ∈ F
Similarly for every string a1 . . . an ∈ L (D) we have a collection of states q0 , q1 , . . . , qn ∈ Q with qn ∈ F
such that δ ⋆ (q0 , a1 . . . an ) = qn . Correspondingly we have the derivation
q0 ⇒ a1 q1 ⇒ a1 a2 q2 ⇒ . . . ⇒ a1 a2 . . . an qn ⇒ a1 a2 . . . an .
The two theorem proved above give us the following characterization
A language L ⊆ Σ⋆ is regular iff
there exists a right-linear grammar
which generates L
Analogous to the notation of a right - linear grammar is the notion of a left-linear grammar in which
all the productions are of the form S −→ T x where T ∈ V ∪ {ε} and x ∈ Σ⋆ . Both right-linear and
left-linear grammars have the same power of generation. But to prove that left-linear grammars generate
only regular languages we need the following lemma.
Lemma 6. If A ⊆ Σ⋆ is regular them so is AR where AR = {xR |x ∈ Σ⋆ } and for any string x = a1 . . . am .
xR = am . . . a1 is the reverse of x.
Proof. Since A is regular there exists a N F A
N = h Q, Σ, △, S, F i with L (N ) = A.
Consider the N F A
N R = h Q, Σ, △R , F, S i
constructed from N by
(i) making S the set of final states
(ii) making F the set of start states
(iii) taking the universe of the relation △ as the
transition relation.
a a
i.e q −→ q ′ ∈ △ iff q ′ −→ q ∈ △R
The result then follows from the following claim
Claim 4. For all q0 ∈ S and qf ∈ F and x ∈ Σ⋆
26
f : GL −→ GR
such that
GL = h VL , Σ, SL , PL i h VR , Σ, SR , PR i
f (GL ) = GR iff
VL = VR , SL = SR and for all T ∈ VL = VR
and ∪ ∈ VL ∪ {ε} = VR ∪ {ε} and x ∈ Σ⋆
T −→ x ∪ ∈ PR iff T −→ ∪xR ∈ PR .
Claim 5. x ∈ L (GL ) iff xR ∈ L (GL ) for all x ∈ Σ⋆ and GR , GL such that GR = f (GL )
Proof. By induction on the length of x.
Main proof
(⇒) If A ⊆ Σ⋆ is regular then AR is also regular and there exists GR which generates AR . From the
claim above f −1 (GR ) = GL generates (AR ) = A. Hence every regular language may be generated by a
left-linear grammar.
(⇐) Let GL be any left-linear grammar. Then GR = f (GL ) is a right-linear grammar that generates
(L (GL ))R . But then (L (GL ))R must be regular which implies L (GL ) is also regular.
However we need to emphasize that a grammar in which there are both left-linear as well as right linear
productions may generate a language that is not regular as the following example shows
A ⊆ Σ⋆ is regular
⇒ ∃m > 0 : ∀x ∈ A : |x| ≥ m ⇒
∃ u, v, w : x = uvw ∧ |uv| ≤ m ∧ |u| > 0
∧ ∀k ≥ 0 : xk = uv k w ∈ A.
Proof. If A is finite then choose m > max{|x| |x ∈ A} and the theorem holds vacuously. So assume A is
infinite.
Claim 6. If A is infinite then for each n ≥ 0 , there exists x ∈ A such that |x| > n.
Proof. Suppose not. Then for some value of n ≥ 0. every
[ string in x ∈ A has a length ≤ n. But for any
k ≥ 0 and finite Σ , Σk is a finite set which implies Σk is also finite. In fact Σk has exactly |Σ|k
k≤n
[ X
k k
different strings and | Σ |= |Σ| .
k≤n k≤n
27
The contrapositive of the pumping lemma states that
∀m > 0 : ∃x ∈ A : |x| ≥ m ∧
∀u, v, w : x = u v w ∧ |uv| ≤ m ∧ |v| > 0
⇒ ∃ k ≥ 0 : xk = uv k w 6∈ A
⇒ A is not regular.
CHALLENGER’S STRATEGY
1. x = am bm
2. k = 0
Example A = {an bn |n ≥ 0} is not regular. Assume A is regular. Therefore the pumping lemma there
must hold and A is infinite. Since A is regular there must be a DF A D which accepts A. Suppose the
DF A has N states. Choose m = N + 1. Consider the string am bm and the sequence of states
a a a a b b b
q0 −→ q1 −→ . . . −→ qN −→ qm −→ qm+1 −→ . . . −→ q2m
The proof proper Since A is regular there exists a DF A D = h Q, Σ, δ, q0 , F i which accepts A. Let
|Q| = n. Since A may be assumed to be infinite it does have strings of length > n + 1. Choose any such
string |x| ≥ n + 1. Then δ ⋆ (q0 , x) = qf ∈ F consider the sequence of states in the DF A which lead to qf
from q0 . This is a sequence of n + 2 states.
q0 , q1 , . . . , qn−1 , qn , qf
By the pigeon hole principle there exists i < j ≤ n such that qi = qj . This implies that x = uvw such
that
|uv| ≤ n + 1 = m
{δ ⋆ (q0 , u) = qi δ ⋆ (qi , v) = qi = qj δ ⋆ (qj , w) = qf }
|v| > 0
Assume v = av ′ and w = bw′ . Clearly δ(qi , a) = qi+1 and δ(qj , b) = qj+1 = δ(qi , b)
It follows therefore that for any k ≥ 0
28
4. CHALLENGER : chooses k ≥ 0
Example. Determine whether A = {a2n |n ≥ 0} is regular. Since the regular expression (a a)⋆ is a
representation of the language A the DEFENDER must have a winning strategy.
⋆
(a a) is accepted by
DEFENDER’S STRATEGY
1. m = 3
2. u = ε, v = a2
Result : DEF has a winning strategy since for all n ≥ 2 and all k ≥ 0 , xk = (a2 )k a2(n−1) = a2(n+k−1) ∈ A.
a a
D : choose m = 3.
C : choose any n ≥ 1.
D : choose i = 1 , j = 2 with u = a and v = a2
⇒ x = uvw = a3 a2n+1−3) ⇒ w = a2n−2 = a2(n−1) .
C : choose any k ≥ 0.
⇒ xk = a(a2 )k a2(n−1) = a2n−2+2k+1 = a2(n+k)+1 ∈ A.
29
DEFENDER’S STRATEGY
1. m = 3
2. i = 1, j = 2 i.e. u = a, v = a2
2
Example Determine whether A = {an |n ≥ 0} is regular. We preove that C has a winning strategy.
CHALLENGER’S STRATEGY
2
1. x = an , n2 > n > m > 0
2. k = 1
Hence challenger chooses k = 1 and it is guaranteed taht n2 < n2 + j < (n + 1)2 and hence n2 + j is
never a perfect square.
n
Example Consider the language A = {a2 |n ≥ 0}.
D : Chooses some m > 0.
n
C : Chooses x = a2 with n ≫ m > 0
D : Chooses i ≥ 0 , j > 0 with u = ai , v = aj
such that i + j ≤ m.
n
⇒ x = u v w and w = a(2 −(i+j))
n
C : Chooses k = 2 ⇒ xk = ai a2j a(2n−(i+j)) = a2 + j
CHALLENGER’S STRATEGY
2
1. x = an , n > m > 0
2. k = 2
Result : C always wins provided 2n + j is not a power of 2. 2n + j is a power of 2 for n > 0 only when
j = 2n . But n > m > 0 ⇒ 2n > m. But j ≤ m ⇒ j = 2n is impossible.
Example of a non-regular language which satisfies the condition of the pumping lemma
30
C = {ai+2 bj ck |i, j, k ≥ 0} = L (aa a⋆ b⋆ c⋆ )
Claim 7. A ∪ B ∪ C is not regular.
Proof. Note : B 6⊆ A ∪ C since ∀x ∈ A ∪ C : #a(x) 6= 1.
whereas ∀y ∈ B : #a(y) = 1.
In fact A, B, C are mutually disjoint .
Proof by contradiction : Assume L = A∪B∪C is regular and accepted by a DF A D = h Q, {a, b, c}, δ, q0, F i.
Since A ∩ B = B ∩ C = C ∩ A = φ we have B = L − (A ∪ C) = L∩ ∼ (A ∪ B) from closure properties
that B is regular which is a contradiction.
Claim 8. |Q| > 1.
Proof. Since q0 ∈ Q, |Q| ≥ 1. Since a ∈ B, δ(q0 , a) 6= q0 otherwise any an for n ≥ 0 would be accepted
by D which is not the case. Hence |Q| > 1.
Assume |Q| = n > 1. Consider any string x = abk ck ∈ B, such that k > n. Let
a b b b c c
q0 −→ q1 −→ . . . −→ qk −→ qk+1 −→ . . . −→ q2k+1
x = uvw where u = abi−1 , v = bj−i , w = bk+1−j ck and hence for any x0 = uw is accepted
by D.
But uw = abi−1 bk+1−j ck But 1 ≤ i < j ≤ k
= a bk−(j−i) ck ⇒ j − i ⇒ −(j − i) < 0
⇒ k − (j − i) < k
6∈ B since k − (j − i) 6= k
A ∪ C since #a(uw) = 1.
31
i i−1
b with i ≥ 2 ⇒ v = b ∧ w = b ⇒ v k w = bk+i−1 ∈ A
j j−1
⇒ x = c with j ≥ 2 ⇒ v = c ∧ w = c ⇒ v k w = ck+j−1 ∈ A
i j
b c with i ≥ 1, j ≥ 1 ⇒ v = b ∧ w = bi−1 cj ⇒ v k w = bk+i−1 cj ∈ A
In each case v k w ∈ A, for all k ≥ 0 and hence DEFENDER wins.
⇒ x = a bi cj with i ≥ 1. (if i = 0 then |x| < 2). DEFENDER chooses u = ε and v = x(1) = a .
Then for every choice of k , we have
i i
b c ∈ A if k = 0
v k w = abi ci ∈ B if k = 1
k i i
a b c ∈ C if k > 1
and the DEFENDER wins.
⇒ x=(a2+i bj cl with i, j, l ≥ 0.
a2 bj cl with i = 0, l ≥ 0.
⇒ x=
ai+3 bj cl with i ≥ 0, j, l ≥ 0.
Case 2.2 x = a2 bj cl
⇒ w = bj cl with j, l ≥ 0
32
The above theroem finishes a decision procedure to test whrther a given DF A , D accepts the empty
language. Since the number of strings of length < m is finite , it is neccessary to run the DF A on all
possible string of length < m , to determine it.
Theorem 13. If A is an infinte regular language then there exist strings u, v, w such that uv i w ∈ A for
all i ≥ 0.
Proof. Obvious
Theorem 14. Let D be a DF A with m states m > 0. Then L (D) is infinite iff L (D) contains a string
x, with m ≤ |x| < 2m.
Proof. (⇐) Assume x ∈ L (D) with m ≤ |x| < 2m. By the pumping lemma x may be decomposed into
u, v, w with v 6= ε and such that for all i ≥ 0, uv i w ∈ L (D). Hence L (D) is infinite.
(⇒) Assume L (D) is infinite. Then there exist strings x ∈ L (D) with |x| ≥ 2m. Let x be the stortest
such string and x = x1 x2 where |x1 | = m. and |x2 | ≥ m. Consider the proof of the pumping lemma with
q = δ(q0 , x1 ). Clearly the path from q0 to q. toches (m + 1) states while accepting x1 . This implies ther
exists a q ′ which appears twice
q0 −→ . . . −→ q ′ −→ . . . −→ q ′ −→ . . . −→ q.
Hence x1 may be decomposed into u, v, w such that v = ε, x1 = uvw and δ(q0 , u) = q ′ , δ(q ′ , v) = q ′ and
δ(q ′ , w) = q. Clearly x = uvwx2 ∈ L (D) and state q ′ is repeated implies x′ = uwx2 ∈ L (D). Further
since D is a DF A , v 6= ε. Hence |ε| > 0. |v| ≤ m.
But since x is the stortest word of length ≥ 2m. It follows that |x′ | 6≥ 2m and must be < 2m. But
|x′ | ≥ m. Hence m ≤ |x′ | < 2m and x′ ∈ L (D)
Ultimate Periodicity
Definition 11. A subset ∪ ⊆ N = {0, 1, 2, . . . } is said to be ultimately periodic if there exist m ≥ 0 ,
p > 0 such that for all n ≤ m , n ∈ ∪ iff n + p ∈ ∪.
Example
1. Every finite subset ∪ of N is ultimately periodic with m = max ∪ +1 and any p > 0.
2. Every infinite arithmetic sequence is ultimately periodic with a period defined by the common dif-
ference between successive number and m the starting number
3. The union of any finite collection of infinite arithmetic sequences with different starting points but
the samw common difference is also ultimately periodic. That is [ if Ai = {mi + j d |j ≥ 0, mi ≥ 0},
i > 0 is a finite collection of k ≥ 0 infinite sets. Then ∪ = Ai is ultimately periodic with
i≤k
m = max mi and period d.
33
HOMOMORPHISMS
Definition 12. Let Σ and Γ bealphabets. Then any function h : Σ⋆ −→ Γ⋆ is a homomorphism if
∀x, y ∈ Σ⋆ : h(x.y) = h(x).h(y)
Definition 13. Let h0 : Σ −→ Γ⋆ . h : Σ⋆ −→ Γ⋆ is said to be a homomorphic extension of h0 if
(i) h is a homomorphism
(ii) ∀a ∈ Σ : h(a) = h0 (a).
Lemma 7. For any homomorphism h : Σ⋆ −→ Γ⋆ , h(ε) = ε
Proof. Assume not. Then h(ε) = u 6= ε. This implies for any x ∈ Σ⋆
v = h(x) = h(ε.x) = h(ε).h(x) = uv = u2 v = . . .
which implies h is not even a function from Σ⋆ to Γ⋆
Lemma 8. Every h0 : Σ −→ Γ⋆ has a unique homomorphic extension.
Proof. Consider h : Σ⋆ −→ Γ⋆ a homomorphic extension of h0 . Since h is a homomorphic we have
h(ε) = ε
and ∀a ∈ Σ : h(a) = h0 (a). and ∀x, y ∈ Σ⋆ : h(x.y) = h(x).h(y). If h′ is any other homomorphic
extension of h0 . We have
h′ (ε) = ε = h(ε)
and by induction hypothesis is (assuming h′ (x) = h(x) f or all |x| < n) if y = ax then h′ (y) = h′ (ax) =
h0 (a).h′ (x) = h0 (a).h(x) = h(ax). Hence h = h′ and homomorphic extensions are unique.
Definition 14. Let h : Σ⋆ −→ Γ⋆ be a homomorphism. For any A ⊆ Σ⋆ h(A) = {h(x)|x ∈ A} and for
any B ⊆ Γ⋆ h−1 (B) = {x ∈ Σ⋆ |h(x) ∈ B}. h(A) is called the image of A under h and h−1 (B) is called
the pre-image of B under h.
Theorem 15. Let h : Σ⋆ −→ Γ⋆ be a homomorphism. for any regular B ⊆ Γ⋆ , h−1 (B) is also regular.
Proof. Let DB = h QB , Γ, δB , q0 , FB i be a DF A that accepts B. Let A = h−1 (B). Construct the DF A
DA = h QA , Σ, δA , q0 , FA i with QA = QB , FA = FB and the same start state q0 .
⋆
Define δA (q, a) = δB (q, h(a)) for all q ∈ QA , a ∈ Σ.
⋆ ⋆
Claim 10. δA (q, x) = δB (q, h(x)) for all x ∈ Σ
Proof. By induction on |x| since δA (q, ε) = q = δB (q, ε)
Claim 11. L (DA ) = h−1 (L (DB )).
⋆
Proof. x ∈ L (DA ) ⇔ δA (q0 , x) ∈ F
⋆
⇔ δA (q0 , h(x)) ∈ F
⇔ h(x) ∈ L (DB )
⇔ x ∈ h−1 (L (DB ))
Theorem 16. Let h : Σ⋆ −→ Γ⋆ be a homomorphism. If A ⊆ Σ⋆ is regular then so is h(A).
Proof. If A ⊆ Σ⋆ is regular then there exists a regular expression α ∈ REΣ such that L (α) = A. Let
h(A) = B ⊆ Γ⋆ . We define a function
REΣ −→ REΓ
which translates regular expression α into β i.e. h(α) = β. We define h by induction on the structure of
α as follows.
h(φ) = φ
h(ε) = ε
34
h(a) = if h(a) = y , for each a ∈ Σ.
h(α + α′ ) = h(α) + h(α′ )
h(α.α′ ) = h(α).h(α′ )
h(α⋆ ) = (h(α))⋆
Claim 12. For any α ∈ REΣ , L (h(α)) = h(h(α)).
Proof. Follows by induction on the structure of α
Theorem 17. Any set A ⊆ {a}⋆ is regular iff the set {m|am ∈ A, m ≤ 0} is ultimately periodic.
Proof. (⇒) If A is finite then take m = max{i|ai ∈ A} + 1. Otherwise let A be infinite and consider any
DF A. D which accepts A. Clearly since it is deterministic the transition graph of the DF A (resticted
to the accessible states) looks as follows where there is an initial
a a a a
q0 −→ q1 −→ . . . −→ qi −→ qi+1
Figure 1:
Choose m = the number of distinct initial states and p the length of the loop.
Note If the path through the DF A extend beyond the loop
a a a a a a
q0 −→ . . . −→ qi −→ qi+1 −→ qi+1 −→ . . . −→ qm
Figure 2:
then an equivalent DF A which accepts the same language may be constructed which conforms to Fig 1.
(⇐) Conversely let ∪ be any ultimately periodic set with period p > 0 and starting point n4. Then
construct a DF A{a} with n + p + 1 states which accepts exactly the set {am |m ∈ ∪}
Application of homomorphisms
Corollary 2. Let A ⊆ Σ⋆ be any regular language. Then the set sup = {|x| | x ∈ A} is ultimately
periodic.
35
Proof. Since A is regular, h(A) is also regular where h : Σ −→ {1} is defined as ∀a ∈ Σ : h(a) = 1.
FRom the pervious theorem it follows that h(A) is ultimately periodic. But h(A) is merely th unary
representation of the elements of ∪.
Example
A = {ai bj ci+j | i ≥ 0, j ≥ 0} can be proven to be non-regular by considering the homomorphism
h(a) = h(b) = b and h(c) = c. Then h(A) = {bi+j ci+j |i + j ≥ 0} which is clearly non-regular.
DF A MINIMIZATION
In general, for any regular language there may be several possible DF A designs. It is possible to minimize
the number of states in two stages :
(i) Get rid of inaccessible states. A state is inaccessible if there is no path to it from the start state.
(ii) Collapse states which are “equivalent” in some sense without chnaging the language accepted.
In general inaccessible states may be identified by “walking” forwards from the start state to the final
states and marking all the the states reached. Hence a depth-first search algorithm or a greadth-first
search algorithm should do the job.
In more interesting question is that of collapsing equivalent states.
Given a DF AD consisting only of accessible states, for any string x, x ∈ L (D) iff δ ⋆ (q0 , x) ∈ F . If
q = δ ⋆ (q0 , x) 6∈ F then q is clearly a reject state.
Definition 15. Two states p, q are said to be indistinguishable (denoted p ≈ q) iff ∀x ∈ Σ⋆ : δ ⋆ (q0 , x) ∈
F ⇔ δ ⋆ (q, x) ∈ F . p is said to be distinguishable from q if there exists a distinguished wors w ∈ Σ⋆ such
that either δ ⋆ (p, w) ∈ F ∧ δ ⋆ (q, w) 6∈ F or δ ⋆ (p, w) 6∈ F and δ ⋆ (q, w) ∈ F .
Fact
We now have to show that this quotient construction is well-defined and that D/ ≈ is indeed a DF A.
This involves showing that δ≈ well-defined.
Lemma 9. If p ≈ q then δ(p, a) ≈ δ(q, a) for all a ∈ Σ
Proof. p ≈ q ⇔ ∀x ∈ Σ⋆ : δ ⋆ (p, x) ∈ F ⇔ δ ⋆ (q, x) ∈ F .
Consider any a ∈ Σ and y ∈ Σ⋆ .
Let δ(p, a) = pa and δ(q, a) = qa . For any y ∈ Σ⋆
we have
36
δ ⋆ (pa , y) ∈ F
⇔ δ ⋆ (p, ay) ∈ F
⇒ pa ≈ qa
⇔ δ ⋆ (q, ay) ∈ F
⇔ δ ⋆ (qa , y) ∈ F
⋆
⇔ δ≈ ([q0 ]x , x) ∈ F/ ≈
⇔ [δ ⋆ (q0 , x)]≈ ∈ F/ ≈
⇔ δ ⋆ (q0 , x) ∈ F
⇔ x ∈ L (D)
Consider the indistinguishability relation on the states of D/ ≈. That is let
[p]≈ ∼ [q]≈ ⇔ ∀x ∈ Σ⋆ : δ≈
⋆ ⋆
([p]≈ , x) ∈ F/ ≈⇔ δ≈ ([q]≈ , x) ∈ F/ ≈
Claim 13. [p]≈ ∼ [q]≈ ⇒ [p]≈ = [q]≈ .
Proof. [p]≈ ∼ [q]≈
⇒ ∀x ∈ Σ⋆ : δ≈
⋆ ⋆
([p]≈ , x) ∈ F/ ≈⇔ δ≈ ([q]≈ , x) ∈ F/ ≈
⇒ ∀x ∈ Σ⋆ : δ ⋆ (p, x) ∈ F/ ≈⇔ δ ⋆ (q, x) ∈ F
⇒p≈q
⇒ [p]≈ = [q]≈
37
CONTEXT-FREE LANGUAGES
Definition 16. A grammar G = h V, Σ, S, P i is linear if every production has at most one variable on
the right hand side.
Example The following grammar G is linear but neither right linear nor left-linear.
S −→ A Equivalently
A −→ aB|ε S −→ ε|aB
B −→ Ab B −→ Sb
L (G) = {an bn | n ≥ 0}.
Definition 17. A grammar G = h V, Σ, S, P i is callled context -free if all productions have the form
A −→ α where A ∈ V and α ∈ (V ∪ Σ)⋆ . The language generated L (G) by a contextr-free grammar is
called a context-free language.
Example S> −→ AS= , S= −→ aS= b | ε , A −→ aA|a is a grammar with start symbol S> that
generates {am bn | m > n ≥ 0}. Similarly
S< −→ S= B , B −→ bB | b
generate {am bn | 0 ≤ n < n}. Then S6= −→ S> | S< generates the language {am bn | m 6= n}.
Example S −→ aSb | SS | ε is a grammar that generates balanced parentheses sequence. This grammar
is context-free but is not linear. The language is described by
Derivation Trees
Let G = h V, Σ, S, P i be a CF G. A rooted ordered tree is called a derivation tree for G iff it satisfies the
following conditions.
38
Definition 18. The yield of a tree is the string of symbols obtained by reading the labels on the leaves
from left to right (omitting all occurrences of ε).
S −→ aSb | SS | ε
S ⇒⋆ α ∈ (V ∪ Σ ∪ {ε})⋆ implies
case(i) ∃ y ≺: y ∈ BP
case(ii) ¬ ∃ y ≺: y ∈ BP
39
= #b(z)
S ⇒⋆ y
S ⇒⋆ z.
S ⇒ SS ⇒⋆ yS ⇒⋆ yz.
This implies x = azb for some z ∈ BP . and |z| < |x|. Hence there exists a derivation S ⇒⋆ z.
Therefore the following derivation generate x
S ⇒ aSb ⇒⋆ azb = x
Definition 19. A leftmost derivation is a derivation in which the leftmost variable is rewrithen. Simi-
larly we have rightmost derivations.
Fact. For every derivation tree there is a unique leftmost (rightmost) derivation which corresponds to a
depthfirst traversal of the tree in which the leftmost rightmost subtree is explored before any other subtree.
Theorem 19. Let G = h V, Σ, S, P i be a CF G. Then for every w ∈ L (G) there exists a derivation tree
whose yield is w.
Proof. Since every derivation tree DT is also a partial derivation tree P DT it suffices to prove the
following claim.
Claim 15. For every sentential form α of G there exists a partial derivation tree whose yield is α.
Proof. By induction on the number fo steps in a derivation of G that ends in α.
Induction Step Assume for every sentential from α derivable in k steps there exists a P DT of depth k. Let
α = β in one step. Hence α = α1 Aα2 for some α1 , α2 ∈ (V ∪ Σ)⋆ and β = α1 γα2 for some A −→ γ ∈ P .
Consider the P DT for α, which exists by IH. The yield of this P DT is α. Hence there is a leaf node A
preceded by leaf nodes of the symbols of α1 and succeeses by the symbols of α2 . By expanding the node
labelled A to leaf nodes corresponding to the order of symbols in γ we get a new P DT whose yield id
α1 γα2 = β.
Theorem 20. Every P DT of G represents some sentential from of G.
40
Proof. By induction on the maximum depth of P DT s.
BAsis 0. The only sentential form is the single root node labelled S which is also the leaf node.
Induction Steps. Assume every P DT of depth ≤ k > 0 represents a sentential form α of G where the
yield of the P DT is α.
Consider any P DT of depth k + 1. Consider all the non-leaf nodes at depth k. Clearly all of them are
variable symbols say A1 , . . . , Aj , j > 0. For each Ai , 1 ≤ i ≤ j consider the ordered sequence of children
α1 , . . . , rj . Clearly there are productions Ai −→ γi ∈ P which yielded this tree. Consider the yield of the
P DT upto depth k. By the IH it represents a sentential form.
α = x0 A1 x1 A2 . . . Aj xj
where x0 , . . . , xj ∈ Σ⋆ . Again this is a sentential form of the grammar there must be a deivation
S ⇒⋆ α = x0 A1 x1 . . . Aj xj
β = x0 γ1 x1 . . . γj xj
S ⇒⋆ x0 A1 x1 . . . xj−1 Aj xj ⇒ x0 γ1 x1 . . . xj−1 Aj xj
⇒
.. j-steps
.
⇒ x0 γ1 x1 . . . xj−1 Aj xj
Hence β is a sentential form of the grammar
Corollary 3. (Converse of yield theorem) The yield of any derivation tree is a sentence of L (G).
Q1. We need to distinguish between states q0 and q1 which generate a carry of 0 or 1. Initially there is
only a previous carry of 0. Depending on the carry a triple may acceptable or not. Once an unacceptable
triple arrives the machine goes into a fail state and never recovers. The
41
Current state Next state
0 1 0 1
0 0
@0A @1A 4 q P a b c q′
0 ,1 ,
0 10 10 1 0 1
0 0 0 0 q0
0 0 1 1
@0A @1A @0A @1A 0 0 0 1 qf
1 0 0 1
, , , 0 0 1 0 qf
q0
0 0 1 1 q0
q0
0 1 0 1 0 1 0 0 qf
0 1
@0A @1A 0 1 0 1 q0
1 0 qf
0 1 1 0 q1
Σ
0 1 1 1 qf
q1 0 1 0 10 1 1 0 0 0 qf
0 S 1 1
@0A @0A @1A
1 0 0 1 q0
0, , , 1 0
1 0 1 0 q1
0 1 0 10 1
0 1 1 1 0 1 1 qf
@1A @0A @1A q1
0 0
, , 1 1 1 0 0 q1
1 1 0 1 qf
To make sure the ε is not accepted , it is a good
1 1 1 0 qf
idea to have a start state as well
1 1 1 1 q1
P12 = {A1 −→ x1 S2 | A1 −→ x1 ∈ P, x1 ∈ Σ⋆ } ∪ P2
P⋆ = {S⋆ −→ ε | S}∪
{A −→ xS⋆ | A −→ x ∈ P , x ∈ Σ⋆ }∪
{A −→ xB | A −→ xB ∈ P , x ∈ Σ⋆ }
where V⋆ = V ∪ {S⋆ }.
Since for right linear grammars all sentential forms generated are of the form ⇒ xA where x ∈ Σ⋆ and
A ∈ V , there is always at most one nonterminal at the end of a sentential form. So there is no danger of
having forms such as xAS2 yS2 .
42
0 1 0 1
0 S 1
@0A @0 A
0, , 0 1
1
q1 can’t be a final
0 0 10
@0 A 0 1 state because the
0 11 @1 A 1 0 1
0 01 S @ 0A
q0 1 , ,0
@0 A 1
@1 A
carry it carries cannot
@ 0A ,1 , 0
1 be consumed. Hence
0,
0 1
0 1 0 1 an
0 1 Σ @ bn Ashould not
0 1 @0A @1A 1
0 cn
qs 1 1 0 0 11 1 A qf
1 generate any carry.
@1A 0
,
0 S @ 0A 0
@1
0 @ 0A
0
, ,
1
q1 1
0 1
1
10 1 @ 1A
,,
10 0 @ 0A 1
00 A 0
A@ 1
@0
1 , 0
Q3 A = {0000wwR | w ∈ Σ⋆ }. To show that A is not regular we prove as challenger that for any m > 0
chosen by the defender,the challenger has a winning strategy.
DEFENDER : Since defender decomposes x into 3 parts u, v, w with v 6= ε and |uv| ≤ m, any decompo-
sition of the defender such that x = yy R = uvw necessarily decomposes y into 4 parts y = uvz. Hence
x = yy R = uvzz R v R uR and w = zz R v R uR .
Note : x = αβ
⇔ xR = β R αR
Hence A is not regular.
Note that A is not regular only if |Σ| > 1
If |Σ| = 1 i.e. Σ = {a} then A = {a2n | n ≥ 0} which is regular.
Othersolutions involve choosing a 6= b and x = am bb am and proceeding with the argument.
43
where ≤ is the partial order on regular expressions induced by the inequation
r ≤r+s
and ≥ is ≤−1 .
S −→ SS | aSb | ε
is ambiguous. For example the sentence abab has two possible derivation trees.
S S
S S
S S
a
b S
S Sa S b S S
ε
a S b a S b ε a S b a S b
ε ε ε ε
The ambiguity in this case has to do with now we group “chunks” of balanced parethesis where a “chuck”
is a maximal substring of the form ak bk . Since catenation is associative there are different ways of group-
ing the chunks into sequences of chunks — either (ab ab)ab or ab(ab ab). In this particular case we may
resolve the ambiguity by choosing a “regular” to denote a chunk and use it to generate tail recursive
sequences of chunks as follows.
S −→ ε | CS
C −→ aSb
↑
Wthin each chunks there may be sequences too !
With this grammar the sequence “ababdb” has only once derivation tree as follows.
a C b
ε
44
The language generated by there grammars is BP . Suppose we need to generate BP + = BP − {ε}. We
could proceed as follows.
S −→ C | CS
C −→ ab | aSb.
Exercise
Facts
S ⇒⋆ αi ⇒i+1 ⇒ . . .
Theorem 21. If G is a branching CF G then the parsing problem for G is solvable i.e. There exists an
algorithm which for any x ∈ Σ⋆ can decide membership and if x ∈ L (G) it produces a parse of x.
Proof.
Claim 16. For each steps αi ⇒ αi+1 in a derivation either |αi | < |αi+1 | or the number of terminal
symbols in αi is less than the number of terminal symbols in αi+1 .
Claim 17. Because of claim 1 ,the number of steps in any derivation S ⇒⋆ x is at most 2|x|.
Claim 18. There are only a finite number of leftmost derivations of any given length
2a terminal production is one of the form A −→ a.
45
Proof. In each derivation steps of a leftmost from a given sentential from αi there are atmost |P | different
αi+1 , since only one of the productions may be applied to a leftmost non-terminal in αi to obtain αi+1 .
Hence any derivation
S ⇒ α1 ⇒ . . . αm
allows for at most |P | possibilities for α1 and for each possibilty α1 , there are at most |P | possibities
of α2 , which implies there are only |P |2 different leftmost derivation. Proceeding in this fashion for any
partial derivation of length k. there are at most |P |k different possibilities.
Exhaustively enumerate all partial derivations of length ≤ 2|x|. x ∈ L (G) iff x is the string generated
by one of the derivations and if so the leftmost derivation that generates x also yields a parse of x in the
grammar.
The previous theorem indicates that the parsing problem for general CF G would be solvable provided
Transformations of CF Gs
The substitution Rule. Let G = hV, Σ, S, P i be a CF G.
Lemma 14. If A −→ αBβ ∈ P for A 6= B and B −→ Γ1 | . . . | Γ are all productions of B. Then
b = hV, Σ, S, Pb i where Pb = P − {A −→ αBβ} ∪ {A −→ αΓi β | 1 ≤ i ≤ k} is an equivalent grammar.
G
Proof. Consider any derivation in G such that
S ⇒G ⋆ρAσ ⇒G ραBβσ ⇒G . . . ⇒G τ BΘ ⇒G τ Γi Θ ⇒ . . .
b exists with
where ρα ⇒ ⋆τ and βρ ⇒ ⋆Θ. A corresponding derivation in G
S ⇒G ⋆ x ⇒ S ⇒G ⋆ x. i.e. b
L (G) ⊆ L (G)
b such that
Similarly for any derivation in G
b ⊆ L (G).
Hence S ⇒Gb ⋆ x ⇒ S ⇒G ⋆ x i.e. L (G)
46
Example. G : S −→ ε | cs, c −→ aSb
b : S −→ ε | aSbS
G
As a converse of the substitution rule we could have a factoring rule which allows a rule A −→ αβΓ ∈ P
to be replaced by A −→ α × Γ and a new rule with X 6∈ V introduced as X −→ β.
Factoring allows us to transform the grammar G b : S −→ ε|aSbS to the grammar G : S −→ ε|CS −→ aSb
∃x ∈ L (G) : S ⇒ ⋆ αAβ ⇒ ⋆ x
otherwise a variable is called useless3 . A production is useless if it involves a useless variable (either on
the LHS or in the RHS).
3A variable is useless if either it can’t be reached from S or it can’t produce a terminal string
47
Algorithm 1
V1 old := φ; V1 := {A ∈ V | ∃x ∈ Σ⋆ : A −→ x ∈ P }
IN V : ∀A ∈ V1 : A ⇒ ⋆x for some x ∈ Σ⋆
while V1 old 6= V1 do
{
V1 old : V1 ;
V1 := V1 ∪ {A ∈ V | ∃ A −→ α ∈ P : variable(α) ⊆ V1 }
P1 := { A −→ α ∈ P | symbols (A −→ α) ⊆ V1 ∪ Σ }
}
end while
Algorithm 2
Vbold := φ; Vb1 := {A ∈ V1 | S −→ . . . A · · · ∈ P1 }
while Vbold 6= Vb do
Vbold := Vb
{
foreach B ∈ V1
Vb := Vb ∪ {B ∈ V1 | ∃ A ∈ Vb : ∃ A −→ . . . B · · · ∈ P1 }
Pb := { A −→ α ∈ P1 | symbols (A −→ α) ⊆ Vb ∪ Σ }
}
end while
It is clear that Vb consists of only useful variables and Pb only useful productions from the grammar. It is
also possible to extend the algorithms to construct Σ, and Σ b which gives only the useful terminal symbols
PROOF OF UNAMBIGUITY
To prove that the grammar
S −→ ε | CS C −→ aSb
is unambiguous
C ⇒⋆ x ⇒ #a(x) = #b(x) ∧
∀y 4 x : #a(y) ≥ #b(y)
S ⇒⋆ x ⇒ #a(x) = #b(x) ∧
∀y 4 x : #a(y) ≥ #b(y)
48
G : S −→ ε|aSbS
S ⇒ aSbS.
Since x has ≤ two leftmost derivations and both derivations come from aSbS , there must exists u′ , v ′
such , u′ , v, ∈ L G
x = au′ bv ′ = aubv
but u′ 6= u
Claim 20. u 6= u′ ⇒ u ≺ u′ or u′ ≺ u
Facts. u, u′ , v, v ′ ∈ L (G)
Proof. u 6= u′ ⇒ v 6= v ′ . Since
x = aubv = au′ bv ′
⇒ ubv = u′ bv ′
WLOG assume u ≺ u′
⇒ u′ = uy with |y| > 0.(y 6= ε)
⇒ ubv = uybv ′
⇒ bv = ybv ′
⇒ y(1) = b ∧ y = bz for some |z| ≥ 0
Definition 22. Any production of the A −→ ε is a null production and a variable A ∈ V is called nullable
if A ⇒⋆ ε.
Fact. A −→ ε implies A is nullable.
Lemma 15. There exists an algorithm to construct the set of all nullable variables.
Proof.
Proof.
49
Algorithm 3
VN old := φ ;
VN := {A ∈ V | A −→ ε ∈ P } ;
while VN old 6= VN do
{
VN old : VN ;
foreach A −→ B1 . . . Bk ∈ P : {B1 , . . . , bk } ⊆ VN old do
{VN : VN ∪ {A}}
}
end while
Algorithm 4
1. Compute VN the set of all nullable variables.
2. P0+ = P − {A −→ ε | A −→ ε ∈ P } ;
foreach A −→ α ∈ P0+ : α = αB1 α1 B2 . . . αn−1 Bn αn ∧
variables(α0 α1 . . . αn ) ∩ VN = φ ∧
∀i : 1 ≤ i ≤ k : Bi ∈ VN do
{
foreach sequence s : {1, . . . , b} −→ {0, 1} :
P + := P0+ ∪ {A −→ αs | s : {1, . . . , n} −→ {0, 1}}
where the sequnce s determines which variable occurrences from α must be deleted to obtain αs.
}
Unit productions
Definition 23. Any production of the form A −→ B , A, B ∈ V is called a unit production.
P1+ = P + − {A −→ A | A ∈ V }
50
Let Pb = {A −→ α |α| ≥ 1, α 6∈ V } ∪
b = L (G+ ).
Claim 23. L (G)
Proof. Follows from the substitution lemma
Example 1. S −→ Aa | B
B −→ A | bb
A −→ a | bc | B
S B
S ⇒⋆ A
S ⇒⋆ B
A ⇒⋆ B
B ⇒⋆ A
A
Pb = {S −→ Aa , B −→ bb , A −→ a|bc} ∪
{S −→ a|bc|bb , A −→ bb , B −→ a|bc}.
Algorithm 5
1: Remove all ε-productions (to obtain a positive grammar G+ )
2: Remove all unit productions (to obtain a branching grammar Gc1 )
3: Remove all useless productions (to obtain a branching grammar Gc1 )
51
NORMAL FORMS
Definition 24. A CF G is in Chomsky Normal form (CNF) if all productions are of the form
A −→ BC or A −→ a
where A, B, C ∈ V and a ∈ Σ. It is in Greibach Normal form (GNF) if each its productions is of the
form
A −→ aB1 B2 . . . Bk
for some k ≥ 0 , A, B1 , B2 , . . . , Bk ∈ V .
Fact
1. Every grammar in CN F or GN F is positive and branching. Hence these normal forms do not
allow for generation of ε or unit productions.
Theorem 29. Every branching CF G , G may be transformed into an equivalent one in CN F .
Proof. Since G is branching it has
)
(i) no null productions Even if it has we may
assume wlog that they
and (ii) no unit productions have been removed.
The construction of the equivalent CF G , Gb in CN F proceeds as follows.
A −→ a or A −→ B1 B2 . . . Bk , k > 1
52
b = L (G′′ ) = L (G′ ) = L (G)
Claim 26. L (G)
A −→ βB | . . . | βs B.
and adding the following productions for B
B −→ α1 . . . | αr
B −→ α1 B | . . . |αr B.
The removal of left-recursion involves adding a new variable E 6∈ V , replacing the left-recursive produc-
tions of A by
A −→ B1 C1 E | . . . | Bn Cn E
|a1 E| . . . |am E
and adding the following productions for E
E −→ D1 | . . . |Dp (1)
|D1 E| . . . |Dp E (2)
53
E −→ D1 | . . . |Dp (3)
|D1 E| . . . |am E (4)
Proof. Since G′ was derived from a CN F G, it is only necessary to transform the productions of the
forms 1 , 2 ,tc3, 4 into CN F .
1 Replace A −→ B1 C1 E | . . . | Bn Cn E by
A −→ B1 E1 | . . . |Bn En —– 1′
where E1 , . . . , En are new variable
) and add the productions
E1 −→ C1 E
—– 1′′
En −→ Cn E
2 Since G was already in CN F there exist variables Xa ∈ V for each a ∈ Σ and the productions
Xa −→ a for each a ∈ Σ in G.
Replace all the productions A −→ Xa1 E | . . . | Xam E —– 2′
3 Since D1 , . . . , Dm are variables belonging to the original CN F G , their productions in G
D1 −→ δ11 | . . . | δ1k1
..
.
Dm −→ δm1 | . . . | δmkm
are already in the required form for a CN F . Hence by replacing the productions E −→ D1 | . . . | Dp by
the set of productions.
E1 −→ δ11 | . . . |δ1k1
|
.. —– 1′′
.
|δm1 | . . . |δmkm
we obtain the productions 3 of E to conform to CN F .
54
Proof. Since G is in CN F we may assume without loss of generality that for every a ∈ Σ (which occurs
in a sentence of L (g)) there exists an unique associated variable Xa and a unique production Xa −→ a.
Even if there is a variable Xab (with Xab −→ a|b) which has more then 1 terminal production , it may
be split into as many different variables as the number of terminals. Correspondingly all occurences of
Xab in any production may be spilt into two productions (one for each terminal).
Claim 29. V may be partitioned into two disjoint sets
V = N ∪ XΣ
where XΣ = Xa |a ∈ Σ and N = V − XΣ .
∀A ∈ N. ∀Xa ∈ XΣ : A < Xa .
Transforming G : If G already satistfies the IIP there is nothing to prove. Otherwise assume for conve-
nience that V = {A1 , . . . , Am } are all the varibles of V with index(Ai ) = i and ordered by the usual < on
integers.
Let k, 1 ≤ k ≤ m be the smallest index which violates the IIP ,i.e there exists a production.
Ak −→ Aj Al , k > j —— 1
and ∀i : 1 ≤ i < k : all the productions of Ai have the IIP.
In particular, all the productions of Aj viz.
j < min(l1 , . . . , lm ) < k ⇒ k − j > k − min(l1 , . . . , lm ). which implies PROCESS I has reduced the
value of.
Repeat process I as many times as necessary till either all productions of Ak have the IIP or there are
some left-recursive productions of Ak .
Claim 30. The smallest index that violates IIP in the grammar is > k
Case. There are left-recursive productions of Ak but all non-left-recursive productions satisfy IIP.
55
Remove left-recursion in each case by introducing a new varibale B and extend the ordering < so that
B < any existing variable of V .
Claim 31. The smallest index of the new grammar that may violate IIP is > k.
However the grammar may not be in CN F . The following case takes this into account
Case k < l1 , . . . , lm , . . . . Then the smallest index that violates IIP is > k. However the grammar is no
longer in CN F . Let the new productions of Ak be
Ak −→ Al1 B1 | . . . |Aln Bn
B1 −→ α1
..
. where ∀i : 1 ≤ i ≤ n : αi ∈ V 2 .
Bn −→ αn
Extend the total ordering < to include the new variables B1 , . . . , Bn such that
∀i, j : Bi < Bj
Claim 32. The grammar obtained above is in CN F with the smallest index violating the IIP > k.
From the above two cases it follows that the diffence (m − k) reduces by the above processes. Hence the
above processes need to be repeated only at most (m − k) times to obtain an equivalent grammar in CN F
with the IIP
Theorem 30. (GN F ) Every branching CF G may be transformed into an equivalent one in GN F .
Proof. W LOG we may assume that G is a CN F with the IIP. Let V = {A1 , . . . , Am } be the variables
in increasing order of index in the total ordering.
Claim 33. Am has only terminal productions.
PROCESS 2
Replace every production of the form
B −→ Am C
with B −→ a1 C | . . . | an C
wher Am −→ a1 | . . . | an are the only productions of Am
Claim 34. the highest indexed variable occuring as first symbol on the right hand side of any production
is Am−1 .
Claim 35. Since there is no left recursion all the productions of Am−1 are of the form
Am−1 −→ a1 α1 | . . . | ap αp .
where α1 , . . . , αp ∈ V .
56
Proof. The replacement of Am by terminal symbol all prod. implies all productions are either of the form
Fact. Every linear grammar is also contxt-free with the proof of a pumping lemma for linear languages.
We will also show the strict containment of the linear languages within the class of CF Ls
an
bn
LΣ cn
RΣ
Theorem 31. Let A ⊆ Σ⋆ be an infinite linear language. Then there exists m > 0 such that any
x ∈ A, |x| ≥ m may be decomposed into z = tuvwx with
|tuwx| ≤ m
|uw| ≥ 1
such that for all i ≥ 0, tui vwi x ∈ A.
In other words
A ⊆ Σ⋆ is linear
⇒ ∃ m > 0 : ∀z ∈ A : |z| ≥ m :
z }| {
∃t, u, v, w, x : z = tuvwx ∧ | tuwx| ≤ m ∧ |uw| ≥ 1
∧ ∀k ≥ 0 : zk = tuk vwk x ∈ A
57
Proof. Since the language is linear there exists a linear grammar gnerating it. As in the case of a CF G
we may use the same techniques to
)
(i) remove unit productions transfer G into a positive
(ii) remove ε-productions branching grammar.
(iii) transfer G to generate A − {ε}
Consider a derivation tree as in the case of the lemma for CF Ls. Since the derivation tree may be
arbitrarily deeep consider only the first and second occurences of the first variable A that repeats itself
in the derivation tree.
Since |V | is bounded it follows that the other variables which do not repeat can only produce only
bounded-length terminal strings t and x. Similarly between the two occurences of A there are only
bounded length strings u and w may be produced.
Choose m = |t| + |u| + |w| + |x|.
Finally u and w may be pumped as many times as we please to obtain derivations of
In other words
A ⊆ Σ⋆ is an infinite CF L
⇒ ∃ m > 0 : ∀z ∈ A : |z| ≥ m :
∃t, w, x, v : z = tuvwx ∧ |uvw| ≤ m ∧ |uw| ≥ 1
∧ ∀k ≥ 0 : zk = tuk vwk x ∈ A
Let n = max{|α| | ∃A ∈ V : A −→ α ∈ P }.
⇒ Any z ∈ A − {ε} has a derivation of length ≥ |z| n . Since A − {ε} is infinite, there exists arbitrarily
long derivations and hence there exists derivation trees of arbitrary height.
58
S Consider such a derivation tree of
occurences of A.
v
⇒ A is not regular
⇒ A is not a CF L
⇒ A is not linear
Examples
1. an bn cn | n ≥ 0 is not CF . For any m > 0 chosen by D,C chooses am bm cm and does a case analysis.
59
But that is impossible since that implies uvw = ai bm ci and |uvw| > m for i > 0 and |v| = m if
i = 0 we may use the b′ s in v to ensure that
zk 6∈ {an bn cn | n ≥ 0}.
2. {ww | w ∈ {a, b}⋆ } is not CF . For any m > 0, C chooses am bm am bm and do case analysis.
3. {an! |n ≥ 0} is not CF Use essentially some argument as for regular case since there is only one
terminal symbol.
2
4. {an bm |n = m2 } is not CF . C chooses am bm .
5. {w ∈ {a, b}⋆ | #a(w) = b(w)} is not linear5 but is CF
since S −→ SS | aSb | bSa | ε generate it.
Define Gs = h V, Γ ∪ Π, S, Ps i where
Ps = {A −→ [i B ]i C | A −→ BCis the i-th rule in P }.
∪ { A −→ a | A −→ a ∈ P }.
5C chooses am b2m am
60
S S
A B B A
a C C C C a
a a a a
We now proceed by induction assuming the result holds for all strings of length < |x|. i.e.
61
Induction steps. Do an analysis of the first step of S ⇒⋆ x.
S S
B A
[ B ]
( A ) C
{ C }
{ C } C
a
a a a a
a
(a) {a}a [ {a}a ]a]
Theorem 33. Let h : Γ ∪ Π −→ Γ ∪ {ε} be the homomorphism which maps every symbol of Γ to itself
and erases every bracket i.e. h([i ) = ε = h(]i ). Then
Ind Steps. Otherwise A ⇒ [i B ]i C ⇒⋆ [i v ]i w = x and it follows that there exists smaller derivations
B ⇒⋆ v and C ⇒⋆ w and it is also clear that x is of the form u[i v ]i w with u = ε, v, w ∈≺. By the
62
IH there exists derivations S ⇒⋆ v and S ⇒⋆ w in ≺. Hence we have S ⇒ SS ⇒ [i S ]i S ⇒⋆ [i v ]i w
in ≺
S ⇒ [i B ]i C ⇒⋆ [i v ]i w = x
Gs Gs
63
Corollary 6. Let G = h V, Γ, S, P i be a grammar in CN F . Then for a suitably chosen Π ∩ Γ = φ of
pairs of bracketing symbols,there exists a regular language R such that
L (G) = h(R ∩ L (Gs )).
Theorem 34. CHOMSKY − SCHUTZEENBERGER. A language L ⊆ Γ⋆ is context-free if and
only if there is a regular language R and a collection Π of matching bracket symbol pairs such that
L = h(R ∩ ≺)
Proof. (⇒) For any G in CN F with L (G) = L − ε, the previous corollary shows the result.
If ε ∈ L , then
(⇒) Since ≺ is context-free ot follows that L is context-free , provided we can show that (i) the intersection
of regular and a CF L is a CF L and (ii) homomorphisms preseve CF − ness
Theorem 35. The family C FΣ is closed union,concatenation and ⋆-closure
Proof. Let G1 = hV1 , Σ, S1 , P1 i and G2 = hV2 , Σ, S2 , P2 i be CF Gs generating A1 , A2 ∈ C FΣ respectively.
Assume V1 ∩ V2 = φ
Union. Let G∪ = h V∪ , Σ, S∪ , P∪ i be defined as follows
V∪ = V1 ∪ V2 ∪ {S∪ }
P∪ = P1 ∪ P2 ∪ {S∪ −→ S1 |S2 }.
Claim 37. L (G∪ ) = L (G1 ) ∪ L (G2 ) = A1 ∪ A2
Concatenation. Let G = h V •, Σ, S•, P • i be the grammar
V • = V1 ∪ V2 ∪ {S•} P • = P1 ∪ P2 ∪ {S• −→ S1 S2 }.
Claim 38. L (G•) = L (G1 ) • L (G2 ) = A1 • A2
⋆-closure. Let G⋆ = h V⋆ , Σ, S⋆ , P⋆ i where
V⋆ = V1 ∪ {S⋆ } , P⋆ = P1 ∪ { S⋆ −→ ε|S1 S⋆
Claim 39. L (G⋆ ) = L (G1 )⋆ = A⋆1
PUSHDOWN AUTOMATA
Definition 28. A nondeterministic push-down automaton(NPDA)
N = h Q, Σ, Γ, δ, q0 , ⊥, F i
Q is a finite set of states.
Σ is a finite input alphabet.
Γ is a finite stack alphabet.
δ ⊆ (Q × (Σ ∪ {ε}) × Γ) × (Q × Γ⋆ ) is a finite transition relation .
= Q × (Σ ∪ {ε}) × Γ −→f 2Q×Γ
⋆
64
a Input
Finite State
P Control
A Stack
B1
..
.
Bk
β = B1 . . . Bk ⇒
↑
((p, a, A), (q, β)) ∈ δ
↑
machine is in state p next machine goes into state q
input symbol is a and top and replaces the A on the
of stack is A stack by β
this is an atomic
step.
↑
no move is possible if the stack is empty
(p, ay, Aβ) −→⋆ (q, y, αβ) if ((p, ε, A), (q, α)) ∈ δ
N
Acceptance. There are two notions of acceptance. Actually there are two kinds of N P DAs
65
L (NE ) = {x ∈ Σ⋆ |(q0 , x, ⊥) −−→⋆ (q, ε, ε)} −→ Then F is irrelevent and may be taken to be φ
NE
Note. But in both cases the input should have been completely consumed.
((q, ε, ⊥), (q, ε))
At any stage the stack contains
Q = {q}
((q, a, ⊥), (q, A))
Σ {a, b}
(i) only A′ s or
Γ = {A6 , B 7 , ⊥}
((q, ε, ⊥), (q, ε))
(ii) only B ′ or
Note
1. A indicates there are more a′ s than b′ s
((q, b, ⊥), (q, B))
(iii) neither A′ s nor B ′ s
2. B indicates there are more b′ s than a′ s
((q, a, A), (q, AA)) The stack would not
contain a mix
of A′ s & B ′ s
((q, a, B), (q, ε))
((q, b, A), (q, ε))
((q, b, ⊥), (q, BB))
Q = { qL 8 , qR } ∪ {qp 9 }
Acceptances
Σ = {a, b}
by empty
stack
Γ = {a, b, ⊥}
Note
1. qL is for start state.
66
2. qp is for acceptace by final state.
((qL , ε, ⊥), (qR , ⊥)) −→ Candidates for the middle of the string.
((qL , a, ⊥), (qL , a⊥))
NONDETERMINISTIC
ր տ
((qL , a, a), (qL , aa)) ((qL , a, a), (qR , ⊥))
((qR , a, a), (qR , ε)) This does not directly allow acceptance by
final state since then the string ‘abb′
could be accepted
((qR , b, b), (qR , ε))
((qR , ε, ⊥), (qR , ε))
| {z }
This transition allows acceptance by empty stack too.
Acceptances : E vs F
Note. When a N P DA accepts by empty stack,then F is irrelevant and may safely be taken to be φ.
Theorem 36. The two acceptances are equivalent i.e. for every NE there exists a NF with LE (NE ) =
LF (NF ) and vice-versa.
Proof. Let N = h Q, Σ, Γ, δ, q0 , ⊥, F i be any N P DA either E-type or F -type. Let s and t be two new
states not in Q. and let 4 be a new stack symbol. Now consider
such that δ ′ = δ ∪
67
Proof. Suppose x ∈ L (N ′ ). Then for some n ≥ 0.
(s, x 4) −−−′→ (q0 , x, ⊥ 4) −−−′→n (q, y, γ, 4) −−−′→ (t, y, γ 4) −−−′→⋆ (t, ε, ε).
N N N N
| {z }
Since y = ε ⇓
(q0 , x, ⊥) −−−→n (q, ε, γ)
N
where q ∈ G.
Now consider (q, ε, γ 4) −−−′→ (t, ε, γ 4)
N
Claim 42. LE ⊆ L (N ′ ) if N = NE
and LF ⊆ L (N ′ ) if N = NF
Proof. If x ∈ LE (N ) and N = NE then (q0 , x, ⊥) −−−→⋆ (q, ε, ε). Then it is easy to see that
N
⋆
(s, x 4) −−−′→ (q0 , x, ⊥ 4) −−−′→ (q, ε, 4) −−−′→ (t, ε, 4) −−−′→ (t, ε, ε) and hence x ∈ LE (N ′ ). On the
N N N N
other hand, if x ∈ LF (N ) and N = NF then (q0 , x, ⊥) −−−′→⋆ (qF , ε, γ) and we may construct
N
⋆
(s, x, 4) −−−′→ (q0 , x, ⊥ 4) −−−′→ (qF , ε, γ 4) −−−′→ (t, ε, γ 4) −−−′→⋆ (t, ε, ε)
N N N N
NP DAs For CF Ls
68
(⇐) By induction on n where (q0 , x, S) −−−→n (q0 , δ, α).
N
Hence by the induction hypothesis we have S ⇒n−1 yβ. The move (q0 , a, β) −−−→ (q0 , ε, α) is possible
G N
only if β = Aγ for some A ∈ V and γ ∈ V ⋆ and further α = ηγ for some η ∈ V ⋆ . But from the
construction of N we know that this is possible only if
(q0 , η) ∈ δ(q0 , a, A)
iff A −→ aη ∈ P .
The Case When ε ∈ A. The grammar G for A+ is modified to include a new start symbol S0 6∈ V
with the productions S0 −→ Sε. It is then clear that
The N P DA, N may then be modified N0 with S0 being the new stack marker and
CF Gs For NP DAs
The converse of the previous theorem viz. that the language accepted by a N P DA is a CF L is proven
in two parts
69
Lemma 25. The language E-accepted by a N P DA with a single state is a CF L
↑
The Construction of the previous theorem
needs to be inverted
⋆ This implies that all state information may be maintained on the stack.
Theorem 38. The language accepted by any N P DA is context-free.
Lemma 27. Every N P DA may be simulated by a N P DA with a single state.
Proof. WLOG from the construction of N ′ we may assume that the given N P DA N has a single final
state.
N = h Q, Σ, Γ, δ, s, ⊥, {t} i
and that N can empty its stack after entering this state.
N ⋆ = h {⋆}, Σ, Γ⋆ , δ ⋆ , ⋆, h s⊥t i, φ i
where Γ× = Q × Γ × Q.
Proof. By induction on n.
Basis n = 0. p = q , x = ε and k = 0 is trivial.
Ind Step. Assume n > 0 and the first step is such that
(p, x, B1 B2 . . . Bk ) −−−→ (r, y, c1 . . . cm B2 . . . Bk ) −−−→n (q, ε, ε)
N N
with x = cy and ((p, c, B1 ), (r, C1 . . . Cm )) ∈ δ and c ∈ Σ ∪ {ε}.
70
By the IH there exists r0 , r1 , . . . , rm−1 q1 , . . . , qk−1,qk such that r0 = r , q = qk and
,
(y , h r0 c1 r1 ih r1 c2 r2 i . . . h rm−1 cm q1 ih q1 B2 q2 i · · · = lanqk−1 bk qk −−→n (⋆, ε, ε).
N×
Also by the construction of N ⋆
((⋆, c, h pB1 q1 i), (⋆, h r0 c1 r1 ih r1 c2 r2 i . . . h rm−1 cm q1 i)) ∈ δ ×
combing these we get
(⋆, x, h pB1 q1 ih q1 B2 q2 i . . . h qk−1 Bk qk i)
−−→1 (⋆, y, h r0 c1 r1 ih r1 c2 r2 i . . . h rm−1 cm q1 ih q1 B2 q2 i . . . h qk−1 bk qk i)
N×
−−→1 (⋆, ε, ε).
N×
n
(⇐) Suppose (⋆, x, h q0 B1 q1 ih q1 B2 q2 i . . . h qk−1 Bk qk i) −−→ (⋆, ε, ε)
× N
and let ((⋆, c, h q0 B1 q1 i), (⋆, h r0 c1 r1 ih r1 c2 r2 i . . . h rm−1 cm q1 i)) be the first transition applied where
c ∈ Σ{ε} and m ≥ 0.
DERTERMINISTIC P DAs
Definition 29. A DP DA is a N P DA satisfying the following constraints
∀a ∈ Σ ∪ {ε} : ∀A ∈ Γ : ∀q ∈ Q : |δ(q, a, A)| ≤ 1
i.e. δ : Q × (Σ ∪ {ε}) × Γ−7→Q × Γ⋆
(i) in other words δ is a partial function which may be undefined for certain triples in Q × (Σ ∪ {ε}) × Γ
(ii) δ(q, ε, A) = (q ′ , α) ⇒ ∀a ∈ Σ : ⇒ δ(q, a, A) is undefined.
Some of our other examples e.g. {wwr | w ∈ {a, b}⋆ } have non-deterministic transitions which decide
whether the midpoint has been reached.
71
However wcwR | w ∈ {a, b}⋆ has a deterministic P DA since the midpoint is clearly marked by a different
symbol.
Note that by changing Σ to Σ ∪ {c} where c is a new terminal symbol,the basic construction of P DAs
remains unchanged.
Clearly the language A1 ∪ A2 has a P DA obtained by taking the union of δ1 ∪ δ2 . However this P DA is
nonderterministic because of the first two rules. But we can go further.
Theorem 39. A1 ∪ A2 is not accepted by any DP DA.
Proof. Assume there exists a DP DA D12 = h Q12 , Σ, Γ, δ12 , q0 , ⊥, F12 i accepting A1 ∪ A2 .
Claim 45. There exists a N P DA which accepts the language A! ∪ A@ ∪ A3 where A3 = {an bn cn |n > 0}.
Proof. Let N123 = h Q123 , Σ, Γ, δ123 , q0 , ⊥, F123 i
where Q123 = F12 ∪ F3 i.e. Q3 = {b q | q ∈ Q12 }
F123 = F12 ∪ F3 with F3 = {b q | q ∈ F12 }
δ123 = δ12 ∪ δ3 where δ3 = {((qF , ε, A), (c qF , A) | qF ∈ F12 , A ∈ Γ}
∪ {((qbi , c, A), (qbj , α) | α ∈ Γ⋆ ∧
δ12 (qi , b, A) = (qj , α)}
δ3
cn
ε ε
D12
a n bn bn
72
We have for d12 to accept an bn , n > 0
(q0 , an bn , ⊥) −−→⋆ (qF , ε, α) with qF ∈ F is unique and since D12 is determinstic ,for an b2n we
D12
have
(q0 , an b2n , ⊥) −−→⋆ (qF , bn , α) −−→⋆ (qF ′ , ε, α′ )
D12 D12
with qf ′ which implies that for aN bn cn we have by construction
(q0 , an bn cn , ⊥) −−−→⋆ (qF , cn , α) −−−→⋆ (qc′ ′
F , ε, α ) with q
cF ∈ F123 .
N123 N123
Hence N123 acceptes A123
But we know that A123 is not CF since A3 is not CF . Hence the assumption that there is a DP DA D12
which accepts A12 is wrong !
73