First
First
h>
//#include<conio.h>
#include<string.h>
#include<ctype.h>
void first(int ,int );
void format();
int n,i,m;
char a[10][10],ans[10][10];
//-----------Main program-----------
int main()
{
int j;
//clrscr();
printf("Enter no of productions(epsilon (@)):");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("Enter the production no %d:",i);
scanf("%s",a[i]);
}
//---calling the recursive function for each terminal(first)--------
for(i=1;i<=n;i++)
{
m=0;
first(i,0);
}
//----displaying the productions-------------
//clrscr();
printf("The productions are:");
for(i=1;i<=n;i++)
printf("\n%s",a[i]);
printf("\n-------------------------------");
//---------formatting the answer---------
format();
//--------displaying the result--------------
for(i=1;i<=n;i++)
if(a[i][0]!=' ')
{
printf("\nfirst(%c)",a[i][0]);
for(j=0;j<=strlen(a[i]);j++)
if(ans[i][j]!=' ')
printf("%4c",ans[i][j]);
}
//getch();
}
// ---------End of main program--------------
// Recursive function first()------------
void first(int x,int y)
{
int j,l=3;
for(j=1;j<=n;j++)
{
if(a[j][0]==a[x][y])
{
if(!isupper(a[j][l]) && a[j][l]!='@')
{
ans[i][m++]=a[j][l];
}
else if (a[j][l]=='@')
{
if(a[x][y+1]!='\0' && y!=0)
{
if(isupper(a[x][y+1]))
first(x,y+1);
else
ans[i][m++]=a[x][y+1];
}
else ans[i][m++]='@';
} //inner if
else first(j,l);
}//outer if
}//for
}
//-----------------end of function first()---------
// Formatting the answer
void format()
{
int j,l,k;
for(i=1;i<=n;i++)
for(j=i+1;j<=n;j++)
if(a[i][0]==a[j][0])
a[j][0]=' ';
for(i=1;i<=n;i++)
for(j=1;j<=strlen(ans[i]);j++)
for(k=j+1;k<=strlen(ans[i]);k++)
if(ans[i][j]==ans[i][k])
ans[i][k]=' ';
}