-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathcparser.cpp
More file actions
721 lines (638 loc) · 24 KB
/
cparser.cpp
File metadata and controls
721 lines (638 loc) · 24 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
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
//
// Project: CMiniLang
// Author: bajdcc
//
#include <sstream>
#include "cparser.h"
#include "clexer.h"
#include "cast.h"
namespace clib {
/**
* 递归下降分析器代码参考自:
* https://github.com/lotabout/write-a-C-interpreter
* 作者文章:
* http://lotabout.me/2015/write-a-C-interpreter-0
*/
cparser::cparser(string_t str)
: lexer(str) {
}
ast_node *cparser::parse() {
// 清空词法分析结果
lexer.reset();
// 清空AST
ast.reset();
// 语法分析(递归下降)
program();
return ast.get_root();
}
ast_node *cparser::root() {
return ast.get_root();
}
void cparser::next() {
lexer_t token;
do {
token = lexer.next();
if (token == l_error) {
auto err = lexer.recent_error();
printf("[%04d:%03d] %-12s - %s\n",
err.line,
err.column,
ERROR_STRING(err.err).c_str(),
err.str.c_str());
}
} while (token == l_newline || token == l_space || token == l_comment || token == l_error);
#if 0
if (token != l_end) {
printf("[%04d:%03d] %-12s - %s\n",
lexer.get_last_line(),
lexer.get_last_column(),
LEX_STRING(lexer.get_type()).c_str(),
lexer.current().c_str());
}
#endif
}
void cparser::program() {
next();
while (!lexer.is_type(l_end)) {
global_declaration();
}
}
// 表达式
ast_node *cparser::expression(operator_t level) {
// 表达式有多种类型,像 `(char) *a[10] = (int *) func(b > 0 ? 10 : 20);
//
// 1. unit_unary ::= unit | unit unary_op | unary_op unit
// 2. expr ::= unit_unary (bin_op unit_unary ...)
ast_node *node = nullptr;
// unit_unary()
if (lexer.is_type(l_end)) { // 结尾
error("unexpected token EOF of expression");
}
if (lexer.is_integer()) { // 数字
auto type = lexer.get_type();
switch (type) {
#define DEFINE_NODE_INT(t) \
case l_##t: \
node = ast.new_node(ast_##t); \
node->data._##t = lexer.get_##t(); \
break;
DEFINE_NODE_INT(char)
DEFINE_NODE_INT(uchar)
DEFINE_NODE_INT(short)
DEFINE_NODE_INT(ushort)
DEFINE_NODE_INT(int)
DEFINE_NODE_INT(uint)
DEFINE_NODE_INT(long)
DEFINE_NODE_INT(ulong)
DEFINE_NODE_INT(float)
DEFINE_NODE_INT(double)
#undef DEFINE_NODE_INT
default:
error("invalid number");
break;
}
match_number();
} else if (lexer.is_type(l_string)) { // 字符串
std::stringstream ss;
ss << lexer.get_string();
#if 0
printf("[%04d:%03d] String> %04X '%s'\n", clexer.get_line(), clexer.get_column(), idx, clexer.get_string().c_str());
#endif
match_type(l_string);
while (lexer.is_type(l_string)) {
ss << lexer.get_string();
#if 0
printf("[%04d:%03d] String> %04X '%s'\n", clexer.get_line(), clexer.get_column(), idx, clexer.get_string().c_str());
#endif
match_type(l_string);
}
node = ast.new_node(ast_string);
ast.set_str(node, ss.str());
} else if (lexer.is_keyword(k_sizeof)) { // sizeof
// 支持 `sizeof(int)`, `sizeof(char)` and `sizeof(*...)`
match_keyword(k_sizeof);
match_operator(op_lparan);
if (lexer.is_keyword(k_unsigned)) {
match_keyword(k_unsigned); // 有符号或无符号大小相同
}
auto size = lexer.get_sizeof();
next();
while (lexer.is_operator(op_times)) {
match_operator(op_times);
if (size != LEX_SIZEOF(ptr)) {
size = LEX_SIZEOF(ptr);
}
}
match_operator(op_rparan);
node = ast.new_node(ast_int);
node->data._int = size;
} else if (lexer.is_type(l_identifier)) { // 变量
// 三种可能
// 1. function call 函数名调用
// 2. Enum variable 枚举值
// 3. global/local variable 全局/局部变量名
auto id = lexer.get_identifier();
match_type(l_identifier);
if (lexer.is_operator(op_lparan)) { // 函数调用
// function call
match_operator(op_lparan);
node = ast.new_node(ast_invoke);
ast.set_id(node, id);
// pass in arguments
while (!lexer.is_operator(op_rparan)) { // 参数数量
auto tmp = ast.new_node(ast_exp_param);
cast::set_child(tmp, expression(op_assign));
cast::set_child(node, tmp);
if (lexer.is_operator(op_comma)) {
match_operator(op_comma);
}
}
match_operator(op_rparan);
} else {
node = ast.new_node(ast_id);
ast.set_id(node, id);
}
} else if (lexer.is_operator(op_lparan)) { // 强制转换
// cast or parenthesis
match_operator(op_lparan);
if (lexer.is_type(l_keyword)) {
auto tmp = parse_type();
auto ptr = 0;
while (lexer.is_operator(op_times)) {
match_operator(op_times);
ptr++;
}
match_operator(op_rparan);
node = ast.new_node(ast_cast);
node->data._type.type = tmp;
node->data._type.ptr = ptr;
cast::set_child(node, expression(op_plus_plus));
} else {
// 普通括号嵌套
node = expression(op_assign);
match_operator(op_rparan);
}
} else if (lexer.is_operator(op_times)) { // 解引用
// dereference *<addr>
match_operator(op_times);
node = ast.new_node(ast_sinop);
node->data._op.op = op_times;
cast::set_child(node, expression(op_plus_plus));
} else if (lexer.is_operator(op_bit_and)) { // 取地址
// get the address of
match_operator(op_bit_and);
node = ast.new_node(ast_sinop);
node->data._op.op = op_bit_and;
cast::set_child(node, expression(op_plus_plus));
} else if (lexer.is_operator(op_logical_not)) {
// not
match_operator(op_logical_not);
node = ast.new_node(ast_sinop);
node->data._op.op = op_logical_not;
cast::set_child(node, expression(op_plus_plus));
} else if (lexer.is_operator(op_bit_not)) {
// bitwise not
match_operator(op_bit_not);
node = ast.new_node(ast_sinop);
node->data._op.op = op_bit_not;
cast::set_child(node, expression(op_plus_plus));
} else if (lexer.is_operator(op_plus)) {
// +var, do nothing
match_operator(op_plus);
node = ast.new_node(ast_sinop);
node->data._op.op = op_plus;
cast::set_child(node, expression(op_plus_plus));
} else if (lexer.is_operator(op_minus)) {
// -var
match_operator(op_minus);
if (lexer.is_integer()) {
auto type = lexer.get_type();
switch (type) {
#define DEFINE_NODE_INT(t) \
case l_##t: \
node = ast.new_node(ast_##t); \
node->data._##t = -lexer.get_##t(); \
break;
DEFINE_NODE_INT(char)
DEFINE_NODE_INT(uchar)
DEFINE_NODE_INT(short)
DEFINE_NODE_INT(ushort)
DEFINE_NODE_INT(int)
DEFINE_NODE_INT(uint)
DEFINE_NODE_INT(long)
DEFINE_NODE_INT(ulong)
DEFINE_NODE_INT(float)
DEFINE_NODE_INT(double)
#undef DEFINE_NODE_INT
default:
error("invalid integer type");
break;
}
match_integer();
} else {
node = ast.new_node(ast_sinop);
node->data._op.op = op_minus;
cast::set_child(node, expression(op_plus_plus));
}
} else if (lexer.is_operator(op_plus_plus, op_minus_minus)) {
auto tmp = lexer.get_operator();
match_type(l_operator);
node = ast.new_node(ast_sinop);
node->data._op.op = tmp;
cast::set_child(node, expression(op_plus_plus));
} else {
error("bad expression");
}
// 二元表达式以及后缀操作符
operator_t op = op__start;
while (lexer.is_type(l_operator) && OPERATOR_PRED(op = lexer.get_operator()) <= OPERATOR_PRED(level)) { // 优先级判断
auto tmp = node;
switch (op) {
case op_rparan:
case op_rsquare:
case op_colon:
return node;
case op_assign: {
// var = expr;
match_operator(op_assign);
node = ast.new_node(ast_binop);
node->data._op.op = op_assign;
cast::set_child(node, tmp);
cast::set_child(node, expression(op_assign));
}
break;
case op_query: {
// expr ? a : b;
match_operator(op_query);
node = ast.new_node(ast_triop);
node->data._op.op = op_query;
cast::set_child(node, tmp);
cast::set_child(node, expression(op_assign));
if (lexer.is_operator(op_colon)) {
match_operator(op_colon);
} else {
error("missing colon in conditional");
}
cast::set_child(node, expression(op_query));
}
break;
case op_lsquare:
match_operator(op);
node = ast.new_node(ast_binop);
node->data._op.op = op;
cast::set_child(node, tmp);
cast::set_child(node, expression(op_assign));
match_operator(op_rsquare);
break;
case op_plus_plus:
case op_minus_minus:
match_operator(op);
node = ast.new_node(ast_sinop);
node->data._op.op = op;
node->data._op.data = 1;
cast::set_child(node, tmp);
break;
case op_equal:
case op_plus:
case op_plus_assign:
case op_minus:
case op_minus_assign:
case op_times:
case op_times_assign:
case op_divide:
case op_div_assign:
case op_bit_and:
case op_and_assign:
case op_bit_or:
case op_or_assign:
case op_bit_xor:
case op_xor_assign:
case op_mod:
case op_mod_assign:
case op_less_than:
case op_less_than_or_equal:
case op_greater_than:
case op_greater_than_or_equal:
case op_not_equal:
case op_logical_and:
case op_logical_or:
case op_pointer:
case op_left_shift:
case op_right_shift:
case op_left_shift_assign:
case op_right_shift_assign:
match_operator(op);
node = ast.new_node(ast_binop);
node->data._op.op = op;
cast::set_child(node, tmp);
cast::set_child(node, expression(op));
break;
default:
error("compiler error, token = " + lexer.current());
break;
}
}
return node;
}
// 基本语句
ast_node *cparser::statement() {
// there are 8 kinds of statements here:
// 1. if (...) <statement> [else <statement>]
// 2. while (...) <statement>
// 3. { <statement> }
// 4. return xxx;
// 5. <empty statement>;
// 6. expression; (expression end with semicolon)
ast_node *node = nullptr;
if (lexer.is_keyword(k_if)) { // if判断
match_keyword(k_if);
node = ast.new_node(ast_if);
match_operator(op_lparan);
cast::set_child(node, expression(op_assign)); // if判断的条件
match_operator(op_rparan);
cast::set_child(node, statement()); // 处理if执行体
if (lexer.is_keyword(k_else)) { // 处理else
match_keyword(k_else);
cast::set_child(node, statement());
}
} else if (lexer.is_keyword(k_while)) { // while循环
match_keyword(k_while);
node = ast.new_node(ast_while);
match_operator(op_lparan);
cast::set_child(node, expression(op_assign)); // 条件
match_operator(op_rparan);
cast::set_child(node, statement());
} else if (lexer.is_operator(op_lbrace)) { // 语句
match_operator(op_lbrace);
node = ast.new_node(ast_block);
while (!lexer.is_operator(op_rbrace)) {
cast::set_child(node, statement());
}
match_operator(op_rbrace);
} else if (lexer.is_keyword(k_return)) { // 返回
// return [expression];
match_keyword(k_return);
node = ast.new_node(ast_return);
if (!lexer.is_operator(op_semi)) {
cast::set_child(node, expression(op_assign));
}
match_operator(op_semi);
} else if (lexer.is_operator(op_semi)) { // 空语句
// empty statement
match_operator(op_semi);
node = ast.new_node(ast_empty);
} else { // 表达式
// a = b; or function_call();
node = ast.new_node(ast_exp);
cast::set_child(node, expression(op_assign));
match_operator(op_semi);
}
if (node && node->flag != ast_block && node->flag != ast_stmt) {
auto tmp = ast.new_node(ast_stmt);
cast::set_child(tmp, node);
node = tmp;
}
return node;
}
// 枚举声明
void cparser::enum_declaration() {
// parse enum [id] { a = 1, b = 3, ...}
int i = 0;
while (!lexer.is_operator(op_rbrace)) {
if (!lexer.is_type(l_identifier)) {
error("bad enum identifier " + lexer.current());
}
match_type(l_identifier);
if (lexer.is_operator(op_assign)) { // 赋值
// like { a = 10 }
next();
if (!lexer.is_integer()) {
error("bad enum initializer");
}
i = lexer.get_integer();
next();
}
// 保存值到变量中
ast.new_child(ast_enum_unit);
auto _id = ast.new_child(ast_id, false);
auto _int = ast.new_child(ast_int, false);
ast.set_id(_id, lexer.get_identifier());
_int->data._int = i++;
ast.to(to_parent);
if (lexer.is_operator(op_comma)) {
next();
}
}
}
// 函数参数
void cparser::function_parameter() {
while (!lexer.is_operator(op_rparan)) { // 判断参数右括号结尾
// int name, ...
auto type = parse_type(); // 基本类型
auto ptr = 0;
// pointer type
while (lexer.is_operator(op_times)) { // 指针
match_operator(op_times);
ptr++;
}
// parameter name
if (!lexer.is_type(l_identifier)) {
error("bad parameter declaration");
}
auto id = lexer.get_identifier();
match_type(l_identifier);
ast.new_child(ast_var_param);
auto _type = ast.new_child(ast_type, false);
auto _id = ast.new_child(ast_id, false);
ast.to(to_parent);
// 保存本地变量
_type->data._type.type = type;
_type->data._type.ptr = ptr;
ast.set_id(_id, id);
if (lexer.is_operator(op_comma)) {
match_operator(op_comma);
}
}
}
// 函数体
void cparser::function_body() {
// type func_name (...) {...}
// -->| |<--
{ // ...
// 1. local declarations
// 2. statements
// }
while (lexer.is_basetype()) {
// 处理基本类型
base_type = parse_type();
while (!lexer.is_operator(op_semi)) { // 判断语句结束
auto ptr = 0;
auto type = base_type;
while (lexer.is_operator(op_times)) { // 处理指针
match_operator(op_times);
ptr++;
}
if (!lexer.is_type(l_identifier)) {
// invalid declaration
error("bad local declaration");
}
auto id = lexer.get_identifier();
match_type(l_identifier);
ast.new_child(ast_var_local);
auto _type = ast.new_child(ast_type, false);
auto _id = ast.new_child(ast_id, false);
ast.to(to_parent);
// 保存本地变量
_type->data._type.type = type;
_type->data._type.ptr = ptr;
ast.set_id(_id, id);
if (lexer.is_operator(op_comma)) {
match_operator(op_comma);
}
}
match_operator(op_semi);
}
ast.new_child(ast_empty, false)->data._int = 1; // 标识声明与语句的分界线
// statements
while (!lexer.is_operator(op_rbrace)) {
auto stmt = statement();
if (stmt != nullptr) {
ast.add_child(stmt);
}
}
}
}
// 函数声明
void cparser::function_declaration() {
// type func_name (...) {...}
// | this part
match_operator(op_lparan);
ast.new_child(ast_param);
function_parameter();
ast.to(to_parent);
match_operator(op_rparan);
match_operator(op_lbrace);
ast.new_child(ast_block);
function_body();
ast.to(to_parent);
// match('}'); 这里不处理右括号是为了上层函数判断结尾
}
// 变量声明语句(全局或函数体内)
// int [*]id [; | (...) {...}]
void cparser::global_declaration() {
base_type = l_int;
// 处理enum枚举
if (lexer.is_keyword(k_enum)) {
// enum [id] { a = 10, b = 20, ... }
match_keyword(k_enum);
if (!lexer.is_operator(op_lbrace)) {
match_type(l_identifier); // 省略了[id]枚举名
}
ast.new_child(ast_enum);
if (lexer.is_operator(op_lbrace)) {
// 处理枚举体
match_operator(op_lbrace);
enum_declaration(); // 枚举的变量声明部分,即 a = 10, b = 20, ...
match_operator(op_rbrace);
}
ast.to(to_parent);
match_operator(op_semi);
return;
}
// 解析基本类型,即变量声明时的类型
base_type = parse_type();
if (base_type == l_none)
base_type = l_int;
// 处理逗号分隔的变量声明
while (!lexer.is_operator(op_semi, op_rbrace)) {
auto type = base_type; // 以先声明的类型为基础
auto ptr = 0;
// 处理指针, 像`int ****x;`
while (lexer.is_operator(op_times)) {
match_operator(op_times);
ptr++;
}
if (!lexer.is_type(l_identifier)) { // 不存在变量名则报错
// invalid declaration
error("bad global declaration");
}
auto id = lexer.get_identifier();
match_type(l_identifier);
if (lexer.is_operator(op_lparan)) { // 有左括号则应判定是函数声明
ast.new_child(ast_func);
auto _type = ast.new_child(ast_type, false);
auto _id = ast.new_child(ast_id, false);
// 处理变量声明
_type->data._type.type = type;
_type->data._type.ptr = ptr;
ast.set_id(_id, id);
#if 0
printf("[%04d:%03d] Function> %04X '%s'\n", clexer.get_line(), clexer.get_column(), id->value._int * 4, id->name.c_str());
#endif
function_declaration();
ast.to(to_parent);
} else {
ast.new_child(ast_var_global);
auto _type = ast.new_child(ast_type, false);
auto _id = ast.new_child(ast_id, false);
// 处理变量声明
_type->data._type.type = type;
_type->data._type.ptr = ptr;
ast.set_id(_id, id);
ast.to(to_parent);
#if 0
printf("[%04d:%03d] Global> %04X '%s'\n", clexer.get_line(), clexer.get_column(), id->value._int, id->name.c_str());
#endif
}
if (lexer.is_operator(op_comma)) {
match_operator(op_comma);
}
}
next();
}
void cparser::expect(bool flag, const string_t &info) {
if (!flag) {
error(info);
}
}
void cparser::match_keyword(keyword_t type) {
expect(lexer.is_keyword(type), string_t("expect keyword ") + KEYWORD_STRING(type));
next();
}
void cparser::match_operator(operator_t type) {
expect(lexer.is_operator(type), string_t("expect operator " + OPERATOR_STRING(type)));
next();
}
void cparser::match_type(lexer_t type) {
expect(lexer.is_type(type), string_t("expect type " + LEX_STRING(type)));
next();
}
void cparser::match_number() {
expect(lexer.is_number(), "expect number");
next();
}
void cparser::match_integer() {
expect(lexer.is_integer(), "expect integer");
next();
}
// 处理基本类型
// 分char,short,int,long以及相应的无符号类型(无符号暂时不支持)
// 以及float和double(暂时不支持)
lexer_t cparser::parse_type() {
auto type = l_int;
if (lexer.is_type(l_keyword)) {
auto _unsigned = false;
if (lexer.is_keyword(k_unsigned)) { // 判定是否带有unsigned前缀
_unsigned = true;
match_keyword(k_unsigned);
}
type = lexer.get_typeof(_unsigned); // 根据keyword得到lexer_t
match_type(l_keyword);
}
return type;
}
void cparser::error(const string_t &info) {
printf("[%04d:%03d] ERROR: %s\n", lexer.get_line(), lexer.get_column(), info.c_str());
throw std::exception();
}
}