-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathparse.y
More file actions
1161 lines (1009 loc) · 24.8 KB
/
Copy pathparse.y
File metadata and controls
1161 lines (1009 loc) · 24.8 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
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
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
%{
/*
* grammar.y
*
* Pascal grammar in Yacc format, based originally on BNF given
* in "Standard Pascal -- User Reference Manual", by Doug Cooper.
* This in turn is the BNF given by the ANSI and ISO Pascal standards,
* and so, is PUBLIC DOMAIN. The grammar is for ISO Level 0 Pascal.
* The grammar has been massaged somewhat to make it LALR, and added
* the following extensions.
*
* constant expressions
* otherwise statement in a case
* productions to correctly match else's with if's
* beginnings of a separate compilation facility
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define NYI fprintf(stderr, "NOT YET IMPLEMENTED at %s:%d\n", __FILE__, __LINE__); exit(1)
extern int line_no;
void yyerror() {
fprintf(stderr, "parse error at line %d\n", line_no);
}
#define OP_PLUS "+"
#define OP_MINUS "-"
#define OP_OR "||"
#define OP_STAR "*"
#define OP_SLASH "/"
#define OP_DIV "/"
#define OP_MOD "%"
#define OP_AND "&&"
#define OP_EQUAL "="
#define OP_NOTEQUAL "!="
#define OP_LT "<"
#define OP_GT ">"
#define OP_LE "<="
#define OP_GE ">="
#define OP_IN "in"
/*
#define OP_PLUS 1
#define OP_MINUS 2
#define OP_OR 3
#define OP_STAR 4
#define OP_SLASH 5
#define OP_DIV 6
#define OP_MOD 7
#define OP_AND 8
#define OP_EQUAL 9
#define OP_NOTEQUAL 10
#define OP_LT 11
#define OP_GT 12
#define OP_LE 13
#define OP_GE 14
#define OP_IN 15
*/
%}
%union {
void *ptr;
struct stringmap *sm;
char *string;
int op;
/* struct program *program;
struct program_heading *program_heading;
struct block *block;
struct identifier *identifier;
struct constant_definition_part *constant_definition_part;
struct type_definition_part *type_definition_part;
struct variable_definition_part *variable_definition_part;
struct procedure_and_function_definition_part *procedure_and_function_definition_part;
struct statement_part *statement_part;
*/
}
%type <string> file program program_heading identifier_list block dparts module
%type <string> label_declaration_part label_list label constant_definition_part constant_list
%type <string> constant_definition cexpression csimple_expression cterm cfactor
%type <string> cexponentiation cprimary constant sign non_string type_definition_part
%type <string> type_definition_list type_definition type_denoter new_type new_ordinal_type
%type <string> enumerated_type subrange_type new_structured_type structured_type array_type
%type <string> index_list index_type ordinal_type component_type record_type
%type <string> record_section_list record_section variant_part variant_selector variant_list
%type <string> variant case_constant_list case_constant tag_field tag_type set_type base_type
%type <string> file_type new_pointer_type domain_type variable_declaration_part
%type <string> variable_declaration_list variable_declaration
%type <string> procedure_and_function_declaration_part proc_or_func_declaration_list
%type <string> proc_or_func_declaration procedure_declaration procedure_heading directive
%type <string> formal_parameter_list formal_parameter_section_list formal_parameter_section
%type <string> value_parameter_specification variable_parameter_specification
%type <string> procedural_parameter_specification functional_parameter_specification
%type <string> procedure_identification procedure_block function_declaration function_heading
%type <string> result_type function_identification function_block statement_part
%type <string> compound_statement statement_sequence statement open_statement
%type <string> closed_statement non_labeled_closed_statement non_labeled_open_statement
%type <string> repeat_statement open_while_statement closed_while_statement
%type <string> open_for_statement closed_for_statement open_with_statement
%type <string> closed_with_statement open_if_statement closed_if_statement
%type <string> assignment_statement variable_access indexed_variable index_expression_list
%type <string> index_expression field_designator procedure_statement params
%type <string> actual_parameter_list actual_parameter goto_statement case_statement
%type <string> case_index case_list_element_list case_list_element otherwisepart
%type <string> control_variable initial_value direction final_value record_variable_list
%type <string> boolean_expression expression simple_expression term factor exponentiation
%type <string> primary unsigned_constant unsigned_number unsigned_real
%type <string> function_designator set_constructor member_designator_list member_designator
%type <string> identifier semicolon comma
%type <string> addop mulop relop crecord_literal crecord_entry_list crecord_entry
%type <string> constant_type carray_literal cexpression_list hexnum
%token <string> AND ARRAY ASSIGNMENT CASE CHARACTER_STRING COLON COMMA CONST DIGSEQ
%token <string> DIV DO DOT DOTDOT DOWNTO ELSE END EQUAL EXTERNAL FOR FORWARD FUNCTION
%token <string> GE GOTO GT IDENTIFIER IF IN LABEL LBRAC LE LPAREN LT MINUS MOD NIL NOT
%token <string> NOTEQUAL OF OR OTHERWISE PACKED PBEGIN PFILE PLUS PROCEDURE PROGRAM RBRAC
%token <string> REALNUMBER RECORD REPEAT RPAREN SEMICOLON SET SLASH STAR STARSTAR THEN
%token <string> TO TYPE UNIT UNTIL UPARROW USES VAR WHILE WITH HEXNUMBER
%%
file : program
{
puts($1);
}
| module
;
program : program_heading semicolon block DOT
{
char *str = malloc(strlen($1) + strlen($3)+50);
sprintf(str, "%s%s", $1, $3);
$$ = str;
}
;
program_heading :
PROGRAM identifier
{
char *str = malloc(strlen($2)+50);
sprintf(str, "/* PROGRAM NAME: %s */\n", $2);
$$ = str;
}
|
PROGRAM identifier LPAREN identifier_list RPAREN
;
identifier_list :
identifier_list comma identifier
{
char *str = malloc(strlen($1) + strlen($3) + 5);
sprintf(str, "%s, %s", $1, $3);
$$ = str;
}
|
identifier
;
block : label_declaration_part
dparts
procedure_and_function_declaration_part
statement_part
{
char *str = malloc(strlen($1)+strlen($2)+strlen($3)+strlen($4)+10);
sprintf(str, "%s%s%s%s", $1, $2, $3, $4);
$$ = str;
}
;
dparts :
{ $$ = ""; }
|
dparts constant_definition_part
{
char *str = malloc(strlen($1)+strlen($2)+10);
sprintf(str, "%s%s", $1, $2);
$$ = str;
}
|
dparts type_definition_part
{
char *str = malloc(strlen($1)+strlen($2)+10);
sprintf(str, "%s%s", $1, $2);
$$ = str;
}
|
dparts variable_declaration_part
{
char *str = malloc(strlen($1)+strlen($2)+10);
sprintf(str, "%s%s", $1, $2);
$$ = str;
}
/* {
char *str = malloc(strlen($1)+strlen($2)+strlen($3)+10);
sprintf(str, "%s%s%s", $1, $2, $3);
$$ = str;
} */
;
module :
ignored
dparts
procedure_and_function_declaration_part
{
char *str = malloc(strlen($2)+strlen($3)+10);
sprintf(str, "%s\n%s\n", $2, $3);
$$ = str;
}
;
ignored :
|
ignored unit
|
ignored uses
unit :
UNIT identifier semicolon
;
uses :
USES identifier_list semicolon
;
label_declaration_part :
LABEL label_list semicolon
{ $$ = $2; }
| {$$ = "";}
;
label_list : label_list comma label
| label
;
label : DIGSEQ
{ NYI; }
;
constant_definition_part : CONST constant_list
{ $$ = $2; }
;
constant_list :
constant_list constant_definition
{
char *str = malloc(strlen($1)+strlen($2)+5);
sprintf(str, "%s%s", $1, $2);
$$ = str;
}
| constant_definition
;
constant_definition : identifier EQUAL cexpression semicolon
{
char *str = malloc(20+strlen($1)+strlen($3));
sprintf(str, "var %s = %s;\n", $1, $3);
$$ = str;
}
|
identifier constant_type EQUAL cexpression semicolon
{
char *str = malloc(20+strlen($1)+strlen($4));
sprintf(str, "var %s = %s;\n", $1, $4);
$$ = str;
}
;
constant_type : COLON type_denoter
{ $$ = ""; }
;
/*constant : cexpression ; /* good stuff! */
cexpression : csimple_expression
| csimple_expression relop csimple_expression
{
char *str = malloc(strlen($1)+strlen($2)+strlen($3)+2);
sprintf(str, "%s%s%s", $1, $2, $3);
$$ = str;
}
;
csimple_expression : cterm
| csimple_expression addop cterm
{
char *str = malloc(strlen($1)+strlen($2)+strlen($3)+2);
sprintf(str, "%s%s%s", $1, $2, $3);
$$ = str;
}
| carray_literal
| crecord_literal
;
crecord_literal : LPAREN crecord_entry_list RPAREN
{
char *str = malloc(strlen($2)+50);
sprintf(str, "{\n%s\n}", $2);
$$ = str;
}
crecord_entry_list : crecord_entry_list semicolon crecord_entry
{
char *str = malloc(strlen($1) + strlen($3)+10);
sprintf(str, "%s,\n%s", $1, $3);
$$ = str;
}
|
crecord_entry
crecord_entry : identifier COLON cexpression
{
char *str = malloc(strlen($1)+strlen($3)+10);
sprintf(str, "%s : %s", $1, $3);
$$ = str;
}
cterm : cfactor
| cterm mulop cfactor
{
char *str = malloc(strlen($1)+strlen($2)+strlen($3)+2);
sprintf(str, "%s%s%s", $1, $2, $3);
$$ = str;
}
;
cfactor : sign cfactor
{
char *str = malloc(strlen($1) + strlen($2)+2);
sprintf(str, "%s%s", $1, $2);
$$ = str;
}
| cexponentiation
;
cexponentiation : cprimary
| cprimary STARSTAR cexponentiation
{
char *str = malloc(strlen($1)+strlen($3)+20);
sprintf(str, "Math.pow(%s, %s)", $1, $3);
$$ = str;
}
;
cprimary : identifier
| LPAREN cexpression RPAREN
{
char *str = malloc(strlen($2)+5);
sprintf(str, "(%s)", $2);
$$ = str;
}
| unsigned_constant
| NOT cprimary
{
char *str = malloc(strlen($2)+5);
sprintf(str, "!%s", $2);
$$ = str;
}
;
constant : non_string
| sign non_string
{
char *str = malloc(strlen($1) + strlen($2)+2);
sprintf(str, "%s%s", $1, $2);
$$ = str;
}
| CHARACTER_STRING
;
sign : PLUS { $$ = ""; }
| MINUS { $$ = "-"; }
;
non_string : DIGSEQ
| identifier
| REALNUMBER
| hexnum
;
hexnum : HEXNUMBER
{
char *str = malloc(strlen($1)+5);
sprintf(str, "0x%s", $1 + 1);
$$ = str;
}
type_definition_part :
TYPE type_definition_list
{
$$ = $2;
}
;
type_definition_list : type_definition_list type_definition
{
char *str = malloc(strlen($1) + strlen($2) + 3);
sprintf(str, "%s%s", $1, $2);
$$ = str;
}
| type_definition
;
type_definition : identifier EQUAL type_denoter semicolon
{
char *str = malloc(strlen($1) + strlen($3)+10);
sprintf(str, "var %s = %s;\n", $1, $3);
$$ = str;
}
;
type_denoter : identifier
| new_type
;
new_type : new_ordinal_type
| new_structured_type
| new_pointer_type
;
new_ordinal_type : enumerated_type
| subrange_type
;
enumerated_type : LPAREN identifier_list RPAREN
{
char *str = malloc(strlen($2) + 5);
sprintf(str, "(%s)", $2);
$$ = str;
}
;
subrange_type : constant DOTDOT constant
{
/* TODO */
}
;
new_structured_type : structured_type
| PACKED structured_type
{
$$ = $2;
}
;
structured_type : array_type
| record_type
| set_type { $$ = ""; }
| file_type { $$ = ""; }
;
array_type : ARRAY LBRAC index_list RBRAC OF component_type
{
$$ = "Array";
}
;
index_list : index_list comma index_type
| index_type
;
index_type : ordinal_type ;
ordinal_type : new_ordinal_type
| identifier
;
component_type : type_denoter ;
record_type : RECORD record_section_list END
{ $$ = "Object"; }
| RECORD record_section_list semicolon variant_part END
{ $$ = "Object"; }
| RECORD variant_part END
{ $$ = "Object"; }
;
record_section_list : record_section_list semicolon record_section
| record_section
;
record_section : identifier_list COLON type_denoter
;
variant_part : CASE variant_selector OF variant_list semicolon
| CASE variant_selector OF variant_list
| {$$ = NULL;}
;
variant_selector : tag_field COLON tag_type
| tag_type
;
variant_list : variant_list semicolon variant
| variant
;
variant : case_constant_list COLON LPAREN record_section_list RPAREN
| case_constant_list COLON LPAREN record_section_list semicolon
variant_part RPAREN
| case_constant_list COLON LPAREN variant_part RPAREN
;
case_constant_list : case_constant_list comma case_constant
| case_constant
;
case_constant : constant
| constant DOTDOT constant
;
tag_field : identifier ;
tag_type : identifier ;
set_type : SET OF base_type
;
base_type : ordinal_type ;
file_type : PFILE OF component_type
;
new_pointer_type : UPARROW domain_type
;
domain_type : identifier ;
variable_declaration_part : VAR variable_declaration_list semicolon
{$$ = $2;}
;
variable_declaration_list :
variable_declaration_list semicolon variable_declaration
{
char *str = malloc(strlen($1)+strlen($3)+4);
sprintf(str, "%s%s", $1, $3);
$$ = str;
}
| variable_declaration
;
variable_declaration : identifier_list COLON type_denoter
{
char *str = malloc(strlen($1)+strlen($3)+20);
if (strlen($3) > 0)
sprintf(str, "var %s = new %s();\n", $1, $3);
else
sprintf(str, "var %s;\n", $1);
$$ = str;
}
;
procedure_and_function_declaration_part :
proc_or_func_declaration_list semicolon
| { $$ = ""; }
;
proc_or_func_declaration_list :
proc_or_func_declaration_list semicolon proc_or_func_declaration
{
char *str = malloc(strlen($1)+strlen($3)+5);
sprintf(str, "%s%s", $1, $3);
$$ = str;
}
| proc_or_func_declaration
;
proc_or_func_declaration : procedure_declaration
| function_declaration
;
procedure_declaration : procedure_heading semicolon directive
{
$$ = "";
}
| procedure_heading semicolon procedure_block
{
char *str = malloc(strlen($1) + strlen($3) + 50);
sprintf(str, "%s {\n%s}\n", $1, $3);
$$ = str;
}
;
procedure_heading : procedure_identification
{
char *str = malloc(strlen($1)+50);
sprintf(str, "function %s()", $1);
$$ = str;
}
| procedure_identification formal_parameter_list
{
char *str = malloc(strlen($1)+strlen($2)+50);
sprintf(str, "function %s%s", $1, $2);
$$ = str;
}
;
directive : FORWARD
| EXTERNAL
;
formal_parameter_list : LPAREN formal_parameter_section_list RPAREN
{
char *str = malloc(strlen($2) + 5);
sprintf(str, "(%s)", $2);
$$ = str;
}
;
formal_parameter_section_list : formal_parameter_section_list semicolon formal_parameter_section
{
char *str = malloc(strlen($1) + strlen($3) + 4);
sprintf(str, "%s, %s", $1, $3);
$$ = str;
}
| formal_parameter_section
;
formal_parameter_section : value_parameter_specification
| variable_parameter_specification
| procedural_parameter_specification
| functional_parameter_specification
;
value_parameter_specification : identifier_list COLON identifier
;
variable_parameter_specification : VAR identifier_list COLON identifier
;
procedural_parameter_specification : procedure_heading ;
functional_parameter_specification : function_heading ;
procedure_identification : PROCEDURE identifier
{
$$ = $2;
}
;
procedure_block : block ;
function_declaration : function_heading semicolon directive /* TODO */
| function_identification semicolon function_block
{
char *str = malloc(strlen($1)+strlen($3)+20);
sprintf(str, "%s {\n%s}\n", $1, $3);
$$ = str;
}
| function_heading semicolon function_block
{
char *str = malloc(strlen($1)+strlen($3)+20);
sprintf(str, "%s {\n%s}\n", $1, $3);
$$ = str;
}
;
function_heading : FUNCTION identifier COLON result_type
{
char *str = malloc(strlen($2)+20);
sprintf(str, "function %s()", $2);
$$ = str;
}
| FUNCTION identifier formal_parameter_list COLON result_type
{
char *str = malloc(strlen($2)+strlen($3)+20);
sprintf(str, "function %s%s", $2, $3);
$$ = str;
}
;
result_type : identifier ;
function_identification : FUNCTION identifier
{
char *str = malloc(strlen($2)+20);
sprintf(str, "function %s()", $2);
$$ = str;
}
;
function_block : block ;
statement_part : compound_statement ;
compound_statement : PBEGIN statement_sequence END
{
$$ = $2;
}
;
statement_sequence : statement_sequence semicolon statement
{
char *str = malloc(strlen($1) + strlen($3)+10);
sprintf(str, "%s;\n%s", $1, $3);
$$ = str;
}
| statement
;
statement : open_statement
| closed_statement
;
open_statement : label COLON non_labeled_open_statement
{ $$ = $3; }
| non_labeled_open_statement
;
closed_statement : label COLON non_labeled_closed_statement
{ $$ = $3; }
| non_labeled_closed_statement
;
non_labeled_closed_statement : assignment_statement
| procedure_statement
| goto_statement
| compound_statement
| case_statement
| repeat_statement
| closed_with_statement
| closed_if_statement
| closed_while_statement
| closed_for_statement
|
{ $$ = ""; }
;
non_labeled_open_statement : open_with_statement
| open_if_statement
| open_while_statement
| open_for_statement
;
repeat_statement : REPEAT statement_sequence UNTIL boolean_expression
{
char *str = malloc(strlen($2)+strlen($4)+100);
sprintf(str, "do {\n%s} while(!(%s));", $2, $4);
$$ = str;
}
;
open_while_statement : WHILE boolean_expression DO open_statement
{
char *str = malloc(100+strlen($2)+strlen($4));
sprintf(str, "while (%s) {\n%s}\n", $2, $4);
$$ = str;
}
;
closed_while_statement : WHILE boolean_expression DO closed_statement
{
char *str = malloc(100+strlen($2)+strlen($4));
sprintf(str, "while (%s) {\n%s}\n", $2, $4);
$$ = str;
}
;
open_for_statement : FOR control_variable ASSIGNMENT initial_value direction
final_value DO open_statement
{
char *str = malloc(100+strlen($2)+strlen($4)+strlen($6)+strlen($8));
if (strcmp($5, "DOWNTO")) { // increasing
sprintf(str, "for (%s=%s; %s<=%s; %s++) {\n%s}",
$2, $4, $2, $6, $2, $8);
} else {
sprintf(str, "for (%s=%s; %s>=%s; %s--) {\n%s}",
$2, $4, $2, $6, $2, $8);
}
$$ = str;
}
;
closed_for_statement : FOR control_variable ASSIGNMENT initial_value direction
final_value DO closed_statement
{
char *str = malloc(100+strlen($2)+strlen($4)+strlen($6)+strlen($8));
if (strcmp($5, "DOWNTO")) { // increasing
sprintf(str, "for (%s=%s; %s<=%s; %s++) {\n%s}",
$2, $4, $2, $6, $2, $8);
} else {
sprintf(str, "for (%s=%s; %s>=%s; %s--) {\n%s}",
$2, $4, $2, $6, $2, $8);
}
$$ = str;
}
;
open_with_statement : WITH record_variable_list DO open_statement /* TODO */
{
char *str = malloc(strlen($2)+strlen($4)+16);
sprintf(str, "with (%s) {\n%s}\n", $2, $4);
$$ = str;
}
;
closed_with_statement : WITH record_variable_list DO closed_statement /* TODO */
{
char *str = malloc(strlen($2)+strlen($4)+16);
sprintf(str, "with (%s) {\n%s}\n", $2, $4);
$$ = str;
}
;
open_if_statement : IF boolean_expression THEN statement
{
char *str = malloc(strlen($2)+strlen($4)+50);
sprintf(str, "if(%s) {\n%s}", $2, $4);
$$ = str;
}
| IF boolean_expression THEN closed_statement ELSE open_statement
{
char *str = malloc(strlen($2)+strlen($4)+strlen($6)+50);
sprintf(str, "if(%s) {\n%s} else {\n%s}", $2, $4, $6);
$$ = str;
}
;
closed_if_statement : IF boolean_expression THEN closed_statement
ELSE closed_statement
{
char *str = malloc(strlen($2)+strlen($4)+strlen($6)+50);
sprintf(str, "if(%s) {\n%s} else {\n%s}", $2, $4, $6);
$$ = str;
}
;
assignment_statement : variable_access ASSIGNMENT expression
{
char *str = malloc(strlen($1) + strlen($3)+5);
sprintf(str, "%s = %s", $1, $3);
$$ = str;
}
;
variable_access : identifier
| indexed_variable
| field_designator
| variable_access UPARROW
;
indexed_variable : variable_access LBRAC index_expression_list RBRAC
{
char *str = malloc(strlen($1)+strlen($3)+5);
sprintf(str, "%s[%s]", $1, $3);
$$ = str;
}
;
index_expression_list : index_expression_list comma index_expression
{
char *str = malloc(strlen($1)+strlen($2)+strlen($3)+3);
sprintf(str, "%s][%s", $1, $3);
$$ = str;
}
| index_expression
;
index_expression : expression ;
field_designator : variable_access DOT identifier
{
char *str = malloc(strlen($1)+strlen($3)+5);
sprintf(str, "%s.%s", $1, $3);
$$ = str;
}
;
procedure_statement : variable_access params
{
char *str = malloc(strlen($1)+strlen($2)+5);
sprintf(str, "%s%s", $1, $2);
$$ = str;
}
| variable_access
{
char *str = malloc(strlen($1)+5);
sprintf(str, "%s()", $1);
$$ = str;
}
;
params : LPAREN actual_parameter_list RPAREN
{
char *str = malloc(strlen($2)+3);
sprintf(str, "(%s)", $2);
$$ = str;
}
;
actual_parameter_list : actual_parameter_list comma actual_parameter
{
char *str = malloc(strlen($1)+strlen($2)+strlen($3)+3);
sprintf(str, "%s, %s", $1, $3);
$$ = str;
}
| actual_parameter
;
/*
* this forces you to check all this to be sure that only write and
* writeln use the 2nd and 3rd forms, you really can't do it easily in
* the grammar, especially since write and writeln aren't reserved
*/
actual_parameter : expression
| expression COLON expression /* TODO handle this more gracefully */
| expression COLON expression COLON expression
;
goto_statement : GOTO label
{
fprintf(stderr, "NO GOTO ALLOWED!\n");
}
;
case_statement : CASE case_index OF case_list_element_list END
{
char *str = malloc(strlen($2)+strlen($4)+50);
sprintf(str, "switch(%s) {\n%s}\n", $2, $4);
$$ = str;
}
| CASE case_index OF case_list_element_list SEMICOLON END
{
char *str = malloc(strlen($2)+strlen($4)+50);
sprintf(str, "switch(%s) {\n%s}\n", $2, $4);
$$ = str;
}
| CASE case_index OF case_list_element_list semicolon
otherwisepart statement END
{
char *str = malloc(strlen($2)+strlen($4)+strlen($7)+50);
sprintf(str, "switch(%s) {\n%s\ndefault: %s}\n", $2, $4, $7);
$$ = str;
}
| CASE case_index OF case_list_element_list semicolon
otherwisepart statement SEMICOLON END
{
char *str = malloc(strlen($2)+strlen($4)+strlen($7)+50);
sprintf(str, "switch(%s) {\n%s\ndefault: %s}\n", $2, $4, $7);
$$ = str;
}
;
case_index : expression ;
case_list_element_list : case_list_element_list semicolon case_list_element
{
char *str = malloc(strlen($1) + strlen($3) + 5);
sprintf(str, "%s%s", $1, $3);
$$ = str;
}
| case_list_element
;
case_list_element : case_constant_list COLON statement
{
char *str = malloc(strlen($1) + strlen($3)+30);
sprintf(str, "case %s: %s\nbreak;\n", $1, $3);
$$ = str;
}
;
otherwisepart : OTHERWISE
| OTHERWISE COLON
;
control_variable : identifier ;
initial_value : expression ;
direction : TO
{ $$ = "TO"; }
| DOWNTO
{ $$ = "DOWNTO"; }
;
final_value : expression ;
record_variable_list : record_variable_list comma variable_access
{
char *str = malloc(strlen($1)+strlen($2)+strlen($3));
sprintf(str, "%s%s %s", $1, $2, $3);
$$ = str;
}
| variable_access
;
boolean_expression : expression ;
expression : simple_expression
| simple_expression relop simple_expression
{
char *str = malloc(strlen($1)+strlen($2)+strlen($3)+4);
sprintf(str, "%s %s %s", $1, $2, $3);
$$ = str;
}
;
carray_literal : LPAREN cexpression_list RPAREN
{