-
Notifications
You must be signed in to change notification settings - Fork 1
/
X0Compiler.final.v2.y
1925 lines (1795 loc) · 55.6 KB
/
X0Compiler.final.v2.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
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
//声明部分
%{
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <memory.h>
#include <malloc.h>
#include <ctype.h>
#define bool int
#define true 1
#define false 0
#define MAX_SYM_TABLE 100 /* 符号表容量 */
#define MAX_AL 10 /* 标识符的最大长度 */
#define MAX_ADD 114514 /* 地址上界*/
#define MAX_CODE 200 /* 最多的虚拟机代码数 */
#define MAX_STACK 114514 /* 运行时数据栈元素最多为500个 */
#define MAX_VAL_LEN 114 /*无类型数据长度*/
#define MAX_DATA_STACK 11451 /*数据栈大小*/
#define MAX_ARRAY_DIM 7
enum object {
constant_single,
variable_single,
constant_array,
variable_array,
};
enum data_type_enum {
integer,
boolean,
string_chars,
single_char,
};
struct tablestruct {
char name[MAX_AL];
enum object kind;
int type;
char val[MAX_VAL_LEN];
int adr;
int size;
int array_lable;
int array_dim_size[MAX_ARRAY_DIM];
int init_lable;
}table[MAX_SYM_TABLE];
enum fct {
lit, opr, lod,
sto, lodb, stob,
ini, jmp, jpc,
};
struct instruction {
enum fct f;
int l;
char val[MAX_VAL_LEN];
}code[MAX_CODE];
struct data_stack{
enum data_type_enum t;
char val[MAX_VAL_LEN];
}s[MAX_STACK];
int sym_table_tail = 0; /* 符号表当前尾指针 */
int VM_pointer; /* 虚拟机代码指针, 取值范围[0, cxmax-1] */
char id[MAX_AL];
enum object cur_kind;
char cur_id[MAX_VAL_LEN];
int cur_table_adr;
int cur_type;
int do_begin_pos;
int num;
int curr_address = 3;
int cur_adr;
int stack_top = 2;
int input_val_int;
int becomes_adr;
int adr_shift;
int arr_size;
struct dim_recoder {
int idx_type; // 0 for num, 1 for var_adr
int val;
};
int arr_dim_recoder[MAX_ARRAY_DIM];
int arr_dim_idx = 0;
struct dim_recoder tmp_dim_recoder[MAX_ARRAY_DIM];
int tmp_dim_idx = 0;
int tmp_type_lable;
int ndim_var_lable;
bool input_val_bool;
char input_val_char;
char input_val_string[MAX_VAL_LEN];
bool listall_switch_on = 1; /* 显示虚拟机代码与否 */
bool sym_table_switch_on = 1; /* 显示符号表与否 */
FILE* fin; /* 输入源文件 */
FILE* ftable; /* 输出符号表 */
FILE* fcode; /* 输出虚拟机代码 */
FILE* foutput; /* 输出出错示意(如有错) */
FILE* fresult; /* 输出执行结果 */
FILE* fdatastack;
char fname[MAX_SYM_TABLE];
int err;
extern int line;
void init();
void enter(enum object k);
void gen(enum fct x, int y, char *z);
void listall();
void displaytable();
//void interpret();
int base(int l, int* s, int b);
#define INT_TYPE 101
#define BOOL_TYPE 102
#define CHAR_TYPE 103
#define STRING_TYPE 104
#define INC_OPR 201
#define DEC_OPR 202
#define NOT_OPR 203
#define PLUS_OPR 204
#define MINUS_OPR 205
#define TIMES_OPR 206
#define SLASH_OPR 207
#define MOD_OPR 208
#define AND_OPR 209
#define OR_OPR 210
#define XOR_OPR 211
#define LSS_OPR 212
#define LEQ_OPR 213
#define GTR_OPR 214
#define GEQ_OPR 215
#define EQL_OPR 216
#define NEQ_OPR 217
#define READ_OPR 218
#define WRITE_OPR 219
#define UMINUS_OPR 220
#define ODD_OPR 221
#define OFF_OPR 222
#define POP_OPR 223
#define CASE_EQL_OPR 224
int const_label = 0;
int declaration_init = 0;
%}
//辅助定义部分
%union {
char *ident;
int number;
int boolean;
int type_val;
char *string_chars;
char single_char;
struct {
int sym, dim;
} arr;
struct {
int case_begin;
int case_end;
} case_recorder;
}
%token BOOLSYM BREAKSYM CASESYM CHARSYM CONSTSYM CONTINUESYM DOSYM DEFAULTSYM ELSESYM FORSYM IFSYM INTSYM
%token MAINSYM READSYM REPEATSYM SWITCHSYM STRINGSYM THENSYM UNSIGNEDSYM UNTILSYM WHILESYM WRITESYM RETURNSYM
%token BECOMES LSS LEQ GTR GEQ EQL NEQ PLUS INC MINUS DEC TIMES SLASH MOD LPAREN RPAREN LBRACK
%token RBRACK LBPAREN RBPAREN AND OR NOT XOR COMMA PERIOD COLON SEMICOLON ODDSYM
%right BECOMES
%left AND OR
%left XOR
%left EQL NEQ
%left GTR LSS LEQ GEQ
%left PLUS MINUS
%left TIMES SLASH MOD
%right INC DEC NOT ODDSYM
%nonassoc UMINUS
%nonassoc LOWER_THAN_ELSE
%nonassoc ELSESYM
%token <ident> IDENT
%token <number> NUMBER
%token <string_chars> STRING
%token <single_char> CHAR
%token <boolean> BOOL
%type <type_val> expression if_stat for_stat
%type <type_val> simple_expr additive_expr multiplying_expr term factor
%type <number> binary_bool_opr binary_add_opr binary_mul_opr unary_opr
%type <number> gen_jmp gen_jpc
%type <number> get_code_addr var
%type <number> dimension_list dimension CASESYM
%type <arr> dimension_exp
%type <case_recorder> case_stat
////////////////////////////////////////////////////////
//规则部分
%%
program: MAINSYM
LBPAREN
declaration_list
statement_list
RBPAREN
;
declaration_list: declaration_list declaration_stat
|
;
declaration_stat: var_declaration
| const_declaration
| var_array_declaration
| const_array_declaration
;
var_declaration: type ident_list SEMICOLON
;
const_declaration: CONSTSYM set_const_label
type ident_list {
const_label = 0;
}
SEMICOLON
;
var_array_declaration: type ident_array_list SEMICOLON
;
const_array_declaration: CONSTSYM set_const_label
type ident_array_def {
const_label = 0;
}
SEMICOLON
;
set_const_label: {
const_label = 1;
}
;
ident_array_list: ident_array_def
| ident_array_list COMMA ident_array_def
;
ident_list: ident_def
| ident_list COMMA ident_def
;
ident_array_def: IDENT LBRACK dimension_list RBRACK {
int i = 0, j = arr_dim_idx-1;
arr_size = $3;
stack_top += arr_size;
strcpy(id, $1);
declaration_init = 0;
/*
printf("CHECK POINT in array declaration: %s[", id);
for (i = 0; i < arr_dim_idx; ++i) {
if (i != arr_dim_idx-1) {
printf("%d][", arr_dim_recoder[i]);
}
else {
printf("%d]\n", arr_dim_recoder[i]);
}
}
*/
if (const_label == 1) { // const declaration part
enter(constant_array);
/* need classification for different data type */
}
else {
enter(variable_array);
}
memset(arr_dim_recoder, 0, sizeof(arr_dim_recoder));
arr_dim_idx = 0;
arr_size = 0;
}
;
dimension_list: dimension {
$$ = $1;
arr_dim_recoder[arr_dim_idx++] = $1;
}
| dimension_list RBRACK LBRACK dimension {
$$ = $1 * $4;
arr_dim_recoder[arr_dim_idx++] = $4;
}
;
dimension: NUMBER {
$$ = $1;
}
;
ident_def: IDENT {
if (const_label == 1) { // const declaration without initialization.
yyerror("Const declaration requires initialization!\n", 1001);
return -1; // error code.
}
else { // var declaration part without initialation
declaration_init = 0;
strcpy(id, $1);
enter(variable_single);
stack_top++;
/* need classification for different data type
switch (cur_type) {
case INT_TYPE:
// ... solution
break;
case BOOL_TYPE:
// ... solution
break;
case CHAR_TYPE:
// ... solution
break;
case STRING_TYPE:
// ... solution
break;
}
*/
}
}
| IDENT BECOMES simple_expr {
//printf("current ident type is: %d in ident_def part\n", cur_type);
//printf("current expr type is: %d in ident_def part\n", $3);
if (cur_type != $3) {
yyerror("Mismatched in data type!\n", 1002);
}
declaration_init = 1;
strcpy(id, $1);
if (const_label == 1) { // const declaration part
enter(constant_single);
/* need classification for different data type */
}
else { // var declaration part with initialation
enter(variable_single);
stack_top++;
int id_addr = 0;
enum data_type_enum type_tmp;
for (int i = 1; i <= sym_table_tail; i++) {
if (strcmp($1, table[i].name) == 0) {
id_addr = table[i].adr;
break;
}
}
gen(sto, cur_type, (char*)id_addr);
}
}
;
type: INTSYM {cur_type = INT_TYPE;}
| CHARSYM {cur_type = CHAR_TYPE;}
| STRINGSYM {cur_type = STRING_TYPE;}
| BOOLSYM {
cur_type = BOOL_TYPE;
//printf("BOOL symbol find!\n");
}
;
compound_stat: LBPAREN
statement_list
RBPAREN
;
statement_list: statement_list statement
| statement
;
statement: expression_stat
| compound_stat
| select_stat
| iteration_stat
| read_stat
| write_stat
;
expression_stat: expression_stat COMMA expression SEMICOLON pop_stack
| expression SEMICOLON pop_stack
;
expression: var {
/*
int i, res = -1;
for (i = 1; i <= sym_table_tail; ++i) {
if (strcmp(table[i].name, cur_id) == 0) {
res = table[i].adr;
break;
}
}
//printf("check point\n");
if (res == -1) {
yyerror("Undefined _single!\n");
}
else {
becomes_adr = cur_adr;
}*/
//becomes_adr = cur_adr;
//printf("cur pos is expression_var part, cur_id is %s with adr %d\n", cur_id, becomes_adr);
}
BECOMES expression {
cur_kind = table[$1].kind;
cur_type = table[$1].type;
if (cur_kind == constant_single) {
yyerror("Illegal assignment on constant_single!\n", 1003);
}
//printf("current var type is: %d in expression part\n", cur_type);
//printf("current expr type is: %d in expression part\n", $3);
//printf("current data stack top is: %d\n", *(int*)s[stack_top].val);
//if ($1 != $4) {
// yyerror("Mismatched in data type!\n");
//}
becomes_adr = table[$1].adr;
//printf("check point1 in expression\n");
//printf("cur pos is expression part, cur_adr to sto is: %d\n", becomes_adr);
if (table[$1].array_lable) {
gen(stob, cur_type, (char*)becomes_adr);
}
else {
gen(sto, cur_type, (char*)becomes_adr);
}
//printf("check point2 in expression\n");
$$ = 0; // undefined
}
| simple_expr {
$$ = $1;
}
| {
}
;
pop_stack: {
int opr_tmp = POP_OPR;
gen(opr, 0, (char*)opr_tmp);
}
;
var: IDENT {
strcpy(cur_id, $1);
//printf("current ident is %s in single var.\n", cur_id);
int i, exist_flag = 0;
for (i = 1; i <= sym_table_tail; ++i) {
if (strcmp(cur_id, table[i].name) == 0) {
exist_flag = 1;
cur_table_adr = i;
cur_adr = table[i].adr;
$$ = i;
cur_kind = table[i].kind;
cur_type = table[i].type;
//printf("current pos is var part, cur_id is %s in %d with type %d\n", cur_id, cur_adr, cur_type);
break;
}
}
if (exist_flag == 0) {
yyerror("Undefinded variable_single!\n", 1004);
}
}
| dimension_exp {
$$ = $1.sym;
/*memset(tmp_dim_recoder, 0, sizeof(tmp_dim_recoder));
tmp_dim_idx = 0;
strcpy(cur_id, $1);
printf("current ident is %s in ndim_arr.\n", cur_id);
int i, j, exist_flag = 0;
int tmp_size = 1, adr_shift = 0;
for (i = 1; i <= sym_table_tail; ++i) {
if (strcmp(cur_id, table[i].name) == 0) {
exist_flag = table[i].array_lable;
cur_table_adr = i;
cur_adr = table[i].adr;
//$$ = INT_TYPE;
cur_kind = table[i].kind;
cur_type = table[i].type;
/*
printf("current dim_size arr is: ");
for (j = 0; j < MAX_ARRAY_DIM; ++j) {
printf("%d\t", table[i].array_dim_size[j]);
}
printf("\n");
//printf("current pos is var_ndim part, cur_id is %s in %d with type %d\n", cur_id, cur_adr, cur_type);
break;
}
}
if (exist_flag == 0) {
yyerror("Undefinded variable or non-array variable!\n");
}
ndim_var_lable = 1;
}
dimension_exp RBRACK {
$$ = cur_type;
}*/
}
;
dimension_exp: IDENT LBRACK expression RBRACK {
strcpy(cur_id, $1);
int i, exist_flag = 0;
for (i = 1; i <= sym_table_tail; ++i) {
if (strcmp(cur_id, table[i].name) == 0) {
exist_flag = table[i].array_lable;
break;
}
}
if (!exist_flag) {
yyerror("Undefinded variable or non-array variable!\n", 1005);
}
$$.sym = i;
$$.dim = 1;
}
| dimension_exp {
/*int j;
printf("current dim_size arr is: ");
for (j = 0; j < MAX_ARRAY_DIM; ++j) {
printf("%d ", table[cur_table_adr].array_dim_size[j]);
}
printf("\n");*/
//printf("gen dim_exp lit when tmp_idx is %d and val is %d\n", tmp_dim_idx, table[cur_table_adr].array_dim_size[tmp_dim_idx]);
gen(lit, INT_TYPE, (char*)&(table[$1.sym].array_dim_size[$1.dim]));
gen(opr, 0, TIMES_OPR);
}
LBRACK expression RBRACK {
gen(opr, 0, PLUS_OPR);
$$.sym = $1.sym;
$$.dim = $1.dim+1;
/*
cur_table_adr = $$.sym;
cur_adr = table[$$.sym].adr;
cur_kind = table[$$.sym].kind;
cur_type = table[$$.sym].type;
*/
}
;
select_stat: if_stat
| switch_stat
;
if_stat: IFSYM LPAREN expression RPAREN gen_jpc
statement %prec LOWER_THAN_ELSE {
int back_patch_pos = $5, back_patch_val = VM_pointer;
memcpy((void*)code[back_patch_pos].val, (const void*)&back_patch_val, MAX_VAL_LEN);
}
| IFSYM LPAREN expression RPAREN gen_jpc
statement ELSESYM gen_jmp
statement {
int back_patch_pos_pre = $5, back_patch_val_pre = $8+1;
memcpy((void*)code[back_patch_pos_pre].val, (const void*)&back_patch_val_pre, MAX_VAL_LEN); // for false stat to jump in
int back_patch_pos = $8, back_patch_val = VM_pointer;
memcpy((void*)code[back_patch_pos].val, (const void*)&back_patch_val, MAX_VAL_LEN); // for ture stat to jump out
}
;
switch_stat: SWITCHSYM LPAREN expression RPAREN LBPAREN case_list default_stat RBPAREN {
gen(opr, 0, POP_OPR)
}
;
case_list: case_list case_stat {
//printf("current case_begin is: %d, case_end is %d\n", $2.case_begin, $2.case_end);
int adr_tmp = $2.case_end;
memcpy((void*)code[$2.case_begin].val, (const void*)&adr_tmp, MAX_VAL_LEN);
}
| case_stat {
//printf("current case_begin is: %d, case_end is %d\n", $1.case_begin, $1.case_end);
int adr_tmp = $1.case_end;
memcpy((void*)code[$1.case_begin].val, (const void*)&adr_tmp, MAX_VAL_LEN);
}
;
case_stat: CASESYM expression {
int opr_tmp;
opr_tmp = CASE_EQL_OPR;
gen(opr, 0, (char*)opr_tmp);
$1 = VM_pointer;
opr_tmp = 0;
gen(jpc, 0, (char*)opr_tmp);
}
COLON compound_stat {
$$.case_begin = $1;
$$.case_end = VM_pointer;
}
;
default_stat: DEFAULTSYM COLON compound_stat
;
gen_jpc: {
$$ = VM_pointer;
int adr_tmp = 0;
gen(jpc,0, (char*)adr_tmp);
}
;
gen_jmp: {
$$ = VM_pointer;
int adr_tmp = 0;
gen(jmp,0, (char*)adr_tmp);
}
;
iteration_stat: for_stat
| while_stat
| do_while_stat
| repeat_stat
;
for_stat: FORSYM LPAREN expression SEMICOLON get_code_addr
expression SEMICOLON get_code_addr {
int opr_tmp = 0;
gen(jpc, 0, (char*)opr_tmp); // false --- jump out
gen(jmp, 0, (char*)opr_tmp); // true --- jump to statement
}
get_code_addr expression RPAREN {
int adr_tmp = $5;
gen(jmp, 0, (char*)adr_tmp); // back to condition part
}
get_code_addr
statement {
int adr_tmp = $10, adr_jpc_e2, adr_jpc_e2_pos = $8, adr_jmp_e2 = $14;
gen(jmp, 0, (char*)adr_tmp);
adr_jpc_e2 = VM_pointer;
memcpy((void*)code[adr_jpc_e2_pos].val, (const void*)&adr_jpc_e2, MAX_VAL_LEN);
memcpy((void*)code[adr_jpc_e2_pos+1].val, (const void*)&adr_jmp_e2, MAX_VAL_LEN);
}
;
while_stat: WHILESYM LPAREN get_code_addr expression RPAREN get_code_addr {
int opr_tmp = 0;
gen(jpc, 0, (char*)opr_tmp);
}
statement {
int while_begin_pos = $3, while_jmp_pos = $6, while_back_patch_pos;
gen(jmp, 0, (char*)while_begin_pos);
while_back_patch_pos = VM_pointer;
memcpy((void*)code[while_jmp_pos].val, (const void*)&while_back_patch_pos, MAX_VAL_LEN);
}
;
repeat_stat: REPEATSYM get_code_addr statement UNTILSYM LPAREN expression RPAREN SEMICOLON {
gen(jpc, 0, $2);
}
;
get_code_addr: {$$ = VM_pointer};
do_while_stat: DOSYM {
do_begin_pos = VM_pointer;
}
statement
WHILESYM LPAREN
expression
RPAREN SEMICOLON {
int opr_tmp = 0;
gen(jpc, 0, (char*)opr_tmp); // jump out
gen(jmp, 0, (char*)do_begin_pos); // back to begin part
memcpy((void*)code[VM_pointer - 2].val, (const void*)&VM_pointer, MAX_VAL_LEN); // complete jump out adr
}
;
read_stat: READSYM LPAREN read_list RPAREN SEMICOLON
;
read_list: read_list COMMA read_var
| read_var
;
read_var: var {
if (cur_kind == constant_single || cur_kind == constant_array) {
yyerror("Operation on contant data!\n", 1006);
}
gen(opr, cur_type, (char*)READ_OPR); // OPR undefined;
cur_adr = table[$1].adr;
if (table[$1].array_lable) {
gen(stob, cur_type, (char*)cur_adr);
int opr_tmp = POP_OPR;
gen(opr, 0, (char*)opr_tmp);
}
else {
gen(sto, cur_type, (char*)cur_adr);
int opr_tmp = POP_OPR;
gen(opr, 0, (char*)opr_tmp);
}
}
;
write_stat: WRITESYM LPAREN write_list RPAREN SEMICOLON
;
write_list: write_list COMMA expression {
int opr_tmp = WRITE_OPR;
gen(opr, 0, (char*)opr_tmp);
}
| expression {
int opr_tmp = WRITE_OPR;
gen(opr, 0, (char*)opr_tmp);
}
;
simple_expr: additive_expr {
$$ = $1;
}
| simple_expr binary_bool_opr additive_expr {
$$ = BOOL_TYPE;
int opr_tmp = $2;
gen(opr, 0, (char*)opr_tmp);
}
;
additive_expr: additive_expr binary_add_opr multiplying_expr {
$$ = $1;
int opr_tmp = $2;
gen(opr, 0, (char*)opr_tmp);
}
| multiplying_expr {
$$ = $1;
}
;
multiplying_expr: multiplying_expr binary_mul_opr term {
$$ = $1;
int opr_tmp = $2;
gen(opr, 0, (char*)opr_tmp);
}
| term {
$$ = $1;
}
;
term: factor {
$$ = $1;
}
| unary_opr term {
$$ = $2;
int opr_tmp = $1 + 3;
printf("current opr in unary_opr term is: %d\n", opr_tmp);
//opr_tmp = (opr_tmp == INC_OPR ? PLUS_OPR : MINUS_OPR);
int tmp_num = 1;
gen(lit, INT_TYPE, (char*)&tmp_num);
gen(opr, 0, (char*)opr_tmp);
if (table[cur_table_adr].array_lable) {
gen(stob, cur_type, (char*)cur_adr);
gen(lit, INT_TYPE, (char*)&cur_table_adr);
gen(lodb, cur_type, (char*)cur_adr);
}
else {
gen(sto, cur_type, (char*)cur_adr);
gen(lod, cur_type, (char*)cur_adr);
}
//gen(lit, 0, (char*)tmp_num);
}
| MINUS factor %prec UMINUS {
$$ = $2;
int opr_tmp = UMINUS_OPR;
gen(opr, 0, (char*)opr_tmp);
}
| NOT factor {
$$ = BOOL_TYPE;
int opr_tmp = NOT_OPR;
gen(opr, 0, (char*)opr_tmp);
}
| ODDSYM factor {
$$ = BOOL_TYPE;
int opr_tmp = ODD_OPR;
gen(opr, 0, (char*)opr_tmp);
}
;
factor: NUMBER {
$$ = INT_TYPE;
input_val_int = $1;
//printf("factor part input val(int) is: %d\n", input_val_int);
gen(lit, INT_TYPE, (char*)&input_val_int);
}
| BOOL {
$$ = BOOL_TYPE;
input_val_bool = $1;
//printf("factor part input val(bool) is: %d\n", input_val_bool);
gen(lit, BOOL_TYPE, (char*)&input_val_bool);
}
| STRING {
$$ = STRING_TYPE;
strcpy(input_val_string, $1);
//input_val_string[strlen(input_val_string)-1] = '\0';
//printf("factor part input val(string) is: %s\n", input_val_string);
gen(lit, STRING_TYPE, &input_val_string);
}
| CHAR {
$$ = CHAR_TYPE;
input_val_char = $1;
//printf("factor part input val(char) is: %c\n", input_val_char);
gen(lit, CHAR_TYPE, (char*)&input_val_char);
}
| var {
cur_table_adr = $1;
cur_adr = table[$1].adr;
cur_kind = table[$1].kind;
if (cur_kind == constant_single) {
gen(lit, cur_type, table[cur_table_adr].val);
}
else if (cur_kind == variable_single){
// Maybe refresh the data_stack val is required.
gen(lod, cur_type, (char*)cur_adr);
}
else {
gen(lit, INT_TYPE, (char*)&cur_table_adr);
gen(lodb, cur_type, (char*)cur_adr);
}
ndim_var_lable = 0;
}
| LPAREN expression RPAREN {
$$ = $2;
}
;
binary_add_opr: PLUS {
$$ = PLUS_OPR;
}
| MINUS {
$$ = MINUS_OPR;
}
;
binary_mul_opr: TIMES {
$$ = TIMES_OPR;
}
| SLASH {
$$ = SLASH_OPR;
}
| MOD {
$$ = MOD_OPR;
}
;
binary_bool_opr: AND {
$$ = AND_OPR;
}
| OR {
$$ = OR_OPR;
}
| XOR {
$$ = XOR_OPR;
}
| LSS {
$$ = LSS_OPR;
}
| LEQ {
$$ = LEQ_OPR;
}
| GTR {
$$ = GTR_OPR;
}
| GEQ {
$$ = GEQ_OPR;
}
| EQL {
$$ = EQL_OPR;
}
| NEQ {
$$ = NEQ_OPR;
}
;
unary_opr: INC {
$$ = INC_OPR;
}
| DEC {
$$ = DEC_OPR;
}
;
////////////////////////////////////////////////////////
//程序部分
%%
int yyerror(char *s, int error_num) {
err = err + 1;
printf("%d: %s in line %d\n", error_num, s, line);
fprintf(foutput, "%d: %s in line %d\n", error_num, s, line);
return 0;
}
/* 初始化 */
void init() {
sym_table_tail = 0;
VM_pointer = 0;
num = 0;
err = 0;
input_val_int = 0;
input_val_bool = 0;
input_val_char = 0;
strcpy(input_val_string, "\0");
}
/* 在符号表中加入一项 */
void enter(enum object k) {
int i;
char data_type_name[][15]= {
{"integer"},{"boolean"},{"string_chars"},{"single_char"},
};
sym_table_tail++;
strcpy(table[sym_table_tail].name, id);
table[sym_table_tail].kind = k;
table[sym_table_tail].init_lable = declaration_init;
table[sym_table_tail].type = cur_type;
//printf("current pos is enter part, the input id is: %s with declaration_init: %d, cur_type: %d\n", id, declaration_init, cur_type);
//printf("%d:\t%d\t%s\t%d\t%d\n", sym_table_tail, k_kind, id, k_type, declaration_init);
switch(k) {
case constant_single:
switch (cur_type) {
case INT_TYPE:
memcpy((void*)&table[sym_table_tail].val, (const void*)&input_val_int, MAX_VAL_LEN);
//table[sym_table_tail].adr = curr_address++;
break;
case BOOL_TYPE:
memcpy((void*)&table[sym_table_tail].val, (const void*)&input_val_bool, MAX_VAL_LEN);
//table[sym_table_tail].adr = curr_address++;
break;
case CHAR_TYPE:
memcpy((void*)&table[sym_table_tail].val, (const void*)&input_val_char, MAX_VAL_LEN);
//table[sym_table_tail].adr = curr_address++;
break;
case STRING_TYPE:
memcpy((void*)&table[sym_table_tail].val, (const void*)&input_val_string, MAX_VAL_LEN);
//table[sym_table_tail].adr = curr_address++;
break;
default:
printf("Unknown data type in constant_single enter part!");
}
break;
case constant_array:
table[sym_table_tail].array_lable = 1;
break;
case variable_single:
//printf("variable_single entered!\n");
switch (cur_type) {
case INT_TYPE:
if (declaration_init) {
memcpy((void*)&table[sym_table_tail].val, (const void*)&input_val_int, MAX_VAL_LEN);
}
table[sym_table_tail].adr = curr_address++;
break;
case BOOL_TYPE:
if (declaration_init) {
memcpy((void*)&table[sym_table_tail].val, (const void*)&input_val_bool, MAX_VAL_LEN);
}
table[sym_table_tail].adr = curr_address++;
break;
case CHAR_TYPE:
if (declaration_init) {
memcpy((void*)&table[sym_table_tail].val, (const void*)&input_val_char, MAX_VAL_LEN);
}
table[sym_table_tail].adr = curr_address++;
break;
case STRING_TYPE:
if (declaration_init) {
memcpy((void*)&table[sym_table_tail].val, (const void*)&input_val_string, MAX_VAL_LEN);
}
table[sym_table_tail].adr = curr_address++;
break;
default:
printf("Unknown data type in variable_single enter part!");
}
break;
case variable_array:
table[sym_table_tail].array_lable = 1;
table[sym_table_tail].adr = curr_address;
table[sym_table_tail].size = arr_size;
for (i = 0; i < MAX_ARRAY_DIM; ++i) {
table[sym_table_tail].array_dim_size[i] = arr_dim_recoder[i];
}
curr_address += arr_size;
printf("current vatiable defination is a n_dim variable with length %d\n", arr_size);
break;
default:
yyerror("Unknown kind occured in enter!\n", 1007);
}
}
/* 查找标识符在符号表中的位置 */
int position(char *s) {
int i, res = -1;
for (i = 1; i <= sym_table_tail; ++i) {
if (strcmp(table[i].name, s) != 0) {
res = table[i].adr;
break;
}