-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.h
More file actions
558 lines (554 loc) · 11.6 KB
/
parser.h
File metadata and controls
558 lines (554 loc) · 11.6 KB
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
#pragma once
#include "lexer.h"
#include "conshow.h"
#include "word.h"
#include<tuple>
#include<vector>
#include<sstream>
typedef int PlaceType;
class Parser
{
struct ToInt
{
int operator()(int num)
{
return num;
}
int operator()(string str)
{
int b;
stringstream(str) >> b;
return b;
}
};
struct ToString
{
template<class T>
string operator()(T something)
{
ostringstream str;
str << something;
return str.str();
}
};
struct ParaList
{
int nextlist=-1;
int falselist=-1;
int truelist=-1;
string op="Null";
int quad=-1;
string offset="Null";
string place="Null";
string array="Null";
int ndim=-1;
};
struct Quad
{
Quad()
:op(), arg1(), arg2(), ret() {}
Quad(string op, string arg1, string arg2, string ret)
:op(op), arg1(arg1), arg2(arg2), ret(ret) {}
string op;
string arg1;
string arg2;
string ret;
};
int lastPos = 0;
vector<Quad> quads;
public:
Lexer &lex;
Output con;
Parser(Lexer &lex) :lex(lex) {};
~Parser() {};
Word cur, pre;
void work()
{
cur = lex.getWord();
/*while(!cur.isEOF())
stmt();*/
ParaList out = stmts();
emit("nop", "-", "-", "-");
if(out.nextlist!=-1)
backpatch(out.nextlist, nextquad() - 1);
int cnt = 0;
for (auto i : quads)
{
con.code << cnt << "#\t" << i.op << "\t," << i.arg1 << "\t," << i.arg2 << "\t," << i.ret << endl;
cnt++;
}
}
ParaList stmts()
{
ParaList out, inrest0;
con.info << "stmts --> stmt rest0" << endl;
ParaList outstmt = stmt();
inrest0.nextlist = outstmt.nextlist;
ParaList outrest0 = rest0(inrest0);
out.nextlist = outrest0.nextlist;
return out;
}
ParaList rest0(ParaList in)
{
ParaList out;
if (cur.type == Identifier || cur.type == KIf || cur.type == KWhile)
{
ParaList inrest01;
con.info << "rest0 --> m stmt rest0" << endl;
ParaList outm = m();
ParaList outstmt = stmt();
backpatch(in.nextlist, outm.quad);
inrest01.nextlist = outstmt.nextlist;
ParaList outrest01 = rest0(inrest01);
out.nextlist = outrest01.nextlist;
}
else
{
con.info << "rest0 --> e" << endl;
out.nextlist = in.nextlist;
}
return out;
}
ParaList m()
{
ParaList out;
con.info << "m --> e" << endl;
out.quad = nextquad();
return out;
}
ParaList n()
{
ParaList out;
con.info << "n --> e" << endl;
out.nextlist = nextquad();
emit("j", "-", "-", "-1");
return out;
}
ParaList stmtBlock()
{
ParaList out;
if (cur.type == OLBrace)
{
expect(OLBrace);
ParaList outstmts=stmts();
expect(ORBrace);
out.nextlist = outstmts.nextlist;
}
else
{
ParaList outstmt=stmt();
out.nextlist = outstmt.nextlist;
}
return out;
}
ParaList stmt()
{
ParaList out;
if (cur.type == Identifier)
{
con.info << "stmt --> loc = expr;" << endl;
ParaList outloc = loc();
expect(OEqu);
ParaList outexpr = expr();
expect(OSemi);
if (outloc.offset == "")
{
emit("=", ToString()(outexpr.place), "-", ToString()(outloc.place));
}
else
{
emit("[]=", ToString()(outexpr.place), "-", ToString()(outloc.place) + "[" + ToString()(outloc.offset) + "]");
}
out.nextlist = -1;
}
else if (cur.type == KIf)
{
cur = lex.getWord();
expect(OLPara);
ParaList outrel = rel();
expect(ORPara);
ParaList outm1 = m();
ParaList outstmt1 = stmtBlock();
if (cur.type == KElse)
{
ParaList outn = n();
con.info << "stmt --> if(rel)m stmt else m stmt" << endl;
expect(KElse);
ParaList outm2 = m();
ParaList outstmt2 = stmtBlock();
backpatch(outrel.truelist, outm1.quad);
backpatch(outrel.falselist, outm2.quad);
out.nextlist = outstmt1.nextlist;
mergeList(out.nextlist, outn.nextlist);
mergeList(out.nextlist, outstmt2.nextlist);
}
else
{
con.info << "stmt --> if(rel)m stmt" << endl;
backpatch(outrel.truelist, outm1.quad);
out.nextlist = outstmt1.nextlist;
mergeList(out.nextlist, outrel.falselist);
}
}
else if (cur.type == KWhile)
{
con.info << "stmt --> while(m rel)m stmt" << endl;
cur = lex.getWord();
expect(OLPara);
ParaList outm1 = m();
ParaList outrel = rel();
expect(ORPara);
ParaList outm2 = m();
ParaList outstmt = stmtBlock();
backpatch(outstmt.nextlist, outm1.quad);
backpatch(outrel.truelist, outm2.quad);
out.nextlist = outrel.falselist;
emit("j", "-", "-", ToString()(outm1.quad));
}
else
{
unexpectSolve();
}
return out;
}
ParaList loc()
{
ParaList out;
expect(Identifier);
if (cur.type == OLBrack)
{
con.info << "loc --> elist]" << endl;
ParaList outelist = elist();
out.place = newtemp();
emit("-", ToString()(outelist.array), "C", ToString()(out.place));
out.offset = newtemp();
emit("*", "w", ToString()(outelist.place), ToString()(out.offset));
}
else
{
out.place = pre.raw;
out.offset = "";
con.info << "loc --> id" << endl;
}
return out;
}
ParaList elist()
{
ParaList out, inrest;
con.info << "elist --> id[expr rest1" << endl;
inrest.array = pre.raw;
expect(OLBrack);
ParaList outexpr = expr();
inrest.ndim = 1;
inrest.place = outexpr.place;
ParaList outrest1 = rest1(inrest);
out.array = outrest1.array;
out.ndim = outrest1.ndim;
out.place = outrest1.place;
return out;
}
ParaList rest1(ParaList in)
{
expect(ORBrack);
ParaList out;
if (cur.type != OLBrack)
{
con.info << "rest1 --> e" << endl;
out.array = in.array;
out.ndim = in.ndim;
out.place = in.place;
}
else
{
ParaList inrest1;
con.info << "rest1 --> ][expr rest1" << endl;
expect(OLBrack);
ParaList outexpr = expr();
string t = newtemp();
int m = in.ndim + 1;
emit("*", ToString()(in.place), ToString()(limit(in.array, m)), ToString()(t));
emit("+", ToString()(t), ToString()(outexpr.place), ToString()(t));
inrest1.array = in.array;
inrest1.ndim = m;
inrest1.place = t;
ParaList outrest1 = rest1(inrest1);
out.array = outrest1.array;
out.ndim = outrest1.ndim;
out.place = outrest1.place;
}
return out;
}
/*START--- To Be Completed */
ParaList bool_exp()
{
con.info << "bool --> equality" << endl;
equality();
}
ParaList equality()
{
con.info << "equality --> rel rest4" << endl;
rel();
rest4();
}
ParaList rest4()
{
if (cur.type == OEquEqu)
{
con.info << "rest4 --> == rel rest4" << endl;
expect(OEquEqu);
}
else if (cur.type == ONotEqu)
{
con.info << "rest4 --> != rel rest4" << endl;
expect(ONotEqu);
}
else
{
con.info << "rest4 --> e" << endl;
}
rel();
rest4();
}
/*END--- To Be Completed */
ParaList rel()
{
ParaList out;
con.info << "rel --> expr relop expr" << endl;
ParaList outexpr1 = expr();
ParaList outrelop = relop();
ParaList outexpr2 = expr();
out.truelist = nextquad();
out.falselist = nextquad() + 1;
emit("j" + outrelop.op, ToString()(outexpr1.place), ToString()(outexpr2.place), "-1");
emit("j", "-", "-", "-1");
return out;
}
ParaList relop()
{
ParaList out;
if (cur.type == OLess)
{
out.op = "<";
con.info << "relop --> <" << endl;
expect(OLess);
}
else if (cur.type == OLessEqu)
{
out.op = "<=";
con.info << "relop --> <=" << endl;
expect(OLessEqu);
}
else if (cur.type == OMore)
{
out.op = ">";
con.info << "relop --> >" << endl;
expect(OMore);
}
else if (cur.type == OMoreEqu)
{
out.op = ">=";
con.info << "relop --> >=" << endl;
expect(OMoreEqu);
}
else
{
ParaList outexpr = expr();
out.truelist = nextquad();
out.falselist = nextquad() + 1;
emit("jnz", ToString()(outexpr.place), "-", "-1");
emit("j", "-", "-", "-1");
}
return out;
}
ParaList expr()
{
ParaList out, inrest5;
con.info << "expr --> term rest5" << endl;
ParaList outterm = term();
inrest5.place = outterm.place;
ParaList outrest5 = rest5(inrest5);
out.place = outrest5.place;
return out;
}
ParaList rest5(ParaList in)
{
ParaList out;
if (cur.type == OPlus)
{
ParaList inrest5;
con.info << "rest5 --> +term rest5" << endl;
expect(OPlus);
ParaList outterm = term();
inrest5.place = newtemp();
emit("+", ToString()(in.place), ToString()(outterm.place), ToString()(inrest5.place));
ParaList outrest5 = rest5(inrest5);
out.place = outrest5.place;
}
else if (cur.type == OMinus)
{
ParaList inrest5;
con.info << "rest5 --> -term rest5" << endl;
expect(OMinus);
ParaList outterm = term();
inrest5.place = newtemp();
emit("-", ToString()(in.place), ToString()(outterm.place), ToString()(inrest5.place));
ParaList outrest5 = rest5(inrest5);
out.place = outrest5.place;
}
else
{
con.info << "rest5 --> e" << endl;
out.place = in.place;
}
return out;
}
ParaList term()
{
ParaList out, inrest6;
con.info << "term --> unary rest6" << endl;
ParaList outunary = unary();
inrest6.place = outunary.place;
ParaList outrest6 = rest6(inrest6);
out.place = outrest6.place;
return out;
}
ParaList rest6(ParaList in)
{
ParaList out;
if (cur.type == OMul)
{
ParaList inrest6;
con.info << "rest6 --> *unary rest6" << endl;
expect(OMul);
ParaList outunary = unary();
inrest6.place = ToString()(newtemp());
emit("*", ToString()(in.place), ToString()(outunary.place), ToString()(inrest6.place));
ParaList outrest6 = rest6(inrest6);
out.place = outrest6.place;
}
else if (cur.type == ODiv)
{
ParaList inrest6;
con.info << "rest6 --> /unary rest6" << endl;
expect(ODiv);
ParaList outunary = unary();
inrest6.place = newtemp();
emit("/", ToString()(in.place), ToString()(outunary.place), ToString()(inrest6.place));
ParaList outrest6 = rest6(inrest6);
out.place = outrest6.place;
}
else
{
con.info << "rest6 --> e" << endl;
out.place = in.place;
}
return out;
}
ParaList unary()
{
ParaList out;
con.info << "unary --> factor" << endl;
ParaList outfactor = factor();
out.place = outfactor.place;
return out;
}
ParaList factor()
{
ParaList out;
if (cur.type == OLPara)
{
con.info << "factor --> (expr)" << endl;
expect(OLPara);
ParaList outexpr = expr();
expect(ORPara);
out.place = outexpr.place;
}
else if (cur.type == Identifier)
{
con.info << "factor --> loc" << endl;
ParaList outloc = loc();
if (outloc.offset == "")
{
out.place = outloc.place;
}
else
{
out.place = newtemp();
emit("=[]", ToString()(outloc.place) + "[" + ToString()(outloc.offset) + "]", "-", ToString()(out.place));
}
}
else if (cur.type == CInt)
{
con.info << "factor --> num" << endl;
expect(CInt);
out.place = ToString()(*((int*)pre.val));
}
else
{
unexpectSolve();
}
return out;
}
private:
string limit(string arrayName, int ndim)
{
return "D" + ToString()(ndim);
}
void backpatch(int nextlist, int quadnum)
{
int pos = nextlist;
while (pos!=-1)
{
int tmp = ToInt()(quads[pos].ret);
quads[pos].ret = ToString()(quadnum);
pos = tmp;
}
}
int nextquad()
{
return lastPos;
}
void emit(string op, string arg1, string arg2, string ret)
{
while (lastPos >= (int)quads.size())
{
quads.push_back(Quad());
}
quads[lastPos] = Quad(op, arg1, arg2, ret);
lastPos++;
}
void mergeList(int &to, int from)
{
if (from == -1)
return;
int pos = from;
while (ToInt()(quads[pos].ret) != -1)
{
pos = ToInt()(quads[pos].ret);
}
quads[pos].ret = ToString()(to);
to = from;
}
string newtemp()
{
static int cnt = 0;
return "X" + ToString()(cnt++);
}
void expect(WordType type)
{
if (cur.type != type)
{
con.error <<"Line "<<cur.lineNum<<": Expect "<< TypeName[type] << endl;
}
else
{
pre = cur;
cur = lex.getWord();
}
}
void unexpectSolve()
{
con.error << "Line " << cur.lineNum << ": Unexpected word:" << cur.raw << endl;
cur = lex.getWord();
}
};