]> git.saurik.com Git - bison.git/blob - src/output.c
* src/gram.h, src/gram.c (error_token_number): Remove, use
[bison.git] / src / output.c
1 /* Output the generated parsing program for bison,
2 Copyright (C) 1984, 1986, 1989, 1992, 2000, 2001, 2002
3 Free Software Foundation, Inc.
4
5 This file is part of Bison, the GNU Compiler Compiler.
6
7 Bison is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 Bison is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Bison; see the file COPYING. If not, write to the Free
19 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA. */
21
22
23 /* The parser tables consist of these tables. Marked ones needed only
24 for the semantic parser. Double marked are output only if switches
25 are set.
26
27 YYTRANSLATE = vector mapping yylex's token numbers into bison's
28 token numbers.
29
30 ++ YYTNAME = vector of string-names indexed by bison token number.
31
32 ++ YYTOKNUM = vector of yylex token numbers corresponding to
33 entries in YYTNAME.
34
35 YYRLINE = vector of line-numbers of all rules. For yydebug
36 printouts.
37
38 YYRHS = vector of items of all rules. This is exactly what RITEMS
39 contains. For yydebug and for semantic parser.
40
41 YYPRHS[R] = index in YYRHS of first item for rule R.
42
43 YYR1[R] = symbol number of symbol that rule R derives.
44
45 YYR2[R] = number of symbols composing right hand side of rule R.
46
47 + YYSTOS[S] = the symbol number of the symbol that leads to state
48 S.
49
50 YYDEFACT[S] = default rule to reduce with in state s, when YYTABLE
51 doesn't specify something else to do. Zero means the default is an
52 error.
53
54 YYDEFGOTO[I] = default state to go to after a reduction of a rule
55 that generates variable NTOKENS + I, except when YYTABLE specifies
56 something else to do.
57
58 YYPACT[S] = index in YYTABLE of the portion describing state S.
59 The lookahead token's type is used to index that portion to find
60 out what to do.
61
62 If the value in YYTABLE is positive, we shift the token and go to
63 that state.
64
65 If the value is negative, it is minus a rule number to reduce by.
66
67 If the value is zero, the default action from YYDEFACT[S] is used.
68
69 YYPGOTO[I] = the index in YYTABLE of the portion describing what to
70 do after reducing a rule that derives variable I + NTOKENS. This
71 portion is indexed by the parser state number, S, as of before the
72 text for this nonterminal was read. The value from YYTABLE is the
73 state to go to if the corresponding value in YYCHECK is S.
74
75 YYTABLE = a vector filled with portions for different uses, found
76 via YYPACT and YYPGOTO.
77
78 YYCHECK = a vector indexed in parallel with YYTABLE. It indicates,
79 in a roundabout way, the bounds of the portion you are trying to
80 examine.
81
82 Suppose that the portion of yytable starts at index P and the index
83 to be examined within the portion is I. Then if YYCHECK[P+I] != I,
84 I is outside the bounds of what is actually allocated, and the
85 default (from YYDEFACT or YYDEFGOTO) should be used. Otherwise,
86 YYTABLE[P+I] should be used.
87
88 YYFINAL = the state number of the termination state. YYFLAG = most
89 negative short int. Used to flag ?? */
90
91 #include "system.h"
92 #include "bitsetv.h"
93 #include "quotearg.h"
94 #include "error.h"
95 #include "getargs.h"
96 #include "files.h"
97 #include "gram.h"
98 #include "LR0.h"
99 #include "complain.h"
100 #include "output.h"
101 #include "lalr.h"
102 #include "reader.h"
103 #include "symtab.h"
104 #include "conflicts.h"
105 #include "muscle_tab.h"
106
107 /* From lib/readpipe.h. */
108 FILE *readpipe PARAMS ((const char *, ...));
109
110 /* From src/scan-skel.l. */
111 int skel_lex PARAMS ((void));
112 extern FILE *skel_in;
113
114 static int nvectors;
115 static int nentries;
116 static short **froms = NULL;
117 static short **tos = NULL;
118 static short *tally = NULL;
119 static short *width = NULL;
120 static short *actrow = NULL;
121 static short *state_count = NULL;
122 static short *order = NULL;
123 static short *base = NULL;
124 static short *pos = NULL;
125 static short *table = NULL;
126 static short *check = NULL;
127 static int lowzero;
128 static int high;
129
130 struct obstack muscle_obstack;
131 static struct obstack format_obstack;
132
133 int error_verbose = 0;
134
135 /* Returns the number of lines of S. */
136 size_t
137 get_lines_number (const char *s)
138 {
139 size_t lines = 0;
140
141 size_t i;
142 for (i = 0; s[i]; ++i)
143 if (s[i] == '\n')
144 ++lines;
145
146 return lines;
147 }
148
149
150 /*----------------------------------------------------------------.
151 | Format the FIRST and then TABLE_DATA[BEGIN..END[ into OOUT, and |
152 | return the number of bits needed for its longuest value. |
153 `----------------------------------------------------------------*/
154
155 static inline long int
156 output_short_table (struct obstack *oout,
157 short *table_data,
158 short first,
159 int begin,
160 int end)
161 {
162 long int max = first;
163 int i;
164 int j = 1;
165
166 obstack_fgrow1 (oout, "%6d", first);
167 for (i = begin; i < end; ++i)
168 {
169 obstack_1grow (oout, ',');
170 if (j >= 10)
171 {
172 obstack_sgrow (oout, "\n ");
173 j = 1;
174 }
175 else
176 ++j;
177 obstack_fgrow1 (oout, "%6d", table_data[i]);
178 if (table_data[i] > max)
179 max = table_data[i];
180 }
181 obstack_1grow (oout, 0);
182
183 return max;
184 }
185
186
187 /*--------------------.
188 | Similar, for ints. |
189 `--------------------*/
190
191 static inline long int
192 output_int_table (struct obstack *oout,
193 int *table_data,
194 int first,
195 int begin,
196 int end)
197 {
198 long int max = first;
199 int i;
200 int j = 1;
201
202 obstack_fgrow1 (oout, "%6d", first);
203 for (i = begin; i < end; ++i)
204 {
205 obstack_1grow (oout, ',');
206 if (j >= 10)
207 {
208 obstack_sgrow (oout, "\n ");
209 j = 1;
210 }
211 else
212 ++j;
213 obstack_fgrow1 (oout, "%6d", table_data[i]);
214 if (table_data[i] > max)
215 max = table_data[i];
216 }
217 obstack_1grow (oout, 0);
218
219 return max;
220 }
221
222
223 /*-----------------------------------------------------------------.
224 | Prepare the muscles related to the tokens: translate, tname, and |
225 | toknum. |
226 `-----------------------------------------------------------------*/
227
228 static void
229 prepare_tokens (void)
230 {
231 long int max = output_short_table (&format_obstack, token_translations,
232 0, 1, max_user_token_number + 1);
233 muscle_insert ("translate", obstack_finish (&format_obstack));
234 MUSCLE_INSERT_LONG_INT ("token_number_max", max);
235 XFREE (token_translations);
236
237 {
238 int i;
239 int j = 0;
240 for (i = 0; i < nsyms; i++)
241 {
242 /* Be sure not to use twice the same quotearg slot. */
243 const char *cp =
244 quotearg_n_style (1, c_quoting_style,
245 quotearg_style (escape_quoting_style,
246 symbols[i]->tag));
247 /* Width of the next token, including the two quotes, the coma
248 and the space. */
249 int strsize = strlen (cp) + 2;
250
251 if (j + strsize > 75)
252 {
253 obstack_sgrow (&format_obstack, "\n ");
254 j = 2;
255 }
256
257 obstack_sgrow (&format_obstack, cp);
258 obstack_sgrow (&format_obstack, ", ");
259 j += strsize;
260 }
261 /* Add a NULL entry to list of tokens (well, 0, as NULL might not be
262 defined). */
263 obstack_sgrow (&format_obstack, "0");
264
265 /* Finish table and store. */
266 obstack_1grow (&format_obstack, 0);
267 muscle_insert ("tname", obstack_finish (&format_obstack));
268 }
269
270 /* Output YYTOKNUM. */
271 {
272 int i;
273 short *values = XCALLOC (short, ntokens + 1);
274 for (i = 0; i < ntokens + 1; ++i)
275 values[i] = symbols[i]->user_token_number;
276 output_short_table (&format_obstack, values,
277 0, 1, ntokens + 1);
278 muscle_insert ("toknum", obstack_finish (&format_obstack));
279 free (values);
280 }
281 }
282
283
284 /*-------------------------------------------------------------.
285 | Prepare the muscles related to the rules: rhs, prhs, r1, r2, |
286 | rline. |
287 `-------------------------------------------------------------*/
288
289 static void
290 prepare_rules (void)
291 {
292 long int max;
293 item_number_t *rhsp;
294 int r;
295 int i = 0;
296 item_number_t *rhs = XMALLOC (item_number_t, nritems);
297 short *prhs = XMALLOC (short, nrules + 1);
298 short *r1 = XMALLOC (short, nrules + 1);
299 short *r2 = XMALLOC (short, nrules + 1);
300 short *rline = XMALLOC (short, nrules + 1);
301
302 for (r = 1; r < nrules + 1; ++r)
303 {
304 /* Index of rule R in RHS. */
305 prhs[r] = i;
306 /* RHS of the rule R. */
307 for (rhsp = rules[r].rhs; *rhsp >= 0; ++rhsp)
308 rhs[i++] = *rhsp;
309 /* LHS of the rule R. */
310 r1[r] = rules[r].lhs->number;
311 /* Length of rule R's RHS. */
312 r2[r] = i - prhs[r];
313 /* Separator in RHS. */
314 rhs[i++] = -1;
315 /* Line where rule was defined. */
316 rline[r] = rules[r].line;
317 }
318 assert (i == nritems);
319
320 max = output_int_table (&format_obstack, rhs, ritem[0], 1, nritems);
321 muscle_insert ("rhs", obstack_finish (&format_obstack));
322 MUSCLE_INSERT_LONG_INT ("rhs_number_max", max);
323
324 output_short_table (&format_obstack, prhs, 0, 1, nrules + 1);
325 muscle_insert ("prhs", obstack_finish (&format_obstack));
326
327 output_short_table (&format_obstack, rline, 0, 1, nrules + 1);
328 muscle_insert ("rline", obstack_finish (&format_obstack));
329
330 output_short_table (&format_obstack, r1, 0, 1, nrules + 1);
331 muscle_insert ("r1", obstack_finish (&format_obstack));
332
333 output_short_table (&format_obstack, r2, 0, 1, nrules + 1);
334 muscle_insert ("r2", obstack_finish (&format_obstack));
335
336 free (rhs);
337 free (prhs);
338 free (r2);
339 }
340
341 /*--------------------------------------------.
342 | Prepare the muscles related to the states. |
343 `--------------------------------------------*/
344
345 static void
346 prepare_states (void)
347 {
348 size_t i;
349 short *values = (short *) alloca (sizeof (short) * nstates);
350 for (i = 0; i < nstates; ++i)
351 values[i] = states[i]->accessing_symbol;
352 output_short_table (&format_obstack, values,
353 0, 1, nstates);
354 muscle_insert ("stos", obstack_finish (&format_obstack));
355 }
356
357
358 /*------------------------------------------------------------------.
359 | Decide what to do for each type of token if seen as the lookahead |
360 | token in specified state. The value returned is used as the |
361 | default action (yydefact) for the state. In addition, actrow is |
362 | filled with what to do for each kind of token, index by symbol |
363 | number, with zero meaning do the default action. The value |
364 | SHRT_MIN, a very negative number, means this situation is an |
365 | error. The parser recognizes this value specially. |
366 | |
367 | This is where conflicts are resolved. The loop over lookahead |
368 | rules considered lower-numbered rules last, and the last rule |
369 | considered that likes a token gets to handle it. |
370 `------------------------------------------------------------------*/
371
372 static int
373 action_row (state_t *state)
374 {
375 int i;
376 int default_rule = 0;
377 reductions *redp = state->reductions;
378 shifts *shiftp = state->shifts;
379 errs *errp = state->errs;
380 /* set nonzero to inhibit having any default reduction */
381 int nodefault = 0;
382
383 for (i = 0; i < ntokens; i++)
384 actrow[i] = 0;
385
386 if (redp->nreds >= 1)
387 {
388 int j;
389 /* loop over all the rules available here which require
390 lookahead */
391 for (i = state->nlookaheads - 1; i >= 0; --i)
392 /* and find each token which the rule finds acceptable
393 to come next */
394 for (j = 0; j < ntokens; j++)
395 /* and record this rule as the rule to use if that
396 token follows. */
397 if (bitset_test (LA[state->lookaheadsp + i], j))
398 actrow[j] = -LArule[state->lookaheadsp + i]->number;
399 }
400
401 /* Now see which tokens are allowed for shifts in this state. For
402 them, record the shift as the thing to do. So shift is preferred
403 to reduce. */
404 for (i = 0; i < shiftp->nshifts; i++)
405 {
406 int symbol;
407 int shift_state = shiftp->shifts[i];
408 if (!shift_state)
409 continue;
410
411 symbol = states[shift_state]->accessing_symbol;
412
413 if (ISVAR (symbol))
414 break;
415
416 actrow[symbol] = shift_state;
417
418 /* Do not use any default reduction if there is a shift for
419 error */
420 if (symbol == errtoken->number)
421 nodefault = 1;
422 }
423
424 /* See which tokens are an explicit error in this state (due to
425 %nonassoc). For them, record SHRT_MIN as the action. */
426 for (i = 0; i < errp->nerrs; i++)
427 {
428 int symbol = errp->errs[i];
429 actrow[symbol] = SHRT_MIN;
430 }
431
432 /* Now find the most common reduction and make it the default action
433 for this state. */
434
435 if (redp->nreds >= 1 && !nodefault)
436 {
437 if (state->consistent)
438 default_rule = redp->rules[0];
439 else
440 {
441 int max = 0;
442 for (i = 0; i < state->nlookaheads; i++)
443 {
444 int count = 0;
445 int rule = -LArule[state->lookaheadsp + i]->number;
446 int j;
447
448 for (j = 0; j < ntokens; j++)
449 if (actrow[j] == rule)
450 count++;
451
452 if (count > max)
453 {
454 max = count;
455 default_rule = rule;
456 }
457 }
458
459 /* actions which match the default are replaced with zero,
460 which means "use the default" */
461
462 if (max > 0)
463 {
464 int j;
465 for (j = 0; j < ntokens; j++)
466 if (actrow[j] == default_rule)
467 actrow[j] = 0;
468
469 default_rule = -default_rule;
470 }
471 }
472 }
473
474 /* If have no default rule, the default is an error.
475 So replace any action which says "error" with "use default". */
476
477 if (default_rule == 0)
478 for (i = 0; i < ntokens; i++)
479 if (actrow[i] == SHRT_MIN)
480 actrow[i] = 0;
481
482 return default_rule;
483 }
484
485
486 static void
487 save_row (int state)
488 {
489 int i;
490 int count;
491 short *sp;
492 short *sp1;
493 short *sp2;
494
495 count = 0;
496 for (i = 0; i < ntokens; i++)
497 if (actrow[i] != 0)
498 count++;
499
500 if (count == 0)
501 return;
502
503 froms[state] = sp1 = sp = XCALLOC (short, count);
504 tos[state] = sp2 = XCALLOC (short, count);
505
506 for (i = 0; i < ntokens; i++)
507 if (actrow[i] != 0)
508 {
509 *sp1++ = i;
510 *sp2++ = actrow[i];
511 }
512
513 tally[state] = count;
514 width[state] = sp1[-1] - sp[0] + 1;
515 }
516
517
518 /*------------------------------------------------------------------.
519 | Figure out the actions for the specified state, indexed by |
520 | lookahead token type. |
521 | |
522 | The YYDEFACT table is output now. The detailed info is saved for |
523 | putting into YYTABLE later. |
524 `------------------------------------------------------------------*/
525
526 static void
527 token_actions (void)
528 {
529 size_t i;
530 short *yydefact = XCALLOC (short, nstates);
531
532 actrow = XCALLOC (short, ntokens);
533 for (i = 0; i < nstates; ++i)
534 {
535 yydefact[i] = action_row (states[i]);
536 save_row (i);
537 }
538
539 output_short_table (&format_obstack, yydefact,
540 yydefact[0], 1, nstates);
541 muscle_insert ("defact", obstack_finish (&format_obstack));
542
543 XFREE (actrow);
544 XFREE (yydefact);
545 }
546
547
548 /*-----------------------------.
549 | Output the actions to OOUT. |
550 `-----------------------------*/
551
552 void
553 actions_output (FILE *out)
554 {
555 int rule;
556 for (rule = 1; rule < nrules + 1; ++rule)
557 if (rules[rule].action)
558 {
559 fprintf (out, " case %d:\n", rule);
560
561 if (!no_lines_flag)
562 fprintf (out, muscle_find ("linef"),
563 rules[rule].action_line,
564 quotearg_style (c_quoting_style,
565 muscle_find ("filename")));
566 /* As a Bison extension, add the ending semicolon. Since some
567 Yacc don't do that, help people using bison as a Yacc
568 finding their missing semicolons. */
569 fprintf (out, "{ %s%s }\n break;\n\n",
570 rules[rule].action,
571 yacc_flag ? ";" : "");
572 }
573 }
574
575
576 /*----------------------------.
577 | Output the guards to OOUT. |
578 `----------------------------*/
579
580 void
581 guards_output (FILE *out)
582 {
583 int rule;
584 for (rule = 1; rule < nrules + 1; ++rule)
585 if (rules[rule].guard)
586 {
587 fprintf (out, " case %d:\n", rule);
588
589 if (!no_lines_flag)
590 fprintf (out, muscle_find ("linef"),
591 rules[rule].guard_line,
592 quotearg_style (c_quoting_style,
593 muscle_find ("filename")));
594 fprintf (out, "{ %s; }\n break;\n\n",
595 rules[rule].guard);
596 }
597 }
598
599
600 /*---------------------------------------.
601 | Output the tokens definition to OOUT. |
602 `---------------------------------------*/
603
604 void
605 token_definitions_output (FILE *out)
606 {
607 int i;
608 int first = 1;
609 for (i = 0; i < ntokens; ++i)
610 {
611 symbol_t *symbol = symbols[i];
612 int number = symbol->user_token_number;
613
614 if (number == SALIAS)
615 continue;
616 /* Skip error token. */
617 if (symbol == errtoken)
618 continue;
619 if (symbol->tag[0] == '\'')
620 continue; /* skip literal character */
621 if (symbol->tag[0] == '\"')
622 {
623 /* use literal string only if given a symbol with an alias */
624 if (symbol->alias)
625 symbol = symbol->alias;
626 else
627 continue;
628 }
629
630 /* Don't #define nonliteral tokens whose names contain periods
631 or '$' (as does the default value of the EOF token). */
632 if (strchr (symbol->tag, '.') || strchr (symbol->tag, '$'))
633 continue;
634
635 fprintf (out, "%s [[[%s]], [%d]]",
636 first ? "" : ",\n", symbol->tag, number);
637 if (semantic_parser)
638 /* FIXME: This is probably wrong, and should be just as
639 above. --akim. */
640 fprintf (out, "# define T%s\t%d\n", symbol->tag, symbol->number);
641 first = 0;
642 }
643 }
644
645
646 static void
647 save_column (int symbol, int default_state)
648 {
649 int i;
650 short *sp;
651 short *sp1;
652 short *sp2;
653 int count;
654 int symno = symbol - ntokens + nstates;
655
656 short begin = goto_map[symbol];
657 short end = goto_map[symbol + 1];
658
659 count = 0;
660 for (i = begin; i < end; i++)
661 if (to_state[i] != default_state)
662 count++;
663
664 if (count == 0)
665 return;
666
667 froms[symno] = sp1 = sp = XCALLOC (short, count);
668 tos[symno] = sp2 = XCALLOC (short, count);
669
670 for (i = begin; i < end; i++)
671 if (to_state[i] != default_state)
672 {
673 *sp1++ = from_state[i];
674 *sp2++ = to_state[i];
675 }
676
677 tally[symno] = count;
678 width[symno] = sp1[-1] - sp[0] + 1;
679 }
680
681 static int
682 default_goto (int symbol)
683 {
684 size_t i;
685 size_t m = goto_map[symbol];
686 size_t n = goto_map[symbol + 1];
687 int default_state = -1;
688 int max = 0;
689
690 if (m == n)
691 return -1;
692
693 for (i = 0; i < nstates; i++)
694 state_count[i] = 0;
695
696 for (i = m; i < n; i++)
697 state_count[to_state[i]]++;
698
699 for (i = 0; i < nstates; i++)
700 if (state_count[i] > max)
701 {
702 max = state_count[i];
703 default_state = i;
704 }
705
706 return default_state;
707 }
708
709
710 /*-------------------------------------------------------------------.
711 | Figure out what to do after reducing with each rule, depending on |
712 | the saved state from before the beginning of parsing the data that |
713 | matched this rule. |
714 | |
715 | The YYDEFGOTO table is output now. The detailed info is saved for |
716 | putting into YYTABLE later. |
717 `-------------------------------------------------------------------*/
718
719 static void
720 goto_actions (void)
721 {
722 int i;
723 short *yydefgoto = XMALLOC (short, nsyms - ntokens);
724
725 state_count = XCALLOC (short, nstates);
726 for (i = ntokens; i < nsyms; ++i)
727 {
728 int default_state = default_goto (i);
729 save_column (i, default_state);
730 yydefgoto[i - ntokens] = default_state;
731 }
732
733 output_short_table (&format_obstack, yydefgoto,
734 yydefgoto[0], 1, nsyms - ntokens);
735 muscle_insert ("defgoto", obstack_finish (&format_obstack));
736
737 XFREE (state_count);
738 XFREE (yydefgoto);
739 }
740
741
742 /* The next few functions decide how to pack the actions and gotos
743 information into yytable. */
744
745 static void
746 sort_actions (void)
747 {
748 int i;
749
750 order = XCALLOC (short, nvectors);
751 nentries = 0;
752
753 for (i = 0; i < nvectors; i++)
754 if (tally[i] > 0)
755 {
756 int k;
757 int t = tally[i];
758 int w = width[i];
759 int j = nentries - 1;
760
761 while (j >= 0 && (width[order[j]] < w))
762 j--;
763
764 while (j >= 0 && (width[order[j]] == w) && (tally[order[j]] < t))
765 j--;
766
767 for (k = nentries - 1; k > j; k--)
768 order[k + 1] = order[k];
769
770 order[j + 1] = i;
771 nentries++;
772 }
773 }
774
775
776 static int
777 matching_state (int vector)
778 {
779 int i = order[vector];
780 int t;
781 int w;
782 int prev;
783
784 if (i >= (int) nstates)
785 return -1;
786
787 t = tally[i];
788 w = width[i];
789
790 for (prev = vector - 1; prev >= 0; prev--)
791 {
792 int j = order[prev];
793 int k;
794 int match = 1;
795
796 if (width[j] != w || tally[j] != t)
797 return -1;
798
799 for (k = 0; match && k < t; k++)
800 if (tos[j][k] != tos[i][k] || froms[j][k] != froms[i][k])
801 match = 0;
802
803 if (match)
804 return j;
805 }
806
807 return -1;
808 }
809
810 /* FIXME: For the time being, best approximation... */
811 #define MAXTABLE SHRT_MAX
812
813 static int
814 pack_vector (int vector)
815 {
816 int i = order[vector];
817 int j;
818 int t = tally[i];
819 int loc = 0;
820 short *from = froms[i];
821 short *to = tos[i];
822
823 assert (t);
824
825 for (j = lowzero - from[0]; j < MAXTABLE; j++)
826 {
827 int k;
828 int ok = 1;
829
830 for (k = 0; ok && k < t; k++)
831 {
832 loc = j + from[k];
833 if (loc > MAXTABLE)
834 fatal (_("maximum table size (%d) exceeded"), MAXTABLE);
835
836 if (table[loc] != 0)
837 ok = 0;
838 }
839
840 for (k = 0; ok && k < vector; k++)
841 if (pos[k] == j)
842 ok = 0;
843
844 if (ok)
845 {
846 for (k = 0; k < t; k++)
847 {
848 loc = j + from[k];
849 table[loc] = to[k];
850 check[loc] = from[k];
851 }
852
853 while (table[lowzero] != 0)
854 lowzero++;
855
856 if (loc > high)
857 high = loc;
858
859 return j;
860 }
861 }
862 #define pack_vector_succeeded 0
863 assert (pack_vector_succeeded);
864 return 0;
865 }
866
867
868 static void
869 pack_table (void)
870 {
871 int i;
872 int place;
873 int state;
874
875 base = XCALLOC (short, nvectors);
876 pos = XCALLOC (short, nentries);
877 table = XCALLOC (short, MAXTABLE);
878 check = XCALLOC (short, MAXTABLE);
879
880 lowzero = 0;
881 high = 0;
882
883 for (i = 0; i < nvectors; i++)
884 base[i] = SHRT_MIN;
885
886 for (i = 0; i < MAXTABLE; i++)
887 check[i] = -1;
888
889 for (i = 0; i < nentries; i++)
890 {
891 state = matching_state (i);
892
893 if (state < 0)
894 place = pack_vector (i);
895 else
896 place = base[state];
897
898 pos[i] = place;
899 base[order[i]] = place;
900 }
901
902 for (i = 0; i < nvectors; i++)
903 {
904 XFREE (froms[i]);
905 XFREE (tos[i]);
906 }
907
908 XFREE (froms);
909 XFREE (tos);
910 XFREE (pos);
911 }
912
913 /* the following functions output yytable, yycheck
914 and the vectors whose elements index the portion starts */
915
916 static void
917 output_base (void)
918 {
919 /* Output pact. */
920 output_short_table (&format_obstack, base,
921 base[0], 1, nstates);
922 muscle_insert ("pact", obstack_finish (&format_obstack));
923
924 /* Output pgoto. */
925 output_short_table (&format_obstack, base,
926 base[nstates], nstates + 1, nvectors);
927 muscle_insert ("pgoto", obstack_finish (&format_obstack));
928
929 XFREE (base);
930 }
931
932
933 static void
934 output_table (void)
935 {
936 output_short_table (&format_obstack, table,
937 table[0], 1, high + 1);
938 muscle_insert ("table", obstack_finish (&format_obstack));
939 XFREE (table);
940 }
941
942
943 static void
944 output_check (void)
945 {
946 output_short_table (&format_obstack, check,
947 check[0], 1, high + 1);
948 muscle_insert ("check", obstack_finish (&format_obstack));
949 XFREE (check);
950 }
951
952 /*-----------------------------------------------------------------.
953 | Compute and output yydefact, yydefgoto, yypact, yypgoto, yytable |
954 | and yycheck. |
955 `-----------------------------------------------------------------*/
956
957 static void
958 output_actions (void)
959 {
960 size_t i;
961 nvectors = nstates + nvars;
962
963 froms = XCALLOC (short *, nvectors);
964 tos = XCALLOC (short *, nvectors);
965 tally = XCALLOC (short, nvectors);
966 width = XCALLOC (short, nvectors);
967
968 token_actions ();
969 bitsetv_free (LA);
970 free (LArule);
971
972 goto_actions ();
973 XFREE (goto_map + ntokens);
974 XFREE (from_state);
975 XFREE (to_state);
976
977 sort_actions ();
978 pack_table ();
979
980 output_base ();
981 output_table ();
982
983 output_check ();
984
985 for (i = 0; i < nstates; ++i)
986 {
987 free (states[i]->shifts);
988 XFREE (states[i]->reductions);
989 free (states[i]->errs);
990 free (states[i]);
991 }
992 XFREE (states);
993 }
994
995 \f
996 /*---------------------------.
997 | Call the skeleton parser. |
998 `---------------------------*/
999
1000 static void
1001 output_skeleton (void)
1002 {
1003 /* Store the definition of all the muscles. */
1004 const char *tempdir = getenv ("TMPDIR");
1005 char *tempfile = NULL;
1006 FILE *out = NULL;
1007 int fd;
1008
1009 if (tempdir == NULL)
1010 tempdir = DEFAULT_TMPDIR;
1011 tempfile = xmalloc (strlen (tempdir) + 11);
1012 sprintf (tempfile, "%s/bsnXXXXXX", tempdir);
1013 fd = mkstemp (tempfile);
1014 if (fd == -1)
1015 error (EXIT_FAILURE, errno, "%s", tempfile);
1016
1017 out = fdopen (fd, "w");
1018 if (out == NULL)
1019 error (EXIT_FAILURE, errno, "%s", tempfile);
1020
1021 /* There are no comments, especially not `#': we do want M4 expansion
1022 after `#': think of CPP macros! */
1023 fputs ("m4_changecom()\n", out);
1024 fputs ("m4_init()\n", out);
1025
1026 fputs ("m4_define([b4_actions], \n[[", out);
1027 actions_output (out);
1028 fputs ("]])\n\n", out);
1029
1030 fputs ("m4_define([b4_guards], \n[[", out);
1031 guards_output (out);
1032 fputs ("]])\n\n", out);
1033
1034 fputs ("m4_define([b4_tokens], \n[", out);
1035 token_definitions_output (out);
1036 fputs ("])\n\n", out);
1037
1038 muscles_m4_output (out);
1039
1040 fputs ("m4_wrap([m4_divert_pop(0)])\n", out);
1041 fputs ("m4_divert_push(0)dnl\n", out);
1042 xfclose (out);
1043
1044 /* Invoke m4 on the definition of the muscles, and the skeleton. */
1045 {
1046 const char *bison_pkgdatadir = getenv ("BISON_PKGDATADIR");
1047 const char *m4 = getenv ("M4");
1048 if (!m4)
1049 m4 = M4;
1050 if (!bison_pkgdatadir)
1051 bison_pkgdatadir = PKGDATADIR;
1052 if (trace_flag)
1053 fprintf (stderr,
1054 "running: %s -I %s m4sugar/m4sugar.m4 %s %s\n",
1055 m4, bison_pkgdatadir, tempfile, skeleton);
1056 skel_in = readpipe (m4,
1057 "-I", bison_pkgdatadir,
1058 "m4sugar/m4sugar.m4",
1059 tempfile,
1060 skeleton,
1061 NULL);
1062 if (!skel_in)
1063 error (EXIT_FAILURE, errno, "cannot run m4");
1064 skel_lex ();
1065
1066 /* If `debugging', keep this file alive. */
1067 if (!trace_flag)
1068 unlink (tempfile);
1069 }
1070 }
1071
1072 static void
1073 prepare (void)
1074 {
1075 MUSCLE_INSERT_INT ("last", high);
1076 MUSCLE_INSERT_INT ("flag", SHRT_MIN);
1077 MUSCLE_INSERT_INT ("pure", pure_parser);
1078 MUSCLE_INSERT_INT ("nsym", nsyms);
1079 MUSCLE_INSERT_INT ("debug", debug_flag);
1080 MUSCLE_INSERT_INT ("final", final_state);
1081 MUSCLE_INSERT_INT ("undef_token_number", undeftoken->number);
1082 MUSCLE_INSERT_INT ("user_token_number_max", max_user_token_number);
1083 MUSCLE_INSERT_INT ("error_verbose", error_verbose);
1084 MUSCLE_INSERT_STRING ("prefix", spec_name_prefix ? spec_name_prefix : "yy");
1085
1086 /* FIXME: This is wrong: the muscles should decide whether they hold
1087 a copy or not, but the situation is too obscure currently. */
1088 MUSCLE_INSERT_STRING ("output_infix", output_infix ? output_infix : "");
1089 MUSCLE_INSERT_STRING ("output_prefix", short_base_name);
1090 MUSCLE_INSERT_STRING ("output_parser_name", parser_file_name);
1091 MUSCLE_INSERT_STRING ("output_header_name", spec_defines_file);
1092
1093 MUSCLE_INSERT_INT ("nnts", nvars);
1094 MUSCLE_INSERT_INT ("nrules", nrules);
1095 MUSCLE_INSERT_INT ("nstates", nstates);
1096 MUSCLE_INSERT_INT ("ntokens", ntokens);
1097
1098 MUSCLE_INSERT_INT ("locations_flag", locations_flag);
1099 MUSCLE_INSERT_INT ("defines_flag", defines_flag);
1100
1101 /* Copy definitions in directive. */
1102 obstack_1grow (&attrs_obstack, 0);
1103 muscle_insert ("prologue", obstack_finish (&attrs_obstack));
1104
1105 /* Find the right skeleton file. */
1106 if (!skeleton)
1107 {
1108 if (semantic_parser)
1109 skeleton = "bison.hairy";
1110 else
1111 skeleton = "bison.simple";
1112 }
1113
1114 /* Parse the skeleton file and output the needed parsers. */
1115 muscle_insert ("skeleton", skeleton);
1116 }
1117
1118
1119 /*----------------------------------------------------------.
1120 | Output the parsing tables and the parser code to ftable. |
1121 `----------------------------------------------------------*/
1122
1123 void
1124 output (void)
1125 {
1126 obstack_init (&format_obstack);
1127
1128 prepare_tokens ();
1129 prepare_rules ();
1130 prepare_states ();
1131 output_actions ();
1132
1133 prepare ();
1134
1135 /* Process the selected skeleton file. */
1136 output_skeleton ();
1137
1138 obstack_free (&muscle_obstack, NULL);
1139 obstack_free (&format_obstack, NULL);
1140 obstack_free (&action_obstack, NULL);
1141 obstack_free (&attrs_obstack, NULL);
1142 }