0% found this document useful (0 votes)
8 views2 pages

First Function

The document contains a C program that computes the FIRST set of a given non-terminal in a context-free grammar. It defines functions to read production rules, process non-terminals, and recursively determine the FIRST set. The program prompts the user for the number of productions and the specific non-terminal to analyze, then outputs the computed FIRST set.

Uploaded by

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

First Function

The document contains a C program that computes the FIRST set of a given non-terminal in a context-free grammar. It defines functions to read production rules, process non-terminals, and recursively determine the FIRST set. The program prompts the user for the number of productions and the specific non-terminal to analyze, then outputs the computed FIRST set.

Uploaded by

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

#include<stdio.

h>
#include<string.h>

int prodCount;
//.....productions array......non-terminal[]............terminal[].............
char prod[30][30];
char nt[10];
char t[10];
//...............................................
char frst[20]; // stores first set
int frc; // count firsts
//..............................................
void first(char c,int hop);
int prevr,prevc; // it will store row n col of previous called first()
int ntc;

//......................FIRST.........................
void first(char c,int hop)
{
int i,j;
char res[20];
int prodIndex = 0;
if(c != '#') // not a null
{
for(i=0;i<prodCount;i++)
{
if(prod[i][0] == c){ //checks if non-terminal symbol
prodIndex = i;
hop = 0;
first(prod[prodIndex][3+hop],hop);
prevr= prodIndex;
prevc= 3 + hop;
}
else // if terminal
{
if(!strchr(frst,c) && !strchr(nt,c)) // if not already in set &
not a Non-Terminal
{
frst[frc] = c;
frc ++;
}
}
}
}
else
{ // if it is null
if(!strchr(frst,c))
{
frst[frc] = c;
frc ++;
}
hop++;
prevc = prevc + hop;
char d = prod[prevr][prevc+3];
first(d , hop);
}
}

//................MAIN..............................
int main()
{
int i,j=0,flag=0;
frc = 0;
printf("Number of productions? ");
scanf("%d",&prodCount);
for(i=0;i<prodCount;i++)
{
printf("Enter prod %d: ",i);
scanf("%s",prod[i]);
nt[ntc] = prod[i][0];
ntc++;
}
char ip[1];
printf("Enter Nonterminal symbol ? ");
scanf("%s",ip);

printf("\n first of %c is : { ",ip[0]);


first(ip[0],0);
for(i=0;i<frc;i++)
{
printf(" %c",frst[i]);
}
printf(" }");
}

You might also like