-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCompiler.c
206 lines (173 loc) · 4.73 KB
/
Compiler.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include "StringStack.h"
typedef enum {
Variable,
Operator,
OpenBracket,
CloseBracket
} TknType;
TknType getTknType(char *tkn) {
if (!strcmp(tkn, "+")
|| !strcmp(tkn, "-")
|| !strcmp(tkn, "*")
|| !strcmp(tkn, "/")
|| !strcmp(tkn, "="))
return Operator;
else if (!strcmp(tkn, "("))
return OpenBracket;
else if (!strcmp(tkn, ")"))
return CloseBracket;
else //if ('A' <= && <= 'Z')変数大文字検査
return Variable;
}
int getOpPty(char *op) {
if (!strcmp(op, "*")
|| !strcmp(op, "/"))
return 3;
else if (!strcmp(op, "+")
|| !strcmp(op, "-"))
return 2;
else if (!strcmp(op, "=")
|| !strcmp(op, ")"))
return 1;
else if (!strcmp(op, ")"))
return 0;
}
int cmpOps(char *op1, char *op2) {
return getOpPty(op1) >= getOpPty(op2);
}
typedef struct {
char Operator[STRINGSTACK_STR_SIZE];
char Operand1[STRINGSTACK_STR_SIZE];
char Operand2[STRINGSTACK_STR_SIZE];
char Variable[STRINGSTACK_STR_SIZE];
} ThreeAddressUnit;
// コンパイル: gcc Compiler.c StringStack.c -o Compiler
void main() {
char formula[STRINGSTACK_STR_SIZE][STRINGSTACK_SIZE_MAX];
int tknCnt;
char rpnf[STRINGSTACK_SIZE_MAX];
strcpy(rpnf, "");
// 数式入力 -------------------------------------------------------
for (tknCnt = 0;; tknCnt++) {
printf("字句[%d]: ", tknCnt);
char tkn[STRINGSTACK_STR_SIZE];
scanf("%s", tkn);
if (!strcmp(tkn, "q"))
break;
strcpy(formula[tknCnt], tkn);
if (tknCnt > STRINGSTACK_SIZE_MAX) {
printf("[エラー] 入力可能な字句数を超越");
exit(-1);
}
}
// ----------------------------------------------------------------
// RPN化 ----------------------------------------------------------
StringStack stack = newStringStack();
for (int i = 0; i < tknCnt; i++) {
char tkn[STRINGSTACK_STR_SIZE];
strcpy(tkn, formula[i]);
switch(getTknType(tkn)) {
case Variable:
sprintf(rpnf, "%s%s", rpnf, tkn);
break;
case OpenBracket:
push(&stack, tkn);
break;
case CloseBracket:
{
char tkn[STRINGSTACK_STR_SIZE];
while (strcmp(get(&stack), "(")) {
strcpy(tkn, pop(&stack));
sprintf(rpnf, "%s%s", rpnf, tkn);
}
pop(&stack);
}
break;
case Operator:
if (stack.index == 0)
push(&stack, tkn);
else if (cmpOps(tkn, get(&stack)))
push(&stack, tkn);
else {
do {
char tknTmp[STRINGSTACK_STR_SIZE];
strcpy(tknTmp, pop(&stack));
sprintf(rpnf, "%s%s", rpnf, tknTmp);
} while (!cmpOps(tkn, get(&stack)));
push(&stack, tkn);
}
break;
}
}
for (;;) {
char tkn[STRINGSTACK_STR_SIZE];
strcpy(tkn, pop(&stack));
if (!strcmp(tkn, "\0"))
break;
sprintf(rpnf, "%s%s", rpnf, tkn);
}
printf("\n< 逆ポーランド記法 >\n");
for (int i = 0; i < strlen(rpnf); i++)
printf("%c ", rpnf[i]);
printf("\n\n");
// ----------------------------------------------------------------
ThreeAddressUnit pool[STRINGSTACK_SIZE_MAX]; // 大きさに意味無し
int poolIdx = 0;
// 三番地コード生成並出力 -----------------------------------------
printf("< 三番地コード >\n");
stack = newStringStack();
char tkn[STRINGSTACK_STR_SIZE];
char c = 'a'; // 変数はaから
for (int i = 0; i < strlen(rpnf); i++) {
sprintf(tkn, "%c", rpnf[i]); // 字句
switch(getTknType(tkn)) {
case Variable:
push(&stack, tkn);
break;
case Operator:
{
strcpy(pool[poolIdx].Operator, tkn);
strcpy(pool[poolIdx].Operand2, pop(&stack)); // 第二引数
strcpy(pool[poolIdx].Operand1, pop(&stack)); // 第一引数
sprintf(pool[poolIdx].Variable, "$%c", c++); // 変数
printf("%s (%s, %s, %s)\n", pool[poolIdx].Operator, pool[poolIdx].Operand1, pool[poolIdx].Operand2, pool[poolIdx].Variable);
push(&stack, pool[poolIdx].Variable);
poolIdx++;
}
break;
}
}
printf("\n");
// ----------------------------------------------------------------
// 仮想CPU用機械語列生成並出力 ------------------------------------
printf("< 仮想CPU用機械語列 >\n");
bool stf = false;
char sta[STRINGSTACK_STR_SIZE];
for (int i = 0; i < poolIdx; i++) {
if (strcmp(pool[i - 1].Variable, pool[i].Operand1))
printf("LD %s\n", pool[i].Operand1);
switch (pool[i].Operator[0]) {
case '+':
printf("AD %s\n", pool[i].Operand2);
break;
case '-':
printf("SB %s\n", pool[i].Operand2);
break;
case '*':
printf("ML %s\n", pool[i].Operand2);
break;
case '/':
printf("DV %s\n", pool[i].Operand2);
break;
}
if (i != poolIdx - 1 && !strcmp(pool[i].Variable, pool[i + 1].Operand1))
continue;
printf("ST %s\n", pool[i].Variable);
}
// ----------------------------------------------------------------
printf("\n");
}