代码拉取完成,页面将自动刷新
#ifndef PARSER_H
#define PARSER_H
#include <stack>
#include <string>
#include <map>
#include<algorithm>
#define NNUM 5
#define TNUM 8
using namespace std;
/*
*
*E->TG
*G->-TG | +TG
*G->epsilon
*T->FM
*M->*FM | /FM
*M->epsilon
*F->(E) | i
*
*
* 语法分析表
* FIRST FOLLOW
*E (,i #,)
*
*G +,-,epsilon #,)
*
*T (,i +,-,#,)
*
*M *,/,epsilon +,-,#,)
*
*F (,i *,/,+,-,#,)
*
*LL(1) 分析表
*
* ( ) i + - * / #
*E E->TG E->TG
*
*G G->epsilon G->+TG G->-TG G->epsilon
*
*T T->FM T->FM
*
*M M->epsilon M->epsilon M->epsilon M->*FM M->/EM M->epsilon
*
*F F->(E) F->i
*
*
*
* 步骤 分析栈 剩余输入串 所用产生式
*/
char token;
char VN[] = {'E', 'G', 'T', 'M', 'F'};
char VT[] = {'(', ')', 'i', '+', '-', '*', '/', '#'};
bool isVT(char);
bool isVN(char);
stack<char> analyseStack;
map<char, map<char, string>> selectMap;
void init();
string stackString();
bool existProduction(char VN,char VT);
bool isVN(char c)
{
for (int i = 0; i < NNUM; i++)
{
if (c == VN[i])
return true;
}
return false;
}
bool isVT(char c)
{
for (int i = 0; i < TNUM; i++)
{
if (c == VT[i])
return true;
}
return false;
}
void init()
{
selectMap['E']['('] = "TG";
selectMap['E']['i'] = "TG";
selectMap['G'][')'] = "~";
selectMap['G']['+'] = "+TG";
selectMap['G']['-'] = "-TG";
selectMap['G']['#'] = "~";
selectMap['T']['('] = "FM";
selectMap['T']['i'] = "FM";
selectMap['M'][')'] = "~";
selectMap['M']['+'] = "~";
selectMap['M']['-'] = "~";
selectMap['M']['*'] = "*FM";
selectMap['M']['/'] = "/EM";
selectMap['M']['#'] = "~";
selectMap['F']['('] = "(E)";
selectMap['F']['i'] = "i";
analyseStack.push('#');
analyseStack.push('E');
}
string stackString()
{
char c;
stack<char> temp;
string s;
while(!analyseStack.empty())
{
s.push_back(analyseStack.top());
//s.append(to_string(analyseStack.top()));
temp.push(analyseStack.top());
analyseStack.pop();
}
while(!temp.empty())
{
analyseStack.push(temp.top());
temp.pop();
}
reverse(s.begin(),s.end());
return s;
}
bool existProduction(char VN,char VT)
{
auto it = selectMap.find(VN);
if(it!=selectMap.end())
{
if(it->second.find(VT)!=it->second.end())
return true;
}
return false;
}
#endif
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。