-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgen_compiler.c
1182 lines (1001 loc) · 29.6 KB
/
gen_compiler.c
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
/*
This file is part of Hyacc, a LR(0)/LALR(1)/LR(1)/LR(k) parser generator.
Copyright (C) 2007, 2008, 2009 Xin Chen. [email protected]
Hyacc is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
Hyacc is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Hyacc; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
/*
* gen_compiler.c
*
* Contains functions to generate a compiler.
*
* @Author: Xin Chen
* @Date started: 10/16/2006
* @Last modified: 3/21/2007
*/
#include "y.h"
#include "lane_tracing.h"
extern char * y_tab_c;
extern char * y_tab_h;
static FILE * fp_yacc; // for yacc input file.
static FILE * fp; // pointer to output file y.tab.c
static FILE * fp_h; // for y.tab.h
static int n_line; // count number of lines in yacc input file.
static int n_col;
#define MAX_RULE_LENGTH 0xfffff
char *yystype_definition = "typedef int YYSTYPE;";
static char *yystype_format =
"#if ! defined YYSTYPE && ! defined YYSTYPE_IS_DECLARED\n"
"%s\n"
"#define YYSTYPE_IS_DECLARED 1\n"
"#endif\n"
;
extern void addCharToSymbol(int c);
extern char * ysymbol; // token symbol.
extern int ysymbol_pt;
extern int ysymbol_size;
static void prepare_outfile()
{
if ((fp = fopen(y_tab_c, "w")) == NULL) {
printf("Cannot open output file %s\n", y_tab_c);
exit(0);
}
if (USE_HEADER_FILE == FALSE) return;
if ((fp_h = fopen(y_tab_h, "w")) == NULL) {
printf("Cannot open output file %s\n", y_tab_h);
fclose(fp);
exit(0);
}
}
static void my_perror(char * msg, int c)
{
printf("\nerror [line %d, col %d]: invalid char '%c'. %s\n",
n_line, n_col, c, msg);
exit(1);
}
#define print_break() { fprintf(fp, "break;\n"); }
/*
* Token - terminal symbols.
*/
void writeTokens()
{
int i;
SymbolNode * a;
for (a = tokens, i = 0; a != NULL; a = a->next, i ++) {
printf("token %d: %s\n", i + 1, a->snode->symbol);
}
}
/*
* Write all terminal tokens that are not quoted, and not "error".
*/
void writeTokensToCompilerFile()
{
int i, index = 0;
SymbolNode * a;
fprintf(fp, "\n/* tokens */\n\n");
if (USE_HEADER_FILE) fprintf(fp_h, "\n/* tokens */\n\n");
for (a = tokens, i = 0; a != NULL; a = a->next, i ++) {
if (a->snode->TP->is_quoted == TRUE ||
strcmp(a->snode->symbol, strError) == 0) continue;
fprintf(fp, "#define %s %d\n", a->snode->symbol, index + 257);
if (USE_HEADER_FILE)
fprintf(fp_h, "#define %s %d\n", a->snode->symbol, index + 257);
index ++;
}
fprintf(fp, yystype_format, yystype_definition);
if (USE_HEADER_FILE) {
fprintf(fp_h, yystype_format, yystype_definition);
fprintf(fp_h, "\nextern YYSTYPE yylval;\n");
}
}
/*
* Get code declarations from section 1, write to
* y.tab.c, and write token declarations too.
*/
void processYaccFile_section1()
{
int c, last_c = '\n', last_last_c;
BOOL IS_CODE = FALSE;
n_line = n_col = 1;
while ((c = getc(fp_yacc)) != EOF) {
if (last_c == '\n' && c == '%') {
IS_CODE = FALSE;
} else if (last_last_c == '\n' && last_c == '%') {
if (c == '%') {
break; // end of section1.
} else if (c == '{') {
IS_CODE = TRUE;
} else if (c == '}') {
IS_CODE = FALSE;
}
} else if (IS_CODE == TRUE) {
putc(c, fp); // write to output file.
}
last_last_c = last_c;
last_c = c;
n_col ++;
if (c == '\n') { n_line ++; n_col = 1; }
}
//writeTokens();
writeTokensToCompilerFile();
}
/*
* rewind to section 2.
*/
static void goto_section2()
{
int c, last_c = 0, last_last_c = '\n';
fseek(fp_yacc, 0L, 0); // go to the beginning of file fp.
n_line = 1;
while ((c = getc(fp_yacc)) != EOF) {
if (c == '\n') n_line ++;
if (last_last_c == '\n' &&
last_c == '%' && c == '%') break; // end section 2.
last_last_c = last_c;
last_c = c;
}
}
/*
* Pass section 2, go to section 3.
* Presumption: finished section 1, entering section 2.
*/
static void goto_section3()
{
int c, last_c = 0, last_last_c = '\n';
while ((c = getc(fp_yacc)) != EOF) {
if (c == '\n') n_line ++;
if (last_last_c == '\n' && last_c == '%' && c == '%') {
break; // end section 2.
}
last_last_c = last_c;
last_c = c;
}
}
static Production *find_full_rule(int rule_count)
{
Production *rule;
SymbolNode *node;
SymbolTblNode *sym;
int full_rule;
for (full_rule = rule_count; full_rule < grammar.rule_count; ++full_rule) {
if ((rule = grammar.rules[full_rule]) &&
(node = rule->nLHS) &&
(sym = node->snode)) {
if (strncmp("$$", sym->symbol, 2)) /* node symbol starting with $$ we continue */
break;
} else {
printf("Malformed grammar rule at index %d\n", full_rule);
exit(1);
}
}
return rule;
}
static int find_mid_prod_index(Production *rule, Production *mid_prod_rule)
{
SymbolNode *lnode = mid_prod_rule->nLHS;
SymbolTblNode *lsym = lnode->snode;
char *l = lsym->symbol;
SymbolNode *rnode;
SymbolTblNode *rsym;
char *r;
int i;
for (i = 0, rnode = rule->nRHS_head; rnode; ++i, rnode = rnode->next)
if (!((rsym = rnode->snode) &&
(r = rsym->symbol))) {
printf("Did not find mid production rule of %s", l);
exit(1);
} else if (strcmp(l, r) == 0)
return i;
return -1;
}
static SymbolTblNode *find_sym(Production *rule, int dollar_number)
{
SymbolNode *node;
SymbolTblNode *sym;
int i;
if (dollar_number == MAX_RULE_LENGTH)
return sym;
for (i = 1, node = rule->nRHS_head; i < dollar_number && node; ++i, node = node->next);
if (i != dollar_number) {
printf("Rule terminated before %d RHS\n", dollar_number);
exit(1);
}
if (!(sym = node->snode)) {
printf("Malformed grammar rule RHS %d had no symbol table node\n", dollar_number);
exit(1);
}
return sym;
}
/*
* Basically, this has the same structure as function
* processYaccFileInput_section2(int c)
* in parsetYaccInput.c.
*
* The purpose here is to extract the code for semantic
* actions of rules.
*/
static void processYaccFile_section2(char * filename)
{
YACC_STATE state = LHS;
int CODE_LEVEL;
BOOL READING_SYMBOL = FALSE;
BOOL READING_NUMBER = FALSE;
BOOL READING_TYPE = FALSE;
int dollar_number = 0;
int c, last_c = 0, last_last_c = 0;
int rule_count = 0;
BOOL END_OF_CODE = FALSE; // for mid-production action.
char *explicit_type = 0;
Production *rule;
SymbolTblNode *sym;
char *token_type;
static char * padding = " ";
while ((c = getc(fp_yacc)) != EOF) {
if (last_c == '%' && c == '%') break; // end section 2.
switch (state) {
case LHS:
if (isspace(c) && READING_SYMBOL == FALSE) {
// do nothing, skip space.
} else if (c == ':') {
READING_SYMBOL = FALSE;
state = RHS;
} else if (isspace(c)) { // finish reading symbol
READING_SYMBOL = FALSE;
state = COLON;
} else if (last_c == '/' && c =='*') {
state = LHS_COMMENT;
if (READING_SYMBOL == TRUE) {
READING_SYMBOL = FALSE;
rule_count --;
}
} else if (c == '/') { // do nothing.
} else if (c == ';') { // do nothing.
} else if (! isspace(c)) {
if (READING_SYMBOL == FALSE) {
rule_count ++;
READING_SYMBOL = TRUE;
}
}
break;
case LHS_COMMENT:
if (last_c == '*' && c == '/') {
state = LHS;
}
break;
case COLON:
if (c == ':') {
state = RHS;
} else if (c == '/') {
// do nothing
} else if (last_c == '/' && c == '*') {
state = COLON_COMMENT;
} else if (! isspace(c)) {
my_perror("error: state COLON. ", c);
}
break;
case COLON_COMMENT:
if (last_c == '*' && c == '/') {
state = COLON;
}
break;
case RHS:
if (isspace(c)) {
// end of a symbol. do nothing.
} else if (c == '\'') {
state = TERMINAL;
if (END_OF_CODE == TRUE) {
rule_count ++;
print_break();
END_OF_CODE = FALSE;
}
} else if (c == ';') {
state = LHS; // end of a rule.
if (END_OF_CODE == TRUE) { print_break(); }
END_OF_CODE = FALSE;
} else if (c == '|') { // end of a rule
rule_count ++;
if (END_OF_CODE == TRUE) { print_break(); }
END_OF_CODE = FALSE;
} else if (c == '{') { // code for rule #rule_count
state = CODE;
CODE_LEVEL = 1;
if (END_OF_CODE == FALSE) {
fprintf(fp, "\n%s case %d:\n", padding, rule_count);
if (USE_LINES)
fprintf(fp, "# line %d \"%s\"\n", n_line, filename);
}
putc('{', fp);
} else if (last_c == '/' && c == '*') {
state = COMMENT;
} else if (last_c == '/' && c == '/') {
state = COMMENT2;
} else if (c == '/') {
// do nothing
} else if (c == ':') {
my_perror("You may miss a ';' in the last rule.", c);
} else {
//reading a symbol. do nothing
if (END_OF_CODE == TRUE) {
rule_count ++;
print_break();
END_OF_CODE = FALSE;
}
}
break;
case TERMINAL:
// avoid '\'' and '\\'.
if (c == '\'' && (last_c != '\\' || last_last_c == '\\')) {
state = RHS;
} else if (! isspace(c)) {
// reading a symbol. do nothing.
}
break;
case CODE: // the meat. write code to yacc output file.
if (READING_TYPE) {
if (c == '>') {
READING_TYPE = FALSE;
c = '$';
addCharToSymbol('\0');
explicit_type = ysymbol;
} else {
addCharToSymbol(c);
}
} else if (last_c == '$' && c == '<') {
ysymbol_pt = 0;
READING_TYPE = TRUE;
} else if (last_c != '$' && c == '$') {
// do nothing, this may be a special character.
} else if (last_c == '$' && c == '$') {
if (explicit_type) {
token_type = explicit_type;
explicit_type = 0;
} else {
rule = find_full_rule(rule_count);
token_type = rule->nLHS->snode->token_type;
}
if (token_type)
fprintf(fp, "(yyval.%s)", token_type);
else
fprintf(fp, "yyval");
} else if (last_c == '$' && isdigit(c)) {
READING_NUMBER = TRUE;
dollar_number = (c - 48) + 10 * dollar_number;
} else if (READING_NUMBER == TRUE && isdigit(c)) {
dollar_number = (c - 48) + 10 * dollar_number;
} else if (READING_NUMBER == TRUE && ! isdigit(c)) {
Production *start_rule = grammar.rules[rule_count];
int RHS_index;
rule = find_full_rule(rule_count);
if (rule != start_rule)
RHS_index = find_mid_prod_index(rule, start_rule);
else
RHS_index = rule->RHS_count;
if (explicit_type) {
token_type = explicit_type;
explicit_type = 0;
} else {
sym = find_sym(rule, dollar_number);
token_type = sym->token_type;
}
if (token_type)
fprintf(fp, "(yypvt[%d].%s)/* %d %d */", (dollar_number - RHS_index), token_type, rule_count, RHS_index);
else
fprintf(fp, "yypvt[%d]/* %d %d */", dollar_number - RHS_index, rule_count, RHS_index);
putc(c, fp);
READING_NUMBER = FALSE;
dollar_number = 0;
} else {
putc(c, fp);
}
if (c == '\"') {
state = CODE_DOUBLE_QUOTE;
} else if (c == '\'') {
state = CODE_SINGLE_QUOTE;
} else if (c == '*' && last_c == '/') {
state = CODE_COMMENT;
} else if (c == '/' && last_c == '/') {
state = CODE_COMMENT2;
} else if (c == '}' && CODE_LEVEL == 1) {
fprintf(fp, " "); // print_break();
state = RHS;
//!!
END_OF_CODE = TRUE; // end of a section of code.
} else if (c == '{') {
CODE_LEVEL ++;
} else if (c == '}') {
CODE_LEVEL --;
} else {
// do nothing.
}
break;
case CODE_DOUBLE_QUOTE:
putc(c, fp);
if (c == '\"' && last_c != '\\') state = CODE;
break;
case CODE_SINGLE_QUOTE:
putc(c, fp);
if (c == '\'') state = CODE;
break;
case CODE_COMMENT:
putc(c, fp);
if (c == '/' && last_c == '*') state = CODE;
break;
case CODE_COMMENT2:
putc(c, fp);
if (c == '\n') state = CODE;
break;
case COMMENT:
if (last_c == '*' && c =='/') state = RHS;
break;
case COMMENT2:
if (last_c == '\n') state = RHS;
break;
default:
break;
} // end switch.
//putc(c, stdout);
last_last_c = last_c;
last_c = c;
n_col ++;
if (c == '\n') { n_line ++; n_col = 1; }
}
}
void processYaccFile_section3()
{
int c;
while((c = getc(fp_yacc)) != EOF) {
putc(c, fp);
}
}
/*
* Copy code from resource files into output file.
*/
void copy_src_file(char * filename)
{
int c;
FILE * fp_src;
if ((fp_src = fopen(filename, "r")) == NULL) {
printf("error: can't open file %s\n", filename);
exit(1);
}
while((c = getc(fp_src)) != EOF) putc(c, fp);
fclose(fp_src);
}
/*
* This function will return the position of $A
* to the end of yaccpar.
* This is for the purpose of inserting code
* associated with reductions.
*/
void copy_yaccpar_file_1(char * filename)
{
int c, last_c = 0;
FILE * fp_src;
if ((fp_src = fopen(filename, "r")) == NULL) {
printf("error: can't open file %s\n", filename);
exit(1);
}
while((c = getc(fp_src)) != EOF) {
if ((last_c == '$' && c == 'A')) break;
putc(c, fp);
last_c = c;
}
fseek(fp, -1L, SEEK_CUR); // write head reverse 1 byte to remove '$'.
fclose(fp_src);
}
void copy_yaccpar_file_2(char * filename)
{
int c, last_c = 0;
FILE * fp_src;
if ((fp_src = fopen(filename, "r")) == NULL) {
printf("error: can't open file %s\n", filename);
exit(1);
}
while((c = getc(fp_src)) != EOF) {
if (last_c == '$' && c == 'A') break;
last_c = c;
}
while((c = getc(fp_src)) != EOF) putc(c, fp);
fclose(fp_src);
}
///////////////////////////////////////////////////////
// Functions to print parsing table arrays. START.
///////////////////////////////////////////////////////
static char * indent = " "; // indentation.
int getIndexInTokensArray(SymbolTblNode * s)
{
SymbolNode * a;
int i;
for (a = tokens, i = 0; a != NULL; a = a->next, i ++) {
if (s == a->snode) return i;
}
return -1;
}
/*
* Returns the index of the given symbol in the
* non-terminal array of the given grammar.
* Used in gen_compiler.c.
*/
int getNonTerminalIndex(SymbolTblNode * snode)
{
SymbolNode * a;
int i = 0;
for (a = grammar.non_terminal_list; a != NULL; a = a->next) {
if (snode == a->snode) return i;
i ++;
}
//printf("getNonTerminalIndex warning: ");
//printf("%s not found!\n", symbol);
return -1;
}
/*
* yyr1[i] represents index of non-terminal symbol
* on the LHS of reduction i, or a terminal symbol
* if use unit-production-removal and in step 3
* the LHS are replaced with leaf terminals in the
* multi-rooted trees.
*/
void print_yyr1()
{
int i;
fprintf(fp, "static YYCONST yytabelem yyr1[] = {\n");
// First rule is always "$accept : ...".
fprintf(fp, "%6d,", 0);
if (USE_REMOVE_UNIT_PRODUCTION) {
int index;
for (i = 1; i < grammar.rule_count; i ++) {
//printf("rule %d lhs: %s\n", i, grammar.rules[i]->LHS);
index = grammar.rules[i]->nLHS->snode->value;
fprintf(fp, "%6d", index);
if (i < grammar.rule_count - 1) fprintf(fp, ",");
if ((i - 9) % 10 == 0) fprintf(fp, "\n");
}
fprintf(fp, "};\n");
return;
}
for (i = 1; i < grammar.rule_count; i ++) {
fprintf(fp, "%6d", (-1) * getNonTerminalIndex(
grammar.rules[i]->nLHS->snode));
if (i < grammar.rule_count - 1) fprintf(fp, ",");
if ((i - 9) % 10 == 0) fprintf(fp, "\n");
}
fprintf(fp, "};\n");
}
/*
* yyr2[0] is a dummy field, and is alwasy 0.
* for i >= 1, yyr2[i] defines the following for grammar rule i:
* let x be the number of symbols on the RHS of rule i,
* let y indicate whether there is any code associated
* with this rule (y = 1 for yes, y = 0 for no).
* then yyr2[i] = (x << 1) + y;
*/
void print_yyr2()
{
int i;
fprintf(fp, "static YYCONST yytabelem yyr2[] = {\n");
fprintf(fp, "%6d,", 0);
for (i = 1; i < grammar.rule_count; i ++) {
fprintf(fp, "%6d", (grammar.rules[i]->RHS_count << 1) +
grammar.rules[i]->hasCode);
if (i < grammar.rule_count - 1) fprintf(fp, ",");
if ( (i - 9) % 10 == 0) fprintf(fp, "\n");
}
fprintf(fp, "};\n");
}
void print_yynonterminals()
{
int i;
SymbolNode * a;
fprintf(fp, "yytoktype yynts[] = {\n");
i = 1; // ignore first nonterminal: $accept.
for (a = grammar.non_terminal_list->next; a != NULL; a = a->next) {
fprintf(fp, "\t\"%s\",\t-%d,\n", a->snode->symbol, i);
i ++;
}
fprintf(fp, "\t\"-unknown-\", 1 /* ends search */\n");
fprintf(fp, "};\n");
}
/*
* Print terminal tokens.
*/
void print_yytoks()
{
int i, index;
SymbolNode * a;
index = 0;
fprintf(fp, "yytoktype yytoks[] = {\n");
for (a = tokens, i = 0; a != NULL; a = a->next, i ++) {
if (strcmp(a->snode->symbol, strError) == 0) continue;
if (strlen(a->snode->symbol) == 2 &&
a->snode->symbol[0] == '\\') { // escape sequence
fprintf(fp, "\t\"\\\\%s\",\t%d,\n", a->snode->symbol,
a->snode->value);
} else {
fprintf(fp, "\t\"%s\",\t%d,\n", a->snode->symbol,
a->snode->value);
}
}
fprintf(fp, "\t\"-unknown-\", -1 /* ends search */\n");
fprintf(fp, "};\n");
}
/*
* Print reductions.
*/
void print_yyreds()
{
int i, j;
char * symbol;
SymbolNode * a;
fprintf(fp, "char * yyreds[] = {\n");
fprintf(fp, "\t\"-no such reduction-\"\n");
for (i = 1; i < grammar.rule_count; i ++) {
fprintf(fp, "\t\"%s : ", grammar.rules[i]->nLHS->snode->symbol);
a = grammar.rules[i]->nRHS_head;
for (j = 0; j < grammar.rules[i]->RHS_count; j ++) {
if (j > 0) fprintf(fp, " ");
if (j > 0) a = a->next;
symbol = a->snode->symbol;
if (strlen(symbol) == 1 ||
(strlen(symbol) == 2 && symbol[0] == '\\') ) {
fprintf(fp, "'%s'", symbol);
} else {
fprintf(fp, "%s", symbol);
}
}
fprintf(fp, "\", \n");
}
fprintf(fp, "};\n");
}
void print_yytoken()
{
int i;
SymbolNode * a;
fprintf(fp, "int yytoken[] = {\n");
for (a = tokens, i = 0; a != NULL; a = a->next, i ++) {
fprintf(fp, "\t\"%s\",\t%d,\n", a->snode->symbol, 257 + i);
}
fprintf(fp, "\t\"-unknown-\", -1 /* ends search */\n");
fprintf(fp, "};\n");
}
void print_parsing_tbl_entry(char action, int state_no, int * count)
{
BOOL isEntry = FALSE;
if (action == 's' || action == 'g') {
fprintf(fp, "%d, ", state_no); // state to go.
isEntry = TRUE;
} else if (action == 'r') {
fprintf(fp, "-%d, ", state_no); // no. of reduction.
isEntry = TRUE;
} else if (action == 'a') {
fprintf(fp, "0, ");
isEntry = TRUE;
}
if (isEntry) {
(* count) ++;
if ( (* count) % 10 == 0 && (* count) != 0) fprintf(fp, "\n");
}
}
/*
* For the actions in a parsing table.
* if an action yyptblact[i] is positive, it's a shift/goto;
* if it is negative, it's a reduce;
* if it's zero, it's accept.
*/
void print_parsing_tbl()
{
int i, j, row, col;
int col_size = ParsingTblCols;
char action;
int state_no;
int * rowoffset = (int *) malloc(sizeof(int) * ParsingTblRows);
int rowoffset_pt = 0;
int count = 0;
SymbolTblNode * n;
fprintf(fp, "static YYCONST yytabelem yyptblact[] = {\n");
if (USE_REMOVE_UNIT_PRODUCTION) {
i = 0;
for (row = 0; row < ParsingTblRows; row ++) {
if (isReachableState(row)) {
#if USE_REM_FINAL_STATE
if (final_state_list[row] < 0) {
print_parsing_tbl_entry('s', final_state_list[row], &count);
* (rowoffset + rowoffset_pt) = count;
rowoffset_pt ++;
continue;
}
#endif
for (col = 0; col < ParsingTblCols; col ++) {
n = ParsingTblColHdr[col];
if (isGoalSymbol(n) == FALSE && isParentSymbol(n) == FALSE) {
getAction(n->type, col, row, & action, & state_no);
if (action == 's' || action == 'g')
state_no = getActualState(state_no);
//printf("%c%d\t", action, state_no);
print_parsing_tbl_entry(action, state_no, &count);
} // end of if.
}
* (rowoffset + rowoffset_pt) = count;
rowoffset_pt ++;
//printf("\n");
} // end of if.
}
} else {
for (i = 0; i < ParsingTblRows; i ++) {
#if USE_REM_FINAL_STATE
if (final_state_list[i] < 0) {
print_parsing_tbl_entry('s', final_state_list[i], &count);
* (rowoffset + rowoffset_pt) = count;
rowoffset_pt ++;
continue;
}
#endif
for (j = 0; j < ParsingTblCols; j ++) {
getAction(ParsingTblColHdr[j]->type,
j, i, & action, & state_no);
//printf("%c%d, ", action, state_no);
print_parsing_tbl_entry(action, state_no, &count);
}
* (rowoffset + rowoffset_pt) = count;
rowoffset_pt ++;
//printf("\n");
} // end of for.
}
fprintf(fp, "-10000000};\n\n"); // -10000000 is space filler
fprintf(fp, "static YYCONST yytabelem yyrowoffset[] = {\n0, ");
for (i = 0; i < rowoffset_pt; i ++) {
fprintf(fp, "%d", *(rowoffset + i));
if (i < rowoffset_pt - 1) fprintf(fp, ", ");
if (i % 10 == 0 && i != 0) fprintf(fp, "\n");
}
fprintf(fp, "};\n\n"); // NOTE: the last entry is (yyptbl.size - 1).
}
void print_parsing_tbl_col_entry(
char action, int token_value, int * count)
{
BOOL isEntry = FALSE;
if (action == 's' || action == 'g' || action == 'r' || action == 'a') {
fprintf(fp, "%d, ", token_value); // state to go.
isEntry = TRUE;
}
if (isEntry) {
(* count) ++;
if ( (* count) % 10 == 0 && (* count) != 0) fprintf(fp, "\n");
}
}
/*
* For the tokens upon which action are taken.
* If it's between 0 - 255, it's an ascii char;
* if it's 256, it's 'error';
* if it's > 256, it's a token;
* if it's < 0, it's a non-terminal.
*/
void print_parsing_tbl_col()
{
int i, j, row, col, count = 0;
int col_size = ParsingTblCols;
char action;
int state;
SymbolTblNode * n;
fprintf(fp, "static YYCONST yytabelem yyptbltok[] = {\n");
if (USE_REMOVE_UNIT_PRODUCTION) {
i = 0;
for (row = 0; row < ParsingTblRows; row ++) {
if (isReachableState(row)) {
#if USE_REM_FINAL_STATE
if (final_state_list[row] < 0) {
print_parsing_tbl_col_entry('r', -10000001, &count);
continue;
}
#endif
for (col = 0; col < ParsingTblCols; col ++) {
n = ParsingTblColHdr[col];
if (isGoalSymbol(n) == FALSE && isParentSymbol(n) == FALSE) {
getAction(n->type, col, row, & action, & state);
print_parsing_tbl_col_entry(action, n->value, &count);
} // end of if.
} // end of for.
} // end of if.
}
} else {
for (i = 0; i < ParsingTblRows; i ++) {
#if USE_REM_FINAL_STATE
if (final_state_list[i] < 0) { // is a final state.
// -10000001 labels a final state's col entry
print_parsing_tbl_col_entry('r', -10000001, &count);
continue;
}
#endif
for (j = 0; j < ParsingTblCols; j ++) {
n = ParsingTblColHdr[j];
getAction(n->type, j, i, & action, & state);
print_parsing_tbl_col_entry(action, n->value, &count);
}
} // end of for.
}
fprintf(fp, "-10000000};\n\n"); // -10000000 is space filler
}
/*
* Find those states that only have a single reduce action.
* Refer: Pager July, 72', Tech Rpt PE 259. Measure 3.
*/
void getFinalStates()
{
int i, j;
fprintf(fp, "static YYCONST yytabelem yyfs[] = {\n");
fprintf(fp, "%d", final_state_list[0]);
j = 0;
for (i = 1; i < ParsingTblRows; i ++) {
if (USE_REMOVE_UNIT_PRODUCTION) {
if (isReachableState(i) == FALSE) continue;
}
fprintf(fp, ", ");
if ((++ j) % 10 == 0) fprintf(fp, "\n");
fprintf(fp, "%d", final_state_list[i]);
}
fprintf(fp, "};\n\n");
}
BOOL useLRK()
{
return USE_LR_K == TRUE &&
(lrk_pt_array != NULL && lrk_pt_array->max_k >= 2);
}
void write_LRK_table_arrays()
{
int i, j;
LRk_P_T * t;
LRk_P_T_row * r;
fprintf(fp, "/*\n * For LR(k) parsing tables.\n */\n");