当前位置:   article > 正文

sicily1000. 输入输出LL(1)语法分析程序_语法分析输入输出

语法分析输入输出

1000. 输入输出LL(1)语法分析程序

题目:
Time Limit: 1sec    Memory Limit:256MB
Description

 输入开始符号,非终结符,终结符,产生式,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  

Input

 输入开始符号;
非终结符个数,非终结符,空格符分隔;
终结符个数,终结符,空格符分隔;
产生式的个数,各产生式的序号,产生式的左边和右边符号,空格符分隔;
LL(1)分析表中的产生式个数,序号,行符号,列符号,产生式编号,空格符分隔;

Output

 第一行:空,安终结符循序输出终结符,结束符‘#’,每个符号占5格;
其余行:非终结符符号,各对应终结符的产生式的右边,每个符号占5格;

Sample Input
 Copy sample input to clipboard
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
Sample Output
         +    -    *    /    (    )    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     


代码:
  1. #include <cstring>
  2. #include <string.h>
  3. #include <iostream>
  4. #include <iomanip>
  5. #include <vector>
  6. using namespace std;
  7. struct v {
  8. char vn;
  9. string vt;
  10. //v(string vn1, string vt1) {vn = vn1, vt = vt1;}
  11. };
  12. struct sel {
  13. char v, t;
  14. int ch;
  15. };
  16. vector<char> s1,s2; //s1非终结符, s2终结符
  17. vector<v> s3;
  18. vector<sel> s4;
  19. string find(char v, char t) {
  20. for (auto i : s4) {
  21. if (i.v == v&&i.t == t) return s3[i.ch-1].vt;
  22. }
  23. return "";
  24. }
  25. int main() {
  26. string start;
  27. cin >> start;
  28. int s1num, s2num, s3num,s4num;
  29. cin >> s1num;
  30. for (int i = 0; i < s1num; i++) {
  31. char tmp;
  32. cin >> tmp;
  33. s1.push_back(tmp);
  34. }
  35. cin >> s2num;
  36. for (int i = 1; i <= s2num; i++) {
  37. char tmp;
  38. cin >> tmp;
  39. s2.push_back(tmp);
  40. }
  41. s2num++;
  42. s2.push_back('#');
  43. cin >> s3num;
  44. for (int i = 1; i <= s3num; i++) {
  45. char vn;
  46. string vt;
  47. int n;
  48. cin >> n;
  49. cin >> vn;
  50. cin >> vt;
  51. v tmp;
  52. tmp.vn = vn, tmp.vt = vt;
  53. s3.push_back(tmp);
  54. }
  55. cin >> s4num;
  56. for (int i = 1; i <= s4num; i++) {
  57. char v, t;
  58. int num, n;
  59. cin >> n,cin >> v >> t, cin >> num;
  60. sel tmp;
  61. tmp.v = v, tmp.t = t, tmp.ch = num;
  62. s4.push_back(tmp);
  63. }
  64. int widthOccupy = 5;
  65. cout << setw(widthOccupy) << ' ';
  66. for (int i = 0; i < s2num; ++i)
  67. cout << setw(widthOccupy) << s2[i];
  68. cout << endl;
  69. for (int i = 0; i < s1num; ++i) {
  70. cout << setw(widthOccupy) << s1[i];
  71. for (int j = 0; j < s2num; ++j) {
  72. cout << setw(widthOccupy) << find(s1[i], s2[j]);
  73. }
  74. cout << endl;
  75. }
  76. return 0;
  77. }


声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/644589
推荐阅读
相关标签
  

闽ICP备14008679号