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