0% found this document useful (0 votes)
3 views

First

The document is a C program that calculates the FIRST set of grammar productions in compiler design. It prompts the user to input the number of productions and their respective rules, then processes each production to determine the FIRST set using a recursive function. The results are formatted and displayed, showing the unique elements of the FIRST sets for each production.

Uploaded by

Ridya Gupta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

First

The document is a C program that calculates the FIRST set of grammar productions in compiler design. It prompts the user to input the number of productions and their respective rules, then processes each production to determine the FIRST set using a recursive function. The results are formatted and displayed, showing the unique elements of the FIRST sets for each production.

Uploaded by

Ridya Gupta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

#include<stdio.

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]=' ';
}

You might also like