Pages

Saturday, October 8

FIRST of a Given Grammar using C program


Hello Friend,

In this post we will discuss how to find FIRST of a grammar using C.


The rules for finding FIRST of a given grammar is:
  1. If X is terminal, FIRST(X) = {X}.
  2. If X → ε is a production, then add ε to FIRST(X).
  3. If X is a non-terminal, and X → Y1 Y2 … Yk is a production, and ε is in all of FIRST(Y1), …, FIRST(Yk), then add ε to FIRST(X).
  4. If X is a non-terminal, and X → Y1 Y2 … Yk is a production, then add a to FIRST(X) if for some i, a is in FIRST(Yi), and ε is in all of FIRST(Y1), …, FIRST(Yi-1).




  1. Each Non terminal character is represented by one Uppercase letter.
  2. Each Terminal character is represented by one lowercase letter.
  3. LHS and RHS of each production will be separated by a "=" symbol.
  4. There will be no Blank space within a production string. 
  5. Assumed that Left recursion is avoided in all input productions. 



/* Author : Vipin
* mail:   vipinnarayantvm@gmail.com
 *Modified on : 23/03/2105
 */

#include<stdio.h>
#include<ctype.h>

void FIRST(char[],char );
void addToResultSet(char[],char);
int numOfProductions;
char productionSet[10][10];

main()
{
    int i;
    char choice; 
    char c;
    char result[20];
    printf("How many number of productions ? :");
    scanf(" %d",&numOfProductions);

    for(i=0;i<numOfProductions;i++)//read production string eg: E=E+T
    {
        printf("Enter productions Number %d : ",i+1);
        scanf(" %s",productionSet[i]);
    }
    do
    {

        printf("\n Find the FIRST of  :");
        scanf(" %c",&c);
        FIRST(result,c); //Compute FIRST; Get Answer in 'result' array
        printf("\n FIRST(%c)= { ",c);
        for(i=0;result[i]!='\0';i++)
        printf(" %c ",result[i]);       //Display result
        printf("}\n");

         printf("press 'y' to continue : ");
        scanf(" %c",&choice);
    }
    while(choice=='y'||choice =='Y');


}
/*
 *Function FIRST:
 *Compute the elements in FIRST(c) and write them
 *in Result Array.
 */
void FIRST(char* Result,char c)
{
    int i,j,k;
    char subResult[20];
    int foundEpsilon;
    subResult[0]='\0';
    Result[0]='\0';

    //If X is terminal, FIRST(X) = {X}.
    if(!(isupper(c)))
    {

        addToResultSet(Result,c);
               return ;
    }
    //If X is non terminal

    //Read each production
    for(i=0;i<numOfProductions;i++)
    {

//Find production with X as LHS
        if(productionSet[i][0]==c)
        {
//If X  ε is a production, then add ε to FIRST(X).
 if(productionSet[i][2]=='$') addToResultSet(Result,'$');

            //If X is a non-terminal, and X  Y1 Y2  Yk
            //is a production, then add a to FIRST(X)
            //if for some i, a is in FIRST(Yi),
            //and ε is in all of FIRST(Y1), …, FIRST(Yi-1).
      else
            {
                j=2;
                while(productionSet[i][j]!='\0')
                {
                foundEpsilon=0;
                FIRST(subResult,productionSet[i][j]);
                for(k=0;subResult[k]!='\0';k++)
                    addToResultSet(Result,subResult[k]);
                 for(k=0;subResult[k]!='\0';k++)
                     if(subResult[k]=='$')
                     {
                         foundEpsilon=1;
                         break;
                     }
                 //No ε found, no need to check next element
                 if(!foundEpsilon)
                     break;
                 j++;

                }
            }
    }
}

    return ;
}

/* addToResultSet adds the computed
 *element to result set. 
 *This code avoids multiple inclusion of elements
  */
void addToResultSet(char Result[],char val)
{
    int k;

    for(k=0 ;Result[k]!='\0';k++)
        if(Result[k]==val)
            return;
    Result[k]=val;
    Result[k+1]='\0';


}



Output







46 comments:

  1. can u pls help me to execute this program with one input output example.

    ReplyDelete
  2. @rahul

    Ok... Look at this sample execution..

    Input:
    How many productions? 8

    Enter 8 productions:

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


    output
    FIRST (E) = FIRST (T) =FIRST (F) = { ( , a}
    FIRST (D) = { + , ε }
    FIRST (S)= { * , ε }

    ReplyDelete
  3. what does this code do pls explain line wise


    void FIRST(char c)
    {
    int j;
    if(!(isupper(c)))first[n++]=c;
    for(j=0;j<count;j++)
    {
    if(prodn[j][0]==c)
    {
    if(prodn[j][2]=='$') first[n++]='$';
    else if(islower(prodn[j][2]))first[n++]=prodn[j][2];
    else FIRST(prodn[j][2]);
    }

    ReplyDelete
  4. explain the code?

    ReplyDelete
  5. hey can you tell me how to implement follow for any grammer in c...plz help for that...i need for practical exam

    ReplyDelete
  6. HW TO TERMINATE DIZ........??its jus asking..press 1 to continue...no termination specified..:(

    ReplyDelete
  7. @Anonymous
    enter zero for termination

    ReplyDelete
  8. can u please tell me why you have used charachter 'ch' over here

    ReplyDelete
  9. can u plz explain the code

    ReplyDelete
  10. why are we using ' ch ' here ?? when i tried removing it and executing, it is not taking input for element.

    ReplyDelete
  11. This one is good.

    ReplyDelete
  12. Hello @Rashmi,

    You can find the C implementation of FOLLOW of a grammar in this link.. Hope this will be useful for you!

    http://vipinnpillai.blogspot.in/2011/11/follow-of-given-grammar-using-c-program.html

    ReplyDelete
  13. Hello @Anonymous Code explanation has been included in modified code.. Please check it out.!!

    ReplyDelete
  14. Hey @Abhi abhinay , Thanking you for your Nice words..!! :)

    ReplyDelete
  15. hey @Anonymous, The termination method is made more simpler. You have to press Y or y key to continue.. If you press any key other than them, code will be terminated

    ReplyDelete
  16. @PRIYANKA KUMARI Hai... if 'ch' is not included the '\n' value which is read by stdin buffer while we pressing enter key will be assigned to next scanf() statement. This anomaly is called "Dangling newline" character.

    To avoid this problem, scanf should be written like this.
    scanf(" %c",&character)
    not like this:
    scanf("%c",&character);

    The blank space before %c will flush out dangling characters.!!

    I hope this explanation could satisfy you.!!

    Thank you for the feedback..

    PS: dangling character issue has been solved in modified code

    ReplyDelete
  17. may i get first and follow of given grammar in a single program

    ReplyDelete
  18. hello naga pavan,

    CHECK THIS LINK
    to get code to find FOLLOW of a grammar.

    Hope it will help you.!

    ReplyDelete
  19. Hi Vipin,
    Do you have an algorithm if the input productions has left recursions in it.
    Thanks,
    Chandra

    ReplyDelete
  20. Hello @Anonymous thanks for the algorithm.It really helped me a lot.:)

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

    ReplyDelete
  22. Thank you very much for programs(FIST AND FOLLOW).

    ReplyDelete
  23. its not working with global inputs like S->ABC|CAab|q|^

    ReplyDelete
  24. How Can I set an empty set that is epsilon not in the input? And one more question. why it is not working with global inputs like S->ABC|CAab|q|^

    ReplyDelete
  25. No ε found, no need to check next element

    i want to know why?

    ReplyDelete
  26. Its Showing the first value only for selected var. m trying to show all the first value at a time. but whenever m trying this i cant stop the result

    ReplyDelete
  27. @Tapasy Rabeya

    We can print FIRST of all variables at a time. How you are trying to print?
    can you just share the mail snippet?

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

    ReplyDelete
  29. can you please provide a code for first and follow together? Thanks in advance :)

    ReplyDelete
  30. void FIRST(char* Result,char c)
    why is the result is of that type

    ReplyDelete
  31. how to denote null as a production like A-> c | NULL

    ReplyDelete
  32. how to denote null as a production like A-> c | NULL

    ReplyDelete
  33. This comment has been removed by a blog administrator.

    ReplyDelete
  34. This comment has been removed by a blog administrator.

    ReplyDelete
  35. @Anonymous
    google and check the function of isuper(INT C) function u will get it

    ReplyDelete
  36. To this production Rule it is not working
    E:E+T
    T:T*F
    F:(E)
    F:id

    ReplyDelete
  37. Is it the code for left recursive grammar ?

    ReplyDelete
  38. first.c: In function ‘first’:
    first.c:40: error: invalid initializer
    first.c:83: error: expected declaration or statement at end of input

    40:Result[0]='\0';
    83:Result[k+1]='\0';
    }

    it shows errors like this,and i didn't understandit.

    ReplyDelete
  39. 4
    S=AaAb
    S=BbBa
    A=$
    B=$
    It shows wrong output.

    ReplyDelete
  40. It contain an error .
    for the productions
    A=Ba
    B=$
    output for A is { $, a}
    but it must be { a }

    ReplyDelete