Showing posts with label first and follow. Show all posts
Showing posts with label first and follow. Show all posts

Friday, May 24, 2013

program to find first and follow position for particular grammar



Aim: Write down program to find first and follow position for particular grammar.
#include<stdio.h>
#include<conio.h>

void S1();
void A1();
void E1();
void S();
void A();
void E();

int i,j,k,go;
char first[5],pro,follow[5];

void main()
{
      clrscr();
      i=0;
      j=0;

      printf("\n\n=================First & Follow==================\n\n");
      printf("S -> iEtSA | a\n");
      printf("A -> eS | ^\n");
      printf("E -> b");

      printf("\n\nEnter the alphabet :--> ");
      scanf("%c",&pro);
      printf("\n\n press [1] to find FIRST set \n\n press [0] to find FOLLOW set \n\n");
      scanf("%d",&go);

      if(go==1)
      {
            if(pro=='S')
            {
                  S1();
            }
            else if(pro=='A')
            {
                  A1();
            }
            else if(pro=='E')
            {
                  E1();
            }
            else if(pro=='a' || pro=='e' || pro=='b')
            {
                  first[i]=pro;
            }
            else if(pro=='\n')
            {
                  first[i]='^';
            }
            else
            {
                  printf("\nCannot find the first set of given input");
                  goto end;
            }
            printf("\n\nFIRST[ %c ] = {  %s }",pro,first);
      }
      else
      {
            if(pro=='S')
            {
                  S();
            }
            else if(pro=='A')
            {
                  A();
            }
            else if(pro=='E')
            {
                  E();
            }
            else
            {
                  printf("\nCannot find the follow set of given input");
                  goto end;
            }
            printf("\n\nFOLLOW[ %c ] = { %s }",pro,follow);
      }


end:  getch();
}

void S1()
{
      first[i]='a';
      i++;
      first[i]='i';
}

void A1()
{
      first[i]='e';
      i++;
      first[i]='^';
}

void E1()
{
      first[i]='b';
}

void S()
{
      follow[j]='$';
      j++;
      follow[j]='e';
}

void A()
{
      S();
}

void E()
{
      follow[j]='t';
}
}

output: