EXP 9 CD 0682
EXP 9 CD 0682
FIRST OF PRODUCTION
Aim: Write a C program to find the FIRST() symbols of the productions
Algorithm:
• Read Input:
• Avoid Duplicates:
• Ensure elements are not added multiple times in the FIRST set.
JANAPATI VENKATA SAI CHANDRA KOUSTHUBH– BU22CSEN0100682 Dept. of CSE, GITAM BLR
Compiler Design Lab 2024-25
int i;
char choice;
char c;
char result[20];
printf("JANAPATI VENKATA SAI CHANDRA KOUSTHUBH\n");
printf("BU22CSEN0100682");
printf("How many number of productions ? :");
scanf(" %d",&numOfProductions);
for(i=0;i<numOfProductions;i++)
{
printf("Enter productions Number %d : ",i+1);
scanf(" %s",productionSet[i]);
}
do
{
printf("\n Find the FIRST of :");
scanf(" %c",&c);
FIRST(result,c);
printf("\n FIRST(%c)= { ",c);
for(i=0;result[i]!='\0';i++)
printf(" %c ",result[i]);
printf("}\n");
printf("press 'y' to continue : ");
scanf(" %c",&choice);
}
while(choice=='y'||choice =='Y');
}
for(i=0;i<numOfProductions;i++)
{
JANAPATI VENKATA SAI CHANDRA KOUSTHUBH– BU22CSEN0100682 Dept. of CSE, GITAM BLR
Compiler Design Lab 2024-25
if(productionSet[i][0]==c)
{
if(productionSet[i][2]=='$') addToResultSet(Result,'$');
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;
}
if(!foundEpsilon)
break;
j++;
}
}
}
}
return ;
}
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';
}
JANAPATI VENKATA SAI CHANDRA KOUSTHUBH– BU22CSEN0100682 Dept. of CSE, GITAM BLR
Compiler Design Lab 2024-25
Result:
Program executed successfully and verified output.
Output:
JANAPATI VENKATA SAI CHANDRA KOUSTHUBH– BU22CSEN0100682 Dept. of CSE, GITAM BLR