You are on page 1of 61

IV.

CONTEXT FREE
LANGUAGES
The most widely used specication tool for
the syntactic structure of programming lan-
guages.
121
Example Various ways to specify the syntax
of a programming language.
(1) Syntax Charts
term
-

_
+
`
_
-
-
-
?
6
-
term

_
? ? ?
6
? ?
6
+ or
simple expression
factor
- -
Term
factor

__

6
? ?
?
?
?
?
?
?
?
*
/
div mod and
unsigned constant
- -
Factor
variable
expression
factor
`
_
`
_
_
`

- -
6
- - - -
- -
( )
not
122
(2) BNF
<simple expression> ::= <term> [ + <term> [ - <term> [
<simple expression> + <term> [
<simple expression> - <term> [
<simple expression> or <term> [
<term> ::= <factor> [ <term> * <factor> [
<term> / <factor> [ <term> div <factor> [
<term> mod <factor>[ <term> and <factor>
<factor> ::= <unsigned constant> [ <variable> [
(<simple expression>) [ not <factor>
(3) Context-free Grammars
E T [ +T [ T [ E +T [ E T [ E or T
T F [ T F [ T/F [ T div F [ T mod F
[ T and F
F a [ (E) [ not F
123
1. Context-free grammars
Denition A context-free grammar (CFG) G
is specied by a quadruple (N, , P, S) where
N : the set of nonterminals (variables);
: the set of terminals, N = ;
P N (N )

: the set of productions;


S N : sentence symbol;
and N, , and P are all nite.
Examples
(1) Dene a CFG for a
n
b
n
[ n 0
S aSb [ N = S, = a, b
(2) Dene a CFG for a
m
b
n
[ m n 0
S aSb [ aS [
124
Rewriting or derivation
A CFG generates a word by rewriting
(or derivation)
Let G = (N, , P, S) be a CFG and
,
/
(N )

. If =
1
A
2
, for
A N,
1
,
2
(N )

, A P, and

/
=
1

2
, then we say that
can be rewritten as
/
, or say that
derives
/
, denoted

/
or
1
A
2

1

2
So, is a binary relation over (N )

.

i

/
, i > 0, if
/
can be obtained from
in i rewriting steps.
125

+

/
if
/
can be obtained from in at
least one rewriting step.


/
if =
/
or
+

/
.
L(G) = w

[ S

w
Example
G = (N, , P, S) where
N = S
= a, b
P : S aSbb [
Then
S
S aSbb abb
S aSbb aaSbbbb aabbbb
For i > 0,
S
i
a
i
S(bb)
i
a
i
b
2i
Intuitively,
L(G) = a
i
b
2i
[ i 0
126
Example
G = (N, , P, S) where
N = S, A, B, = a, b,
P : S A [ B
A aA [
B aBb [ ab
It is clear that
L(G) = a
i
[ i 0 a
i
b
i
[ i 1
Example
G = (N, , P, S) where
N = S
= a, b
P : S [ aSb [ bSa [ SS
Consider,
S aSb abSab abab
S SS aSbS aSbbSa aaSbbbSa
aaSbbbbSaa aabbbbSaa
aabbbbaa
L(G) = w a, b

[ [w[
a
= [w[
b
, intuitively.
127
Formally, we prove it as follows:
Given
(1) G = (S, a, b, P, S) where
P : S [ aSb [ bSa [ SS
(2) L = w [ w a, b

and [w[
a
= [w[
b
,
prove that L(G) = L.
Proof:
(I) First we prove that L(G) L.
Claim 1. For all w

such that
S
+
w, [w[
a
= [w[
b
.
Proof of Claim 1: (prove by induction on
n, the number of derivation steps)
Basis : n = 1, S , [[
a
= [[
b
= 0.
I.H. : Assume it holds for any 1 k < n,
i.e., that S
k
w, k < n, implies [w[
a
= [w[
b
.
I.S. : Consider S
n
w, n > 1. There are
three cases concerning the rst step
of the derivation.
128
Case 1 : S aSb
n1
aw
/
b.
So, S
n1
w
/
.
By I.H. [w
/
[
a
= [w
/
[
b
. This
implies [w[
a
= [w[
b
.
Case 2 : S bSa
n1
bw
/
a = w
S
n1
w
/
implies [w
/
[
a
= [w
/
[
b
by I.H.
So, [w[
a
= [w[
b
.
Case 3 : S SS
n1
w
1
w
2
= w, where
S
i
w
1
, S
j
w
2
for i, j < n.
By I.H., [w
1
[
a
= [w
1
[
b
, [w
2
[
a
= [w
2
[
b
.
therefore, [w[
a
= [w[
b
.
So, w L(G) implies w L.
(II) Prove that L L(G).
Claim 2 if w L, then S
+
w in G.
Proof of Claim 2: By induction on the num-
ber of as in w.
129
Basis : [w[
a
= 0. Then w = . S .
I.H. : Assume that if [w[
a
< n and w L
then S
+
w.
I.S. : [w[
a
= n.
Case 1 : w = axb. Thus [x[
a
< n.
Hence S
+
x by I.H. . So,
S aSb
+
axb = w in G.
Case 2 : w = bxa. As above.
Case 3 : w = axa. Then
w = yz for some y, z s.t.
[y[
a
= [y[
b
, [z[
a
= [z[
b
. y, z ,=
(We prove it later).
Hence, S
+
y and S
+
z by I.H.
Therefore, S SS
+
yz = w.
Case 4 : w = bxb. As above.
So, L L(G). We now conclude that L =
L(G).
130
Claim If w L (i.e.[w[
a
= [w[
b
) and w = axa,
then w = yz such that [y[
a
= [y[
b
and [z[
a
= [z[
b
.
Proof:
Let w = c
1
c
2
c
3
. . . c
2n
, c
i
a, b, for all 1 i
2n.
Let w
i
= c
1
c
2
. . . c
i
for 1 i 2n.
Now, consider the sequence:
[w
1
[
a
[w
1
[
b
, [w
2
[
a
[w
2
[
b
, . . . ,[w
2n1
[
a
[w
2n1
[
b
Obviously, [w
1
[
a
[w
1
[
b
= 1 and
[w
2n1
[
a
[w
2n1
[
b
= 1.
Let k, be the smallest integer s.t.
[w
k
[
a
[w
k
[
b
= 1. Then 2 < k 2n 1.
Clearly, [w
k1
[
a
[w
k1
[
b
= 0
Let y = w
k1
, z = c
k
c
k+1
. . . c
2n
.
Therefore, w = yz and [y[
a
= [y[
b
, [z[
a
= [z[
b
.
131
Regular grammars
Denition A CFG, G = (N, , P, S) is said to
be linear if every production in P is either of
the forms
A x, x

, A N,
or A xBy, x, y

, B N
Denition A CFG G = (N, , P, S) is said to be
right linear if every production in P is of one
of the forms:
A x, x

, A N,
A xB, B N.
Denition . . . Left linear . . .
A x, . . .
A Bx, . . .
132
Denition A CFG G is said to be regular
if it is right linear or left linear
Since left linear grammars dene the same
set of languages as right linear grammars,
by regular grammars we usually mean
right linear grammars.
The following denition is equivalent to the
above one.
Denition A CFG G = (N, , P, S) is
regular if every production in P is
of one of the following forms:
A a, a , A N
A aB, B N.
Example
1. S
1
[ aS
1
[ bB
B [ bB
2. S
2
AA
A aA [
133
Lemma 1 For every regular grammar G = (N, , P, S),
there is an -NFA M such that L(G) = L(M).
Proof: Let M = (Q, , , s, f) where
Q = N f
s = S
= (A, a, B) : A aB is in P
(A, a, f) : A a is in P
Claim A

w in G i Aw
+
f in M.
Proof by induction on derivation lenght.
Example G = (N, , P, S) when P:
S aS [ [ bB, B bB [ ,
Construct an -NFA M such that
L(M) = L(G).
134
Lemma 2 For every NFA M = (Q, , , s, F)
there is a regular grammar G such that L(G) =
L(M).
Proof: Let G = (N, , P, S) where
N = Q
S = s
P = p aq : (p, a, q)
p : p F
Example M:
1
2
3
0
0
1
0
Construct a regular grammar G using the
above method
G = (N, , P, S)
N = (1), (2), (3), S = (1)
P : (1) 0(2), (1) 0(3), (2) 1(3)
(3) 0(2), (1) , (2)
135
Claim For any w

, pw

q in M
i p

wq in G.
The claim implies sw

f in M, for some
f F, i s

wf w in G.
Therefore, w L(M) i w L(G). 2
Theorem 1 Regular grammars dene exactly
the family of regular languages (i.e. DFA lan-
guages).
Proof: By Lemma 1 and 2.
Theorem 2 /
REG
/
CF
Proof : By Theorem 1 and the fact that
a
n
b
n
[ n 0 is not regular.
136
Derivations of CFGs
Denition Let G = (N, , P, S) be a CFG. A
word V

(i.e., (N )

) is a
sentential form if S

.
If

, then is also called a sentence.


Now we consider the derivation of a CFG
G
1
= (N
1
,
1
, P
1
, S
1
) where P
1
:
S
1
T [ S
1
+ T
T F [ F T
F a [ (S
1
)
To derive a + a, we have
S
1
S
1
+ T T + T
and from T + T there are 6 ways to derive
a + a:
(1) T + T F + T a + T a + F a + a
(2) T + T F + T F + F a + F a + a
(3) T + T F + T F + F F + a a + a
(4) T + T T + F T + a F + a a + a
137
(5) T + T T + F F + F F + a a + a
(6) T + T T + F F + F a + F a + a
The above derivations can be represented by
rooted directed trees.
For the 6 sequences of derivations, we have:
S
1
S
1
+
T
F T
F
a
a
S
1
S
1
+
T
F T
F
a
a
Consider another CFG G
2
= (N
2
,
2
, P
2
, S
2
) where
P
2
: S
2
S
2
+ S
2
[ a
To derive a + a + a, we have
S
2
S
2
+S
2
S
2
+S
2
+S
2
a+S
2
+S
2

2
a+a+a
S
2
S
2
+S
2
S
2
+S
2
+S
2
a+S
2
+S
2

2
a+a+a
138
S
2
S
2
+ S
2
S
2
+
S
2
a
a a
S
2
S
2
+ S
2
a S
2
+
a
S
2
a
,
,
,
l
l
l
Hence, dierent derivation sequences do not
necessarily represent dierent structures.
To solve this problem we use
(1) syntax trees, or
(2) canonical derivations.
139
Syntax trees
Denition Let G = (N, , P, S) be a CFG.
Then T is a syntax tree with respect to G
if every node u in T satises the following
conditions:
(1) If u is an external node, then it is
labelled with a symbol in N or ,
and in the latter case it is the only child
of its parent.
(2) Otherwise, u is labelled with a symbol
A in N, and it has k children, k 1,
labelled with X
1
. . . X
k
, from left to right
and,
A X
1
. . . X
k
is in P.
140
(2) Canonical derivations
A rewriting (derivation) step is called a
rightmost rewriting (derivation) step if the
rightmost nonterminal is being rewritten.
For example, aSSb aSbSab is a rightmost
derivation step.
A sequence of rightmost derivation steps is
called a rightmost derivation (sequence).
Leftmost derivations are similarly dened.
Notations
Rightmost derivations:

R

+
R

R

n
R
Leftmost derivations:

L

+
L

L

n
L
141
Let G = (N, , P, S) be a CFG.
Let w L(G).
If there are two distinct derivation trees (left-
most or rightmost derivations) that derive w
by G, then w is said to be ambiguous with
respect to G.
G is said to be ambiguous if there is at least
one word in L(G) that is ambiguous.
A context-free language is ambiguous if for
all CFGs G, with L(G) = L, G is ambiguous.
142
Simplications and Normal Forms
Redundant Symbols
Denition A symbol X V (V = N ) is said
to be a terminating symbol if
(1) X is a terminal; (i.e, X )
(2) X P and consists solely of termi-
nating symbols.
Example Let G be described by
S ASB [ BSA [ SS [ aS [
A AB [ B
B BA [ A
(a) Find the set of terminating symbols of G
(1) a, (2) a, S
(b) Eliminate non-terminating symbols
S SS [ aS [
143
Denition Let G = (N, , P, S) be a CFG. A
symbol X V is said to be reachable if S

X, for , V

.
Example Let G be
S aS [ SB [ SS [
A ASA [ C
B b
(a) Mark all the reachable symbols
S
S, a, B
S, a, B, b
(b) Eliminate unreachable symbols
S aS [ SB [ SS [
B b
144
Summary of Redundant Symbols
Redundant symbols:
(1) nonterminating symbols
nonterminal symbols that do not
derive any terminal word.
(2) unreachable symbols
symbols not appear in any
sentential form
Denition A CFG G is said to be reduced
if G does not contain redundant symbols.
Theorem Given a CFG G = (N, , P, S), an
equivalent reduced CFG G
/
= (N
/
,
/
, P
/
, S)
can be constructed such that N
/
N,
/

and P
/
P.
145
Empty Productions
Denition An empty-production is
a production of the form
A .
Empty productions are also called
-productions, null-productions.
A nonterminal B is called a
-nonterminal if B
+
.
Use a marking alghorithm to nd all the
-nonterminals.
Example Let G = (N, , P, S) be
S aSaS [ SS [ bA
A BC
B
C BB [ bb [ aC [ aCbA
Find all the -nonterminals in G.
(1) B, (2) B, C, (3) B, C, A
146
Algorithm to remove -productions
i) Use the marking alghorithm to
nd all the -nonterminals.
ii) For every -nonterminal A in N do
for every B in P with [[
A
,= 0 do
Let =
0
A
1
. . .
t1
A
t
, where

0
,
1
, . . . ,
t
do not contain A.
Replace B in P with all the
productions
B
0
X
1

1
. . .
t1
X
t

t
[ X
i
, A
iii) Reduce the new grammar (i.e remove
all the redundant symbols).
Theorem Let G be a reduced CFG G = (N, , P, S).
Then there exists a -equivalent CFG G
/
=
(N
/
, , P
/
, S) that is also reduced and -free.
-equivalent: L(G) = L(G
/
)
-free: no -productions.
147
Chomsky Normal Form
Denition A CFG G is said to be in Chomsky
normal form if it only has productions of the
forms:
i) A a, a ;
ii) A BC, B, C N.
An arbitrary CFG G may have productions
of the following forms:
(Assume: G doesnt have -productions,
G is reduced)
i) A a;
ii) A BC;
iii) A B; (unit-productions)
iv) A , [[ > 2 and V
+
;
v) A , [[ = 2 and , N
2
;
We are going to show that iii), iv) and v) can
be changed to i) and ii).
148
Unit-production removal
While there is a unit-production A C in P do
if A = C then remove A C from P
else replace A C with all productions
A : C in P.
Does this alghorithm always terminate?
Why?
Reduced, -free CFG
==transfer= reduced, -free, unit-free CFG
Long production removal
Let Maxrhs(G) = max[[ : A in P.
Claim Let k = Maxrhs(G) and k 3. Then
we can construct an equivalent G
/
such that
Maxrhs(G
/
) < k.
149
Proof (outline)
If A is in P and [[ = k 3,
then replace it with
i) A
1
[A]
ii) [A]
2
where [A] is a new nonterminal, =
1

2
and [
1
[ = [[/2|, [
2
[ = [[/2|.
Note that if i) is used in a derivation
then ii) must be used.
So, G
/
is equivalent to G.
Note: If G is -free, unit-free then
G
/
is a -free and unit-free.
By iterating the above approach, we
can get a CFG G s.t. Maxrhs(G) = 2.
150
Changing to CNF
Now, we have all the productions
in the forms i), ii) and v) where:
v) :

A aB
A Ba
A ab
A aB A aB, a a
A Ba A Ba, a a
A ab A ab, a a, b b;
where a, b are new nonterminals.
The CFGs in CNF can generate all
the CFLs.
151
Summary
Given an arbitrary CFG G = (N, , P, S),
we can construct a -equivalent CFG G
/
s.t. G
/
is in CNF by the following steps:
1) reduction;
i) remove nonterminating symbols,
ii) remove unreachable symbols;
2) remove -productions; (may reduce again)
3) remove unit-productions;
4) remove long productions; ( 3)
5) change to CNF
152
2. Pushdown Automata
Denition A PDA A is a 7-tuple
(Q, , , , q
0
, Z
0
, F) where
Q : a nite set of states;
: input alphabet;
: stack alphabet;
: Q( ) Q

transition
relation;
q
0
Q : the initial state;
Z
0
: the bottom-of-stack symbol;
F Q : set of nal states;
`
_
`
_

_
- -
q
0
q
1
`
6

6
a, Z
0
/AZ
0
a, A/AA
b, A/
, Z
0
/
b, A/
By Final State : a
i
b
j
[ i j 1
153
Instantaneous descriptions (IDs)
( q

current state
,
remaining part of the input

x ,

current content of the stack
)
An ID describes a conguration of a PDA.
Example
ID for the initial conguration

(q
0
, aab, Z
0
) (q
0
, ab, AZ
0
) (q
0
, b, AAZ
0
)
(q
1
, , AZ
0
)

ID for an accepting conguration
Acceptance methods of PDA:
(1) by nal state
T(A) = w [ (q
0
, w, Z
0
)

(q
f
, , ), q
f
F
(2) by empty stack
N(A) = w [ (q
0
, w, Z
0
)

(q, , )
(3) by both nal state and empty stack
L(A) = w [ (q
0
, w, Z
0
, )

(q
f
, , ), q
f
F
154
Example
`
_
`
_

_
- -
q
0
q
1
`
6

6
a, Z
0
/AZ
0
a, A/AA
b, A/
, Z
0
/
b, A/
`
_
`
_
R
}

7
_
?
, Z
0
/
q
2
q
3
b, A/
, A/
, A/A
N(B) = a
i
b
i
[ i > 0 a
2i
b
i
[ i > 0
L(B) = a
i
b
i
[ i > 0
T(B) = a
i
b
j
[ i j > 0
155
Deterministic Context-free Languages
Denition A PDA M = (Q, , , , q
0
, Z
0
, F) is
deterministic if
1) for each q in Q, a in , and X ,
(q, a, X) contains at most one element,
2) whenever (q, a, X) is nonempty for some
a , then (q, , X) is empty.
Note that
DPDA allow transitions.
Each transition is determined by the
current state, the input symbol, and the
top-of-stack symbol.
So, for each pair of a state and an input
symbol, there can be several transitions,
one for each stack symbol.
(q, , X) should not be dened if (q, a, X)
is dened for any a .
156
Let T
DPDA
, ^
DPDA
, and /
DPDA
denote the sets
of languages accepted by DPDA with accep-
tance by nal state, empty stack, and
nal state and empty stack, respectively.
Then ^
DPDA
= /
DPDA
T
DPDA
Example
L = a
m
b
n
[m n, and m, n > 0
Then L = T(A) where A:
`
_

_
- -
q
0
q
1
`
6

6
a, Z
0
/AZ
0
a, A/AA
b, A/
b, A/
`
_
q
2
-
`
6
, Z
0
/Z
0
b, Z
0
/Z
0
`
_
But L , ^
DPDA
Denition The family of deterministic
context-free languages is the set of all lan-
guages accepted by DPDA with acceptance
by nal state.
157
The family of DCFLs is a proper
subset of the family of CFLs.
Examples
The following CFLs are not DCFLs
1. a
n
b
n
[ n 0 a
n
b
2n
[ n 0
2. ww
R
[ w Z

3. ww [ w Z

The family of DCFLs is closed under


(1) complementation,
(2) intersection with regular sets,
not closed under
(1) union
(2) intersection.
158
3. CFL Pumping Lemma & Closure Properties
Pumping Lemma
Let L = L(G) and G = (N, , P, S) be a -free,
unit-free CFG, such that
m = max([[ [ A P) and p = 1 + m
#N+1
.
Then, for all words z in L(G) such that
[z[ p, z has a derivation sequence
S

uAv
+
uxAyv
+
uxwyv = z
for some A in N and some u, v, w, x, y in

such that
i) [xwy[ < p;
ii) [xy[ 1;
iii) ux
i
wy
i
v is in L, for all integers i 0.
159
Example (Use of CFG P.L.)
Prove that L = a
i
b
i
c
i
[ i 1 is not a CFL.
Proof
Assume L is a CFL.
Then there exists a -free unit-free
CFG G s.t. L = L(G). Let p be
the constant for G dened in P.L
By P.L, all words z with [z[ p can
be decomposed into z = uxwyv s.t
i) [xwy[ < p
ii) [xy[ 1
iii) ux
i
wy
i
v L, for all i 0.
Therefore, to obtain a contradiction, it is suf-
cient to give one word that for all decompositions,
conditions i), ii), and iii) cannot be satised
at the same time.
Consider z = a
p
b
p
c
p
. Obviously, [z[ > p.
Since [xwy[ < p, xwy is in
a
+
b
+
c
+
a
+
b
+
b
+
c
+
. (The only possibilities)
160
Case 1 xwy is in a
+
Then xy = a
k
, for all 1 k < p.
Consider ux
0
wy
0
v = uwv = a
pk
b
p
c
p
.
Since k 1, there are less as than
bs and cs. uwv , L
xwy in b
+
or in c
+
are similar.
Case 2 xwy is in a
+
b
+
(1) x is in a
+
b
+
(or y is in a
+
b
+
)
Then ux
2
wy
2
v is not in L since
it has as following bs.
(2) x in a

, y in b

. Since [xy[ 1,
x, y cannot all be . Then
ux
0
wy
0
v , L since there are less
as or bs than cs.
The case of xwy in b
+
c
+
is similar.
Since all the decompositions fail to satisfy all
the conditions i), ii) and iii), z = a
p
b
p
c
p
con-
tradicts P.L. Therefore, L is not in CFL.
161
Proof of CFG P.L
It is easy to prove that an m-ary tree with
> m
h
external nodes has height > h.
Therefore,
i) if a syntax tree for G has a yield > m
h
(m = maxrhs(G)), then its height is > h.
ii) if it has height h, then its yield has a
length m
h
.
Consider a word z with [z[ p > m
#N+1
.
Then any syntax tree T for z satises
ht(T) > #N +1. Consider a longest path from
the root to frontier.
Its length is #N + 2. (It contains #N + 3
symbols.)
162
Consider the lowest #N + 1 nonterminal
symbols. By Pigeonhole principle, there must
be some nonterminal A that appears at least
twice among these #N + 1 nonterminals on
this path. This provides a decomposition of
z.
S

uAv
+
uxAyv
+
uxwyv
163
S

uAv
+
uxAyv
+
ux
i
Ay
i
v
+
ux
i
wy
i
v
Now, consider the three conditions.
i) Since the upper A is at a distance
of at most #N + 1 from the frontier,
[xwy[ m
#N+1
p.
ii) Since G is -free, unit-free,
[xy[ 1.
iii) As discussed on the last page.
164
Closure Properties
We show that /
CF
is closed under , , , but
not under and

.
1. Union
L
1
, L
2
/
CF
(i.e L
1
, L
2
are CFLs).
Show that L = L
1
L
2
is CF.
Proof:
G
1
= (N
1
,
1
, P
1
, S
1
), G
2
= (N
2
,
2
, P
2
, S
2
)
Assume N
1
N
2
= . Construct
G =
(N
1
N
2
S),
1

2
, P
1
P
2
S S
1
[S
2
, S).
Then L(G) = L(G
1
) L(G
2
)
165
2. Catenation
L
1
, L
2
/
CF
L
1
L
2
/
CF
G
1
= (N
1
,
1
, P
1
, S
1
) , G
2
= (N
2
,
2
, P
2
, S
2
)
G = (N
1
N
2
S,
1

2
,
P
1
P
2
S S
1
S
2
, S))
L(G) = L(G
1
) L(G
2
)
3. *
L
1
/
CF
L

1
/
CF
(L(G
1
))

S S
1
S[
4. Intersection
L
1
, L
2
/
CF
, L
1
L
2
/
CF
L = a
i
b
i
c
i
[ i 0 is not in CF
L
1
= a
i
b
j
c
k
[ i = j, i, j, k 0
L
2
= a
i
b
j
c
k
[ j = k, i, j, k 0
L
1
L
2
= L
166
5. Complementation
L
1
/
CF
, L
1
/
CF
Proof:
Assume /
CF
is closed under

.
Consider two arbitrary CFLs L
1
, L
2
.
L = L
1
L
2
= L
1
L
2
.
L is CF
/
CF
is closed under .
This is a contradition.
167
V. TURING MACHINES
Turing machines have more features than FA
and PDA.
(1) The read-write head can move
in either direction.
(2) It can write on the tape.
Turing machines are studied as a theoretical
model of computers.
168
Some assumptions for TMs:
(1) At beginning, the input string (symbols) is
placed at the left end of the input tape and
followed by innitely many blank symbols
denoted by Bs.
(2) There is only one nal state denoted by
f.
(3) A TM stops when it enters the nal state
f.
169
Denition A deterministic Turing Machine
(DTM) is specied by a sextuple
(Q, , , , s, f) where
Q: is a nite set of states;
: is an alphabet of input symbols;
: is an alphabet of tape symbols,
B
: Q Q L, R, is a
transition function;
s Q is a start state;
f Q is a nal state;
State diagram
(p, a) = (q, b, D) is depicted graphically:

_
p q
-
a/b, D
170
Example A DTM that accepts
L = a
i
b
i
[ i 0
171
Example A DTM that accepts
L = a
i
b
i
[ i 0
172
Conguration
A conguration is a word in

.
Strictly speaking, a conguration is a word
in

( B)

Q
(Note: Q = )
One move of a DTM
g
1
ph
1
g
2
qh
2
if
(i) either h
1
= Ah
/
1
, for some A in , h
/
1
in

or
h
1
= , then A = B and h
/
1
= ;
(ii) (p, A) is dened and p ,= f;
(iii) (p, A) = (q, A
/
, D)
(a) D = L, g
1
= g
/
1
C for some C , and then
h
2
= CA
/
h
/
1
(if g
1
= , then M halts)
(b) D = R, g
1
A
/
= g
2
and h
2
= h
/
1
(c) D = , g
2
= g
1
and h
2
= A
/
h
/
1
(if A
/
=
B, h
/
1
= , then h
2
= )
173

i
,
+
,

are dened as before.


Language acceptance
L(M) = x [ sx

yfz, for some y, z

/
DTM
= L [ L = L(M) for some DTM M.
A DTM can be used
(i) as a language acceptor;
(ii) to compute a function:
f
M
:

( B)

f
M
(x) = y in ( B)

i
sx

y
1
fy
2
, where y = y
1
y
2
(iii) as a decision maker.
174
Example A right shift machine
Initial state:
B: write B, move , goto f;
a: write B, move right, goto A;
b: write B, move right, goto B;
A-state:
a: write a, move right, goto A;
b: write a, move right, goto B;
B: write a, move right, goto f;
B-state:
a: write b, move right, goto A;
b: write b, move right, goto B;
B: write b, move right, goto f;
[a[a[b[b[b[a[a[a[B[B[
[B[a[a[b[b[b[a[a[a[B[
175
Right-shift machine:
`
_
`
_
`
_
`
_
- -
? ?
-
-

`
6
_
?
/
`
_
S A
B
f
a/B, R
B/b,
b/B, R
B/a,
B/B,
a/a, R
b/b, R
a/b, R
b/a, R
Example A cyclic right shift machine:
[b[a[a[b[b[b[a[B[B[ . . . . . .
is transformed in:
[a[b[a[a[b[b[b[B[B[ . . . . . .
`
_
`
_
`
_
`
_
- -
? ?
-
-

`
6
_
?
/
S A
B
f
a/B, R
B/b,
b/B, R
B/a,
B/B,
a/a, R
b/b, R
a/b, R
b/a, R
`
_
`
_
`
_
`
_
- -
6

_
-
6
f

`
6
_
?
7
a/a, L
b/b, L
a/a, L
b/b, L
b/B, L
a/B, L
B/b,
B/a,
B/B,
176
Example A reversal machine
input: aabba
177
The Busy Beaver Problem
Consider a DTM with
two-way innite tape;
a tape alphabet = 1, B;
n states apart from f.
Question (by Tibor Rado)
How many 1s can there be on entering f,
when given the empty word as input?
(1s are like twigs. Beavers build busily with
twigs.)
Dene (n) to be the maximum number of
1s that can be obtained by a DTM with n
states.
1-state DTM:

_
`
_
- -
B/1, R
S
f
178
(1) = 1
(2) = 4
(3) = 6
(4) = 13
(5) =?, 1915
2-state Machine:
`
_
`
_

_
`
_
- - -
=
>
1 2
f
B/1, R
B/1, L
1/1, L
1/1, R
. . . . . . [B[B[ B

1
[B[ . . . . . .
. . . . . . [B[B[1[ B

2
[ . . . . . .
. . . . . . [B[B[ 1

1
[1[ . . . . . .
. . . . . . [B[ B

2
[1[1[ . . . . . .
. . . . . . [ B

1
[1[1[1[ . . . . . .
. . . . . . [1[ 1

2
[1[1[ . . . . . .
. . . . . . [1[1[ 1

f
[1[ . . . . . .
179
Denitions
Decision-making TM
A DTM M = (Q, , , , s, f) is said to be a
decision making TM if y and n are in and
not in , and for all x

, either sx

fy or
sx

fn.
Yes language of M
Y (M) = x [ x

and sx

fy
No language of M
N(M) = x [ x

and sx

fn
Decidability
Let L

, (B , ). L is decidable i there
is a decision-making TM M with L = Y (M).
180
Recursive Languages
L is recursive i L is decidable.
Notation
/
REC
denotes the family of recursive languages.
Computability
Let f :

be a function, where B , .
f is said to be computable i there is a DTM
M = (Q, , , , s, f) with and for all x

if f(x) = y then
sx

y
1
fy
2
and y = y
1
y
2
for some y
1
, y
2

.
181

You might also like