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