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