You are on page 1of 20

Program 1.

Find the number of lines in a program, number of single comments, number of


multiple comments, number of curly braces and number of executable lines.

Code: (In Python)

import re
fo=open('input.c','r')
f=fo.readlines()
print('Number of lines is:', len(f))
f1=[]
for i in f:
i=i.strip()
f1.append(i)
tsc=0
for i in f1:
c=i[:2]
if(c=='//'):
tsc+=1
print('Number of single comment lines is:',tsc)
tcb=0
for i in f1:
c=i[:1]
if(c=='{'):
tcb+=1
print('Number of curly braces is:',tcb*2)
l1=[]
l2=[]
for i in range(0,len(f1)):
if f1[i].startswith('/*'):
l1.append(i)
if f1[i].endswith('*/'):
l2.append(i)
count=0
tmc=len(l2)
print('Number of multiple comment lines is:',tmc)
for i in range(0,len(l1)):
count+=l2[i]-l1[i]+1
print('Number of lines in multi comments is:',count)
tel=len(f1)-count-tsc
print('Number of executable lines is:',tel)

Input file: (input.c)

#include<stdio.h>
void main()
{
//declaring a variable
int a;
/*value of a is 5*/
a=5;
//Done declaring
printf("Value of a is:%d",a);
/*This prints
value of a*/
}

Output:

Program 2.
Find the cyclomatic complexity of a code and get the adjacency matrix for a
program.

Input File: (input.c)

#include<stdio.h>
int main()
{
int a=5;
if(a%2==0)
return 1;
else
return 0;
return 0;
}

Script File Code: (In Python to convert cfg file to dot file)

f=open('input.c.012t.cfg','r')
f1=open('input.dot','a')
f1.write("digraph cfg {\n");
f1.write("Entry -> 2\n");
l=f.readlines()
l1=[]
for i in l:
if i.startswith(';;'):
i=i[2:]
i=i.strip()
l1.append(i)
for i in range(0,len(l1)):
t1=[];t2=[];t3=[]
if (l1[i][:5]=='basic'):
temp=l1[i].split()
block=temp[2][:-1]
for j in range(i+1,len(l1)):
if(l1[j][:5]=='basic'):
break
else:
t1.append(l1[j])
print('t1=',t1,end='\n')
for j in range(0,len(t1)):
t2=t1[j].split()
print('t2=',t2)
if(len(t2)!=1):
command=t2[0]
if(command=="succ:"):
str=block+" -> "+t2[1]+"\n"
f1.write(str)
print(str)
for k in range(j+1,len(t1)):
str=block+" -> "+t1[k]+"\n"
f1.write(str)
print(str)
f1.write("}");
f1.close()
Output of Script Code: (input.dot)

Code: (To convert dot file to adjacency matrix and find cyclomatic complexity)

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int v,e;//vertices and edges
int main(int argc,char *argv[])
{
int **AdjMat;
FILE *fp;
int temp;
void countvertex(FILE *fp);
void matcreate(FILE *fp,int **AdjMat);
void print(int **AdjMat);
if(argc!=2)
{
printf("File name is missing");
exit(0);
}
else
{
fp=fopen(argv[1],"r");//argv[0] is a.out and argv[1] is file name
countvertex(fp);//number of vertices is counted
AdjMat=(int **)calloc(v,sizeof(int *));//we passed int * because each row we need
many values in it
for(temp=0;temp<v;temp++)
{
AdjMat[temp]=(int *)calloc(v,sizeof(int));//creating elements i rows
}
rewind(fp);//moving file pointer to position 0
matcreate(fp,AdjMat);
printf("\n Adjacency Matrix:\n");
print(AdjMat);
printf("\n Cyclomatic Complexity: %d",e-v+2);
fclose(fp);
}
}

void countvertex(FILE *fp)


{
char wrd[100],*line=NULL;
//reading every valus as char and if it is digit converting to int else leave as a char
size_t len=0;
int vertex[100]={0};
int eof,j;
getline(&line,&len,fp);//no need of firstline from file.
while(eof=(getline(&line,&len,fp))!=-1)
{
sscanf(line,"%s",wrd);
if(!strcmp(wrd,"}"))//matched with curlu brace, so break
break;
else
{
e++;//we need to count the number of edges too.
if(!strcmp(wrd,"Entry"))
{
v++;
vertex[1]=-1;
}
else
{
j=atoi(wrd);//integer reprsentation of word
if(vertex[j]==0)
{
v++;
vertex[j]=-1;
}
}
}
}
v++;
free(line);//we release the space of line. Optional
}
void matcreate(FILE *fp,int **AdjMat)
{
char wrd[100],*line=NULL;
size_t len=0;
int l,i,j,src,dest,eof;
//while loop for line and for loop for words in a line
getline(&line,&len,fp);
while(eof=getline(&line,&len,fp)!=-1)
{
for(i=0,j=0;1==sscanf(line+i,"%s%n",wrd,&l);i=i+l,j++)
{
if(!strcmp(wrd,"}"))
break;
if(j==0)
{
if(!strcmp(wrd,"Entry"))
src=0;//---------------change 1 to 0 here
else
src=atoi(wrd);
}
else if(j==2)
{
if(!strcmp(wrd,"EXIT"))
dest=1;//---------------change 0 to 1 here
else
dest=atoi(wrd);
}
else
continue;
}
AdjMat[src][dest]=1;
printf("\n %d ---> %d",src,dest);
}
free(line);
}

void print(int **AdjMat)


{
int temp,temp1;
for(temp=0;temp<v;temp++)
{
printf("\n");
for(temp1=0;temp1<v;temp1++)
printf("%d",AdjMat[temp][temp1]);
}
}
Output: (Adjacency matrix for the file input.c)

Program 3.
Program to create dominator matrix using adjacency matrix for the above input
file.

Code:

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int v,e;//vertices and edges
//----------- Dominator Matrix
void CheckLoopHeader(int **DomMat,int **AdjMat);
void DominatorAnalysis(int **DomMat,int **AdjMat);
//Dominator Matrix-----------
int main(int argc,char *argv[])
{
int **AdjMat,**DomMat;//---------added **DomMat here
FILE *fp;
int temp;
void countvertex(FILE *fp);
void matcreate(FILE *fp,int **AdjMat);
void print(int **AdjMat);
if(argc!=2)
{
printf("File name is missing");
exit(0);
}
else
{
fp=fopen(argv[1],"r");//argv[0] is a.out and argv[1] is file name
countvertex(fp);//number of vertices is counted
AdjMat=(int **)calloc(v,sizeof(int *));//we passed int * because each row we need
many values in it
for(temp=0;temp<v;temp++)
{
AdjMat[temp]=(int *)calloc(v,sizeof(int));//creating elements i rows
}
rewind(fp);//moving file pointer to position 0
matcreate(fp,AdjMat);
printf("\n Adjacency Matrix:\n");
print(AdjMat);
printf("\n Cyclomatic Complexity: %d\n",e-v+2);

//-----------Dominator Matrix
DomMat=(int **)calloc(v,sizeof(int *));
for(temp=0;temp<v;temp++)
DomMat[temp]=(int *)calloc(v,sizeof(int ));

DominatorAnalysis(DomMat,AdjMat);

printf("\nDominator Matrix for graph\n");


print(DomMat);
printf("\n");
CheckLoopHeader(DomMat,AdjMat);
//Dominator Matrix----------------

fclose(fp);
}
}

void countvertex(FILE *fp)


{
char wrd[100],*line=NULL;
//reading every valus as char and if it is digit converting to int else leave as a char
size_t len=0;
int vertex[100]={0};
int eof,j;
getline(&line,&len,fp);//no need of firstline from file.
while(eof=(getline(&line,&len,fp))!=-1)
{
sscanf(line,"%s",wrd);
if(!strcmp(wrd,"}"))//matched with curlu brace, so break
break;
else
{
e++;//we need to count the number of edges too.
if(!strcmp(wrd,"Entry"))
{
v++;
vertex[1]=-1;
}
else
{
j=atoi(wrd);//integer reprsentation of word
if(vertex[j]==0)
{
v++;
vertex[j]=-1;
}
}
}
}
v++;
free(line);//we release the space of line. Optional
}
void matcreate(FILE *fp,int **AdjMat)
{
char wrd[100],*line=NULL;
size_t len=0;
int l,i,j,src,dest,eof;
//while loop for line and for loop for words in a line
getline(&line,&len,fp);
while(eof=getline(&line,&len,fp)!=-1)
{
for(i=0,j=0;1==sscanf(line+i,"%s%n",wrd,&l);i=i+l,j++)
{
if(!strcmp(wrd,"}"))
break;
if(j==0)
{
if(!strcmp(wrd,"Entry"))
src=0;//---------------change 1 to 0 here
else
src=atoi(wrd);
}
else if(j==2)
{
if(!strcmp(wrd,"EXIT"))
dest=1;//---------------change 0 to 1 here
else
dest=atoi(wrd);
}
else
continue;
}
AdjMat[src][dest]=1;
printf("\n %d ---> %d",src,dest);
}
free(line);
}

void print(int **AdjMat)


{
int temp,temp1;
for(temp=0;temp<v;temp++)
{
printf("\n");
for(temp1=0;temp1<v;temp1++)
printf("%d",AdjMat[temp][temp1]);
}
}

//--------------Dominator Matrix
void DominatorAnalysis(int **DomMat,int **AdjMat)
{
int i=0,temp,tempx,tempy,flag;
int *tempArr;
tempArr=(int *)calloc(v,sizeof(int));
for(tempx=0;tempx<v;tempx++)
{
for(tempy=0;tempy<v;tempy++)
{
if(tempx==0 && tempy==0)
DomMat[tempx][tempy]=1;
else if(tempx==0)
DomMat[tempx][tempy]=0;
else
DomMat[tempx][tempy]=1;
}
}
printf("\nAfter initializing Dominator Matrix for Graph\n");
print(DomMat);
flag=1;
while(flag==1)
{
flag=0;
printf("\n\n\n------------------------------ Iteration - %d ------------------------------\n\n",i++);
for(tempx=1;tempx<v;tempx++)
{
for(temp=0;temp<v;temp++)
tempArr[temp]=1;
for(tempy=0;tempy<v;tempy++)
{
if(AdjMat[tempy][tempx]==1)
{
for(temp=0;temp<v;temp++)

tempArr[temp]=tempArr[temp]&&DomMat[tempy][temp];
}
}
tempArr[tempx]=1;
for(temp=0;temp<v;temp++)
{
if(DomMat[tempx][temp]!=tempArr[temp])
{
DomMat[tempx][temp]=tempArr[temp];
flag=1;
}
}
printf("\nAfter doing intersection of all predecessor of node %d the Dominator
Matrix for Graph\n",tempx);
print(DomMat);
}
}

void CheckLoopHeader(int **DomMat,int **AdjMat)


{
int tempx,tempy,flag=0;
for(tempx=0;tempx<v;tempx++)
{
for(tempy=0;tempy<v;tempy++)
{
if(AdjMat[tempx][tempy] && DomMat[tempx][tempy])
{
printf("Loop back edge %d --> %d and %d is the loop
header\n",tempx,tempy,tempy);
flag=1;
}
}
}
if(flag==0)
printf("\nNo Loops are present in the given Graph\n\n\n");
}
//Dominator Matrix------------

Output:

Program 4.
Write a program to count the number of nodes present in a loop for the following
input codes.

Code:

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int v,e;//vertices and edges
//----------- Dominator Matrix
void CheckLoopHeader(int **DomMat,int **AdjMat);
void DominatorAnalysis(int **DomMat,int **AdjMat);
//Dominator Matrix-----------
//************No of nodes in loop
void PrintLoopNode(int **DomMat,int **AdjMat,int i,int j);
//No of nodes in loops*******************
int main(int argc,char *argv[])
{
int **AdjMat,**DomMat;//---------added **DomMat here
FILE *fp;
int temp;
void countvertex(FILE *fp);
void matcreate(FILE *fp,int **AdjMat);
void print(int **AdjMat);
if(argc!=2)
{
printf("File name is missing");
exit(0);
}
else
{
fp=fopen(argv[1],"r");//argv[0] is a.out and argv[1] is file name
countvertex(fp);//number of vertices is counted
AdjMat=(int **)calloc(v,sizeof(int *));//we passed int * because each row we need
many values in it
for(temp=0;temp<v;temp++)
{
AdjMat[temp]=(int *)calloc(v,sizeof(int));//creating elements i rows
}
rewind(fp);//moving file pointer to position 0
matcreate(fp,AdjMat);
printf("\n Adjacency Matrix:\n");
print(AdjMat);
printf("\n Cyclomatic Complexity: %d",e-v+2);
//-----------Dominator Matrix
DomMat=(int **)calloc(v,sizeof(int *));
for(temp=0;temp<v;temp++)
DomMat[temp]=(int *)calloc(v,sizeof(int ));
DominatorAnalysis(DomMat,AdjMat);
printf("\nDominator Matrix for graph\n");
print(DomMat);
printf("\n");
CheckLoopHeader(DomMat,AdjMat);
//Dominator Matrix----------------
fclose(fp);
}
}
void countvertex(FILE *fp)
{
char wrd[100],*line=NULL;
//reading every valus as char and if it is digit converting to int else leave as a char
size_t len=0;
int vertex[100]={0};
int eof,j;
getline(&line,&len,fp);//no need of firstline from file.
while(eof=(getline(&line,&len,fp))!=-1)
{
sscanf(line,"%s",wrd);
if(!strcmp(wrd,"}"))//matched with curlu brace, so break
break;
else
{
e++;//we need to count the number of edges too.
if(!strcmp(wrd,"Entry"))
{
v++;
vertex[1]=-1;
}
else
{
j=atoi(wrd);//integer reprsentation of word
if(vertex[j]==0)
{
v++;
vertex[j]=-1;
}
}
}
}
v++;
free(line);//we release the space of line. Optional
}
void matcreate(FILE *fp,int **AdjMat)
{
char wrd[100],*line=NULL;
size_t len=0;
int l,i,j,src,dest,eof;
//while loop for line and for loop for words in a line
getline(&line,&len,fp);
while(eof=getline(&line,&len,fp)!=-1)
{
for(i=0,j=0;1==sscanf(line+i,"%s%n",wrd,&l);i=i+l,j++)
{
if(!strcmp(wrd,"}"))
break;
if(j==0)
{
if(!strcmp(wrd,"Entry"))
src=0;//---------------change 1 to 0 here
else
src=atoi(wrd);
}
else if(j==2)
{
if(!strcmp(wrd,"EXIT"))
dest=1;//---------------change 0 to 1 here
else
dest=atoi(wrd);
}
else
continue;
}
AdjMat[src][dest]=1;
printf("\n %d ---> %d",src,dest);
}
free(line);
}
void print(int **AdjMat)
{
int temp,temp1;
for(temp=0;temp<v;temp++)
{
printf("\n");
for(temp1=0;temp1<v;temp1++)
printf("%d",AdjMat[temp][temp1]);
}
}
//--------------Dominator Matrix
void DominatorAnalysis(int **DomMat,int **AdjMat)
{
int i=0,temp,tempx,tempy,flag;
int *tempArr;
tempArr=(int *)calloc(v,sizeof(int));
for(tempx=0;tempx<v;tempx++)
{
for(tempy=0;tempy<v;tempy++)
{
if(tempx==0 && tempy==0)
DomMat[tempx][tempy]=1;
else if(tempx==0)
DomMat[tempx][tempy]=0;
else
DomMat[tempx][tempy]=1;
}
}
printf("\nAfter initializing Dominator Matrix for Graph\n");
print(DomMat);
flag=1;
while(flag==1)
{
flag=0;
printf("\n\n\n------------------------------ Iteration - %d ------------------------------\n\n",i++);
for(tempx=1;tempx<v;tempx++)
{
for(temp=0;temp<v;temp++)
tempArr[temp]=1;
for(tempy=0;tempy<v;tempy++)
{
if(AdjMat[tempy][tempx]==1)
{
for(temp=0;temp<v;temp++)

tempArr[temp]=tempArr[temp]&&DomMat[tempy][temp];
}
}
tempArr[tempx]=1;
for(temp=0;temp<v;temp++)
{
if(DomMat[tempx][temp]!=tempArr[temp])
{
DomMat[tempx][temp]=tempArr[temp];
flag=1;
}
}
printf("\nAfter doing intersection of all predecessor of node %d the Dominator
Matrix for Graph\n",tempx);
print(DomMat);
}
}

}
void CheckLoopHeader(int **DomMat,int **AdjMat)
{
int tempx,tempy,flag=0;
for(tempx=0;tempx<v;tempx++)
{
for(tempy=0;tempy<v;tempy++)
{
if(AdjMat[tempx][tempy] && DomMat[tempx][tempy])
{
printf("Loop back edge %d --> %d and %d is the loop
header\n",tempx,tempy,tempy);
flag=1;
//************No of nodes in loop
PrintLoopNode(DomMat,AdjMat,tempx,tempy);
//No of nodes in loops*******************
}
}
}
if(flag==0)
printf("\nNo Loops are present in the given Graph\n\n\n");
}
//Dominator Matrix------------
//************No of nodes in loop
void PrintLoopNode(int **DomMat,int **AdjMat,int i,int j)
{
//We are not using DomMat here.
int *nodestack,count=0,stackindex=-1,stacktop;
int *loopnode,temp;
nodestack=(int *)calloc(v,sizeof(int));
loopnode=(int *)calloc(v,sizeof(int));
stackindex++;//for the first time we change from -1 to 0
nodestack[stackindex]=i;
loopnode[i]=1;//i=15 so loopnode[15] means we included 15 to the set
loopnode[j]=1;//j=17 so loopnode[17] means we included 17 to the set
while(stackindex!=-1)//if stackindex=-1 means stack is empty
{
stacktop=nodestack[stackindex--];//we are taking the top of stack adn decrementing
count in stack
//count--;
for(temp=0;temp<v;temp++)//to check all vertices are pred of the top of the stack or
not
{
if(AdjMat[temp][stacktop]==1 && loopnode[temp]==0)//adjmat[i][j]==1 means
we say we have index from i to j and we are checking that eleement is included in list or not
{
loopnode[temp]=1;
stackindex++;
nodestack[stackindex]=temp;
//count++;
}
}
}
printf("Loop Nodes:");
for(temp=0;temp<v;temp++)
{
if(loopnode[temp]==1)
printf("%d,",temp);
}
printf("\n");
}
//No of nodes in loops*******************
Example Input File 1: (gcdsche.c)

//oddeven qq
#include<stdio.h>
void main()
{
int y1;
int y2;
int res=1;
int temp1;
int temp2;
while(y1!=y2)
{
temp1=y1%2;
temp2=y2%2;
if(temp1!=0 && temp2==0){
y2=y2/2;}
if(temp1!=0 && temp2!=0){
if(y1>y2){
y1=y1-y2;}
else{
y2=y2-y1;}
}
if(temp1==0 && temp2==0){
res=res*2;
y1=y1/2;
y2=y2/2; }
if(temp1==0 && temp2!=0) {
y1=y1/2;}
}
res=res*y1;
}

Dot File: (gcdsche.dot)


Output: (Adjacency matrix of gcdsche.c)

Output: (Dominator matrix and number of nodes in a loop of gcdsche.c)

You might also like