Program to find the trailing edge for non terminals present in the grammar production.
Program Code:
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<ctype.h>
main()
{
int n,j,i,len,l;
char pr[10][10],trail[10];
cout<<"Enter the number of production rules you want to enter\n";
cin>>n;
cout<<"Enter the production rules";
for(i=0;i<n;i++)
{
cin>>pr[i];
}
for(i=0;i<n;i++)
{int d=0;
for(j=strlen(pr[i]);j>2;j--)
{
if(islower(pr[i][j]))
{
trail[pr[i][0]]=pr[i][j];
d=1;
break;
}
}
if(d==0)
{ while(pr[i][j]!='\0')
{l=j;j++;}
if(trail[pr[i][j]]!='\0')
{
trail[pr[i][0]]=trail[pr[i][l]]
;
break;
}
}
}for(i=0;i<n;i++)
{
cout<<"trailing["<<pr[i][0]<<"]="<<trail[pr[i][0]]<<endl;
}
getch();
}
Output:
0 comments:
Post a Comment