0% found this document useful (0 votes)
310 views

Experiment No. 2 Aim:-Write C Program To Implement First of A Given Grammar

The document describes a C program to implement the FIRST set of a given grammar. It contains 3 rules to compute the FIRST set: 1) If x is a terminal, FIRST(x) = {x}. 2) If x-> ε is a production, add ε to FIRST(x). 3) If X->Y1 Y2...Yn, FIRST(X) = FIRST(Y1) unless it contains ε, then FIRST(X) = {FIRST(Y1)- ε} U {FIRST(Y2)}. The program takes grammar productions as input, calls the FIRST function to compute the FIRST set element for a given non-terminal, and displays the result.

Uploaded by

Lion
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
310 views

Experiment No. 2 Aim:-Write C Program To Implement First of A Given Grammar

The document describes a C program to implement the FIRST set of a given grammar. It contains 3 rules to compute the FIRST set: 1) If x is a terminal, FIRST(x) = {x}. 2) If x-> ε is a production, add ε to FIRST(x). 3) If X->Y1 Y2...Yn, FIRST(X) = FIRST(Y1) unless it contains ε, then FIRST(X) = {FIRST(Y1)- ε} U {FIRST(Y2)}. The program takes grammar productions as input, calls the FIRST function to compute the FIRST set element for a given non-terminal, and displays the result.

Uploaded by

Lion
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

EXPERIMENT NO.

2
Aim:- Write c program to implement first of a given grammar

Rules to compute FIRST set:


1. If x is a terminal, then FIRST(x) = { ‘x’ }
2. If x-> Є, is a production rule, then add Є to FIRST(x).
3. If X->Y1 Y2 Y3….Yn is a production,
1. FIRST(X) = FIRST(Y1), If FIRST(Y1) not contain Є.
2. If FIRST(Y1) contains Є then FIRST(X) = { FIRST(Y1) – Є } U { FIRST(Y2) }
3. If FIRST (Yi) contains Є for all i = 1 to n, then add Є to FIRST(X).

PROGRAM

#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

You might also like