Dfa

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 5

#include<stdio.

h>
#include<string.h>
#include<stdlib.h>
struct tree
{
char data;
int firstpos[10];
int lastpos[10];
int nodenum;
int flen;
int llen;
int nullable;
};
int main()
{
char input[20],output[20],stack[20];
int num[20]={0};
int count=1;
int i,j,a,ilen=0,olen=0,top=-1,state=0,k=0,initial,next,end,b4end;
for(i=0;i<20;i++)
{
input[i] ='\0';
output[i]='\0';
stack[i] ='\0';
}
printf("\n\nEnter the Regular Expression: ");
scanf("%[^\n]",input);
ilen = strlen(input);
for(i=0;i<ilen;i++)
{
if((input[i]!='|')&&(input[i]!='*')&&(input[i]!='.')&&(input[i]!
='(')&&(input[i]!=')'))
{
output[olen] = input[i];
olen++;
}
else
{
if(top==-1)
{
top++;
stack[top] = input[i];
}
else
{
if(input[i]=='(')
{
top++;
stack[top] = input[i];
}
else if(input[i]==')')
{
while(stack[top]!='(')
{
output[olen] = stack[top];
olen++;
stack[top]='\0';
top--;

}
stack[top]='\0';
top--;
}
else
{
if((stack[top]!='(')&&(stack[top]!='|')&
&(stack[top]!='.'))
{
output[olen] = stack[top];
olen++;
stack[top]=input[i];
}
else if((stack[top]=='|')||(stack[top]==
'.'))
{
if(input[i]=='*')
{
top++;
stack[top] = input[i];
}
else
{
output[olen] = stack[top
];
olen++;
stack[top]=input[i];
}
}
else
{
top++;
stack[top] = input[i];
}
}
}
}
}//end of for...
while(top!=-1)
{
output[olen] = stack[top];
top--;
olen++;
}
output[olen]='#';
olen++;
output[olen]='.';
olen++;
printf("\n%s\n\n",output);
struct tree dfa[20];
for(i=0;i<olen;i++)
{
if(((output[i]>=97)&&(output[i]<=123))||(output[i]=='#'))
{
dfa[i].nodenum = count++;
dfa[i].data = output[i];
}
else
{

dfa[i].nodenum = 0;
dfa[i].data = output[i];
}
dfa[i].nullable=0;
dfa[i].flen=1;
dfa[i].llen=1;
/*for(j=0;j<20;j++)
{
dfa[i].firstpos[j] = 0;
dfa[i].lastpos[j] = 0;
}*/
}
for(i=0;i<olen;i++)
printf("%d,",dfa[i].nodenum);
printf("\n\n");
j=0;
for(i=0;i<olen;i++)
{
if(output[i]=='|')
{
if((dfa[i-1].nullable==1)||(dfa[i-2].nullable==1))
dfa[i].nullable=1;
for(k=0;k<dfa[i-2].flen-1;k++)
{
dfa[i].firstpos[j] = dfa[i-2].firstpos[k];
j++;
dfa[i].flen++;
}
for(k=0;k<dfa[i-1].flen-1;k++)
{
dfa[i].firstpos[j] = dfa[i-1].firstpos[k];
j++;
dfa[i].llen++;
}
j=0;
for(k=0;k<dfa[i-2].llen-1;k++)
{
dfa[i].lastpos[j] = dfa[i-2].lastpos[k];
j++;
dfa[i].flen++;
}
for(k=0;k<dfa[i-1].llen-1;k++)
{
dfa[i].lastpos[j] = dfa[i-1].lastpos[k];
j++;
dfa[i].llen++;
}
j=0;
}
else if(output[i]=='*')
{
dfa[i].nullable=1;
for(j=0;j<dfa[i-1].flen-1;j++)
{
dfa[i].firstpos[j] = dfa[i-1].firstpos[j];
}
for(j=0;j<dfa[i].llen-1;j++)
{
dfa[i].lastpos[j] = dfa[i-1].lastpos[j];

}
}
else if(output[i]=='.')
{
if((dfa[i-1].nullable==1)&&(dfa[i-2].nullable==1))
dfa[i].nullable=1;
if(dfa[i-2].nullable==1)
{
j=0;
for(k=0;k<dfa[i-2].flen-1;k++)
{
dfa[i].firstpos[j] = dfa[i-2].firstpos[k
];
j++;
dfa[i].flen++;
}
for(k=0;k<dfa[i-1].flen-1;k++)
{
dfa[i].firstpos[j] = dfa[i-1].firstpos[k
];
j++;
dfa[i].flen++;
}
}
else
{
j=0;
for(k=0;k<dfa[i-2].flen-1;k++)
{
printf("hi");
dfa[i].firstpos[j] = dfa[i-2].firstpos[k
];
j++;
dfa[i].flen++;
}
}
if(dfa[i-1].nullable==1)
{
j=0;
for(k=0;k<dfa[i-2].llen-1;k++)
{
dfa[i].lastpos[j] = dfa[i-2].lastpos[k];
j++;
dfa[i].llen++;
}
for(k=0;k<dfa[i-1].llen-1;k++)
{
dfa[i].lastpos[j] = dfa[i-1].lastpos[k];
j++;
dfa[i].llen++;
}
}
else
{
j=0;
for(k=0;k<dfa[i-1].llen-1;k++)
{
printf("heloo");
dfa[i].lastpos[j] = dfa[i-1].lastpos[k];
j++;

dfa[i].llen++;
}
}
}
else
{
dfa[i].flen++;
dfa[i].llen++;;
dfa[i].firstpos[0] = dfa[i].nodenum;
dfa[i].lastpos[0] = dfa[i].nodenum;
}
}
for(i=0;i<olen;i++)
{
printf("\n\n\t\tData: %c",dfa[i].data);
if((dfa[i].data=='|')||(dfa[i].data=='.')||(dfa[i].data=='*'))
{
printf("\nFirst Position: ");
for(j=0;j<dfa[i].flen-1;j++)
{
printf("%d,",dfa[i].firstpos[j]);
}
printf("\nLast Position: ");
for(j=0;j<dfa[i].llen-1;j++)
{
printf("%d,",dfa[i].lastpos[j]);
}
}
else
{
printf("\nFirst Position: %d",dfa[i].firstpos[0]);
printf("\nLast Position: %d",dfa[i].lastpos[0]);
}
}
int followpos[20][5];
printf("\n");
}

You might also like