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