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 :

Rahul said... Best Blogger Tips [Reply to comment] Best Blogger Templates

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

v!p!n said... Best Blogger Tips [Reply to comment] Best Blogger Templates

@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)= { * , ε }

Anonymous said... Best Blogger Tips [Reply to comment] Best Blogger Templates

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]);
}

Anonymous said... Best Blogger Tips [Reply to comment] Best Blogger Templates

explain the code?

Unknown said... Best Blogger Tips [Reply to comment] Best Blogger Templates

thank u its very easy

Rashmi said... Best Blogger Tips [Reply to comment] Best Blogger Templates

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

Anonymous said... Best Blogger Tips [Reply to comment] Best Blogger Templates

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

Anonymous said... Best Blogger Tips [Reply to comment] Best Blogger Templates

@Anonymous
enter zero for termination

Unknown said... Best Blogger Tips [Reply to comment] Best Blogger Templates

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

Anonymous said... Best Blogger Tips [Reply to comment] Best Blogger Templates

can u plz explain the code

Unknown said... Best Blogger Tips [Reply to comment] Best Blogger Templates

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

Anonymous said... Best Blogger Tips [Reply to comment] Best Blogger Templates

This one is good.

Anonymous said... Best Blogger Tips [Reply to comment] Best Blogger Templates

esrtsertaedrg

v!p!n said... Best Blogger Tips [Reply to comment] Best Blogger Templates

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

v!p!n said... Best Blogger Tips [Reply to comment] Best Blogger Templates

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

v!p!n said... Best Blogger Tips [Reply to comment] Best Blogger Templates

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

v!p!n said... Best Blogger Tips [Reply to comment] Best Blogger Templates

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

v!p!n said... Best Blogger Tips [Reply to comment] Best Blogger Templates

@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

Unknown said... Best Blogger Tips [Reply to comment] Best Blogger Templates

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

v!p!n said... Best Blogger Tips [Reply to comment] Best Blogger Templates

hello naga pavan,

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

Hope it will help you.!

Unknown said... Best Blogger Tips [Reply to comment] Best Blogger Templates

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

Anonymous said... Best Blogger Tips [Reply to comment] Best Blogger Templates

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

Unknown said... Best Blogger Tips [Reply to comment] Best Blogger Templates
This comment has been removed by the author.
Unknown said... Best Blogger Tips [Reply to comment] Best Blogger Templates

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

v!p!n said... Best Blogger Tips [Reply to comment] Best Blogger Templates

You are welcome, Yogesh Pawar

Anonymous said... Best Blogger Tips [Reply to comment] Best Blogger Templates

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

Unknown said... Best Blogger Tips [Reply to comment] Best Blogger Templates

Thanks a lot :)

Anonymous said... Best Blogger Tips [Reply to comment] Best Blogger Templates

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|^

Unknown said... Best Blogger Tips [Reply to comment] Best Blogger Templates

No ε found, no need to check next element

i want to know why?

Unknown said... Best Blogger Tips [Reply to comment] Best Blogger Templates

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

v!p!n said... Best Blogger Tips [Reply to comment] Best Blogger Templates

@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?

v!p!n said... Best Blogger Tips [Reply to comment] Best Blogger Templates

You are welcome @Unknown

Unknown said... Best Blogger Tips [Reply to comment] Best Blogger Templates
This comment has been removed by the author.
Unknown said... Best Blogger Tips [Reply to comment] Best Blogger Templates

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

Nithin said... Best Blogger Tips [Reply to comment] Best Blogger Templates

can u explain it pls..

Nithin said... Best Blogger Tips [Reply to comment] Best Blogger Templates

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

JASZ said... Best Blogger Tips [Reply to comment] Best Blogger Templates

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

JASZ said... Best Blogger Tips [Reply to comment] Best Blogger Templates

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

Anonymous said... Best Blogger Tips [Reply to comment] Best Blogger Templates
This comment has been removed by a blog administrator.
Anonymous said... Best Blogger Tips [Reply to comment] Best Blogger Templates
This comment has been removed by a blog administrator.
ROHIT G said... Best Blogger Tips [Reply to comment] Best Blogger Templates

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

Unknown said... Best Blogger Tips [Reply to comment] Best Blogger Templates

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

Unknown said... Best Blogger Tips [Reply to comment] Best Blogger Templates

Is it the code for left recursive grammar ?

vijay said... Best Blogger Tips [Reply to comment] Best Blogger Templates

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.

LearnAlgorithm said... Best Blogger Tips [Reply to comment] Best Blogger Templates

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

Adesh said... Best Blogger Tips [Reply to comment] Best Blogger Templates

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

Post a Comment