经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » C 语言 » 查看文章
编译原理作业(第一次)-完成retinf.c(阉割版)
来源:cnblogs  作者:恶魔人Crybaby  时间:2019/3/7 8:52:58  对本文有异议

首先,作业要求概括如下:

根据前缀表达式文法,实现statements() 和expression() 两个函数。

并且要求使得语义分析在完成分析前缀表达式并输出中间代码的同时,也能够将前缀表达式翻译为中缀表达式, 且要求翻译后的中缀表达式中尽可能少用括号

 

  1. 1 statements -> expression SEMI
  2. 2 | expression SEMI statements
  3. 3
  4. 4 expression -> PLUS expression expression
  5. 5 | MINUS expression expression
  6. 6 | TIMES expression expression
  7. 7 | DIVISION expression expression
  8. 8 | NUM_OR_ID

 

举例如下: 输入"+ a * b c;"时,应输出中缀式为" a + b * c", 而不是"a + (b * c)"或"(a) + (b * c)"等。

最后测试效果如下:

 

 

其中未实现河流命名算法故为阉割版。为方便读者阅读并理解后自行更改,这里只提供初版(low版),其代码如下(DDL后会更新):

 

  1. 1 //retinf.c
  2. 2 #include <stdio.h>
  3. 3 #include <string.h>
  4. 4 #include <stdlib.h>
  5. 5 #include <stdbool.h>
  6. 6
  7. 7 #include "lex.h"
  8. 8
  9. 9 char err_id[] = "error";
  10. 10 char * midexp;
  11. 11 extern char * yytext;
  12. 12
  13. 13 struct YYLVAL {
  14. 14 int last_op; /* last operation of expression
  15. 15 for elimination of redundant parentheses */
  16. 16
  17. 17 char * val; /* 记录表达式中间临时变量 */
  18. 18 char * expr; /* 记录表达式后缀式 */
  19. 19 };
  20. 20
  21. 21 typedef struct YYLVAL Yylval;
  22. 22
  23. 23 Yylval * expression(void);
  24. 24
  25. 25 char *newname(void); /* 在name.c中定义 */
  26. 26
  27. 27 extern void freename(char *name);
  28. 28
  29. 29 void statements(void) {
  30. 30 Yylval *temp;
  31. 31 printf("Please input an infix expression and ending with \";\"\n");
  32. 32 while (!match(EOI)) {
  33. 33
  34. 34 temp = expression();
  35. 35
  36. 36 printf("The Expression Is %s\n", temp->expr);
  37. 37 freename(temp->val);
  38. 38
  39. 39 free(temp->expr);
  40. 40 free(temp);
  41. 41 if (match(SEMI)) {
  42. 42 printf("Please input an infix expression and ending with \";\"\n");
  43. 43 advance();
  44. 44
  45. 45 }
  46. 46 else {
  47. 47 fprintf(stderr, "%d: Inserting missing semicolon\n", yylineno);
  48. 48 }
  49. 49 }
  50. 50 }
  51. 51
  52. 52 Yylval * expression(void) {
  53. 53 Yylval *tempToReturn;
  54. 54 tempToReturn = (Yylval *)malloc(sizeof(Yylval));
  55. 55
  56. 56 Yylval *temp0, *temp1;
  57. 57 while (match(PLUS) || match(MINUS) || match(TIMES) || match(DIVISION)) {
  58. 58
  59. 59 char op = yytext[0];
  60. 60 advance();
  61. 61 temp0 = expression();
  62. 62
  63. 63 temp1 = expression();
  64. 64
  65. 65 bool tempToReturnIsPro = op == '*' || op == '/';
  66. 66 bool temp0IsLower = temp0->last_op == PLUS || temp0->last_op == MINUS;
  67. 67 bool temp1IsLower = temp1->last_op == PLUS || temp1->last_op == MINUS;
  68. 68
  69. 69
  70. 70 if (tempToReturnIsPro) {
  71. 71 if (temp0IsLower && temp1IsLower) {
  72. 72 tempToReturn->expr = (char*)malloc(strlen(temp0->expr) + strlen(temp1->expr) + 12);
  73. 73 sprintf(tempToReturn->expr, "( %s ) %c ( %s )",
  74. 74 temp0->expr, op, temp1->expr);
  75. 75 }
  76. 76 else if (temp0IsLower) {
  77. 77 tempToReturn->expr = (char*)malloc(strlen(temp0->expr) + strlen(temp1->expr) + 8);
  78. 78 sprintf(tempToReturn->expr, "( %s ) %c %s",
  79. 79 temp0->expr, op, temp1->expr);
  80. 80 }
  81. 81 else if (temp1IsLower) {
  82. 82 tempToReturn->expr = (char*)malloc(strlen(temp0->expr) + strlen(temp1->expr) + 8);
  83. 83 sprintf(tempToReturn->expr, "%s %c ( %s )",
  84. 84 temp0->expr, op, temp1->expr);
  85. 85 }
  86. 86 else {
  87. 87 tempToReturn->expr = (char*)malloc(strlen(temp0->expr) + strlen(temp1->expr) + 4);
  88. 88 sprintf(tempToReturn->expr, "%s %c %s",
  89. 89 temp0->expr, op, temp1->expr);
  90. 90 }
  91. 91 }
  92. 92 else {
  93. 93 tempToReturn->expr = (char*)malloc(strlen(temp0->expr) + strlen(temp1->expr) + 4);
  94. 94 sprintf(tempToReturn->expr, "%s %c %s",
  95. 95 temp0->expr, op, temp1->expr);
  96. 96 }
  97. 97 switch (op)
  98. 98 {
  99. 99 case '+':tempToReturn->last_op = PLUS; break;
  100. 100 case '-':tempToReturn->last_op = MINUS; break;
  101. 101 case '*':tempToReturn->last_op = TIMES; break;
  102. 102 case '/':tempToReturn->last_op = DIVISION; break;
  103. 103 default:
  104. 104 break;
  105. 105 }
  106. 106
  107. 107 printf(" %s %c= %s\n", temp0->val, op, temp1->val);
  108. 108 freename(temp1->val);
  109. 109 tempToReturn->val = temp0->val;
  110. 110 return tempToReturn;
  111. 111
  112. 112 }
  113. 113
  114. 114 if (match(NUM_OR_ID)) {
  115. 115 printf(" %s = %0.*s\n", tempToReturn->val = newname(), yyleng, yytext);
  116. 116 tempToReturn->expr = (char*)malloc(yyleng + 1);
  117. 117 strncpy(tempToReturn->expr, yytext, yyleng);
  118. 118 advance();
  119. 119 tempToReturn->last_op = TIMES;
  120. 120 return tempToReturn;
  121. 121 }
  122. 122 else if (match(SEMI)){
  123. 123 printf("111");
  124. 124 return;
  125. 125 }
  126. 126 else {
  127. 127 tempToReturn->val = newname();
  128. 128 advance();
  129. 129 fprintf(stderr, "%d: Number or identifier expected\n", yylineno);
  130. 130 return;
  131. 131 }
  132. 132 }

 

另:

 

  1. 1 /*main.c XL分析器 */
  2. 2
  3. 3 main()
  4. 4 {
  5. 5 statements();
  6. 6 }

 

  1. 1 /*lex.c XL分析器 */
  2. 2
  3. 3
  4. 4 #include "lex.h"
  5. 5 #include <stdio.h>
  6. 6 #include <ctype.h>
  7. 7
  8. 8 char *yytext = ""; /* 当前词形,注意由于是直接指向
  9. 9 行缓冲区input_buffer,因此不是以'\0'结尾,
  10. 10 因此使用时要小心, 设初值为0, 表示缓冲区为空,
  11. 11 需要重新读行 */
  12. 12 int yyleng = 0; /* 词形的长度 */
  13. 13 int yylineno = 0; /* 输入的行号 */
  14. 14
  15. 15 lex()
  16. 16 {
  17. 17 static char input_buffer[128];
  18. 18 char *current;
  19. 19
  20. 20 current = yytext + yyleng; /* 跳过以读过的词形 */
  21. 21
  22. 22 while (1) { /* 读下一个词形 */
  23. 23 while (!*current) {
  24. 24 /* 如果当前缓冲区已读完,重新从键盘读入新的一行.
  25. 25 并且跳过空格
  26. 26 */
  27. 27
  28. 28 current = input_buffer;
  29. 29 /* 如果读行有误,返回 EOI */
  30. 30 if (!fgets(input_buffer, 127, stdin)) {
  31. 31 *current = '\0';
  32. 32 return EOI;
  33. 33 }
  34. 34
  35. 35 ++yylineno;
  36. 36
  37. 37 while (isspace(*current))
  38. 38 ++current;
  39. 39 }
  40. 40
  41. 41 for (; *current; ++current) {
  42. 42 /* Get the next token */
  43. 43
  44. 44 yytext = current;
  45. 45 yyleng = 1;
  46. 46
  47. 47 /* 返回不同的词汇代码 */
  48. 48 switch (*current) {
  49. 49 case ';': return SEMI;
  50. 50 case '+': return PLUS;
  51. 51 case '-': return MINUS;
  52. 52 case '/': return DIVISION;
  53. 53 case '*': return TIMES;
  54. 54 case '(': return LP;
  55. 55 case ')': return RP;
  56. 56
  57. 57 case '\n':
  58. 58 case '\t':
  59. 59 case ' ': break;
  60. 60
  61. 61 default:
  62. 62 if (!isalnum(*current))
  63. 63 fprintf(stderr, "Ignoring illegal input <%c>\n", *current);
  64. 64 else {
  65. 65 while (isalnum(*current))
  66. 66 ++current;
  67. 67
  68. 68 yyleng = current - yytext;
  69. 69 return NUM_OR_ID;
  70. 70 }
  71. 71
  72. 72 break;
  73. 73 }
  74. 74 }
  75. 75 }
  76. 76 }
  77. 77
  78. 78 static int Lookahead = -1; /* 向前查看的词汇,设初值为-1
  79. 79 表示第一次调用match函数时
  80. 80 必须要读取一个词汇 */
  81. 81
  82. 82 int match(int token)
  83. 83 {
  84. 84 /* 判断token是否和当前向前查看的词汇相同. */
  85. 85
  86. 86 if (Lookahead == -1)
  87. 87 Lookahead = lex();
  88. 88
  89. 89 return token == Lookahead;
  90. 90 }
  91. 91
  92. 92 void advance()
  93. 93 {
  94. 94 /* 向前都一个词汇 */
  95. 95 Lookahead = lex();
  96. 96 }

 

  1. 1 /* lex.h XL分析器*/
  2. 2 #define EOI 0 /* end of input */
  3. 3 #define SEMI 1 /* ; */
  4. 4 #define PLUS 2 /* + */
  5. 5 #define TIMES 3 /* * */
  6. 6 #define LP 4 /* ( */
  7. 7 #define RP 5 /* ) */
  8. 8 #define NUM_OR_ID 6 /* decimal number or identifier */
  9. 9 #define MINUS 7
  10. 10 #define DIVISION 8
  11. 11 extern char *yytext; /* in lex.c */
  12. 12 extern int yyleng;
  13. 13 extern int yylineno;

 

  1. /*name.c XL分析器 */
  2. #include <stdio.h>
  3.  
  4. char *Names[] = { "t0", "t1", "t2", "t3", "t4", "t5", "t6", "t7",
  5. "t8", "t9", "t10", "t11", "t12", "t13", "t14", "t15" };
  6. char **Namep = Names;
  7. extern int yylineno;
  8. char *newname()
  9. {
  10. if (Namep >= &Names[sizeof(Names) / sizeof(*Names)]) {
  11. fprintf(stderr, "%d: Expression too complex\n", yylineno);
  12. exit(1);
  13. }
  14. return(*Namep++);
  15. }
  16. freename(s)
  17. char *s;
  18. {
  19. if (Namep > Names)
  20. *--Namep = s;
  21. else
  22. fprintf(stderr, "%d: (Internal error) Name stack underflow\n",
  23. yylineno);
  24. }

 

具体生成方法不一,使用makefile方式最好。

这里只提供基本的gcc 方式(部分linux中没有自带,自行安装)。

 

  1. 1 gcc -c lex.c
  2. 2
  3. 3 gcc -c retinf.c
  4. 4
  5. 5 gcc -c name.c
  6. 6
  7. 7 gcc -c main.c
  8. 8
  9. 9 gcc -o namebalabala lex.o retinf.o name.o main.o
  10. 10 //gcc -o namebalabala *.o
  11. 11
  12. 12 ./namebalabala

 

注意,如果非要在win下vs中直接运行此程序的话,你会发现:

 

 

很正常,这是因为执行的标准不一样。为了防止溢出,微软要求用sprintf_s()strncpy_s() 函数(其中_s代表safe)代替sprintf()strncpy()

也就多了一个目标串长度的参数,百度一下就好了。

 

但实际过程中应该还是会有一些问题的,特别是地址方面的。这个时候就自己debug吧~

 

原文链接:http://www.cnblogs.com/lugf/p/10486744.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号