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