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