TOC (CS 501) Lab Manual Compressed

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 42

TOC LAB [2022]

ORIENTAL COLLEGE OF TECHNOLOGY, BHOPAL

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

LAB MANUAL (CS-501)

“Theory of Computation”

BACHELOR OF TECHNOLOGY (B.Tech) COURSE

SEMESTER –V
TOC LAB [2022]

Oriental college of Technology, Bhopal


Department of Computer Science & Engineering

LABORATORY PLAN

Department CSE Session 2022


Name of
Prof. Namrata Choudhary
Faculty
Subject Theory of Computation Subject Code CS-501

Actual
Exp CO HOD
Experiment Date of Remarks
No. Verification
Completion

1 Design a Program for creating


machine that accepts the string CO1
always ending with 101.
2 Design a Program for creating
machine that accepts three CO2
consecutive one.

3 Design a Program for Mode 3 CO3


Machine
4 Design a program for accepting
CO4
decimal number divisible by 2

5 Design a program for creating a


machine which accepts string having CO4
equal no. of 1’s and 0’s.

6 Design a program for creating a


machine which count number of 1’s CO4
and 0’s in a given string.

7 Design a Program to find 2’s


CO5
complement of a given binary
TOC LAB [2022]

number.

8 Design a Program which will


increment the given binary number CO5
by 1.

9 Design a Program to convert NDFA


CO5
to DFA.

10 Design a Program to create PDA


machine that accept the well-formed CO5
parenthesis.

11 Design a PDA to accept WCWR


where w is any string and WR is
CO5
reverse of that string and C is a
Special symbol.

12 Design a Turing machine that’s


accepts the following language an b CO5
n c n where n>0.
TOC LAB [2022]

Oriental college of Technology, Bhopal


Department of Computer Science & Engineering

LAB MANUAL
Theory of Computation
EXPERIMENT-1

AIM: Design a Program for creating machine that accepts the string always ending
with 101.

Discussion:

As per the AIM, set of valid strings are represented by set A:


A = {101, 110111101, 1110101, 0101011110101,...}
means any string should be declared valid if it contains 101 at the end of string. Let M be the
machine for above AIM, hence it can be define as M(Q, Σ, 𝛿, q0, F) where
Q: set of states: {q0,q1,q2,q3}
Σ: set of input symbols: {0, 1}
q0: initial state (q0)
F: set of Final states: {q3}
𝛿: Transition Function: (Transition state diagram is shown in Figure 1.)

State Input
- 0 1

Q0 Q0 Q1

Q1 Q2 Q1

Q2 Q0 Q3

Q3 Q0 Q1
TOC LAB [2022]

Transition Diagram

Code in C++

#include<iostream>
using namespace std;

void q0(string,int);
void q1(string,int);
void q2(string,int);
void q3(string,int);

void q0(string s, int i)


{
cout<<"q0->";
if(i==s.length())
{
cout<<"\n Rejected";
return;
}
if(s[i]=='0')
q0(s,i+1);
else
q1(s,i+1);
}
void q1(string s, int i)
{
cout<<"q1->";
if(i==s.length())
{
cout<<"\n Rejected";
TOC LAB [2022]

return;
}
if(s[i]=='0')
q2(s,i+1);
else
q1(s,i+1);
}
void q2(string s, int i)
{
cout<<"q2->";
if(i==s.length())
{
cout<<"\n Rejected";
return;
}
if(s[i]=='0')
q0(s,i+1);
else
q3(s,i+1);
}
void q3(string s, int i)
{
cout<<"q3->";
if(i==s.length())
{
cout<<"\n Given String is accepted";
return;
}
if(s[i]=='0')
q0(s,i+1);
else
q1(s,i+1);
}
int main()
{
string s;
cout<<"Enter the string (0 or 1)";
cin>>s;
cout<<"The state of transition are :";
q0(s,0);}
TOC LAB [2022]

EXPERIMENT 2
AIM: Design a Program for creating machine that accepts three consecutive one.

Discussion:

As per the AIM, set of valid strings are represented by set A:


A = {111, 0111, 1110, 0101011110101,...}
means any string should be declared valid if it contains 101 at the end of string. Let M be the
machine for above AIM, hence it can be define as M(Q, Σ, 𝛿, q0, F) where
Q: set of states: {q0,q1,q2,q3}
Σ: set of input symbols: {0, 1}
q0: initial state (q0)
F: set of Final states: {q3}
𝛿: Transition Function: (Transition state diagram is shown in Figure 2.)

State Input
- 0 1
Q0 Q0 Q1
Q1 Q0 Q2
Q2 Q0 Q3
Q3 Q3 Q3

Transition Diagram
TOC LAB [2022]

#include<iostream>
using namespace std;

void q0(string,int);
void q1(string,int);
void q2(string,int);
void q3(string,int);

void q0(string s, int i)


{
cout<<"q0->";
if(i==s.length())
{
cout<<"\n Rejected";
return;
}
if(s[i]=='0')
q0(s,i+1);
else
q1(s,i+1);
}
void q1(string s, int i)
{
cout<<"q1->";
if(i==s.length())
{
cout<<"\n Rejected";
return;
}
if(s[i]=='0')
q0(s,i+1);
else
q2(s,i+1);
}
void q2(string s, int i)
{
cout<<"q2->";
if(i==s.length())
{
cout<<"\n Rejected";
return;
TOC LAB [2022]

}
if(s[i]=='0')
q0(s,i+1);
else
q3(s,i+1);
}
void q3(string s, int i)
{
cout<<"q3->";
if(i==s.length())
{
cout<<"\n Given String is accepted";
return;
}
if(s[i]=='0')
q3(s,i+1);
else
q3(s,i+1);
}
int main()
{
string s;
cout<<"Enter the string (0 or 1)";
cin>>s;
cout<<"The state of transition are :";
q0(s,0);
}
TOC LAB [2022]

EXPERIMENT 3
AIM: Design a Program for Mode 3 Machine
Discussion:

As per the AIM, set of valid strings are represented by set A:


A = = {0, 00, 000, 11, 011, 110, ...}
means any binary string that when divide by three gives remainder zero. Let M be the machine for
above AIM, hence it can be define as M(Q, Σ, 𝛿, q0, F) where
Q: set of states: {q0,q1,q2}
Σ: set of input symbols: {0, 1}
q0: initial state (q0)
F: set of Final states: {q0}
𝛿: Transition Function: (Transition state diagram is shown in Figure 3.)

State Input
- 0 1

Q0 Q0 Q1

Q1 Q2 Q0

Q2 Q1 Q2

Transition Diagram
TOC LAB [2022]

#include<iostream>
using namespace std;

void q0(string,int);
void q1(string,int);
void q2(string,int);

void q0(string s, int i)


{
cout<<"q0->";
if(i==s.length())
{
cout<<"\n Rejected";
return;
}
if(s[i]=='0')
q0(s,i+1);
else
q1(s,i+1);
}
void q1(string s, int i)
{
cout<<"q1->";
if(i==s.length())
{
cout<<"\n Rejected";
return;
}
if(s[i]=='0')
q2(s,i+1);
else
q0(s,i+1);
}
void q2(string s, int i)
{
cout<<"q2->";
if(i==s.length())
{
cout<<"\n Rejected";
return;
}
TOC LAB [2022]

if(s[i]=='0')
q1(s,i+1);
else
q2(s,i+1);
}
int main()
{
string s;
cout<<"Enter the string (0 or 1)";
cin>>s;
cout<<"The state of transition are :";
q0(s,0);
}
TOC LAB [2022]

EXPERIMENT 4
AIM: Design a program for accepting decimal number divisible by 2.

Discussion:
As per the AIM, set of valid strings are represented by set A:
A = {0,1,2,3,4,5,6,7,8,9}.
means any binary string that when divide by two gives remainder zero. Let M be the
machine for above AIM, hence it can be define as M(Q, Σ, 𝛿, q0, F) where
Q: set of states: {q, q0}
Σ: set of input symbols: {0, 1}
q0: initial state {q0}
F: set of Final states: {q0}
𝛿: Transition Function: (Transition state diagram is shown in Figure 4.)

State Input
- 0,2,4,6,8 1,3,5,7,9
Q0 Q0 Q1
Q1 Q2 Q0

Transition Diagram
Figure 1: FSM - accepting binary string if divisible by 2
TOC LAB [2022]

#include<iostream>
using namespace std;

void q0(string,int);
void q1(string,int);

void q0(string s, int i)


{
cout<<"q0->";
if(i==s.length())
{
cout<<"\n Given Binary String is Divisible by 2";
return;
}
if(s[i]=='0'||s[i]==’2’||s[i]==’4’||s[i]==’6’||s[i]==’8’)
q0(s,i+1);
else
q1(s,i+1);
}
void q1(string s, int i)
{
cout<<"q1->";
if(i==s.length())
{
cout<<"\n Given string is not Divisible by 2";
return;
}
if(s[i]=='0'||s[i]==’2’||s[i]==’4’||s[i]==’6’||s[i]==’8’)
q0(s,i+1);
else
q1(s,i+1);
}
int main()
{
string s;
cout<<"Enter the decimal no. string ";
cin>>s;
cout<<"The state of transition are :";
q0(s,0);
}
TOC LAB [2022]

EXPERIMENT 5

AIM: Design a program for creating a machine which accepts string having equal no. of 1’s and
0’s.

Discussion:

As per the AIM, set of valid strings are represented by set A:


A = {(), {()}, {[()]}, {}{[]},...}
means any string should be declared valid if it contains balanced paranthesis. Let M be the machine
for above AIM, hence it can be define as M(Q, Σ, 𝛿, q0, F) where
Q: set of states: {q0,q1 }
Σ: set of input symbols: {(,),{,},[,]}
q0: initial state (q0)
F: set of Final states: {q1}
𝛿: Transition Function: (Transition state diagram is shown in Figure 1.)

Row State Input ⸹(transition function) Stack(leftmost symbol represents top of stack) Stack after move

1 q0 0 ⸹(0,Z/0Z) 0 q0
2 q0 0 ⸹(0,0/00) 0 q0
3 q0 0 ⸹(0,1/€) € q0
4 q0 1 ⸹(1,Z/1Z) 1 q0
5 q0 1 ⸹(1,1/11) 1 q0
6 q0 1 ⸹(1,0/€) € q0
7 q0 € ⸹(€,Z/Z) q1

Fig:1-Transition Diagram

Solution:-
TOC LAB [2022]

#include<iostream>
#include<string.h>
using namespace std;
int top;
char s[10];
class Stack
{
public:
void push(int x)
{
s[top++]=x;
}
void pop(int x)
{
s[top--]=x;
}
};
int main()
{
int i,n;
char a[10];
cout<<"\nProgram For PDA Which Accpets Strings Of (0^n)(1^n)\n";
cout<<"\nEnter String::";
cin>>a;
n=strlen(a);
Stack st;
top=-1;
for(i=0;i<n;i++)
{
if(a[i]=='0' || a[i]=='1')
{
if(a[i]=='0')
{
st.push(a[i]);
}
else
{
st.pop(a[i]);
}
}
else
TOC LAB [2022]

{
break;
}
}
if(top==-1)
{
cout<<"\nString Accepted.\n";
}
else
{
cout<<"\nString Rejected.\n";
}
return 0;
}
TOC LAB [2022]

EXPERIMENT 6

AIM: Design a program for creating a machine which counts number of 1’s and 0’s in a given
string.

Discussion:

As per the AIM, set of valid strings are represented by set A:


A = {10, 1100, 101010, 011011001010,...}
means any string should be declared valid if it contains equal no. of 0’s and 1’s. Let M be the
machine for above AIM, hence it can be define as M(Q, Σ, 𝛿, q0, F) where
Q: set of states: {q0,q1,q2,q3}
Σ: set of input symbols: {0, 1}
q0: initial state (q0)
F: set of Final states: {q3}
𝛿: Transition Function: (Transition state diagram is shown in Figure 1.)

Next state

Present state input = 0 input = 1

State Output State Output

→A A y B x

B B y B x

Figure 1: FSM – Moore Machine for counting no. of 0’s and 1’s
TOC LAB [2022]
Solution:-

#include<iostream>
using namespace std;

void A(string,int);
void B(string,int);

int z=0;
int o=0;

void A(string s, int i)


{

if(i==s.length())
{
cout<<"\n no of zeros are"<<z;
cout<<"\n no. of ones are"<<o;
cout<<"\n thanku ";
return;
}
cout<<"\n A->";
if(s[i]=='0')
{
z++;
cout<<"\n out put is y";
A (s,i+1);
}
else
{
o++;
cout<<"\n output is x";
B(s,i+1);
}
}
void B(string s, int i)
{

if(i==s.length())
{
TOC LAB [2022]

cout<<"\n no of zeros are"<<z;


cout<<"\n no. of ones are"<<o;
cout<<"\n thanku ";
return;
}
cout<<"\n B->";
if(s[i]=='0')
{
z++;
cout<<"\n output is y";
A(s,i+1);
}
else
{
o++;
cout<<"\n output is x";
B(s,i+1);
}

}
int main()
{

string s;
cout<<"Enter the string (0 or 1)";
cin>>s;
cout<<"The state of transition are :";
A(s,0);

}
TOC LAB [2022]

EXPERIMENT 7

AIM: Design a Program to find 2’s complement of a given binary number.

Discussion:

As per the AIM, set of valid strings are represented by set A:


A = {10, 1100, 101010, 011011001010,...}
means any string should be declared valid if it contains equal no. of 0’s and 1’s. Let M be the
machine for above AIM, hence it can be define as M(Q, Σ, 𝛿, q0, F) where
Q: set of states: {q1,q2,q3}
Σ: set of input symbols: {0, 1}
q0: initial state (q1)
𝛿: Transition Function: (Transition state diagram is shown in Figure 1.)

Next state

Present state input = 0 input = 1

State Output State Output

→ q1 q1 0 q2 1

q2 q2 1 q3 0

q3 q2 1 q3 0

Transition Table

Figure 1: Machine for 2’s complement of binary string


TOC LAB [2022]

Solution:-

#include<iostream>
using namespace std;

void q1(string,int);
void q2(string,int);
void q3(string,int);

int a[10], j;
void q1(string s, int i)
{

if(i==s.length())
{
cout<<"\n 2's complement of the given stringis : ";
for(j=s.length()-1;j>=0;j--)
cout<<a[j];
return;
}
cout<<"\n q1->";
if(s[i]=='0')
{
cout<<"\n out put is 0";
a[j]=0;
j++;
q1(s,i+1);
}
else
{
cout<<"\n output is 1";
a[j]=1;
j++;
q2(s,i+1);
}
}
void q2(string s, int i)
{

if(i==s.length())
TOC LAB [2022]

cout<<"\n 2's complement of the given stringis : ";


for(j=s.length()-1;j>=0;j--)
cout<<a[j];
return;
}
cout<<"\n q2->";
if(s[i]=='0')
{
cout<<"\n output is 1";
a[j]=1;
j++;
q2(s,i+1);
}
else
{
cout<<"\n output is 0";
a[j]=0;
j++;
q3(s,i+1);
}

}
void q3(string s, int i)
{

if(i==s.length())
{
cout<<"\n 2's complement of the given stringis : ";
for(j=s.length()-1;j>=0;j--)
cout<<a[j];
return;
}
cout<<"\n q2->";
if(s[i]=='0')
{
cout<<"\n output is 1";
a[j]=1;
j++;
q2(s,i+1);
TOC LAB [2022]

}
else
{
cout<<"\n output is 0";
a[j]=0;
j++;
q3(s,i+1);
}

}
int main()
{

string s;
cout<<"Enter the string (0 or 1)";
cin>>s;
string rev = string(s.rbegin(),s.rend());

cout<<"The state of transition are :";


q1(rev,0);
}
TOC LAB [2022]

EXPERIMENT 8

AIM: Design a Program which will increment the given binary number by 1.

Discussion:

As per the AIM, set of valid strings are represented by set A:


A = {10, 1100, 101010, 011011001010,...}
means any string should be declared valid if it contains equal no. of 0’s and 1’s. Let M be the
machine for above AIM, hence it can be define as M(Q, Σ, 𝛿, q0, F) where
Q: set of states: {q0,q1}
Σ: set of input symbols: {0, 1}
q0: initial state (q0)
𝛿: Transition Function: (Transition state diagram is shown in Figure 1.

Next state

Present state input = 0 input = 1

State Output State Output

→ q0 q1 1 q0 0

q1 q0 0 q1 1

Transition Table

Figure 1: Machine for binary string incremented by 1


TOC LAB [2022]

Solution:-

#include<iostream>
using namespace std;

void q0(string,int);
void q1(string,int);
void q2(string,int);

int a[10], j;
void q0(string s, int i)
{
if(i==s.length())
{
cout<<"\n binary string after incremented by 1 : ";
for(j=s.length()-1;j>=0;j--)
cout<<a[j];
return;
}
cout<<"\n q1->";
if(s[i]=='0')
{
cout<<"\n out put is 1";
a[j]=1;
j++;
q1(s,i+1);
}
else
{
cout<<"\n output is 0";
a[j]=0;
j++;
q0(s,i+1);
}
}
void q1(string s, int i)
{

if(i==s.length())
TOC LAB [2022]

cout<<"\n n binary string after incremented by 1: ";


for(j=s.length()-1;j>=0;j--)
cout<<a[j];
return;
}
cout<<"\n q2->";
if(s[i]=='0')
{
cout<<"\n output is 0";
a[j]=0;
j++;
q2(s,i+1);
}
else
{
cout<<"\n output is 1";
a[j]=1;
j++;
q1(s,i+1);
}

}
void q2(string s, int i)
{
if(i==s.length())
{
cout<<"\n binary string after incremented by 1 : ";
for(j=s.length()-1;j>=0;j--)
cout<<a[j];
return;
}
cout<<"\n q2->";
if(s[i]=='0')
{
cout<<"\n out put is 0";
a[j]=0;
j++;
q2(s,i+1);
}
TOC LAB [2022]

else
{
cout<<"\n output is 1";
a[j]=1;
j++;
q1(s,i+1);
}
}

int main()
{
string s;
cout<<"Enter the string (0 or 1)";
cin>>s;
string rev = string(s.rbegin(),s.rend());
cout<<"The state of transition are :";
q0(rev,0);
}
TOC LAB [2022]

EXPERIMENT 9

AIM: Design a Program to convert NDFA to DFA


Solution:-

#include <bits/stdc++.h>
#define pb push_back
using namespace std;

const int N=109;


int n, m;
vector<int> nt[N][N]; // stores the nfa table
int dt[N][N]; // stores the dfa table with entries into ds
vector<int> ds[N]; // stores the label of dfa state (union of nfa states)
int tot; // total no. of states in dfa

void print_dfa() {
cout << "\n DFA Table:\n";
cout << "================\n";
cout << "Q\t";
for(int j=0; j<m; j++) {
cout << j << "\t";
}
cout << endl;
for(int i=0; i<tot; i++) {
cout << "[";
for(int k=0; k < ds[i].size(); k++) cout << ds[i][k]; cout << "]\t";
for(int j=0; j<m; j++) {
cout << "[";
for(int k=0; k<ds[dt[i][j]].size(); k++) {
cout << ds[dt[i][j]][k];
}
cout << "]\t";
}
cout << endl;
}
cout << endl;
}

int main() {
TOC LAB [2022]

// set no of states ans symbols


n = 4, m = 2;

/*
Input:
21
11
11
01
010
21
33
2

*/

// input the count of transitions


for(int i=0; i<n; i++) {
for(int j=0; j<m; j++) {
int sz; cin >> sz;
nt[i][j].resize(sz);
}
}

// input the actual nfa transition table


for(int i=0; i<n; i++) {
for(int j=0; j<m; j++) {
for(int k=0; k < nt[i][j].size(); k++) {
cin >> nt[i][j][k];
}
}
}

queue<int> q;

// add {0} as the initial state


vector<int> v; v.pb(0); q.push(0);
ds[tot++] = v;

// keep adding new states reachable from initial state


TOC LAB [2022]

while(!q.empty()) {

int top = q.front(); q.pop();

for(int j=0; j<m; j++) {


vector<int> cur;
for(int i=0; i < ds[top].size(); i++) {
for(int k=0; k < nt[ds[top][i]][j].size(); k++) {
cur.pb(nt[ds[top][i]][j][k]);
}
}

sort(cur.begin(), cur.end());
cur.resize(unique(cur.begin(), cur.end()) - cur.begin());

// check if this state is encountered before


int prev = -1;
for(int i=0; i<tot; i++) {
if(ds[i] == cur) {
prev = i;
break;
}
}
if(prev == -1) {
ds[tot] = cur;
q.push(tot);
dt[top][j] = tot;
tot++;
} else {
dt[top][j] = prev;
}

}
}

print_dfa();

return 0;
}
TOC LAB [2022]

EXPERIMENT 10

AIM: Design a Program to create PDA machine that accept the well-formed parenthesis

Discussion:

As per the AIM, set of valid strings are represented by set A:


A = {10, 1100, 101010, 011011001010,...}
means any string should be declared valid if it contains equal no. of 0’s and 1’s. Let M be the
machine for above AIM, hence it can be define as M(Q, Σ, 𝛿, q0, F) where
Q: set of states: {q0,q1 }
Σ: set of input symbols: {0, 1}
q0: initial state (q0)
F: set of Final states: {q1}
𝛿: Transition Function: (Transition state diagram is shown in Figure 1.)

Row State Input ⸹(transition function) Stack(leftmost symbol represents top of stack) Stack after move

1 q0 ( ⸹((,Z/0Z) ( q0
2 q0 ( ⸹((,(/(() ( q0
3 q0 ( ⸹((,)/€) € q0
4 q0 1 ⸹(),)/))) ) q0
5 q0 1 ⸹(),(/€) € q0
6 q0 € ⸹(€,Z/Z) q1

Fig:1-Transition Diagram
TOC LAB [2022]
Solution:-

#include<bits/stdc++.h>
using namespace std;
bool areParanthesisBalance(string expr)
{
stack<char>s;
char x;
for(int i=0;i<expr.length();i++)
{
if(expr[i]=='('||expr[i]=='['||expr[i]=='{')
{
s.push(expr[i]);
continue;
}
if(s.empty())
return false;

switch(expr[i])
{
case')':
x=s.top();
s.pop();
if(x=='{'||x=='[')
return false;
break;

case'}':
x=s.top();
s.pop();
if(x=='('||x=='[')
return false;
break;

case']':
x=s.top();
s.pop();
if(x=='('||x=='{')
return false;
break;
TOC LAB [2022]

}
}
return(s.empty());
}
int main()
{
string expr;
cout<<"enter the paranthesis";
cin>>expr;
if(areParanthesisBalance(expr))
cout<<"balanced";
else
cout<<"Not Balanced";
return 0;
}
TOC LAB [2022]

EXPERIMENT 11

AIM: Design a PDA to accept WCWR where w is any string and WR is reverse of
that string and C is a Special symbol.
Discussion:
As per the AIM, set of valid strings that can be generated by given language is represented in set A:
A = {0C0, 1C1, 011000110C011000110, 101011C110101, ....}
means string must have some binary string followed by special character 'C' followed reverse of
binary string that appears before 'C'. Block diagram of push down automata is shown in Figure 1.

Figure 1: Block Diagram of Push Down Automata.

Input string can be valid or invalid, valid if the input string follow set A (define above). PDA has to
determine whether the input string is according to the language or not.

Let M be the PDA machine for above AIM, hence it can be define as M(Q, Σ, Г, δ, q0, Z0, F) where
Q: set of states: {q0, q1, q2}
Σ: set of input symbols: {0, 1, C}
Г: Set of stack symbols: {A, B, Z}
q0: initial state (q0)
Z0: initial stack symbol (Z)
F: set of Final states: { } [Note: Here, set of final states is null as decision of validity ofstring is
based on stack whether it is empty or not. If empty means valid else invalid.]
δ: Transition Function: (Transition state diagram is shown in Figure 2.)
TOC LAB [2022]
δ(q0, 0, Z) → (q0, AZ)

δ(q0, 1, Z) → (q0, BZ)

δ(q0, 0, A) → (q0, AA)

δ(q0, 0, B) → (q0, AB)

δ(q0, 1, A) → (q0, BA)

δ(q0, 1, B) → (q0, BB)

δ(q0, C, A) → (q1, A)

δ(q0, C, B) → (q1, B)

δ(q1, 0, A) → (q1, ε)

δ(q1, 1, B) → (q1, ε)

δ(q1, ε, Z) → (q2, ε)

Rules for implementing PDA for a given language


Initial Setup: Load the input string in input buffer, push Z as an initial stack symbol and consider
the machine in at initial state q0.
Rules:

1. If the input symbol of string is '0' and stack top is Z then push symbol 'A' into stack and read
the next character in input string.
2. If the input symbol of string is '1' and stack top is Z then push symbol 'B' into stack and read
the next character in input string.
3. If the input symbol of string is '0' and stack top is A or B then push symbol 'A' into stack and
read the next character in input string.
4. If the input symbol of string is '1' and stack top is A or B then push symbol 'B' into stack and
read the next character in input string.
5. If the input symbol of string is 'C' and stack top is A or B then change state from q0 to q1
and read the next character in input string.
6. If the input symbol of string is '0', machine state is q1 and stack top is A then pop stack top
and read the next character in input string.
7. If the input symbol of string is '1', machine state is q1 and stack top is B then pop stack top
and read the next character in input string.
8. If all the characters of input string are parsed, stack top is Z and machine state is q1, it
means string is valid, pop Z from stack and change the state from q1 to q2.
TOC LAB [2022]

Transition Diagram
Figure 2: PDA that accept language WCWR where w is any binary string and WR is reverse of that
string and C is a special symbol.

Code in C++
#include <iostream.h>
#include <conio.h>
#include <stdio.h>
void main()
{
char Input[100];
char stack[100];
int Top = -1;
clrscr();
cout<<"Enter string to be validate\n";
gets(Input);
stack[++Top] = 'Z';//Taking 'Z'as an initial stack symbol.
int i=-1;
q0:
i++;
if(Input[i]=='0' && stack[Top]== 'Z')
{

stack[++Top]= 'A';
TOC LAB [2022]
goto q0;

}
else if(Input[i]=='1' && stack[Top]== 'Z')

{
stack[++Top]= 'B';
goto q0;
}
else if(Input[i]=='0' && (stack[Top]== 'A'|| stack[Top]== 'B'))
{
stack[++Top]= 'A';
goto q0;
}
else if(Input[i]=='1' && (stack[Top]== 'A'|| stack[Top]== 'B'))
{
stack[++Top]= 'B';
goto q0;
}
else if(Input[i]=='C' &&(stack[Top]== 'A'||stack[Top]== 'B'))
{
goto q1;
}
else
{
goto Invalid;
}
q1:
i++;
if(Input[i]=='0' && stack[Top]== 'A')
{
Top--;
goto q1;
}
else if(Input[i]=='1' && stack[Top]== 'B')

Top--;

goto q1;

}
TOC LAB [2022]
else if(Input[i]=='\0' && stack[Top]== 'Z')

{
goto Valid;
}

else

goto Invalid;

Valid:
cout<<"\n Output: Valid String";
goto exit;
Invalid:
cout<<"\n Output: Invalid String";
goto exit;
exit:
getch();
}
TOC LAB [2022]

EXPERIMENT 12

AIM: Design a Turing machine that’s accepts the following language an b n c n where n>0
Mark 'a' then move right.

Mark 'b' then move right

Mark 'c' then move left

Come to far left till we get 'X'

Repeat above steps till all 'a', 'b' and 'c' are marked

At last if everything is marked that means string is accepted.

TAPE movement for string "aaabbbccc":

turing_machine_tape_for_a_to_power_n_and_b_to_power_n_and_c_to_power_n

Explanation of TAPE movement

Step 1-2

Input is given as "aaabbbccc" (scan string from left to right)

Mark 'a' as 'X' and move one step right

Reach unmarked 'b' and pass every 'a' and 'Y'(in case) on the way to 'b'

Step 3-4

Mark 'b' as 'Y' and move one step right

Reach unmarked 'c' and pass every 'b' and 'Z'(in case) on the way to 'c'

Mark 'c' as 'Z' and move one step left cause now we have to repeat process

Reach unmarked 'X' and pass every 'Z', 'b', 'Y', 'a' on the way to 'X'

Move to 'a' and repeat the process

Step 5-9

Step 1-4 are repeated

Step 10

When TAPE header reaches 'X' and next symbol is 'Y' that means 'a's are finished

Check for 'b's and 'c's by going till BLANK(in right) and passing 'Y' and 'Z' on the way
TOC LAB [2022]
If no 'b' and 'c' are not found that means string is accepted

State Transition Diagram

We have designed state transition diagram for anbncn | n ≥ 1 as follows:

Following Steps:

a. Mark 'a' with 'X' and move towards unmarked 'b'

b. Move towards unmarked 'b' by passing all 'a's

c. To move towards unmarked 'b' also pass all 'Y's if exist

Following Steps:

a. Mark 'b' with 'Y' and move towards unmarked 'c'

b. Move towards unmarked 'c' by passing all 'b's

c. To move towards unmarked 'c' also pass all 'Z's if exist

Following Steps:

a. Mark 'c' with 'Z' and move towards first 'X' (in left)

b. Move towards first 'X' by passing all 'Z's, 'b's, 'Y's and 'a's

c. When 'X' is reached just move one step right by doing nothing.
TOC LAB [2022]

To check all the 'a's, 'b's and 'c's are over add loops for checking 'Y' and 'Z' after "we get 'X' followed by 'Y'"

To reach final state(qf) just replace BLANK with BLANK and move either direction

You might also like