-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.cpp
905 lines (757 loc) · 24.6 KB
/
parser.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
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
// ****************************************************************************
// ****************************************************************************
// parser.cpp
// ****************************************************************************
//
// ****************************************************************************
// ****************************************************************************
// ****************************************************************************
// Includes
// ****************************************************************************
#include "common.h"
// ****************************************************************************
// Initialize Static Members
// ****************************************************************************
NonTerm* Parser::m_entrySymbol;
ParseTree* Parser::m_tree;
// ****************************************************************************
// init()
// ****************************************************************************
void
Parser::init(const comString& file)
{
printf("Initializing parser.\n");
if (file != "")
buildGrammar(file);
else
builtinGrammar();
// If we are running in a special parser mode, do that and then exit.
if (Global::isDumpGrammar()) {
NonTerm::dumpGrammar();
Global::succeed();
} else if (Global::isGenerateTree()) {
generate();
Global::succeed();
}
}
// ****************************************************************************
// run()
// ****************************************************************************
void
Parser::run()
{
printf("Running parser.\n");
// Construct our parse tree. The top level non terminal is S
m_tree = new ParseTree(m_entrySymbol);
// Prepare to iterate over the tokens.
TokListIter iter(Lexer::getTokens());
UINT tokensParsed;
if (parse(iter, m_tree, tokensParsed)) {
if (!iter.next()) {
printf("Done running parser.\n");
} else {
fprintf(stderr, "Failed to parse all tokens.\n");
Global::fail();
}
} else {
fprintf(stderr, "Failed to parse tokens\n");
Global::fail();
}
}
// ****************************************************************************
// typeCheck()
// ****************************************************************************
void
Parser::typeCheck()
{
printf("Running type check.\n");
m_tree->typeCheck();
printf("Passed type check.\n");
}
// ****************************************************************************
// parse()
// ****************************************************************************
bool
Parser::parse(TokListIter& iter,
ParseTree* tree,
UINT& tokensParsed)
{
// This is a non terminal node, so get the productions and loop through
// them.
NonTerm* nTerm = tree->getNonTerm();
ProdVec& prods = nTerm->getProductions();
Token* token;
for (UINT i = 0; i < prods.getNumEntries(); i++) {
// For each production, loop through the elements and...
Production* prod = prods[i];
bool prodSuccess = true;
UINT tokensConsumed = 0;
for (UINT j = 0; j < prod->getNumEntries(); j++) {
ProdEle* pe = prod->get(j);
if (pe->m_isTerm) {
if (pe->m_term->m_tType == Token::NONE) {
// If the nonterminal is an epsilon, effectively ignore
// it, consuming no tokens.
continue;
}
if ((token = iter.next()))
tokensConsumed++;
if (!token || !pe->m_term->matches(token)) {
// If the next token does not match the production,
// this production is not appropriate.
prodSuccess = false;
break;
} else {
tree->addChild(new ParseTree(pe->m_term, token));
}
} else {
// If the element is not a terminal, create a new tree for
// this nonterminal and recurse. Also keep track of how
// many tokens this recursion consumes in case we need to
// roll back the iterator later.
ParseTree* child = new ParseTree(pe->m_nonTerm);
UINT recurseParsed = 0;
if (parse(iter, child, recurseParsed)) {
// If the recursive parse succeeded, add the number of
// newly parsed tokens to the tokens consumed count and
// attach the child tree to the current tree.
tree->addChild(child);
tokensConsumed += recurseParsed;
} else {
// If the recursive parse failed, this production is
// not appropriate.
delete child;
prodSuccess = false;
break;
}
}
}
if (prodSuccess) {
// If the current production worked the whole way through, set
// this production as this tree's production, add the number of
// tokens consumed by the production to the number of tokens
// parsed and return.
tree->setProduction(prod);
tokensParsed += tokensConsumed;
return true;
} else {
// If the current production did not succeed, roll back the
// iterator based on the number of tokens consumed and remove
// any child trees from the current tree.
for (UINT j = 0; j < tokensConsumed; j++)
iter.prev();
tree->cullChildren();
}
}
token = iter.next();
printf("Unexpected token (%s:\"%s\") on line %d\n", (const char*) Token::getTypeName(token->getType()), (const char*) token->getSpelling(), token->getLine());
iter.prev();
// If we are here, we have gone through all of this nonterminal's
// productions and not gotten a parse out of it.
return false;
}
// ****************************************************************************
// printTree()
// ****************************************************************************
void
Parser::printTree()
{
if (m_tree)
m_tree->print();
else
printf("No tree to print");
}
// ****************************************************************************
// generate()
// ****************************************************************************
void
Parser::generate()
{
FILE* source = fopen("generated.ibtl", "w");
ParseTree* tree = generate(new ParseTree(m_entrySymbol), source, Translator::None);
tree->print("gen.out");
fclose(source);
}
ParseTree*
Parser::generate(ParseTree* tree,
FILE* file,
Translator::Type tType)
{
// Choose a random production to use which includes a translation scheme
// which has the proper return type. We assume that we will eventually get
// a useful production.
Production* prod = NULL;
TransScheme* tScheme = NULL;
while (true) {
prod = tree->getNonTerm()->getProductions()[rand() % tree->getNonTerm()->getProductions().getNumEntries()];
tree->setProduction(prod);
// Make sure this production doesn't use variables.
bool variables = false;
for (UINT i = 0; i < prod->getNumEntries(); i++) {
if (prod->get(i)->m_isTerm && prod->get(i)->m_term->m_tType == Token::Identifier) {
variables = true;
break;
}
}
if (variables)
continue;
// Choose a random translation scheme to use whose return type is the same
// as the type we are supposed to use. Make sure before trying to select
// one that one exists.
if (tType != Translator::None && tType != Translator::Any) {
bool valid = false;
for (UINT i = 0; i < prod->getTransSchemes().getNumEntries(); i++) {
if (prod->getTransSchemes()[i]->returns() == tType) {
valid = true;
break;
}
}
if (!valid)
continue;
}
do {
tScheme = prod->getTransSchemes()[rand() % prod->getTransSchemes().getNumEntries()];
} while (tType != Translator::None && tType != Translator::Any && tScheme->returns() != tType);
break;
}
// For each element in the production...
for (UINT j = 0; j < prod->getNumEntries(); j++) {
ProdEle* pe = prod->get(j);
// If the element is a terminal...
if (pe->m_isTerm) {
if (pe->m_term->m_tType == Token::NONE) {
// If the element is an epsilon, do nothing.
continue;
}
// Add the terminal to the tree.
const comString& spelling = (pe->m_term->m_spelling != "") ? pe->m_term->m_spelling : Token::getExample(pe->m_term->m_tType);
tree->addChild(new ParseTree(pe->m_term, new Token(pe->m_term->m_tType, spelling, 0)));
fprintf(file, "%s ", (const char*) spelling);
} else {
// If the element is not a terminal, create a new tree for
// this nonterminal and recurse.
tree->addChild(generate(new ParseTree(pe->m_nonTerm), file, tScheme->getPreType(j)));
}
}
return tree;
}
// ****************************************************************************
// buildGrammar()
// ****************************************************************************
void
Parser::buildGrammar(const comString& filename)
{
FILE* input = fopen((const char*) filename, "r");
char line[128];
if (!input) {
fprintf(stderr, "Grammar file \"%s\" failed to open.\n", (const char*) filename);
Global::fail();
}
comVector<NonTerm*> nTerms;
comVector<Terminal*> terms;
comVector<Production*> prods;
bool unique;
// Loop through each line of the file, looking for nonterminal
// declarations.
while (fgets(line, 128, input)) {
// Get rid of icky line breaks.
for (char* loc = line; *loc; loc++) {
if (*loc == '\n' || *loc =='\r')
*loc = '\0';
}
// If the first character on the line is not a tab, the line is a
// nonterminal declaration.
if (*line != '\t') {
// If this nonterm is an edge node, truncate the line to clip off
// the edge node indicator and indicate it is such to the
// constructor.
char* loc;
bool scope = false;
if ((loc = strstr(line, "@"))) {
*loc = '\0';
scope = true;
}
nTerms.append(new NonTerm(line, scope));
}
}
rewind(input);
NonTerm* current = NULL;
Production* pCurr = NULL;
// Loop through each line in the file, looking for productions.
while(fgets(line, 128, input)) {
// Get rid of icky line breaks.
for (char* loc = line; *loc; loc++) {
if (*loc == '\n' || *loc =='\r')
*loc = '\0';
}
if (*line == '\t') {
// If the first character on the line is a tab, it is a production
// for the current nonterminal.
// If the second character is also a tab, this is a translation
// scheme for the current production.
if (*(line + 1) == '\t') {
if (!pCurr) {
fprintf(stderr, "Tried to parse a translation without a production. \"%s\"", line);
Global::fail();
}
// If the last parsed production was not unique, do not pay
// attention to its translation schemes.
if (!unique)
continue;
TransScheme* tScheme = new TransScheme;
pCurr->addTScheme(tScheme);
char buf[16];
char* bLoc = buf;
bool pre = true;
UINT prIndex = 0;
// Read out translation scheme elements seperated by single
// spaces.
for (char* loc = line + 2; *(loc - 1) != '\0'; loc++) {
// If we've reached the end of the current tScheme element,
// add it to the proper scheme half and then continue.
if (*loc == '\t' || *loc == ' ' || *loc == '\0') {
*bLoc = '\0';
// If the buffer is empty, just continue;
if (bLoc == buf)
continue;
// If the spelling was '->', just ignore it.
if (strcmp(buf, "->") == 0) {
bLoc = buf;
continue;
}
// If the spelling was ':', unset the pre flag.
if (strcmp(buf, ":") == 0) {
pre = false;
bLoc = buf;
continue;
}
// Do the actual adding now that we've pulled out
// indicator stuff.
if (pre) {
// Get the type and add a pretranslation unit if it
// is valid.
Translator::Type type = Translator::getType(buf);
if (type != (Translator::Type) -1) {
tScheme->addPre(type, prIndex);
bLoc = buf;
}
prIndex++;
} else {
if (*buf != '#' && Global::isNumeric(buf)) {
tScheme->addPost((UINT) atoi(buf));
bLoc = buf;
} else {
if (*buf == '#')
tScheme->addPost(buf + 1);
else
tScheme->addPost(buf);
bLoc = buf;
}
}
bLoc = buf;
} else {
// Copy the current character on the line into the buffer.
*(bLoc++) = *loc;
}
}
} else {
if (!current) {
fprintf(stderr, "Tried to parse a production without a nonterminal. \"%s\"", line);
Global::fail();
}
Production* prod = new Production;
char buf[64];
char* bLoc = buf;
// Read out production elements seperated by single spaces and add
// them to the current production.
for (char* loc = line + 1; *(loc - 1) != '\0'; loc++) {
if (*loc == ' ' || *loc == '\0') {
// We've reached the end of the current production
// element's spelling. Look for it in the list of nonterms
// and then the list of terminals.
*bLoc = '\0';
bool found = false;
// If the word read was '@', indicate that this
// production is a declarator, but do not add it to the
// list of production elements.
if (strcmp(buf, "@") == 0) {
prod->setDeclarator();
bLoc = buf;
continue;
}
// If the word read was '#', indicate that this
// production is a function declarator, but do not add
// it to the list of production elements.
if (strcmp(buf, "#") == 0) {
prod->setFuncDeclarator();
bLoc = buf;
continue;
}
// If the word read was '!', indicate that this
// production is a function invocation, but do not add
// it to the list of production elements.
if (strcmp(buf, "!") == 0) {
prod->setFuncInvocation();
bLoc = buf;
continue;
}
for (UINT i = 0; i < nTerms.getNumEntries(); i++) {
if (nTerms[i]->getName() == buf) {
prod->add(nTerms[i]);
bLoc = buf;
found = true;
break;
}
}
if (found)
continue;
for (UINT i = 0; i < terms.getNumEntries(); i++) {
if (termMatch(terms[i], buf)) {
prod->add(terms[i]);
bLoc = buf;
found = true;
break;
}
}
if (found)
continue;
// If we're here, this production element does not yet exist.
// Add it as a terminal.
Terminal* term = makeTerminal(buf);
terms.append(term);
prod->add(term);
bLoc = buf;
} else {
// Copy the current character on the line into the buffer.
*(bLoc++) = *loc;
}
}
// Look for duplicate productions. If one exists, replace this
// one with it.
unique = true;
for (UINT i = 0; i < prods.getNumEntries(); i++) {
if (prods[i]->matches(prod)) {
delete prod;
prod = prods[i];
unique = false;
break;
}
}
if (unique)
prods.append(prod);
current->addProduction(prod);
pCurr = prod;
}
} else {
// The current line is a nonterm. Find it in the list of prepared
// nonterms and make it the current production.
char* loc;
if ((loc = strstr(line, "@")))
*loc = '\0';
line[strlen(line)] = '\0';
for (UINT i = 0; i < nTerms.getNumEntries(); i++) {
if (nTerms[i]->getName() == line) {
current = nTerms[i];
break;
}
}
}
}
// Check that all of the nonterminals have productions.
for (UINT i = 0; i < nTerms.getNumEntries(); i++) {
if (nTerms[i]->getProductions().getNumEntries() == 0) {
fprintf(stderr, "Nonterminal symbol \"%s\" has no productions.\n", (const char*) nTerms[i]->getName());
Global::fail();
}
}
// Check that all of the productions have translation schemes if we are
// actually going to translate.
for (UINT i = 0; i < prods.getNumEntries(); i++) {
if (prods[i]->getTransSchemes().getNumEntries() == 0) {
comString printable;
fprintf(stderr, "Production \"%s\" has no translation schemes.\n", (const char*) prods[i]->printable(printable));
Global::fail();
}
}
// Assume the first symbol in the list is the entry symbol.
m_entrySymbol = nTerms[0];
fclose(input);
}
// ****************************************************************************
// makeTerminal()
// ****************************************************************************
Terminal*
Parser::makeTerminal(const comString& spelling)
{
if (spelling == "id")
return new Terminal(Token::Identifier);
if (spelling == "string")
return new Terminal(Token::StrConst);
if (spelling == "int")
return new Terminal(Token::IntConst);
if (spelling == "real")
return new Terminal(Token::RealConst);
if (spelling == "bool")
return new Terminal(Token::BoolConst);
if (spelling == "type")
return new Terminal(Token::PrimType);
if (spelling == "~" || spelling == "NONE")
return new Terminal(Token::NONE);
Token* token = Lexer::tokenize(spelling);
if (!token) {
fprintf(stderr, "Failed to tokenize \"%s\"\n", (const char*) spelling);
Global::fail();
}
Terminal* ret = new Terminal(token->getType(), token->getSpelling());
delete token;
return ret;
}
// ****************************************************************************
// termMatch()
// ****************************************************************************
bool
Parser::termMatch(Terminal* term,
const comString& spelling)
{
// If the terminal's spelling in blank, it is a constant with variable
// spelling and must be checked against the token type.
if (term->m_spelling == "") {
switch (term->m_tType) {
case Token::StrConst:
return spelling == "string";
case Token::IntConst:
return spelling == "int";
case Token::RealConst:
return spelling == "real";
case Token::BoolConst:
return spelling == "bool";
case Token::Identifier:
return spelling == "id";
case Token::PrimType:
return spelling == "type";
case Token::NONE:
return spelling == "~" || spelling == "NONE";
default:
fprintf(stderr, "Trying to match against non-literal constant type token with variable spelling: (%d)\n", term->m_tType);
Global::fail();
}
}
return term->m_spelling == spelling;
}
// ****************************************************************************
// builtinGrammar()
// ****************************************************************************
void
Parser::builtinGrammar()
{
// Create the terminal symbols
Terminal* open = new Terminal(Token::Paren, "(");
Terminal* close = new Terminal(Token::Paren, ")");
Terminal* assign = new Terminal(Token::Operator, ":=");
Terminal* iden = new Terminal(Token::Identifier);
Terminal* sub = new Terminal(Token::Operator, "-");
comVector<Terminal*> binOps;
binOps.append(new Terminal(Token::Operator, "+"));
binOps.append(new Terminal(Token::Operator, "*"));
binOps.append(new Terminal(Token::Operator, "/"));
binOps.append(new Terminal(Token::Operator, "%"));
binOps.append(new Terminal(Token::Operator, "^"));
binOps.append(new Terminal(Token::Operator, "="));
binOps.append(new Terminal(Token::Operator, ">"));
binOps.append(new Terminal(Token::Operator, ">="));
binOps.append(new Terminal(Token::Operator, "<"));
binOps.append(new Terminal(Token::Operator, "<="));
binOps.append(new Terminal(Token::Operator, "!="));
binOps.append(new Terminal(Token::Operator, "or"));
binOps.append(new Terminal(Token::Operator, "and"));
comVector<Terminal*> unOps;
unOps.append(new Terminal(Token::Operator, "not"));
unOps.append(new Terminal(Token::Operator, "sin"));
unOps.append(new Terminal(Token::Operator, "cos"));
unOps.append(new Terminal(Token::Operator, "tan"));
Terminal* constStr = new Terminal(Token::StrConst);
Terminal* constInt = new Terminal(Token::IntConst);
Terminal* constReal = new Terminal(Token::RealConst);
Terminal* constBool = new Terminal(Token::BoolConst);
Terminal* printKey = new Terminal(Token::StmtWord, "stdout");
Terminal* ifKey = new Terminal(Token::StmtWord, "if");
Terminal* whileKey = new Terminal(Token::StmtWord, "while");
Terminal* letKey = new Terminal(Token::Operator, "let");
Terminal* typeName = new Terminal(Token::PrimType);
Terminal* epsilon = new Terminal(Token::NONE);
// Create the nonterminal symbols
m_entrySymbol = new NonTerm("S");
NonTerm* S = m_entrySymbol;
NonTerm* SPAREN = new NonTerm("SPAREN");
NonTerm* SP = new NonTerm("SP");
NonTerm* EXPR = new NonTerm("EXPR");
NonTerm* RESR = new NonTerm("RESR");
NonTerm* OPER = new NonTerm("OPER");
NonTerm* OPERM = new NonTerm("OPER_MINUS");
NonTerm* STMTS = new NonTerm("STMTS");
NonTerm* ELSE = new NonTerm("ELSE");
NonTerm* ELIST = new NonTerm("EXPRLIST");
NonTerm* ELISTC = new NonTerm("EXPRLIST_CAP");
NonTerm* VLIST = new NonTerm("VARLIST");
NonTerm* VLISTC = new NonTerm("VARLIST_CAP");
Production* prod = NULL;
// ----------------------------------------------------
// S Productions
// ----------------------------------------------------
// S -> ( SPAREN ) SP
prod = new Production;
prod->add(open);
prod->add(SPAREN);
prod->add(close);
prod->add(SP);
S->addProduction(prod);
// S -> EXPR SP
prod = new Production;
prod->add(EXPR);
prod->add(SP);
S->addProduction(prod);
// ----------------------------------------------------
// SPAREN Productions
// ----------------------------------------------------
// SPAREN -> S
SPAREN->addProduction(S);
// SPAREN -> epsilon
SPAREN->addProduction(epsilon);
// ----------------------------------------------------
// SP Productions
// ----------------------------------------------------
// SP -> S SP
prod = new Production;
prod->add(S);
prod->add(SP);
SP->addProduction(prod);
// SP -> epsilon
prod = new Production;
prod->add(epsilon);
SP->addProduction(prod);
// ----------------------------------------------------
// EXPR Productions
// ----------------------------------------------------
// EXPR -> RESR
EXPR->addProduction(RESR);
// EXPR -> ( STMTS )
prod = new Production;
prod->add(open);
prod->add(STMTS);
prod->add(close);
EXPR->addProduction(prod);
// ----------------------------------------------------
// RESR Productions
// ----------------------------------------------------
// RESR -> ( OPER )
prod = new Production;
prod->add(open);
prod->add(OPER);
prod->add(close);
RESR->addProduction(prod);
// RESR -> strings | ints | reals | bools | identifiers
RESR->addProduction(constStr);
RESR->addProduction(constInt);
RESR->addProduction(constReal);
RESR->addProduction(constBool);
RESR->addProduction(iden);
// ----------------------------------------------------
// OPER Productions
// ----------------------------------------------------
// OPER -> := identifiers RESR
prod = new Production;
prod->add(assign);
prod->add(iden);
prod->add(RESR);
OPER->addProduction(prod);
// OPER -> - RESR OPER_MINUS
prod = new Production;
prod->add(sub);
prod->add(RESR);
prod->add(OPERM);
OPER->addProduction(prod);
// <binary operator> RESR RESR
for (UINT i = 0; i < binOps.getNumEntries(); i++) {
prod = new Production;
prod->add(binOps[i]);
prod->add(RESR);
prod->add(RESR);
OPER->addProduction(prod);
}
// <unary operator> RESR
for (UINT i = 0; i < unOps.getNumEntries(); i++) {
prod = new Production;
prod->add(unOps[i]);
prod->add(RESR);
OPER->addProduction(prod);
}
// ----------------------------------------------------
// OPER_MINUS Productions
// ----------------------------------------------------
// OPER_MINUS -> RESR
OPERM->addProduction(RESR);
// OPER_MINUS -> epsilon
OPERM->addProduction(epsilon);
// ----------------------------------------------------
// STMTS Productions
// ----------------------------------------------------
// STMTS -> if EXPR EXPR ELSE
prod = new Production;
prod->add(ifKey);
prod->add(EXPR);
prod->add(EXPR);
prod->add(ELSE);
STMTS->addProduction(prod);
// STMTS -> while EXPR EXPRLIST
prod = new Production;
prod->add(whileKey);
prod->add(EXPR);
prod->add(ELIST);
STMTS->addProduction(prod);
// STMTS -> let ( VARLIST )
prod = new Production;
prod->add(letKey);
prod->add(open);
prod->add(VLIST);
prod->add(close);
STMTS->addProduction(prod);
// STMTS -> stdout RESR
prod = new Production;
prod->add(printKey);
prod->add(RESR);
STMTS->addProduction(prod);
// ----------------------------------------------------
// ELSE Productions
// ----------------------------------------------------
// ELSE -> EXPR
ELSE->addProduction(EXPR);
// ELSE -> epsilon
ELSE->addProduction(epsilon);
// ----------------------------------------------------
// EXPRLIST and EXPRLIST_CAP Productions
// ----------------------------------------------------
// EXPRLIST -> EXPR EXPRLIST_CAP
prod = new Production;
prod->add(EXPR);
prod->add(ELISTC);
ELIST->addProduction(prod);
// EXPRLIST_CAP -> EXPRLIST
ELISTC->addProduction(ELIST);
// EXPRLIST_CAP -> epsilon
ELISTC->addProduction(epsilon);
// ----------------------------------------------------
// VARLIST and VARLIST_CAP Productions
// ----------------------------------------------------
// VARLIST -> (identifier type ) VARLIST_CAP
prod = new Production;
prod->add(open);
prod->add(iden);
prod->add(typeName);
prod->add(close);
prod->add(VLISTC);
VLIST->addProduction(prod);
// VARLIST_CAP -> VARLIST
VLISTC->addProduction(VLIST);
// VARLIST_CAP -> epsilon
VLISTC->addProduction(epsilon);
}