First and follow

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • morshruti
    New Member
    • Sep 2008
    • 1

    First and follow

    hey i am a student and i want a C program to compute the first and follow of any given productions.Plz zz rply soon......
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Please read the posting guidelines.

    I can help with specific programming and design issues but I cannot provide complete solutions or act as a tutor.

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      Since you're repeating your previous question, I repeat my question to you:
      do you know how to compute the first and follow sets given a grammar?

      kind regards,

      Jos

      Comment

      • curiously enough
        New Member
        • Aug 2008
        • 79

        #4
        "i want a C program to compute the first and follow of any given productions."
        say what?

        Comment

        • donbock
          Recognized Expert Top Contributor
          • Mar 2008
          • 2427

          #5
          Originally posted by curiously enough
          "i want a C program to compute the first and follow of any given productions."
          say what?
          Finding the first and follow sets of a given grammar are things you would have to do if you were writing your own lex or flex.

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            Originally posted by donbock
            Finding the first and follow sets of a given grammar are things you would have to do if you were writing your own lex or flex.
            Make that 'yacc' or 'bison' etc. Lex and flex use a simple NFA ---> DFA construct
            (Non - Deterministic Finite Automaton) to build their lexical analyzer tables.

            kind regards,

            Jos

            Comment

            • nirjhor003
              New Member
              • Jul 2016
              • 1

              #7
              #include<stdio. h>
              #include<ctype. h>
              char a[8][8];
              int n=5 ;
              struct firTab
              {
              int n;
              char firT[5];
              };
              struct folTab
              {
              int n;
              char folT[5];
              };
              struct folTab follow[5];
              struct firTab first[5];
              int col;
              void findFirst(char, char);
              void findFollow(char ,char);
              void folTabOperation (char,char);
              void firTabOperation (char,char);
              void main()
              {
              int i,j,c=0,cnt=0;
              char ip;
              char b[8];
              printf("\n\t\t= =====FIRST AND FOLLOW SET====== \n\nEnter Production Amount : ");
              scanf("%d",&n);
              printf("\n\tent er %d productions in format E->T+G\n",n);
              printf("\t------------------------------------\n");
              for(i=0; i<n; i++)
              {
              scanf("%s",&a[i]);
              }
              for(i=0; i<n; i++)
              {
              c=0;
              for(j=0; j<i+1; j++)
              {
              if(a[i][0] == b[j])
              {
              c=1;
              break;
              }
              }
              if(c !=1)
              {
              b[cnt] = a[i][0];
              cnt++;
              }

              }
              printf("\n");

              for(i=0; i<cnt; i++)
              {
              col=1;
              first[i].firT[0] = b[i];
              first[i].n=0;
              findFirst(b[i],i);
              }
              for(i=0; i<cnt; i++)
              {
              col=1;
              follow[i].folT[0] = b[i];
              follow[i].n=0;
              findFollow(b[i],i);
              }

              printf("\n");
              for(i=0; i<cnt; i++)
              {
              for(j=0; j<=first[i].n; j++)
              {
              if(j==0)
              {
              printf("First(% c) : {",first[i].firT[j]);
              }
              else
              {
              printf(" %c",first[i].firT[j]);
              }
              }
              printf(" } ");
              printf("\n");
              }
              printf("\n");
              for(i=0; i<cnt; i++)
              {
              for(j=0; j<=follow[i].n; j++)
              {
              if(j==0)
              {
              printf("Follow( %c) : {",follow[i].folT[j]);
              }
              else
              {
              printf(" %c",follow[i].folT[j]);
              }
              }
              printf(" } ");

              printf("\n");
              }

              }
              void findFirst(char ip,char pos)
              {
              int i;
              for(i=0; i<n; i++)
              {
              if(ip == a[i][0])
              {
              if(isupper(a[i][3]))
              {
              findFirst(a[i][3],pos);
              }
              else
              {

              first[pos].firT[col]=a[i][3];
              first[pos].n++;
              col++;
              }
              }
              }
              }
              void findFollow(char ip,char row)
              {
              int i,j;
              if(row==0 && col==1)
              {
              follow[row].folT[col]= '$';
              col++;
              follow[row].n++;
              }
              for(i=0; i<n; i++)
              {
              for(j=3; j<7; j++)
              {
              if(a[i][j] == ip)
              {
              if(a[i][j+1] == '\0')
              {
              if(a[i][j] != a[i][0])
              {
              folTabOperation (a[i][0],row);
              }
              }
              else if(isupper(a[i][j+1]))
              {
              if(a[i][j+1] != a[i][0])
              {
              firTabOperation (a[i][j+1],row);

              }
              }
              else
              {
              follow[row].folT[col] = a[i][j+1];
              col++;
              follow[row].n++;


              }
              }
              }
              }
              }
              void folTabOperation (char ip,char row)
              {
              int i,j;
              for(i=0; i<5; i++)
              {
              if(ip == follow[i].folT[0])
              {
              for(j=1; j<=follow[i].n; j++)
              {
              follow[row].folT[col] = follow[i].folT[j];
              col++;
              follow[row].n++;
              }
              }
              }
              }
              void firTabOperation (char ip,char row)
              {
              int i,j;
              for(i=0; i<5; i++)
              {
              if(ip == first[i].firT[0])
              {
              for(j=1; j<=first[i].n; j++)
              {
              if(first[i].firT[j] != '0')
              {
              follow[row].folT[col] = first[i].firT[j];
              follow[row].n++;
              col++;
              }
              else
              {
              folTabOperation (ip,row);
              }
              }
              }
              }

              }

              Comment

              Working...