6.26.2010

FIRST AND FOLLOW

#include
#include
#include
#include
char p[50][20][20];
char first[30],follow[30],nonter[30],ter[30];
int v=0,s;
void foundfirst(char non)
{

char *ptr;
int a;
ptr=strchr(nonter,non);
if(ptr!=NULL)
a=ptr-nonter;
if(ptr!=NULL)
{
for(int i=0;i<10;i++)
{
if(p[a][i][0]!='\0')
{
non=p[a][i][0];
foundfirst(non);
}
}
}
else
{
first[v++]=non;
first[v]='\0';
return;
}
}
void findfollow(char non[])
{
char *ptr=strchr(nonter,non[0]);
if(ptr!=NULL)
{
for(int i=0;i<=10;i++)
{

for(int j=0;j<10;j++)
{
char *b=strstr(p[i][j],non);
if(b!=NULL)
{
v=0;
char f[30];
strcpy(f,b);
if(strcmp(b+1,"\0")==0)
f[1]='e';
foundfirst(f[1]);

strcat(follow,first);
s=strlen(follow);
if(strstr(first,"e")!=NULL)
{
v=0;
if(nonter[i]!=non[0])
{
non[0]=nonter[i];
non[1]='\0';
findfollow(non);
}
}
}
}
}
}
else
{
follow[s++]=non[0];
follow[s]='\0';
return;
}
return;
}
void main()
{
clrscr();
char start;
fstream fp;
fp.open("grammer.txt",ios::in);
char sym,temp[30];
int a1,i=0,j=0;
fp>>ter;
fp>>nonter;
fp>>temp;
do
{
j=0;
while(strcmp(temp,"$")!=0)
{
strcpy(p[i][j],temp);
fp>>temp;
j++;
}
i++;
fp>>temp;
}while(fp.eof()!=1);
fp.close();

cout<<"\nEnter symbol: ";
cin>>sym;
foundfirst(sym);
cout<<"\nFirst: {" ;
for(i=0;first[i]!='\0';i++)
cout<cout<<"}";
char nonterm1[30];
cout<<"\nEnter symbol: ";
cin>>nonterm1;
findfollow(nonterm1);
cout<<"\nFollow: {" ;
for(i=0;follow[i]!='\0';i++)
{
if(follow[i]!='e')
cout<}
if(nonterm1[0]==nonter[0])
cout<<"$";
cout<<"}";
getch();
return;
}

No comments: