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