赞
踩
输入开始符号,非终结符,终结符,产生式,LL(1)分析表
输出LL(1)分析表
G[E]:E →E+T | E-T | T
T →T*F | T/F | F
F →(E) | D
D →x | y | z
消除左递归G1[E]:
E →TA
A →+TA | -TA | e
T →FB
B →*FB | /FB | e
F →(E) | D
D →x | y | z
输入开始符号;
非终结符个数,非终结符,空格符分隔;
终结符个数,终结符,空格符分隔;
产生式的个数,各产生式的序号,产生式的左边和右边符号,空格符分隔;
LL(1)分析表中的产生式个数,序号,行符号,列符号,产生式编号,空格符分隔;
第一行:空,安终结符循序输出终结符,结束符‘#’,每个符号占5格;
其余行:非终结符符号,各对应终结符的产生式的右边,每个符号占5格;
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 z # E TA TA TA TA A +TA -TA k k T FB FB FB FB B k k *FB /FB k k F (E) D D D D x y z
- #include <cstring>
- #include <string.h>
- #include <iostream>
- #include <iomanip>
- #include <vector>
- using namespace std;
-
- struct v {
- char vn;
- string vt;
- //v(string vn1, string vt1) {vn = vn1, vt = vt1;}
- };
-
- struct sel {
- char v, t;
- int ch;
- };
-
- vector<char> s1,s2; //s1非终结符, s2终结符
- vector<v> s3;
- vector<sel> s4;
-
- string find(char v, char t) {
- for (auto i : s4) {
- if (i.v == v&&i.t == t) return s3[i.ch-1].vt;
- }
- return "";
- }
-
- int main() {
- string start;
- cin >> start;
- int s1num, s2num, s3num,s4num;
- cin >> s1num;
- for (int i = 0; i < s1num; i++) {
- char tmp;
- cin >> tmp;
- s1.push_back(tmp);
- }
- cin >> s2num;
- for (int i = 1; i <= s2num; i++) {
- char tmp;
- cin >> tmp;
- s2.push_back(tmp);
- }
- s2num++;
- s2.push_back('#');
- cin >> s3num;
- for (int i = 1; i <= s3num; i++) {
- char vn;
- string vt;
- int n;
- cin >> n;
- cin >> vn;
- cin >> vt;
- v tmp;
- tmp.vn = vn, tmp.vt = vt;
- s3.push_back(tmp);
- }
- cin >> s4num;
- for (int i = 1; i <= s4num; i++) {
- char v, t;
- int num, n;
- cin >> n,cin >> v >> t, cin >> num;
- sel tmp;
- tmp.v = v, tmp.t = t, tmp.ch = num;
- s4.push_back(tmp);
- }
- int widthOccupy = 5;
- cout << setw(widthOccupy) << ' ';
- for (int i = 0; i < s2num; ++i)
- cout << setw(widthOccupy) << s2[i];
- cout << endl;
- for (int i = 0; i < s1num; ++i) {
- cout << setw(widthOccupy) << s1[i];
- for (int j = 0; j < s2num; ++j) {
- cout << setw(widthOccupy) << find(s1[i], s2[j]);
- }
- cout << endl;
- }
- return 0;
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。