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