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

#define N 10 #define M 15

typedef struct { char op; // 操作符 int arg1; // 第一个操作数的地址 int arg2; // 第二个操作数的地址 int result; // 存放结果的地址 } Quaternary;

int map(char ch) { char loca[7]={'+','*','i','(',')','#'}; char *p; p=strchr(loca,ch); if (p==NULL) return -1; else return p-loca; }

int main(int argc, char* argv[]) { char syn[M]; // 语法栈 int top = 0; // 栈顶指针 int top_in = 1; // 移进指针 int handle[N]; // <栈 int top_h = -1; // <栈顶指针 char w; // 当前单词 char exp[M]; // 表达式区 int i_exp = 0; // 表达式指针 int prio[6][6] = { {3, 1, 1, 1, 3, 3}, {3, 3, 1, 1, 3, 3}, {3, 3, 0, 0, 3, 3}, {1, 1, 1, 1, 2, 0}, {3, 3, 0, 0, 3, 3}, {1, 1, 1, 1, 0, 4} }; // 优先分析表
int i, j; // 表行和列 int code; // 表项 char rules[N][10] = { "i = E", "E + E", "E - E", "E * E", "E / E" }; // 产生式 Quaternary q[N]; // 存放四元式的数组 int q_cnt = 0; // 四元式计数器

printf("please input your expression:");
scanf("%s",exp);    // 输入表达式

syn[0] = '#';       // 初始化
w = exp[i_exp++];    // read(w)
while (1) {
    // 查分析表code=prio(i,j)
    i = map(syn[top]);  // 定位行和列
    j = map(w);
    code = prio[i][j];  // 查表
    // 空或OK
    if (code == 0 || code == 4) {
        break;
    }
    // 栈操作 and 输入操作
    if (code < 3) {     // < or =,push(w)
        syn[top_in] = w;    // 进栈
        if (code == 1) {    // 记录句柄的左端位置
            handle[++top_h] = top + 1; 
        }
        top = top_in++;
        w = exp[i_exp++];
    } else {            // >,REDUCE(SYN)
        syn[top_in] = '\0';
        i = 0;
        while (strcmp(rules[i], syn + handle[top_h]) &
要求:1算术表达式和赋值语句的文法是可根据需要适当改变: S→i=E E→E+EE-EEEEEEi2根据算符优先分析法将赋值语句进行语法分析翻译成等价的一组基本操作每一基本操作用四元式表示。使用C语言根据上面俩个要求修改并补全下面代码:#include stringh#define N 4int mapchar ch

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

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