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

#Include #Include: // Gets (A (I) )

This C program contains functions to calculate the follow set of variables in a context-free grammar: - The main() function prompts the user to enter grammar productions and a variable, and calls the follow() function. - The follow() function recursively traverses the productions, calling first() when it encounters the given variable, and follow() when it reaches the end of a production. - The first() function handles terminals and nonterminals, recursively calling follow() or adding to the result set. - addToResult() adds unique characters to the result follow set.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views2 pages

#Include #Include: // Gets (A (I) )

This C program contains functions to calculate the follow set of variables in a context-free grammar: - The main() function prompts the user to enter grammar productions and a variable, and calls the follow() function. - The follow() function recursively traverses the productions, calling first() when it encounters the given variable, and follow() when it reaches the end of a production. - The first() function handles terminals and nonterminals, recursively calling follow() or adding to the result set. - addToResult() adds unique characters to the result follow set.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

6b--->

#include<stdio.h>
#include<string.h>

int n, m = 0, p, i = 0, j = 0;
char a[10][10], followResult[10];

void follow(char c);

void first(char c);

void addToResult(char);

int main() {
int i;
int choice;
char c, ch;
printf("Enter the no.of productions: ");
scanf("%d", &n);
printf(" Enter %d productions\nProduction with multiple terms should be give as separate productions \n", n);
for (i = 0; i < n; i++)
scanf("%s%c", a[i], &ch);
// gets(a[i]);
do {
m = 0;
printf("Find FOLLOW of -->");
scanf(" %c", &c);
follow(c);
printf("FOLLOW(%c) = { ", c);
for (i = 0; i < m; i++)
printf("%c ", followResult[i]);
printf(" }\n");
printf("Do you want to continue(Press 1 to continue....)?");
scanf("%d%c", &choice, &ch);
} while (choice == 1);
}

void follow(char c) {
if (a[0][0] == c)addToResult('$');
for (i = 0; i < n; i++) {
for (j = 2; j < strlen(a[i]); j++) {
if (a[i][j] == c) {
if (a[i][j + 1] != '\0')first(a[i][j + 1]);
if (a[i][j + 1] == '\0' && c != a[i][0])
follow(a[i][0]);
}
}
}
}

void first(char c) {
int k;
if (!(isupper(c)))
//f[m++]=c;
addToResult(c);
for (k = 0; k < n; k++) {
if (a[k][0] == c) {
if (a[k][2] == '$') follow(a[i][0]);
else if (islower(a[k][2]))
//f[m++]=a[k][2];
addToResult(a[k][2]);
else first(a[k][2]);
}
}
}

void addToResult(char c) {
int i;
for (i = 0; i <= m; i++)
if (followResult[i] == c)
return;
followResult[m++] = c;
}

7---

#include<stdio.h>
#include<string.h>
void main() {
char string[50];
int flag,count=0;
printf("The grammar is: S->aS, S->Sb, S->ab\n");
printf("Enter the string to be checked:\n");
gets(string);
if(string[0]=='a') {
flag=0;
for (count=1;string[count-1]!='\0';count++) {
if(string[count]=='b') {
flag=1;
continue;
} else if(flag==1 && string[count]=='a') {
printf("The string does not belong to the specified grammar");
break;
} else if(string[count]=='a')
continue; else if(flag==1 && string[count]=='\0') {
printf("String accepted…..!!!!");
break;
} else {
printf("String not accepted");
}
}
}
}

You might also like