-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathjyk.cpp
348 lines (338 loc) · 7.95 KB
/
jyk.cpp
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
#include "head.h"
#include <iostream>
#include <vector>
#include <string.h>
extern int t_k; //用来表示算数表达式tk中的k
extern string tk;
extern vector<Quadruple> qua_list;
extern struct Quadruple qua;
extern vector<Word> words;
extern int token_i;
int syn0 = 0;
vector<string> A;//分析栈
vector<string> SEM;
string s = "";//记录>,<,>=,<=,==
string ss = "";//记录+-*/
string connect="";//连接+-*/和GEQ
void reset0()//清空分析栈
{
while(!A.empty())
{
A.pop_back();
}
}
void PU(string s)
{
SEM.push_back(s);
}
void WE()
{
qua.s[0] = "we";
qua.s[1] = "_";
qua.s[2] = "_";
qua.s[3] = "_";
out_qua();
}
void WH()
{
qua.s[0] = "wh";
qua.s[1] = "_";
qua.s[2] = "_";
qua.s[3] = "_";
out_qua();
}
void DO()
{
if(SEM.size()>=2)
{
tk = "t"+int_to_str(++t_k);
qua.s[0] = s;
qua.s[1] = SEM[SEM.size()-2];
qua.s[2] = SEM.back();
qua.s[3] = tk;
out_qua();
qua.s[0] = "do";
qua.s[1] = tk;
qua.s[2] = "_";
qua.s[3] = "_";
out_qua();
SEM.pop_back();
SEM.pop_back();
}
else if(SEM.size()==1)
{
qua.s[0] = "do";
qua.s[1] = SEM[0];
qua.s[2] = "_";
qua.s[3] = "_";
out_qua();
SEM.pop_back();
}
}
void GEQ(string ss)
{
string b[2]={""};
b[0] = SEM.back();
SEM.pop_back();
b[1] = SEM.back();
SEM.pop_back();
tk = "t"+int_to_str(++t_k);
qua.s[0] = ss;
qua.s[1] = b[1];
qua.s[2] = b[0];
qua.s[3] = tk;
SEM.push_back(tk);
out_qua();
}
int fun_while()
{
if(A.back().compare("S")==0&&words[token_i].value.compare("while")==0)
{
//cout<<"S->W D F"<<endl;
A.pop_back();
A.push_back("WE");
A.push_back("F");
A.push_back("D");
A.push_back("W");
return 1;
}
else if(A.back().compare("F")==0&&words[token_i].value.compare("{")==0)
{
//cout<<"F->{f}"<<endl;
A.pop_back();
A.push_back("}");
A.push_back("f");
A.push_back("{");
return 1;
}
else if(A.back().compare("F")==0&&words[token_i].value.compare("{")!=0)
{
//cout<<"F->f"<<endl;
A.pop_back();
A.push_back("f");
return 1;
}
else if(A.back().compare("W")==0)
{
//cout<<"W->while"<<endl;
A.pop_back();
A.push_back("WH");
A.push_back("while");
return 1;
}
else if(A.back().compare("D")==0&&words[token_i].value.compare("(")==0)
{
//cout<<"D->(R)"<<endl;
A.pop_back();
A.push_back("DO");
A.push_back(")");
A.push_back("R");
A.push_back("(");
return 1;
}
else if(A.back().compare("R")==0)
{
//cout<<"R->I R'"<<endl;
A.pop_back();
A.push_back("R'");
A.push_back("E");
return 1;
}
else if(A.back().compare("R'")==0&&(words[token_i].value.compare(">")==0||words[token_i].value.compare("<")==0||words[token_i].value.compare("==")==0||words[token_i].value.compare(">=")==0||words[token_i].value.compare("<=")==0))
{
//cout<<"R'->w I"<<endl;
A.pop_back();
A.push_back("E");
A.push_back(words[token_i].value);
s = words[token_i].value;
return 1;
}
else if(A.back().compare("R'")==0&&words[token_i].value.compare(">")!=0&&words[token_i].value.compare("<")!=0&&words[token_i].value.compare("==")!=0&&words[token_i].value.compare(">=")!=0&&words[token_i].value.compare("<=")!=0)
{
//cout<<"R'->"<<endl;
A.pop_back();
return 1;
}
else if(A.back().compare("E")==0)
{
//cout<<"E->T E'"<<endl;
A.pop_back();
A.push_back("E'");
A.push_back("T");
return 1;
}
else if(A.back().compare("E'")==0&&(words[token_i].value.compare("+")==0||words[token_i].value.compare("-")==0))
{
//cout<<"E'->w0 T E'"<<endl;
connect = "GEQ"+words[token_i].value;
A.pop_back();
A.push_back("E'");
A.push_back(connect);
A.push_back("T");
A.push_back(words[token_i].value);
return 1;
}
else if(A.back().compare("E'")==0&&words[token_i].value.compare("+")!=0&&words[token_i].value.compare("-")!=0)
{
//cout<<"E'->e"<<endl;
A.pop_back();
return 1;
}
else if(A.back().compare("T")==0)
{
//cout<<"T->P T'"<<endl;
A.pop_back();
A.push_back("T'");
A.push_back("P");
return 1;
}
else if(A.back().compare("T'")==0&&(words[token_i].value.compare("*")==0||words[token_i].value.compare("/")==0))
{
//cout<<"T'->w1 P T'"<<endl;
connect = "GEQ"+words[token_i].value;
A.pop_back();
A.push_back("T'");
A.push_back(connect);
A.push_back("P");
A.push_back(words[token_i].value);
return 1;
}
else if(A.back().compare("T'")==0&&words[token_i].value.compare("*")!=0&&words[token_i].value.compare("/")!=0)
{
//cout<<"T'->e"<<endl;
A.pop_back();
return 1;
}
else if(A.back().compare("P")==0&&(words[token_i].type.compare("I")==0||words[token_i].type.compare("c")==0))
{
//cout<<"P->I'"<<endl;
if(words[token_i].type.compare("I")==0) //判断标识符是否未定义
{
if(un_def(words[token_i].value))
return 0;
}
A.pop_back();
A.push_back("I");
return 1;
}
else if(A.back().compare("P")==0&&words[token_i].value.compare("(")==0)
{
//cout<<"P->(E)'"<<endl;
A.pop_back();
A.push_back(")");
A.push_back("E");
A.push_back("(");
return 1;
}
else if(A.back().compare("f")==0)
{
fn_body();
A.pop_back();
return 1;
}
else if(A.back().compare("WE")==0)
{
A.pop_back();
WE();
return 1;
}
else if(A.back().compare("WH")==0)
{
A.pop_back();
WH();
return 1;
}
else if(A.back().compare("DO")==0)
{
A.pop_back();
DO();
return 1;
}
else if(A.back().compare("GEQ+")==0||A.back().compare("GEQ-")==0||A.back().compare("GEQ*")==0||A.back().compare("GEQ/")==0)
{
if(A.back().compare("GEQ+")==0)
GEQ("+");
else if(A.back().compare("GEQ-")==0)
GEQ("-");
else if(A.back().compare("GEQ*")==0)
GEQ("*");
else if(A.back().compare("GEQ/")==0)
GEQ("/");
A.pop_back();
return 1;
}
else if(A.back().compare("I")==0&&(words[token_i].type.compare("I")==0||words[token_i].type.compare("c")==0))
{
A.pop_back();
PU(words[token_i].value);
token_i++;
return 1;
}
else if(A.back().compare("#")==0)
{
A.pop_back();
return 2;
}
else{
if(A.back().compare(words[token_i].value)==0)
{
if(A.back().compare(")")==0)
{
//cout<<" "<<"D->(R)"<<endl;
}
if(A.back().compare("}")==0)
{
//cout<<" "<<"F->{f}"<<endl;
}
A.pop_back();
token_i++;
return 1;
}
else
return 0;
}
return 1;
}
int fn_while()
{
if(words[token_i].value.compare("while")!=0)
return 0;
else
{
A.push_back("#");
A.push_back("S");
}
int a = 1;
if(syn0==0)
{
reset0();
A.push_back("#");
A.push_back("S");
syn0++;
}
while(a==1)
{
a = fun_while();
/*
for(int i=0;i<A.size();i++)
{
cout<<A[i];
}
cout<<"\t";
for(int i=0;i<SEM.size();i++)
{
cout<<SEM[i];
}
cout<<endl;*/
}
if(a==2){
//cout<<"RIGHT"<<endl;
//token_i++;
//cout<<token_i;
return 1;
}
else if(a==0){
//cout<<"WRONG"<<endl;
return 0;
}
}