TOC LAB [2022]
Oriental college of Technology, Bhopal
D
De
ep
pa
ar
rt
tm
me
en
nt
t o
of
f C
Co
om
mp
pu
ut
te
er
r S
Sc
ci
ie
en
nc
ce
e &
& E
En
ng
gi
in
ne
ee
er
ri
in
ng
g
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.)
Present state
Next 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.)
Present state
Next 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.
Present state
Next 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 << "Qt";
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:
2 1
1 1
1 1
0 1
0 1 0
2 1
3 3
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(CS-501) (19-49).pdf

  • 1.
    TOC LAB [2022] Orientalcollege of Technology, Bhopal D De ep pa ar rt tm me en nt t o of f C Co om mp pu ut te er r S Sc ci ie en nc ce e & & E En ng gi in ne ee er ri in ng g 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
  • 2.
    TOC LAB [2022] TransitionDiagram 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";
  • 3.
    TOC LAB [2022] return; } if(s[i]=='0') q2(s,i+1); else q1(s,i+1); } voidq2(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);}
  • 4.
    TOC LAB [2022] EXPERIMENT2 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
  • 5.
    TOC LAB [2022] #include<iostream> usingnamespace 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;
  • 6.
    TOC LAB [2022] } if(s[i]=='0') q0(s,i+1); else q3(s,i+1); } voidq3(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); }
  • 7.
    TOC LAB [2022] EXPERIMENT3 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
  • 8.
    TOC LAB [2022] #include<iostream> usingnamespace 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; }
  • 9.
    TOC LAB [2022] if(s[i]=='0') q1(s,i+1); else q2(s,i+1); } intmain() { string s; cout<<"Enter the string (0 or 1)"; cin>>s; cout<<"The state of transition are :"; q0(s,0); }
  • 10.
    TOC LAB [2022] EXPERIMENT4 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
  • 11.
    TOC LAB [2022] #include<iostream> usingnamespace 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); }
  • 12.
    TOC LAB [2022] EXPERIMENT5 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:-
  • 13.
    TOC LAB [2022] #include<iostream> #include<string.h> usingnamespace 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
  • 14.
    TOC LAB [2022] { break; } } if(top==-1) { cout<<"nStringAccepted.n"; } else { cout<<"nString Rejected.n"; } return 0; }
  • 15.
    TOC LAB [2022] EXPERIMENT6 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.) Present state Next 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
  • 16.
    TOC LAB [2022] Solution:- #include<iostream> usingnamespace 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()) {
  • 17.
    TOC LAB [2022] cout<<"nno 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); }
  • 18.
    TOC LAB [2022] EXPERIMENT7 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.) Present state Next 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
  • 19.
    TOC LAB [2022] Solution:- #include<iostream> usingnamespace 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())
  • 20.
    TOC LAB [2022] { cout<<"n2'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);
  • 21.
    TOC LAB [2022] } else { cout<<"noutput 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); }
  • 22.
    TOC LAB [2022] EXPERIMENT8 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. Present state Next 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
  • 23.
    TOC LAB [2022] Solution:- #include<iostream> usingnamespace 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())
  • 24.
    TOC LAB [2022] { cout<<"nn 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); }
  • 25.
    TOC LAB [2022] else { cout<<"noutput 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); }
  • 26.
    TOC LAB [2022] EXPERIMENT9 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 << "Qt"; 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() {
  • 27.
    TOC LAB [2022] //set no of states ans symbols n = 4, m = 2; /* Input: 2 1 1 1 1 1 0 1 0 1 0 2 1 3 3 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
  • 28.
    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; }
  • 29.
    TOC LAB [2022] EXPERIMENT10 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
  • 30.
    TOC LAB [2022] Solution:- #include<bits/stdc++.h> usingnamespace 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;
  • 31.
    TOC LAB [2022] } } return(s.empty()); } intmain() { string expr; cout<<"enter the paranthesis"; cin>>expr; if(areParanthesisBalance(expr)) cout<<"balanced"; else cout<<"Not Balanced"; return 0; }