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