Calculating the first of a given grammar (without epsilon)
#include<stdio.h>
#include<ctype.h>
int i;
int n;
chat p[25][25];
char c;
char b;
char h;
void first(char c);
void result(char h);
main()
{
printf("Enter the number of production rules\n");
scanf("%d",&c);
for(i=0;i<n;i++)
{
printf("Rule %d: ",i+1);
scanf("%s",p[i]);
}
printf("Enter the non terminal for which first should be calculated\n");
scanf("%s",&c);
first(c);
}
void first(char c)
{
b=c;
for(i=0;i<n;i++)
{
if(p[i][0]==b)
{
if(p[i][2]>64 && p[i][2]<97)
{
first(p[i][2]);
}
if(p[i][2]>96 && p[i][2]<122)
{
result(p[i][2]);
}
}
}
}
void result(char c)
{
h=b;
printf("The first of the non terminal %s is : %s",c,p[i][2]);
}
#include<ctype.h>
int i;
int n;
chat p[25][25];
char c;
char b;
char h;
void first(char c);
void result(char h);
main()
{
printf("Enter the number of production rules\n");
scanf("%d",&c);
for(i=0;i<n;i++)
{
printf("Rule %d: ",i+1);
scanf("%s",p[i]);
}
printf("Enter the non terminal for which first should be calculated\n");
scanf("%s",&c);
first(c);
}
void first(char c)
{
b=c;
for(i=0;i<n;i++)
{
if(p[i][0]==b)
{
if(p[i][2]>64 && p[i][2]<97)
{
first(p[i][2]);
}
if(p[i][2]>96 && p[i][2]<122)
{
result(p[i][2]);
}
}
}
}
void result(char c)
{
h=b;
printf("The first of the non terminal %s is : %s",c,p[i][2]);
}
Comments
Post a Comment