编译原理 算符优先法文法分析
由于水平有限,这里就不写原理了,请参考书《编译原理》(第3版)——清华大学出版社。
书中相关内容为第110页5.3.3节到第117页。其中FIRSTVT集和LASTVT集是根据第114页的简单关系图形所求的,规约过程参考第116页表5.7和表5.8。
代码如下:
#include <iostream>
#include <vector>
#include <map>
#include <string>
using namespace std;
//输入文法
string input;
//存储文法
vector<string>Context;
//非终结符
vector<char>Vn;
//终结符
vector<char>Vt;
//FIRSTVT集
map<char, string>FIRSTVT;
//LASTVT集
map<char, string>LASTVT;
//算符优先关系表
map<char, map<char, char>>OperatorTable;
//处理文法、终结符、非终结符
void InputContext()
{
while (cin>>input)
{
if (input == "@") break;
Context.push_back(input);
for (int i = 0; i < input.size(); i++)
{
if ((input[i] >= 'a' && input[i] <= 'z')||input[i]=='(' || input[i] == ')' || input[i] == '+' || input[i] == '*' || input[i] == '!' || input[i] == '#')
{
if (find(Vt.begin(), Vt.end(), input[i]) == Vt.end())
{
Vt.push_back(input[i]);
}
}
if (input[i] >= 'A' && input[i] <= 'Z')
{
if (find(Vn.begin(), Vn.end(), input[i]) == Vn.end())
{
Vn.push_back(input[i]);
}
}
}
}
cout << endl;
//输出终结符
cout << "终结符: ";
for (int i = 0; i < Vn.size(); i++)
{
cout << Vn[i]<<" ";
}
cout << endl;
//输出非终结符
cout << "非终结符: " ;
for (int i = 0; i < Vt.size(); i++)
{
cout << Vt[i] <<" ";
}
cout << endl;
}
//计算FIRSTVT集,one
void InitFIRSTVT()
{
for (int i = 0; i < Context.size(); i++)
{
if ((Context[i][3] >= 'a' && Context[i][3] <= 'z') || Context[i][3] == '(' || Context[i][3] == ')' || Context[i][3] == '+' || Context[i][3] == '*' || Context[i][3] == '!' || Context[i][3] == '#')
{
FIRSTVT[Context[i][0]] += Context[i][3];
}
if (Context[i].size()>= 4)
{
if ((Context[i][3] >= 'A' && Context[i][3] <= 'Z') &&( (Context[i][4] >= 'a' && Context[i][4] <= 'z') || Context[i][4] == '(' || Context[i][4] == ')' || Context[i][4] == '+' || Context[i][4] == '*' || Context[i][4] == '!' || Context[i][4] == '#'))
{
FIRSTVT[Context[i][0]] += Context[i][4];
}
}
if (Context[i][3] >= 'A' && Context[i][3] <= 'Z'&& Context[i][3]!= Context[i][0])
{
if (-1==FIRSTVT[Context[i][0]].find(Context[i][3]))
{
FIRSTVT[Context[i][0]] += Context[i][3];
}
}
}
//输出
cout << endl;
cout << "中间结果" << endl;
map<char, string>::iterator it;
for (it = FIRSTVT.begin(); it != FIRSTVT.end(); it++)
{
cout << it->first << ": " << it->second << endl;
}
}
//计算FIRSTVT集,two
void CalFIRSTVT(char start)
{
for (int i = 0; i <FIRSTVT[start].size(); i++)
{
if (FIRSTVT[start][i]>= 'A' && FIRSTVT[start][i] <= 'Z')
{
char c = FIRSTVT[start][i];
FIRSTVT[start].erase(i, 1);
FIRSTVT[start] += FIRSTVT[c];
i = 0;
}
}
}
//计算FIRSTVT,Finally
void AllCalFIRSTVT()
{
cout <<endl;
cout << "FIRSTVT集" << endl;
map<char, string>::iterator it;
for (it = FIRSTVT.begin(); it != FIRSTVT.end(); it++)
{
CalFIRSTVT(it->first);
cout << it->first<<" 的FIRSVT集: "<< FIRSTVT[it->first] << endl;
}
}
//计算LASTVT集,one
void InitLASTVT()
{
for (int i = 0; i < Context.size(); i++)
{
if ((Context[i][Context[i].size()-1] >= 'a' && Context[i][Context[i].size() - 1] <= 'z') || Context[i][Context[i].size() - 1] == '(' || Context[i][Context[i].size() - 1] == ')' || Context[i][Context[i].size() - 1] == '+' || Context[i][Context[i].size() - 1] == '*' || Context[i][Context[i].size() - 1] == '!' || Context[i][Context[i].size() - 1] == '#')
{
LASTVT[Context[i][0]] += Context[i][Context[i].size() - 1];
}
if (Context[i].size() >= 5)
{
if ((Context[i][Context[i].size() - 1] >= 'A' && Context[i][Context[i].size() - 1] <= 'Z') && ((Context[i][Context[i].size() - 2] >= 'a' && Context[i][Context[i].size() - 2] <= 'z') || Context[i][Context[i].size() - 2] == '(' || Context[i][Context[i].size() - 2] == ')' || Context[i][Context[i].size() - 2] == '+' || Context[i][Context[i].size() - 2] == '*' || Context[i][Context[i].size() - 2] == '!' || Context[i][Context[i].size() - 2] == '#'))
{
LASTVT[Context[i][0]] += Context[i][Context[i].size() - 2];
}
}
if (Context[i][Context[i].size() - 1] >= 'A' && Context[i][Context[i].size() - 1] <= 'Z' && Context[i][Context[i].size() - 1] != Context[i][0])
{
if (-1 ==LASTVT[Context[i][0]].find(Context[i][Context[i].size() - 1]))
{
LASTVT[Context[i][0]] += Context[i][Context[i].size() - 1];
}
}
}
//输出
cout << endl;
cout << "中间结果" << endl;
map<char, string>::iterator it;
for (it =LASTVT.begin(); it != LASTVT.end(); it++)
{
cout << it->first << ": " << it->second << endl;
}
}
//计算LASTVT集,two
void CalLASTVT(char start)
{
for (int i = 0; i < LASTVT[start].size(); i++)
{
if (LASTVT[start][i] >= 'A' && LASTVT[start][i] <= 'Z')
{
char c = LASTVT[start][i];
LASTVT[start].erase(i, 1);
LASTVT[start] += LASTVT[c];
i = 0;
}
}
//cout << LASTVT[start] << endl;
}
//计算FIRSTVT,Finally
void AllCalLASTVT()
{
cout << endl;
cout << "LASTVT集" << endl;
map<char, string>::iterator it;
for (it = LASTVT.begin(); it != LASTVT.end(); it++)
{
CalLASTVT(it->first);
cout << it->first << " 的LASTVT集: " << LASTVT[it->first] << endl;
}
}
//生成算符优先关系表
void CalOperatorTable()
{
for (int i = 0; i < Context.size(); i++)
{
for (int j = 3; j < Context[i].size(); j++)
{
if (Context[i].size() >= 5&&j<(Context[i].size()-1))
{
if (((Context[i][j] >= 'a' && Context[i][j] <= 'z') || Context[i][j] == '('
|| Context[i][j] == ')' || Context[i][j] == '+' || Context[i][j] == '*'
|| Context[i][j] == '!' || Context[i][j] == '#')
&& Context[i][j + 1] >= 'A' && Context[i][j + 1] <= 'Z')
{
for (int k = 0; k < FIRSTVT[Context[i][j + 1]].size(); k++)
{
OperatorTable[Context[i][j]][ FIRSTVT[Context[i][j + 1]][k] ] = '<';
}
}
if (((Context[i][j+1] >= 'a' && Context[i][j + 1] <= 'z') || Context[i][j + 1] == '('
|| Context[i][j + 1] == ')' || Context[i][j + 1] == '+' || Context[i][j + 1] == '*'
|| Context[i][j + 1] == '!' || Context[i][j + 1] == '#') && Context[i][j] >= 'A'
&& Context[i][j] <= 'Z')
{
for (int k = 0; k < LASTVT[Context[i][j]].size(); k++)
{
OperatorTable[LASTVT[Context[i][j]][k]][Context[i][j+1]] = '>';
}
}
}
if (Context[i].size() >= 5&&j < (Context[i].size() -1))
{
if (((Context[i][j] >= 'a' && Context[i][j] <= 'z') || Context[i][j] == '(' || Context[i][j] == ')'
|| Context[i][j] == '+' || Context[i][j] == '*' || Context[i][j] == '!' || Context[i][j] == '#')
&& ((Context[i][j+1] >= 'a' && Context[i][j+1] <= 'z') || Context[i][j+1] == '('
|| Context[i][j+1] == ')' || Context[i][j+1] == '+' || Context[i][j+1] == '*'
|| Context[i][j+1] == '!' || Context[i][j+1] == '#'))
{
OperatorTable[Context[i][j]][Context[i][j + 1]] = '=';
}
}
if (Context[i].size() >= 6 && j < (Context[i].size() - 2))
{
if (((Context[i][j] >= 'a' && Context[i][j] <= 'z') || Context[i][j] == '(' || Context[i][j] == ')'
|| Context[i][j] == '+' || Context[i][j] == '*' || Context[i][j] == '!' || Context[i][j] == '#')
&& ((Context[i][j + 2] >= 'a' && Context[i][j + 2] <= 'z') || Context[i][j + 2] == '(' ||
Context[i][j + 2] == ')' || Context[i][j + 2] == '+' || Context[i][j + 2] == '*' ||
Context[i][j + 2] == '!' || Context[i][j + 2] == '#')
&& (Context[i][j + 1] >= 'A' && Context[i][j + 1] <= 'Z'))
{
OperatorTable[Context[i][j]][Context[i][j + 2]] = '=';
}
}
}
}
cout << endl;
cout << "算符优先关系表" << endl;
map<char, map<char, char>>::iterator it;
for (it = OperatorTable.begin(); it != OperatorTable.end(); it++)
{
map<char, char>::iterator it2;
for (it2 = it->second.begin(); it2 != it->second.end(); it2++)
{
cout << it->first << " " << it2->first << " " << it2->second << endl;
}
}
}
//规约
void GY(string s)
{
string stks;
stks += '#';
for (int i = 0; i < s.size(); i++)
{
int len = stks.size();
string output;
while (isalpha(stks[len - 1])&&stks[len-1]!='i')
{
len--;
}
char c = stks[len - 1];
if (OperatorTable[c][s[i]] != '>' && OperatorTable[c][s[i]] != '<' && OperatorTable[c][s[i]] != '=')
{
cout << "失败" << endl;
break;
}
if (OperatorTable[c][s[i]] == '<')
{
for (int j = i; j < s.size(); j++) { output += s[j]; }
cout << stks << " " << '<' << " " << output << " " << "移进" << endl;
stks += s[i];
continue;
}
if (OperatorTable[c][s[i]] == '>')
{
for (int j = i; j < s.size(); j++) { output += s[j]; }
cout << stks << " " << '>' << " " << output << " " << "规约" << endl;
for (int k = stks.size() - 1; k >= 0; k--)
{
if (stks[k] == 'i')
{
stks[k] = 'E';
i--;
break;
}
if ((stks[k] == '*'|| stks[k] == '+') &&stks[k+1]=='E'&&stks[k-1]=='E')
{
stks.erase(k - 1, 3);
stks += 'E';
i--;
break;
}
if (stks[k] == '(' && stks[k + 2] == ')')
{
stks.erase(k, 3);
stks += 'E';
i--;
break;
}
}
continue;
}
if (OperatorTable[c][s[i]] == '=')
{
int num = 0;
for (int j = i; j < s.size(); j++) { output += s[j]; }
for (int t = 0; t < stks.size(); t++)
{
if (!isalpha(stks[t]))
{
num++;
}
}
if (num==1)
{
cout << stks << " " << '=' << " " << output << " " << "接受" << endl;
break;
}
else
{
cout << stks << " " << '<' << " " << output << " " << "移进" << endl;
stks += s[i];
}
}
}
}
int main()
{
//初始化
InputContext();
//FIRSTVT集
InitFIRSTVT();
AllCalFIRSTVT();
//LASTVT集
InitLASTVT();
AllCalLASTVT();
//算符优先关系表
CalOperatorTable();
//分析
cout << endl;
cout << "请输入串" << endl;
string in;
cin >> in;
cout << endl;
GY(in);
return 0;
}