//文法:
//(1)S->E //拓广文法
//(2)E->E+T
//(3)E->T
//(4)T->T*F
//(5)T->F
//(6)F->(E)
//(7)F->i
构造SLR(1)分析表,对给定的句子 进行SLR(1)分析法
| Time Limit | $1$ 秒/Second(s) | Memory Limit | $512$ 兆字节/Megabyte(s) |
| 提交总数 | $880$ | 正确数量 | $343$ | "
| 裁判形式 | 标准裁判/Standard Judge | 我的状态 | 尚未尝试 |
| 难度 | 分类标签 | 编译原理 |
//文法:
//(1)S->E //拓广文法
//(2)E->E+T
//(3)E->T
//(4)T->T*F
//(5)T->F
//(6)F->(E)
//(7)F->i
构造SLR(1)分析表,对给定的句子 进行SLR(1)分析法
i#
1 0 # i# S5
2 05 #i # r6 3
3 03 #F # r4 2
4 02 #T # r2 1
5 01 #E # acc
参考格式代码
int i = 0;
while (true) {
string t = st.back();
//assert(isdigit(t) == true);
string y = table[stoi(t)][p[s[i]]];
if (y[0] == 's') {
output(st, i);
y[0] = toupper(y[0]);
cout << "\t\t" << y << "\n";
st.push_back(s.substr(i, 1));//先压符号
st.push_back(y.substr(1));//在压状态
i++;
} else if (y[0] == 'r') {//根据r? 归约
output(st, i);
int cnt = 0;
Ga g1 = g[stoi(y.substr(1))];//要归约的产生式
while (cnt < 2 * g1.right.size()) {//弹出 2*right.size()
st.pop_back();
cnt++;
}
string c1 = g1.left;//产生式的左部
string y1 = table[stoi(st.back())][p[c1[0]]];//goto表
st.push_back(c1);//先压符号 压入左部
st.push_back(y1);//在压状态
cout << "\t\t" << y << "\t" << y1 << "\n";
} else if (y == "acc") {
output(st, i);
cout << "\t\t" << "acc" << "\n";
break;
} else {
output(st, i);
cout << "\t\t" << "error" << "\n";
break;
}
}