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

Program Code

This document contains code to find the FIRST set of grammar production rules. It defines structures to store non-terminals and productions, reads in grammar rules, implements functions to check if a character is terminal and add to a set, and recursively computes and prints the FIRST set for each non-terminal.

Uploaded by

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

Program Code

This document contains code to find the FIRST set of grammar production rules. It defines structures to store non-terminals and productions, reads in grammar rules, implements functions to check if a character is terminal and add to a set, and recursively computes and prints the FIRST set for each non-terminal.

Uploaded by

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

Experiment No:4

#include <stdio.h>

#include <stdbool.h>

#include <string.h>

#define MAX_PROD 10

#define MAX_LEN 10

typedef struct

char non_terminal;

char productions[MAX_LEN];

} ProductionRule;

ProductionRule productions[MAX_PROD];

int num_productions;

void findFirst(char non_terminal);

bool isTerminal(char ch)

return !(ch>='A' && ch<='Z');

void addToSet(char set[],char ch)

int len=strlen(set);

if(strchr(set,ch)==NULL)

set[len]=ch;

set[len+1]='\0';

}
}

void computeFirst(char non_terminal,char first[])

for(int i=0;i<num_productions;i++)

if(productions[i].non_terminal==non_terminal)

char next_char=productions[i].productions[0];

if(isTerminal(next_char))

addToSet(first,next_char);

else

computeFirst(next_char,first);

void findFirst(char non_terminal)

char first[MAX_LEN]="";

computeFirst(non_terminal,first);

printf("First(%c)={%s}\n",non_terminal,first);

}
int main()

printf("\n Enter the number of production rules:");

scanf("%d",&num_productions);

printf("\n Enter the production rules(non_terminal->production):\n");

for(int i=0;i<num_productions;i++)

scanf("%c->%s",&productions[i].non_terminal,productions[i].productions);

printf("\nFirst and Follow sets:\n");

for(int i=0;i<num_productions;i++)

findFirst(productions[i].non_terminal);

return 0;

}
Output:

You might also like