当前位置:   article > 正文

制度下降算法c语言

制度下降算法c语言
  1. #include<stdio.h>
  2. #include<string.h>
  3. int location=0; //遍历字符串的当前位置
  4. char arr[20]="id+id*id#";
  5. void error(); //错误提示函数
  6. /*
  7. 每一个非终结符都构造一个函数
  8. */
  9. void E(char t);
  10. void Ep(char t);
  11. void T(char t);
  12. void Tp(char t);
  13. void F(char t);
  14. void E(char t){
  15. if(t=='('){
  16. printf("E->TE'");
  17. printf("\n");
  18. T(arr[location]);
  19. Ep(arr[location]);
  20. }else if(t=='i'){
  21. printf("E->TE'");
  22. printf("\n");
  23. T(arr[location]);
  24. Ep(arr[location]);
  25. }
  26. else{
  27. error();
  28. location++;
  29. E(arr[location]);
  30. }
  31. }
  32. void T(char t){
  33. if(t=='i'){
  34. printf("T->FT'");
  35. printf("\n");
  36. F(arr[location]);
  37. Tp(arr[location]);
  38. }else if(t=='('){
  39. printf("T->FT'");
  40. printf("\n");
  41. F(arr[location]);
  42. Tp(arr[location]);
  43. }else{
  44. error();
  45. location++;
  46. T(arr[location]);
  47. }
  48. }
  49. void Ep(char t){
  50. if(t=='+'){
  51. printf("E'->+TE'");
  52. printf("\n");
  53. location++;
  54. T(arr[location]);
  55. Ep(arr[location]);
  56. }else if(t==')'){
  57. printf("E'->&");
  58. printf("\n");
  59. }else if(t=='#'){
  60. }
  61. else{
  62. error();
  63. if(t=='i')
  64. location+=2;
  65. else
  66. location++;
  67. Ep(arr[location]);
  68. }
  69. }
  70. void Tp(char t){
  71. switch(t){
  72. case '+':
  73. printf("T'->&");
  74. printf("\n");
  75. break;
  76. case '*':
  77. printf("T'->*FT'");
  78. printf("\n");
  79. location++;
  80. F(arr[location]);
  81. Tp(arr[location]);
  82. break;
  83. case ')':
  84. printf("T'->&");
  85. printf("\n");
  86. break;
  87. case '#':
  88. printf("T'->&");
  89. printf("\n");
  90. break;
  91. default:
  92. error();
  93. if(t=='i')
  94. location+=2;
  95. else
  96. location++;
  97. Tp(arr[location]);
  98. break;
  99. }
  100. }
  101. void F(char t){
  102. if(t=='i'){
  103. printf("F->id");
  104. printf("\n");
  105. location+=2;
  106. }else if(t=='('){
  107. printf("F->(E)");
  108. printf("\n");
  109. }else{
  110. error();
  111. location++;
  112. F(arr[location]);
  113. }
  114. }
  115. void error(){
  116. printf("有一个错误,略过当前词法记号");
  117. printf("\n");
  118. }
  119. int main(){
  120. E(arr[location]);
  121. }

运行结果

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

闽ICP备14008679号