写出上述问题的C语言代码
时间: 2024-03-01 16:52:51 浏览: 68
好的,以下是一个简单的C语言代码实现,用于对输入的LL(1)文法进行预测分析:
```c
#include <stdio.h>
#include <string.h>
#define MAX 100
char stack[MAX];
int top = -1;
char input[MAX];
int ptr = 0;
char table[100][100][MAX];
int n, m;
int is_terminal(char c) {
if (c >= 'a' && c <= 'z')
return 1;
if (c >= 'A' && c <= 'Z')
return 0;
return -1;
}
int get_row(char c) {
for (int i = 0; i < n; i++) {
if (table[i][0][0] == c)
return i;
}
return -1;
}
int get_col(char c) {
for (int i = 0; i < m; i++) {
if (table[0][i][0] == c)
return i;
}
return -1;
}
int main() {
// 输入文法
printf("请输入文法的产生式个数:");
scanf("%d", &n);
printf("请输入文法的终结符个数:");
scanf("%d", &m);
printf("请输入文法的产生式(左递归形式):\n");
for (int i = 0; i < n; i++) {
scanf("%s", table[i][0]);
int k;
scanf("%d", &k);
for (int j = 1; j <= k; j++)
scanf("%s", table[i][j]);
}
printf("请输入文法的终结符:\n");
for (int i = 0; i < m; i++) {
char s[10];
scanf("%s", s);
strcpy(table[0][i+1], s);
}
strcpy(table[0][0], " ");
// 构造first集和follow集
// ...
// 构造预测分析表
for (int i = 1; i <= n; i++) {
char A = table[i][0][0];
for (int j = 1; j <= m; j++) {
char a = table[0][j][0];
strcpy(table[i][j], "");
for (int k = 1; k <= n; k++) {
if (table[k][0][0] == A) {
int flag = 0;
for (int l = 1; l <= strlen(table[k][1]); l++) {
char X = table[k][1][l-1];
if (is_terminal(X) == 1 && X == a) {
strcat(table[i][j], table[k][1]);
flag = 1;
break;
} else if (is_terminal(X) == -1) {
int row = get_row(X);
int col = get_col(a);
if (table[row][col][0] != '\0') {
strcat(table[i][j], table[k][1]);
strcat(table[i][j], table[row][col]);
flag = 1;
break;
} else {
break;
}
} else {
break;
}
}
if (flag)
break;
}
}
}
}
// 输入待分析的串
printf("请输入待分析的串:");
scanf("%s", input);
// 预测分析
stack[++top] = ' ';
stack[++top] = table[1][0][0];
while (ptr <= strlen(input)) {
if (stack[top] == ' ') {
top--;
continue;
}
if (is_terminal(stack[top]) == 1) {
if (stack[top] == input[ptr]) {
ptr++;
top--;
} else {
printf("输入串不符合该文法的语法规则!\n");
return 0;
}
} else {
int row = get_row(stack[top]);
int col = get_col(input[ptr]);
if (row == -1 || col == -1) {
printf("输入串不符合该文法的语法规则!\n");
return 0;
}
if (table[row+1][col+1][0] == '\0') {
printf("输入串不符合该文法的语法规则!\n");
return 0;
}
top--;
for (int i = strlen(table[row+1][col+1])-1; i >= 0; i--) {
stack[++top] = table[row+1][col+1][i];
}
}
}
if (stack[top] == ' ' && ptr == strlen(input)) {
printf("输入串符合该文法的语法规则!\n");
} else {
printf("输入串不符合该文法的语法规则!\n");
}
return 0;
}
```
需要注意的是,以上代码只是一个简单的实现,可能存在某些细节上的问题,需要根据具体的情况进行调整和完善。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)