-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.cpp
597 lines (465 loc) · 14.7 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
//
// Created by Adam Zvada on 17.06.17.
//
#include "parser.h"
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <cstdio>
#include <string.h>
#include <cstdlib>
#include <fstream>
void Parser::PrintSymb(LexicalSymbol lexSymbol) {
printf("<%s", symbTable[lexSymbol.type]);
switch (lexSymbol.type) {
case IDENT:
printf(", %s", lexSymbol.ident);
break;
case NUMB:
printf(", %d", lexSymbol.number);
break;
default:
break;
}
printf(">\n");
}
Parser::Parser(string filename, llvm::LLVMContext & context, llvm::Module * module, llvm::IRBuilder<> & builder, BasicBlock * breakTarget) {
_symTable = new SymboleTable();
// init static variables for LLVM and pass
llvmAstInit(context, module, builder, breakTarget, _symTable);
if (!initLexan(filename.c_str())) {
throw ParserException("Lexer had problem with init.");
}
this->_symbole = readLexem();
}
void Parser::Compare(LexSymbolType symboleType) {
if (_symbole.type == symboleType) {
ReadToken();
} else {
ostringstream os;
os << "Error with COMPARE of my symbole type " << (char)_symbole.type << " and given type " << (char)symboleType << endl;
ParserException(os.str());
}
}
void Parser::CompareIndent(string & ident) {
if (_symbole.type == IDENT) {
ident = string(_symbole.ident);
ReadToken();
} else {
ostringstream os;
os << "Error with COMPARE IDENT where my symbole type is " << (char)_symbole.type << endl;
ParserException(os.str());
}
}
void Parser::CompareNumb(int & num) {
if (_symbole.type == NUMB) {
num = _symbole.number;
ReadToken();
cout << _symbole.type << endl;
} else {
ostringstream os;
os << "Error with COMPARE NUM where my symbole type is " << (char)_symbole.type << endl;
ParserException(os.str());
}
}
void Parser::ReadToken() {
_symbole = readLexem();
PrintSymb(_symbole);
}
//------------------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------------------
// <program> -> 'kwPROGRAM' 'IDENT' 'SEMICOLON' <decleration> <block> 'PERIOD'
Prog * Parser::Program() {
Compare(kwPROGRAM);
string programIdent;
CompareIndent(programIdent);
Compare(SEMICOLON);
Decleration();
StatmList * statmentList = ProgramBlock();
Compare(DOT);
return new Prog(programIdent, statmentList);
}
//------------------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------------------
// <decleration> -> kwCONST <constantDeleration> <decleration> | kwVAR <variableDecleration> <decleration> | kwFUNC <functionsDecleration> <decleration> | EPS
void Parser::Decleration() {
switch (_symbole.type) {
case kwVAR:
ReadToken();
DeclerationVar();
Decleration();
break;
case kwCONST:
ReadToken();
DeclerationConst();
Decleration();
break;
case kwFUNC:
ReadToken();
DeclerationFunc();
Decleration();
break;
default:
//EPS
break;
}
}
// <variableDecleration> -> IDENT <nextVariableDecleration> SEMICOLON | IDENT COLON INTEGER SEMICOLON | IDENT COLON ARRAY LBRA <numbarr> DOT DOT <numbarr> RBRA SEMICOLON
void Parser::DeclerationVar() {
string varIdent = "";
CompareIndent(varIdent);
//_symTable->DeclareVar(varIdent);
if (_symbole.type == COLON) {
ReadToken();
if (_symbole.type == kwARRAY) {
ReadToken();
Compare(LBRA);
Numb * start = NumbArr();
Compare(DOT);
Compare(DOT);
Numb * end = NumbArr();
Compare(RBRA);
_symTable->DeclareVarArr(varIdent, start->GetNumb(), end->GetNumb());
} else if (_symbole.type == kwINTEGER) {
ReadToken();
_symTable->DeclareVar(varIdent);
}
} else {
_symTable->DeclareVar(varIdent);
DeclerationVarNext();
}
Compare(SEMICOLON);
}
// <nextVariableDecleration> -> COMMA IDENT <nextVariableDecleration> | EPS
void Parser::DeclerationVarNext() {
if (_symbole.type == COMMA) {
ReadToken();
string varIdent;
CompareIndent(varIdent);
_symTable->DeclareVar(varIdent);
DeclerationVarNext();
}
}
// <constatntDecleration> -> IDENT ASSIGN NUMB <nextConstantDecleration> SEMICOLON
void Parser::DeclerationConst() {
string constIdent;
int value;
CompareIndent(constIdent);
Compare(ASSIGN);
CompareNumb(value);
_symTable->DeclareConst(constIdent, value);
DeclerationConstNext();
Compare(SEMICOLON);
}
// <nextConstantDecleration> -> COMMA IDENT ASSIGN NUMB <nextConstDecleration> | EPS
void Parser::DeclerationConstNext() {
if (_symbole.type == COMMA) {
ReadToken();
string constIdent;
int value;
CompareIndent(constIdent);
Compare(ASSIGN);
CompareNumb(value);
_symTable->DeclareConst(constIdent, value);
DeclerationConstNext();
}
}
void Parser::DeclerationFunc() {
//TODO
cout << "FUNCTION Not implemented yet" << endl;
exit(1);
}
// <programBlock> -> 'kwBEGIN' <statement> <statementNext> 'kwEND'
StatmList * Parser::ProgramBlock() {
Compare(kwBEGIN);
StatmList * listStatm = new StatmList(Statement(), StatementNext());
Compare(kwEND);
return listStatm;
}
//------------------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------------------
// <statement> -> <assignment> | <write> | <if> | <for> | <while> | <switch> | <break> | EPS
Statm * Parser::Statement() {
switch (_symbole.type) {
case IDENT:
return AssigmentStat();
case kwWRITE:
return WriteStat();
case kwREAD:
return ReadStat();
case kwIF:
return IfStat();
case kwFOR:
return ForStat();
case kwWHILE:
return WhileStat();
case kwSWITCH:
return SwitchStat();
case kwBREAK:
return BreakStat();
default:
break;
}
return nullptr;
}
// <statementNext> -> kwSEMICOLON <statement> <statementNext> | EPS
StatmList * Parser::StatementNext() {
if(_symbole.type == SEMICOLON) {
ReadToken();
return new StatmList(Statement(), StatementNext());
}
return nullptr;
}
// <assigment> -> IDENT ASSIGN EXPR | IDENT LBRA <expression> RBRA ASSIGN EXPR
Statm * Parser::AssigmentStat() {
string ident;
CompareIndent(ident);
Var * var;
if (_symbole.type == LBRA) {
ReadToken();
Expr * index = Expression();
Compare(RBRA);
var = new VarArray(ident, index);
} else {
var = new Var(_symTable->GetValue(ident), ident);
}
Compare(ASSIGN);
Expr * expression = Expression();
return new Assign(var, expression);
}
// <write> -> kwWRITE <expr>
Statm * Parser::WriteStat() {
Compare(kwWRITE);
Expr * expression = Expression();
Statm * statement = new Write(expression);
return statement;
}
// <read> -> kwREAD IDENT
Statm * Parser::ReadStat() {
Compare(kwREAD);
string ident;
CompareIndent(ident);
Var * var = new Var(_symTable->GetValue(ident), ident);
Statm * statement = new Read(var);
return statement;
}
// <if> -> kwIF <condition> kwTHEN <statement> <elseStatement> | kwIF <condition> kwTHEN <programBlock> <elseStatement>
Statm * Parser::IfStat() {
Compare(kwIF);
Expr * expression = Condition();
Compare(kwTHEN);
if (_symbole.type == kwBEGIN) {
return new If(expression, ProgramBlock(), Else());
} else {
return new If(expression, Statement(), Else());
}
}
/**
* For loop created by using while loop
*/
// TODO, accept for i to 20 do ... without int of var
// <for> -> kwFOR <assigment> kwTO <expression> kwDO <programBlock> | kwFOR <assigment> kwTO <expression> kwDO <statement>
Statm * Parser::ForStat() {
bool isAscendning;
Compare(kwFOR);
Statm * init = AssigmentStat();
if (_symbole.type == kwTO) {
isAscendning = true;
} else if (_symbole.type == kwDOWNTO) {
isAscendning = false;
} else {
cout << "Error, expects either 'TO' or 'DOWNTO'." << endl;
exit(1);
}
ReadToken();
Expr * endExpression = Expression();
Compare(kwDO);
Var * var = dynamic_cast<Assign*>(init)->GetVar();
LexSymbolType ascOrDesc = isAscendning ? PLUS : MINUS;
Expr * condition = new BinOp(NOT_EQ, new Var(var->GetName(), true), endExpression);
Statm * updateValue = new Assign(new Var(var->GetName(), false), new BinOp(ascOrDesc, new Var(var->GetName(), true), new Numb(1)));
StatmList * body;
if (_symbole.type == kwBEGIN) {
body = new StatmList(updateValue, ProgramBlock());
} else {
body = new StatmList(updateValue, new StatmList(Statement(), nullptr));
}
Statm * whileBlock = new While(condition, body);
StatmList * forStatement = new StatmList(init, new StatmList(whileBlock, nullptr));
return forStatement;
}
// <while> -> kwWHILE <condition> kwDO <statement> | kwWHILE <condition> kwDO <programBlock>
Statm * Parser::WhileStat() {
Compare(kwWHILE);
Expr * condition = Condition();
Compare(kwDO);
if (_symbole.type == kwBEGIN) {
return new While(condition, ProgramBlock());
} else {
return new While(condition, Statement());
}
}
// <switch> ->
Statm * Parser::SwitchStat() {
//TODO
return nullptr;
}
// <break> -> kwBREAK
Statm * Parser::BreakStat() {
ReadToken();
return new Break();
}
// <condition> -> <expression> <operator> <expression>
Expr * Parser::Condition() {
Expr * left = Expression();
LexSymbolType op = Operator();
Expr * right = Expression();
return new BinOp(op, left, right);
}
// <elseStatement> -> kwELSE <statement> | kwELSE <programBlock> | EPS
Statm * Parser::Else() {
Statm * statement = nullptr;
if (_symbole.type == kwELSE) {
ReadToken();
if (_symbole.type == kwBEGIN) {
statement = ProgramBlock();
} else {
statement = Statement();
}
}
return statement;
}
// <operator> -> EQ | NOT_EQ | LESS | GRATHER | LESS_OR_EQ | GRATHER_OR_EQ | OR | AND | MOD
LexSymbolType Parser::Operator() {
switch (_symbole.type) {
case EQ:
ReadToken();
return LexSymbolType::EQ;
case NOT_EQ:
ReadToken();
return LexSymbolType::NOT_EQ;
case LESS:
ReadToken();
return LexSymbolType::LESS;
case GRATHER:
ReadToken();
return LexSymbolType::GRATHER;
case LESS_OR_EQ:
ReadToken();
return LexSymbolType::LESS_OR_EQ;
case GRATHER_OR_EQ:
ReadToken();
return LexSymbolType::GRATHER_OR_EQ;
case kwOR:
ReadToken();
return LexSymbolType::kwOR;
case kwAND:
ReadToken();
return LexSymbolType::kwAND;
case kwMOD:
ReadToken();
return LexSymbolType::kwMOD;
default:
throw ParserException("Unsupported Operator");
//exit(1);
}
}
//------------------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------------------
// <expression> -> MINUS <term> <expressionNext> | <term> <expressionNext>
Expr * Parser::Expression() {
if (_symbole.type == MINUS) {
ReadToken();
return ExpressionNext(new UnaryMinus(Term()));
}
return ExpressionNext(Term());
}
// <expressionNext> -> PLUS <term> <expressionNext> | MINUS <term> <expressionNext> | EPS
Expr * Parser::ExpressionNext(Expr * inheretedExp) {
switch (_symbole.type) {
case PLUS:
ReadToken();
return ExpressionNext(new BinOp(PLUS, inheretedExp, Term()));
case MINUS:
ReadToken();
return ExpressionNext(new BinOp(MINUS, inheretedExp, Term()));
default:
return inheretedExp;
}
}
// <term> -> <factor> <termNext>
Expr * Parser::Term() {
return TermNext(Factor());
}
// <termNext> -> MULTIPLY <factor> <termNext> | DIVIDE <factor> <termNext> | kwMOD <factor> <termNext> | EPS
Expr * Parser::TermNext(Expr * inheretedExp) {
switch (_symbole.type) {
case MULTIPLY:
ReadToken();
return TermNext(new BinOp(MULTIPLY, inheretedExp, Term()));
case DIVIDE:
ReadToken();
return TermNext(new BinOp(DIVIDE, inheretedExp, Term()));
case kwMOD:
ReadToken();
return TermNext(new BinOp(kwMOD, inheretedExp, Term()));
default:
return inheretedExp;
}
}
// <factor> -> IDENT | NUMB | ( <expression> )
Expr * Parser::Factor() {
switch (_symbole.type) {
case IDENT: {
string ident;
CompareIndent(ident);
return VarOrConst(ident);
}
case NUMB: {
int value;
CompareNumb(value);
return new Numb(value);
}
case LPAR: {
ReadToken();
Expr * expression = Expression();
Compare(RPAR);
return expression;
}
default:
throw ParserException("Factor has unsupported symbol.");
//exit(1);
}
}
// <numbarray> -> MINUS NUMB | NUMB
Numb * Parser::NumbArr() {
int num;
if (_symbole.type == MINUS) {
ReadToken();
CompareNumb(num);
num = num * (-1);
} else {
CompareNumb(num);
}
return new Numb(num);
}
Expr * Parser::VarOrConst(string & id) {
int value;
SymboleType type = _symTable->GetConstOrVar(id, &value);
switch (type) {
case CONST:
return new Numb(value);
case VAR:
return new Var(value, id, true);
case ARRAY: {
Compare(LBRA);
Expr * index = Expression();
Compare(RBRA);
return new VarArray(id, index, true);
}
default:
return nullptr;
}
}
//------------------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------------------