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
|
--- parser.y.orig 2010-05-20 18:31:37.000000000 +0400
+++ parser.y 2010-05-20 18:38:46.000000000 +0400
@@ -1,4 +1,4 @@
-%{
+%code requires {
#include <stdio.h>
#include <stdlib.h>
@@ -9,6 +9,10 @@
#include "trnod.h"
#include "util.h"
+}
+
+%code provides {
+
static int zzcnv_table[] = {
#define DEF_TOKEN(mnem, cat, cls, yacc) yacc,
#include "token.dpp"
@@ -21,7 +25,7 @@
error(curr_token, "syntax error: %s", text);
}
-%}
+}
%union {
@@ -281,23 +285,27 @@
$2->attrib(ctx_program);
$2->translate(ctx_program);
}
+;
input_file: program | module | unit
+ ;
program: block '.' { $$ = new program_node(NULL, NULL, NULL, NULL, $1, $2); }
| PROGRAM IDENT prog_param_list ';' block '.'
{
$$ = new program_node($1, $2, $3, $4, $5, $6);
}
+;
progend: { $$ = NULL; } | '.'
+;
module: decl_part_list { $$ = new module_node(NULL, NULL, NULL, NULL, $1, NULL); }
| PROGRAM IDENT prog_param_list ';' decl_part_list progend
{
$$ = new module_node($1, $2, $3, $4, $5, $6);
}
-
+;
/* Turbo Pascal specific */
@@ -309,38 +317,46 @@
{ $$ = new unit_node(NULL, NULL, NULL, $1, $2, $3, $4, NULL, $5, $6); }
| INTERFACE unit_decl_list IMPLEMENTATION unit_def_list compoundst '.'
{ $$ = new unit_node(NULL, NULL, NULL, $1, $2, $3, $4, $5, NULL, $6); }
+ ;
unit_def_list: decl_part_list
-
+ ;
prog_param_list: { $$ = NULL; }
| '(' ident_list ')' { $$ = new import_list_node($1, $2, $3); }
+ ;
ident_list: IDENT ',' ident_list { $$ = new token_list($1, $3); }
| IDENT { $$ = new token_list($1); }
+ ;
block: decl_part_list compoundst
{
$$ = new block_node($1, $2);
}
+;
decl_part_list: { $$ = NULL; }
| decl_part decl_part_list { $1->next = $2; $$ = $1; }
+;
decl_part: label_decl_part | const_def_part | type_def_part | var_decl_part
| proc_def | proc_fwd_decl | unit_spec
+;
unit_spec: UNIT IDENT ';' INTERFACE unit_decl_list UNIT_END
{ $$ = new unit_spec_node($1, $2, $3, $4, $5); }
| INTERFACE unit_decl_list UNIT_END
{ $$ = new unit_spec_node(NULL, NULL, NULL, $1, $2); }
-
+;
unit_decl_list: { $$ = NULL; }
| unit_decl unit_decl_list { $1->next = $2; $$ = $1; }
+;
unit_decl: label_decl_part | const_def_part | type_def_part | var_decl_part
| proc_spec | proc_fwd_decl | unit_spec
+;
/*
//=============================================================================
@@ -397,15 +413,20 @@
| ICONST ':' statement { $$ = new label_node($1, $2, $3); }
| IDENT ':' statement { $$ = new label_node($1, $2, $3); }
| compoundst { $$ = $1; }
+;
compoundst: BEGIN sequence END { $$ = new compound_node($1, $2, $3); }
+;
sequence: statement | statement ';' sequence { $1->next = $3; $$ = $1; }
+;
actual_params: { $$ = NULL; } | expr_group { $$ = $1; }
+;
write_params: { $$ = NULL; }
| '(' write_list ')' { $$ = new write_list_node($1, $2, $3); }
+;
case_list: case_items
| case_items otherwise sequence
@@ -419,20 +440,26 @@
$$ = new case_node(NULL, $2, $3);
}
}
+;
otherwise: OTHERWISE | ELSE /* Turbo Pascal */
+;
case_items: { $$ = NULL; }
| case_item
| case_item ';' case_items { $1->next = $3; $$ = $1; }
+;
case_item: case_elem_list ':' statement { $$ = new case_node($1, $2, $3); }
+;
case_elem_list: case_elem
| case_elem ',' case_elem_list { $1->next = $3; $$ = $1; }
+;
case_elem: expr { $$ = $1; }
| expr DOTS expr { $$ = new case_range_node($1, $2, $3); }
+;
/*
//=============================================================================
@@ -481,6 +508,7 @@
| expr OR expr { $$ = new op_node(tn_or, $1, $2, $3); }
| expr XOR expr { $$ = new op_node(tn_xor, $1, $2, $3); }
+
| expr GT expr { $$ = new op_node(tn_gt, $1, $2, $3); }
| expr LT expr { $$ = new op_node(tn_lt, $1, $2, $3); }
| expr LE expr { $$ = new op_node(tn_le, $1, $2, $3); }
@@ -488,6 +516,7 @@
| expr EQ expr { $$ = new op_node(tn_eq, $1, $2, $3); }
| expr NE expr { $$ = new op_node(tn_ne, $1, $2, $3); }
| expr IN expr { $$ = new op_node(tn_in, $1, $2, $3); }
+;
simple_expr: primary
| PLUS simple_expr %prec UPLUS {
@@ -498,6 +527,7 @@
{ $$ = new op_node(tn_not, NULL, $1, $2); }
| '@' primary { $$ = new address_node($1, $2); }
| AND primary %prec ADDRESS { $$ = new address_node($1, $2); }
+;
primary: constant
| '(' expr_list ')' { $$ = new expr_group_node($1, $2, $3); }
@@ -506,6 +536,7 @@
| primary '^' { $$ = new deref_expr_node($1, $2); }
| primary '[' expr_list ']' { $$ = new idx_expr_node($1, $2, $3, $4); }
| LOOPHOLE '(' type ',' expr ')' { $$ = new loophole_node($1, $2, $3, $4, $5, $6); }
+;
constant: record_constant
| ICONST { $$ = new integer_node($1); }
@@ -513,41 +544,49 @@
| SCONST { $$ = new string_node($1); }
| '[' set_elem_list ']' { $$ = new set_node($1, $2, $3); }
| IDENT { $$ = new atom_expr_node($1); }
+;
set_elem_list: { $$ = NULL; }
| set_elem
| set_elem ',' set_elem_list { $1->next = $3; $$ = $1; }
+;
set_elem: expr { $$ = new set_elem_node($1); }
| expr DOTS expr { $$ = new set_range_node($1, $2, $3); }
+;
expr_list: expr | expr ',' expr_list { $1->next = $3; $$ = $1; }
+;
act_param_list: act_param
| act_param ',' act_param_list { $1->next = $3; $$ = $1; }
+;
act_param: expr | { $$ = new skipped_node(curr_token->prev_relevant()); }
-
+;
record_constant: '(' field_init_list ')' {
$$ = new record_constant_node($1, $2, $3);
}
+;
field_init_list: field_init_item { $$ = $1; }
| field_init_item ';' field_init_list { $1->next = $3; $$ = $1; }
+;
field_init_item: IDENT ':' expr { $$ = new field_init_node($1, $2, $3); }
-
+;
expr_group: '(' expr_list ')' { $$ = new expr_group_node($1, $2, $3); }
-
+;
write_list: write_param | write_param ',' write_list { $1->next = $3; $$ = $1; }
+;
write_param: expr { $$ = new write_param_node($1); }
| expr ':' expr { $$ = new write_param_node($1, $2, $3); }
| expr ':' expr ':' expr { $$ = new write_param_node($1, $2, $3, $4, $5); }
-
+;
/*
//=============================================================================
@@ -590,31 +629,40 @@
label_decl_part: LABEL label_list ';'
{ $$ = new label_decl_part_node($1, $2, $3); }
+;
label_list: ICONST { $$ = new token_list($1); }
| ICONST ',' label_list { $$ = new token_list($1, $3); }
| IDENT { $$ = new token_list($1); }
| IDENT ',' label_list { $$ = new token_list($1, $3); }
+;
const_def_part: CONST const_def_list
{ $$ = new const_def_part_node($1, $2); }
+;
const_def_list: { $$ = NULL; }
| const_def ';' const_def_list { $1->next = $3; $$ = $1; }
+;
const_def: IDENT EQ expr { $$ = new const_def_node($1, $2, $3); }
| IDENT ':' const_type EQ expr { $$ = new typed_const_def_node($1, $2, $3, $4, $5); }
+;
type_def_part: TYPE type_def_list
{ $$ = new type_def_part_node($1, $2); }
+;
type_def_list: { $$ = NULL; }
| type_def ';' type_def_list { $1->next = $3; $$ = $1; }
+;
type_def: IDENT EQ type { $$ = new type_def_node($1, $2, $3); }
+;
var_decl_part: VAR var_decl_list
{ $$ = new var_decl_part_node($1, $2); }
+;
var_decl_list: { $$ = NULL; }
| var_decl
@@ -630,29 +678,33 @@
$1->next = $5; $$ = $1;
}
| var_decl ';' var_decl_list { $1->next = $3; $$ = $1; }
+;
var_decl: ident_list ':' type { $$ = new var_decl_node($1, $2, $3); }
| IDENT ORIGIN expr ':' simple_type
{ $$ = (var_decl_node*)new var_origin_decl_node($1, $2, $3, $4, $5); }
+;
proc_decl:
PROCEDURE IDENT formal_params
{ $$ = new proc_decl_node($1, $2, $3); }
| FUNCTION IDENT formal_params ':' type
{ $$ = new proc_decl_node($1, $2, $3, $4, $5); }
-
+;
proc_fwd_decl:
PROCEDURE IDENT formal_params ';' qualifiers ';'
{ $$ = new proc_fwd_decl_node($1, $2, $3, NULL, NULL, $4, $5, $6); }
| FUNCTION IDENT formal_params ':' type ';' qualifiers ';'
{ $$ = new proc_fwd_decl_node($1, $2, $3, $4, $5, $6, $7, $8); }
+;
proc_spec:
PROCEDURE IDENT formal_params ';'
{ $$ = new proc_fwd_decl_node($1, $2, $3, NULL, NULL, $4); }
| FUNCTION IDENT formal_params ':' type ';'
{ $$ = new proc_fwd_decl_node($1, $2, $3, $4, $5, $6); }
+;
proc_def:
PROCEDURE IDENT formal_params ';' block ';'
@@ -671,135 +723,166 @@
{ $$ = new proc_def_node($1, NULL, NULL, $2, $3, $4, $5, $6, $7, $8, $9, $10); }
| FUNCTION IDENT ';' FAR ';' block ';'
{ $$ = new proc_def_node($1, NULL, NULL, $2, NULL, NULL, NULL, $3, $4, $5, $6, $7); }
+;
qualifiers: IDENT qualifiers { $$ = new token_list($1, $2); }
| SCOPE qualifiers { $$ = new token_list($1, $2); }
| IDENT { $$ = new token_list($1); }
| SCOPE { $$ = new token_list($1); }
-
+;
formal_params: { $$ = NULL; }
| '(' formal_param_list ')' { $$ = new param_list_node($1, $2, $3); }
+;
formal_param_list: formal_param
| formal_param ';' formal_param_list { $1->next = $3; $$ = $1; }
+;
formal_param: VAR param_decl { $$ = new var_decl_part_node($1, $2); }
| param_decl { $$ = $1; } | proc_decl
+;
param_decl: ident_list ':' param_type { $$ = new var_decl_node($1, $2, $3); }
| ident_list { $$ = new var_decl_node($1, NULL, NULL); }
+;
param_type: simple_type | conformant_array_type
-
-
+;
/* Types definition */
type: simple_type | array_type | record_type | object_type | set_type | file_type
| pointer_type | enum_type | range_type | string_type | fptr_type
+;
const_type: simple_type | const_array_type | record_type | const_set_type | string_type
+;
fptr_type: FUNCTION formal_params ':' type
{ $$ = new fptr_tpd_node($1, $2, $3, $4); }
| PROCEDURE formal_params { $$ = new fptr_tpd_node($1, $2); }
+;
string_type: STRING '[' expr ']' { $$ = new varying_tpd_node($1, $2, $3, $4); }
+;
simple_type: IDENT { $$ = new simple_tpd_node($1); }
| STRING { $$ = new string_tpd_node($1); }
+;
array_type: packed ARRAY '[' indices ']' OF type
{ $$ = new array_tpd_node($1, $2, $3, $4, $5, $6, $7); }
+;
const_array_type: packed ARRAY '[' indices ']' OF const_type
{ $$ = new array_tpd_node($1, $2, $3, $4, $5, $6, $7); }
+;
conformant_array_type: packed ARRAY '[' conformant_indices ']' OF simple_type
{ $$ = new array_tpd_node($1, $2, $3, $4, $5, $6, $7); }
| packed ARRAY '[' conformant_indices ']' OF conformant_array_type
{ $$ = new array_tpd_node($1, $2, $3, $4, $5, $6, $7); }
+;
enum_type: '(' ident_list ')' { $$ = new enum_tpd_node($1, $2, $3); }
+;
range_type: expr DOTS expr
{ $$ = new range_tpd_node($1, $2, $3); }
+;
pointer_type: '^' type { $$ = new ptr_tpd_node($1, $2); }
+;
set_type: packed SET OF type { $$ = new set_tpd_node($1, $2, $3, $4); }
+;
const_set_type: packed SET OF const_type { $$ = new set_tpd_node($1, $2, $3, $4); }
+;
record_type: packed RECORD field_list END
{ $$ = new record_tpd_node($1, $2, $3, $4); }
+;
object_type: OBJECT object_components END
{ $$ = new object_tpd_node($1, NULL, NULL, NULL, $2, $3); }
| OBJECT '(' IDENT ')' object_components END
{ $$ = new object_tpd_node($1, $2, $3, $4, $5, $6); }
+;
object_components: object_fields object_methods
{ $1->next = $2; $$ = $1; }
| object_methods
| object_fields
+;
object_fields: field_decl_list
{ $$ = new var_decl_part_node(NULL, $1); }
+;
field_decl_list: var_decl
| var_decl ';' { $$ = $1; }
| var_decl ';' field_decl_list { $1->next = $3; $$ = $1; }
+;
object_methods: proc_fwd_decl | proc_spec
| proc_fwd_decl object_methods { $1->next = $2; $$ = $1; }
| proc_spec object_methods { $1->next = $2; $$ = $1; }
-
-
+;
file_type: packed FIL OF type { $$ = new file_tpd_node($1, $2, $3, $4); }
+;
-packed: { $$ = NULL; } | PACKED
+packed: { $$ = NULL; } | PACKED
+;
conformant_indices: conformant_index
| conformant_index ';' conformant_indices { $1->next = $3; $$ = $1; }
+;
conformant_index: IDENT DOTS IDENT ':' type
{ $$ = new conformant_index_node($1, $2, $3, $4, $5); }
+;
indices: index_spec | index_spec ',' indices
{ $1->next = $3; $$ = $1; }
+;
index_spec: simple_type { $$ = new type_index_node($1); }
| expr DOTS expr { $$ = new range_index_node($1, $2, $3); }
-
+;
field_list:
fixed_part variant_part
{ $$ = new field_list_node($1, $2); }
| fixed_part
{ $$ = new field_list_node($1); }
+;
fixed_part: var_decl_list
+;
variant_part: CASE selector OF variant_list
{
$$ = new variant_part_node($1, $2, $3, $4);
}
+;
selector: IDENT ':' type { $$ = new selector_node($1, $2, $3); }
| type { $$ = new selector_node(NULL, NULL, $1); }
-
+;
variant_list: variant
| variant ';' { $$ = $1; }
| variant ';' variant_list { $1->next = $3; $$ = $1; }
+;
variant: expr_list ':' '(' field_list ')'
{
$$ = new variant_node($1, $2, $3, $4, $5);
}
+;
%%
|