1 Star 0 Fork 0

Loser_Lus/编译原理实验

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
LR.cpp 3.57 KB
一键复制 编辑 原始数据 按行查看 历史
Loser_Lus 提交于 2022-05-26 20:12 . 实现四则运算的LR1分析
#include <iostream>
#include <iomanip>
#include "LR.h"
using namespace std;
int step = 0;
int inputIndex = 0;
string input;
void printStep(Node &node);
void LR1(string input);
int main()
{
init();
cout << "输入一以#结束的符号串(包括+—*/()i#):";
cin >> input;
cout.setf(ios::left);
cout << "Step\t\t" << setw(30) << "State" << setw(20) << "Symbol" << setw(20) << "Input" << setw(20) << "Action" << endl;
LR1(input);
return 0;
}
void printStep(Node &node)
{
string stateStackMsg = stackString(stateStack);
string symbolStackMsg = stackString(symbolStack);
string lastinput = input.substr(inputIndex);
cout.setf(ios::left);
if (step != 0)
{
if (node.action == SHIFT)
cout << step << "\t\t" << setw(30) << stateStackMsg << setw(20) << symbolStackMsg << setw(20) << lastinput << "S" << node.destState << endl;
else if (node.action == REDUCE)
cout << step << "\t\t" << setw(30) << stateStackMsg << setw(20) << symbolStackMsg << setw(20) << lastinput << "R" << node.production.num << "\t" << node.production.Left << " -> " << node.production.Right << endl;
else if (node.action = ACC)
cout << step << "\t\t" << setw(30) << stateStackMsg << setw(20) << symbolStackMsg << setw(20) << lastinput << "ACC" << endl;
}
else
{
cout << step << "\t\t" << setw(30) << stateStackMsg << setw(20) << symbolStackMsg << setw(20) << lastinput << endl;
}
step++;
}
void LR1(string input)
{
token = input.at(inputIndex);
// symbolStack.push(token);
// Action action;
Node node;
printStep(node);
int stateTop = stateStack.top();
char symbolTop = symbolStack.top();
if (existNode(stateTop, token))
{
node = LRMap[stateTop][token];
while (node.action != ACC)
{
if (node.action == SHIFT)
{
symbolStack.push(token);
stateStack.push(node.destState);
inputIndex++;
printStep(node);
}
else if (node.action == REDUCE)
{
int len = node.production.Right.length();
printStep(node);
for (int i = 0; i < len; i++)
{
stateStack.pop();
symbolStack.pop();
}
symbolStack.push(node.production.Left);
stateTop = stateStack.top();
symbolTop = symbolStack.top();
if (existNode(stateTop, symbolTop))
{
node = LRMap[stateTop][symbolTop];
stateStack.push(node.destState);
// printStep(node);
}
else
{
cout << "error2" << endl;
cout << "LR1分析表中不存在GOTO[ " << stateTop << " , " << symbolTop << "]" << endl;
exit(0);
}
}
else
{
cout << "error1" << endl;
cout << "LR1分析表中不存在ACTION[ " << stateTop << " , " << symbolTop << "]" << endl;
exit(0);
}
stateTop = stateStack.top();
symbolTop = symbolStack.top();
token = input.at(inputIndex);
node = LRMap[stateTop][token];
}
printStep(node);
cout << "ACCEPT" << endl;
}
else
{
cout << "error0" << endl;
cout << "LR1分析表中不存在ACTION[ " << stateTop << " , " << symbolTop << "]" << endl;
exit(0);
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/loser-lus/compilation-principle.git
git@gitee.com:loser-lus/compilation-principle.git
loser-lus
compilation-principle
编译原理实验
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385