Follow
Follow
h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define MAX_NON_TERMINALS 26
#define MAX_SYMBOLS 10
typedef struct {
char lhs;
char rhs[MAX_SYMBOLS];
} Produc on;
char non_terminals[MAX_NON_TERMINALS];
int num_non_terminals = 0;
char follow[MAX_NON_TERMINALS][MAX_SYMBOLS];
int is_non_terminal(char c) {
return isupper(c);
int i;
}
follow[nt - 'A'][i] = symbol;
void compute_follow() {
int i, j, k;
int changed;
// Add $ to FOLLOW(S)
do {
changed = 0;
nt = produc ons[i].lhs;
if (is_non_terminal(produc ons[i].rhs[j])) {
changed = 1;
changed = 1;
} else {
changed = 1;
} while (changed);
void print_follow() {
int i, j;
prin ("}\n");
}
int main() {
int i;
while (1) {
prin ("Invalid input format. Please use 'X -> abc' format.\n");
con nue;
if (!is_non_terminal(lhs)) {
con nue;
num_produc ons++;
if (i == num_non_terminals) {
non_terminals[num_non_terminals++] = lhs;
}
}
compute_follow();
print_follow();
return 0;
// E -> E+T
// E -> T
// T -> T*F
// T -> F
// F -> (E)
// F -> id
// S->end
// FOLLOW(E) = { $ + ) }
// FOLLOW(T) = { $ + * ) }
// FOLLOW(F) = { $ + * ) }