#include <stdio.h> #include <stdlib.h> #include <string.h>

//变量定义 char s[100], str[100], stackStr[100];//s:输入串、str:中间变量、stackStr : 模拟栈 int i; char ch;//当前分析字符 char v[100][100];//字符串类型的数组(文法+分析栈) int idx = 0;//数组下标 //函数声明 void E(); //E-->TG void G(); //G-->+TG|ε void T(); //T-->FS void S(); //S-->*FS|ε void F(); //F-->(E)|i void err(); //提示错误信息 int check();//验证是否已经到栈底 void push(char *pre, char *value);//将字符串存入输出栈

/**

  • 函数功能:提示错误信息 */ void err() { printf("ERROR\n"); exit(0); }

/**

  • 函数功能:将字符串存入输出栈 */ void push(char *pre, char value) { ch = s[i]; idx++; strcpy(v[idx], stackStr); if (value[0] != 'ε')//不是空字时 { char value1[10]; strcpy(value1, value); if (value[0] == '+')strcpy(value1, "TG"); if (value[0] == '')strcpy(value1, "FS"); if (value[0] == '(')strcpy(value1, "E"); if (value[0] == 'i')strcpy(value1, ""); int len = strlen(pre); for (int j = 0; j < len; j++)//将pre中的字符一个一个替换为value中的字符 { int k = strchr(stackStr, pre[j]) - stackStr; stackStr[k] = value1[j]; } } else { int len = strlen(pre); for (int j = 0; j < len; j++)//将pre中的字符一个一个删除 { int k = strchr(stackStr, pre[j]) - stackStr; for (int l = k; l < strlen(stackStr) - 1; l++) { stackStr[l] = stackStr[l + 1]; } stackStr[strlen(stackStr) - 1] = '\0'; } } strcat(v[idx], pre); strcat(v[idx], value); strcat(v[idx], ","); strcat(v[idx], stackStr);//将对应的表达式和栈的内容存加入在数组v中 }

/**

  • 函数功能:验证是否已经到栈底 */ int check() { if (i >= strlen(s)) { return 1; } else if (s[i] == '#') { ch = '#'; return 1; } return 0; }

/**

  • 函数功能:E-->TG */ void E() { push("E-->", "TG"); T(); G(); }

/**

  • 函数功能:G-->+TG|ε */ void G() { if (s[i] == '+') { str[0] = s[i]; str[1] = '\0'; strcat(str, "TG"); i++; push("G-->", str); T(); G(); } else { push("G-->", "ε"); } }

/**

  • 函数功能:T-->FS */ void T() { push("T-->", "FS"); F(); S(); }

/**

  • 函数功能:S-->*FS|ε / void S() { if (s[i] == '') { str[0] = s[i]; str[1] = '\0'; strcat(str, "FS"); i++; push("S-->", str); F(); S(); } else { push("S-->", "ε"); } }

/**

  • 函数功能:F-->(E)|i */ void F() { if (s[i] == '(') { i++; push("F-->", "(E)"); E(); if (s[i] == ')') { i++; } else { err(); } } else if (s[i] == 'i') { i++; push("F-->", "i"); } else { err(); } }

/**

  • 函数功能:主函数 */ int main() { printf("===================================================\n"); printf("=== 递归下降分析 ===\n"); printf("===================================================\n"); printf("===请输入字符串 (以#号结束)===\n"); while (scanf("%s", s) != EOF) //输入要分析的字符串 { idx = 0; memset(v, 0, sizeof(v)); i = 0; strcpy(stackStr, "E#");//初始化栈 E(); if (check()) { printf("=====>\t\t 输入串分析正确! \n"); printf("推导过程如下: \n"); printf("文法\t\t分析栈\t\t当前分析字符\n"); printf("E \t\tE#\t\t%c\n", s[0]);//初始栈的内容 int j; for (j = 1; j <= idx; j++) { printf("%s\t", v[j]); printf("%-10s\t\t", v[j] + strlen(v[j]) + 1); printf("%c\n", ch); ch = s[j]; } } else printf("==>\t 输入串不符合该文法 \n"); }

    return 0;

用c修改下列代码并修改和完善代码:#include iostream#include iomanip#include string#include vectorusing namespace std;变量定义string s str stackStr;s:输入串、str:中间变量、stackStr 模拟栈int i;string ch;当前分析字符vectorstring v;字符串类型的向量文

原文地址: https://www.cveoy.top/t/topic/fiDD 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录