设为首页 加入收藏

TOP

编译原理作业(第一次)-完成retinf.c(阉割版)(二)
2019-03-07 00:07:57 】 浏览:418
Tags:编译 原理 作业 第一次 完成 retinf.c 阉割
93 tempToReturn->expr = (char*)malloc(strlen(temp0->expr) + strlen(temp1->expr) + 4); 94 sprintf(tempToReturn->expr, "%s %c %s", 95 temp0->expr, op, temp1->expr); 96 } 97 switch (op) 98 { 99 case '+':tempToReturn->last_op = PLUS; break; 100 case '-':tempToReturn->last_op = MINUS; break; 101 case '*':tempToReturn->last_op = TIMES; break; 102 case '/':tempToReturn->last_op = DIVISION; break; 103 default: 104 break; 105 } 106 107 printf(" %s %c= %s\n", temp0->val, op, temp1->val); 108 freename(temp1->val); 109 tempToReturn->val = temp0->val; 110 return tempToReturn; 111 112 } 113 114 if (match(NUM_OR_ID)) { 115 printf(" %s = %0.*s\n", tempToReturn->val = newname(), yyleng, yytext); 116 tempToReturn->expr = (char*)malloc(yyleng + 1); 117 strncpy(tempToReturn->expr, yytext, yyleng); 118 advance(); 119 tempToReturn->last_op = TIMES; 120 return tempToReturn; 121 } 122 else if (match(SEMI)){ 123 printf("111"); 124 return; 125 } 126 else { 127 tempToReturn->val = newname(); 128 advance(); 129 fprintf(stderr, "%d: Number or identifier expected\n", yylineno); 130 return; 131 } 132 }

 

另:

 

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

 

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

 

 1 /* lex.h      XL分析器*/
 2 #define EOI           0        /*  end of input                */
 3 #define SEMI          1        /*       ;                      */
 4 #define PLUS          2        /*       +                      */
 5 #define TIMES         3        /*       *                      */
 6 #define LP            4        /*       (                      */
 7 #define RP            5        /*       )                      */
 8 #define NUM_OR_ID     6        /* decimal number or identifier */
 9 #define MINUS         7
10 #define D
首页 上一页 1 2 3 下一页 尾页 2/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇用clock()函数计时的坑 下一篇2019.3.6

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目