-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.cpp
More file actions
849 lines (720 loc) · 21.5 KB
/
parser.cpp
File metadata and controls
849 lines (720 loc) · 21.5 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
//*****************************************************************************
// purpose: Recursive descent parsing of arithmetic expressions
// Builds the parse tree while parsing the input
// version: Spring 2025
// author: Delvin Buckley
//*****************************************************************************
#include "parser.h"
#include "nodes.h"
#include <stdlib.h>
#include <iostream>
using namespace std;
// Forward declarations of first_of functions
bool first_of_program(); // program should start with TOK_PROGRAM
bool first_of_block(); // block should start with TOK_VAR or TOK_BEGIN
bool first_of_statement(); // statement should start with TOK_IDENT, TOK_BEGIN, TOK_IF, TOK_WHILE, TOK_READ, or TOK_WRITE
bool first_of_compound_statement(); // compound statement should start with TOK_BEGIN
bool first_of_expression(); // expression should start with TOK_IDENT, TOK_INTLIT, TOK_FLOATLIT, or TOK_OPENPAREN
bool first_of_simple_expression(); // simple expression should start with TOK_IDENT, TOK_INTLIT, TOK_FLOATLIT, or TOK_OPENPAREN
bool first_of_term(); // term should start with TOK_IDENT, TOK_INTLIT, TOK_FLOATLIT, or TOK_OPENPAREN
bool first_of_factor(); // factor should start with TOK_IDENT, TOK_INTLIT, TOK_FLOATLIT, or TOK_OPENPAREN
int nextToken = 0; // hold nextToken returned by lex
bool printParse = false; // shall we print the parse tree?
bool printTree = false;
//*****************************************************************************
// Holds variable names and their types and values
symbolTableT symbolTable;
// Determine if a symbol is in the symbol table
bool inSymbolTable(string idName) {
symbolTableT::iterator it;
it = symbolTable.find(idName);
// If idName is missing, will be set to the end
return !(it == symbolTable.end());
}
// Which tree level are we currently in? Setting this to -1
// means the top-level expression is at level 0.
static int level = -1;
// Handle syntax errors
void error() {
cout << endl << "===========================" << endl;
cout << "ERROR near: " << yytext;
cout << endl << "===========================" << endl;
if (yyin)
fclose(yyin);
exit(EXIT_FAILURE);
}
//*****************************************************************************
// Print each level with appropriate indentation
void indent() {
for (int i = 0; i<level; i++)
cout << (" ");
}
//*****************************************************************************
// Announce what the lexical analyzer has found
void output() {
indent();
cout << "---> FOUND " << yytext << endl;
}
//*****************************************************************************
// Read the next token from the input stream
int lex() {
nextToken = yylex();
if (nextToken == TOK_EOF) {
// save a "lexeme" into yytext
yytext[0] = 'E';
yytext[1] = 'O';
yytext[2] = 'F';
yytext[3] = 0;
}
if(printParse) {
// Tell us about the token and lexeme
indent();
cout << "Next token is: ";
switch(nextToken) {
case TOK_BEGIN: cout << "TOK_BEGIN"; break;
case TOK_BREAK: cout << "TOK_BREAK"; break;
case TOK_CONTINUE: cout << "TOK_CONTINUE"; break;
case TOK_DOWNTO: cout << "TOK_DOWNTO"; break;
case TOK_ELSE: cout << "TOK_ELSE"; break;
case TOK_END: cout << "TOK_END"; break;
case TOK_FOR: cout << "TOK_FOR"; break;
case TOK_IF: cout << "TOK_IF"; break;
case TOK_LET: cout << "TOK_LET"; break;
case TOK_PROGRAM: cout << "TOK_PROGRAM"; break;
case TOK_READ: cout << "TOK_READ"; break;
case TOK_THEN: cout << "TOK_THEN"; break;
case TOK_TO: cout << "TOK_TO"; break;
case TOK_VAR: cout << "TOK_VAR"; break;
case TOK_WHILE: cout << "TOK_WHILE"; break;
case TOK_WRITE: cout << "TOK_WRITE"; break;
case TOK_INTEGER: cout << "TOK_INTEGER"; break;
case TOK_REAL: cout << "TOK_REAL"; break;
case TOK_SEMICOLON: cout << "TOK_SEMICOLON"; break;
case TOK_COLON: cout << "TOK_COLON"; break;
case TOK_OPENPAREN: cout << "TOK_OPENPAREN"; break;
case TOK_CLOSEPAREN: cout << "TOK_CLOSEPAREN"; break;
case TOK_PLUS: cout << "TOK_PLUS"; break;
case TOK_MINUS: cout << "TOK_MINUS"; break;
case TOK_MULTIPLY: cout << "TOK_MULTIPLY"; break;
case TOK_DIVIDE: cout << "TOK_DIVIDE"; break;
case TOK_ASSIGN: cout << "TOK_ASSIGN"; break;
case TOK_EQUALTO: cout << "TOK_EQUALTO"; break;
case TOK_LESSTHAN: cout << "TOK_LESSTHAN"; break;
case TOK_GREATERTHAN: cout << "TOK_GREATERTHAN"; break;
case TOK_NOTEQUALTO: cout << "TOK_NOTEQUALTO"; break;
case TOK_MOD: cout << "TOK_MOD"; break;
case TOK_NOT: cout << "TOK_NOT"; break;
case TOK_OR: cout << "TOK_OR"; break;
case TOK_AND: cout << "TOK_AND"; break;
case TOK_IDENT: cout << "TOK_IDENT"; break;
case TOK_INTLIT: cout << "TOK_INTLIT"; break;
case TOK_FLOATLIT: cout << "TOK_FLOATLIT"; break;
case TOK_STRINGLIT: cout << "TOK_STRINGLIT"; break;
case TOK_UNKNOWN: cout << "TOK_UNKNOWN"; break;
default: error();
}
cout << ", Next lexeme is: " << yytext << endl;
}
return nextToken;
}
//*****************************************************************************
// Parses strings in the language generated by the rule:
// <program> → TOK_PROGRAM TOK_IDENT TOK_SEMICOLON <block>
ProgramNode* program()
{
if (!first_of_program())
error();
if(printParse) {
indent();
cout << "Enter <program>" << endl;
}
level = level + 1;
lex(); // Read past TOK_PROGRAM
string programName;
if (nextToken == TOK_IDENT) {
if(printParse) output();
programName = yytext;
lex(); // Read past the identifier
} else {
error();
}
if (nextToken == TOK_SEMICOLON) {
if(printParse) output();
lex(); // Read past the semicolon
} else {
error();
}
BlockNode* blockPtr = block();
if (nextToken == TOK_EOF) {
if(printParse) output();
// End of file reached. Do nothing.
}
else
error();
level = level - 1;
if(printParse) {
indent();
cout << "Exit <program>" << endl;
}
ProgramNode* newProgramNode = new ProgramNode(level, programName, blockPtr);
return newProgramNode;
}
bool first_of_program()
{
return nextToken == TOK_PROGRAM;
}
//*****************************************************************************
// Parses strings in the language generated by the rule:
// <block> → ( TOK_VAR TOK_IDENT TOK_COLON type TOK_SEMIOLON {[ TOK_IDENT TOK_COLON type TOK_SEMIOLON ]} <compound> ) | <compound>
BlockNode* block()
{
if (!first_of_block())
error();
if(printParse) {
indent();
cout << "Enter <block>" << endl;
}
level = level + 1;
BlockNode* newBlockNode = new BlockNode(level);
if (nextToken == TOK_VAR) {
if(printParse) output();
lex(); // Read past TOK_VAR
while (nextToken == TOK_IDENT) {
std::string *varName = new std::string(yytext);
if(printParse) output();
if (inSymbolTable(*varName)) {
error();
}
lex(); // Read past the identifier
if (nextToken == TOK_COLON) {
if(printParse) output();
lex(); // Read past the colon
} else {
error();
}
if (nextToken == TOK_INTEGER || nextToken == TOK_REAL) {
if(printParse) output(); // Read past the type
} else {
error();
}
std::string *varType = new std::string(yytext);
/*newBlockNode->varNames.push_back(varName);
newBlockNode->varTypes.push_back(varType);*/
symbolTable.insert(std::pair<std::string, float>(*varName, 0.0f));
lex(); // Read past the type
if (nextToken == TOK_SEMICOLON) {
if (printParse) output();
lex(); // Read past the semicolon
} else {
error();
}
}
}
newBlockNode->compound = compound_statement();
level = level - 1;
if(printParse) {
indent();
cout << "Exit <block>" << endl;
}
return newBlockNode;
}
bool first_of_block()
{
return nextToken == TOK_BEGIN || nextToken == TOK_VAR;
}
//*****************************************************************************
// Parses strings in the language generated by the rule:
// <statement> → <assignment> | <compound> | <if> | <while> | <read> | <write>
StatementNode* statement()
{
if (!first_of_statement())
error();
if(printParse) {
indent();
cout << "Enter <statement>" << endl;
}
level = level + 1;
StatementNode* newStatementNode = nullptr;
switch (nextToken) {
case TOK_IDENT:
newStatementNode = assignment_statement();
break;
case TOK_BEGIN:
newStatementNode = compound_statement();
break;
case TOK_IF:
newStatementNode = if_statement();
break;
case TOK_WHILE:
newStatementNode = while_statement();
break;
case TOK_READ:
newStatementNode = read_statement();
break;
case TOK_WRITE:
newStatementNode = write_statement();
break;
default:
error();
}
level = level - 1;
if(printParse) {
indent();
cout << "Exit <statement>" << endl;
}
return newStatementNode;
}
bool first_of_statement()
{
switch (nextToken) {
case TOK_IDENT:
return true;
case TOK_BEGIN:
return true;
case TOK_IF:
return true;
case TOK_WHILE:
return true;
case TOK_READ:
return true;
case TOK_WRITE:
return true;
default:
return false;
}
}
//*****************************************************************************
// Parses strings in the language generated by the rule:
// <assignment> → TOK_IDENT TOK_ASSIGN <expression>
AssignmentNode* assignment_statement()
{
if (nextToken != TOK_IDENT)
error();
string id = string(yytext); // Save the identifier
if(printParse) {
indent();
cout << "Enter <assignment>" << endl;
}
level = level + 1;
if (printParse) output();
lex(); // Read past the identifier
if (nextToken == TOK_ASSIGN) {
if(printParse) output();
lex(); // Read past the assignment operator
} else {
error();
}
ExpressionNode* expr = expression();
AssignmentNode* newAssignmentNode = new AssignmentNode(level, id, expr);
level = level - 1;
if(printParse) {
indent();
cout << "Exit <assignment>" << endl;
}
return newAssignmentNode;
}
//*****************************************************************************
// Parses strings in the language generated by the rule:
// <compound> → TOK_BEGIN <statement> { TOK_SEMICOLON <statement> } TOK_END
CompoundNode* compound_statement()
{
if (!first_of_compound_statement())
error();
if(printParse) {
indent();
cout << "Enter <compound>" << endl;
}
level = level + 1;
CompoundNode* newCompoundNode = new CompoundNode(level);
lex(); // Read past TOK_BEGIN
newCompoundNode->addStatement(statement()); // Add the first statement
while (nextToken == TOK_SEMICOLON) {
if(printParse) output();
lex(); // Read past the semicolon
newCompoundNode->addStatement(statement());
}
if (nextToken == TOK_END) {
if(printParse) output();
lex(); // Read past TOK_END
} else {
error();
}
level = level - 1;
if(printParse) {
indent();
cout << "Exit <compound>" << endl;
}
return newCompoundNode;
}
bool first_of_compound_statement()
{
return nextToken == TOK_BEGIN;
}
//*****************************************************************************
// Parses strings in the language generated by the rule:
// <if> → TOK_IF <expression> TOK_THEN <statement> [ TOK_ELSE <statement> ]
IfNode* if_statement()
{
if (nextToken != TOK_IF)
error();
if(printParse) {
indent();
cout << "Enter <if>" << endl;
}
level = level + 1;
lex(); // Read past TOK_IF
ExpressionNode* expr = expression();
StatementNode* thenStatement = nullptr;
StatementNode* elseStatement = nullptr;
if (nextToken == TOK_THEN) {
if(printParse) output();
lex(); // Read past TOK_THEN
thenStatement = statement();
} else {
error();
}
if (nextToken == TOK_ELSE) {
if(printParse) output();
lex(); // Read past TOK_ELSE
elseStatement = statement();
}
IfNode* newIfNode = new IfNode(level, expr, thenStatement, elseStatement);
level = level - 1;
if(printParse) {
indent();
cout << "Exit <if>" << endl;
}
return newIfNode;
}
//*****************************************************************************
// Parses strings in the language generated by the rule:
// <while> → TOK_WHILE <expression> <statement>
WhileNode* while_statement() {
if (nextToken != TOK_WHILE)
error();
if(printParse) {
indent();
cout << "Enter <while>" << endl;
}
level = level + 1;
lex(); // Read past TOK_WHILE
ExpressionNode* expr = expression();
StatementNode* stmt = statement();
WhileNode* newWhileNode = new WhileNode(level, expr, stmt);
level = level - 1;
if(printParse) {
indent();
cout << "Exit <while>" << endl;
}
return newWhileNode;
}
//*****************************************************************************
// Parses strings in the language generated by the rule:
// <read> → TOK_READ TOK_OPENPAREN TOK_IDENT TOK_CLOSEPAREN
ReadNode* read_statement() {
if (nextToken != TOK_READ)
error();
if(printParse) {
indent();
cout << "Enter <read>" << endl;
}
level = level + 1;
lex(); // Read past TOK_READ
if (nextToken == TOK_OPENPAREN) {
if(printParse) output();
lex(); // Read past TOK_OPENPAREN
} else {
error();
}
std::string id;
if (nextToken == TOK_IDENT) {
if(printParse) output();
id = std::string(yytext);
lex(); // Read past the identifier
} else {
error();
}
if (nextToken == TOK_CLOSEPAREN) {
if(printParse) output();
lex(); // Read past TOK_CLOSEPAREN
} else {
error();
}
ReadNode* newReadNode = new ReadNode(level, id);
level = level - 1;
if(printParse) {
indent();
cout << "Exit <read>" << endl;
}
return newReadNode;
}
//*****************************************************************************
// Parses strings in the language generated by the rule:
// <write> → TOK_WRITE TOK_OPENPAREN ( TOK_IDENT | TOK_STRINGLIT ) TOK_CLOSEPAREN
WriteNode* write_statement() {
if (nextToken != TOK_WRITE)
error();
if(printParse) {
indent();
cout << "Enter <write>" << endl;
}
level = level + 1;
lex(); // Read past TOK_WRITE
if (nextToken == TOK_OPENPAREN) {
if(printParse) output();
lex(); // Read past TOK_OPENPAREN
} else {
error();
}
string id;
string str;
if (nextToken == TOK_IDENT) {
id = string(yytext);
if(printParse) output();
lex(); // Read past the identifier
} else if (nextToken == TOK_STRINGLIT) {
str = string(yytext);
if(printParse) output();
lex(); // Read past the string literal
} else {
error();
}
if (nextToken == TOK_CLOSEPAREN) {
if(printParse) output();
lex(); // Read past TOK_CLOSEPAREN
} else {
error();
}
WriteNode* newWriteNode = new WriteNode(level, id, str);
level = level - 1;
if(printParse) {
indent();
cout << "Exit <write>" << endl;
}
return newWriteNode;
}
//*****************************************************************************
// Parses strings in the language generated by the rule:
// <expression> → <simple_expression> [ ( TOK_EQUALTO | TOK_LESSTHAN | TOK_GREATERTHAN | TOK_NOTEQUALTO ) <simple_expression> ]
ExpressionNode* expression() {
// Check that the <expr> starts with a valid token
if(!first_of_expression())
error();
if(printParse) {
indent();
cout << "Enter <expr>" << endl;
}
level = level + 1;
ExpressionNode* newExprNode = new ExpressionNode(level);
/* Parse the first term */
newExprNode->firstSimpleExpr = simple_expression();
if(nextToken == TOK_EQUALTO || nextToken == TOK_LESSTHAN || nextToken == TOK_GREATERTHAN || nextToken == TOK_NOTEQUALTO) {
if(printParse) output();
newExprNode->relop = nextToken;
lex();
newExprNode->secondSimpleExpr = simple_expression();
}
level = level - 1;
if(printParse) {
indent();
cout << "Exit <expr>" << endl;
}
return newExprNode;
}
bool first_of_expression(void)
{
switch (nextToken) {
case TOK_IDENT:
return true;
case TOK_INTLIT:
return true;
case TOK_FLOATLIT:
return true;
case TOK_OPENPAREN:
return true;
case TOK_NOT:
return true;
case TOK_MINUS:
return true;
default:
return false;
}
}
//*****************************************************************************
// Parses strings in the language generated by the rule:
// <simple_expression> → <term> { ( TOK_PLUS | TOK_MINUS | TOK_OR ) <term> }
SimpleExpressionNode* simple_expression() {
if(!first_of_simple_expression())
error();
if(printParse) {
indent();
cout << "Enter <simple_expression>" << endl;
}
level = level + 1;
SimpleExpressionNode* newSimpleExprNode = new SimpleExpressionNode(level);
newSimpleExprNode->firstTerm = term();
while (nextToken == TOK_PLUS || nextToken == TOK_MINUS || nextToken == TOK_OR) {
if(printParse) output();
newSimpleExprNode->restSmplExprOps.push_back(nextToken);
lex();
newSimpleExprNode->restTerms.push_back(term());
}
level = level - 1;
if(printParse) {
indent();
cout << "Exit <simple_expression>" << endl;
}
return newSimpleExprNode;
}
bool first_of_simple_expression(void)
{
switch (nextToken) {
case TOK_IDENT:
return true;
case TOK_INTLIT:
return true;
case TOK_FLOATLIT:
return true;
case TOK_OPENPAREN:
return true;
case TOK_NOT:
return true;
case TOK_MINUS:
return true;
default:
return false;
}
}
//*****************************************************************************
// Parses strings in the language generated by the rule:
// <term> → <factor> { ( TOK_MULTIPLY | TOK_DIVIDE | TOK_MOD ) <factor> }
TermNode* term() {
/* Check that the <term> starts with a valid token */
if(!first_of_term())
error();
if(printParse) {
indent();
cout << "Enter <term>" << endl;
}
level = level + 1;
TermNode* newTermNode = new TermNode(level);
/* Parse the first factor */
newTermNode->firstFactor = factor();
/* As long as the next token is * or /, get the
next token and parse the next factor */
while(nextToken == TOK_MULTIPLY || nextToken == TOK_DIVIDE || nextToken == TOK_MOD) {
if(printParse) output();
newTermNode->restTermOps.push_back(nextToken);
lex();
newTermNode->restFactors.push_back(factor());
}
level = level - 1;
if(printParse) {
indent();
cout << "Exit <term>" << endl;
}
return newTermNode;
}
bool first_of_term(void)
{
switch (nextToken) {
case TOK_IDENT:
return true;
case TOK_INTLIT:
return true;
case TOK_FLOATLIT:
return true;
case TOK_OPENPAREN:
return true;
case TOK_NOT:
return true;
case TOK_MINUS:
return true;
default:
return false;
}
}
//*****************************************************************************
// Parses strings in the language generated by the rule:
// <factor> → TOK_IDENT | TOK_INTLIT | TOK_FLOATLIT | TOK_OPENPAREN <expression> TOK_CLOSEPAREN | TOK_NOT <factor> | TOK_MINUS <factor>
FactorNode* factor() {
// Check that the <factor> starts with a valid token
if(!first_of_factor())
error();
if(printParse) {
indent();
cout << "Enter <factor>" << endl;
}
level = level + 1;
FactorNode* newFactorNode = nullptr;
// Determine which RHS token we have
switch(nextToken) {
case TOK_IDENT:
if(printParse) output();
newFactorNode = new IdentifierNode(level, string(yytext));
nextToken = lex(); // Read past what we have found
break;
case TOK_INTLIT:
if(printParse) output();
newFactorNode = new IntLitNode(level, atoi(yytext));
nextToken = lex();
break;
case TOK_FLOATLIT:
if(printParse) output();
newFactorNode = new FloatLitNode(level, atof(yytext));
nextToken = lex();
break;
case TOK_OPENPAREN:
// We expect ( <expr> ) parse it
if(printParse) output();
nextToken = lex();
if (!first_of_expression())
error();
newFactorNode = new NestedExpressionNode(level, expression());
if (nextToken == TOK_CLOSEPAREN) {
if(printParse) output();
nextToken = lex();
}
else
error();
break;
case TOK_NOT:
if(printParse) output();
nextToken = lex();
newFactorNode = new NotNode(level, factor());
break;
case TOK_MINUS:
if(printParse) output();
nextToken = lex();
newFactorNode = new MinusNode(level, factor());
break;
default:
// If we made it to here, syntax error
error();
}
level = level - 1;
if(printParse) {
indent();
cout << "Exit <factor>" << endl;
}
return newFactorNode;
}
bool first_of_factor(void)
{
switch (nextToken) {
case TOK_IDENT:
return true;
case TOK_INTLIT:
return true;
case TOK_FLOATLIT:
return true;
case TOK_OPENPAREN:
return true;
case TOK_NOT:
return true;
case TOK_MINUS:
return true;
default:
return false;
}
}