0% found this document useful (0 votes)
10 views30 pages

ATC Module 4

The document discusses context-free languages (CFLs) and their properties, including the distinction between context-free and non-context-free languages, the pumping theorem, and closure properties. It explains how CFLs are generated, their decidability, and the algorithms related to Turing machines. Additionally, it covers the limitations of CFLs in terms of intersection, complement, and difference, while also addressing deterministic context-free languages.

Uploaded by

MUKTA PUJAR
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views30 pages

ATC Module 4

The document discusses context-free languages (CFLs) and their properties, including the distinction between context-free and non-context-free languages, the pumping theorem, and closure properties. It explains how CFLs are generated, their decidability, and the algorithms related to Turing machines. Additionally, it covers the limitations of CFLs in terms of intersection, complement, and difference, while also addressing deterministic context-free languages.

Uploaded by

MUKTA PUJAR
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 30

Context-Free and Non-Context-Free Languages

Where Do the Context-Free Languages Fit in the Big Picture?


Showing that a Language is Context-Free
Pumping theorem for CFL
Important closure properties of CFLs
Deterministic CFLs
Algorithms and Decision Procedures for CFLs: Decidable questions
Undecidable questions
Turing Machine: Turing machine model
Representation
Language acceptability by TM
Design of TM
Techniques for TM construction.

Context-Free and Non-


Context-Free Languages
● The language AnBn = {anbn | n≥ 0} is
context-free.
● The language AnBnCn ={ anbncn |n≥0} is not context free because a PDA's stack cannot count
all three of the letter regions and compare them.

Where Do the Context-Free Languages Fit in the Big Picture?


THEOREM: The Context-Free Languages Properly Contain the Regular
Languages.

Theorem: The regular languages are a proper subset of the context-free languages.
Proof: We first show that every regular language is context-free. We then show that
there exists at least one context-free language that is not regular.

Every regular language is context-free : We show this by construction.

 If L is regular then it is accepted by some DFSM M = (K, Σ, δ, s, A).


 From M we construct a PDA
M' = (K',Σ’, Γ’,Δ’,s’,A’) to accept L. where Δ' is constructed as follows:
For every transition (qi,c,qj)in δ, add to Δ' the transition
((qi,c,ε),(qi,ε)),So
L(M)=L(M').

So, the set of regular languages is a subset of the CFL.


There exists at least one context-free language that is not regular : The regular
languages are a proper subset the context-free languages because there exists at least one
language anbn that is context –free but not regular.

Theorem: There is a countably infinite number of context-


free languages.
Proof:

Every context-free language is generated by some context-free grammar G = (V,Σ,R,S).

There cannot be more CFLs than CFGs. So there are at most a countably infinite number of
context-free languages. There is not a one-to-one relationship between CFLs and CFGs, since
there are an infinite number of grammars that generate any given language. But we know that,
every regular language is context free and there is a countably infinite number of regular
languages.

So there is at least and at most a countably infinite number of CFLs.

Showing That a Language is Context-Free


Two techniques that can be used to show that language L is context-free:
• Exhibit a context-free grammar for it.
• Exhibit a (possibly nondeterministic) PDA for it.

Theorem: The length of the yield of any tree T with height h


and
branching factor b is <= bh.
Proof:
If h is 1, then a single rule applies. So the longest yield is of length less than or equal to b.
Assume the claim is true for h=n. We show that it is true for h=n+1.
Consider any tree with h=n+1. It consists of a root, and some number of subtrees, each of height
<=n. By the induction hypothesis, the length of the yield of each of those subtrees is <= bn. So the
length of the yield must be <=b.(bn)=bn+1=bh.

The Pumping Theorem for Context-Free languages


Statement: If L is CFL, then: ∃k≥1 (∀strings w∈L, where |w|≥k (∃u,v,x,y,z
(w=uvxyz, vy≠Ɛ, |vxy| ≤ k and ∀q ≥ 0 (uvqxyqz is in L))))
Proof: If L is context-free, then there exists a CFG G=(V,Σ,R,S) with n nonterminal symbols and
branching factor b.
Let k be bn+1.
Any string that can be generated by G and whose parse tree contains no paths with repeated
nonterminals must have length less than or equal to bn. Assuming that b≥2, it must be the case that
bn+1 > bn. So let w be any string in L( G) where |w|≥k.
Let T be any smallest parse tree for w. T must have height at least n+1. Choose some path in T of
length at least n + 1. Let X be the bottom-most repeated non terminal along that path. Then w can be
rewritten as uvxyz as shown in below tree,

The tree rooted at [1] has height at most n+1.Thus its yield, vxy, has length less than or equal
to bn+1,which is k. Further, vy≠Ɛ .Since if vy were Ɛ then there would be a sma ler parse tree for w
and we choose T so that h at wasn't so.
Finally, v and y can be pumped: uxz must be in L because rule2 could have been used
immediately at[1]. And, for any q≥1, uvqxyqz must be in L because rule1 could have been used q
times before
finally using rule2.

Application of pumping lemma (Proving Language is Not Context Free)


Ex1: Prove that the Language L = {anbncn| n>=0} is Not Context-Free.
Solution: If L is CFL then there would exist some k such that any string w, where |w|>=k
must satisfy the conditions of the theorem.
Let w = akbkck, where ‘k’ is the constant from the Pumping lemma theorem. For w to satisfy
the conditions of the Pumping Theorem there must be some u,v,x,y and z, such that w=uvxyz, vy≠Ɛ,
|vxy|≤k and ∀q ≥ 0 , uvqxyqz is in L.
w=aaa…aaabbb…bbbccc…ccc, select v and y as follows:
w=aaa…aaabbb…bbbccc…ccc
v y
Let q=2, then
V2

y2
w=aaa…aaabbaabb b..bbccccc…
ccc
The resulting string will have letters out of order and thus not in L.
So L is not context-free.

Ex 2: Prove that the Language L= {WcW: w∈{a,b}*}is Not Context-Free.


For w to satisfy the conditions of the Pumping Theorem there must be some u,v,x,y,and z, such that
w = uvxyz , vy≠Ɛ, |vxy| ≤ k and ∀q≥0, uvqxyqz is in L. We show that no such u,v,x,y and z exist.
Imagine w divided into five regions as follows:

Call the part before the c the leftside and the part after the c the right side. We consider all the
cases for where v and y could fall and show that in none of them are all the condition so f the theorem met:
•If either v or y overlaps region 3, set q to 0. The resulting string will no longer contain a c and so
is not in WcW.
•If both v and y occur before region 3 or they both occur after region 3, then set q to 2. One side
will be longer than the other and so the resulting string is not in WcW.
•If either v or y overlaps region1 ,then set q to 2. In order to make the right side match.
Something would have to be pumped into region 4. But any v,y pair that did that would violate the
requirement
that lvxyl ≤ k.
•If either v or y overlaps region2, then set q to 2. In order to make the right side match,
something would have to be pumped into region 5. But any v,y pair that did that would violate the
requirement that lvxyl ≤ k.
•There is no way to divide w into uvxyz such that all the conditions of the Pumping
Theorem are met . So WcW is not context-free.

Some Important Closure Properties of Context-Free Languages

Theorem: The context- free languages are closed under Union,


Concatenation, Kleene star, Reverse, and Letter substitution.
(1) The context-free languages are closed under union:

•If L1 and L2 are context free languages then there exists a context-free grammar G1= (V1,Σ1, R1,S1)
and G2=(V2,Σ2,R2,S2) such that L1=L(G1) and L2=L(G2).
•We will build a new grammar G such that L(G)=L(G1)UL(G2). G will contain all the rules of both
G1 and G2.
•We add to G a new start symbol S and two new rules. S→S1 and S→S2. The two new rules allow G
to generate a string iff at least one of G1or G2 generates it.
So, G = ( V1 U V2 U {S}, Σ1 U Σ2, R1 U R2 U {S→ S1,S→S2}, S )

(2)The context-free languages are closed under concatenation


• If L1 and L2 are context free languages then there exist context-free grammar G1= (V1,Σ1,R1,S1) and
G2=(V2,Σ2,R2,S2) such that L1= L(G1) and L2= L(G2).
• We will build a new grammar G such that L (G) = L(G1)L(G2).
• G will contain all the rules of both G1and G2.
• We add to G a new start symbol S and one new rule. S→S1S2
So G= (V1UV2U{S}, Σ1 U Σ2, R1UR2U{S→S1S2),S)

(3) The context-free Languages are closed


under Kleene star:
• If L1 is a context free language then there exists a context-free grammar G1=(V1,Σ1,R1,S1) such that
L1= L(G1).
• We will build a new grammar G such that L(G)=L(G1)* G will contain all the rules of G1.
• We add to G a new start symbol S and two new rules. S→ℇ and S→SS1
So G = (V1 U {S}, Σ1, R1U {S→ℇ, S→SS1), S )

(4) The context-free languages are closed under


reverse
•If L is a context free language then it is generated by some Chomsky Normal Form from
grammar G= (V,Σ,R, S).
• Every rule in G is of the form X→BC or X→a, where X, B, and C are elements of (V-Σ) and a ϵΣ
• So construct, from G, a new grammar G1 , Such that L(G1)= LR .
• G1= (VG, ΣG, R', SG) , Where R' is constructed as follows:
 For every rule in G of the form X→BC, add to R' the rule X→CB
 For every rule in G of the form X→ a then add to R' the rule X→ a

(5)The context-free languages are closed under letter Substitution


• Consider two alphabets Σ1 and Σ2 .
• Let sub be any function from Σ1 to Σ2*.
• Then letsub is a letter substitution function from L1 to L2 iff letsub(L1) ={ w∈ Σ2*: ∃y ∈ L1

(w=y except that every character c of y has replaced by sub(c))}.


Example : Let y= VTU ∈ L1 And sub(c) is given as : sub(V) = Visvesvaraya
sub(T) = Technological
sub(U)= University
Then , sub(VTU) = Visvesvaraya Technological
University

Closure Under Intersection,


Complement, and Difference
Theorem:The Context-free language are not closed under
intersection, complement or difference.
1) The context-free languages are not closed under intersection
The proof is by counter example. Let: L1={anbncm|n,m≥0} L2={ambncn|n,m≥0} Both L1and L2
are context-free since there exist straight forward CFGs for them.
But now consider: L =L1∩L2= { anbncn | n,m≥0}. If the context-free languages were closure under
intersection. L would have to be context-free. But we have proved that L is not CFG by using
pumping lemma for CFLs.

(2) The context-free languages are not closure under


Given any sets L1and L2 , L1∩ L2 = ¬ ( ¬ L1∪ ¬ L2)
• The context-free languages are closed under union.
• But we just showed that they are not, thus they are not closed under complement either.
•So, if they were also closed under complement, they would necessarily be closed
under intersection.

(3) The context-free languages are not closed under


difference (subtraction) :
Given any language L and ¬ L=Σ*- L.
Σ* is context-free So, if the context-free languages were closed under difference, the complement of
any CFL would necessarily be context-free But we just showed that is not so.

Closure Under Intersection With the Regular Languages


Theorem: The context-free languages are closed under intersection with the
regular languages.
Proof: The proof is by construction.
• If L1 is context-free, then there exists some PDA M1= (K1,Σ,𝛤1,𝚫1,S1,A1) that accepts it.

• If L2 is regular then there exists a DFSM M2= (K2,Σ,𝛿,S2,A2) that accepts it.

•We construct a new PDA, M3 that accepts L1 ∩ L2. M3 will work by simulating the
parallel execution of M1 and M2.

• M3= (K1X K2, Σ , 𝛤1, 𝚫3,(S1,S2),A1X A2), Where 𝚫3 is built as follows:

• For each transition ((q1, a, 𝛃) ,(p1, 𝛄)) in 𝚫1 and each transition ((q2, a ) ,p2) in
𝛿, add 𝚫3 the transition: (((q1,q2),a,𝛃) ,((p1,p2), 𝛄)).
• For each transition ((q1,ℇ,𝛃) ,(p1, 𝛄)) in 𝚫1and each state q2 in k2, add to 𝚫3 the

transition: (((q1,q2),ℇ,𝛃) ,((p1,p2), 𝛄)).

Closure Under Difference with the Regular Language.


Theorem: The difference (L1-L2) between a context-free language L1 and a
regular language L2is context-free.
Proof: L1-L2= L1∩ ¬ L2
•If L2 is regular, then, since the regular languages are closed under complement, ¬ L2 is
also regular.
• Since L1 is context-free, by Theorem we already proved that L1∩ ¬ L2 is context-free.

Using the Pumping Theorem in Conjunction with the Closure


Properties
Languages that impose no specific order constraints on the symbols contained in their strings
are not always context-free. But it may be hard to prove that one isn't just by using the Pumping
Theorem. In such a case it is proved by considering the fact that the context-free languages are
closed under intersection with the regular languages.

Deterministic Context-Free Languages


The technique used to show that the regular languages are closed under complement starts
with a given (possibly nondeterministic ) FSM M1, we used the following procedure to construct
a new FSM M2 such that L(M2)= ¬ L(M1):
The regular languages are closed under complement, intersection and difference. Why are the
context-free languages different? Because the machines that accept them may necessarily be
nondeterministic.
1. From M1, construct an equivalent DFSM M', using the algorithm ndfsmtodfsm, presented in
the proof of Theorem5.3. (If M1is already deterministic. M'=M1.)
2.M' must be stated completely. so if it is described with an implied dead state, add the dead
state and all required transitions to it.
3.Begin building M2 by setting it equal to M'. Then swap the accepting and the non-accepting
states. So M2 M'= (KM',.Σ,𝜹M',sM',KM'-AM').
We have no PDA equivalent of ndfstodfsm because there provably isn't one. We defined a PDA M
to be deterministic iff:
•ΔM contains opairs of transitions that compete with each other, and
• if q is an accepting state of M, then there is no transition ((q,ε,ε),(p,a)) for any p or a.

What is a Deterministic Context-Free language?


 Let $ be an end-of-string marker. A language L is deterministic context-free iff L$ can be
accepted by some deterministic PDA.

EXAMPLE: Why an End-of-String Marker is Useful


Let L= a* ∪ { anbn| n>0}
Consider any PDA M that accepts L. When it begins reading a’s, M must push them onto the
stack in case there are going to be b's. But if it runs out of input without seeing b's, it needs a way to
pop those a's from the stack before it can accept. Without an end-of-string marker, there is no way to
allow that popping to happen only when all the input has been read.

For example, the PDA accepts L, but it is nondeterministic because the transition to state3 (where the
a's will be popped) can compete with both of the other transitions from state1.
With an end-of-string marker, we can build the deterministic PDA, which can only take the transition
to state3, the a-popping state. When it sees the $:

NOTE: Adding the end-of-string marker cannot convert a language that was not
context-free into one that is.

CFLs and Deterministic CFLs


Theorem: Every deterministic context-free language is context-free.
Proof:
If L is deterministic context-free, then L$ is accepted by some deterministic PDA
M=(K,Σ,𝜞,𝜟,s,A) . From M, we construct M' such that L (M') = L. We can define the following
procedure to construct M':
without$(M:PDA)=
1. Initially. set M' to M.
/*Make the copy that does not read any input.
2. For every state q in M, add to M' a new state q'.
3. For every transition ((q, ε ,𝜸1),(p,𝜸2)) in 𝜟 M do:
1. Add to 𝜟M the transition ((q',ε,𝜸1),(p',𝜸2)).
/*Link up the two copies.
4. For every transition ((q,$,𝜸1),(p,𝜸2)) in 𝜟 M do:
1. Add to 𝜟M’ the transition ((q,ε,𝜸1),(p',𝜸2)).
2. Remove ((q,$,𝜸1),(p,𝜸2)) from 𝜟M’
/*Set the accepting state s of M'.
5.AM' = {q':q∈A}.
Closure Properties of the Deterministic Context-Free
Languages
1) Closure Under Complement
Theorem: The deterministic context-free languages are closed
under complement.
Proof: The proof is by construction. If L is a deterministic context-free language over the alphabet Σ,
then L$ is accepted by some deterministic PDA M = (K, Σ U{$}, 𝜞, 𝜟, s, A).
We need to describe an algorithm that constructs a new deterministic PDA that accepts ( ¬ L)$.
We defined a construction that proceeded in two steps:
• Given an arbitrary FSM, convert it to an equivalent DFSM, and then
• Swap accepting and non accepting states.
A deterministic PDA may fail to accept an input string w for any one of several reasons:
1. Its computation ends before it finishes reading w.
2. Its computation ends in an accepting state but the stack is not empty.
3. Its computation loops forever, following ε-transitions, without ever halting in
either an accepting or a non accepting state.
4. Its computation ends in a non accepting state.
If we simply swap accepting and non accepting states we will correctly fail to accept every string
that M would have accepted (i.e., every string in L$). But we will not necessarily accept every string
in ( ¬ L)$. To do that, we must also address issues 1through 3 above.
An additional problem is that we don't want to accept ¬ L(M). That includes strings that do not end
in $. We must accept only strings that do end in $ and that are in ( ¬ L)$.

2) Non closure Under Union


Theorem: The deterministic context-free languages are not closed under union.
Proof: We show a counter example:
Let, L1={ aibjck| i,j,k ≥0 and i≠ j } and L2={ aibjck | i,j,k≥0 and j ≠ k}
Let, L' = L1U L2= { aibjck | i,j,k≥0 and ( ( i≠ j) and ( j ≠ k) ) }.
Let, L" = ¬ L'.
={ aibjck |i,j,k≥0 and (i=j=k)} U {w∈{a,b,c}*: the letters are out of order}.
Let, L"'=L"∩a*b*c* = {anbncn | n ≥ 0}
But L'" is AnBnCn={ anbncn |n≥0},which we have shown is not context-free.

3) Non Closure Under Intersection


Theorem: The deterministic context-free languages are not closed under inter
section.
Proof: We show a counter example:
Let, L1= { aibjck | i,j,k≥0 and i= j }and L2={ aibjck |i,j,k≥0 and j=k}
Let, L' = L1∩ L2 ={ anbncn | n ≥ 0}
L1 and L2 are deterministic context-free.
The deterministic PDA shown accepts L1$, A similar
one accepts L2. But we have shown that their
intersection L' is not context-free much less deterministic
context-free.

A hierarchy within the class of


context-free languages
Some CFLs are not Deterministic
Theorem: The class of deterministic context-free languages is a proper subset of
the class of context-free languages. Thus there exist nondeterministic PDAs for
which no equivalent deterministic PDA exists.
Proof: We show that there exists at least one context-free language that is not deterministic
context-free.
Consider L = { aibjck |i,j,k ≥ 0 and ( ( i≠ j) or ( j ≠ k) ) }. L is context-
free. If L were deterministic context-free, then, its complement
L'={ aibjck |i,j,k ≥ 0 and (i=j=k) } U {w∈{a,b,c}*:the letters are out
of order}
Would also be deterministic context-free and thus context-free. If L'
were context-free,

then
L"=L'∩ a*b*c* would also be context-free (since the context-free languages are closed under inter
section with the regular languages).
But L"= AnBnCn ={anbncn|n≥0},which is not context free.
So L is context-free but not deterministic context-free.
Since L is context-free, it is accepted by some (non deterministic) PDA M. M is an example
of an on deterministic PDA for which no equivalent deterministic PDA L exists. If such a
deterministic PDA did exist and accept L, it could be converted into a deterministic PDA that
accepted L$. But, if that machine existed. L would be deterministic context-free and we just showed
that it is not.

Inherent Ambiguity versus Non determinism


There are context-free languages for which unambiguous grammars exist and there are others
that are inherently ambiguous, by which we mean that every corresponding grammar is ambiguous.
Example:
The language L1= {aibjck | i, j, k ≥ 0 and ( (i= j) or ( j = k) )} can also be
described as
{anbncm|n,m ≥0}U{ anbmcm |n,m ≥0}. L1 is inherently ambiguous because every string that is
also in AnBnCn ={anbncn|n≥0} is an element of both sub languages and so has at least two derivations
in any grammar for L1.
• Now consider the language L2={anbncmd|n,m≥0}U{anbmcme| n,m≥0} is not inherently ambiguous.
•Any string in is an element of only one of them (since each such string must end in d or e but
not both).
There exists no PDA that can decide which of the two sublanguages a particular string is in until it
has consumed the entire string.

What is the relationship between the deterministic context-free languages and


the languages that are not inherently ambiguous?
The answer is shown in below Figure.

There exist deterministic context-free languages that are not


regular. One example is AnBn={anbn|n,m≥0}.
• There exist context-
free languages and
not inherently
ambiguous. Examples:

(a) PalEven={wwR:w∈{a,b}*}.

(b) {anbncmd|n,m≥0}U{anbmcme|n,m≥0}.
• There exist languages that are in the outer donut because they are inherently ambiguous.

Two examples are:


 {aibjck|i,j,k ≥ 0 and ((i≠j)or(j≠k))}
 {aibjck|i,j,k ≥ 0 and ((i=j) or (j=k))}
Regular Language is Deterministic Context-Free
Theorem: Every regular language is deterministic context-free.
Proof: The proof is by construction. {$} is regular. So, if L is regular then so is L$ (since the
regular languages are closed under concatenation).So there is a DFSM M that accepts it. Using the
construction to show that every regular language is context-free Construct, from M a PDA P that
accepts L$. P will be deterministic.

Every Deterministic CFL has an Unambiguous Grammar


Theorem: For every deterministic context-free language there exists an
unambiguous grammar.
Proof: If a language L is deterministic context-free, then there exists a deterministic PDA M that
accepts L$. We prove the theorem by construction of an unambiguous grammar G such that L(M)=
L(G). We construct G as follows:
The algorithm PDAtoCFG proceeded in two steps:
1. Invoke convenPDAtorestricted(M) to build M', an equivalent PDA in restricted normal form.
2.Invoke buildgrammar(M'), to build an equivalent grammar
G So the construction that proves the theorem is:

buildunambiggrammar(M:deterministicPDA)=
1. Let
G=buildgrammar(convertPDAtodetnormalfor
m(M)).
2. Let G' be the result of substituting ε for $ in
each rule in which $ occurs.
3. Return G'.

NOTE: The algorithm convertPDAtodetnormalform, is described in the theorem that proves the
deterministic context-free languages are closed under complement.

The Decidable Questions


Membership

"Given a language L and a string w, is w in L?'

This question can be answered for every context-free language and for every context-free
language L there exists a PDA M such that M accepts L. But existence of a PDA that accepts L does
not guarantee the existence of a procedure that decides it.
It turns out that there are two alternative approaches to solving this problem, both of which
work:

● Use a grammar: Using facts about every derivation that is produced by a grammar in
Chomsky normal form, we can construct an algorithm that explores a finite number of
derivation paths and finds one that derives a particular string w iff such a path exists.
● Use a PDA : While not all PDAs halt, it is possible, for any context-free language L, to
craft a PDA M that is guaranteed to halt on all inputs and that accepts all strings in L and
rejects all strings that are not in L.

Using a Grammar to Decide

Algorithm for deciding whether a string w is in a language L:

decideCFLusingGrammar(L: CFL,w: string) =

1. If L is specified as a PDA, use PDA to CFG, to construct a grammar G such that L(G) =L (M).

2. If L is specified as a grammar G, simply use G.

3. If w =ε then if SG is nullable then accept, otherwise reject.

4. If w ≠ ε then:

1. From G, construct G' such that L (G') = L(G)-{ε} and G' is in Chomsky normal form.

2. If G derives to, it does so in (2 • |w| - 1) steps. Try all derivations in G of that

number of steps. If one of them derives w, accept. Otherwise reject.

Using a PDA to Decide


A two-step approach.
 We first show that, for every context-free language L, it is possible to build a PDA that
accepts L-{ε} and that has no ε-transitions.
 Then we show that every PDA with no ε-transitions is guaranteed to halt

Elimination of ε-Transitions
Theorem: Given any context-free grammar G=(V,Σ,R,S), there exists a PDA M
such that L(M)=L(G)-{ε} and M contains no transitions of the
form
((q1,ε,α),(q2,𝜷)). In other words, every transition reads exactly one input
character.
Proof: The proof is by a construction that begins by converting G to Greibach normal form. Now
consider again the algorithm cfgtoPDAtopdown, which builds, from any context-free grammar G, a
PDA M that, on input w, simulates G deriving w, starting from S.
M= ({p,q},Σ,V,Δ, p,{q}), where Δ contains:
1.The start-up transition ((p,ε,ε),(q,S)), which pushes the start symbol on to the stack
and goes to state q.
2.For each rule X→s1s2...sn, in R, the transition ((q,ε,X),(q,s1s2...sn)), which replaces X
by s1s2...sn. If n=0 (i.e., the right-hand side of the rule is ε), then the transition ( (q, ε, X), (q,
ε)).
3.For each character c ∈ Σ. the transition ((q, c, c), (q,ε)), which compares an
expected character from the stack against the next input character.
If G contains the rule X→cs2...sn, (where c ∈Σ and s2 through sn, are elements of V-Σ), it is not
necessary to push c onto the stack, only to pop it with a rule from step 3.
Instead, we collapse the push and the pop into a single transition. So we create a transition that can
be taken only if the next input character is c. In that case, the string s2 through sn is pushed onto the
stack.
Since terminal symbols are no longer pushed onto the stack. We no longer need the transitions
created in step3 of the original algorithm.
So, M=({p,q},Σ,V,Δ,p,{q}), where Δ contains:
1. The start-up transitions: For each rule S→cs2...sn the transition ((p,c,ε),(q,s2...sn)).
2. For each rule X→cs2...sn (where c∈Σ and s2 through sn, are elements of V-Σ), the
transition ((q,c,X),(q,s2...sn)).
cfgtoPDAnoeps(G:context-freegrammar)=
4. Convert G to Greibach normal form, producing G'.
5. From G' build the PDA M described above.

Halting Behavior of PDAs Without ε-Transitions


Theorem: Let M be a PDA that contains no transitions of the form ((q1,ε,s1),(q2,s2)). i.e., no ε-
transitions. Consider the operation of M on input w∈ Σ*. M must halt and either accept or reject w.
Let n=|w|.
We make three additional claims:
a) Each individual computation of M must halt within n steps.
b) The total number of computations pursued by M must be less than or equal to bn, where b
is the maximum number of competing transitions from any state in M.
c) The total number of steps that will be executed by all computations of M is bounded by nbn
Proof:
a)Since each computation of M must consume one character of w at each step and M will halt
when it runs out of input, each computation must halt within n steps.
b)M may split into at most b branches at each step in a computation. The number of steps in
a computation is less than or equal to n. So the total number of computations must be less than or
equal
to bn. bn
c)Since the maximum number of computations is and the maximum length of each is n,
the maximum number of steps that can be executed before all computations of M halt is nbn.
So a second way to answer the question, "Given a context-free language L and a string w, is w
in L?" is to execute the following algorithm:
decideCFLusingPDA(L:CFL,w:string)=
1. If L is specified as a PDA, use PDAtoCFG, to construct a grammar G such that L(G)=L(M).
2. If L is specified as a grammar G, simply use G.
3. If w=ε then if SG is nullable then accept, otherwise reject.
4.If w≠ε then:
1. From G, construct G' such that L(G')=L(G)-{ε} and G' is in Greibach normal form.
2. From G' construct, using cfgtoPDAnoeps, a PDA M' such that L(M')=L(G') and M'
has no ε-transitions.
3. We have proved previously that, all paths of M' are guaranteed to halt
within a finite number of steps. So run M' on w, Accept if M' accepts and reject
otherwise.

Emptiness and Finiteness


Decidability of Emptiness and Finiteness
Theorem: Given a context-free language L. There exists a decision procedure that answers each of
the following questions:
1. Given a context-free language L, is L=⦰?
2. Given a context-free language L, is L infinite?
Since we have proven that there exists a grammar that generates L iff there exists a PDA that accepts
it. These questions will have the same answers whether we ask them about grammars or about PDAs.
Proof :
decideCFLempty( G: context-free grammar) =
1. Let G' =removeunproductive(G).
2. If S is not present in G' then return True else return False.
decideCFLinfinite(G:context-freegrammar)=
3. Lexicographically enumerate all strings in Σ* of length greater than bn and less than or
equal to bn+1+bn.
4. If, for any such string w, decideCFL(L,w) returns True then return True. L is infinite.
5. If, for all such strings w, decideCFL(L,w) returns False then return False. L is not
infinite.

The Undecidable Questions


• Given a context-free language L, is L=Σ*?
• Given a CFL L, is the complement of L context-free?
• Given a context-free language L, is L regular?
• Given two context-free languages L1 and L2 is L1=L2?
• Given two context-free languages L1 and L2, is L1⊆ L2?
• Given two context-free languages L1and L2, is L1∩L2=⦰?
• Given a context-free language L, is L inherently ambiguous?
• Given a context-free grammar G, is G ambiguous?

TURING MACHINE
The Turing machine provides an ideal theoretical model of a computer. Turing machines are useful
in several ways:
• Turing machines are also used for determining the undecidability of certain languages and
•As an automaton, the Turing machine is the most general model, It accepts type-0
languages.
•It can also be used for computing functions. It turns out to be a mathematical model of
partial recursive functions.
• Measuring the space and time complexity of problems.
Turing assumed that while computing, a person writes symbols on a one-dimensional paper (instead
of a two dimensional paper as is usually done) which can be viewed as a tape divided into cells. In
Turing machine one scans the cells one at a time and usually performs one of the three simple
operations, namely:
(i) Writing a new symbol in the cell being currently scanned,
(ii) Moving to the cell left of the present cell, and
(iii) Moving to the cell right of the present cell.

Turing machine model

• Each ce l can store only one symbol.


• The input to and the output from the finite state automaton are affected by the R/W head which can

examine one cell at a time.


In one move, the machine examines the present symbol under the R/W head on the tape and the
present state of an automaton to determine:
(i) A new symbol to be written on the tape in the cell under the R/W head,
(ii) A motion of the R/W head along the tape: either the head moves one cell left
(L),or one cell right (R).
(iii) The next state of the automaton, and
(iv) Whether to halt or not.
Definition:
Turing machine M is a 7-tuple, namely (Q, Σ, 𝚪, 𝛅, q0, b, F), where
1. Q is a finite nonempty set of states.
2.𝚪 is a finite nonempty set of tape symbols,
3. b∈ 𝚪 is the blank.
4. Σ is a nonempty set of input symbols and is a subset o f 𝚪 and b∉Σ.
5. 𝛅 is the transition function mapping (q,x) onto (q',y,D) where D denotes the
direction of movement of R/W head; D=L orR according as the movement is to the left or
right.
6. q0∈ Q is the initial state, and
7. F⊆Q is the set of final states.
Notes:
(1) The acceptability of a string is decided by the reachability from the initial state to some final state.
(2) 𝛅 may not be defined for some elements of QX 𝚪.

REPRESENTATION OF TURINGMACHINES
We can describe a Turing machine employing
(i) Instantaneous descriptions using move-relations.
(ii) Transition table, and
(iii) Transition diagram (Transition graph).
REPRESENTATION BY INSTANTANEOUS DESCRIPTIONS
Definition: An ID of a Turing machine M is a string 𝛼𝛽𝛾, where 𝛽 is the present state of M, the
entire input string is split as 𝛼𝛾, the first symbol of 𝛾 is the current symbol a under the R/W head
and
𝛾 has all the subsequent symbols of the input string, and the string 𝛼 is the substring of the
input string formed by all the symbols to the left of a.
EXAMPLE: A snapshot of Turing machine is shown in below Fig. Obtain the instantaneous
description.

The present symbol under the R/W


head is a1. The present state is q3. So a1 is written to the right of q3 The nonblank symbols to the left
of al form the string a4a1a2a1a2a2, which is written to the left of q3. The sequence of nonblank
symbols to the right of a1 is a4a2. Thus the ID is as given in below Fig.
Notes: (1) For constructing the ID, we simply insert the current state in the input string to the left of
the symbol under the R/W head.
(2) We observe that the blank symbol may occur as part of the left or right substring.

REPRESENTATION BY TRANSITION TABLE

We give the definition of 𝛅 in the form of a table called the transition table If (q, a)=(𝛾,𝛼,𝛽).
We write 𝛼𝛽𝛾 under the 𝛼-column and in the q-row. So if we get 𝛼𝛽𝛾 in the table, it means
that 𝛼 is written in the current cell, 𝛽 gives the movement of the head (L or R) and 𝛾 denotes the
new state into which the Turing machine enters.
EXAMPLE:
Consider, for example, a Turing machine with five states q1,...,q5 where q1 is the initial state and q5 is
the (only) final state. The tape symbols are 0,1and b. The transition table given below describes 𝛅:

REPRESENTATION BY TRANSITION DIAGRAM (TD)


The states are represented by vertices. Directed edges are used to represent transition of
states. The labels are triples of the form (𝛼,𝛽,𝛾)where 𝛼,𝛽∈𝚪and𝛾∈{L,R}.When there is
a directed edge from qi to qj with label (𝛼,𝛽,𝛾),it means that 𝛅(qi,𝛼)=(qj,𝛽,𝛾).
EXAMPLE:
LANGUAGE ACCEPTABILITY BY TURING MACHINES
Let us consider the Turing machine M=(Q,Σ,𝚪,𝛅,q0,b,F). A string w in Σ* is said to
be accepted by M, if q0w 𝛼1p𝛼2 for some P∈F and 𝛼1,𝛼2∈𝚪*.
EXAMPLE: Consider the Turing machine M described by the table below

IDs for the strings (a) 011 (b)0011 (c)001

As (q5,1) is not defined, M halts; so the input string 011 is not accepted

M halts. As q6 is an accepting state, the input string 0011is accepted byM.


M halts. As q2 is not an accepting state,001 is not accepted by M.

DESIGN OF TURING MACHINES


Basic guidelines for designing a Turing machine:
1.The fundamental objective in scanning a symbol by the R/W head is to know what to do
in the future. The machine must remember the past symbols scanned. The Turing machine
can remember this by going to the next unique state.
2.The number of states must be minimized. This can be achieved by changing the states
only when there is a change in the written symbol or when there is a change in the movement of the
R/W head.
EXAMPLE 1
Design aTuring machine to recognize all strings consisting of an even number of 1's.
Solution:
The construction is made by defining moves in the following manner:
(a) ql is the initial state. M enters the state q2 on scanning 1 and writes b.
(b) If M is in state q2 and scans 1, it enters ql and writes b.
(c) ql is the only accepting state.
Symbolically M= ({ql,q2},{1,b},{1,b},𝛅,q,b,{ql}), Where 𝛅 is defined by ,
EXAMPLE 2: Design a TM that accepts {0n1n| n≥ 0}
Solution: We require the following moves:
(a)If the leftmost symbol in the given input string w is 0, replace it by x and move right till
we encounter a leftmost 1in w. Change it to y and move backwards.
(b) Repeat (a) with the leftmost 0. If we move back and forth and no 0 or 1 remains. Move to a final
state.
(c)For strings not in the form 0n1n, the resulting state has to be non-
final. we construct a TM M as follows:M = (Q, Σ,𝚪, 𝛅, q0,b, F)
Q = {q0,q1,q2,q3,qf}
F = {qf}
Σ = { 0,1}
𝚪=
{ 0,1,x,y,b
}

Computation sequence of 0011:

q4 is final state, hence 0011 is accepted by M.


TECHNIQUES FOR TM CONSTRUCTION
1. TURING MACHINE WITH STATIONARY HEAD
Suppose, we want to include the option that the head can continue to be in the same cell for
some input symbol. Then we define (q,a) as (q',y,S).This means that the TM, on reading the input
symbol a, changes the state to q' and writes y in the current cell in place of a and continues to remain
in the same cell. In this model (q, a) =(q', y, D) where D = L, R or S.
2. STORAGE IN THE STATE
We can use a state to store a symbol as well. So the state becomes a pair(q,a) where q is the
state and a is the tape symbol stored in (q, a). So the new set of states becomes Qx𝚪.
EXAMPLE: Construct a TM that accepts the language 0 1* + 1 0*.
We have to construct a TM that remembers the first symbol and checks that it does not
appear afterwards in the input string.
So we require two states, q0, q1. The tape symbols are 0,1 and b. So the TM, having the
'storage facility in state‘, is M=({q0,q1}X{0,1,b},{0,1},{0,1,b},𝛅,[q0,b],[q1,b]})
We describe 𝛅 by its implementation description.
1.In the initial state, M is in q0 and has b in its data portion. On seeing the first symbol of the
input sting w, M moves right, enters the state q1 and the first symbol, say a, it has seen.
2. M is now in [q1,a].
(i) If its next symbol is b, M enters [q1,b], an accepting state.
(ii) If the next symbol is a, M halts without reaching the final state (i.e.𝛅 is not defined).
(iii) If the next symbol is ā, (ā=0 if a=1 and ā=1 if a=0), M moves right without
changing state.
3. Step2 is repeated until M reaches [q1,b] or halts (𝛅 is not defined for an input symbol in w).
4. MULTIPLE TRACK TURING MACHINE
In a multiple track TM, a single tape is assumed to be divided into several tracks. Now the
tape alphabet is required to consist of k-tuples of tape symbols, k being the number of tracks. In the
case of the standard Turing machine, tape symbols are elements of r; in the case of TM with multiple
tracks, it is 𝚪k.
4. SUBROUTINES
First a TM program for the subroutine is written. This will have an initial state and a 'return'
state. After reaching the return state, there is a temporary halt for using a subroutine, new states are
introduced. When there is a need for calling the subroutine, moves are effected to enter the initial
state for the subroutine. When the return state of the subroutine is reached, return to the main
program of TM.
EXAMPLE: Design a TM which can multiply two positive integers.
Solution: The input (m,n), m,n being given ,the positive integers represented by 0m10n.
M starts with 0m10n in its tape. At the end of the computation, 0mn (mn in unary representation)
surrounded by b's is obtained as the output.
The major steps in the construction are as follows:
1. 0m10n1 is placed on the tape (the output will be written after the rightmost 1).
2. The leftmost 0 is erased.
3. A block of n 0's is copied onto the right end.
4. Steps 2 and 3 are repeated m times and 10m10mn is obtained on the tape.
5. The prefix 10m1of 10m10mn is erased, leaving the product 0mn as the output.
For every 0 in 0m, 0n is added onto the right end. This requires repetition of step3. We define a
subroutine called COPY for step3. For the subroutine COPY the initial state is q1 and the final state
is q5 is given by the transition table as below:

The Turing machine M has the initial state q0. The initial ID for M is q00m10n. On seeing 0,the
following moves take place

q1 is the initial state of COPY. The following moves take place for M1:

After exhausting 0s, q1 encounters 1. M1 moves to state q4. All 2's are converted back to 0's
and M1 halts in q5. The TM M picks up the computation by starting from q5 The q0 and q6 are the
states of M. Additional states are created to check whether reach 0 in 0m gives rise to 0m at the end of
the rightmost 1 in the input string. Once this is over, M erases 10n1 and finds 0mn in the input tape.
M can be defined by M=({q0,q1,....q12}{0,1},{0,,2,b},𝛅,q0,b,{q12}) where 𝛅 is defined by
table given below:
ADDITIONAL PROBLEMS
1. ​Design a Turing machine to obtain complement of a binary number.
IDEA OF CONSTRUCTION:
1) If symbol is 0 change it to 1, move read write head to RIGHT
2) If symbol is 1 change it to 0, move read write head to RIGHT
3)Symbol is b (blank) don’t change, move read write head to RIGHT, and
HALT. The construction is made by defining moves in the following manner:
(a) ql is the initial state. On scanning 1, no change in state and write 0 and move head to
RIGHT.
(c) If M is in state q1and scans blank, it enters q2 and writes b move to right.
(d) q2 is the only accepting state.
Symbolically, M=({ql,q2},{1,0,b},{1,0,b},𝛅,ql,b,{q2}) Where 𝛅 is defined by:

The computation sequence of 1010:

2. ​Design a TM that converts binary number into its 2’s complement representation.
IDEA OF CONSTRUCTION:
 Read input from left to right until right end blank is scanned.
 Begin scan from right to left keep symbols as it is until 1 found on input file.
 If 1 found on input file, move head to left one cell without changing input.
 Now until left end blank is scanned, change a l 1’s to 0 and 0’s to
1. We require the following moves:
(a)Let q1 be initial state, until blank is scanned, move head to RIGHT without
changing anything. On scanning blank, move head to RIGHT change state to q2 without
changing the content of input.
(b)If q2 is the state, until 1 is scanned, move head to LEFT without changing anything.
On reading 1, change state to q3, move head to LEFT without changing input.
(c)If q3 is the state, until blank is scanned, move head to LEFT, if symbol is 0 change to
1, otherwise if symbol is 1 change to 0.On finding blank change state to q4, move head to
LEFT without Changing input.
(d)q4 is the only accepting
state. We construct a TM M as follows:
M = (Q, Σ,, 𝛅, q0,b, F)
Q = {q1,q2,q3,q4}
F = {q4 }
Σ = {0,1}
𝚪=
{0,1,b}

3. Design a TM that add two integers


IDEA OF
CONSTRUCTION:
 Read input from LEFT
to RIGHT until blank
(separator of two
numbers) is found.
 Continue LEFT to
RIGHT until blank (end
of second number) is
found.
 Change separator b to 1
move head to RIGHT.
 move header to Left ( to
point rightmost 1)
 Change 1 to b and move
right, Halt.
(d) q4 is the final state, TM halts.
we construct a TM M as follows: M = (Q, Σ, 𝚪, 𝛅, q0,b,
F)
Q = {q1,q2,q3,q4}
F = {q4}
Σ = { b,1}
𝚪= {1,b}

4. ​Design a TM that accepts the set of all palindromes over


{0,1}* IDEA OF CONSTRUCTION:
 If it is 0 and changes to X, similarly if it is 1, it is changed to Y, and moves right until it finds
blank.
 Starting at the left end it checks the first symbol of the input,
 Nowmovesonestepleftandcheckwhetherthesymbolreadmatchesthemostrecentlychanged.Ifsoiti
salsochangedcorrespondingly.
 Now machine moves back left until it finds 0 or 1.
 This process is continued by moving left and right alternately until a l 0’s and 1’s have been
matched.
We require the following moves:
5. If state is q0and it scans 0.
 Then go to state q1 and change the 0 to an X,
 move RIGHT over a l 0’s and 1’s, until it finds either X or Y or B
 Now move one step left and change state to q3
 It verifies that the symbol read is 0, and changes the 0 to X and goes to state q5.
6. ​If state is q0 and it scans 1

 Then go to state q2 and change the 1 to an Y,


 Move RIGHT over a l 0’s and 1’s, until it finds either X or Y or B
o Now move one step left and change state to q4
o It verifies that the symbol read is 1, and changes the 1 to Y and goes to state q5.
3. If state is q5
 Move LEFT over a l 0’s and 1’s, until it finds either X or Y.
 Now move one step RIGHT and change state to q0.
 Now at q0 there are two cases:
1. If 0’s and 1’s are found on input , it repeats the matching cycle just described.
2. If X’s and Y’s are found on input, then it changes all the 0’s to X and all the 1’s to Y’s.
The input was a palindrome of even length, Thus, state changed to q6.
4.If state is q3 or q4
If X’s and Y’s are found on input, it concludes that: The input was a palindrome of odd
length, thus, state changed to q6.
We construct a TM M as follows:
M = (Q, Σ, 𝚪, 𝛅, q0,b,F)
Q = {q0,q1,q2,q3,q4,q5,q6}
F = {q6}
Σ = { b,1,0}
𝚪 = {X,Y,b}

PRACTICE PROBLEMS
1. Design a Turing machine to replace a l a’s with X and a l b’s with Y.
2. Design a Turing machine to accept anbm n>m.
3. Design a Turing machine to accept anbn n<m.
4. Design a Turing machine to accept (0+1)*00(0+1)* .
5. Design a Turing machine to increment a given input.
6. Design a Turing machine to decrement a given input.
7. Design a Turing machine to subtract two unary numbers.
8. Design a Turing machine to multiply two unary numbers.
9. Design a Turing machine to accept a string 0’s followed by a 1.
10. Design a Turing machine to verify if the given binary number is an even number or not.
11. Design a Turing machine to shift the given input by one cell to left.
12. Design a Turing machine to shift the given input to the right by one cell .
13. Design a Turing machine to rotate a given input by one cell.
14. Design a Turing machine to erase the tape.
15. Design a Turing machine to accept anbncn .
16. Design a Turing machine to accept any string of a’s & b’s with equal number of a’s & b’s.
17. Design a Turing machine to accept anb2n.
18. Design a Turing machine to accept anbkcm: where n=m+k.
19. Design a Turing machine to accept anbkcm: where m=n+k.
20. Design a Turing machine to accept anbkcm: where k=m+n.

You might also like