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