]> git.saurik.com Git - bison.git/blob - src/output.c
* src/gram.h (rprec, rprecsym, rassoc): Remove, now part of...
[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 output_short_table (&table_obstack,
379 "YYRLINE[YYN] -- source line where rule number YYN was defined",
380 "yyrline", rline,
381 0, 1, nrules + 1);
382
383 obstack_sgrow (&table_obstack, "#endif\n\n");
384
385 if (token_table_flag || no_parser_flag)
386 {
387 obstack_fgrow1 (&table_obstack, "#define YYNTOKENS %d\n", ntokens);
388 obstack_fgrow1 (&table_obstack, "#define YYNNTS %d\n", nvars);
389 obstack_fgrow1 (&table_obstack, "#define YYNRULES %d\n", nrules);
390 obstack_fgrow1 (&table_obstack, "#define YYNSTATES %d\n", nstates);
391 obstack_fgrow1 (&table_obstack, "#define YYMAXUTOK %d\n\n",
392 max_user_token_number);
393 }
394
395 /* Output the table of symbol names. */
396 if (!token_table_flag && !no_parser_flag)
397 obstack_sgrow (&table_obstack,
398 "\n#if YYDEBUG != 0 || defined YYERROR_VERBOSE\n\n");
399 obstack_sgrow (&table_obstack, "\
400 /* YYTNAME[TOKEN_NUM] -- String name of the token TOKEN_NUM. */\n");
401 obstack_sgrow (&table_obstack,
402 "static const char *const yytname[] =\n{\n ");
403
404 j = 0;
405 for (i = 0; i < nsyms; i++)
406 /* this used to be i<=nsyms, but that output a final "" symbol
407 almost by accident */
408 {
409 /* Width of the next token, including the two quotes, the coma
410 and the space. */
411 int strsize = 4;
412 char *p;
413
414 for (p = tags[i]; p && *p; p++)
415 if (*p == '"' || *p == '\\' || *p == '\n' || *p == '\t'
416 || *p == '\b')
417 strsize += 2;
418 else if (*p < 040 || *p >= 0177)
419 strsize += 4;
420 else
421 strsize++;
422
423 if (j + strsize > 75)
424 {
425 obstack_sgrow (&table_obstack, "\n ");
426 j = 2;
427 }
428
429 obstack_1grow (&table_obstack, '\"');
430 for (p = tags[i]; p && *p; p++)
431 {
432 if (*p == '"' || *p == '\\')
433 obstack_fgrow1 (&table_obstack, "\\%c", *p);
434 else if (*p == '\n')
435 obstack_sgrow (&table_obstack, "\\n");
436 else if (*p == '\t')
437 obstack_sgrow (&table_obstack, "\\t");
438 else if (*p == '\b')
439 obstack_sgrow (&table_obstack, "\\b");
440 else if (*p < 040 || *p >= 0177)
441 obstack_fgrow1 (&table_obstack, "\\%03o", *p);
442 else
443 obstack_1grow (&table_obstack, *p);
444 }
445
446 obstack_sgrow (&table_obstack, "\", ");
447 j += strsize;
448 }
449 /* add a NULL entry to list of tokens */
450 obstack_sgrow (&table_obstack, "NULL\n};\n");
451
452 if (!token_table_flag && !no_parser_flag)
453 obstack_sgrow (&table_obstack, "#endif\n\n");
454
455 /* Output YYTOKNUM. */
456 if (token_table_flag)
457 {
458 output_short_table (&table_obstack,
459 "YYTOKNUM[YYLEX] -- Index in YYTNAME corresponding to YYLEX",
460 "yytoknum", user_toknums,
461 0, 1, ntokens + 1);
462 }
463
464 /* Output YYR1. */
465 {
466 short *values = XCALLOC (short, nrules + 1);
467 for (i = 0; i < nrules + 1; ++i)
468 values[i] = rule_table[i].lhs;
469 output_short_table (&table_obstack,
470 "YYR1[YYN] -- Symbol number of symbol that rule YYN derives",
471 "yyr1", values,
472 0, 1, nrules + 1);
473 XFREE (values);
474 }
475
476 obstack_1grow (&table_obstack, '\n');
477
478 /* Output YYR2. */
479 short_tab = XMALLOC (short, nrules + 1);
480 for (i = 1; i < nrules; i++)
481 short_tab[i] = rule_table[i + 1].rhs - rule_table[i].rhs - 1;
482 short_tab[nrules] = nitems - rule_table[nrules].rhs - 1;
483 output_short_table (&table_obstack,
484 "YYR2[YYN] -- Number of symbols composing right hand side of rule YYN",
485 "yyr2", short_tab,
486 0, 1, nrules + 1);
487 obstack_1grow (&table_obstack, '\n');
488
489 XFREE (short_tab);
490
491 XFREE (rule_table + 1);
492 }
493
494
495 static void
496 output_defines (void)
497 {
498 obstack_fgrow1 (&table_obstack, "\n\n#define\tYYFINAL\t\t%d\n", final_state);
499 obstack_fgrow1 (&table_obstack, "#define\tYYFLAG\t\t%d\n", MINSHORT);
500 obstack_fgrow1 (&table_obstack, "#define\tYYNTBASE\t%d\n", ntokens);
501 }
502
503
504 /*------------------------------------------------------------------.
505 | Decide what to do for each type of token if seen as the lookahead |
506 | token in specified state. The value returned is used as the |
507 | default action (yydefact) for the state. In addition, actrow is |
508 | filled with what to do for each kind of token, index by symbol |
509 | number, with zero meaning do the default action. The value |
510 | MINSHORT, a very negative number, means this situation is an |
511 | error. The parser recognizes this value specially. |
512 | |
513 | This is where conflicts are resolved. The loop over lookahead |
514 | rules considered lower-numbered rules last, and the last rule |
515 | considered that likes a token gets to handle it. |
516 `------------------------------------------------------------------*/
517
518 static int
519 action_row (int state)
520 {
521 int i;
522 int j;
523 int k;
524 int m = 0;
525 int n = 0;
526 int count;
527 int default_rule;
528 int nreds;
529 int max;
530 int rule;
531 int shift_state;
532 int symbol;
533 unsigned mask;
534 unsigned *wordp;
535 reductions *redp;
536 shifts *shiftp;
537 errs *errp;
538 int nodefault = 0; /* set nonzero to inhibit having any default reduction */
539
540 for (i = 0; i < ntokens; i++)
541 actrow[i] = 0;
542
543 default_rule = 0;
544 nreds = 0;
545 redp = state_table[state].reduction_table;
546
547 if (redp)
548 {
549 nreds = redp->nreds;
550
551 if (nreds >= 1)
552 {
553 /* loop over all the rules available here which require
554 lookahead */
555 m = state_table[state].lookaheads;
556 n = state_table[state + 1].lookaheads;
557
558 for (i = n - 1; i >= m; i--)
559 {
560 rule = -LAruleno[i];
561 wordp = LA (i);
562 mask = 1;
563
564 /* and find each token which the rule finds acceptable
565 to come next */
566 for (j = 0; j < ntokens; j++)
567 {
568 /* and record this rule as the rule to use if that
569 token follows. */
570 if (mask & *wordp)
571 actrow[j] = rule;
572
573 mask <<= 1;
574 if (mask == 0)
575 {
576 mask = 1;
577 wordp++;
578 }
579 }
580 }
581 }
582 }
583
584 shiftp = state_table[state].shift_table;
585
586 /* Now see which tokens are allowed for shifts in this state. For
587 them, record the shift as the thing to do. So shift is preferred
588 to reduce. */
589
590 if (shiftp)
591 {
592 k = shiftp->nshifts;
593
594 for (i = 0; i < k; i++)
595 {
596 shift_state = shiftp->shifts[i];
597 if (!shift_state)
598 continue;
599
600 symbol = state_table[shift_state].accessing_symbol;
601
602 if (ISVAR (symbol))
603 break;
604
605 actrow[symbol] = shift_state;
606
607 /* Do not use any default reduction if there is a shift for
608 error */
609 if (symbol == error_token_number)
610 nodefault = 1;
611 }
612 }
613
614 errp = err_table[state];
615
616 /* See which tokens are an explicit error in this state (due to
617 %nonassoc). For them, record MINSHORT as the action. */
618
619 if (errp)
620 {
621 k = errp->nerrs;
622
623 for (i = 0; i < k; i++)
624 {
625 symbol = errp->errs[i];
626 actrow[symbol] = MINSHORT;
627 }
628 }
629
630 /* Now find the most common reduction and make it the default action
631 for this state. */
632
633 if (nreds >= 1 && !nodefault)
634 {
635 if (state_table[state].consistent)
636 default_rule = redp->rules[0];
637 else
638 {
639 max = 0;
640 for (i = m; i < n; i++)
641 {
642 count = 0;
643 rule = -LAruleno[i];
644
645 for (j = 0; j < ntokens; j++)
646 {
647 if (actrow[j] == rule)
648 count++;
649 }
650
651 if (count > max)
652 {
653 max = count;
654 default_rule = rule;
655 }
656 }
657
658 /* actions which match the default are replaced with zero,
659 which means "use the default" */
660
661 if (max > 0)
662 {
663 for (j = 0; j < ntokens; j++)
664 {
665 if (actrow[j] == default_rule)
666 actrow[j] = 0;
667 }
668
669 default_rule = -default_rule;
670 }
671 }
672 }
673
674 /* If have no default rule, the default is an error.
675 So replace any action which says "error" with "use default". */
676
677 if (default_rule == 0)
678 for (j = 0; j < ntokens; j++)
679 {
680 if (actrow[j] == MINSHORT)
681 actrow[j] = 0;
682 }
683
684 return default_rule;
685 }
686
687
688 static void
689 save_row (int state)
690 {
691 int i;
692 int count;
693 short *sp;
694 short *sp1;
695 short *sp2;
696
697 count = 0;
698 for (i = 0; i < ntokens; i++)
699 {
700 if (actrow[i] != 0)
701 count++;
702 }
703
704 if (count == 0)
705 return;
706
707 froms[state] = sp1 = sp = XCALLOC (short, count);
708 tos[state] = sp2 = XCALLOC (short, count);
709
710 for (i = 0; i < ntokens; i++)
711 {
712 if (actrow[i] != 0)
713 {
714 *sp1++ = i;
715 *sp2++ = actrow[i];
716 }
717 }
718
719 tally[state] = count;
720 width[state] = sp1[-1] - sp[0] + 1;
721 }
722
723
724 /*------------------------------------------------------------------.
725 | Figure out the actions for the specified state, indexed by |
726 | lookahead token type. |
727 | |
728 | The YYDEFACT table is output now. The detailed info is saved for |
729 | putting into YYTABLE later. |
730 `------------------------------------------------------------------*/
731
732 static void
733 token_actions (void)
734 {
735 int i;
736 short *yydefact = XCALLOC (short, nstates);
737
738 actrow = XCALLOC (short, ntokens);
739 for (i = 0; i < nstates; ++i)
740 {
741 yydefact[i] = action_row (i);
742 save_row (i);
743 }
744 XFREE (actrow);
745
746 output_short_table (&table_obstack,
747 "YYDEFACT[S] -- default rule to reduce with in state S when YYTABLE\n\
748 doesn't specify something else to do. Zero means the default is an\n\
749 error",
750 "yydefact", yydefact,
751 yydefact[0], 1, nstates);
752 obstack_1grow (&table_obstack, '\n');
753 XFREE (yydefact);
754 }
755
756
757 static void
758 free_shifts (void)
759 {
760 shifts *sp, *sptmp; /* JF derefrenced freed ptr */
761
762 for (sp = first_shift; sp; sp = sptmp)
763 {
764 sptmp = sp->next;
765 XFREE (sp);
766 }
767 }
768
769
770 static void
771 free_reductions (void)
772 {
773 reductions *rp, *rptmp; /* JF fixed freed ptr */
774
775 for (rp = first_reduction; rp; rp = rptmp)
776 {
777 rptmp = rp->next;
778 XFREE (rp);
779 }
780 }
781
782
783
784 static void
785 save_column (int symbol, int default_state)
786 {
787 int i;
788 short *sp;
789 short *sp1;
790 short *sp2;
791 int count;
792 int symno;
793
794 short begin = goto_map[symbol];
795 short end = goto_map[symbol + 1];
796
797 count = 0;
798 for (i = begin; i < end; i++)
799 {
800 if (to_state[i] != default_state)
801 count++;
802 }
803
804 if (count == 0)
805 return;
806
807 symno = symbol - ntokens + nstates;
808
809 froms[symno] = sp1 = sp = XCALLOC (short, count);
810 tos[symno] = sp2 = XCALLOC (short, count);
811
812 for (i = begin; i < end; i++)
813 {
814 if (to_state[i] != default_state)
815 {
816 *sp1++ = from_state[i];
817 *sp2++ = to_state[i];
818 }
819 }
820
821 tally[symno] = count;
822 width[symno] = sp1[-1] - sp[0] + 1;
823 }
824
825 static int
826 default_goto (int symbol)
827 {
828 int i;
829 int m;
830 int n;
831 int default_state;
832 int max;
833
834 m = goto_map[symbol];
835 n = goto_map[symbol + 1];
836
837 if (m == n)
838 return -1;
839
840 for (i = 0; i < nstates; i++)
841 state_count[i] = 0;
842
843 for (i = m; i < n; i++)
844 state_count[to_state[i]]++;
845
846 max = 0;
847 default_state = -1;
848
849 for (i = 0; i < nstates; i++)
850 {
851 if (state_count[i] > max)
852 {
853 max = state_count[i];
854 default_state = i;
855 }
856 }
857
858 return default_state;
859 }
860
861
862 /*-------------------------------------------------------------------.
863 | Figure out what to do after reducing with each rule, depending on |
864 | the saved state from before the beginning of parsing the data that |
865 | matched this rule. |
866 | |
867 | The YYDEFGOTO table is output now. The detailed info is saved for |
868 | putting into YYTABLE later. |
869 `-------------------------------------------------------------------*/
870
871 static void
872 goto_actions (void)
873 {
874 int i;
875
876 short *yydefgoto = XMALLOC (short, nsyms - ntokens);
877 state_count = XCALLOC (short, nstates);
878
879 for (i = ntokens; i < nsyms; ++i)
880 {
881 int default_state = default_goto (i);
882 save_column (i, default_state);
883 yydefgoto[i - ntokens] = default_state;
884 }
885
886 output_short_table (&table_obstack, NULL, "yydefgoto", yydefgoto,
887 yydefgoto[0], 1, nsyms - ntokens);
888
889 XFREE (state_count);
890 XFREE (yydefgoto);
891 }
892
893
894 /* The next few functions decide how to pack the actions and gotos
895 information into yytable. */
896
897 static void
898 sort_actions (void)
899 {
900 int i;
901 int j;
902 int k;
903 int t;
904 int w;
905
906 order = XCALLOC (short, nvectors);
907 nentries = 0;
908
909 for (i = 0; i < nvectors; i++)
910 {
911 if (tally[i] > 0)
912 {
913 t = tally[i];
914 w = width[i];
915 j = nentries - 1;
916
917 while (j >= 0 && (width[order[j]] < w))
918 j--;
919
920 while (j >= 0 && (width[order[j]] == w) && (tally[order[j]] < t))
921 j--;
922
923 for (k = nentries - 1; k > j; k--)
924 order[k + 1] = order[k];
925
926 order[j + 1] = i;
927 nentries++;
928 }
929 }
930 }
931
932
933 static int
934 matching_state (int vector)
935 {
936 int i;
937 int j;
938 int k;
939 int t;
940 int w;
941 int match;
942 int prev;
943
944 i = order[vector];
945 if (i >= nstates)
946 return -1;
947
948 t = tally[i];
949 w = width[i];
950
951 for (prev = vector - 1; prev >= 0; prev--)
952 {
953 j = order[prev];
954 if (width[j] != w || tally[j] != t)
955 return -1;
956
957 match = 1;
958 for (k = 0; match && k < t; k++)
959 {
960 if (tos[j][k] != tos[i][k] || froms[j][k] != froms[i][k])
961 match = 0;
962 }
963
964 if (match)
965 return j;
966 }
967
968 return -1;
969 }
970
971
972 static int
973 pack_vector (int vector)
974 {
975 int i;
976 int j;
977 int k;
978 int t;
979 int loc = 0;
980 int ok;
981 short *from;
982 short *to;
983
984 i = order[vector];
985 t = tally[i];
986
987 assert (t);
988
989 from = froms[i];
990 to = tos[i];
991
992 for (j = lowzero - from[0]; j < MAXTABLE; j++)
993 {
994 ok = 1;
995
996 for (k = 0; ok && k < t; k++)
997 {
998 loc = j + from[k];
999 if (loc > MAXTABLE)
1000 fatal (_("maximum table size (%d) exceeded"), MAXTABLE);
1001
1002 if (table[loc] != 0)
1003 ok = 0;
1004 }
1005
1006 for (k = 0; ok && k < vector; k++)
1007 {
1008 if (pos[k] == j)
1009 ok = 0;
1010 }
1011
1012 if (ok)
1013 {
1014 for (k = 0; k < t; k++)
1015 {
1016 loc = j + from[k];
1017 table[loc] = to[k];
1018 check[loc] = from[k];
1019 }
1020
1021 while (table[lowzero] != 0)
1022 lowzero++;
1023
1024 if (loc > high)
1025 high = loc;
1026
1027 return j;
1028 }
1029 }
1030
1031 berror ("pack_vector");
1032 return 0; /* JF keep lint happy */
1033 }
1034
1035
1036 static void
1037 pack_table (void)
1038 {
1039 int i;
1040 int place;
1041 int state;
1042
1043 base = XCALLOC (short, nvectors);
1044 pos = XCALLOC (short, nentries);
1045 table = XCALLOC (short, MAXTABLE);
1046 check = XCALLOC (short, MAXTABLE);
1047
1048 lowzero = 0;
1049 high = 0;
1050
1051 for (i = 0; i < nvectors; i++)
1052 base[i] = MINSHORT;
1053
1054 for (i = 0; i < MAXTABLE; i++)
1055 check[i] = -1;
1056
1057 for (i = 0; i < nentries; i++)
1058 {
1059 state = matching_state (i);
1060
1061 if (state < 0)
1062 place = pack_vector (i);
1063 else
1064 place = base[state];
1065
1066 pos[i] = place;
1067 base[order[i]] = place;
1068 }
1069
1070 for (i = 0; i < nvectors; i++)
1071 {
1072 if (froms[i])
1073 XFREE (froms[i]);
1074 if (tos[i])
1075 XFREE (tos[i]);
1076 }
1077
1078 XFREE (froms);
1079 XFREE (tos);
1080 XFREE (pos);
1081 }
1082
1083 /* the following functions output yytable, yycheck
1084 and the vectors whose elements index the portion starts */
1085
1086 static void
1087 output_base (void)
1088 {
1089 output_short_table (&table_obstack, NULL, "yypact", base,
1090 base[0], 1, nstates);
1091
1092 obstack_1grow (&table_obstack, '\n');
1093
1094 output_short_table (&table_obstack, NULL, "yypgoto", base,
1095 base[nstates], nstates + 1, nvectors);
1096
1097 XFREE (base);
1098 }
1099
1100
1101 static void
1102 output_table (void)
1103 {
1104 obstack_fgrow1 (&table_obstack, "\n\n#define\tYYLAST\t\t%d\n\n\n", high);
1105 output_short_table (&table_obstack, NULL, "yytable", table,
1106 table[0], 1, high + 1);
1107 XFREE (table);
1108 }
1109
1110
1111 static void
1112 output_check (void)
1113 {
1114 output_short_table (&table_obstack, NULL, "yycheck", check,
1115 check[0], 1, high + 1);
1116 XFREE (check);
1117 }
1118
1119 /* compute and output yydefact, yydefgoto, yypact, yypgoto, yytable
1120 and yycheck. */
1121
1122 static void
1123 output_actions (void)
1124 {
1125 nvectors = nstates + nvars;
1126
1127 froms = XCALLOC (short *, nvectors);
1128 tos = XCALLOC (short *, nvectors);
1129 tally = XCALLOC (short, nvectors);
1130 width = XCALLOC (short, nvectors);
1131
1132 token_actions ();
1133 free_shifts ();
1134 free_reductions ();
1135 XFREE (LA);
1136 XFREE (LAruleno);
1137
1138 goto_actions ();
1139 XFREE (goto_map + ntokens);
1140 XFREE (from_state);
1141 XFREE (to_state);
1142
1143 sort_actions ();
1144 pack_table ();
1145 obstack_1grow (&table_obstack, '\n');
1146 output_base ();
1147 output_table ();
1148 obstack_1grow (&table_obstack, '\n');
1149 output_check ();
1150 }
1151
1152 /*------------------------------------------.
1153 | Copy the parser code into TABLE_OBSTACK. |
1154 `------------------------------------------*/
1155
1156 static void
1157 output_parser (void)
1158 {
1159 int c;
1160 FILE *fskel;
1161 size_t line;
1162 int actions_dumped = 0;
1163
1164 if (pure_parser)
1165 obstack_sgrow (&table_obstack, "#define YYPURE 1\n\n");
1166
1167 /* Loop over lines in the standard parser file. */
1168 if (!skeleton)
1169 {
1170 if (semantic_parser)
1171 skeleton = skeleton_find ("BISON_HAIRY", BISON_HAIRY);
1172 else
1173 skeleton = skeleton_find ("BISON_SIMPLE", BISON_SIMPLE);
1174 }
1175 assert (skeleton);
1176 fskel = xfopen (skeleton, "r");
1177
1178 /* Set LINE to 2, not 1: `#line LINENUM' -- Here LINENUM is a
1179 decimal integer constant. This specifies that the line number of
1180 the *following* line of input, in its original source file, was
1181 LINENUM. */
1182 line = 2;
1183
1184 while (1)
1185 {
1186 enum line_type_e
1187 {
1188 regular_line,
1189 sync_line, /* #line. */
1190 actions_line /* %% actions. */
1191 };
1192 enum line_type_e line_type = regular_line;
1193
1194 c = getc (fskel);
1195
1196 /* Is this line special? */
1197 if (c == '#')
1198 {
1199 /* See if it's a `#line' line. */
1200 if ((c = getc (fskel)) == 'l')
1201 if ((c = getc (fskel)) == 'i')
1202 if ((c = getc (fskel)) == 'n')
1203 if ((c = getc (fskel)) == 'e')
1204 line_type = sync_line;
1205 else
1206 obstack_sgrow (&table_obstack, "#lin");
1207 else
1208 obstack_sgrow (&table_obstack, "#li");
1209 else
1210 obstack_sgrow (&table_obstack, "#l");
1211 else
1212 obstack_sgrow (&table_obstack, "#");
1213 }
1214 else if (c == '%')
1215 {
1216 /* See if it's a `%% actions' line. */
1217 if ((c = getc (fskel)) == '%')
1218 if ((c = getc (fskel)) == ' ')
1219 if ((c = getc (fskel)) == 'a')
1220 if ((c = getc (fskel)) == 'c')
1221 if ((c = getc (fskel)) == 't')
1222 if ((c = getc (fskel)) == 'i')
1223 if ((c = getc (fskel)) == 'o')
1224 if ((c = getc (fskel)) == 'n')
1225 if ((c = getc (fskel)) == 's')
1226 line_type = actions_line;
1227 else
1228 obstack_sgrow (&table_obstack, "%% action");
1229 else
1230 obstack_sgrow (&table_obstack, "%% actio");
1231 else
1232 obstack_sgrow (&table_obstack, "%% acti");
1233 else
1234 obstack_sgrow (&table_obstack, "%% act");
1235 else
1236 obstack_sgrow (&table_obstack, "%% ac");
1237 else
1238 obstack_sgrow (&table_obstack, "%% a");
1239 else
1240 obstack_sgrow (&table_obstack, "%% ");
1241 else
1242 obstack_sgrow (&table_obstack, "%%");
1243 else
1244 obstack_sgrow (&table_obstack, "%");
1245 }
1246
1247 switch (line_type)
1248 {
1249 case sync_line:
1250 if (!no_lines_flag)
1251 obstack_fgrow2 (&table_obstack, "#line %d %s\n",
1252 line, quotearg_style (c_quoting_style, skeleton));
1253
1254 /* Skip the end of line. */
1255 for (; c != '\n' && c != EOF; c = getc (fskel))
1256 /* nothing */;
1257 break;
1258
1259 case actions_line:
1260 {
1261 size_t size = obstack_object_size (&action_obstack);
1262
1263 actions_dumped++;
1264 assert (actions_dumped == 1);
1265 obstack_grow (&table_obstack,
1266 obstack_finish (&action_obstack),
1267 size);
1268 }
1269
1270 /* Skip the end of line. */
1271 for (; c != '\n' && c != EOF; c = getc (fskel))
1272 /* nothing */;
1273 break;
1274
1275 case regular_line:
1276 for (; c != '\n' && c != EOF; c = getc (fskel))
1277 obstack_1grow (&table_obstack, c);
1278 }
1279
1280 if (c == EOF)
1281 break;
1282 obstack_1grow (&table_obstack, c);
1283 line++;
1284 }
1285 assert (actions_dumped == 1);
1286 xfclose (fskel);
1287 }
1288
1289 static void
1290 output_program (void)
1291 {
1292 int c;
1293
1294 if (!no_lines_flag)
1295 obstack_fgrow2 (&table_obstack, "#line %d %s\n",
1296 lineno, quotearg_style (c_quoting_style, infile));
1297
1298 while ((c = getc (finput)) != EOF)
1299 obstack_1grow (&table_obstack, c);
1300 }
1301
1302
1303 static void
1304 free_itemsets (void)
1305 {
1306 core *cp, *cptmp;
1307 for (cp = first_state; cp; cp = cptmp)
1308 {
1309 cptmp = cp->next;
1310 XFREE (cp);
1311 }
1312 }
1313
1314
1315 /*----------------------------------------------------------.
1316 | Output the parsing tables and the parser code to ftable. |
1317 `----------------------------------------------------------*/
1318
1319 void
1320 output (void)
1321 {
1322 /* output_token_defines(ftable); / * JF put out token defines FIRST */
1323
1324 /* If using a simple parser the definition of YYSTYPE are put into
1325 TABLE_OBSTACK. */
1326 if (!semantic_parser)
1327 {
1328 size_t size = obstack_object_size (&attrs_obstack);
1329 obstack_grow (&table_obstack, obstack_finish (&attrs_obstack), size);
1330 }
1331 reader_output_yylsp (&table_obstack);
1332 if (debug_flag)
1333 obstack_sgrow (&table_obstack, "\
1334 #ifndef YYDEBUG\n\
1335 # define YYDEBUG 1\n\
1336 #endif\n\
1337 \n");
1338
1339 if (semantic_parser)
1340 obstack_fgrow1 (&table_obstack, "#include %s\n",
1341 quotearg_style (c_quoting_style, attrsfile));
1342
1343 if (!no_parser_flag)
1344 obstack_sgrow (&table_obstack, "#include <stdio.h>\n\n");
1345
1346 free_itemsets ();
1347 output_defines ();
1348 output_token_translations ();
1349 /* if (semantic_parser) */
1350 /* This is now unconditional because debugging printouts can use it. */
1351 output_gram ();
1352 XFREE (ritem);
1353 if (semantic_parser)
1354 output_stos ();
1355 output_rule_data ();
1356 output_actions ();
1357 XFREE (state_table);
1358
1359 if (!no_parser_flag)
1360 output_parser ();
1361 output_program ();
1362 }