-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgpl.y
More file actions
567 lines (487 loc) · 15.9 KB
/
gpl.y
File metadata and controls
567 lines (487 loc) · 15.9 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
/*
This file contains the input to the bison compiler generator.
bison will use this file to generate a C/C++ parser.
The default output file for bison is: y.tab.c
$ bison record.y will produce the file y.tab.c
This grammar describes a language that is a collection of record
declarations. Each has a name and zero or more fields.
record sally
{
height = 63;
weight = 132;
age = 42;
}
record george
{
age = 31;
phone = 5551212;
}
This example DOES NOT demonstrate how a real language would
create records. It is a contrived and overly simplified
example to demonstrate the bison/flex syntax.
*/
%{ // bison syntax to indicate the start of the header
// the header is copied directly into y.tab.c (the generated parser)
#include "symbol_table.h"
Symbol_table *my_table = Symbol_table::instance();
extern int yylex(); // this lexer function returns next token
extern int yyerror(const char *); // used to print errors
extern int line_count; // current line in the input; from record.l
#include "error.h" // class for printing errors (used by gpl)
#include "parser.h"
#include <iostream>
#include <string>
using namespace std;
// bison syntax to indicate the end of the header
%}
// The union is used to declare the variable yylval which is used to
// pass data between the flex generated lexer and the bison generated parser,
// and to pass values up/down the parse tree.
//
// A union is kind of like a structure or class, but you can only use one
// field at a time. Each line describes one item in the union. The left hand
// side is the type, the right hand side is a name for the type.
//
// Unions do not have any error checking. For example, if you put an int in
// the following union (my_union.union_int = 42) and then attempt to use it
// as a string (cout << my_union.union_string) you will get garbage.
// The "%union" is bison syntax
// The "union_" is my convention to indicate a member of the union
// (it can be hard to tell what is a union field and what is not)
//
// In this example, the union only has one member (union_int). You will
// be adding a double declaration to the union.
%union {
int union_int;
double union_double;
std::string *union_string; // MUST be a pointer to a string (this sucks!)
}
// Tokens with a < > after the %token require a type.
// This is the type of the variable the scanner puts into the union.
// Fill in the < > with the appropriate type
// for example
// %token <union_int> T_INT_CONSTANT "int constant"
%error-verbose
%token T_INT "int"
%token T_DOUBLE "double"
%token T_STRING "string"
%token T_TRIANGLE "triangle"
%token T_PIXMAP "pixmap"
%token T_CIRCLE "circle"
%token T_RECTANGLE "rectangle"
%token T_TEXTBOX "textbox"
%token <union_int> T_FORWARD "forward" // value is line number
%token T_INITIALIZATION "initialization"
%token T_TRUE "true"
%token T_FALSE "false"
%token T_TOUCHES "touches"
%token T_NEAR "near"
%token T_ANIMATION "animation"
%token T_IF "if"
%token T_FOR "for"
%nonassoc IF_NO_ELSE
%nonassoc T_ELSE "else"
%token <union_int> T_PRINT "print" // value is line number
%token <union_int> T_EXIT "exit" // value is line number
%token T_LPAREN "("
%token T_RPAREN ")"
%token T_LBRACE "{"
%token T_RBRACE "}"
%token T_LBRACKET "["
%token T_RBRACKET "]"
%token T_SEMIC ";"
%token T_COMMA ","
%token T_PERIOD "."
%token T_ASSIGN "="
%token T_PLUS_ASSIGN "+="
%token T_MINUS_ASSIGN "-="
%token T_SIN "sin"
%token T_COS "cos"
%token T_TAN "tan"
%token T_ASIN "asin"
%token T_ACOS "acos"
%token T_ATAN "atan"
%token T_SQRT "sqrt"
%token T_FLOOR "floor"
%token T_ABS "abs"
%token T_RANDOM "random"
%token T_NOT "!"
%left T_NOT
%token T_OR "||"
%left T_OR
%token T_AND "&&"
%left T_AND
%token T_NOT_EQUAL "!="
%left T_NOT_EQUAL
%token T_EQUAL "=="
%left T_EQUAL
%token T_GREATER_EQUAL ">="
%left T_GREATER_EQUAL
%token T_LESS_EQUAL "<="
%left T_LESS_EQUAL
%token T_GREATER ">"
%left T_GREATER
%token T_LESS "<"
%left T_LESS
%token T_MINUS "-"
%left T_MINUS
%token T_PLUS "+"
%left T_PLUS
%token T_MOD "%"
%left T_MOD
%token T_DIVIDE "/"
%left T_DIVIDE
%token T_ASTERISK "*"
%left T_ASTERISK
%token T_ON "on"
%token T_SPACE "space"
%token T_LEFTARROW "leftarrow"
%token T_RIGHTARROW "rightarrow"
%token T_UPARROW "uparrow"
%token T_DOWNARROW "downarrow"
%token T_LEFTMOUSE_DOWN "leftmouse_down"
%token T_MIDDLEMOUSE_DOWN "middlemouse_down"
%token T_RIGHTMOUSE_DOWN "rightmouse_down"
%token T_LEFTMOUSE_UP "leftmouse_up"
%token T_MIDDLEMOUSE_UP "middlemouse_up"
%token T_RIGHTMOUSE_UP "rightmouse_up"
%token T_MOUSE_MOVE "mouse_move"
%token T_MOUSE_DRAG "mouse_drag"
%token T_F1 "f1"
%token T_AKEY "akey"
%token T_SKEY "skey"
%token T_DKEY "dkey"
%token T_FKEY "fkey"
%token T_HKEY "hkey"
%token T_JKEY "jkey"
%token T_KKEY "kkey"
%token T_LKEY "lkey"
%token T_WKEY "wkey"
%token <union_string> T_ID "identifier"
%token <union_int> T_INT_CONSTANT "int constant"
%token <union_double> T_DOUBLE_CONSTANT "double constant"
%token <union_string> T_STRING_CONSTANT "string constant"
// special token that does not match any production
// used for characters that are not part of the language
%token T_ERROR "error"
%type <union_int> simple_type
%nonassoc UNARY_OPS
%% // indicates the start of the rules
//---------------------------------------------------------------------
program:
declaration_list block_list
;
//---------------------------------------------------------------------
declaration_list:
declaration_list declaration
| empty
;
//---------------------------------------------------------------------
declaration:
variable_declaration T_SEMIC
| object_declaration T_SEMIC
| forward_declaration T_SEMIC
;
//---------------------------------------------------------------------
variable_declaration:
simple_type T_ID optional_initializer {
if($1 == T_INT){
Symbol *s = my_table->lookup(*($2));
if(s==NULL){
Symbol *s = new Symbol;
s->type = INT;
s->name = *($2);
s->value = new V(42);
my_table->insert(*$2,s);
}
else{
Error::error(Error::PREVIOUSLY_DECLARED_VARIABLE, *($2));
}
}
else if($1 == T_STRING){
Symbol *s = my_table->lookup(*($2));
if(s==NULL){
Symbol *s = new Symbol;
s->type = STRING;
s->name = *($2);
s->value = new V("Hello world");
my_table->insert(*$2,s);
}
else{
Error::error(Error::PREVIOUSLY_DECLARED_VARIABLE, *($2));
}
}
else if($1 == T_DOUBLE){
Symbol *s = my_table->lookup(*($2));
if(s==NULL){
Symbol *s = new Symbol;
s->type = DOUBLE;
s->name = *($2);
s->value = new V(3.14159);
my_table->insert(*$2,s);
}
else{
Error::error(Error::PREVIOUSLY_DECLARED_VARIABLE, *($2));
}
}
}
//| simple_type T_ID T_LBRACKET expression T_RBRACKET}
|simple_type T_ID T_LBRACKET T_INT_CONSTANT T_RBRACKET {
if($1 == T_INT){
Symbol *s = my_table->lookup(*($2));
if(s==NULL){
Symbol *s = new Symbol;
s->type = INT_ARRAY;
s->name = *($2);
s->value = new V(INT_ARRAY, $4);
my_table->insert(*($2),s);
}
else{
Error::error(Error::PREVIOUSLY_DECLARED_VARIABLE, *($2));
}
}
else if($1 == T_STRING){
Symbol *s = my_table->lookup(*($2));
if(s==NULL){
Symbol *s = new Symbol;
s->type = STRING_ARRAY;
s->name = *($2);
s->value = new V(STRING_ARRAY, $4);
my_table->insert(*($2),s);
}
else{
Error::error(Error::PREVIOUSLY_DECLARED_VARIABLE, *($2));
}
}
else if($1 == T_DOUBLE){
Symbol *s = my_table->lookup(*($2));
if(s==NULL){
Symbol *s = new Symbol;
s->type = DOUBLE_ARRAY;
s->name = *($2);
s->value = new V(DOUBLE_ARRAY, $4);
my_table->insert(*($2),s);
}
else{
Error::error(Error::PREVIOUSLY_DECLARED_VARIABLE, *($2));
}
}
}
;
//---------------------------------------------------------------------
simple_type:
T_INT {$$ = T_INT;}
| T_DOUBLE {$$ = T_DOUBLE;}
| T_STRING {$$ = T_STRING;}
;
//---------------------------------------------------------------------
optional_initializer:
T_ASSIGN expression
| empty
;
//---------------------------------------------------------------------
object_declaration:
object_type T_ID T_LPAREN parameter_list_or_empty T_RPAREN
| object_type T_ID T_LBRACKET expression T_RBRACKET
;
//---------------------------------------------------------------------
object_type:
T_TRIANGLE
| T_PIXMAP
| T_CIRCLE
| T_RECTANGLE
| T_TEXTBOX
;
//---------------------------------------------------------------------
parameter_list_or_empty :
parameter_list
| empty
;
//---------------------------------------------------------------------
parameter_list :
parameter_list T_COMMA parameter
| parameter
;
//---------------------------------------------------------------------
parameter:
T_ID T_ASSIGN expression
;
//---------------------------------------------------------------------
forward_declaration:
T_FORWARD T_ANIMATION T_ID T_LPAREN animation_parameter T_RPAREN
;
//---------------------------------------------------------------------
block_list:
block_list block
| empty
;
//---------------------------------------------------------------------
block:
initialization_block
| animation_block
| on_block
;
//---------------------------------------------------------------------
initialization_block:
T_INITIALIZATION statement_block
;
//---------------------------------------------------------------------
animation_block:
T_ANIMATION T_ID T_LPAREN check_animation_parameter T_RPAREN T_LBRACE { } statement_list T_RBRACE end_of_statement_block
;
//---------------------------------------------------------------------
animation_parameter:
object_type T_ID
;
//---------------------------------------------------------------------
check_animation_parameter:
T_TRIANGLE T_ID
| T_PIXMAP T_ID
| T_CIRCLE T_ID
| T_RECTANGLE T_ID
| T_TEXTBOX T_ID
;
//---------------------------------------------------------------------
on_block:
T_ON keystroke statement_block
;
//---------------------------------------------------------------------
keystroke:
T_SPACE
| T_UPARROW
| T_DOWNARROW
| T_LEFTARROW
| T_RIGHTARROW
| T_LEFTMOUSE_DOWN
| T_MIDDLEMOUSE_DOWN
| T_RIGHTMOUSE_DOWN
| T_LEFTMOUSE_UP
| T_MIDDLEMOUSE_UP
| T_RIGHTMOUSE_UP
| T_MOUSE_MOVE
| T_MOUSE_DRAG
| T_AKEY
| T_SKEY
| T_DKEY
| T_FKEY
| T_HKEY
| T_JKEY
| T_KKEY
| T_LKEY
| T_WKEY
| T_F1
;
//---------------------------------------------------------------------
if_block:
statement_block_creator statement end_of_statement_block
| statement_block
;
//---------------------------------------------------------------------
statement_block:
T_LBRACE statement_block_creator statement_list T_RBRACE end_of_statement_block
;
//---------------------------------------------------------------------
statement_block_creator:
// this goes to nothing so that you can put an action here in p7
;
//---------------------------------------------------------------------
end_of_statement_block:
// this goes to nothing so that you can put an action here in p7
;
//---------------------------------------------------------------------
statement_list:
statement_list statement
| empty
;
//---------------------------------------------------------------------
statement:
if_statement
| for_statement
| assign_statement T_SEMIC
| print_statement T_SEMIC
| exit_statement T_SEMIC
;
//---------------------------------------------------------------------
if_statement:
T_IF T_LPAREN expression T_RPAREN if_block %prec IF_NO_ELSE
| T_IF T_LPAREN expression T_RPAREN if_block T_ELSE if_block
;
//---------------------------------------------------------------------
for_statement:
T_FOR T_LPAREN statement_block_creator assign_statement end_of_statement_block T_SEMIC expression T_SEMIC statement_block_creator assign_statement end_of_statement_block T_RPAREN statement_block
;
//---------------------------------------------------------------------
print_statement:
T_PRINT T_LPAREN expression T_RPAREN
;
//---------------------------------------------------------------------
exit_statement:
T_EXIT T_LPAREN expression T_RPAREN
;
//---------------------------------------------------------------------
assign_statement:
variable T_ASSIGN expression
| variable T_PLUS_ASSIGN expression
| variable T_MINUS_ASSIGN expression
;
//---------------------------------------------------------------------
variable:
T_ID
| T_ID T_LBRACKET expression T_RBRACKET
| T_ID T_PERIOD T_ID
| T_ID T_LBRACKET expression T_RBRACKET T_PERIOD T_ID
;
//---------------------------------------------------------------------
expression:
primary_expression
| expression T_OR expression
| expression T_AND expression
| expression T_LESS_EQUAL expression
| expression T_GREATER_EQUAL expression
| expression T_LESS expression
| expression T_GREATER expression
| expression T_EQUAL expression
| expression T_NOT_EQUAL expression
| expression T_PLUS expression
| expression T_MINUS expression
| expression T_ASTERISK expression
| expression T_DIVIDE expression
| expression T_MOD expression
| T_MINUS expression %prec UNARY_OPS
| T_NOT expression %prec UNARY_OPS
| math_operator T_LPAREN expression T_RPAREN
| variable geometric_operator variable
;
//---------------------------------------------------------------------
primary_expression:
T_LPAREN expression T_RPAREN
| variable
| T_INT_CONSTANT
| T_TRUE
| T_FALSE
| T_DOUBLE_CONSTANT
| T_STRING_CONSTANT
;
//---------------------------------------------------------------------
geometric_operator:
T_TOUCHES
| T_NEAR
;
//---------------------------------------------------------------------
math_operator:
T_SIN
| T_COS
| T_TAN
| T_ASIN
| T_ACOS
| T_ATAN
| T_SQRT
| T_ABS
| T_FLOOR
| T_RANDOM
;
//---------------------------------------------------------------------
empty:
// empty goes to nothing so that you can use empty in productions
// when you want a production to go to nothing
;