编写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)两种算法来分析表达式。