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

EXP 9 CD 0682

The document outlines a C program designed to compute the FIRST set of grammar productions in compiler design. It includes an algorithm for reading input, defining the FIRST function, avoiding duplicates, and computing the FIRST set for user-specified non-terminals. The program has been executed successfully, and the output is verified.

Uploaded by

chandujv06
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

EXP 9 CD 0682

The document outlines a C program designed to compute the FIRST set of grammar productions in compiler design. It includes an algorithm for reading input, defining the FIRST function, avoiding duplicates, and computing the FIRST set for user-specified non-terminals. The program has been executed successfully, and the output is verified.

Uploaded by

chandujv06
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Compiler Design Lab 2024-25

Ex. No.: 9 Date: 27 – 02-2025

FIRST OF PRODUCTION
Aim: Write a C program to find the FIRST() symbols of the productions
Algorithm:

• Read Input:

• Take the number of productions as input.


• Store production rules in an array.

• Define FIRST(X) Function:

• If X is a terminal, return {X} as its FIRST set.


• If X is a non-terminal:
o Initialize an empty set FIRST(X).
o For each production where X → α:
1. If α is ε (epsilon), add ε to FIRST(X).
2. Else, for each symbol Y in α (from left to right):
▪ Compute FIRST(Y).
▪ Add all elements of FIRST(Y) (excluding ε) to FIRST(X).
▪ If FIRST(Y) contains ε, continue to the next symbol in α.
▪ Stop if a symbol does not contain ε.

• Avoid Duplicates:

• Ensure elements are not added multiple times in the FIRST set.

• Compute FIRST Set for User Input:

• Prompt the user to enter a non-terminal to find its FIRST set.


• Call the FIRST(X) function and display the result.

Program for FIRST OF PRODUCTION:


#include<stdio.h>
#include<ctype.h>
void FIRST(char[],char );
void addToResultSet(char[],char);
int numOfProductions;
char productionSet[10][10];
main()
{

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

void FIRST(char* Result,char c)


{
int i,j,k;
char subResult[20];
int foundEpsilon;
subResult[0]='\0';
Result[0]='\0';
if(!(isupper(c)))
{
addToResultSet(Result,c);
return ;
}

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

You might also like