酷代码 AI
菜单
全部AI  /  热门  /  AI写代码神器
服务商

递归下降语法分析程序举例 本例文法: g[<表达式>]: g[e]: <表达式>→<表达式>+<项>|<表达式>-<项>|<项> e→e+t|t <项>→<项>*<因式>|<项>/<因式>|<因式> t→t*f|f <因式>→<标识符>|<无符号整数>|(<表达式>) f→i|(e) #include <stdio.h> #include <dos.h> #include <stdlib.h> #include <string.h> char a[50],b[50],d[200],e[10];/*数组a存输入串,数组d存推导式,*/ char ch; int n1,i1=0,flag=1,n=5; /*flag=1处理非终结符,flag=0处理终结符*/ int total=0; /*步骤计数器*/ int e(); int e1(); int t(); int g();/*e’*/ int s();/*t’*/ int f(); void input(); void input1(

以下是为您补充的 3 点额外需要注意的逻辑点: 1. 对于输入字符串的处理,要确保格式的正确性和完整性,避免因输入错误导致程序异常。 2. 在递归调用各个函数时,要注意控制递归的深度,防止出现栈溢出的情况。 3. 对于符号的判断和处理要准确无误,特别是运算符的优先级和结合性。 以下是为您编写的递归下降语法分析程序的完整代码及 ```c #include <stdio.h> #include <dos.h> #include <stdlib.h> #include <string.h> char a[50], b[50], d[200], e[10]; /*数组 a 存输入串,数组 d 存推导式,*/ char ch; int n1, i1 = 0, flag = 1, n = 5; /*flag=1 处理非终结符,flag=0 处理终结符*/ int total = 0; /*步骤计数器*/ // 处理表达式 e int e() { if (t()) { // 先处理项 t if (ch == '+' || ch == '-') { // 如果后面是 + 或 - strcpy(d + strlen(d), "e->e"); // 记录推导式 strcat(d + strlen(d), e[total++] + '0'); // 步骤计数 strcat(d + strlen(d), " + t\n"); ch = a[i1++]; // 读取下一个字符 if (e1()) { // 处理表达式 e 的后续部分 return 1; } } else { return 1; } } return 0; } // 处理表达式 e 的后续部分 e1 int e1() { if (t()) { // 处理项 t if (ch == '+') { // 如果是 + strcpy(d + strlen(d), "e->e"); strcat(d + strlen(d), e[total++] + '0'); strcat(d + strlen(d), " + t\n"); ch = a[i1++]; if (e1()) { // 继续处理 e1 return 1; } } else if (ch == '-') { // 如果是 - strcpy(d + strlen(d), "e->e"); strcat(d + strlen(d), e[total++] + '0'); strcat(d + strlen(d), " - t\n"); ch = a[i1++]; if (e1()) { // 继续处理 e1 return 1; } } else { return 1; } } return 0; } // 处理项 t int t() { if (f()) { // 先处理因式 f if (ch == '*' || ch == '/') { // 如果后面是 * 或 / strcpy(d + strlen(d), "t->t"); strcat(d + strlen(d), e[total++] + '0'); strcat(d + strlen(d), " * f\n"); ch = a[i1++]; if (s()) { // 处理项 t 的后续部分 return 1; } } else { return 1; } } return 0; } // 处理项 t 的后续部分 s int s() { if (f()) { // 处理因式 f if (ch == '*') { // 如果是 * strcpy(d + strlen(d), "t->t"); strcat(d + strlen(d), e[total++] + '0'); strcat(d + strlen(d), " * f\n"); ch = a[i1++]; if (s()) { // 继续处理 s return 1; } } else if (ch == '/') { // 如果是 / strcpy(d + strlen(d), "t->t"); strcat(d + strlen(d), e[total++] + '0'); strcat(d + strlen(d), " / f\n"); ch = a[i1++]; if (s()) { // 继续处理 s return 1; } } else { return 1; } } return 0; } // 处理因式 f int f() { if (ch >= 'a' && ch <= 'z') { // 如果是标识符 strcpy(d + strlen(d), "f->i\n"); ch = a[i1++]; return 1; } else if (ch >= '0' && ch <= '9') { // 如果是无符号整数 strcpy(d + strlen(d), "f->n\n"); ch = a[i1++]; return 1; } else if (ch == '(') { // 如果是左括号 ch = a[i1++]; if (e()) { // 处理表达式 e if (ch == ')') { // 如果后面是右括号 ch = a[i1++]; strcpy(d + strlen(d), "f->(e)\n"); return 1; } } } return 0; } // 输入函数 void input() { printf("请输入表达式:"); scanf("%s", a); } int main() { input(); // 调用输入函数获取表达式 ch = a[i1++]; if (e() && ch == '\0') { // 进行表达式的分析 printf("分析成功\n"); printf("%s", d); // 输出推导式 } else { printf("分析失败\n"); } return 0; } ``` [2024-12-12 22:46:04 | AI写代码神器 | 1371点数解答]

相关提问