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