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