使用c语言写代码要求如下1算术表达式和赋值语句的文法是可根据需要适当改变: S→i=E E→E+EE-EEEEEEi2根据算符优先分析法将赋值语句进行语法分析翻译成等价的一组基本操作每一基本操作用四元式表示。
以下是使用c语言实现算符优先分析法,将赋值语句进行语法分析,翻译成等价的一组基本操作的代码:
#include <stdio.h> #include <stdlib.h> #include <string.h>
#define MAX_LENGTH 100 #define MAX_STACK 100
typedef struct { char op; int priority; } Operator;
typedef struct { char name; int value; } Variable;
typedef struct { char op; int arg1, arg2, result; } Quadruple;
Operator operators[] = { {'(', 0}, {'+', 1}, {'-', 1}, {'*', 2}, {'/', 2}, {')', 0}, };
int operatorCount = sizeof(operators) / sizeof(Operator);
int operatorPriority(char op) { int i; for (i = 0; i < operatorCount; i++) { if (operators[i].op == op) { return operators[i].priority; } } return -1; }
char* readLine() { char* line = (char*)malloc(sizeof(char) * MAX_LENGTH); fgets(line, MAX_LENGTH, stdin); line[strlen(line) - 1] = '\0'; return line; }
int isOperator(char c) { int i; for (i = 0; i < operatorCount; i++) { if (operators[i].op == c) { return 1; } } return 0; }
int isVariable(char c) { return c >= 'a' && c <= 'z'; }
int evaluate(char op, int arg1, int arg2) { switch (op) { case '+': return arg1 + arg2; case '-': return arg1 - arg2; case '*': return arg1 * arg2; case '/': return arg1 / arg2; default: return 0; } }
void printQuadruple(Quadruple q) { printf("(%c, %d, %d, %d)\n", q.op, q.arg1, q.arg2, q.result); }
void printQuadruples(Quadruple* quadruples, int count) { int i; for (i = 0; i < count; i++) { printQuadruple(quadruples[i]); } }
void parseAssignment(char* line) { Variable variables[MAX_STACK]; Quadruple quadruples[MAX_STACK]; int variableCount = 0; int quadrupleCount = 0; char operatorStack[MAX_STACK]; int operatorCount = 0; int i, j, k; for (i = 0; i < strlen(line); i++) { char c = line[i]; if (isVariable(c)) { variables[variableCount].name = c; variables[variableCount].value = 0; variableCount++; quadruples[quadrupleCount].op = 's'; quadruples[quadrupleCount].result = variableCount - 1; quadrupleCount++; } else if (isOperator(c)) { while (operatorCount > 0 && operatorStack[operatorCount - 1] != '(' && operatorPriority(operatorStack[operatorCount - 1]) >= operatorPriority(c)) { char op = operatorStack[operatorCount - 1]; operatorCount--; int arg2 = variables[variableCount - 1].value; variableCount--; int arg1 = variables[variableCount - 1].value; variableCount--; variables[variableCount].value = evaluate(op, arg1, arg2); quadruples[quadrupleCount].op = op; quadruples[quadrupleCount].arg1 = arg1; quadruples[quadrupleCount].arg2 = arg2; quadruples[quadrupleCount].result = variableCount; quadrupleCount++; } if (c == ')') { operatorCount--; } else { operatorStack[operatorCount] = c; operatorCount++; } } } while (operatorCount > 0) { char op = operatorStack[operatorCount - 1]; operatorCount--; int arg2 = variables[variableCount - 1].value; variableCount--; int arg1 = variables[variableCount - 1].value; variableCount--; variables[variableCount].value = evaluate(op, arg1, arg2); quadruples[quadrupleCount].op = op; quadruples[quadrupleCount].arg1 = arg1; quadruples[quadrupleCount].arg2 = arg2; quadruples[quadrupleCount].result = variableCount; quadrupleCount++; } printf("Quadruples:\n"); printQuadruples(quadruples, quadrupleCount); printf("Result:\n"); for (i = 0; i < variableCount; i++) { printf("%c = %d\n", variables[i].name, variables[i].value); } }
int main() { char* line = readLine(); parseAssignment(line); free(line); return 0;
原文地址: https://www.cveoy.top/t/topic/hdNm 著作权归作者所有。请勿转载和采集!