Professional Documents
Culture Documents
Toc Lab Manual
Toc Lab Manual
LAB MANUAL
(2021-22)
Theory of Computation
CONTENTS
To be the fountainhead of novel ideas & innovations in science & technology &
persist to be a foundation of pride for all Indians.
1. To prepare students for successful careers in software industry that meet the
needs of Indian and multinational companies.
2. To develop the skills among students to analyze real world problem &
implement with computer engineering solution and in multidisciplinary
projects.
PO5. Modern tool usage: Create, select, and apply appropriate techniques,
resources, and modern engineering and IT tools including prediction and modeling
to complex engineering activities with an understanding of the limitations.
PO6. The engineer and society: Apply reasoning informed by the contextual
knowledge to assess societal, health, safety, legal and cultural issues and the
consequent responsibilities relevant to the professional engineering practice.
PO8. Ethics: Apply ethical principles and commit to professional ethics and
responsibilities and norms of the engineering practice.
PO12. Life-long learning: Recognize the need for, and have the preparation and
ability to engage in independent and life-long learning in the broadest context of
technological change.
1. Explain the basic concepts of switching and finite automata theory & languages.
2. Relate practical problems to languages, automata, computability and complexity.
3. Construct abstract models of computing, check their power to recognize the languages and
analyze the grammar, its types, simplification and normal form.
4. Interpret rigorously formal mathematical methods to prove properties of languages, grammars
and automata.
5. Develop an overview of how automata theory, languages and computation are applicable in
engineering application.
2. While entering into the LAB students should wear their ID cards.
5. Students should sign in the LOGIN REGISTER before entering into the
laboratory.
6. Students should come with observation and record note book to the
laboratory.
INDEX
S.
Experiment Name Date Grade Signature
No.
Design a Program for creating machine that
1. 06/08/2021
accepts three consecutive one.
Design a Program for creating machine that
2. 13/08/2021
accepts the string always ending with 101.
3. Design a Program for Mode 3 Machine. 27/08/2021
Design a program for accepting decimal number
4. 03/09/2021
divisible by 2.
Experiment No. 1
Design a Program for creating machine that accepts three consecutive one.
#include <iostream.h>
#include<string.h>
#include<conio.h>
#include<stdio.h>
void main()
{clrscr();
int i, j;
int state=0;
char a[10];
cout<<"Enter the string to check\n";
gets(a);
int x = strlen(a);
if(state==3){
cout<<"Accept";
}else{
cout<<"Not Accept";
}
getch();
}
Output:
Enter the string to check
011101
Not Accept
Viva Questions:
1. What Is Difference Between Fa’s And Nfa’s. Are They Opposite To Each Other ?
Ans: FA stands for finite automata while NFA stands for non-deterministic finite automata, In FA there
must be a transition for each letter of the alphabet from each state. So in FA number of transitions must
be equal to (number of states * number of letter in alphabet).
While in NFA there may be more than one transition for a letter from a state. And finally every FA is
an NFA while every NFA may be an FA or not.
Ans: In TG, there are transitions for the strings. While in GTG, one can write whole RE as a transition
from one state to another one.
Experiment No. 2
Design a Program for creating machine that accepts the string always ending with 101.
#include <iostream.h>
#include<string.h>
#include<conio.h>
#include<stdio.h>
void main()
{
clrscr();
int i, j;
char a[10];
}
else
{
cout<<"Invalid!";
}
getch();
Output:
Enter the string to check
011101
Accept
Viva Questions:
Experiment No. 3
Design a Program for Mod 3 Machine.
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
#include<process.h>
void main()
{
clrscr();
char a[10];
cout<<"Enter the string";
gets(a);
int x=strlen(a);
for(int i=0;i<x;i++)
{
if(a[i]!='0' && a[i]!='1' && a[i]!=’2’ && a[i]!=’3’ && a[i]!=’4’ && a[i]!=’5’ && a[i]!=’6’ &&
a[i]!=’7’ && a[i]!=’8’ && a[i]!=’9’)
{
cout<<"invalid string";
getch();
exit(0);
}
int state=0;
for(i=0; i < x; i++)
state=2;
if(a[i]=='0' || a[i]==’3’ || a[i]==’6’ || a[i]==’9’ && state==2)
state= 0;
else if(a[i]=='1' || a[i]==’4’ || a[i]==’7’ && state==2)
state=1;
else if(a[i]=='2' || a[i]==’5’ || a[i]==’8’ && state==2)
state=2;
}
if(state==0)
cout<<"Accept";
else
cout<<"Not Accept";
getch();
}
Output:
0110
Accept
Viva Questions:
Experiment No. 4
Design a program for accepting decimal number divisible by 2.
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
#include<process.h>
void main()
{
clrscr();
char a[10];
cout<<"Enter the string";
gets(a);
int x=strlen(a);
for(int i=0;i<x;i++)
{
if(a[i]!='0' && a[i]!='1' && a[i]!=’2’ && a[i]!=’3’ && a[i]!=’4’ && a[i]!=’5’ && a[i]!=’6’ &&
a[i]!=’7’ && a[i]!=’8’ && a[i]!=’9’)
{
cout<<"invalid string";
getch();
exit(0);
}
int state=0;
for(i=0; i < x; i++)
else
cout<<"Not Accept";
getch();
}
Output:
12
Accept
Viva Questions:
1. How Moore And Mealy Machine Works In Computer Memory What Is Their Importance In
Computing ?
Ans: Mealy & Moore Machines work in computing as incrementing machine & 1’s complement
machine etc. These operations as basic computer operations so these machines are very important.
Experiment No. 5
Design a program for creating a machine which accepts string having equal no. of 1’s and 0’s.
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
#include<process.h>
void main()
{
clrscr();
char a[10];
cout<<"Enter the string";
gets(a);
int x=strlen(a);
for(int i=0;i<x;i++)
{
if(a[i]!='0' && a[i]!='1')
{
cout<<"invalid string";
getch();
exit(0);
}
}
int c0=0,c1=0;
for(i=0;i<x;i++)
{
if(a[i]=='0')
c0++;
else
c1++;
}
int temp=c0-c1;
if(temp==0)
cout<<"Accept";
else
cout<<"reject" ;
getch();
}
Output:
100101
Accept
Viva Questions:
1. What Is The Difference Between Semiword And Word Please Also Give An Example
Regarding This?
Ans: Word: A word is complete combinations of terminals only e.g. abba or ab or a or null string.
Semiword: A semiword is a string of terminals (may be none) concatenated with exactly one
nonterminal on the right i.e. a semi word, in general, is of the following form (terminal)(terminal) —
—- (terminal)(nonterminal)
For example
aaaaaaB , aabbaaaA , A.
Experiment No. 6
Design a program for creating a machine which count number of 1’s and 0’s in a given string.
#include <bits/stdc++.h>
using namespace std;
bool isOne(int i)
{
if (i == 1)
return true;
else
return false;
}
int main()
{
int a[] = { 1, 0, 0, 1, 0, 0, 1 };
return 0;
}
Output:
1’s: 3
0’s: 4
Viva Questions:
1. What Is The Difference Between Concatenation And Intersection Of Two Fa’s Also What Is
The Difference Among Union Of Two Fa’s And Addition Of Them?
Ans: In intersection of two FA’s only those strings are accepted which are independently accepted
by both FA’s, while in concatenation of two FA’s only those strings will be accepted in which
first part of string is accepted by first FA and remaining part of string is accepted by the second
FA.
While taking union of two FA’s one can represent it using + sign. So (FA1 U FA2) and (FA +
FA2) both are same. There is no difference between them.
2. Is It Possible To Make Cfg For Infix And Post-fix Expression’s Using Derivation Tree ?
Ans: Derivation tree is only used to derive words of language that is described by a CFG. Yes, we
can create CFG for languages infix expressions, postfix expressions.
Experiment No. 7
Design a Program to find 2’s complement of a given binary number.
#include <bits/stdc++.h>
using namespace std;
twos = ones;
for (i = n - 1; i >= 0; i--)
{
if (ones[i] == '1')
twos[i] = '0';
else
{
twos[i] = '1';
break;
}
}
if (i == -1)
twos = '1' + twos;
int main()
{
string bin = "1100";
printOneAndTwosComplement(bin);
return 0;
}
Output:
Viva Questions:
1. What Is The Difference Between The Strings And The Words Of A Language?
Ans: A string is any combination of the letters of an alphabet where as the words of a language are
the strings that are always made according to certain rules used to define that language.For example
if we take
Alphabet Σ = { a , b } Here a , b are the letters of this alphabet.
As you can see we can make a lot of strings from these letters a and b.
For example a,b,aa,ab,ba,bb,aaa,aab,aba,baa,…………………… and so on.
But when we define a language over this alphabet having no a’s and only odd number ofb’s. Then
the words of this language would have only those strings that have only odd number of b’s and no
a’s.some example words of our defined language are b , bbb , bbbbb , bbbbbbb
,……………………………..and so on.
So we can say that all the words are strings but all the strings may not be the words of a language.
Hence strings are any combination of letters of an alphabet and the words of a language are strings
made according to some rule.
2. What Is The Difference Between An Alphabet And An Element Of A Set. Whether Alphabet
Is An Element Of A Set Or It Is A Set Itself?
Ans: An Alphabet is a set in itself. The elements of an Alphabet are called letters.
For example
Binary Alphabet Σ = {0,1}
Here 0,1 are the letters of binary alphabet.
Binary Alphabet is very important because it the Alphabet used by the computer.
Set of Natural Numbers
N={1,2,3,4,5,…………………………………..}
Here 1,2,3……………………………………. are the elements of set of Natural Numbers.
Experiment No. 8
Design a Program which will increment the given binary number by 1.
#include <bits/stdc++.h>
using namespace std;
int addOne(int x)
{
int m = 1;
int main()
{
cout<<addOne(13);
return 0;
}
Output:
Viva Questions:
2. What Is Non-determinism And Determinism And What Is The Difference Between Them ?
Ans: Determinism means that our computational model (machine) knows what to do for every possible
inputs. Non determinism our machine may or may not know what it has to do on all possible inputs.
As you can conclude from above definition that Non-Deterministic machine can not be implemented (
used ) on computer unless it is converted in Deterministic machine.
Experiment No. 9
Design a Program to convert NDFA to DFA.
#include <stdio.h>
#include <string.h>
#define STATES 50
struct Dstate
{
char name;
char StateString[STATES+1];
char trans[10];
int is_final;
}
Dstates[50];
struct tran
{
char sym;
int tostates[50];
int notran;
};
struct state
{
int no;
struct tran tranlist[50];
};
int stackA[100],stackB[100],C[100],Cptr=-1,Aptr=-1,Bptr=-1;
struct state States[STATES];
char temp[STATES+1],inp[10];
int nos,noi,nof,j,k,nods=-1;
void pushA(int z)
{
stackA[++Aptr]=z;
}
void pushB(int z)
{
stackB[++Bptr]=z;
}
int popA()
{
return stackA[Aptr--];
}
void copy(int i)
{
char temp[STATES+1]=" ";
int k=0;
Bptr=-1;
strcpy(temp,Dstates[i].StateString);
while(temp[k]!='\0')
{
pushB(temp[k]-'0');
k++;
}
}
int popB()
{
return stackB[Bptr--];
}
int peekB()
{
return stackA[Bptr];
}
int peekA()
{
return stackA[Aptr];
}
int seek(int arr[],int ptr,int s)
{
int i;
for(i=0;i<=ptr;i++)
{
if(s==arr[i])
return 1;
}
return 0;
}
void sort()
{
int i,j,temp;
for(i=0;i<Bptr;i++)
{
for(j=0;j<(Bptr-i);j++)
{
if(stackB[j]>stackB[j+1])
{
temp=stackB[j];
stackB[j]=stackB[j+1];
stackB[j+1]=temp;
}
}
}
}
void tostring()
{
int i=0; sort();
for(i=0;i<=Bptr;i++)
{
temp[i]=stackB[i]+'0';
}
temp[i]='\0';
}
void display_DTran()
{
int i,j;
printf("\n\t\t DFA Transition Table ");
printf("\n\t\t -------------------- ");
printf("\nStates\tString\tInputs\n ");
for(i=0;i<noi;i++)
{
printf("\t%c",inp[i]);
}
printf("\n \t----------");
for(i=0;i<nods;i++)
{
if(Dstates[i].is_final==0)
printf("\n%c",Dstates[i].name);
else
printf("\n*%c",Dstates[i].name);
printf("\t%s",Dstates[i].StateString);
for(j=0;j<noi;j++)
{
printf("\t%c",Dstates[i].trans[j]);
}
}
printf("\n");
}
void move(int st,int j)
{
int ctr=0;
while(ctr<States[st].tranlist[j].notran)
{
pushA(States[st].tranlist[j].tostates[ctr++]);
}
}
void lambda_closure(int st)
{
int ctr=0,in_state=st,curst=st,chk;
while(Aptr!=-1)
{
curst=popA();
ctr=0;
in_state=curst;
while(ctr<=States[curst].tranlist[noi].notran)
{
chk=seek(stackB,Bptr,in_state);
if(chk==0)
pushB(in_state);
in_state=States[curst].tranlist[noi].tostates[ctr++];
chk=seek(stackA,Aptr,in_state);
if(chk==0 && ctr<=States[curst].tranlist[noi].notran)
pushA(in_state);
}
}
}
main()
{
int final[20],start,fin=0,i;
char c,ans,st[20];
printf("\nEnter no. of states in NFA : ");
scanf("%d",&nos);
for(i=0;i<nos;i++)
{
States[i].no=i;
}
printf("\nEnter the start state : ");
scanf("%d",&start);
printf("Enter the no. of final states : ");
scanf("%d",&nof);
printf("\nEnter the final states : \n");
for(i=0;i<nof;i++)
scanf("%d",&final[i]);
printf("\nEnter the no. of input symbols : ");
scanf("%d",&noi);
c=getchar();
printf("\nEnter the input symbols : \n ");
for(i=0;i<noi;i++)
{
scanf("%c",&inp[i]);
c=getchar();
}
inp[i]='e';
Dstates[i].trans[j]=Dstates[k].name;
break;
}
}
if(k==nods)
{
nods++;
for(k=0;k<nof;k++)
{
fin=seek(stackB,Bptr,final[k]);
if(fin==1)
{
Dstates[nods-1].is_final=1;
break;
}
}
strcpy(Dstates[nods-1].StateString,temp);
Dstates[nods-1].name='A'+nods-1;
Dstates[i].trans[j]=Dstates[nods-1].name;
}
}
i++;
}
display_DTran();
}
Viva Questions:
1. What Is Difference Between Fa’s And Nfa’s. Are They Opposite To Each Other ?
Ans: FA stands for finite automata while NFA stands for non-deterministic finite automata, In FA there
must be a transition for each letter of the alphabet from each state. So in FA number of transitions must
be equal to (number of states * number of letter in alphabet).
While in NFA there may be more than one transition for a letter from a state. And finally every FA is
an NFA while every NFA may be an FA or not.
Experiment No. 10
Design a Program to create PDA machine that accept the well-formed parenthesis.
#include<bits/stdc++.h>
using namespace std;
if (s.empty())
return false;
switch (expr[i])
{
case ')':
case '}':
case ']':
int main()
{
string expr = "{()}[]";
if (areParanthesisBalanced(expr))
cout << "Balanced";
else
cout << "Not Balanced";
return 0;
}
Output:
Viva Questions:
2. What Is Difference Between Push Down Stack And Push Down Store ?
Ans: No difference at all. Both terms are used to describe memory structure attached with FAs to
store some characters in it.
Experiment No. 11
Design a PDA to accept WCWR where w is any string and WR is reverse of that string and C is a
Special symbol.
#include <stdio.h>
#include <string.h>
void isPalindrome(char str[])
{
// Start from leftmost and rightmost corners of str
int l = 0;
int h = strlen(str) - 1;
int main()
{
isPalindrome("abba");
isPalindrome("abbccbba");
isPalindrome("geeks");
return 0;
}
Output:
Viva Questions:
Experiment No. 12
Design a Turing machine that’s accepts the following language a n bn cn where n>0.
#include<iostream>
#include<stdlib.h>
#include<string.h>
using namespace std;
int main()
{
char a[50];
int i,j,k,len;
cout<<"Enter the string to check\n"; //input the string
cin>>a;
if(strlen(a)%3!=0) //if string length is not multiple of 3 , reject it
{
cout<<"Not accepted\n";
exit(0);
}
len=strlen(a)/3; //len= length of the input string
for(i=0;i<len;i++)
{
if(a[i]!='a')
{
cout<<"Not accepted\n";
exit(0);
}
}
for(j=i;j<i+len;j++) //checks if next len/3 characters are ‘b’
{
if(a[j]!='b')
{
cout<<"Not accepted\n";
exit(0);
}
}
for(k=j;k<j+len;k++) //checks if next len/3 characters are ‘c’
{
if(a[k]!='c')
{
cout<<"Not accepted";
exit(0);
}
}
/*if all conditions are satisfied, it shows that input is accepted*/
cout<<"Accepted\n";
return 0;
}
Output:
Viva Questions:
Definition
A Turing Machine (TM) is a mathematical model it includes an infinite length tape categorized into
cells along with the given input. It includes a head which is used to read t he input tape. Here a state
register saves the state of the Turing machine. Once you read an input symbol, it will exchange with
another symbol and it changes the internal state. At the same time it moves from one cell to the right or
left. Once the TM reaches the final state, then the input string is accepted, otherwise rejected.
A TM is usually explained as a 7-tuple (Q, X, ∑, δ, q0, B, F) where −
• Q is a finite set of states
• X is the tape alphabet
• ∑ is the input alphabet
• δ is a transition function; δ : Q × X → Q × X × {Left_shift, Right_shift}.
• q0 is the initial state
• B is the blank symbol
• F is the set of final states
Ans: While a turning machine refers to the time complexity to calculate the number of times the
tape moves when the machine started to provide some input symbols and the space complexity
while writing the number of cells of the tape written.
Experiment No. 13
State Machine diagram for ATM.
State machine diagram is a behavior diagram which shows discrete behavior of a part of designed
system through finite state transitions. Behavioral state machine is specialization of behavior
and is used to specify discrete behavior of a part of designed system through finite state transitions.
The state machine formalism used in this case is an object -based variant of Harel statecharts.
Behavior is modeled as a traversal of a graph of state nodes connected with transitions. Transitions
are triggered by the dispatching of series of events. During the traversal, the state machine could also
execute some activities.
Behavioral state machine could be owned by behavioral classifier which is called its context. The
context defines which signal and call triggers are defined for this state machine, and which attributes
and operations are available in activities of the state machine. Signal triggers and call triggers for the
state machine are defined according to the receptions and operations of this classifier.
State machine may have an associated behavioral feature (specification) and be the method of this
behavioral feature. In this case the state machine specifies the behavior of this behavioral feature. The
parameters of the state machine match the parameters of the behavioral feature and provide the means
for accessing the behavioral feature parameters within the state machine.
The event pool for the state machine is the event pool of the instance according to the behaviored
context classifier, or the classifier owning the behavioral feature for which the state machine is a
method.
The context classifier of the method state machine of a behavioral feature must be the classifier that
owns the behavioral feature. A state machine without a context classifier may use triggers that are
independent of receptions or operations of a classifier, i.e., either just signal triggers or call triggers
based upon operation template parameters of the (parameterized) state machine.
The association between a state machine and its context classifier or behavioral feature does not have a
special notation.
State machine could be rendered in the frame labeled as state machine or stm in abbreviated form. The
content area of the frame is usually state machine itself but in general it could contain other kinds of
UML diagrams.
Service
IPS ACADEMY
16 Collages, 71 Courses, 51 Acre Campus
Knowledge Village
Rajendra Nagar
A. B. Road Indore
452012(M.P.) India
Ph: 0731-4014601-604 Mo: 07746000161
E Mail: office.ies@ipsacademy.org
Website: ies.ipsacademy.org & www.ipsacademy.org
1