1 Star 0 Fork 0

Loser_Lus/编译原理实验

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
parser.h 2.72 KB
一键复制 编辑 原始数据 按行查看 历史
Loser_Lus 提交于 2022-05-19 17:16 . LL1分析法
#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
马建仓 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