50% found this document useful (2 votes)
2K views

DAG Program For Compiler Lab

The document contains code for two programs - a type checking program and a directed acyclic graph (DAG) program. The type checking program takes input values for variables a and b, determines if they are integers or other data types, and checks for type errors. The DAG program builds a graph from edge inputs, and prints the graph representation. It contains functions to build and print the graph from edge array inputs.

Uploaded by

sendhil28
Copyright
© © All Rights Reserved
50% found this document useful (2 votes)
2K views

DAG Program For Compiler Lab

The document contains code for two programs - a type checking program and a directed acyclic graph (DAG) program. The type checking program takes input values for variables a and b, determines if they are integers or other data types, and checks for type errors. The DAG program builds a graph from edge inputs, and prints the graph representation. It contains functions to build and print the graph from edge array inputs.

Uploaded by

sendhil28
Copyright
© © All Rights Reserved
You are on page 1/ 4

CS6612-Compiler Laboratory

JP COLLEGE OF ENGINEERING

Type Checking Program


#include <stdio.h>
#include <conio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
char* type(char[],int);
void main()
{
char a[10],b[10],mess[20],mess1[20];
int i,l;
clrscr();
printf( "\n\n int a,b;\n\n int c=a+b\n");
printf( "\n\n Enter a value for a\n");
scanf("%s", a);
l=strlen(a);
printf(" \n a is :");
strcpy(mess,type(a,l));
printf("%s",mess);
printf( "\n\n Enter a value for b\n\n");
scanf("%s",b);
l=strlen(b);
printf(" \n b is :");
strcpy(mess1,type(b,l));
printf("%s",mess1);
if(strcmp(mess,"int")==0&&strcmp(mess1,"int")==0)
{
printf("\n\n No Type Error");
}
else
{
printf("\n\n Type Error");
}
N.SENTHIL MURUGAN AP/CSE

sendhil28@gmail.com

CS6612-Compiler Laboratory

JP COLLEGE OF ENGINEERING

getch();
}
char* type(char x[],int m)
{
int i;
char mes[20];
for(i=0;i<m;i++)
{
if(isalpha(x[i]))
{
strcpy(mes,"AlphaNumeric");
goto X;
}
else if (x[i]=='.')
{
strcpy(mes,"float");
goto X;
}
}
strcpy(mes,"int");
X:
return mes;
}

N.SENTHIL MURUGAN AP/CSE

sendhil28@gmail.com

CS6612-Compiler Laboratory

JP COLLEGE OF ENGINEERING

DAG Program:
#include <stdio.h>
#define N 8
typedef struct node node;
struct node
{
int count;
node *next;
};
node graph[N];
void buildGraph( int a[][2], int edges )
{
int i;
for( i=0; i<N; ++i ) {
graph[i].count = 0;
graph[i].next = NULL;
}
for( i=0; i<edges; ++i )
{
node *ptr = (node *)malloc( sizeof(node) );
ptr->count = a[i][1];
ptr->next = graph[ a[i][0] ].next;
graph[ a[i][0] ].next = ptr;
graph[ a[i][1] ].count++;
}
}
void printGraph()
{
int i;
node *ptr;

N.SENTHIL MURUGAN AP/CSE

sendhil28@gmail.com

CS6612-Compiler Laboratory

JP COLLEGE OF ENGINEERING

for( i=0; i<N; ++i )


{
node *ptr;
printf( "%d: pred=%d: connected to ", i, graph[i].count );
for( ptr=graph[i].next; ptr; ptr=ptr->next )
printf( "%d ", ptr->count );
printf( "\n" );
}
}
void main()
{
int a[8][2],i,j;
clrscr();
printf("\n\nEnter the edges b/w nodes \n");
for(i=0;i<8;i++)
{
for(j=0;j<2;j++)
{
scanf("%d",&a[i][j]);
}
}
buildGraph( a, 8 );
printf("\nThe DAG for (a+b)*c+((a+b)+e) : \n\n");
printGraph();
getch();
}

N.SENTHIL MURUGAN AP/CSE

sendhil28@gmail.com

You might also like