当前位置:   article > 正文

编译原理实验Sicily--输入输出LL(1)语法分析程序_sicily 文法推导

sicily 文法推导

Description

 输入开始符号,非终结符,终结符,产生式,LL(1)分析表
输出LL(1)分析表

Input

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

Output

 输出推导过程,每一步一行,中间“ & ”前是已经识别的子串,后是栈中信息。

Sample InputCopy

  1. E
  2. 6 E A T B F D
  3. 9 + - * / ( ) x y z
  4. 13
  5. 1 E TA
  6. 2 A +TA
  7. 3 A -TA
  8. 4 A k
  9. 5 T FB
  10. 6 B *FB
  11. 7 B /FB
  12. 8 B k
  13. 9 F (E)
  14. 10 F D
  15. 11 D x
  16. 12 D y
  17. 13 D z
  18. 25
  19. 1 E ( 1
  20. 2 E x 1
  21. 3 E y 1
  22. 4 E z 1
  23. 5 A + 2
  24. 6 A - 3
  25. 7 A ) 4
  26. 8 A # 4
  27. 9 T ( 5
  28. 10 T x 5
  29. 11 T y 5
  30. 12 T z 5
  31. 13 B + 8
  32. 14 B - 8
  33. 15 B * 6
  34. 16 B / 7
  35. 17 B ) 8
  36. 18 B # 8
  37. 19 F ( 9
  38. 20 F x 10
  39. 21 F y 10
  40. 22 F z 10
  41. 23 D x 11
  42. 24 D y 12
  43. 25 D z 13
  44. (x+(y-x*z)*(y+x*z))+x/z#

Sample OutputCopy

  1. # & E#
  2. # & TA#
  3. # & FBA#
  4. # & (E)BA#
  5. #( & E)BA#
  6. #( & TA)BA#
  7. #( & FBA)BA#
  8. #( & DBA)BA#
  9. #( & xBA)BA#
  10. #(x & BA)BA#
  11. #(x & A)BA#
  12. #(x & +TA)BA#
  13. #(x+ & TA)BA#
  14. #(x+ & FBA)BA#
  15. #(x+ & (E)BA)BA#
  16. #(x+( & E)BA)BA#
  17. #(x+( & TA)BA)BA#
  18. #(x+( & FBA)BA)BA#
  19. #(x+( & DBA)BA)BA#
  20. #(x+( & yBA)BA)BA#
  21. #(x+(y & BA)BA)BA#
  22. #(x+(y & A)BA)BA#
  23. #(x+(y & -TA)BA)BA#
  24. #(x+(y- & TA)BA)BA#
  25. #(x+(y- & FBA)BA)BA#
  26. #(x+(y- & DBA)BA)BA#
  27. #(x+(y- & xBA)BA)BA#
  28. #(x+(y-x & BA)BA)BA#
  29. #(x+(y-x & *FBA)BA)BA#
  30. #(x+(y-x* & FBA)BA)BA#
  31. #(x+(y-x* & DBA)BA)BA#
  32. #(x+(y-x* & zBA)BA)BA#
  33. #(x+(y-x*z & BA)BA)BA#
  34. #(x+(y-x*z & A)BA)BA#
  35. #(x+(y-x*z & )BA)BA#
  36. #(x+(y-x*z) & BA)BA#
  37. #(x+(y-x*z) & *FBA)BA#
  38. #(x+(y-x*z)* & FBA)BA#
  39. #(x+(y-x*z)* & (E)BA)BA#
  40. #(x+(y-x*z)*( & E)BA)BA#
  41. #(x+(y-x*z)*( & TA)BA)BA#
  42. #(x+(y-x*z)*( & FBA)BA)BA#
  43. #(x+(y-x*z)*( & DBA)BA)BA#
  44. #(x+(y-x*z)*( & yBA)BA)BA#
  45. #(x+(y-x*z)*(y & BA)BA)BA#
  46. #(x+(y-x*z)*(y & A)BA)BA#
  47. #(x+(y-x*z)*(y & +TA)BA)BA#
  48. #(x+(y-x*z)*(y+ & TA)BA)BA#
  49. #(x+(y-x*z)*(y+ & FBA)BA)BA#
  50. #(x+(y-x*z)*(y+ & DBA)BA)BA#
  51. #(x+(y-x*z)*(y+ & xBA)BA)BA#
  52. #(x+(y-x*z)*(y+x & BA)BA)BA#
  53. #(x+(y-x*z)*(y+x & *FBA)BA)BA#
  54. #(x+(y-x*z)*(y+x* & FBA)BA)BA#
  55. #(x+(y-x*z)*(y+x* & DBA)BA)BA#
  56. #(x+(y-x*z)*(y+x* & zBA)BA)BA#
  57. #(x+(y-x*z)*(y+x*z & BA)BA)BA#
  58. #(x+(y-x*z)*(y+x*z & A)BA)BA#
  59. #(x+(y-x*z)*(y+x*z & )BA)BA#
  60. #(x+(y-x*z)*(y+x*z) & BA)BA#
  61. #(x+(y-x*z)*(y+x*z) & A)BA#
  62. #(x+(y-x*z)*(y+x*z) & )BA#
  63. #(x+(y-x*z)*(y+x*z)) & BA#
  64. #(x+(y-x*z)*(y+x*z)) & A#
  65. #(x+(y-x*z)*(y+x*z)) & +TA#
  66. #(x+(y-x*z)*(y+x*z))+ & TA#
  67. #(x+(y-x*z)*(y+x*z))+ & FBA#
  68. #(x+(y-x*z)*(y+x*z))+ & DBA#
  69. #(x+(y-x*z)*(y+x*z))+ & xBA#
  70. #(x+(y-x*z)*(y+x*z))+x & BA#
  71. #(x+(y-x*z)*(y+x*z))+x & /FBA#
  72. #(x+(y-x*z)*(y+x*z))+x/ & FBA#
  73. #(x+(y-x*z)*(y+x*z))+x/ & DBA#
  74. #(x+(y-x*z)*(y+x*z))+x/ & zBA#
  75. #(x+(y-x*z)*(y+x*z))+x/z & BA#
  76. #(x+(y-x*z)*(y+x*z))+x/z & A#
  77. #(x+(y-x*z)*(y+x*z))+x/z & #

Problem Source: 自上而下语法分析

程序:

  1. #include <iostream>
  2. #include <stack>
  3. using namespace std;
  4. string S=""; //开始符号
  5. struct { int number;string sign[20]; string res_LL[20][20]; } not_endsign={0}; //非终结符
  6. struct { int number;string sign[20]; } end_sign={0}; //终结符
  7. struct { int number;int order[100]; string left[100],right[100]; } production={0}; //产生式
  8. struct { int number;int order[100]; string rank[100],col[100];int production_num[100]; } LL={0}; //LL(1)分析表
  9. string test;
  10. void input();
  11. void print(string left,stack<string > right);
  12. int main(){
  13. input();
  14. //定义输出结果
  15. string left;
  16. stack<string > right;
  17. right.push(S) ;
  18. print(left,right);
  19. while(!right.empty()){
  20. string top = right.top();
  21. string firstletter = test.substr(0,1);
  22. if(top==firstletter){
  23. left += top;
  24. test = test.substr(1,test.length()-1 );
  25. right.pop();
  26. print(left,right);
  27. continue;
  28. }
  29. else {
  30. //替换掉 top
  31. for(int i=0;i<LL.number;i++){
  32. if(LL.rank[i]==top &&LL.col[i]==firstletter ){
  33. right.pop();
  34. string temp = production.right[LL.production_num[i]-1 ];
  35. if(temp=="k") continue;
  36. while(temp.length()!=0){
  37. string temp0 = temp.substr( temp.length()-1,1);
  38. right.push(temp0);
  39. temp = temp.substr(0,temp.length()-1);
  40. }
  41. }
  42. }
  43. }
  44. print(left,right);
  45. }
  46. return 0;
  47. }
  48. void print(string left,stack<string > right){
  49. cout<<"#"<<left<<" & ";
  50. string temp="";
  51. while(!right.empty()){
  52. cout<<right.top();
  53. temp+=right.top();
  54. right.pop();
  55. }
  56. cout<<"#"<<endl;
  57. while(temp.length()!=0){
  58. string temp0 = temp.substr( temp.length()-1,1);
  59. right.push(temp0);
  60. temp = temp.substr(0,temp.length()-1);
  61. }
  62. return ;
  63. }
  64. void input(){
  65. cin>>S;
  66. cin>>not_endsign.number;
  67. for(int i=0;i<not_endsign.number;i++){
  68. cin>>not_endsign.sign[i];
  69. }
  70. cin>>end_sign.number;
  71. for(int i=0;i<end_sign.number;i++){
  72. cin>>end_sign.sign[i];
  73. }
  74. cin>>production.number;
  75. for(int i=0;i<production.number;i++){
  76. cin>>production.order[i]>>production.left[i]>>production.right[i];
  77. }
  78. cin>>LL.number;
  79. for(int i=0;i<LL.number;i++){
  80. cin>>LL.order[i]>>LL.rank[i]>>LL.col[i]>>LL.production_num[i];
  81. }
  82. cin>>test;
  83. return ;
  84. }

 

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

闽ICP备14008679号