Search This Blog

Wednesday, October 9, 2013

FOLLOW SET OF GIVEN GRAMMAR USING C PROGRAM


#include<stdio.h>
#include<string.h>
int n,m=0,p,i=0,j=0;
char a[10][10],f[10];
void follow(char c);
void first(char c);
int main()
{
 int i,z;
 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);

 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(a[i][j+1]);

    if(a[i][j+1]=='\0'&&c!=a[i][0])
     follow(a[i][0]);

   }
  }
 }
}
void first(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(a[k][2]);
                 }
                 }

}






SAMPLE OUTPUT:

productions:
E=TD
D=+TD
D=$
T=FS
S=*FS
S=$
F=(E)
F=a

FOLLOW(E)=FOLLOW(D)={),$}
FOLLOW(T)=FOLLOW(S)={+,),$}
FOLLOW(F)={+,*,),$}

 




12 comments:

  1. Wht do you do if production is entered in the form S=*FS/$

    ReplyDelete
    Replies
    1. Bcoz it cannot consider two productions at a time
      So we are taking it as two separate productions

      Delete
  2. Thank You Very Much... This was s great help.. (Y)

    ReplyDelete
  3. please calculate follow of this production T=T+A

    ReplyDelete
  4. This is partially wrong.
    It wont give the correct answer for the following input:
    S=ABCD
    A=a
    A=$
    B=b
    B=$
    C=c
    D=d

    ReplyDelete
  5. This is partially wrong.
    It wont give the correct answer for the following input:
    S=ABCD
    A=a
    A=$
    B=b
    B=$
    C=c
    D=d

    ReplyDelete
  6. its giving me the error ...(isupper) was not declare in the scope.& also (islower) was not declare in the scope.?

    ReplyDelete
  7. what will be the solution of this error?

    ReplyDelete
  8. This comment has been removed by the author.

    ReplyDelete