0% found this document useful (0 votes)
10 views4 pages

First

Uploaded by

kumshubham9870
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)
10 views4 pages

First

Uploaded by

kumshubham9870
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

#include <stdio.

h>

#include <ctype.h>

#include <string.h>

#define MAX 10

void findFirst(char, int, int);

int count, n;

char calc_first[MAX][MAX];

char produc on[MAX][MAX], first[MAX];

// Func on to find the FIRST of a non-terminal

void findFirst(char c, int q1, int q2) {

int j;

// If the symbol is a terminal, add it to the FIRST set

if (!(isupper(c))) {

calc_first[q1][q2++] = c;

} else {

for (j = 0; j < count; j++) {

if (produc on[j][0] == c) {

// If epsilon is present, look for alterna ves

if (produc on[j][2] == '#') {

if (produc on[q1][q2] == '\0')

calc_first[q1][q2++] = '#';

else if (produc on[q1][q2] != c)

findFirst(produc on[q1][q2], q1, q2);

} else if (!isupper(produc on[j][2])) {

calc_first[q1][q2++] = produc on[j][2];

} else {

findFirst(produc on[j][2], q1, q2);


}

int main() {

int i, choice;

char c, ch;

// Read number of produc ons

prin ("Enter the number of produc ons: ");

scanf("%d", &count);

// Read the grammar produc ons

for (i = 0; i < count; i++) {

prin ("Enter produc on %d: ", i + 1);

scanf("%s", produc on[i]);

int k;

char done[MAX];

int ptr = -1;

// Calculate FIRST sets for each non-terminal

for (k = 0; k < count; k++) {

c = produc on[k][0];

int flag = 0;

for (i = 0; i <= ptr; i++)

if (c == done[i])

flag = 1;
if (flag == 1)

con nue;

findFirst(c, k, 0);

ptr += 1;

// Store the processed non-terminal

done[ptr] = c;

prin ("\nFIRST(%c) = { ", c);

for (i = 0; i < strlen(calc_first[k]); i++) {

prin ("%c ", calc_first[k][i]);

prin ("}\n");

/*

Enter the number of produc ons: 6

Enter produc on 1: E->E+T

Enter produc on 2: E->T

Enter produc on 3: T->T*F

Enter produc on 4: T->F

Enter produc on 5: F->(E)

Enter produc on 6: F->id

FIRST(E) = { id ( }

FIRST(T) = { id ( }

FIRST(F) = { id ( }

*/

You might also like