Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

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

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 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