Description
输入开始符号,非终结符,终结符,产生式
输出简单优先分析过程
以简单优先算术表达式G[E]: 为例
E→E+T | E-T | A
A→T
T→F*T | F/T | F
F→(B) | D
B→E
D→x | y | z
Input
输入开始符号;
非终结符个数,非终结符,空格符分隔;
终结符个数,终结符,空格符分隔;
产生式的个数,各产生式的序号,产生式的左边和右边符号,空格符分隔;
简单优先分析表方阵的行列数,简单优先分析表,空格符分隔;(0错误;1小于;2等于;3大于)
输入分析字符串,#结束
Output
输出分析过程;
用“ $ ”分隔,左边表示栈底到栈顶符号;右边表示尚未分析的字符串。
Sample InputCopy
E
6 E A T F B D
9 + - * / ( ) x y z
13
1 E E+T
2 E E-T
3 E A
4 A T
5 T F*T
6 T F/T
7 T F
8 F (B)
9 F D
10 B E
11 D x
12 D y
13 D z
15
0 0 0 0 0 0 2 2 0 0 0 3 0 0 0
0 0 0 0 0 0 3 3 0 0 0 3 0 0 0
0 0 0 0 0 0 3 3 0 0 0 3 0 0 0
0 0 0 0 0 0 3 3 2 2 0 3 0 0 0
0 0 0 0 0 0 0 0 0 0 0 2 0 0 0
0 0 0 0 0 0 3 3 3 3 0 3 0 0 0
0 0 2 1 0 1 0 0 0 0 1 0 1 1 1
0 0 2 1 0 1 0 0 0 0 1 0 1 1 1
0 0 2 1 0 1 0 0 0 0 1 0 1 1 1
0 0 2 1 0 1 0 0 0 0 1 0 1 1 1
1 1 1 1 2 1 0 0 0 0 1 0 1 1 1
0 0 0 0 0 0 3 3 3 3 0 3 0 0 0
0 0 0 0 0 0 3 3 3 3 0 3 0 0 0
0 0 0 0 0 0 3 3 3 3 0 3 0 0 0
0 0 0 0 0 0 3 3 3 3 0 3 0 0 0
(x+(y-x*z)*(y+x*z))+x/z#
Sample OutputCopy
# & (x+(y-x*z)*(y+x*z))+x/z#
#( & x+(y-x*z)*(y+x*z))+x/z#
#(x & +(y-x*z)*(y+x*z))+x/z#
#(D & +(y-x*z)*(y+x*z))+x/z#
#(F & +(y-x*z)*(y+x*z))+x/z#
#(T & +(y-x*z)*(y+x*z))+x/z#
#(A & +(y-x*z)*(y+x*z))+x/z#
#(E & +(y-x*z)*(y+x*z))+x/z#
#(E+ & (y-x*z)*(y+x*z))+x/z#
#(E+( & y-x*z)*(y+x*z))+x/z#
#(E+(y & -x*z)*(y+x*z))+x/z#
#(E+(D & -x*z)*(y+x*z))+x/z#
#(E+(F & -x*z)*(y+x*z))+x/z#
#(E+(T & -x*z)*(y+x*z))+x/z#
#(E+(A & -x*z)*(y+x*z))+x/z#
#(E+(E & -x*z)*(y+x*z))+x/z#
#(E+(E- & x*z)*(y+x*z))+x/z#
#(E+(E-x & *z)*(y+x*z))+x/z#
#(E+(E-D & *z)*(y+x*z))+x/z#
#(E+(E-F & *z)*(y+x*z))+x/z#
#(E+(E-F* & z)*(y+x*z))+x/z#
#(E+(E-F*z & )*(y+x*z))+x/z#
#(E+(E-F*D & )*(y+x*z))+x/z#
#(E+(E-F*F & )*(y+x*z))+x/z#
#(E+(E-F*T & )*(y+x*z))+x/z#
#(E+(E-T & )*(y+x*z))+x/z#
#(E+(E & )*(y+x*z))+x/z#
#(E+(B & )*(y+x*z))+x/z#
#(E+(B) & *(y+x*z))+x/z#
#(E+F & *(y+x*z))+x/z#
#(E+F* & (y+x*z))+x/z#
#(E+F*( & y+x*z))+x/z#
#(E+F*(y & +x*z))+x/z#
#(E+F*(D & +x*z))+x/z#
#(E+F*(F & +x*z))+x/z#
#(E+F*(T & +x*z))+x/z#
#(E+F*(A & +x*z))+x/z#
#(E+F*(E & +x*z))+x/z#
#(E+F*(E+ & x*z))+x/z#
#(E+F*(E+x & *z))+x/z#
#(E+F*(E+D & *z))+x/z#
#(E+F*(E+F & *z))+x/z#
#(E+F*(E+F* & z))+x/z#
#(E+F*(E+F*z & ))+x/z#
#(E+F*(E+F*D & ))+x/z#
#(E+F*(E+F*F & ))+x/z#
#(E+F*(E+F*T & ))+x/z#
#(E+F*(E+T & ))+x/z#
#(E+F*(E & ))+x/z#
#(E+F*(B & ))+x/z#
#(E+F*(B) & )+x/z#
#(E+F*F & )+x/z#
#(E+F*T & )+x/z#
#(E+T & )+x/z#
#(E & )+x/z#
#(B & )+x/z#
#(B) & +x/z#
#F & +x/z#
#T & +x/z#
#A & +x/z#
#E & +x/z#
#E+ & x/z#
#E+x & /z#
#E+D & /z#
#E+F & /z#
#E+F/ & z#
#E+F/z & #
#E+F/D & #
#E+F/F & #
#E+F/T & #
#E+T & #
#E & #
程序:
#include <iostream>
#include <map>
#include <string>
using namespace std;
int main()
{
map<string,char> map1;
map<char,int> map2;
char start;
cin>>start;
int n,m,t;
cin>>n;
for(int i = 0; i < n; i++)
{
char ch;
cin>>ch;
map2[ch] = i;
}
cin>>m;
for(int i = 0; i < m; i++)
{
char ch;
cin>>ch;
map2[ch] = i + n;
}
cin>>t;
for(int i = 0; i < t; i++)
{
int index;
char ch;
string chan;
cin>>index>>ch>>chan;
map1[chan] = ch;
}
int matrix[101][101] = {0};
int nn;
cin>>nn;
for(int i = 0; i < nn; i++)
{
for(int j = 0; j < nn; j++)
{
cin>>matrix[i][j];
}
}
string sen;
cin>>sen;
sen.erase(sen.size()-1,1);
cout<<"# & "<<sen<<"#"<<endl;
string zuo = "";
while(sen.size() != 0)
{
if(zuo.size() == 0)
{
zuo += sen[0];
sen.erase(0,1);
cout<<"#"<<zuo<<" & "<<sen<<"#"<<endl;
}
else
{
int a = map2[zuo[zuo.size()-1]];
int b = map2[sen[0]];
if(matrix[a][b] == 1 || matrix[a][b] == 2)
{
zuo += sen[0];
sen.erase(0,1);
cout<<"#"<<zuo<<" & "<<sen<<"#"<<endl;
}
else if(matrix[a][b] == 3)
{
while(matrix[a][b] == 3)
{
for(int i = 0; i < zuo.size(); i++)
{
string temp = zuo.substr(i,zuo.size()-i);
if(map1[temp])
{
zuo.erase(i,zuo.size()-i);
zuo += map1[temp];
a = map2[zuo[zuo.size()-1]];
b = map2[sen[0]];
cout<<"#"<<zuo<<" & "<<sen<<"#"<<endl;
break;
}
}
}
}
}
}
while(zuo.size() != 1)
{
for(int i = 0; i < zuo.size(); i++)
{
string temp = zuo.substr(i,zuo.size()-i);
if(map1[temp])
{
zuo.erase(i,zuo.size()-i);
zuo += map1[temp];
cout<<"#"<<zuo<<" & "<<sen<<"#"<<endl;
break;
}
}
}
return 0;
}