使用C语言编写LL(1)分析法,输入一个文法,输出first和follow集,并输出预测分析表,利用栈和预测分析表来对输入串进行分析,判断输入串是否是该文法的一个合适的语法范畴。
时间: 2024-06-08 17:09:15 浏览: 84
LL(1)分析法是一种自顶向下的语法分析方法,它在处理输入时从左到右进行,同时采用最左推导的方式。下面是一个使用C语言编写LL(1)分析法的示例:
假设我们有以下文法:
```
S -> ABC
A -> a | ε
B -> b | ε
C -> c | ε
```
首先,我们需要计算出该文法的First集和Follow集,代码如下:
```C
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 100
char first[MAX][MAX];
char follow[MAX][MAX];
char predict_table[MAX][MAX];
int num_rules;
char rules[MAX][MAX];
char non_terminals[MAX];
char terminals[MAX];
int num_non_terminals;
int num_terminals;
int has_epsilon[MAX];
void compute_first(int index);
void compute_follow();
int is_terminal(char c);
int is_non_terminal(char c);
void add_to_set(char set[], char c);
void print_set(char set[]);
void print_matrix(char matrix[][MAX], int rows, int cols);
int get_terminal_index(char c);
int get_non_terminal_index(char c);
void initialize_predict_table();
void add_to_predict_table(char non_terminal, char terminal, char rule[]);
void print_predict_table();
int main() {
printf("Enter the number of rules: ");
scanf("%d", &num_rules);
printf("Enter the rules:\n");
for (int i = 0; i < num_rules; i++) {
scanf("%s", rules[i]);
}
// Find the non-terminals and terminals
for (int i = 0; i < num_rules; i++) {
char non_terminal = rules[i][0];
if (!is_non_terminal(non_terminal)) {
non_terminals[num_non_terminals++] = non_terminal;
}
for (int j = 2; j < strlen(rules[i]); j++) {
char c = rules[i][j];
if (is_non_terminal(c)) {
if (!is_non_terminal(non_terminal)) {
add_to_set(first[get_non_terminal_index(non_terminal)], c);
}
} else {
if (c != 'ε') {
if (!is_terminal(c)) {
terminals[num_terminals++] = c;
}
add_to_set(first[get_non_terminal_index(non_terminal)], c);
break;
} else {
has_epsilon[get_non_terminal_index(non_terminal)] = 1;
}
}
}
}
// Compute the first sets
for (int i = 0; i < num_non_terminals; i++) {
compute_first(i);
}
// Compute the follow sets
compute_follow();
// Print the first and follow sets
printf("\nFirst sets:\n");
for (int i = 0; i < num_non_terminals; i++) {
printf("%c: ", non_terminals[i]);
print_set(first[i]);
}
printf("\nFollow sets:\n");
for (int i = 0; i < num_non_terminals; i++) {
printf("%c: ", non_terminals[i]);
print_set(follow[i]);
}
// Initialize the predict table
initialize_predict_table();
// Compute the predict table
for (int i = 0; i < num_rules; i++) {
char non_terminal = rules[i][0];
int non_terminal_index = get_non_terminal_index(non_terminal);
for (int j = 2; j < strlen(rules[i]); j++) {
char c = rules[i][j];
if (is_non_terminal(c)) {
for (int k = 0; k < strlen(first[get_non_terminal_index(c)]); k++) {
char terminal = first[get_non_terminal_index(c)][k];
add_to_predict_table(non_terminal, terminal, rules[i]);
}
if (!has_epsilon[get_non_terminal_index(c)]) {
break;
}
} else {
add_to_predict_table(non_terminal, c, rules[i]);
break;
}
}
if (has_epsilon[non_terminal_index]) {
for (int k = 0; k < strlen(follow[non_terminal_index]); k++) {
char terminal = follow[non_terminal_index][k];
add_to_predict_table(non_terminal, terminal, rules[i]);
}
}
}
// Print the predict table
printf("\nPredict table:\n");
print_predict_table();
// Parse an input string
printf("\nEnter an input string: ");
char input[MAX];
scanf("%s", input);
// Initialize the stack
char stack[MAX];
int top = -1;
// Push the start symbol onto the stack
stack[++top] = non_terminals[0];
// Parse the input string
int i = 0;
while (i < strlen(input) && top >= 0) {
char c = stack[top];
if (is_terminal(c)) {
if (c == input[i]) {
top--;
i++;
} else {
printf("\nError: Mismatched input symbol\n");
break;
}
} else {
int row = get_non_terminal_index(c);
int col = get_terminal_index(input[i]);
if (row == -1 || col == -1) {
printf("\nError: Invalid symbol\n");
break;
}
if (predict_table[row][col] != '-') {
char rule[MAX];
strcpy(rule, &(predict_table[row][col]));
top--;
for (int j = strlen(rule) - 1; j >= 2; j--) {
if (rule[j] != 'ε') {
stack[++top] = rule[j];
}
}
} else {
printf("\nError: No rule found in predict table\n");
break;
}
}
}
if (i == strlen(input) && top == -1) {
printf("\nInput string is valid!\n");
} else {
printf("\nInput string is invalid!\n");
}
return 0;
}
void compute_first(int index) {
char non_terminal = non_terminals[index];
for (int i = 0; i < num_rules; i++) {
if (rules[i][0] == non_terminal) {
for (int j = 2; j < strlen(rules[i]); j++) {
char c = rules[i][j];
if (is_non_terminal(c)) {
int non_terminal_index = get_non_terminal_index(c);
for (int k = 0; k < strlen(first[non_terminal_index]); k++) {
add_to_set(first[index], first[non_terminal_index][k]);
}
if (!has_epsilon[non_terminal_index]) {
break;
}
} else {
add_to_set(first[index], c);
break;
}
}
}
}
}
void compute_follow() {
add_to_set(follow[0], '$');
for (int i = 0; i < num_rules; i++) {
for (int j = 2; j < strlen(rules[i]); j++) {
char c = rules[i][j];
if (is_non_terminal(c)) {
int non_terminal_index = get_non_terminal_index(c);
for (int k = j + 1; k < strlen(rules[i]); k++) {
char d = rules[i][k];
if (is_non_terminal(d)) {
int d_index = get_non_terminal_index(d);
for (int l = 0; l < strlen(first[d_index]); l++) {
if (first[d_index][l] != 'ε') {
add_to_set(follow[non_terminal_index], first[d_index][l]);
}
}
if (!has_epsilon[d_index]) {
break;
}
} else {
add_to_set(follow[non_terminal_index], d);
break;
}
}
if (j == strlen(rules[i]) - 1) {
int non_terminal_index = get_non_terminal_index(rules[i][0]);
for (int k = 0; k < strlen(follow[non_terminal_index]); k++) {
add_to_set(follow[index], follow[non_terminal_index][k]);
}
}
}
}
}
}
int is_terminal(char c) {
for (int i = 0; i < num_terminals; i++) {
if (terminals[i] == c) {
return 1;
}
}
return 0;
}
int is_non_terminal(char c) {
for (int i = 0; i < num_non_terminals; i++) {
if (non_terminals[i] == c) {
return 1;
}
}
return 0;
}
void add_to_set(char set[], char c) {
if (strchr(set, c) == NULL) {
int len = strlen(set);
set[len] = c;
set[len + 1] = '\0';
}
}
void print_set(char set[]) {
for (int i = 0; i < strlen(set); i++) {
printf("%c ", set[i]);
}
printf("\n");
}
void print_matrix(char matrix[][MAX], int rows, int cols) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("%c ", matrix[i][j]);
}
printf("\n");
}
}
int get_terminal_index(char c) {
for (int i = 0; i < num_terminals; i++) {
if (terminals[i] == c) {
return i;
}
}
return -1;
}
int get_non_terminal_index(char c) {
for (int i = 0; i < num_non_terminals; i++) {
if (non_terminals[i] == c) {
return i;
}
}
return -1;
}
void initialize_predict_table() {
for (int i = 0; i < num_non_terminals; i++) {
for (int j = 0; j < num_terminals; j++) {
predict_table[i][j] = '-';
}
}
}
void add_to_predict_table(char non_terminal, char terminal, char rule[]) {
int row = get_non_terminal_index(non_terminal);
int col = get_terminal_index(terminal);
if (row != -1 && col != -1) {
if (predict_table[row][col] == '-') {
strcpy(&(predict_table[row][col]), rule);
} else {
printf("\nError: Ambiguous grammar\n");
exit(1);
}
}
}
void print_predict_table() {
printf(" ");
for (int i = 0; i < num_terminals; i++) {
printf("%c ", terminals[i]);
}
printf("\n");
for (int i = 0; i < num_non_terminals; i++) {
printf("%c | ", non_terminals[i]);
for (int j = 0; j < num_terminals; j++) {
printf("%c ", predict_table[i][j]);
}
printf("\n");
}
}
```
该程序先从标准输入读入文法,然后计算出该文法的First集和Follow集,并构建预测分析表。最后,用户可以输入一个字符串,程序将使用栈和预测分析表来对该字符串进行分析。
注意:该程序仅支持包含终结符和非终结符的文法,不支持带有操作符和优先级的文法。
阅读全文