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