-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSyntaxAnalyse.cpp
623 lines (587 loc) · 19.4 KB
/
SyntaxAnalyse.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
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
//
// Created by IceCapriccio on 2018/11/28.
//
#include "SyntaxAnalyse.h"
extern string name[];
// 匹配一个终结符
AbstractSyntaxTreeNode* SyntaxAnalyse::MatchToken(Type expect,bool flag) {
// flag = true:声明一个标识符
// flag = false:调用一个标识符
Type lexType = lex[cur].type;
if (lexType != expect) {
SyntaxError();
}
if (lexType == Identifier) {
if (flag) {
if (SyntaxAnalyse::symbolTable.query(lex[cur].value) != -1) {
SyntaxError();
} else {
SyntaxAnalyse::symbolTable.insert(lex[cur].value, depth);
}
} else {
if (SyntaxAnalyse::symbolTable.query(lex[cur].value) == -1) {
SyntaxError();
}
}
}
if(lexType == RightBrace) {
// SyntaxAnalyse::symbolTable.del(depth);
depth--;
}
if(lexType == LeftBrace) {
depth++;
}
auto *node = new AbstractSyntaxTreeNode;
node->info = lex[cur];
cur++;
return node;
}
// 语法分析错误
void SyntaxAnalyse::SyntaxError() {
printf("syntax error!\n");
exit(1);
}
AbstractSyntaxTreeNode* SyntaxAnalyse::ParseProgram() {
auto root = new AbstractSyntaxTreeNode;
root->info.value = "Program";
switch (lex[cur].type) {
case MainKey:
root->child[root->childNum++] = MatchToken(MainKey, false);
root->child[root->childNum++] = MatchToken(LeftParentheses, false);
root->child[root->childNum++] = MatchToken(RightParentheses, false);
root->child[root->childNum++] = MatchToken(LeftBrace, false);
root->child[root->childNum++] = ParseDeclarationSequence();
root->child[root->childNum++] = ParseStatementSequence();
root->child[root->childNum++] = MatchToken(RightBrace, false);
break;
default:
SyntaxError();
}
return root;
}
AbstractSyntaxTreeNode* SyntaxAnalyse::ParseDeclarationSequence() {
auto root = new AbstractSyntaxTreeNode;
root->info.value = "DeclarationSequence";
switch (lex[cur].type) {
case IntKey:
root->child[root->childNum++] = ParseDeclarationStatement();
root->child[root->childNum++] = ParseDeclarationSequence();
break;
case RightBrace:
case IfKey:
case WhileKey:
case ForKey:
case ScanfKey:
case PrintfKey:
case LeftBrace:
case Identifier:
break;
default:
SyntaxError();
}
return root;
}
AbstractSyntaxTreeNode* SyntaxAnalyse::ParseDeclarationStatement() {
auto root = new AbstractSyntaxTreeNode;
root->info.value = "DeclarationStatement";
switch (lex[cur].type) {
case IntKey:
root->child[root->childNum++] = MatchToken(IntKey, false);
root->child[root->childNum++] = ParseIdentifierTable();
root->child[root->childNum++] = MatchToken(Semicolon, false);
break;
default:
SyntaxError();
}
return root;
}
AbstractSyntaxTreeNode* SyntaxAnalyse::ParseIdentifierTable() {
auto root = new AbstractSyntaxTreeNode;
root->info.value = "IdentifierTable";
switch (lex[cur].type) {
case Identifier:
root->child[root->childNum++] = MatchToken(Identifier, true);
root->child[root->childNum++] = ParseIdentifierTableS();
break;
default:
SyntaxError();
break;
}
return root;
}
AbstractSyntaxTreeNode* SyntaxAnalyse::ParseIdentifierTableS() {
auto root = new AbstractSyntaxTreeNode;
root->info.value = "IdentifierTableS";
switch (lex[cur].type) {
case Comma:
root->child[root->childNum++] = MatchToken(Comma, false);
root->child[root->childNum++] = ParseIdentifierTable();
break;
case Semicolon:
break;
default:
SyntaxError();
}
return root;
}
AbstractSyntaxTreeNode* SyntaxAnalyse::ParseStatementSequence() {
auto root = new AbstractSyntaxTreeNode;
root->info.value = "StatementSequence";
switch (lex[cur].type) {
case IfKey:
case WhileKey:
case ForKey:
case ScanfKey:
case PrintfKey:
case LeftBrace:
case Identifier:
root->child[root->childNum++] = ParseSentence();
root->child[root->childNum++] = ParseStatementSequence();
break;
case RightBrace:
break;
default:
SyntaxError();
}
return root;
}
AbstractSyntaxTreeNode* SyntaxAnalyse::ParseSentence() {
auto root = new AbstractSyntaxTreeNode;
root->info.value = "Sentence";
switch (lex[cur].type) {
case IfKey:
root->child[root->childNum++] = ParseIfSentence();
break;
case WhileKey:
root->child[root->childNum++] = ParseWhileSentence();
break;
case ForKey:
root->child[root->childNum++] = ParseForSentence();
break;
case ScanfKey:
root->child[root->childNum++] = ParseScanfSentence();
break;
case PrintfKey:
root->child[root->childNum++] = ParsePrintfSentence();
break;
case LeftBrace:
root->child[root->childNum++] = ParseCompoundStatement();
break;
case Identifier:
case Number:
case LeftParentheses:
root->child[root->childNum++] = ParseAssignmentStatement();
break;
default:
SyntaxError();
}
return root;
}
AbstractSyntaxTreeNode* SyntaxAnalyse::ParseIfSentence() {
auto root = new AbstractSyntaxTreeNode;
root->info.value = "IfSentence";
switch (lex[cur].type) {
case IfKey:
root->child[root->childNum++] = MatchToken(IfKey, false);
root->child[root->childNum++] = MatchToken(LeftParentheses, false);
root->child[root->childNum++] = ParseExpression();
root->child[root->childNum++] = MatchToken(RightParentheses, false);
root->child[root->childNum++] = ParseCompoundStatement();
root->child[root->childNum++] = ParseElseSentence();
break;
default:
SyntaxError();
}
return root;
}
AbstractSyntaxTreeNode* SyntaxAnalyse::ParseElseSentence() {
auto root = new AbstractSyntaxTreeNode;
root->info.value = "ElseSentence";
switch (lex[cur].type) {
case ElseKey:
MatchToken(ElseKey, false);
root->child[root->childNum++] = ParseCompoundStatement();
break;
case ForKey:
case WhileKey:
case Identifier:
case IfKey:
case PrintfKey:
case ScanfKey:
case LeftBrace:
case RightBrace:
break;
default:
SyntaxError();
}
return root;
}
AbstractSyntaxTreeNode* SyntaxAnalyse::ParseWhileSentence() {
auto root = new AbstractSyntaxTreeNode;
root->info.value = "WhileSentence";
switch (lex[cur].type) {
case WhileKey:
root->child[root->childNum++] = MatchToken(WhileKey, false);
root->child[root->childNum++] = MatchToken(LeftParentheses, false);
root->child[root->childNum++] = ParseExpression();
root->child[root->childNum++] = MatchToken(RightParentheses, false);
root->child[root->childNum++] = ParseCompoundStatement();
break;
default:
SyntaxError();
}
return root;
}
AbstractSyntaxTreeNode* SyntaxAnalyse::ParseForSentence() {
auto root = new AbstractSyntaxTreeNode;
root->info.value = "ForSentence";
switch (lex[cur].type) {
case ForKey:
root->child[root->childNum++] = MatchToken(ForKey, false);
root->child[root->childNum++] = MatchToken(LeftParentheses, false);
root->child[root->childNum++] = ParseExpression();
root->child[root->childNum++] = MatchToken(Semicolon, false);
root->child[root->childNum++] = ParseExpression();
root->child[root->childNum++] = MatchToken(Semicolon, false);
root->child[root->childNum++] = ParseExpression();
root->child[root->childNum++] = MatchToken(RightParentheses, false);
root->child[root->childNum++] = ParseCompoundStatement();
break;
default:
SyntaxError();
}
return root;
}
AbstractSyntaxTreeNode* SyntaxAnalyse::ParseCompoundStatement() {
auto root = new AbstractSyntaxTreeNode;
root->info.value = "CompoundStatement";
switch (lex[cur].type) {
case LeftBrace:
root->child[root->childNum++] = MatchToken(LeftBrace, false);
root->child[root->childNum++] = ParseStatementSequence();
root->child[root->childNum++] = MatchToken(RightBrace, false);
break;
default:
SyntaxError();
}
return root;
}
AbstractSyntaxTreeNode* SyntaxAnalyse::ParseAssignmentStatement() {
auto root = new AbstractSyntaxTreeNode;
root->info.value = "AssignmentStatement";
switch (lex[cur].type) {
case Identifier:
case Number:
case LeftParentheses:
root->child[root->childNum++] = ParseExpression();
root->child[root->childNum++] = MatchToken(Semicolon, false);
break;
default:
SyntaxError();
}
return root;
}
AbstractSyntaxTreeNode* SyntaxAnalyse::ParseExpression() {
AbstractSyntaxTreeNode *root = new AbstractSyntaxTreeNode;
root -> info.value = "Expression";
switch (lex[cur].type) {
case Identifier:
root ->child[root ->childNum++] = MatchToken(Identifier, false);
root -> child[root -> childNum++] = ParseExpressionS();
break;
default:
SyntaxError();
}
return root;
}
AbstractSyntaxTreeNode* SyntaxAnalyse::ParseExpressionS() {
AbstractSyntaxTreeNode *root = new AbstractSyntaxTreeNode;
root -> info.value = "ExpressionS";
switch (lex[cur].type) {
case Assignment:
root -> child[root -> childNum++] = MatchToken(Assignment, false);
root -> child[root -> childNum++] = ParseArithmeticExpression();
break;
case Add:
case Subtract:
case Multiply:
case Divide:
case GreaterThan:
case LessThan:
case NoGreaterThan:
case NoLessThan:
case Equal:
case Unequal:
case Semicolon:
case RightParentheses:
root -> child[root -> childNum++] = ParseRel();
break;
default:
SyntaxError();
}
return root;
}
AbstractSyntaxTreeNode* SyntaxAnalyse::ParseRel() {
AbstractSyntaxTreeNode* root = new AbstractSyntaxTreeNode;
root -> info.value = "Rel";
switch (lex[cur].type) {
case Add:
case Subtract:
case Multiply:
case Divide:
case LessThan:
case GreaterThan:
case NoGreaterThan:
case NoLessThan:
case Equal:
case Unequal:
root -> child[root -> childNum++] = ParseOpt();
root -> child[root -> childNum++] = ParseRelationOperator();
root -> child[root -> childNum++] = ParseArithmeticExpression();
break;
case Semicolon:
case RightParentheses:
break;
default:
SyntaxError();
}
return root;
}
AbstractSyntaxTreeNode* SyntaxAnalyse::ParseOpt() {
AbstractSyntaxTreeNode* root = new AbstractSyntaxTreeNode;
root -> info.value = "Opt";
switch (lex[cur].type) {
case Add:
root ->child[root -> childNum++] = MatchToken(Add, false);
root ->child[root -> childNum++] = ParseArithmeticExpression();
break;
case Subtract:
root -> child[root -> childNum++] = MatchToken(Subtract, false);
root -> child[root -> childNum++] = ParseArithmeticExpression();
break;
case Multiply:
root ->child[root -> childNum++] = MatchToken(Multiply, false);
root ->child[root -> childNum++] = ParseArithmeticExpression();
break;
case Divide:
root -> child[root -> childNum++] = MatchToken(Divide, false);
root -> child[root -> childNum++] = ParseArithmeticExpression();
break;
case GreaterThan:
case LessThan:
case NoGreaterThan:
case NoLessThan:
case Equal:
case Unequal:
break;
default:
SyntaxError();
}
return root;
}
AbstractSyntaxTreeNode* SyntaxAnalyse::ParseRelationOperator()
{
AbstractSyntaxTreeNode *root = new AbstractSyntaxTreeNode;
root -> info.value = "RelationOperator";
switch (lex[cur].type)
{
case GreaterThan:
root -> child[root -> childNum++] = MatchToken(GreaterThan, false);
break;
case LessThan:
root -> child[root -> childNum++] = MatchToken(LessThan, false);
break;
case NoLessThan:
root -> child[root -> childNum++] = MatchToken(NoLessThan, false);
break;
case NoGreaterThan:
root -> child[root -> childNum++] = MatchToken(NoGreaterThan, false);
break;
case Unequal:
root -> child[root -> childNum++] = MatchToken(Unequal, false);
break;
case Equal:
root -> child[root -> childNum++] = MatchToken(Equal, false);
break;
default:
SyntaxError();
}
return root;
}
AbstractSyntaxTreeNode* SyntaxAnalyse::ParseArithmeticExpression()
{
AbstractSyntaxTreeNode *root = new AbstractSyntaxTreeNode;
root -> info.value = "ArithmeticExpression";
switch (lex[cur].type)
{
case Identifier:
root -> child[root -> childNum++] = ParseTerm();
root -> child[root -> childNum++] = ParseOparit();
break;
case Number:
root -> child[root -> childNum++] = ParseTerm();
root -> child[root -> childNum++] = ParseOparit();
break;
case LeftParentheses:
root -> child[root -> childNum++] = ParseTerm();
root -> child[root -> childNum++] = ParseOparit();
break;
default:
SyntaxError();
}
return root;
}
AbstractSyntaxTreeNode* SyntaxAnalyse::ParseOparit()
{
AbstractSyntaxTreeNode* root = new AbstractSyntaxTreeNode;
root -> info.value= "Oparit";
switch (lex[cur].type)
{
case Add:
root -> child[root -> childNum++] = MatchToken(Add, false);
root -> child[root -> childNum++] = ParseArithmeticExpression();
break;
case Subtract:
root -> child[root -> childNum++] = MatchToken(Subtract, false);
root -> child[root -> childNum++] = ParseArithmeticExpression();
break;
case LessThan:break;
case GreaterThan:break;
case NoGreaterThan:break;
case NoLessThan:break;
case Unequal:break;
case Equal:break;
case RightParentheses:break;
case Semicolon:break;
default:
SyntaxError();
}
return root;
}
AbstractSyntaxTreeNode* SyntaxAnalyse::ParseTerm()
{
AbstractSyntaxTreeNode* root = new AbstractSyntaxTreeNode;
root -> info.value = "Term";
switch (lex[cur].type)
{
case Identifier:
root -> child[root -> childNum++] = ParseFactor();
root -> child[root -> childNum++] = ParseOpterm();
break;
case Number:
root -> child[root -> childNum++] = ParseFactor();
root -> child[root -> childNum++] = ParseOpterm();
break;
case LeftParentheses:
root -> child[root -> childNum++] = ParseFactor();
root -> child[root -> childNum++] = ParseOpterm();
break;
default:
SyntaxError();
}
return root;
}
AbstractSyntaxTreeNode* SyntaxAnalyse::ParseOpterm()
{
AbstractSyntaxTreeNode* root = new AbstractSyntaxTreeNode;
root -> info.value = "Opterm";
switch (lex[cur].type)
{
case Multiply:
root -> child[root -> childNum++] = MatchToken(Multiply, false);
root -> child[root -> childNum++] = ParseTerm();
break;
case Divide:
root -> child[root -> childNum++] = MatchToken(Divide, false);
root -> child[root -> childNum++] = ParseTerm();
break;
case Add:break;
case Subtract:break;
case LessThan:break;
case GreaterThan:break;
case NoLessThan:break;
case NoGreaterThan:break;
case Equal:break;
case Unequal:break;
case Semicolon:break;
case RightParentheses:break;
default:
SyntaxError();
}
return root;
}
AbstractSyntaxTreeNode* SyntaxAnalyse::ParseFactor()
{
AbstractSyntaxTreeNode* root = new AbstractSyntaxTreeNode;
root -> info.value = "Factor";
switch (lex[cur].type)
{
case Identifier:
root -> child[root -> childNum++] = MatchToken(Identifier, false);
break;
case Number:
root -> child[root -> childNum++] = MatchToken(Number, false);
break;
case LeftParentheses:
root -> child[root -> childNum++] = MatchToken(LeftParentheses, false);
root -> child[root -> childNum++] = ParseArithmeticExpression();
root -> child[root -> childNum++] = MatchToken(RightParentheses, false);
break;
default:
SyntaxError();
}
return root;
}
AbstractSyntaxTreeNode* SyntaxAnalyse::ParseScanfSentence()
{
auto root = new AbstractSyntaxTreeNode;
root->info.value = "ScanfSentence";
switch (lex[cur].type)
{
case ScanfKey:
root -> child[root -> childNum++] = MatchToken(ScanfKey, false);
root -> child[root -> childNum++] = MatchToken(LeftParentheses, false);
root -> child[root -> childNum++] = MatchToken(Identifier,false);
root -> child[root -> childNum++] = MatchToken(RightParentheses, false);
root -> child[root -> childNum++] = MatchToken(Semicolon, false);
break;
default:SyntaxError();
}
return root;
}
AbstractSyntaxTreeNode* SyntaxAnalyse::ParsePrintfSentence()
{
AbstractSyntaxTreeNode *root = new AbstractSyntaxTreeNode;
root -> info.value = "PrintfSentence";
switch (lex[cur].type)
{
case PrintfKey:
root -> child[root -> childNum++] = MatchToken(PrintfKey, false);
root -> child[root -> childNum++] = MatchToken(LeftParentheses, false);
root -> child[root -> childNum++] = MatchToken(Identifier, false);
root -> child[root -> childNum++] = MatchToken(RightParentheses, false);
root -> child[root -> childNum++] = MatchToken(Semicolon, false);
break;
default:SyntaxError();
}
return root;
}
AbstractSyntaxTreeNode* SyntaxAnalyse::Parse(Node *node) {
this->lex = node;
cur = 0;
depth = 0;
return ParseProgram();
}
void SyntaxAnalyse::ShowTree(AbstractSyntaxTreeNode *root, int depth) {
for (int i = 0; i < depth; i++)
printf("-- ");
int idx = root->info.type;
printf("%s %s\n", name[idx].c_str(), root->info.value.c_str());
for (int i = 0; i < root->childNum; i++)
ShowTree(root->child[i], depth + 1);
}
SyntaxAnalyse::SyntaxAnalyse() {
cur = 0;
depth = 0;
lex = nullptr;
}