编写C程序(数据结构用栈)实现LR分析法

时间: 2024-05-04 22:16:13 浏览: 11
好的!这是一个关于数据结构使用栈来实现LR分析法的C程序示例代码: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX 100 // 定义操作符栈 typedef struct { char item[MAX]; int top; } operator_stack; // 定义数字栈 typedef struct { int item[MAX]; int top; } number_stack; // 初始化栈 void initStack(operator_stack *S) { S->top = -1; } // 初始化栈 void initStack(number_stack *S) { S->top = -1; } // 判断栈是否为空 int isEmpty(operator_stack *S) { if (S->top == -1) return 1; else return 0; } // 判断栈是否为空 int isEmpty(number_stack *S) { if (S->top == -1) return 1; else return 0; } // 判断栈是否已满 int isFull(operator_stack *S) { if (S->top == MAX - 1) return 1; else return 0; } // 判断栈是否已满 int isFull(number_stack *S) { if (S->top == MAX - 1) return 1; else return 0; } // 获取栈顶元素 char getTop(operator_stack *S) { if (isEmpty(S)) return '\0'; else return S->item[S->top]; } // 获取栈顶元素 int getTop(number_stack *S) { if (isEmpty(S)) return 0; else return S->item[S->top]; } // 压栈 void push(operator_stack *S, char ch) { if (isFull(S)) { printf("栈满,不能再入栈了!\n"); return; } S->top++; S->item[S->top] = ch; } // 压栈 void push(number_stack *S, int num) { if (isFull(S)) { printf("栈满,不能再入栈了!\n"); return; } S->top++; S->item[S->top] = num; } // 弹栈 char pop(operator_stack *S) { char ch; if (isEmpty(S)) { printf("栈空,不能出栈了!\n"); return '\0'; } ch = S->item[S->top]; S->top--; return ch; } // 弹栈 int pop(number_stack *S) { int num; if (isEmpty(S)) { printf("栈空,不能出栈了!\n"); return 0; } num = S->item[S->top]; S->top--; return num; } // 判断字符是否为操作符 int isOperator(char ch) { if (ch == '+' || ch == '-' || ch == '*' || ch == '/' || ch == '(' || ch == ')' || ch == '#') return 1; else return 0; } // 获取操作符的优先级 int getPriority(char op) { int priority; switch(op) { case '+': case '-': priority = 1; break; case '*': case '/': priority = 2; break; case '(': priority = 0; break; case '#': priority = -1; break; } return priority; } // 计算表达式的值 int calculate(number_stack *numS, char op) { int num1, num2, result; num2 = pop(numS); num1 = pop(numS); switch (op) { case '+': result = num1 + num2; break; case '-': result = num1 - num2; break; case '*': result = num1 * num2; break; case '/': result = num1 / num2; break; } return result; } // 根据LR分析表分析输入串 void analyze(char analyzeTable[][5][5], operator_stack *opS, number_stack *numS, char *inputStr) { char analyzeChar, op; int i = 0, j, row, col, num, result; push(opS, '#'); analyzeChar = inputStr[i]; op = getTop(opS); while (op != '#' || analyzeChar != '#') { if (!isOperator(analyzeChar)) { num = analyzeChar - '0'; while (!isOperator(analyzeChar)) { i++; analyzeChar = inputStr[i]; if (!isOperator(analyzeChar)) { num = num * 10 + analyzeChar - '0'; } } push(numS, num); } else { switch (analyzeTable[op - 35][analyzeChar - 35][0]) { case '<': push(opS, analyzeChar); i++; analyzeChar = inputStr[i]; break; case '>': op = pop(opS); while (getPriority(op) >= getPriority(getTop(opS))) { result = calculate(numS, op); push(numS, result); op = pop(opS); } push(opS, op); break; case '=': pop(opS); i++; analyzeChar = inputStr[i]; break; case 'e': printf("ERROR!\n"); return; } } printf("\n操作符栈: "); for (j = 0; j <= opS->top; j++) printf("%c ", opS->item[j]); printf("\n"); printf("数字栈: "); for (j = 0; j <= numS->top ; j++) printf("%d ", numS->item[j]); printf("\n"); op = getTop(opS); } result = pop(numS); printf("\n计算结果:%d", result); } // 主函数 int main() { operator_stack opS; number_stack numS; char analyzeTable[][5][5] = { {"", "<", "", "<", "<"}, {">", ">", "", ">", ">"}, {"<", "<", "", "<", "<"}, {">", ">", "", ">", ">"}, {">", ">", "e", ">", ">"} }; char inputStr[MAX]; int choice; printf("请输入想要计算的表达式:"); scanf("%s", inputStr); printf("\n请选择要使用的算符优先关系:\n"); printf("1. LR(0)分析表(‘.’后面是状态编号’)\n"); printf("2. SLR(1)分析表(‘.’后面是产生式编号’)\n"); scanf("%d", &choice); switch (choice) { case 1: analyzeTable[2][2][0] = '='; break; case 2: analyzeTable[2][2][0] = '<'; break; } initStack(&opS); initStack(&numS); analyze(analyzeTable, &opS, &numS, inputStr); return 0; } ``` 注意:该程序实现了栈的基本操作并使用LR分析表来分析输入的中缀表达式,计算出最终结果。可以在main函数中选择LR(0)和SLR(1)两种算法来分析表达式。

相关推荐

最新推荐

recommend-type

4 实验四:LR分析程序的设计与实现

1、了解LR(0)分析方法是严格的从左向右扫描,和自底向上的语法分析方法。 2、掌握LR(0)语法分析方法。
recommend-type

编译原理课程设计 LR(0)分析表和分析器的构造和程序实现

LR(0)分析表算法的程序实现 1. 对任意给定的文法 ,完成识别文法活前缀的 、 的状态转化矩阵及 项目集规范族的构造; 2. 判断该文法是否为 文法,实现 分析表的构造,并输出到指定文件中; 3. 实现 分析器总控程序...
recommend-type

LR分析器总控程序的实现

自己写课设报告,想要的自己拿去看看,有什么问题可以联系我,邮箱zxy6651@126.com
recommend-type

LR(0)语法分析的实现

LR(0)语法分析的实现:对于所输入的LR(0)文法,不论对错,都应有明确的信息告诉外界。对于符合规则的LR(0)文法,将输出LR(0)分析表,并可以对输入的句子进行语法分析输出相应语法树。
recommend-type

LR(0)语法分析的设计与实现.doc

内含代码片段。原理包含CLOSURE和GOTO函数的构造说明,前缀、项目、拓广文法的定义说明,文法项目集规范族的构造伪代码,判断文法是否为LR(0)文法的说明,以及分析表构造讲解与输入串合法性分析步骤。
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

MATLAB柱状图在信号处理中的应用:可视化信号特征和频谱分析

![matlab画柱状图](https://img-blog.csdnimg.cn/3f32348f1c9c4481a6f5931993732f97.png) # 1. MATLAB柱状图概述** MATLAB柱状图是一种图形化工具,用于可视化数据中不同类别或组的分布情况。它通过绘制垂直条形来表示每个类别或组中的数据值。柱状图在信号处理中广泛用于可视化信号特征和进行频谱分析。 柱状图的优点在于其简单易懂,能够直观地展示数据分布。在信号处理中,柱状图可以帮助工程师识别信号中的模式、趋势和异常情况,从而为信号分析和处理提供有价值的见解。 # 2. 柱状图在信号处理中的应用 柱状图在信号处理
recommend-type

用Spring boot和vue写一个登录注册界面

好的,这是一个比较常见的技术问题,我可以为您提供一些参考资料和思路。首先,您可以使用Spring Boot作为后端框架来处理用户认证和注册的逻辑,比如使用Spring Security实现用户登录认证。同时,您还需要设计相应的数据模型和数据库表结构来存储用户信息。在前端方面,您可以使用Vue.js作为框架来构建登录注册页面,使用Axios来发起API请求并和后端进行交互。当然,在实现过程中,还需要考虑一些具体细节,比如数据校验、安全性和用户体验等方面。希望这些信息能够帮助到您。
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。