Description
输入开始符号,非终结符,终结符,产生式,LL(1)分析表
输出LL(1)分析表
此题需要提交实验报告;“实验报告用“学号+姓名+72”
Input
输入开始符号;
非终结符个数,非终结符,空格符分隔;
终结符个数,终结符,空格符分隔;
产生式的个数,各产生式的序号,产生式的左边和右边符号,空格符分隔;
LL(1)分析表中的产生式个数,序号,行符号,列符号,产生式编号,空格符分隔;
输入一个算术式符号串,用#结束
Output
输出推导过程,每一步一行,中间“ & ”前是已经识别的子串,后是栈中信息。
Sample Input
E
6 E A T B F D
9 + - * / ( ) x y z
13
1 E TA
2 A +TA
3 A -TA
4 A k
5 T FB
6 B *FB
7 B /FB
8 B k
9 F (E)
10 F D
11 D x
12 D y
13 D z
25
1 E ( 1
2 E x 1
3 E y 1
4 E z 1
5 A + 2
6 A - 3
7 A ) 4
8 A # 4
9 T ( 5
10 T x 5
11 T y 5
12 T z 5
13 B + 8
14 B - 8
15 B * 6
16 B / 7
17 B ) 8
18 B # 8
19 F ( 9
20 F x 10
21 F y 10
22 F z 10
23 D x 11
24 D y 12
25 D z 13
(x+(y-x*z)*(y+x*z))+x/z#
Sample Output:
# & E#
# & TA#
# & FBA#
# & (E)BA#
#( & E)BA#
#( & TA)BA#
#( & FBA)BA#
#( & DBA)BA#
#( & xBA)BA#
#(x & BA)BA#
#(x & A)BA#
#(x & +TA)BA#
#(x+ & TA)BA#
#(x+ & FBA)BA#
#(x+ & (E)BA)BA#
#(x+( & E)BA)BA#
#(x+( & TA)BA)BA#
#(x+( & FBA)BA)BA#
#(x+( & DBA)BA)BA#
#(x+( & yBA)BA)BA#
#(x+(y & BA)BA)BA#
#(x+(y & A)BA)BA#
#(x+(y & -TA)BA)BA#
#(x+(y- & TA)BA)BA#
#(x+(y- & FBA)BA)BA#
#(x+(y- & DBA)BA)BA#
#(x+(y- & xBA)BA)BA#
#(x+(y-x & BA)BA)BA#
#(x+(y-x & *FBA)BA)BA#
#(x+(y-x* & FBA)BA)BA#
#(x+(y-x* & DBA)BA)BA#
#(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(y+x*z))+x/ & FBA#
#(x+(y-x*z)*(y+x*z))+x/ & DBA#
#(x+(y-x*z)*(y+x*z))+x/ & zBA#
#(x+(y-x*z)*(y+x*z))+x/z & BA#
#(x+(y-x*z)*(y+x*z))+x/z & A#
#(x+(y-x*z)*(y+x*z))+x/z & #
本程序主要有由三个模块组成
模块一功能:输入数据
模块二功能:生成LL分析表
模块三功能:运行总控算法
Code:
#include <iostream>
#include <vector>
#include <iomanip>
using namespace std;
bool findVT(const std::vector<char> &VT, char ch){
for(int i=0;i<VT.size();i++)
if(VT[i] == ch)
return true;
return false;
}
int main(int argc, char const *argv[])
{
std::vector<char> start;
std::vector<char> VN;
std::vector<char> VT;
std::vector<char> PL;
std::vector<string> PR;
// input the data
char char_helper;
int int_helper;
cin >> char_helper;
start.push_back(char_helper);
cin >> int_helper;
for(int i=0;i<int_helper;i++){
cin >> char_helper;
VN.push_back(char_helper);
}
cin >> int_helper;
for(int i=0;i<int_helper;i++){
cin >> char_helper;
VT.push_back(char_helper);
}
VT.push_back('#');
cin >> int_helper;
for(int i=0;i<int_helper;i++){
int temp;
cin >> temp;
cin >> char_helper;
PL.push_back(char_helper);
string string_helper;
cin >> string_helper;
PR.push_back(string_helper);
}
// complete the table
std::vector<string> vector_helper(VT.size(),"");
std::vector<std::vector<string> > table;
for(int i=0;i<VN.size();i++)
table.push_back(vector_helper);
cin >> int_helper;
for(int i=0;i<int_helper;i++){
int temp;
cin >> temp;
char ch1;
char ch2;
int index, row, col;
cin >> ch1 >> ch2 >> index;
for(int i=0;i<VN.size();i++){
if(VN[i] == ch1){
row = i;
break;
}
}
for(int i=0;i<VT.size();i++){
if(VT[i] == ch2){
col = i;
break;
}
}
table[row][col] = PR[index-1];
}
// run the algorithm
string equation;
cin >> equation;
string S;
string visited;
int ptr = 0;
S.push_back(start[0]);
cout << "# & E#" << endl;
while(equation[ptr] != '#'){
char ch = S[0];
S.erase(S.begin());
if(ch == equation[ptr]){
visited.push_back(ch);
ptr++;
}
else{
int row,col;
for(int i=0;i<VN.size();i++){
if(ch == VN[i]){
row = i;
break;
}
}
for(int i=0;i<VT.size();i++){
if(equation[ptr] == VT[i]){
col = i;
break;
}
}
string temp = table[row][col];
if(temp != "k")
S = temp + S;
}
cout << "#" << visited << " & " << S << "#" << endl;
}
while(S.size()){
S.erase(S.begin());
cout << "#" << visited << " & " << S << "#" << endl;
}
return 0;
}