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