#include<stdio.h>
#include<string.h>
void check_inout(void);
void check_assign(void);
void initialise(void);
void check_assign_3(void);

FILE *fd,*fs;
char source[10],dest[10],temp[30],temp1[30];
char assign1[20];

struct in_out
{
char var[10][10];
int count;
}in,out;

struct assign_in
{
char as[10][10];
int count;
}assign;

struct and_count
{
int count;
}and,or,xor,not,nand,nor,xnor,nand3,nor3;

int main(void)
{
initialise();
printf("Enter the source file name..");
scanf("%s",source);
printf("Enter the destination file name ..");
scanf("%s",dest);

if((fd=fopen(dest,"w"))==NULL) {
printf("Error opeaning source file");
exit();
}
if((fs=fopen(source,"r"))==NULL) {
printf("Error opeaning destination file");
exit();
}

fgets(temp,sizeof(temp),fs);
fprintf(fd,"`include\"genlib.h\"\n");
fprintf(fd,"%s",temp);
fscanf(fs,"%s",temp);
while(strcmp(temp,"input")==0 || strcmp(temp,"output")==0)
{
check_inout();
fscanf(fs,"%s",temp);
}
fprintf(fd,"\n");

while(strcmp(temp,"assign")==0)
{
check_assign();
fscanf(fs,"%s",temp);
}

fprintf(fd,"\n\nendmodule\n");
}


void check_inout(void)
{
if(strcmp(temp,"input")==0) 
{
fprintf(fd,"\ninput");
fscanf(fs,"%s",in.var[in.count]);
fprintf(fd,"  %s",in.var[in.count]);
in.count++;
}

if(strcmp(temp,"output")==0) 
{
fprintf(fd,"\noutput");
fscanf(fs,"%s",out.var[out.count]);
fprintf(fd,"  %s",out.var[out.count]);
out.count++;
}
}

void check_assign(void)
{
fscanf(fs,"%s",assign.as[assign.count]);
strcpy(assign1,assign.as[assign.count]);

/*if(((assign1[3]=='~')&&(assign1[6]=='~'))&&((assign1[4]=='&')||(assign1[4]=='|'))&&(assign1[7]=='&')||(assign1[7]=='|')))
{
check_assign_3();
}*/

if(assign1[3]=='~'&& assign1[4]=='^')
{
fprintf(fd,"\nXNOR2 XNOR2_%d",xnor.count);
fprintf(fd,"(%c,%c,%c);",assign1[0],assign1[2],assign1[5]);
xnor.count++;
}
else if(assign1[3]=='~'&& assign1[4]=='|')
{
fprintf(fd,"\nNR2 NR2_%d",nor.count);
fprintf(fd,"(%c,%c,%c);",assign1[0],assign1[2],assign1[5]);
nor.count++;
}
else if(assign1[3]=='~'&& assign1[4]=='&')
{
fprintf(fd,"\nND2 ND2_%d",nand.count);
fprintf(fd,"(%c,%c,%c);",assign1[0],assign1[2],assign1[5]);
nand.count++;
}
else if(assign1[2]=='~')
{
fprintf(fd,"\nINRBH INRBH_%d",not.count);
fprintf(fd,"(%c,%c);",assign1[0],assign1[3]);
not.count++;
}
else if(assign1[3]=='&')
{
fprintf(fd,"\nAND2 AND_%d ",and.count);
fprintf(fd,"(%c,%c,%c);",assign1[0],assign1[2],assign1[4]);
and.count++;
}
else if(assign1[3]=='|')
{
fprintf(fd,"\nOR2 OR_%d ",or.count);
fprintf(fd,"(%c,%c,%c);",assign1[0],assign1[2],assign1[4]);
or.count++;
}
else if(assign1[3]=='^')
{
fprintf(fd,"XOR2 XOR_%d ",xor.count);
fprintf(fd,"(%c,%c,%c);",assign1[0],assign1[2],assign1[4]);
xor.count++;
}
assign.count++;
}

void initialise(void)
{
and.count=1;
or.count=1;
xor.count=1;
not.count=1;
nand.count=1;
xnor.count=1;
nand3.count=1;
nor3.count=1;
assign.count=1;
}

void check_assign_3(void)
{
if((assign1[3]=='~'&& assign1[4]=='|')&& (assign1[6]=='~'&& assign1[7]=='|'))
{
fprintf(fd,"\nNR3 NR3_%d",nor3.count);
fprintf(fd,"(%c,%c,%c,%c);",assign1[0],assign1[2],assign1[5],assign1[8]);
nor3.count++;
}
else if((assign1[3]=='~'&&assign1[4]=='&')&& (assign1[6]=='~'&& assign1[7]=='&'))
{
fprintf(fd,"\nND3 ND3_%d",nand3.count);
fprintf(fd,"(%c,%c,%c,%c);",assign1[0],assign1[2],assign1[5],assign1[8]);
nand3.count++;
}

}
