-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrammar.y
676 lines (645 loc) · 30 KB
/
grammar.y
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
%{
#include <vector>
#include <string>
#include <iostream>
#include <any>
#include <variant>
#include <unordered_map>
#include <set>
#include <functional>
#include <algorithm>
#include <stdexcept>
#include "SymbolTypes.hpp"
#include "Tree.hpp"
#include "Expression.hpp"
#include "If.hpp"
#include "While.hpp"
#include "For.hpp"
#include "RepeatUntil.hpp"
#include "InputOutput.hpp"
#include "Utility.hpp"
#include "Symbol.hpp"
#include "Subroutine.hpp"
%}
%require "3.7"
%language "c++"
%defines "Parser.hpp"
%output "Parser.cpp"
%define api.value.type variant
%define api.token.raw
%define api.parser.class { Parser }
%define parse.error detailed
%locations
%define api.location.file "Location.hpp"
%parse-param { Lexer *lexer } { Symbol *table } { Tree *prev } { std::ostream *err }
%token CONSTANT USERINPUT OUTPUT
%token DIV MOD AND OR NOT
%token LEN POSITION SUBSTRING RANDOM_INT RECORD ENDRECORD SUBROUTINE RETURN ENDSUBROUTINE
%token STRING_TO_INT INT_TO_STRING STRING_TO_REAL REAL_TO_STRING CHAR_TO_CODE CODE_TO_CHAR
%token IF THEN ELSE ENDIF WHILE ENDWHILE REPEAT UNTIL FOR TO STEP IN ENDFOR
%token
ENDOFFILE 0
EOL "newline"
ASSIGN "<-"
PLUS "+"
MINUS "-"
MULTIPLY "*"
DIVIDE "/"
LPAREN "("
RPAREN ")"
GT ">"
LT "<"
EQ "="
NE "/="
GE ">="
LE "<="
COMMA ","
TYPESPEC ":"
DOT "."
LBRAK "["
RBRAK "]"
LCURL "{"
RCURL "}"
;
%token <std::string> ID SUBID UNKNOWN;
%token <StringT> STRING;
%token <RealT> FLOAT;
%token <IntT> INTEGER;
%token <BoolT> BOOLEAN;
%nterm <Tree*> Block Stmnts Stmnt Marker
%nterm <Expression*> Exp BoolExp
%nterm <std::string> SubId SubCall ForId ForInId
%nterm <std::pair<std::string,ExpI>> Field Param
%nterm <std::pair<Expression*,Tree*>> ElseIf
%nterm <std::vector<std::pair<Expression*,Tree*>>> ElseIfs
%nterm <std::vector<std::pair<std::string,ExpI>>> Fields OptParL ParList
%nterm <std::vector<Expression*>> OptExpL ExpList
%nterm <std::vector<std::variant<Expression *,std::vector<Expression*>>>> Array2
%left OR
%left AND
%right NOT
%nonassoc GT LT EQ NE GE LE
%left PLUS MINUS
%left MULTIPLY DIVIDE DIV MOD
%right UMINUS
%code requires {
namespace yy {
class Lexer;
}
}
%code {
#include <FlexLexer.h>
#include "Lexer.hpp"
#define yylex(a, b) lexer->lex(a, b)
#define VALID_OP(op1, op2, op, l) \
if (op1->type() != op2->type()) { \
error(l, "mixed-mode operations are not allowed"); \
YYERROR; \
} \
else if (valid_ops.at(op).second.find(op1->type()) == valid_ops.at(op).second.end()) { \
error(l, "operator not supported for this type"); \
YYERROR; \
}
}
%%
Program : ENDOFFILE { YYACCEPT; }
| Stmnts
;
Stmnts : Stmnts Stmnt
| Stmnt
| error { YYABORT; } /* return on first error encountered */
;
Block : Stmnts
| %empty { $$ = nullptr; }
;
Stmnt : EOL { $$ = new Empty; prev->link($$); prev = $$; }
| UNKNOWN { error(@1, "unrecognized input: " + $1); YYERROR; }
| CONSTANT ID ASSIGN Exp EOL {
if (!table->check($2).first) {
if ($4->isConstant()) {
table->store($2, $4->apply());
}
else {
error(@2, "cannot apply non-constant expression to constant: " + $2);
YYERROR;
}
}
else {
error(@2, "constant already assigned: " + $2);
YYERROR;
}
$$ = new Empty;
prev->link($$);
prev = $$;
}
| ID ASSIGN Exp EOL {
if (!table->check($1).first) {
table->store($1, $3->type());
$$ = new Assign($3, $1);
prev->link($$);
prev = $$;
}
else {
if ($3->type() == table->type($1)) {
$$ = new Assign($3, $1);
prev->link($$);
prev = $$;
}
else {
error(@1, "variable previously assigned with different type: " + $1);
YYERROR;
}
}
}
| ID ASSIGN LBRAK Array2 RBRAK EOL {
if (!table->check($1).first) {
Array2T types{};
auto array2 = $4.front().index() == 1;
auto array2sz = array2 ? std::get<std::vector<Expression*>>($4.front()).size() : 1;
for (const auto& elem : $4) {
if (array2) {
if (elem.index() != 1) {
error(@4, "expecting a sub-array");
YYERROR;
}
if (std::get<std::vector<Expression*>>(elem).size() != array2sz) {
error(@4, "sub-arrays are different lengths");
YYERROR;
}
ArrayT types1{};
for (const auto& elem1 : std::get<std::vector<Expression*>>(elem)) {
types1.push_back(elem1->type());
}
types.push_back(types1);
}
else {
if (elem.index() != 0) {
error(@4, "expecting a single element");
YYERROR;
}
types.push_back(std::get<Expression*>(elem)->type());
}
}
table->store($1, ExpT{ types });
$$ = new ArrayAssign(std::move($4), $1);
prev->link($$);
prev = $$;
}
else {
error(@1, "variable previously assigned: " + $1);
YYERROR;
}
}
| ID ASSIGN ID LCURL ExpList RCURL EOL {
if (!table->check($1).first) {
if (!table->check($3).first || table->type($3) != ExpI::RecordT || !std::get<RecordT>(table->value($3)).second) {
error(@3, "no such record definition: " + $3);
YYERROR;
}
if ($5.size() != std::get<RecordT>(table->value($3)).first.size()) {
error(@5, "wrong number of fields for record type: " + $3);
YYERROR;
}
for (size_t i{ 0 }; auto& e : $5) {
if (e->type() != table->fieldtype($3, i++)) {
error(@5, "wrong type(s) of field value(s) for record type: " + $3);
YYERROR;
}
}
RecordT rec = std::get<RecordT>(table->value($3));
rec.second = false;
table->store($1, ExpT{ rec });
$$ = new RecordAssign($1, std::move($5), std::move(rec));
prev->link($$);
prev = $$;
}
else {
error(@1, "variable previously assigned: " + $1);
YYERROR;
}
}
| ID LBRAK Exp RBRAK ASSIGN Exp EOL {
if ($3->type() != ExpI::IntT) {
error(@3, "array index must be IntExp");
YYERROR;
}
if (table->type($1) != ExpI::Array2T) {
error(@1, "not an array: " + $1);
YYERROR;
}
if (table->type($1) == ExpI::Array2T) {
if (table->arraytype($1) != $6->type()) {
error(@6, "expression has different type to array");
YYERROR;
}
}
$$ = new ElementAssign($6, $1, $3);
prev->link($$);
prev = $$;
}
| ID LBRAK Exp RBRAK LBRAK Exp RBRAK ASSIGN Exp EOL {
if (($3->type() != ExpI::IntT) || ($6->type() != ExpI::IntT)) {
error(location(@3.begin, @6.end), "array indices must be IntExp");
YYERROR;
}
if ((table->type($1) != ExpI::Array2T)) {
error(@1, "not an array: " + $1);
YYERROR;
}
if (table->arraytype($1) != $9->type()) {
error(@9, "expression has different type to array");
YYERROR;
}
$$ = new ElementAssign2($9, $1, $3, $6);
prev->link($$);
prev = $$;
}
| IF BoolExp THEN EOL Marker Block ElseIfs ELSE EOL Marker Block ENDIF EOL {
$$ = new If($2, $6, $11, $7);
$5->link($$);
prev = $$;
}
| IF BoolExp THEN EOL Marker Block ELSE EOL Marker Block ENDIF EOL {
$$ = new If($2, $6, $10);
$5->link($$);
prev = $$;
}
| IF BoolExp THEN EOL Marker Block ENDIF EOL {
$$ = new If($2, $6);
$5->link($$);
prev = $$;
}
| OUTPUT ExpList EOL {
for (auto& exp : $2) {
if (exp->type() != ExpI::StringT) {
error(@2, "expression to output must be a StringExp");
YYERROR;
}
}
$$ = new Output($2);
prev->link($$);
prev = $$;
}
| WHILE BoolExp EOL Marker Block ENDWHILE EOL {
$$ = new While($2, $5);
$4->link($$);
prev = $$;
}
| REPEAT EOL Marker Block UNTIL BoolExp EOL {
$$ = new RepeatUntil($6, $4);
$3->link($$);
prev = $$;
}
| FOR ForId ASSIGN Exp TO Exp EOL Marker Block ENDFOR EOL {
if (($4->type() != ExpI::IntT) || ($6->type() != ExpI::IntT)) {
error(location(@4.begin, @6.end), "FOR expressions must be IntExp");
}
$$ = new For($2, $4, $6, new Value(1), $9);
$8->link($$);
prev = $$;
}
| FOR ForId ASSIGN Exp TO Exp STEP Exp EOL Marker Block ENDFOR EOL {
if (($4->type() != ExpI::IntT) || ($6->type() != ExpI::IntT) || ($8->type() != ExpI::IntT)) {
error(location(@4.begin, @8.end), "FOR expressions must be IntExp");
}
else if (!$8->isConstant()) {
error(@8, "STEP must be a constant");
}
else if (!std::get<IntT>($8->apply())) {
error(@8, "STEP must be non-zero");
}
$$ = new For($2, $4, $6, $8, $11);
$10->link($$);
prev = $$;
}
| FOR ForInId IN Exp EOL Marker Block ENDFOR EOL {
if ($4->type() != ExpI::StringT) {
error(@4, "FOR-IN expressions must be StringExp");
}
$$ = new ForIn($2, $4, $7);
$6->link($$);
prev = $$;
}
| RECORD ID EOL Fields ENDRECORD EOL {
if (table->check($2).first) {
error(@2, "already exists: " + $2);
YYERROR;
}
table->store($2, ExpT{ RecordT{ $4, true } });
$$ = new Empty;
prev->link($$);
prev = $$;
}
| ID DOT ID ASSIGN Exp {
if (!table->check($1).first || table->type($1) != ExpI::RecordT || std::get<RecordT>(table->value($1)).second) {
error(@1, "no such record: " + $1);
YYERROR;
}
auto type = table->fieldtype($1, $3);
if (type == ExpI::None) {
error(@3, "no such field: " + $3);
YYERROR;
}
if (type != $5->type()) {
error(location(@3.begin, @5.end), "field and expression are of different types");
YYERROR;
}
$$ = new Assign($5, $1 + '.' + $3);
prev->link($$);
prev = $$;
}
| SUBROUTINE SubId OptParL RPAREN EOL Marker Block ENDSUBROUTINE EOL {
table->endsub($3);
$$ = new Subroutine(new Decls(table, $2), $2, std::pair { $3, ExpI::None }, $7);
$6->link($$);
prev = $$;
}
| SUBROUTINE SubId OptParL RPAREN EOL Marker Block RETURN Exp EOL ENDSUBROUTINE EOL {
table->endsub($3, $9->type());
if ($9->type() > ExpI::StringT) {
error(@8, "unsupported return type for subroutine");
YYERROR;
}
$$ = new Subroutine(new Decls(table, $2), $2, std::pair { $3, $9->type() }, $7, $9);
$6->link($$);
prev = $$;
}
| SubCall OptExpL RPAREN EOL {
if ($2.size() != table->types($1).first.size()) {
error(@2, "wrong number of arguments for call to subroutine: " + $1);
YYERROR;
}
auto iter = table->types($1).first.cbegin();
for (const auto& arg : $2) {
if (arg->type() != iter++->second) {
error(@2, "wrong type(s) of arguments for call to subroutine: " + $1);
YYERROR;
}
}
$$ = new SubroutineCall($1, $2);
prev->link($$);
prev = $$;
}
;
Marker : %empty { $$ = prev; prev = new Empty; } /* [Compilers] p350 */
;
ElseIfs : ElseIf { $$.push_back($1); }
| ElseIfs ElseIf { $$ = $1; $$.push_back($2); }
;
ElseIf : ELSE IF Exp THEN EOL Marker Block { $$ = std::pair { $3, $7 }; }
;
ForId : ID {
if (!table->check($1).first) {
table->store($1, ExpI::IntT);
}
else {
if (table->type($1) != ExpI::IntT) {
error(@1, "variable previously assigned with non-Int type: " + $1);
YYERROR;
}
}
$$ = $1;
}
;
ForInId : ID {
if (!table->check($1).first) {
table->store('_' + $1, ExpI::IntT);
table->store($1, ExpI::StringT);
}
else {
if (table->type($1) != ExpI::StringT) {
error(@1, "variable previously assigned with non-Char type: " + $1);
YYERROR;
}
}
$$ = $1;
}
;
SubId : SUBID {
if (table->insub()) {
error(@1, "cannot nest subroutines");
YYERROR;
}
if (table->check($1).first) {
error(@1, "already exists: " + $1);
YYERROR;
}
table->store($1, ExpT{ SubroutineT{} });
$$ = $1;
}
;
Fields : Fields Field { $$ = $1; $$.push_back($2); }
| Field { $$.push_back($1); }
;
Field : ID TYPESPEC ID EOL {
if (auto iter = ExpI_types.find($3); iter == ExpI_types.end()) {
error(@3, "not a valid type specifier: " + $3);
YYERROR;
}
$$ = std::pair { $1, ExpI_types.at($3) };
}
;
OptParL : %empty { $$ = {}; }
| ParList { $$ = $1; }
;
ParList : ParList COMMA Param { $$ = $1; $$.push_back($3); }
| Param { $$.push_back($1); }
;
Param : ID TYPESPEC ID {
if (auto iter = ExpI_types.find($3); iter == ExpI_types.end()) {
error(@3, "not a valid type specifier: " + $3);
YYERROR;
}
$$ = std::pair { $1, ExpI_types.at($3) };
table->store($1, ExpI_types.at($3));
}
;
SubCall : SUBID {
if (!table->check($1).first) {
error(@1, "no such subroutine: " + $1);
YYERROR;
}
else if (table->type($1) != ExpI::SubroutineT) {
error(@1, "not a subroutine: " + $1);
YYERROR;
}
$$ = $1;
}
;
OptExpL : %empty { $$ = {}; }
| ExpList { $$ = $1; }
;
ExpList : Exp { $$.push_back($1); }
| ExpList COMMA Exp { $$ = $1; $$.push_back($3); }
;
Array2 : Exp { $$.push_back($1); }
| Array2 COMMA Exp { $$ = $1; $$.push_back($3); }
| LBRAK ExpList RBRAK { $$.push_back($2); }
| Array2 COMMA LBRAK ExpList RBRAK { $$ = $1; $$.push_back($4); }
;
BoolExp : Exp {
if ($1->type() != ExpI::BoolT) {
error(@1, "non-boolean expresssion in conditional context");
YYERROR;
}
$$ = $1;
}
;
Exp : STRING { $$ = new Value($1); }
| FLOAT { $$ = new Value($1); }
| INTEGER { $$ = new Value($1); }
| BOOLEAN { $$ = new Value($1); }
| ID {
if (table->check($1).first) {
if (table->check($1).second) {
$$ = new Value(table->value($1));
}
else {
$$ = new Variable(table->type($1), $1);
}
}
else {
error(@1, "no such variable: " + $1);
YYERROR;
}
}
| ID DOT ID {
if (!table->check($1).first || table->type($1) != ExpI::RecordT || std::get<RecordT>(table->value($1)).second) {
error(@1, "no such record: " + $1);
YYERROR;
}
auto type = table->fieldtype($1, $3);
if (type == ExpI::None) {
error(@3, "no such field: " + $3);
YYERROR;
}
$$ = new Variable(table->fieldtype($1, $3), $1 + '.' + $3);
}
| ID LBRAK Exp RBRAK { $$ = new Element($1, $3, std::get<ExpI>(std::get<Array2T>(table->value($1)).at(0))); }
| ID LBRAK Exp RBRAK LBRAK Exp RBRAK { $$ = new Element2($1, $3, $6, std::get<ArrayT>(std::get<Array2T>(table->value($1)).at(0)).at(0)); }
| Exp PLUS Exp { VALID_OP($1, $3, Operator::plus, @$); $$ = new ExpressionOp($1, $3, Operator::plus); }
| Exp MINUS Exp { VALID_OP($1, $3, Operator::minus, @$); $$ = new ExpressionOp($1, $3, Operator::minus); }
| Exp MULTIPLY Exp { VALID_OP($1, $3, Operator::multiply, @$); $$ = new ExpressionOp($1, $3, Operator::multiply); }
| Exp DIVIDE Exp { VALID_OP($1, $3, Operator::divide, @$); if ($3->isConstant() && (std::get<RealT>($3->apply()) == 0.0)) { error(@$, "division by zero"); YYERROR; } $$ = new ExpressionOp($1, $3, Operator::divide); }
| Exp DIV Exp { VALID_OP($1, $3, Operator::DIV, @$); if ($3->isConstant() && (std::get<IntT>($3->apply()) == 0)) { error(@$, "division by zero"); YYERROR; } $$ = new ExpressionOp($1, $3, Operator::DIV); }
| Exp MOD Exp { VALID_OP($1, $3, Operator::MOD, @$); if ($3->isConstant() && (std::get<IntT>($3->apply()) == 0)) { error(@$, "division by zero"); YYERROR; } $$ = new ExpressionOp($1, $3, Operator::MOD); }
| MINUS Exp %prec UMINUS { VALID_OP($2, $2, Operator::negative, @$); $$ = new ExpressionOp($2, nullptr, Operator::negative); }
| Exp OR Exp { VALID_OP($1, $3, Operator::OR, @$); $$ = new ExpressionOp($1, $3, Operator::OR); }
| Exp AND Exp { VALID_OP($1, $3, Operator::AND, @$); $$ = new ExpressionOp($1, $3, Operator::AND); }
| NOT Exp { VALID_OP($2, $2, Operator::NOT, @$); $$ = new ExpressionOp($2, nullptr, Operator::NOT); }
| Exp EQ Exp { VALID_OP($1, $3, Operator::equal, @$); $$ = new ExpressionOp($1, $3, Operator::equal); }
| Exp NE Exp { VALID_OP($1, $3, Operator::not_equal, @$); $$ = new ExpressionOp($1, $3, Operator::not_equal); }
| Exp LT Exp { VALID_OP($1, $3, Operator::less_than, @$); $$ = new ExpressionOp($1, $3, Operator::less_than); }
| Exp LE Exp { VALID_OP($1, $3, Operator::less_equal, @$); $$ = new ExpressionOp($1, $3, Operator::less_equal); }
| Exp GE Exp { VALID_OP($1, $3, Operator::greater_equal, @$); $$ = new ExpressionOp($1, $3, Operator::greater_equal); }
| Exp GT Exp { VALID_OP($1, $3, Operator::greater_than, @$); $$ = new ExpressionOp($1, $3, Operator::greater_than); }
| LPAREN Exp RPAREN { $$ = $2; }
| USERINPUT { $$ = new UserInput(); }
| SubCall OptExpL RPAREN {
if ($2.size() != table->types($1).first.size()) {
error(@2, "wrong number of arguments for call to subroutine: " + $1);
YYERROR;
}
auto iter = table->types($1).first.cbegin();
for (const auto& arg : $2) {
if (arg->type() != iter++->second) {
error(@2, "wrong type(s) of arguments for call to subroutine: " + $1);
YYERROR;
}
}
$$ = new SubroutineExpression($1, $2, table->types($1).second);
}
| LEN ID RPAREN {
if (table->type($2) == ExpI::Array2T) {
$$ = new ArrayLen($2, false);
}
else if (table->type($2) == ExpI::StringT) {
$$ = new StringLen(new Variable(ExpI::StringT, $2));
}
else {
error(@2, "not an array or string: " + $2);
YYERROR;
}
}
| LEN ID LBRAK Exp RBRAK RPAREN {
if (table->type($2) != ExpI::Array2T) {
error(@2, "not an array" + $2);
YYERROR;
}
$$ = new ArrayLen($2, true);
}
| POSITION Exp COMMA Exp RPAREN {
if (($2->type() != ExpI::StringT) || ($4->type() != ExpI::StringT)) {
error(@$, "syntax POSITION(StringExp, CharExp)");
YYERROR;
}
$$ = new StringPos($2, $4);
}
| SUBSTRING Exp COMMA Exp COMMA Exp RPAREN {
if (($2->type() != ExpI::IntT) || ($4->type() != ExpI::IntT) || ($6->type() != ExpI::StringT)) {
error(@$, "syntax SUBSTRING(IntExt, IntExp, StringExp)");
YYERROR;
}
$$ = new StringSub($2, $4, $6);
}
| STRING_TO_INT Exp RPAREN {
if ($2->type() != ExpI::StringT) {
error(@2, "expression to convert must be a StringExp");
YYERROR;
}
$$ = new StringToInt($2);
}
| INT_TO_STRING Exp RPAREN {
if ($2->type() != ExpI::IntT) {
error(@2, "expression to convert must be an IntExp");
YYERROR;
}
$$ = new IntToString($2);
}
| STRING_TO_REAL Exp RPAREN {
if ($2->type() != ExpI::StringT) {
error(@2, "expression to convert must be a StringExp");
YYERROR;
}
$$ = new StringToReal($2);
}
| REAL_TO_STRING Exp RPAREN {
if ($2->type() != ExpI::RealT) {
error(@2, "expression to convert must be a RealExp");
YYERROR;
}
$$ = new RealToString($2);
}
| CHAR_TO_CODE Exp RPAREN {
if ($2->type() != ExpI::StringT) {
error(@2, "expression to convert must be a CharExp");
YYERROR;
}
$$ = new CharToCode($2);
}
| CODE_TO_CHAR Exp RPAREN {
if ($2->type() != ExpI::IntT) {
error(@2, "expression to convert must be an IntExp");
YYERROR;
}
$$ = new CodeToChar($2);
}
| RANDOM_INT Exp COMMA Exp RPAREN {
if (($2->type() != ExpI::IntT) || ($4->type() != ExpI::IntT)) {
error(location(@2.begin, @4.end), "expressions must be IntExp");
YYERROR;
}
$$ = new RandomInt($2, $4);
}
;
%%
void yy::Parser::error(const location_type& loc, const std::string &e) {
*err << loc.begin.line << ':' << loc.begin.column;
if (loc.end.line != loc.begin.line) {
*err << '-' << loc.end.line << ':' << loc.end.column - 1;
}
else if ((loc.end.column - loc.begin.column) > 1) {
*err << '-' << loc.end.column - 1;
}
*err << ", " << e << '\n';
}