]> git.saurik.com Git - bison.git/blob - src/output.c
f7e218801f844286049aafadb7112a82fca764dd
[bison.git] / src / output.c
1 /* Output the generated parsing program for bison,
2 Copyright (C) 1984, 1986, 1989, 1992, 2000 Free Software Foundation, Inc.
3
4 This file is part of Bison, the GNU Compiler Compiler.
5
6 Bison is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 Bison is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with Bison; see the file COPYING. If not, write to the Free
18 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
19 02111-1307, USA. */
20
21
22 /* functions to output parsing data to various files. Entries are:
23
24 output_headers ()
25
26 Output constant strings to the beginning of certain files.
27
28 output_trailers()
29
30 Output constant strings to the ends of certain files.
31
32 output ()
33
34 Output the parsing tables and the parser code to ftable.
35
36 The parser tables consist of these tables.
37 Starred ones needed only for the semantic parser.
38 Double starred are output only if switches are set.
39
40 yytranslate = vector mapping yylex's token numbers into bison's token numbers.
41
42 ** yytname = vector of string-names indexed by bison token number
43
44 ** yytoknum = vector of yylex token numbers corresponding to entries in yytname
45
46 yyrline = vector of line-numbers of all rules. For yydebug printouts.
47
48 yyrhs = vector of items of all rules.
49 This is exactly what ritems contains. For yydebug and for semantic
50 parser.
51
52 yyprhs[r] = index in yyrhs of first item for rule r.
53
54 yyr1[r] = symbol number of symbol that rule r derives.
55
56 yyr2[r] = number of symbols composing right hand side of rule r.
57
58 * yystos[s] = the symbol number of the symbol that leads to state s.
59
60 yydefact[s] = default rule to reduce with in state s,
61 when yytable doesn't specify something else to do.
62 Zero means the default is an error.
63
64 yydefgoto[i] = default state to go to after a reduction of a rule that
65 generates variable ntokens + i, except when yytable
66 specifies something else to do.
67
68 yypact[s] = index in yytable of the portion describing state s.
69 The lookahead token's type is used to index that portion
70 to find out what to do.
71
72 If the value in yytable is positive,
73 we shift the token and go to that state.
74
75 If the value is negative, it is minus a rule number to reduce by.
76
77 If the value is zero, the default action from yydefact[s] is used.
78
79 yypgoto[i] = the index in yytable of the portion describing
80 what to do after reducing a rule that derives variable i + ntokens.
81 This portion is indexed by the parser state number, s,
82 as of before the text for this nonterminal was read.
83 The value from yytable is the state to go to if
84 the corresponding value in yycheck is s.
85
86 yytable = a vector filled with portions for different uses,
87 found via yypact and yypgoto.
88
89 yycheck = a vector indexed in parallel with yytable.
90 It indicates, in a roundabout way, the bounds of the
91 portion you are trying to examine.
92
93 Suppose that the portion of yytable starts at index p
94 and the index to be examined within the portion is i.
95 Then if yycheck[p+i] != i, i is outside the bounds
96 of what is actually allocated, and the default
97 (from yydefact or yydefgoto) should be used.
98 Otherwise, yytable[p+i] should be used.
99
100 YYFINAL = the state number of the termination state.
101 YYFLAG = most negative short int. Used to flag ??
102 YYNTBASE = ntokens.
103
104 */
105
106 #include <stdio.h>
107 #include "system.h"
108 #include "machine.h"
109 #include "alloc.h"
110 #include "files.h"
111 #include "gram.h"
112 #include "state.h"
113 #include "complain.h"
114
115
116 extern int debugflag;
117 extern int nolinesflag;
118 extern int noparserflag;
119 extern int toknumflag;
120
121 extern char **tags;
122 extern int *user_toknums;
123 extern int tokensetsize;
124 extern int final_state;
125 extern core **state_table;
126 extern shifts **shift_table;
127 extern errs **err_table;
128 extern reductions **reduction_table;
129 extern short *accessing_symbol;
130 extern unsigned *LA;
131 extern short *LAruleno;
132 extern short *lookaheads;
133 extern char *consistent;
134 extern short *goto_map;
135 extern short *from_state;
136 extern short *to_state;
137
138 void output_headers PARAMS((void));
139 void output_trailers PARAMS((void));
140 void output PARAMS((void));
141 void output_token_translations PARAMS((void));
142 void output_gram PARAMS((void));
143 void output_stos PARAMS((void));
144 void output_rule_data PARAMS((void));
145 void output_defines PARAMS((void));
146 void output_actions PARAMS((void));
147 void token_actions PARAMS((void));
148 void save_row PARAMS((int));
149 void goto_actions PARAMS((void));
150 void save_column PARAMS((int, int));
151 void sort_actions PARAMS((void));
152 void pack_table PARAMS((void));
153 void output_base PARAMS((void));
154 void output_table PARAMS((void));
155 void output_check PARAMS((void));
156 void output_parser PARAMS((void));
157 void output_program PARAMS((void));
158 void free_shifts PARAMS((void));
159 void free_reductions PARAMS((void));
160 void free_itemsets PARAMS((void));
161 int action_row PARAMS((int));
162 int default_goto PARAMS((int));
163 int matching_state PARAMS((int));
164 int pack_vector PARAMS((int));
165
166 extern void berror PARAMS((char *));
167 extern void reader_output_yylsp PARAMS((FILE *));
168
169 static int nvectors;
170 static int nentries;
171 static short **froms;
172 static short **tos;
173 static short *tally;
174 static short *width;
175 static short *actrow;
176 static short *state_count;
177 static short *order;
178 static short *base;
179 static short *pos;
180 static short *table;
181 static short *check;
182 static int lowzero;
183 static int high;
184
185
186
187 #define GUARDSTR "\n#include \"%s\"\nextern int yyerror;\n\
188 extern int yycost;\nextern char * yymsg;\nextern YYSTYPE yyval;\n\n\
189 yyguard(n, yyvsp, yylsp)\nregister int n;\nregister YYSTYPE *yyvsp;\n\
190 register YYLTYPE *yylsp;\n\
191 {\n yyerror = 0;\nyycost = 0;\n yymsg = 0;\nswitch (n)\n {"
192
193 #define ACTSTR "\n#include \"%s\"\nextern YYSTYPE yyval;\
194 \nextern int yychar;\
195 yyaction(n, yyvsp, yylsp)\nregister int n;\nregister YYSTYPE *yyvsp;\n\
196 register YYLTYPE *yylsp;\n{\n switch (n)\n{"
197
198 #define ACTSTR_SIMPLE "\n switch (yyn) {\n"
199
200
201 void
202 output_headers (void)
203 {
204 if (semantic_parser)
205 fprintf(fguard, GUARDSTR, attrsfile);
206
207 if (noparserflag)
208 return;
209
210 fprintf(faction, (semantic_parser ? ACTSTR : ACTSTR_SIMPLE), attrsfile);
211 /* if (semantic_parser) JF moved this below
212 fprintf(ftable, "#include \"%s\"\n", attrsfile);
213 fprintf(ftable, "#include <stdio.h>\n\n");
214 */
215
216 /* Rename certain symbols if -p was specified. */
217 if (spec_name_prefix)
218 {
219 fprintf(ftable, "#define yyparse %sparse\n", spec_name_prefix);
220 fprintf(ftable, "#define yylex %slex\n", spec_name_prefix);
221 fprintf(ftable, "#define yyerror %serror\n", spec_name_prefix);
222 fprintf(ftable, "#define yylval %slval\n", spec_name_prefix);
223 fprintf(ftable, "#define yychar %schar\n", spec_name_prefix);
224 fprintf(ftable, "#define yydebug %sdebug\n", spec_name_prefix);
225 fprintf(ftable, "#define yynerrs %snerrs\n", spec_name_prefix);
226 }
227 }
228
229
230 void
231 output_trailers (void)
232 {
233 if (semantic_parser)
234 fprintf(fguard, "\n }\n}\n");
235
236 fprintf(faction, "\n");
237
238 if (noparserflag)
239 return;
240
241 if (semantic_parser)
242 fprintf(faction, " }\n");
243 fprintf(faction, "}\n");
244 }
245
246
247 void
248 output (void)
249 {
250 int c;
251
252 /* output_token_defines(ftable); / * JF put out token defines FIRST */
253 if (!semantic_parser) /* JF Put out other stuff */
254 {
255 rewind(fattrs);
256 while ((c=getc(fattrs))!=EOF)
257 putc(c,ftable);
258 }
259 reader_output_yylsp(ftable);
260 if (debugflag)
261 fprintf(ftable, "#ifndef YYDEBUG\n#define YYDEBUG %d\n#endif\n\n",
262 !!debugflag);
263
264 if (semantic_parser)
265 fprintf(ftable, "#include \"%s\"\n", attrsfile);
266
267 if (! noparserflag)
268 fprintf(ftable, "#include <stdio.h>\n\n");
269
270 /* Make "const" do nothing if not in ANSI C. */
271 fprintf (ftable, "#ifndef __cplusplus\n#ifndef __STDC__\n#define const\n#endif\n#endif\n\n");
272
273 free_itemsets();
274 output_defines();
275 output_token_translations();
276 /* if (semantic_parser) */
277 /* This is now unconditional because debugging printouts can use it. */
278 output_gram();
279 FREE(ritem);
280 if (semantic_parser)
281 output_stos();
282 output_rule_data();
283 output_actions();
284 if (! noparserflag)
285 output_parser();
286 output_program();
287 }
288
289
290 void
291 output_token_translations (void)
292 {
293 register int i, j;
294 /* register short *sp; JF unused */
295
296 if (translations)
297 {
298 fprintf(ftable,
299 "\n#define YYTRANSLATE(x) ((unsigned)(x) <= %d ? yytranslate[x] : %d)\n",
300 max_user_token_number, nsyms);
301
302 if (ntokens < 127) /* play it very safe; check maximum element value. */
303 fprintf(ftable, "\nstatic const char yytranslate[] = { 0");
304 else
305 fprintf(ftable, "\nstatic const short yytranslate[] = { 0");
306
307 j = 10;
308 for (i = 1; i <= max_user_token_number; i++)
309 {
310 putc(',', ftable);
311
312 if (j >= 10)
313 {
314 putc('\n', ftable);
315 j = 1;
316 }
317 else
318 {
319 j++;
320 }
321
322 fprintf(ftable, "%6d", token_translations[i]);
323 }
324
325 fprintf(ftable, "\n};\n");
326 }
327 else
328 {
329 fprintf(ftable, "\n#define YYTRANSLATE(x) (x)\n");
330 }
331 }
332
333
334 void
335 output_gram (void)
336 {
337 register int i;
338 register int j;
339 register short *sp;
340
341 /* With the ordinary parser,
342 yyprhs and yyrhs are needed only for yydebug. */
343 /* With the noparser option, all tables are generated */
344 if (! semantic_parser && ! noparserflag)
345 fprintf(ftable, "\n#if YYDEBUG != 0");
346
347 fprintf(ftable, "\nstatic const short yyprhs[] = { 0");
348
349 j = 10;
350 for (i = 1; i <= nrules; i++)
351 {
352 putc(',', ftable);
353
354 if (j >= 10)
355 {
356 putc('\n', ftable);
357 j = 1;
358 }
359 else
360 {
361 j++;
362 }
363
364 fprintf(ftable, "%6d", rrhs[i]);
365 }
366
367 fprintf(ftable, "\n};\n");
368
369 fprintf(ftable, "\nstatic const short yyrhs[] = {%6d", ritem[0]);
370
371 j = 10;
372 for (sp = ritem + 1; *sp; sp++)
373 {
374 putc(',', ftable);
375
376 if (j >= 10)
377 {
378 putc('\n', ftable);
379 j = 1;
380 }
381 else
382 {
383 j++;
384 }
385
386 if (*sp > 0)
387 fprintf(ftable, "%6d", *sp);
388 else
389 fprintf(ftable, " 0");
390 }
391
392 fprintf(ftable, "\n};\n");
393
394 if (! semantic_parser && ! noparserflag)
395 fprintf(ftable, "\n#endif\n");
396 }
397
398
399 void
400 output_stos (void)
401 {
402 register int i;
403 register int j;
404
405 fprintf(ftable, "\nstatic const short yystos[] = { 0");
406
407 j = 10;
408 for (i = 1; i < nstates; i++)
409 {
410 putc(',', ftable);
411
412 if (j >= 10)
413 {
414 putc('\n', ftable);
415 j = 1;
416 }
417 else
418 {
419 j++;
420 }
421
422 fprintf(ftable, "%6d", accessing_symbol[i]);
423 }
424
425 fprintf(ftable, "\n};\n");
426 }
427
428
429 void
430 output_rule_data (void)
431 {
432 register int i;
433 register int j;
434
435 fputs ("\n\
436 #if YYDEBUG != 0\n\
437 /* YYRLINE[yyn]: source line where rule number YYN was defined. */\n\
438 static const short yyrline[] = { 0", ftable);
439
440 j = 10;
441 for (i = 1; i <= nrules; i++)
442 {
443 putc(',', ftable);
444
445 if (j >= 10)
446 {
447 putc('\n', ftable);
448 j = 1;
449 }
450 else
451 {
452 j++;
453 }
454
455 fprintf(ftable, "%6d", rline[i]);
456 }
457 fprintf(ftable, "\n};\n#endif\n\n");
458
459 if (toknumflag || noparserflag)
460 {
461 fprintf(ftable, "#define YYNTOKENS %d\n", ntokens);
462 fprintf(ftable, "#define YYNNTS %d\n", nvars);
463 fprintf(ftable, "#define YYNRULES %d\n", nrules);
464 fprintf(ftable, "#define YYNSTATES %d\n", nstates);
465 fprintf(ftable, "#define YYMAXUTOK %d\n\n", max_user_token_number);
466 }
467
468 if (! toknumflag && ! noparserflag)
469 fprintf(ftable, "\n#if YYDEBUG != 0 || defined (YYERROR_VERBOSE)\n\n");
470
471 /* Output the table of symbol names. */
472
473 fprintf(ftable,
474 "static const char * const yytname[] = { \"%s\"",
475 tags[0]);
476
477 j = strlen (tags[0]) + 44;
478 for (i = 1; i < nsyms; i++)
479 /* this used to be i<=nsyms, but that output a final "" symbol
480 almost by accident */
481 {
482 register char *p;
483 putc(',', ftable);
484 j++;
485
486 if (j > 75)
487 {
488 putc('\n', ftable);
489 j = 0;
490 }
491
492 putc ('\"', ftable);
493 j++;
494
495 for (p = tags[i]; p && *p; p++)
496 {
497 if (*p == '"' || *p == '\\')
498 {
499 fprintf(ftable, "\\%c", *p);
500 j += 2;
501 }
502 else if (*p == '\n')
503 {
504 fprintf(ftable, "\\n");
505 j += 2;
506 }
507 else if (*p == '\t')
508 {
509 fprintf(ftable, "\\t");
510 j += 2;
511 }
512 else if (*p == '\b')
513 {
514 fprintf(ftable, "\\b");
515 j += 2;
516 }
517 else if (*p < 040 || *p >= 0177)
518 {
519 fprintf(ftable, "\\%03o", *p);
520 j += 4;
521 }
522 else
523 {
524 putc(*p, ftable);
525 j++;
526 }
527 }
528
529 putc ('\"', ftable);
530 j++;
531 }
532 /* add a NULL entry to list of tokens */
533 fprintf (ftable, ", NULL\n};\n");
534
535 if (! toknumflag && ! noparserflag)
536 fprintf (ftable, "#endif\n\n");
537
538 /* Output YYTOKNUM. */
539 if (toknumflag)
540 {
541 fprintf(ftable, "static const short yytoknum[] = { 0");
542 j = 10;
543 for (i = 1; i <= ntokens; i++) {
544 putc(',', ftable);
545 if (j >= 10)
546 {
547 putc('\n', ftable);
548 j = 1;
549 }
550 else
551 j++;
552 fprintf(ftable, "%6d", user_toknums[i]);
553 }
554 fprintf(ftable, "\n};\n\n");
555 }
556
557 /* Output YYR1. */
558 fputs ("\
559 /* YYR1[YYN]: Symbol number of symbol that rule YYN derives. */\n\
560 static const short yyr1[] = { 0", ftable);
561
562 j = 10;
563 for (i = 1; i <= nrules; i++)
564 {
565 putc(',', ftable);
566
567 if (j >= 10)
568 {
569 putc('\n', ftable);
570 j = 1;
571 }
572 else
573 {
574 j++;
575 }
576
577 fprintf(ftable, "%6d", rlhs[i]);
578 }
579 FREE(rlhs + 1);
580 fputs ("\n\
581 };\n\
582 \n", ftable);
583
584 /* Output YYR2. */
585 fputs ("\
586 /* YYR2[YYN]: Number of symbols composing right hand side of rule YYN. */\n\
587 static const short yyr2[] = { 0", ftable);
588 j = 10;
589 for (i = 1; i < nrules; i++)
590 {
591 putc(',', ftable);
592
593 if (j >= 10)
594 {
595 putc('\n', ftable);
596 j = 1;
597 }
598 else
599 {
600 j++;
601 }
602
603 fprintf(ftable, "%6d", rrhs[i + 1] - rrhs[i] - 1);
604 }
605
606 putc(',', ftable);
607 if (j >= 10)
608 putc('\n', ftable);
609
610 fprintf(ftable, "%6d\n};\n", nitems - rrhs[nrules] - 1);
611 FREE(rrhs + 1);
612 }
613
614
615 void
616 output_defines (void)
617 {
618 fprintf(ftable, "\n\n#define\tYYFINAL\t\t%d\n", final_state);
619 fprintf(ftable, "#define\tYYFLAG\t\t%d\n", MINSHORT);
620 fprintf(ftable, "#define\tYYNTBASE\t%d\n", ntokens);
621 }
622
623
624
625 /* compute and output yydefact, yydefgoto, yypact, yypgoto, yytable and yycheck. */
626
627 void
628 output_actions (void)
629 {
630 nvectors = nstates + nvars;
631
632 froms = NEW2(nvectors, short *);
633 tos = NEW2(nvectors, short *);
634 tally = NEW2(nvectors, short);
635 width = NEW2(nvectors, short);
636
637 token_actions();
638 free_shifts();
639 free_reductions();
640 FREE(lookaheads);
641 FREE(LA);
642 FREE(LAruleno);
643 FREE(accessing_symbol);
644
645 goto_actions();
646 FREE(goto_map + ntokens);
647 FREE(from_state);
648 FREE(to_state);
649
650 sort_actions();
651 pack_table();
652 output_base();
653 output_table();
654 output_check();
655 }
656
657
658
659 /* figure out the actions for the specified state, indexed by lookahead token type.
660
661 The yydefact table is output now. The detailed info
662 is saved for putting into yytable later. */
663
664 void
665 token_actions (void)
666 {
667 register int i;
668 register int j;
669 register int k;
670
671 actrow = NEW2(ntokens, short);
672
673 k = action_row(0);
674 fprintf(ftable, "\nstatic const short yydefact[] = {%6d", k);
675 save_row(0);
676
677 j = 10;
678 for (i = 1; i < nstates; i++)
679 {
680 putc(',', ftable);
681
682 if (j >= 10)
683 {
684 putc('\n', ftable);
685 j = 1;
686 }
687 else
688 {
689 j++;
690 }
691
692 k = action_row(i);
693 fprintf(ftable, "%6d", k);
694 save_row(i);
695 }
696
697 fprintf(ftable, "\n};\n");
698 FREE(actrow);
699 }
700
701
702
703 /* Decide what to do for each type of token if seen as the lookahead token in specified state.
704 The value returned is used as the default action (yydefact) for the state.
705 In addition, actrow is filled with what to do for each kind of token,
706 index by symbol number, with zero meaning do the default action.
707 The value MINSHORT, a very negative number, means this situation
708 is an error. The parser recognizes this value specially.
709
710 This is where conflicts are resolved. The loop over lookahead rules
711 considered lower-numbered rules last, and the last rule considered that likes
712 a token gets to handle it. */
713
714 int
715 action_row (int state)
716 {
717 register int i;
718 register int j;
719 register int k;
720 register int m = 0;
721 register int n = 0;
722 register int count;
723 register int default_rule;
724 register int nreds;
725 register int max;
726 register int rule;
727 register int shift_state;
728 register int symbol;
729 register unsigned mask;
730 register unsigned *wordp;
731 register reductions *redp;
732 register shifts *shiftp;
733 register errs *errp;
734 int nodefault = 0; /* set nonzero to inhibit having any default reduction */
735
736 for (i = 0; i < ntokens; i++)
737 actrow[i] = 0;
738
739 default_rule = 0;
740 nreds = 0;
741 redp = reduction_table[state];
742
743 if (redp)
744 {
745 nreds = redp->nreds;
746
747 if (nreds >= 1)
748 {
749 /* loop over all the rules available here which require lookahead */
750 m = lookaheads[state];
751 n = lookaheads[state + 1];
752
753 for (i = n - 1; i >= m; i--)
754 {
755 rule = - LAruleno[i];
756 wordp = LA + i * tokensetsize;
757 mask = 1;
758
759 /* and find each token which the rule finds acceptable to come next */
760 for (j = 0; j < ntokens; j++)
761 {
762 /* and record this rule as the rule to use if that token follows. */
763 if (mask & *wordp)
764 actrow[j] = rule;
765
766 mask <<= 1;
767 if (mask == 0)
768 {
769 mask = 1;
770 wordp++;
771 }
772 }
773 }
774 }
775 }
776
777 shiftp = shift_table[state];
778
779 /* now see which tokens are allowed for shifts in this state.
780 For them, record the shift as the thing to do. So shift is preferred to reduce. */
781
782 if (shiftp)
783 {
784 k = shiftp->nshifts;
785
786 for (i = 0; i < k; i++)
787 {
788 shift_state = shiftp->shifts[i];
789 if (! shift_state) continue;
790
791 symbol = accessing_symbol[shift_state];
792
793 if (ISVAR(symbol))
794 break;
795
796 actrow[symbol] = shift_state;
797
798 /* do not use any default reduction if there is a shift for error */
799
800 if (symbol == error_token_number) nodefault = 1;
801 }
802 }
803
804 errp = err_table[state];
805
806 /* See which tokens are an explicit error in this state
807 (due to %nonassoc). For them, record MINSHORT as the action. */
808
809 if (errp)
810 {
811 k = errp->nerrs;
812
813 for (i = 0; i < k; i++)
814 {
815 symbol = errp->errs[i];
816 actrow[symbol] = MINSHORT;
817 }
818 }
819
820 /* now find the most common reduction and make it the default action for this state. */
821
822 if (nreds >= 1 && ! nodefault)
823 {
824 if (consistent[state])
825 default_rule = redp->rules[0];
826 else
827 {
828 max = 0;
829 for (i = m; i < n; i++)
830 {
831 count = 0;
832 rule = - LAruleno[i];
833
834 for (j = 0; j < ntokens; j++)
835 {
836 if (actrow[j] == rule)
837 count++;
838 }
839
840 if (count > max)
841 {
842 max = count;
843 default_rule = rule;
844 }
845 }
846
847 /* actions which match the default are replaced with zero,
848 which means "use the default" */
849
850 if (max > 0)
851 {
852 for (j = 0; j < ntokens; j++)
853 {
854 if (actrow[j] == default_rule)
855 actrow[j] = 0;
856 }
857
858 default_rule = - default_rule;
859 }
860 }
861 }
862
863 /* If have no default rule, the default is an error.
864 So replace any action which says "error" with "use default". */
865
866 if (default_rule == 0)
867 for (j = 0; j < ntokens; j++)
868 {
869 if (actrow[j] == MINSHORT)
870 actrow[j] = 0;
871 }
872
873 return (default_rule);
874 }
875
876
877 void
878 save_row (int state)
879 {
880 register int i;
881 register int count;
882 register short *sp;
883 register short *sp1;
884 register short *sp2;
885
886 count = 0;
887 for (i = 0; i < ntokens; i++)
888 {
889 if (actrow[i] != 0)
890 count++;
891 }
892
893 if (count == 0)
894 return;
895
896 froms[state] = sp1 = sp = NEW2(count, short);
897 tos[state] = sp2 = NEW2(count, short);
898
899 for (i = 0; i < ntokens; i++)
900 {
901 if (actrow[i] != 0)
902 {
903 *sp1++ = i;
904 *sp2++ = actrow[i];
905 }
906 }
907
908 tally[state] = count;
909 width[state] = sp1[-1] - sp[0] + 1;
910 }
911
912
913
914 /* figure out what to do after reducing with each rule,
915 depending on the saved state from before the beginning
916 of parsing the data that matched this rule.
917
918 The yydefgoto table is output now. The detailed info
919 is saved for putting into yytable later. */
920
921 void
922 goto_actions (void)
923 {
924 register int i;
925 register int j;
926 register int k;
927
928 state_count = NEW2(nstates, short);
929
930 k = default_goto(ntokens);
931 fprintf(ftable, "\nstatic const short yydefgoto[] = {%6d", k);
932 save_column(ntokens, k);
933
934 j = 10;
935 for (i = ntokens + 1; i < nsyms; i++)
936 {
937 putc(',', ftable);
938
939 if (j >= 10)
940 {
941 putc('\n', ftable);
942 j = 1;
943 }
944 else
945 {
946 j++;
947 }
948
949 k = default_goto(i);
950 fprintf(ftable, "%6d", k);
951 save_column(i, k);
952 }
953
954 fprintf(ftable, "\n};\n");
955 FREE(state_count);
956 }
957
958
959
960 int
961 default_goto (int symbol)
962 {
963 register int i;
964 register int m;
965 register int n;
966 register int default_state;
967 register int max;
968
969 m = goto_map[symbol];
970 n = goto_map[symbol + 1];
971
972 if (m == n)
973 return (-1);
974
975 for (i = 0; i < nstates; i++)
976 state_count[i] = 0;
977
978 for (i = m; i < n; i++)
979 state_count[to_state[i]]++;
980
981 max = 0;
982 default_state = -1;
983
984 for (i = 0; i < nstates; i++)
985 {
986 if (state_count[i] > max)
987 {
988 max = state_count[i];
989 default_state = i;
990 }
991 }
992
993 return (default_state);
994 }
995
996
997 void
998 save_column (int symbol, int default_state)
999 {
1000 register int i;
1001 register int m;
1002 register int n;
1003 register short *sp;
1004 register short *sp1;
1005 register short *sp2;
1006 register int count;
1007 register int symno;
1008
1009 m = goto_map[symbol];
1010 n = goto_map[symbol + 1];
1011
1012 count = 0;
1013 for (i = m; i < n; i++)
1014 {
1015 if (to_state[i] != default_state)
1016 count++;
1017 }
1018
1019 if (count == 0)
1020 return;
1021
1022 symno = symbol - ntokens + nstates;
1023
1024 froms[symno] = sp1 = sp = NEW2(count, short);
1025 tos[symno] = sp2 = NEW2(count, short);
1026
1027 for (i = m; i < n; i++)
1028 {
1029 if (to_state[i] != default_state)
1030 {
1031 *sp1++ = from_state[i];
1032 *sp2++ = to_state[i];
1033 }
1034 }
1035
1036 tally[symno] = count;
1037 width[symno] = sp1[-1] - sp[0] + 1;
1038 }
1039
1040
1041
1042 /* The next few functions decide how to pack the actions and gotos
1043 information into yytable. */
1044
1045 void
1046 sort_actions (void)
1047 {
1048 register int i;
1049 register int j;
1050 register int k;
1051 register int t;
1052 register int w;
1053
1054 order = NEW2(nvectors, short);
1055 nentries = 0;
1056
1057 for (i = 0; i < nvectors; i++)
1058 {
1059 if (tally[i] > 0)
1060 {
1061 t = tally[i];
1062 w = width[i];
1063 j = nentries - 1;
1064
1065 while (j >= 0 && (width[order[j]] < w))
1066 j--;
1067
1068 while (j >= 0 && (width[order[j]] == w) && (tally[order[j]] < t))
1069 j--;
1070
1071 for (k = nentries - 1; k > j; k--)
1072 order[k + 1] = order[k];
1073
1074 order[j + 1] = i;
1075 nentries++;
1076 }
1077 }
1078 }
1079
1080
1081 void
1082 pack_table (void)
1083 {
1084 register int i;
1085 register int place;
1086 register int state;
1087
1088 base = NEW2(nvectors, short);
1089 pos = NEW2(nentries, short);
1090 table = NEW2(MAXTABLE, short);
1091 check = NEW2(MAXTABLE, short);
1092
1093 lowzero = 0;
1094 high = 0;
1095
1096 for (i = 0; i < nvectors; i++)
1097 base[i] = MINSHORT;
1098
1099 for (i = 0; i < MAXTABLE; i++)
1100 check[i] = -1;
1101
1102 for (i = 0; i < nentries; i++)
1103 {
1104 state = matching_state(i);
1105
1106 if (state < 0)
1107 place = pack_vector(i);
1108 else
1109 place = base[state];
1110
1111 pos[i] = place;
1112 base[order[i]] = place;
1113 }
1114
1115 for (i = 0; i < nvectors; i++)
1116 {
1117 if (froms[i])
1118 FREE(froms[i]);
1119 if (tos[i])
1120 FREE(tos[i]);
1121 }
1122
1123 FREE(froms);
1124 FREE(tos);
1125 FREE(pos);
1126 }
1127
1128
1129
1130 int
1131 matching_state (int vector)
1132 {
1133 register int i;
1134 register int j;
1135 register int k;
1136 register int t;
1137 register int w;
1138 register int match;
1139 register int prev;
1140
1141 i = order[vector];
1142 if (i >= nstates)
1143 return (-1);
1144
1145 t = tally[i];
1146 w = width[i];
1147
1148 for (prev = vector - 1; prev >= 0; prev--)
1149 {
1150 j = order[prev];
1151 if (width[j] != w || tally[j] != t)
1152 return (-1);
1153
1154 match = 1;
1155 for (k = 0; match && k < t; k++)
1156 {
1157 if (tos[j][k] != tos[i][k] || froms[j][k] != froms[i][k])
1158 match = 0;
1159 }
1160
1161 if (match)
1162 return (j);
1163 }
1164
1165 return (-1);
1166 }
1167
1168
1169
1170 int
1171 pack_vector (int vector)
1172 {
1173 register int i;
1174 register int j;
1175 register int k;
1176 register int t;
1177 register int loc = 0;
1178 register int ok;
1179 register short *from;
1180 register short *to;
1181
1182 i = order[vector];
1183 t = tally[i];
1184
1185 if (t == 0)
1186 berror("pack_vector");
1187
1188 from = froms[i];
1189 to = tos[i];
1190
1191 for (j = lowzero - from[0]; j < MAXTABLE; j++)
1192 {
1193 ok = 1;
1194
1195 for (k = 0; ok && k < t; k++)
1196 {
1197 loc = j + from[k];
1198 if (loc > MAXTABLE)
1199 fatal (_("maximum table size (%d) exceeded"), MAXTABLE);
1200
1201 if (table[loc] != 0)
1202 ok = 0;
1203 }
1204
1205 for (k = 0; ok && k < vector; k++)
1206 {
1207 if (pos[k] == j)
1208 ok = 0;
1209 }
1210
1211 if (ok)
1212 {
1213 for (k = 0; k < t; k++)
1214 {
1215 loc = j + from[k];
1216 table[loc] = to[k];
1217 check[loc] = from[k];
1218 }
1219
1220 while (table[lowzero] != 0)
1221 lowzero++;
1222
1223 if (loc > high)
1224 high = loc;
1225
1226 return (j);
1227 }
1228 }
1229
1230 berror("pack_vector");
1231 return 0; /* JF keep lint happy */
1232 }
1233
1234
1235
1236 /* the following functions output yytable, yycheck
1237 and the vectors whose elements index the portion starts */
1238
1239 void
1240 output_base (void)
1241 {
1242 register int i;
1243 register int j;
1244
1245 fprintf(ftable, "\nstatic const short yypact[] = {%6d", base[0]);
1246
1247 j = 10;
1248 for (i = 1; i < nstates; i++)
1249 {
1250 putc(',', ftable);
1251
1252 if (j >= 10)
1253 {
1254 putc('\n', ftable);
1255 j = 1;
1256 }
1257 else
1258 {
1259 j++;
1260 }
1261
1262 fprintf(ftable, "%6d", base[i]);
1263 }
1264
1265 fprintf(ftable, "\n};\n\nstatic const short yypgoto[] = {%6d", base[nstates]);
1266
1267 j = 10;
1268 for (i = nstates + 1; i < nvectors; i++)
1269 {
1270 putc(',', ftable);
1271
1272 if (j >= 10)
1273 {
1274 putc('\n', ftable);
1275 j = 1;
1276 }
1277 else
1278 {
1279 j++;
1280 }
1281
1282 fprintf(ftable, "%6d", base[i]);
1283 }
1284
1285 fprintf(ftable, "\n};\n");
1286 FREE(base);
1287 }
1288
1289
1290 void
1291 output_table (void)
1292 {
1293 register int i;
1294 register int j;
1295
1296 fprintf(ftable, "\n\n#define\tYYLAST\t\t%d\n\n", high);
1297 fprintf(ftable, "\nstatic const short yytable[] = {%6d", table[0]);
1298
1299 j = 10;
1300 for (i = 1; i <= high; i++)
1301 {
1302 putc(',', ftable);
1303
1304 if (j >= 10)
1305 {
1306 putc('\n', ftable);
1307 j = 1;
1308 }
1309 else
1310 {
1311 j++;
1312 }
1313
1314 fprintf(ftable, "%6d", table[i]);
1315 }
1316
1317 fprintf(ftable, "\n};\n");
1318 FREE(table);
1319 }
1320
1321
1322 void
1323 output_check (void)
1324 {
1325 register int i;
1326 register int j;
1327
1328 fprintf(ftable, "\nstatic const short yycheck[] = {%6d", check[0]);
1329
1330 j = 10;
1331 for (i = 1; i <= high; i++)
1332 {
1333 putc(',', ftable);
1334
1335 if (j >= 10)
1336 {
1337 putc('\n', ftable);
1338 j = 1;
1339 }
1340 else
1341 {
1342 j++;
1343 }
1344
1345 fprintf(ftable, "%6d", check[i]);
1346 }
1347
1348 fprintf(ftable, "\n};\n");
1349 FREE(check);
1350 }
1351
1352
1353
1354 /* copy the parser code into the ftable file at the end. */
1355
1356 void
1357 output_parser (void)
1358 {
1359 register int c;
1360 #ifdef DONTDEF
1361 FILE *fpars;
1362 #else
1363 #define fpars fparser
1364 #endif
1365
1366 if (pure_parser)
1367 fprintf(ftable, "#define YYPURE 1\n\n");
1368
1369 #ifdef DONTDEF /* JF no longer needed 'cuz open_extra_files changes the
1370 currently open parser from bison.simple to bison.hairy */
1371 if (semantic_parser)
1372 fpars = fparser;
1373 else fpars = fparser1;
1374 #endif
1375
1376 /* Loop over lines in the standard parser file. */
1377
1378 while (1)
1379 {
1380 int write_line = 1;
1381
1382 c = getc(fpars);
1383
1384 /* See if the line starts with `#line.
1385 If so, set write_line to 0. */
1386 if (nolinesflag)
1387 if (c == '#')
1388 {
1389 c = getc(fpars);
1390 if (c == 'l')
1391 {
1392 c = getc(fpars);
1393 if (c == 'i')
1394 {
1395 c = getc(fpars);
1396 if (c == 'n')
1397 {
1398 c = getc(fpars);
1399 if (c == 'e')
1400 write_line = 0;
1401 else
1402 fprintf(ftable, "#lin");
1403 }
1404 else
1405 fprintf(ftable, "#li");
1406 }
1407 else
1408 fprintf(ftable, "#l");
1409 }
1410 else
1411 fprintf(ftable, "#");
1412 }
1413
1414 /* now write out the line... */
1415 for (; c != '\n' && c != EOF; c = getc(fpars))
1416 if (write_line) {
1417 if (c == '$')
1418 {
1419 /* `$' in the parser file indicates where to put the actions.
1420 Copy them in at this point. */
1421 rewind(faction);
1422 for(c=getc(faction);c!=EOF;c=getc(faction))
1423 putc(c,ftable);
1424 }
1425 else
1426 putc(c, ftable);
1427 }
1428 if (c == EOF)
1429 break;
1430 putc(c, ftable);
1431 }
1432 }
1433
1434 void
1435 output_program (void)
1436 {
1437 register int c;
1438
1439 if (!nolinesflag)
1440 fprintf(ftable, "#line %d \"%s\"\n", lineno, infile);
1441
1442 c = getc(finput);
1443 while (c != EOF)
1444 {
1445 putc(c, ftable);
1446 c = getc(finput);
1447 }
1448 }
1449
1450
1451 void
1452 free_itemsets (void)
1453 {
1454 register core *cp,*cptmp;
1455
1456 FREE(state_table);
1457
1458 for (cp = first_state; cp; cp = cptmp) {
1459 cptmp=cp->next;
1460 FREE(cp);
1461 }
1462 }
1463
1464
1465 void
1466 free_shifts (void)
1467 {
1468 register shifts *sp,*sptmp;/* JF derefrenced freed ptr */
1469
1470 FREE(shift_table);
1471
1472 for (sp = first_shift; sp; sp = sptmp) {
1473 sptmp=sp->next;
1474 FREE(sp);
1475 }
1476 }
1477
1478
1479 void
1480 free_reductions (void)
1481 {
1482 register reductions *rp,*rptmp;/* JF fixed freed ptr */
1483
1484 FREE(reduction_table);
1485
1486 for (rp = first_reduction; rp; rp = rptmp) {
1487 rptmp=rp->next;
1488 FREE(rp);
1489 }
1490 }