代码拉取完成,页面将自动刷新
#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);
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。