/*CSEMATTER.BLOGSPOT.IN
PROGRAM-TO FIND FIRST AND FOLLOW*/
#include<stdio.h>
#include<ctype.h>
#include<string.h>
#include<conio.h>
void follow(char c);
void first_1(char c);
void FIRST(char );
int n,m=0,p,i=0,j=0;
char a[10][10],f[10];
int count,n1=0;
char first[10];
int main()
{
int i,z,choice;
char c,ch;
printf("Enter the no.of productions:");
scanf("%d",&n);
printf("Enter the productions(epsilon=$):\n");
for(i=0;i<n;i++)
scanf("%s%c",a[i],&ch);
count=n;
do
{
n1=0;
printf("Element :");
scanf("%c",&c);
FIRST(c);
printf("\n FIRST(%c)= { ",c);
for(i=0;i<n1;i++)
printf("%c ",first[i]);
printf("}\n");
printf("press 1 to continue : ");
scanf("%d%c",&choice,&ch);
}
while(choice==1);
do
{
m=0;
printf("Enter the element whose FOLLOW is to be found:");
scanf("%c",&c);
follow(c);
printf("FOLLOW(%c) = { ",c);
for(i=0;i<m;i++)
printf("%c ",f[i]);
printf(" }\n");
printf("Do you want to continue(0/1)?");
scanf("%d%c",&z,&ch);
}
while(z==1);
}
void follow(char c)
{
if(a[0][0]==c)
f[m++]='$';
for(i=0;i<n;i++)
{
for(j=2;j<strlen(a[i]);j++)
{
if(a[i][j]==c)
{
if(a[i][j+1]!='\0')
first_1(a[i][j+1]);
if(a[i][j+1]=='\0'&&c!=a[i][0])
follow(a[i][0]);
}
}
}
}
void first_1(char c)
{
int k;
if(!(isupper(c)))
f[m++]=c;
for(k=0;k<n;k++)
{
if(a[k][0]==c)
{
if(a[k][2]=='$')
follow(a[i][0]);
else if(islower(a[k][2]))
f[m++]=a[k][2];
else
first_1(a[k][2]);
}
}
}
void FIRST(char c)
{
int j;
if(!(isupper(c)))first[n1++]=c;
for(j=0;j<count;j++)
{
if(a[j][0]==c)
{
if(a[j][2]=='$') first[n1++]='$';
else if(islower(a[j][2]))first[n1++]=a[j][2];
else FIRST(a[j][2]);
}
}
}
/*OUTPUT
Enter the no.of productions:8
Enter the productions(epsilon=$):
E=TD
D=+TD
D=$
T=FS
S=*FS
S=$
F=(E)
F=a
Element :E
FIRST(E)= { ( a }
press 1 to continue : 1
Element :D
FIRST(D)= { + $ }
press 1 to continue : 1
Element :T
FIRST(T)= { ( a }
press 1 to continue : 1
Element :S
FIRST(S)= { * $ }
press 1 to continue : 1
Element :F
FIRST(F)= { ( a }
press 1 to continue : 0
Enter the element whose FOLLOW is to be found:E
FOLLOW(E) = { $ ) }
Do you want to continue(0/1)?1
Enter the element whose FOLLOW is to be found:D
FOLLOW(D) = { $ ) }
Do you want to continue(0/1)?1
Enter the element whose FOLLOW is to be found:T
FOLLOW(T) = { + $ ) }
Do you want to continue(0/1)?1
Enter the element whose FOLLOW is to be found:S
FOLLOW(S) = { + $ ) }
Do you want to continue(0/1)?1
Enter the element whose FOLLOW is to be found:F
FOLLOW(F) = { * + $ ) }
Do you want to continue(0/1)?0 */
PROGRAM-TO FIND FIRST AND FOLLOW*/
#include<stdio.h>
#include<ctype.h>
#include<string.h>
#include<conio.h>
void follow(char c);
void first_1(char c);
void FIRST(char );
int n,m=0,p,i=0,j=0;
char a[10][10],f[10];
int count,n1=0;
char first[10];
int main()
{
int i,z,choice;
char c,ch;
printf("Enter the no.of productions:");
scanf("%d",&n);
printf("Enter the productions(epsilon=$):\n");
for(i=0;i<n;i++)
scanf("%s%c",a[i],&ch);
count=n;
do
{
n1=0;
printf("Element :");
scanf("%c",&c);
FIRST(c);
printf("\n FIRST(%c)= { ",c);
for(i=0;i<n1;i++)
printf("%c ",first[i]);
printf("}\n");
printf("press 1 to continue : ");
scanf("%d%c",&choice,&ch);
}
while(choice==1);
do
{
m=0;
printf("Enter the element whose FOLLOW is to be found:");
scanf("%c",&c);
follow(c);
printf("FOLLOW(%c) = { ",c);
for(i=0;i<m;i++)
printf("%c ",f[i]);
printf(" }\n");
printf("Do you want to continue(0/1)?");
scanf("%d%c",&z,&ch);
}
while(z==1);
}
void follow(char c)
{
if(a[0][0]==c)
f[m++]='$';
for(i=0;i<n;i++)
{
for(j=2;j<strlen(a[i]);j++)
{
if(a[i][j]==c)
{
if(a[i][j+1]!='\0')
first_1(a[i][j+1]);
if(a[i][j+1]=='\0'&&c!=a[i][0])
follow(a[i][0]);
}
}
}
}
void first_1(char c)
{
int k;
if(!(isupper(c)))
f[m++]=c;
for(k=0;k<n;k++)
{
if(a[k][0]==c)
{
if(a[k][2]=='$')
follow(a[i][0]);
else if(islower(a[k][2]))
f[m++]=a[k][2];
else
first_1(a[k][2]);
}
}
}
void FIRST(char c)
{
int j;
if(!(isupper(c)))first[n1++]=c;
for(j=0;j<count;j++)
{
if(a[j][0]==c)
{
if(a[j][2]=='$') first[n1++]='$';
else if(islower(a[j][2]))first[n1++]=a[j][2];
else FIRST(a[j][2]);
}
}
}
/*OUTPUT
Enter the no.of productions:8
Enter the productions(epsilon=$):
E=TD
D=+TD
D=$
T=FS
S=*FS
S=$
F=(E)
F=a
Element :E
FIRST(E)= { ( a }
press 1 to continue : 1
Element :D
FIRST(D)= { + $ }
press 1 to continue : 1
Element :T
FIRST(T)= { ( a }
press 1 to continue : 1
Element :S
FIRST(S)= { * $ }
press 1 to continue : 1
Element :F
FIRST(F)= { ( a }
press 1 to continue : 0
Enter the element whose FOLLOW is to be found:E
FOLLOW(E) = { $ ) }
Do you want to continue(0/1)?1
Enter the element whose FOLLOW is to be found:D
FOLLOW(D) = { $ ) }
Do you want to continue(0/1)?1
Enter the element whose FOLLOW is to be found:T
FOLLOW(T) = { + $ ) }
Do you want to continue(0/1)?1
Enter the element whose FOLLOW is to be found:S
FOLLOW(S) = { + $ ) }
Do you want to continue(0/1)?1
Enter the element whose FOLLOW is to be found:F
FOLLOW(F) = { * + $ ) }
Do you want to continue(0/1)?0 */
No comments:
Post a Comment