Tuesday 25 July 2017

Ty BCS Sys pro & OS All programs

All system programming and operating system programs

demo program


#include<stdio.h>
#include<string.h>
#include<stdlib.h>



typedef struct node
{
char symb[20];
int addr,len,val,decl,used;
struct node *link;
}NODE;

char optab[][6]={"STOP","ADD","SUB","MULT","MOVER","MOVEM","COMP","BE","DIV","READ","PRINT"};
char regtab[][5]={"AREG","BREG","CREG","DREG"};
char condtab[][4]={"LT","LE","EQ","GT","GE","ANY"};
char buff[80],t1[20],t2[20],t3[20],t4[20],fname[20];
FILE *fp;
int lc;
NODE *first,*last;

void add_symb1(char *s,int addr,int len,int val)
{
NODE *p;
p=(NODE*)malloc(sizeof(NODE));
strcpy(p->symb,s);
p->addr=addr;
p->len=len;
p->val=val;
p->decl=1;
p->used=0;
p->link=NULL;

if(first==NULL)
first=p;
else
last->link=p;
last=p;
}

void add_symb2(char *s)
{
NODE *p;
p=(NODE *)malloc(sizeof(NODE));
strcmp(p->symb,s);
p->addr=p->len=p->val=p->decl=0;
p->used=1;
p->link=NULL;

if(first==NULL)
first=p;
else
last->link=p;
last=p;
}

NODE * search_symb(char *s)
{
NODE *p;
p=first;
while(p!=NULL)
{
if(strcmp(p->symb,s)==0)
break;
p=p->link;
}
return p;
}

int search_optab(char *s)
{
int i;
for(i=0;i<11;i++)
if(strcmp(optab[i],s)==0)
return i;
return -1;
}
int search_regtab(char *s)
{
int i;
for(i=0;i<4;i++)
if(strcmp(regtab[i],s)==0);
return i;
return -1;
}
int search_condtab(char *s)
{
int i;
for (i=0;i<6;i++)
if(strcmp(condtab[i],s)==0)
return i;
return -1;
}
void print_symtab()
{
NODE *p;
int i=1;
printf("#\tsymbol\taddr\tlen\tval\tded \tused\n");
p=first;
while(p!=NULL)
{
printf("%d\t%s\t%d\t%d\t%d\t%d\t%d\n",i,p->symb,p->addr,p->len,p->val,p->decl,p->used);
i++;
p=p->link;
}
p=first;
while(p!=NULL)
{
if(p->decl==1 && p->used==0)
printf("Symb %s is declared but not used\n",p->symb);
if(p->decl==0 && p->used==1)
printf("Symb %s is used but not declared\n",p->symb);
p=p->link;
}
}

main()
{
NODE *p;
int i,j,n;
system("clear");
printf("Enter source file name:");
scanf("%s",fname);
fp=fopen(fname,"r");
if(p==NULL)
{
printf("File %s not found\n",fname);
exit(0);
}
while(!feof(fp))
{
fgets(buff,80,fp);
if(!feof(fp))
printf("%s\n",buff);
n=sscanf(buff,"%s%s%s%s",t1,t2,t3,t4);
{
n=sscanf(buff,"%s %s %s %s",t1,t2,t3,t4);
switch(n)
{
case 2:
if(strcmp(t1,"START")==0)
{
lc=atoi(t2)-1;
break;
}
i=search_optab(t1);
if(i==9 || i==10)//read
{
p=search_symb(t2);
if(p==NULL)
add_symb2(t2);
else
p->used=1;
break;
}
p=search_symb(t1);
if(p==NULL)
add_symb1(t1,lc,0,0);
else
{
if(p->decl==1)
printf("%d Redeclaration of symbol %s",lc,t1);
else
{
p->addr=lc;
p->decl=1;
}
}
p=search_symb(t3);
if(p==NULL)
add_symb2(t3);
else
p->used=1;
break;

case 4:
p=search_symb(t1);
if(p==NULL)
add_symb1(t1,lc,0,0);
else
{
if(p->decl==1)
printf("%d redeclaration of symbol",lc,t1);
else
{
p->addr=lc;
p->decl=1;
}
}
p=search_symb(t4);
if(p==NULL)
add_symb2(t4);
else
p->used=1;
}//switch
lc++;
}
}
fclose(fp);
print_symtab();

}




Download link sem 1 : click here

Download link sem 2 : click here

Download link slips :  click here 

No comments:

Post a Comment