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