-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsyntaxanalyze.cpp
More file actions
1904 lines (1865 loc) · 40.6 KB
/
Copy pathsyntaxanalyze.cpp
File metadata and controls
1904 lines (1865 loc) · 40.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
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
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#define _CRT_SECURE_NO_WARNINGS
#include <stack>
#include <string.h>
#include "syntaxanalyze.h"
#include "lexicalanalyze.h"
using namespace std;
extern char c;
extern char token_text[100];//用于存储词法单元的全局变量
extern char string_num[100];
extern char FileName[50];//文件名
extern FILE* fp;
extern int ncount;//用于记录代码所在行数
int type;//存储变量类型
VDN* Vroot;//链表头节点
int isVoid, hasReturn, isInrecycle;
int w;//存储当前读入的单词种类编码
FDN* Froot;//函数链表头节点
/// <summary>
/// 用于调试过程中检查变量作用域
/// </summary>
void Traverse() {
VDN* p = Vroot;
while (p != NULL) {
for (int i = 0; i < (p->size); i++) {
cout << p->datas.variable[i] << " " << p->datas.type[i] << " ";
}
p = p->next;
cout << endl;
}
}
/// <summary>
/// 生成输出语法树
/// </summary>
void syntaxAnalyze() {
cout << "请输入文件名:" << endl;
cin >> FileName;
fp = fopen(FileName, "r");
if (fp == NULL) {//打开失败
cout << "文件打开失败";
return;
}
ASTTree* root = program();
if (root == NULL) {
cout << "有语法错误";
fclose(fp);//关闭文件
return;
}
else {
PreTraverse(root, 0);
//Traverse();
freeTree(root);
fclose(fp);//关闭文件
return;
}
}
/// <summary>
/// 释放语法树内存
/// </summary>
/// <param name="root">语法树根节点</param>
void freeTree(ASTTree* root) {
if (root) {
freeTree(root->l);
freeTree(root->r);
delete(root);
}
}
/// <summary>
/// 读取下一个词法单元,同时跳过头文件,注释,宏
/// </summary>
void skip() {
w = gettoken(fp)->type;
while (w == ANNO || w == INCLUDE || w == MACRO) w = gettoken(fp)->type;
if (w == ERROR_TOKEN) {
cout << "第" << ncount << "行出现错误" << endl;
cout << "错误原因:错误的词法单元" << endl;
exit(0);
}
}
/// <summary>
/// 先序遍历输出抽象语法树
/// </summary>
/// <param name="root">程序根节点</param>
/// <param name="depth">语句深度</param>
void PreTraverse(ASTTree* root, int depth) {
if (root == NULL) {
cout << " ";
return;
}
else {
int i = 0;
//for (i = 0; i < depth; i++) cout << " ";
showType(root->type);
if (root->data.text != NULL) {
cout << root->data.text << endl;
}
PreTraverse(root->l, depth + 1);
PreTraverse(root->r, depth + 1);
}
}
/// <summary>
/// 输出各个语法单元类型
/// </summary>
/// <param name="type">结点类型</param>
void showType(int type) {
switch (type) {
case 1:
//cout << "外部定义序列" << endl;
break;
case 2:
cout << "外部变量定义" << endl;
break;
case 3:
cout << "外部变量定义类型:";
break;
case 4:
//cout << "外部变量名称序列" << endl;
break;
case 5:
cout << "外部变量名称:";
break;
case 6:
cout << "函数定义" << endl;
break;
case 7:
cout << "函数返回类型:";
break;
case 8:
cout << "函数名称:";
break;
case 9:
cout << "函数参数序列" << endl;
break;
case 10:
//cout << "函数参数定义" << endl;
break;
case 11:
cout << "函数参数定义类型:";
break;
case 12:
cout << "函数参数名称:";
break;
case 13:
cout << "函数体" << endl;
break;
case 14:
//cout << "局部变量定义序列" << endl;
break;
case 15:
cout << "局部变量定义" << endl;
break;
case 16:
cout << "局部变量定义类型:";
break;
case 17:
cout << "局部变量名称序列" << endl;
break;
case 18:
cout << "局部变量名称:";
break;
case 19:
//cout << "语句序列" << endl;
break;
case 20:
cout << "操作数:";
break;
case 21:
cout << "运算符:";
break;
case 22:
cout << "表达式" << endl;
break;
case 23:
cout << "if语句条件部分" << endl;
break;
case 24:
cout << "else语句部分" << endl;
break;
case 25:
cout << "if语句" << endl;
break;
case 26:
cout << "if-else语句" << endl;
break;
case 27:
cout << "while语句" << endl;
break;
case 28:
cout << "while语句条件部分" << endl;
break;
case 29:
cout << "while语句体" << endl;
break;
case 30:
cout << "for语句" << endl;
break;
case 31:
cout << "for语句条件" << endl;
break;
case 32:
cout << "for语句条件1" << endl;
break;
case 33:
cout << "for语句条件2" << endl;
break;
case 34:
cout << "for语句条件3" << endl;
break;
case 35:
cout << "for语句体" << endl;
break;
case 36:
cout << "return语句" << endl;
break;
case 37:
cout << "break语句" << endl;
break;
case 38:
cout << "do-while语句" << endl;
break;
case 39:
cout << "do-while语句体" << endl;
break;
case 40:
cout << "do-while语句条件部分" << endl;
break;
case 41:
cout << "continue语句" << endl;
break;
case 42:
cout << "函数声明" << endl;
break;
case 43:
cout << "数组定义" << endl;
break;
case 44:
cout << "数组类型:";
break;
case 45:
cout << "数组名称:";
break;
case 46:
cout << "数组大小:";
break;
case 47:
cout << "数组定义序列" << endl;
break;
case 48:
cout << "赋值为:";
}
}
/// <summary>
/// 生成程序根结点
/// </summary>
/// <returns>程序根结点</returns>
ASTTree* program() {
skip();
//初始化节点
Vroot = new VDN;
//变量个数为0
Vroot->size = 0;
Vroot->next = NULL;
Froot = new FDN;
Froot->next = NULL;
Froot->size = 0;
//生成外部定义序列结点
ASTTree* p = ExtDefList();
if (p != NULL) {
//新建根节点,将其设为根结点
ASTTree* root = new ASTTree;
root->l = NULL;
root->r = NULL;
root = p;
root->type = EXTDEFLIST;//结点类型
return root;
}
else return NULL;
}
/// <summary>
/// 生成外部定义序列结点
/// </summary>
/// <returns>外部定义序列结点</returns>
ASTTree* ExtDefList() {
// 判断是否到达文件结束
if (w == ERROR)
return NULL;
//创建根结点
ASTTree* node = new ASTTree;
node->type = EXTDEFLIST;
node->data.text = NULL;
//左子树解析一个外部定义
node->l = ExtDef();
skip();
//递归调用,继续解析后续外部定义
node->r = ExtDefList();
return node;
}
/// <summary>
/// 生成外部定义结点
/// </summary>
/// <returns>外部定义结点</returns>
ASTTree* ExtDef() {
int a;
//不是变量定义类型
if (w != INT && w != DOUBLE && w != CHAR && w != LONG && w != SHORT && w != FLOAT && w != VOID) {
cout << "第" << ncount << "行出现错误" << endl;
cout << "错误原因:错误的外部定义";
exit(0);
}
//存储变量定义类型,便于后续保存使用
type = w;
skip();
//既不是标识符也不是数组
if (w != IDENT && w != ARRAY) {
cout << "第" << ncount << "行出现错误" << endl;
cout << "错误原因:错误的外部定义";
exit(0);
}
//存储变量类型,便于后续保存使用
a = w;
//外部定义结点
ASTTree* p = new ASTTree;
p->l = NULL;
p->r = NULL;
char* token_text0 = new char[25];
strcpy(token_text0, token_text);
skip();
strcpy(token_text, token_text0);
if (w == LP) {
p = FuncDef();//左括号,函数定义
hasReturn = 0;
}
else if (a == ARRAY) p = ArrayDef();//变量名为数组,数组定义
else p = ExtVarDef();//外部变量定义
return p;
}
/// <summary>
/// 处理数组名称
/// </summary>
void arraySolve(char* token_text) {
string s1;
for (int i = 0;; i++) {
if (token_text[i] == '[') break;
else s1[i] = token_text[i];
}
strcpy(token_text, s1.c_str());
}
/// <summary>
/// 生成数组定义结点
/// </summary>
/// <returns>数组定义结点</returns>
ASTTree* ArrayDef() {
//void不能定义数组,报错
if (type == VOID) {
cout << "第" << ncount << "行出现错误" << endl;
cout << "错误原因:不能定义void类型的数组";
exit(0);
}
arraySolve(token_text);
//将变量加入链表
addNameF(token_text);
//数组定义节点初始化
ASTTree* node = new ASTTree;
node->type = ARRAYDEF;
node->l = NULL;
node->r = NULL;
node->data.text = NULL;
//数组类型,是定义结点左子树
ASTTree* p = new ASTTree;
p->type = ARRAYTYPE;
p->l = NULL;
p->r = NULL;
//存储数组类型
char* token_text4 = new char[25];
if (type == INT) strcpy(token_text4, "int");
else if (type == DOUBLE) strcpy(token_text4, "double");
else if (type == CHAR) strcpy(token_text4, "char");
else if (type == FLOAT) strcpy(token_text4, "float");
else if (type == LONG) strcpy(token_text4, "long");
else if (type == SHORT) strcpy(token_text4, "short");
p->data.text = token_text4;
node->l = p;
//数组名称,是定义结点右子树
ASTTree* q = new ASTTree;
q->type = ARRAYNAME;
q->l = NULL;
q->r = NULL;
char* token_text3 = new char[25];
strcpy(token_text3, token_text);
q->data.text = token_text3;
//继续存储数组大小
node->r = q;
ASTTree* m = new ASTTree;
m->type = ARRAYSIZE;
m->l = NULL;
m->r = NULL;
char* token_text5 = new char[25];
strcpy(token_text5, string_num);
//数组大小值存储
m->data.text = token_text5;
//是名称结点左孩子结点
q->l = m;
return node;
}
/// <summary>
/// 将变量名称加入到变量定义链表中
/// </summary>
/// <param name="token_text"></param>
/// <returns></returns>
void addName(char* token_text) {
int i, flag = 0;
//链表头节点
VDN* p = Vroot;
//到链表末尾
while (p->next != NULL) p = p->next;
for (i = 0; i < (p->size); i++) {
//变量名称重复
if (strcmp(token_text, p->datas.variable[i]) == 0) {
flag = 1;
break;
}
}
if (flag == 1) {
cout << "第" << ncount << "行出现错误" << endl;
cout << "错误原因:重复的变量定义";
exit(0);
}
//保存变量名
char* savename = new char[25];
strcpy(savename, token_text);
p->datas.variable[p->size] = savename;
p->datas.type[p->size] = type;
p->size++;
return;
}
/// <summary>
/// 将变量名称加入到全局变量定义链表中
/// </summary>
/// <param name="token_text"></param>
/// <returns></returns>
void addNameF(char* token_text) {
int i, flag = 0;
//链表头节点
VDN* p = Vroot;
//到链表末尾
for (i = 0; i < (p->size); i++) {
//变量名称重复
if (strcmp(token_text, p->datas.variable[i]) == 0) {
flag = 1;
break;
}
}
if (flag == 1) {
cout << "第" << ncount << "行出现错误" << endl;
cout << "错误原因:重复的变量定义";
exit(0);
}
//保存变量名
char* savename = new char[25];
strcpy(savename, token_text);
p->datas.variable[p->size] = savename;
p->datas.type[p->size] = type;
p->size++;
return;
}
/// <summary>
/// 将函数名称加入到全局变量定义链表中
/// </summary>
/// <param name="token_text"></param>
/// <returns></returns>
void addNameFunc(char* token_text) {
int i, flag = 0;
//链表头节点
VDN* p = Vroot;
//到链表末尾
for (i = 0; i < (p->size); i++) {
//变量名称重复
if (strcmp(token_text, p->datas.variable[i]) == 0) {
flag = 1;
break;
}
}
if (flag == 1) {
cout << "第" << ncount << "行出现错误" << endl;
cout << "错误原因:重复的变量定义";
exit(0);
}
//保存变量名
char* savename = new char[25];
strcpy(savename, token_text);
p->datas.variable[p->size] = savename;
FDN* q = Froot;
while (q->next != NULL) q = q->next;
char* savenames = new char[25];
strcpy(savenames, token_text);
q->fName = savenames;
p->datas.type[p->size] = type;
p->datas.isFunc[p->size++] = 1;
FDN* last = Froot;
while (last->next != NULL) {
last = last->next;
}
last->next = new FDN;
last = last->next;
last->next = NULL;
last->size = 0;
return;
}
/// <summary>
/// 用于转化局部变量类型
/// </summary>
/// <param name="token_text"></param>
/// <returns></returns>
int trans(char* token_text) {
VDN* p = Vroot;
while (p->next != NULL) p = p->next;
int i = 0;
int flag = 0;
int flags = 0;
for (i = 0; i < (p->size); i++) {
if (strcmp(token_text, p->datas.variable[i]) == 0) {
flag = 1;
flags = 1;
break;
}
}
if (flags == 0) {
for (i = 0; i < (Vroot->size); i++) {
if (strcmp(token_text, Vroot->datas.variable[i]) == 0) {
p = Vroot;
flag = 1;
break;
}
}
}
if (flag == 1 && p->datas.isFunc[i] == 0) {
if (p->datas.type[i] == INT) return INT_CONST;
else if (p->datas.type[i] == DOUBLE) return FLOAT_CONST;
else if (p->datas.type[i] == CHAR) return CHAR_CONST;
else if (p->datas.type[i] == FLOAT) return FLOAT_CONST;
else if (p->datas.type[i] == LONG) return INT_CONST;
else if (p->datas.type[i] == SHORT) return INT_CONST;
else return w;
}
else if (flag == 1 && p->datas.isFunc[i] == 1) {
transF(token_text);
if (p->datas.type[i] == INT) return INT_CONST;
else if (p->datas.type[i] == DOUBLE) return FLOAT_CONST;
else if (p->datas.type[i] == CHAR) return CHAR_CONST;
else if (p->datas.type[i] == FLOAT) return FLOAT_CONST;
else if (p->datas.type[i] == LONG) return INT_CONST;
else if (p->datas.type[i] == SHORT) return INT_CONST;
else if (p->datas.type[i] == VOID) {
return VOID;
}
}
else return w;
}
/// <summary>
/// 处理函数调用
/// </summary>
/// <param name="token_text2">可能调用的函数名</param>
void transF(char* token_text2) {
char token_text3[25];
strcpy(token_text3, token_text2);
FDN* k = Froot;
while (k->next != NULL) {
if (strcmp(k->fName, token_text3) == 0) break;
}
int i = 0;
int j = 0;//控制循环
skip();
if (w != LP) {
cout << "第" << ncount << "行出现错误" << endl;
cout << "错误原因:错误的表达式";
exit(0);
}
else {
addtoken(token_text3, '(');
while (i < k->size) {
skip();
if (j % 2 != 0 && w == COMMA) {
addtoken(token_text3, ',');
j++;
continue;
}
else if (j % 2 != 0 && w != COMMA) {
cout << "第" << ncount << "行出现错误" << endl;
cout << "错误原因:错误的表达式";
exit(0);
}
else {
w = trans(token_text);
if (w != k->name[i]) {
cout << "第" << ncount << "行出现错误" << endl;
cout << "错误原因:与函数参数类型不匹配";
exit(0);
}
strcat(token_text3, token_text);
i++;
j++;
}
}
skip();
if (w != RP) {
cout << "第" << ncount << "行出现错误" << endl;
cout << "错误原因:错误的表达式";
exit(0);
}
else {
addtoken(token_text3, ')');
strcpy(token_text2, token_text3);
}
}
}
/// <summary>
/// 生成外部变量定义节点
/// </summary>
/// <returns>外部变量定义节点</returns>
ASTTree* ExtVarDef() {
int cnt = 0;
//void不能定义变量
if (type == VOID) {
cout << "第" << ncount << "行出现错误" << endl;
cout << "错误原因:不能定义void类型的变量";
exit(0);
}
//将变量加入链表
addNameF(token_text);
//新建外部变量定义结点
ASTTree* root = new ASTTree;
root->type = EXTVARDEF;
root->l = NULL;
root->r = NULL;
root->data.text = NULL;
//存储外部变量类型,是定义节点左子树
ASTTree* p = new ASTTree;
p->type = EXTVARTYPE;
p->l = NULL;
p->r = NULL;
p->data.type = type;
char* token_text5 = new char[25];
if (type == INT) strcpy(token_text5, "int");
else if (type == DOUBLE) strcpy(token_text5, "double");
else if (type == CHAR) strcpy(token_text5, "char");
else if (type == FLOAT) strcpy(token_text5, "float");
else if (type == LONG) strcpy(token_text5, "long");
else if (type == SHORT) strcpy(token_text5, "short");
p->data.text = token_text5;
root->l = p;
//外部变量定义序列,是右子树
p = new ASTTree;
p->type = EXTVARLIST;
p->l = NULL;
p->r = NULL;
p->data.text = NULL;
root->r = p;
//存储变量名称,是外部变量定义(第二个)左子树
p->l = new ASTTree;
p->l->l = NULL;
p->l->r = NULL;
p->l->type = EXTVAR;
char* token_text1 = new char[25];
strcpy(token_text1, token_text);
p->l->data.text = token_text1;
while (1) {
//不是逗号也不是分号
if (w != COMMA && w != SEMI && w != ASSIGN) {
if (ncount > cnt) ncount--;
cout << "第" << ncount << "行出现错误" << endl;
cout << "错误原因:错误的外部定义";
exit(0);
}
//是分号,定义完成
else if (w == SEMI) return root;
else if (w == ASSIGN) {
skip();
w = trans(token_text);
if (w == VOID) {
cout << "第" << ncount << "行出现错误" << endl;
cout << "错误原因:错误的函数调用";
exit(0);
}
if (((type == INT || type == LONG || type == SHORT) && w == INT_CONST) || ((type == DOUBLE || type == FLOAT) && w == FLOAT_CONST) || (type == CHAR && w == CHAR_CONST)) {
ASTTree* m = new ASTTree;
m->l = NULL;
m->r = NULL;
m->type = ASSIGN_CONST;
char* token_text12 = new char[25];
strcpy(token_text12, token_text);
m->data.text = token_text12;
p->l->l = m;
}
else {
cout << "第" << ncount << "行出现错误" << endl;
cout << "错误原因:定义变量与类型不匹配或者所赋的值不存在";
exit(0);
}
skip();
continue;
}
skip();
//是逗号但后面不是标识符报错
if (w != IDENT) {
cout << "第" << ncount << "行出现错误" << endl;
cout << "错误原因:错误的外部定义";
exit(0);
}
addNameF(token_text);
//是逗号的情况,变量定义类型都是一样的
ASTTree* q = new ASTTree;
q->l = q->r = NULL;
q->data.text = NULL;
q->type = EXTVARLIST;
p->r = q;
p = q;
//存储变量名称
p->l = new ASTTree;
p->l->l = p->l->r = NULL;
p->l->type = EXTVAR;
char* token_text1 = new char[25];
strcpy(token_text1, token_text);
p->l->data.text = token_text1;
cnt = ncount;
skip();
}
}
/// <summary>
/// 生成函数定义节点
/// </summary>
/// <returns>函数定义节点</returns>
ASTTree* FuncDef() {
//函数定义根节点
ASTTree* root = new ASTTree;
root->l = NULL;
root->r = NULL;
root->data.text = NULL;
root->type = FUNCDEF;
//函数返回类型节点,是根节点的左子树
ASTTree* p = new ASTTree;
p->type = FUNCRETURNTYPE;
p->l = NULL;
p->r = NULL;
p->data.type = type;
char* token_text6 = new char[25];
if (type == INT) {
strcpy(token_text6, "int");
isVoid = 0;
}
if (type == DOUBLE) {
strcpy(token_text6, "double");
isVoid = 0;
}
if (type == CHAR) {
strcpy(token_text6, "char");
isVoid = 0;
}
if (type == FLOAT) {
strcpy(token_text6, "float");
isVoid = 0;
}
if (type == LONG) {
strcpy(token_text6, "long");
isVoid = 0;
}
if (type == SHORT) {
strcpy(token_text6, "short");
isVoid = 0;
}
if (type == VOID) {
strcpy(token_text6, "void");
isVoid = 1;//标记函数不返回值
}
p->data.text = token_text6;
root->l = p;
//函数名字节点,是根节点的右子树
ASTTree* q = new ASTTree;
q->type = FUNCNAME;
q->l = NULL;
q->r = NULL;
addNameFunc(token_text);
char* token_text1 = new char[25];
strcpy(token_text1, token_text);
q->data.text = token_text1;
VDN* last = Vroot;
while (last->next != NULL) {
last = last->next;
}
last->next = new VDN;
last = last->next;
last->next = NULL;
last->size = 0;
root->r = q;
//构建函数参数列表,是函数名称的左子树
q->l = FormParaList();
FDN* m = Froot;
while (m->next->next != NULL) m = m->next;
VDN* n = Vroot;
while (n->next != NULL) n = n->next;
int i = 0;
for (i = 0; i < (n->size); i++) {
if (n->datas.type[i] == INT) m->name[i] = INT_CONST;
else if (n->datas.type[i] == DOUBLE) m->name[i] = FLOAT_CONST;
else if (n->datas.type[i] == CHAR) m->name[i] = CHAR_CONST;
else if (n->datas.type[i] == FLOAT) m->name[i] = FLOAT_CONST;
else if (n->datas.type[i] == LONG) m->name[i] = INT_CONST;
else if (n->datas.type[i] == SHORT) m->name[i] = INT_CONST;
m->size++;
}
skip();
//后面为分号,是函数声明
if (w == SEMI) {
//函数名称结点右子树为空
root->r->r = NULL;
//函数定义结点标记为函数声明
root->type = FUNCCLAIM;
}
//后面为左大括号,为函数体
else if (w == LBRACE) {
q->r = CompState();
q->r->type = FUNCBODY;
if (isVoid == 0 && hasReturn == 0) {
cout << "程序出现错误" << endl;
cout << "错误原因:函数缺少返回值";
exit(0);
}
}
else {
cout << "第" << ncount << "行出现错误" << endl;
cout << "错误原因:错误的函数定义或者声明";
exit(0);
}
return root;
}
/// <summary>
/// 生成函数参数序列节点
/// </summary>
/// <returns>函数参数序列节点</returns>
ASTTree* FormParaList() {
skip();
//如果为右小括号,不含参数
if (w == RP) return NULL;
//为逗号,跳过
if (w == COMMA) skip();
//新建函数参数列表根节点
ASTTree* root = new ASTTree;
root->l = NULL;
root->r = NULL;
root->data.text = NULL;
root->type = FUNCFORMALPARALIST;
//左子树存定义,右子树为形参序列
root->l = FormParaDef();
root->r = FormParaList();
return root;
}
/// <summary>
/// 生成函数参数定义
/// </summary>
/// <returns>函数参数定义</returns>
ASTTree* FormParaDef() {
if (w != INT && w != DOUBLE && w != CHAR && w != LONG && w != SHORT && w != FLOAT && w != STRING_CONST) {
cout << "第" << ncount << "行出现错误" << endl;
cout << "错误原因:错误的函数参数定义";
exit(0);
}
//存函数参数类型
type = w;
skip();
//不是标识符,报错
if (w != IDENT) {
cout << "第" << ncount << "行出现错误" << endl;
cout << "错误原因:错误的函数参数定义";
exit(0);
}
//新建函数参数定义根节点
ASTTree* root = new ASTTree;
root->type = FUNCFORMALPARADEF;
root->l = NULL;
root->r = NULL;
root->data.text = NULL;
//函数参数类型节点,是根节点的左子树
ASTTree* p = new ASTTree;
p->type = FUNCFORMALPARATYPE;
p->l = NULL;
p->r = NULL;
p->data.type = type;
char* token_text7 = new char[25];
if (type == INT) strcpy(token_text7, "int");
if (type == DOUBLE) strcpy(token_text7, "double");
if (type == CHAR) strcpy(token_text7, "char");
if (type == FLOAT) strcpy(token_text7, "float");
if (type == LONG) strcpy(token_text7, "long");
if (type == SHORT) strcpy(token_text7, "short");
p->data.text = token_text7;
root->l = p;
//函数参数名称结点,是根节点的右子树
p = new ASTTree;
p->type = FUNCFORMALPARA;
p->l = NULL;
p->r = NULL;
char* token_text1 = new char[25];
strcpy(token_text1, token_text);
p->data.text = token_text1;
addName(token_text1);
root->r = p;
return root;
}
/// <summary>
/// 生成复合语句结点
/// </summary>
/// <returns>复合语句结点</returns>
ASTTree* CompState() {
//新建函数体结点,类型在FuncDef函数里面
ASTTree* root = new ASTTree;
root->data.text = NULL;
root->l = NULL;
root->r = NULL;
skip();
//函数体内部定义
if (w == INT || w == DOUBLE || w == CHAR || w == LONG || w == SHORT || w == FLOAT) root->l = LocalVarDefList();
else root->l = NULL;
root->r = StateList();
//是右大括号,调用完毕
if (w == RBRACE) return root;
else {
cout << "第" << ncount << "行出现错误" << endl;
cout << "错误原因:复合语句部分出错";
exit(0);
}
}
/// <summary>
/// 生成局部变量定义结点
/// </summary>
/// <returns>局部变量定义结点</returns>
ASTTree* LocalVarDefList() {
//创建局部变量定义序列根节点
ASTTree* root = new ASTTree;
root->type = LOCALVARDEFLIST;
root->data.text = NULL;
root->l = NULL;
root->r = NULL;
//局部变量定义结点,是根节点的左子树
ASTTree* p = new ASTTree;
p->data.text = NULL;
p->type = LOCALVARDEF;
p->l = NULL;
p->r = NULL;
root->l = p;
//局部变量类型结点,是局部变量定义结点的左子树
p->l = new ASTTree;
p->l->l = NULL;
p->l->r = NULL;
p->l->type = LOCALVARTYPE;
type = w;
char* token_text1 = new char[25];
strcpy(token_text1, token_text);
p->l->data.text = token_text1;
skip();
//局部变量名称序列结点,是局部变量定义结点的右子树
ASTTree* q = new ASTTree;
q->data.text = NULL;
q->l = NULL;
q->r = NULL;
p->r = q;
q->type = LOCALVARNAMELIST;
//局部变量名称结点
q->l = new ASTTree;
q->l->type = LOCALVARNAME;