]> git.saurik.com Git - bison.git/blob - src/output.c
* src/LR0.c (generate_states): Use nritems, not nitems, nor using
[bison.git] / src / output.c
1 /* Output the generated parsing program for bison,
2 Copyright 1984, 1986, 1989, 1992, 2000, 2001
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 "quotearg.h"
93 #include "getargs.h"
94 #include "files.h"
95 #include "gram.h"
96 #include "LR0.h"
97 #include "complain.h"
98 #include "output.h"
99 #include "lalr.h"
100 #include "reader.h"
101 #include "conflicts.h"
102 #include "muscle_tab.h"
103
104
105 static int nvectors;
106 static int nentries;
107 static short **froms = NULL;
108 static short **tos = NULL;
109 static short *tally = NULL;
110 static short *width = NULL;
111 static short *actrow = NULL;
112 static short *state_count = NULL;
113 static short *order = NULL;
114 static short *base = NULL;
115 static short *pos = NULL;
116 static short *table = NULL;
117 static short *check = NULL;
118 static int lowzero;
119 static int high;
120
121 struct obstack muscle_obstack;
122 static struct obstack format_obstack;
123
124 int error_verbose = 0;
125
126 /* Returns the number of lines of S. */
127 static size_t
128 get_lines_number (const char *s)
129 {
130 size_t lines = 0;
131
132 size_t i;
133 for (i = 0; s[i]; ++i)
134 if (s[i] == '\n')
135 ++lines;
136
137 return lines;
138 }
139
140
141 /* FIXME. */
142
143 static inline void
144 output_table_data (struct obstack *oout,
145 short *table_data,
146 short first,
147 int begin,
148 int end)
149 {
150 int i;
151 int j = 1;
152
153 obstack_fgrow1 (oout, "%6d", first);
154 for (i = begin; i < end; ++i)
155 {
156 obstack_1grow (oout, ',');
157 if (j >= 10)
158 {
159 obstack_sgrow (oout, "\n ");
160 j = 1;
161 }
162 else
163 ++j;
164 obstack_fgrow1 (oout, "%6d", table_data[i]);
165 }
166 obstack_1grow (oout, 0);
167 }
168
169
170 static void
171 output_token_translations (void)
172 {
173 output_table_data (&format_obstack, token_translations,
174 0, 1, max_user_token_number + 1);
175 muscle_insert ("translate", obstack_finish (&format_obstack));
176 XFREE (token_translations);
177 }
178
179
180 static void
181 output_gram (void)
182 {
183 {
184 int i;
185 short *values = XCALLOC (short, nrules + 1);
186 for (i = 0; i < nrules + 1; ++i)
187 values[i] = rule_table[i].rhs;
188 output_table_data (&format_obstack, values,
189 0, 1, nrules + 1);
190 XFREE (values);
191 }
192
193 muscle_insert ("prhs", obstack_finish (&format_obstack));
194
195 {
196 short *yyrhs;
197 int i;
198
199 yyrhs = XMALLOC (short, nritems);
200
201 for (i = 1; i < nritems; ++i)
202 yyrhs[i] = ritem[i] >= 0 ? ritem[i] : -1;
203
204 output_table_data (&format_obstack, yyrhs,
205 ritem[0], 1, nritems);
206 muscle_insert ("rhs", obstack_finish (&format_obstack));
207
208 XFREE (yyrhs);
209 }
210
211 #if 0
212 if (!semantic_parser)
213 obstack_sgrow (&table_obstack, "\n#endif\n");
214 #endif
215 }
216
217
218 static void
219 output_stos (void)
220 {
221 int i;
222 short *values = (short *) alloca (sizeof (short) * nstates);
223 for (i = 0; i < nstates; ++i)
224 values[i] = state_table[i]->accessing_symbol;
225 output_table_data (&format_obstack, values,
226 0, 1, nstates);
227 muscle_insert ("stos", obstack_finish (&format_obstack));
228 }
229
230
231 static void
232 output_rule_data (void)
233 {
234 int i;
235 int j;
236 short *short_tab = NULL;
237
238 {
239 short *values = XCALLOC (short, nrules + 1);
240 for (i = 0; i < nrules + 1; ++i)
241 values[i] = rule_table[i].line;
242 output_table_data (&format_obstack, values,
243 0, 1, nrules + 1);
244 muscle_insert ("rline", obstack_finish (&format_obstack));
245 XFREE (values);
246 }
247
248
249 j = 0;
250 for (i = 0; i < nsyms; i++)
251 {
252 /* Be sure not to use twice the same quotearg slot. */
253 const char *cp =
254 quotearg_n_style (1, c_quoting_style,
255 quotearg_style (escape_quoting_style, tags[i]));
256 /* Width of the next token, including the two quotes, the coma
257 and the space. */
258 int strsize = strlen (cp) + 2;
259
260 if (j + strsize > 75)
261 {
262 obstack_sgrow (&format_obstack, "\n ");
263 j = 2;
264 }
265
266 obstack_sgrow (&format_obstack, cp);
267 obstack_sgrow (&format_obstack, ", ");
268 j += strsize;
269 }
270 /* add a NULL entry to list of tokens */
271 obstack_sgrow (&format_obstack, "NULL");
272
273 /* Finish table and store. */
274 obstack_1grow (&format_obstack, 0);
275 muscle_insert ("tname", obstack_finish (&format_obstack));
276
277 /* Output YYTOKNUM. */
278 output_table_data (&format_obstack, user_toknums,
279 0, 1, ntokens + 1);
280 muscle_insert ("toknum", obstack_finish (&format_obstack));
281
282 /* Output YYR1. */
283 {
284 short *values = XCALLOC (short, nrules + 1);
285 for (i = 0; i < nrules + 1; ++i)
286 values[i] = rule_table[i].lhs;
287 output_table_data (&format_obstack, values,
288 0, 1, nrules + 1);
289 muscle_insert ("r1", obstack_finish (&format_obstack));
290 XFREE (values);
291 }
292
293 /* Output YYR2. */
294 short_tab = XMALLOC (short, nrules + 1);
295 for (i = 1; i < nrules; i++)
296 short_tab[i] = rule_table[i + 1].rhs - rule_table[i].rhs - 1;
297 short_tab[nrules] = nritems - rule_table[nrules].rhs - 1;
298 output_table_data (&format_obstack, short_tab,
299 0, 1, nrules + 1);
300 muscle_insert ("r2", obstack_finish (&format_obstack));
301 XFREE (short_tab);
302 }
303
304 /*------------------------------------------------------------------.
305 | Decide what to do for each type of token if seen as the lookahead |
306 | token in specified state. The value returned is used as the |
307 | default action (yydefact) for the state. In addition, actrow is |
308 | filled with what to do for each kind of token, index by symbol |
309 | number, with zero meaning do the default action. The value |
310 | MINSHORT, a very negative number, means this situation is an |
311 | error. The parser recognizes this value specially. |
312 | |
313 | This is where conflicts are resolved. The loop over lookahead |
314 | rules considered lower-numbered rules last, and the last rule |
315 | considered that likes a token gets to handle it. |
316 `------------------------------------------------------------------*/
317
318 static int
319 action_row (state_t *state)
320 {
321 int i;
322 int default_rule = 0;
323 reductions *redp = state->reductions;
324 shifts *shiftp = state->shifts;
325 errs *errp = state->errs;
326 /* set nonzero to inhibit having any default reduction */
327 int nodefault = 0;
328
329 for (i = 0; i < ntokens; i++)
330 actrow[i] = 0;
331
332 if (redp->nreds >= 1)
333 {
334 int j;
335 /* loop over all the rules available here which require
336 lookahead */
337 for (i = state->nlookaheads - 1; i >= 0; --i)
338 /* and find each token which the rule finds acceptable
339 to come next */
340 for (j = 0; j < ntokens; j++)
341 /* and record this rule as the rule to use if that
342 token follows. */
343 if (BITISSET (LA (state->lookaheadsp + i), j))
344 actrow[j] = -LAruleno[state->lookaheadsp + i];
345 }
346
347 /* Now see which tokens are allowed for shifts in this state. For
348 them, record the shift as the thing to do. So shift is preferred
349 to reduce. */
350 for (i = 0; i < shiftp->nshifts; i++)
351 {
352 int symbol;
353 int shift_state = shiftp->shifts[i];
354 if (!shift_state)
355 continue;
356
357 symbol = state_table[shift_state]->accessing_symbol;
358
359 if (ISVAR (symbol))
360 break;
361
362 actrow[symbol] = shift_state;
363
364 /* Do not use any default reduction if there is a shift for
365 error */
366 if (symbol == error_token_number)
367 nodefault = 1;
368 }
369
370 /* See which tokens are an explicit error in this state (due to
371 %nonassoc). For them, record MINSHORT as the action. */
372 for (i = 0; i < errp->nerrs; i++)
373 {
374 int symbol = errp->errs[i];
375 actrow[symbol] = MINSHORT;
376 }
377
378 /* Now find the most common reduction and make it the default action
379 for this state. */
380
381 if (redp->nreds >= 1 && !nodefault)
382 {
383 if (state->consistent)
384 default_rule = redp->rules[0];
385 else
386 {
387 int max = 0;
388 for (i = 0; i < state->nlookaheads; i++)
389 {
390 int count = 0;
391 int rule = -LAruleno[state->lookaheadsp + i];
392 int j;
393
394 for (j = 0; j < ntokens; j++)
395 if (actrow[j] == rule)
396 count++;
397
398 if (count > max)
399 {
400 max = count;
401 default_rule = rule;
402 }
403 }
404
405 /* actions which match the default are replaced with zero,
406 which means "use the default" */
407
408 if (max > 0)
409 {
410 int j;
411 for (j = 0; j < ntokens; j++)
412 if (actrow[j] == default_rule)
413 actrow[j] = 0;
414
415 default_rule = -default_rule;
416 }
417 }
418 }
419
420 /* If have no default rule, the default is an error.
421 So replace any action which says "error" with "use default". */
422
423 if (default_rule == 0)
424 for (i = 0; i < ntokens; i++)
425 if (actrow[i] == MINSHORT)
426 actrow[i] = 0;
427
428 return default_rule;
429 }
430
431
432 static void
433 save_row (int state)
434 {
435 int i;
436 int count;
437 short *sp;
438 short *sp1;
439 short *sp2;
440
441 count = 0;
442 for (i = 0; i < ntokens; i++)
443 if (actrow[i] != 0)
444 count++;
445
446 if (count == 0)
447 return;
448
449 froms[state] = sp1 = sp = XCALLOC (short, count);
450 tos[state] = sp2 = XCALLOC (short, count);
451
452 for (i = 0; i < ntokens; i++)
453 if (actrow[i] != 0)
454 {
455 *sp1++ = i;
456 *sp2++ = actrow[i];
457 }
458
459 tally[state] = count;
460 width[state] = sp1[-1] - sp[0] + 1;
461 }
462
463
464 /*------------------------------------------------------------------.
465 | Figure out the actions for the specified state, indexed by |
466 | lookahead token type. |
467 | |
468 | The YYDEFACT table is output now. The detailed info is saved for |
469 | putting into YYTABLE later. |
470 `------------------------------------------------------------------*/
471
472 static void
473 token_actions (void)
474 {
475 int i;
476 short *yydefact = XCALLOC (short, nstates);
477
478 actrow = XCALLOC (short, ntokens);
479 for (i = 0; i < nstates; ++i)
480 {
481 yydefact[i] = action_row (state_table[i]);
482 save_row (i);
483 }
484
485 output_table_data (&format_obstack, yydefact,
486 yydefact[0], 1, nstates);
487 muscle_insert ("defact", obstack_finish (&format_obstack));
488
489 XFREE (actrow);
490 XFREE (yydefact);
491 }
492
493
494 /*-----------------------------.
495 | Output the actions to OOUT. |
496 `-----------------------------*/
497
498 static void
499 actions_output (FILE *out, size_t *line)
500 {
501 int rule;
502 for (rule = 1; rule < nrules + 1; ++rule)
503 if (rule_table[rule].action)
504 {
505 fprintf (out, " case %d:\n", rule);
506
507 if (!no_lines_flag)
508 fprintf (out, muscle_find ("linef"),
509 rule_table[rule].action_line,
510 quotearg_style (c_quoting_style,
511 muscle_find ("filename")));
512 /* As a Bison extension, add the ending semicolon. Since some
513 Yacc don't do that, help people using bison as a Yacc
514 finding their missing semicolons. */
515 fprintf (out, "{ %s%s }\n break;\n\n",
516 rule_table[rule].action,
517 yacc_flag ? ";" : "");
518
519 /* We always output 4 '\n' per action. */
520 *line += 4;
521 /* Plus one if !no_lines_flag. */
522 if (!no_lines_flag)
523 ++*line;
524 /* Get the number of lines written by the user. */
525 *line += get_lines_number (rule_table[rule].action);
526 }
527 }
528
529
530 /*----------------------------.
531 | Output the guards to OOUT. |
532 `----------------------------*/
533
534 static void
535 guards_output (FILE *out, size_t *line)
536 {
537 int rule;
538 for (rule = 1; rule < nrules + 1; ++rule)
539 if (rule_table[rule].action)
540 {
541 fprintf (out, " case %d:\n", rule);
542
543 if (!no_lines_flag)
544 fprintf (out, muscle_find ("linef"),
545 rule_table[rule].guard_line,
546 quotearg_style (c_quoting_style,
547 muscle_find ("filename")));
548 fprintf (out, "{ %s; }\n break;\n\n",
549 rule_table[rule].guard);
550
551 /* We always output 4 '\n' per action. */
552 *line += 4;
553 /* Plus one if !no_lines_flag. */
554 if (!no_lines_flag)
555 ++*line;
556 /* Get the number of lines written by the user. */
557 *line += get_lines_number (rule_table[rule].guard);
558 }
559 }
560
561
562 static void
563 save_column (int symbol, int default_state)
564 {
565 int i;
566 short *sp;
567 short *sp1;
568 short *sp2;
569 int count;
570 int symno = symbol - ntokens + nstates;
571
572 short begin = goto_map[symbol];
573 short end = goto_map[symbol + 1];
574
575 count = 0;
576 for (i = begin; i < end; i++)
577 if (to_state[i] != default_state)
578 count++;
579
580 if (count == 0)
581 return;
582
583 froms[symno] = sp1 = sp = XCALLOC (short, count);
584 tos[symno] = sp2 = XCALLOC (short, count);
585
586 for (i = begin; i < end; i++)
587 if (to_state[i] != default_state)
588 {
589 *sp1++ = from_state[i];
590 *sp2++ = to_state[i];
591 }
592
593 tally[symno] = count;
594 width[symno] = sp1[-1] - sp[0] + 1;
595 }
596
597 static int
598 default_goto (int symbol)
599 {
600 int i;
601 int m = goto_map[symbol];
602 int n = goto_map[symbol + 1];
603 int default_state = -1;
604 int max = 0;
605
606 if (m == n)
607 return -1;
608
609 for (i = 0; i < nstates; i++)
610 state_count[i] = 0;
611
612 for (i = m; i < n; i++)
613 state_count[to_state[i]]++;
614
615 for (i = 0; i < nstates; i++)
616 if (state_count[i] > max)
617 {
618 max = state_count[i];
619 default_state = i;
620 }
621
622 return default_state;
623 }
624
625
626 /*-------------------------------------------------------------------.
627 | Figure out what to do after reducing with each rule, depending on |
628 | the saved state from before the beginning of parsing the data that |
629 | matched this rule. |
630 | |
631 | The YYDEFGOTO table is output now. The detailed info is saved for |
632 | putting into YYTABLE later. |
633 `-------------------------------------------------------------------*/
634
635 static void
636 goto_actions (void)
637 {
638 int i;
639 short *yydefgoto = XMALLOC (short, nsyms - ntokens);
640
641 state_count = XCALLOC (short, nstates);
642 for (i = ntokens; i < nsyms; ++i)
643 {
644 int default_state = default_goto (i);
645 save_column (i, default_state);
646 yydefgoto[i - ntokens] = default_state;
647 }
648
649 output_table_data (&format_obstack, yydefgoto,
650 yydefgoto[0], 1, nsyms - ntokens);
651 muscle_insert ("defgoto", obstack_finish (&format_obstack));
652
653 XFREE (state_count);
654 XFREE (yydefgoto);
655 }
656
657
658 /* The next few functions decide how to pack the actions and gotos
659 information into yytable. */
660
661 static void
662 sort_actions (void)
663 {
664 int i;
665
666 order = XCALLOC (short, nvectors);
667 nentries = 0;
668
669 for (i = 0; i < nvectors; i++)
670 if (tally[i] > 0)
671 {
672 int k;
673 int t = tally[i];
674 int w = width[i];
675 int j = nentries - 1;
676
677 while (j >= 0 && (width[order[j]] < w))
678 j--;
679
680 while (j >= 0 && (width[order[j]] == w) && (tally[order[j]] < t))
681 j--;
682
683 for (k = nentries - 1; k > j; k--)
684 order[k + 1] = order[k];
685
686 order[j + 1] = i;
687 nentries++;
688 }
689 }
690
691
692 static int
693 matching_state (int vector)
694 {
695 int i = order[vector];
696 int t;
697 int w;
698 int prev;
699
700 if (i >= nstates)
701 return -1;
702
703 t = tally[i];
704 w = width[i];
705
706 for (prev = vector - 1; prev >= 0; prev--)
707 {
708 int j = order[prev];
709 int k;
710 int match = 1;
711
712 if (width[j] != w || tally[j] != t)
713 return -1;
714
715 for (k = 0; match && k < t; k++)
716 if (tos[j][k] != tos[i][k] || froms[j][k] != froms[i][k])
717 match = 0;
718
719 if (match)
720 return j;
721 }
722
723 return -1;
724 }
725
726
727 static int
728 pack_vector (int vector)
729 {
730 int i = order[vector];
731 int j;
732 int t = tally[i];
733 int loc = 0;
734 short *from = froms[i];
735 short *to = tos[i];
736
737 assert (t);
738
739 for (j = lowzero - from[0]; j < MAXTABLE; j++)
740 {
741 int k;
742 int ok = 1;
743
744 for (k = 0; ok && k < t; k++)
745 {
746 loc = j + from[k];
747 if (loc > MAXTABLE)
748 fatal (_("maximum table size (%d) exceeded"), MAXTABLE);
749
750 if (table[loc] != 0)
751 ok = 0;
752 }
753
754 for (k = 0; ok && k < vector; k++)
755 if (pos[k] == j)
756 ok = 0;
757
758 if (ok)
759 {
760 for (k = 0; k < t; k++)
761 {
762 loc = j + from[k];
763 table[loc] = to[k];
764 check[loc] = from[k];
765 }
766
767 while (table[lowzero] != 0)
768 lowzero++;
769
770 if (loc > high)
771 high = loc;
772
773 return j;
774 }
775 }
776 #define pack_vector_succeeded 0
777 assert (pack_vector_succeeded);
778 return 0;
779 }
780
781
782 static void
783 pack_table (void)
784 {
785 int i;
786 int place;
787 int state;
788
789 base = XCALLOC (short, nvectors);
790 pos = XCALLOC (short, nentries);
791 table = XCALLOC (short, MAXTABLE);
792 check = XCALLOC (short, MAXTABLE);
793
794 lowzero = 0;
795 high = 0;
796
797 for (i = 0; i < nvectors; i++)
798 base[i] = MINSHORT;
799
800 for (i = 0; i < MAXTABLE; i++)
801 check[i] = -1;
802
803 for (i = 0; i < nentries; i++)
804 {
805 state = matching_state (i);
806
807 if (state < 0)
808 place = pack_vector (i);
809 else
810 place = base[state];
811
812 pos[i] = place;
813 base[order[i]] = place;
814 }
815
816 for (i = 0; i < nvectors; i++)
817 {
818 XFREE (froms[i]);
819 XFREE (tos[i]);
820 }
821
822 XFREE (froms);
823 XFREE (tos);
824 XFREE (pos);
825 }
826
827 /* the following functions output yytable, yycheck
828 and the vectors whose elements index the portion starts */
829
830 static void
831 output_base (void)
832 {
833 /* Output pact. */
834 output_table_data (&format_obstack, base,
835 base[0], 1, nstates);
836 muscle_insert ("pact", obstack_finish (&format_obstack));
837
838 /* Output pgoto. */
839 output_table_data (&format_obstack, base,
840 base[nstates], nstates + 1, nvectors);
841 muscle_insert ("pgoto", obstack_finish (&format_obstack));
842
843 XFREE (base);
844 }
845
846
847 static void
848 output_table (void)
849 {
850 output_table_data (&format_obstack, table,
851 table[0], 1, high + 1);
852 muscle_insert ("table", obstack_finish (&format_obstack));
853 XFREE (table);
854 }
855
856
857 static void
858 output_check (void)
859 {
860 output_table_data (&format_obstack, check,
861 check[0], 1, high + 1);
862 muscle_insert ("check", obstack_finish (&format_obstack));
863 XFREE (check);
864 }
865
866 /* compute and output yydefact, yydefgoto, yypact, yypgoto, yytable
867 and yycheck. */
868
869 static void
870 output_actions (void)
871 {
872 int i;
873 nvectors = nstates + nvars;
874
875 froms = XCALLOC (short *, nvectors);
876 tos = XCALLOC (short *, nvectors);
877 tally = XCALLOC (short, nvectors);
878 width = XCALLOC (short, nvectors);
879
880 token_actions ();
881 XFREE (LA);
882 XFREE (LAruleno);
883
884 goto_actions ();
885 XFREE (goto_map + ntokens);
886 XFREE (from_state);
887 XFREE (to_state);
888
889 sort_actions ();
890 pack_table ();
891
892 output_base ();
893 output_table ();
894
895 output_check ();
896
897 for (i = 0; i < nstates; ++i)
898 {
899 free (state_table[i]->shifts);
900 XFREE (state_table[i]->reductions);
901 free (state_table[i]->errs);
902 free (state_table[i]);
903 }
904 XFREE (state_table);
905 }
906
907 \f
908 /*------------------------------------------------------------.
909 | Copy the parser code from SKEL_FILENAME into OOUT obstack. |
910 | and do the muscle substitution. |
911 `------------------------------------------------------------*/
912
913 static void
914 output_parser (const char *skel_filename, FILE *out)
915 {
916 int c;
917 FILE *fskel;
918 size_t output_line;
919 size_t skeleton_line;
920
921 fskel = xfopen (skel_filename, "r");
922
923 /* New output code. */
924 output_line = 1;
925 skeleton_line = 1;
926 c = getc (fskel);
927 while (c != EOF)
928 {
929 if (c != '%')
930 {
931 if (c == '\n')
932 {
933 ++output_line;
934 ++skeleton_line;
935 }
936 putc (c, out);
937 c = getc (fskel);
938 }
939 else if ((c = getc (fskel)) == '%')
940 {
941 /* Read the muscle. */
942 const char *muscle_key = 0;
943 const char *muscle_value = 0;
944
945 while (isalnum (c = getc (fskel)) || c == '-')
946 obstack_1grow (&muscle_obstack, c);
947 obstack_1grow (&muscle_obstack, 0);
948
949 /* Output the right value, or see if it's something special. */
950 muscle_key = obstack_finish (&muscle_obstack);
951 muscle_value = muscle_find (muscle_key);
952 if (!strcmp (muscle_key, "actions"))
953 actions_output (out, &output_line);
954 else if (!strcmp (muscle_key, "guards"))
955 guards_output (out, &output_line);
956 else if (!strcmp (muscle_key, "line"))
957 fprintf (out, "%d", output_line);
958 else if (!strcmp (muscle_key, "skeleton-line"))
959 fprintf (out, "%d", skeleton_line);
960 else if (muscle_value)
961 {
962 fputs (muscle_value, out);
963 output_line += get_lines_number (muscle_value);
964 }
965 else
966 {
967 fputs ("%%", out);
968 fputs (muscle_key, out);
969 }
970 }
971 else
972 putc ('%', out);
973 }
974
975 /* End. */
976 xfclose (fskel);
977 }
978
979 /*----------------------------------------.
980 | Prepare the master parser to be output |
981 `----------------------------------------*/
982
983 static void
984 output_master_parser (void)
985 {
986 FILE *parser = xfopen (parser_file_name, "w");
987 if (!skeleton)
988 {
989 if (semantic_parser)
990 skeleton = skeleton_find ("BISON_HAIRY", BISON_HAIRY);
991 else
992 skeleton = skeleton_find ("BISON_SIMPLE", BISON_SIMPLE);
993 }
994 muscle_insert ("skeleton", skeleton);
995 muscle_insert ("parser-file-name", parser_file_name);
996
997 output_parser (skeleton, parser);
998 xfclose (parser);
999 }
1000
1001
1002 /* FIXME. */
1003
1004 #define MUSCLE_INSERT_INT(Key, Value) \
1005 { \
1006 obstack_fgrow1 (&muscle_obstack, "%d", Value); \
1007 obstack_1grow (&muscle_obstack, 0); \
1008 muscle_insert (Key, obstack_finish (&muscle_obstack)); \
1009 }
1010
1011 #define MUSCLE_INSERT_STRING(Key, Value) \
1012 { \
1013 obstack_sgrow (&muscle_obstack, Value); \
1014 obstack_1grow (&muscle_obstack, 0); \
1015 muscle_insert (Key, obstack_finish (&muscle_obstack)); \
1016 }
1017
1018 #define MUSCLE_INSERT_PREFIX(Key, Value) \
1019 { \
1020 obstack_fgrow2 (&muscle_obstack, "%s%s", spec_name_prefix, Value); \
1021 obstack_1grow (&muscle_obstack, 0); \
1022 muscle_insert (Key, obstack_finish (&muscle_obstack)); \
1023 }
1024
1025 static void
1026 prepare (void)
1027 {
1028 MUSCLE_INSERT_INT ("last", high);
1029 MUSCLE_INSERT_INT ("flag", MINSHORT);
1030 MUSCLE_INSERT_INT ("pure", pure_parser);
1031 MUSCLE_INSERT_INT ("nsym", nsyms);
1032 MUSCLE_INSERT_INT ("debug", debug_flag);
1033 MUSCLE_INSERT_INT ("final", final_state);
1034 MUSCLE_INSERT_INT ("maxtok", max_user_token_number);
1035 MUSCLE_INSERT_INT ("error-verbose", error_verbose);
1036 MUSCLE_INSERT_STRING ("prefix", spec_name_prefix);
1037
1038 MUSCLE_INSERT_INT ("nnts", nvars);
1039 MUSCLE_INSERT_INT ("nrules", nrules);
1040 MUSCLE_INSERT_INT ("nstates", nstates);
1041 MUSCLE_INSERT_INT ("ntokens", ntokens);
1042
1043 MUSCLE_INSERT_INT ("locations-flag", locations_flag);
1044 }
1045
1046
1047 /*-------------------------.
1048 | Output the header file. |
1049 `-------------------------*/
1050
1051 static void
1052 header_output (void)
1053 {
1054 FILE *out = xfopen (spec_defines_file, "w");
1055 char *macro_name = compute_header_macro ();
1056
1057 fprintf (out, "#ifndef %s\n", macro_name);
1058 fprintf (out, "# define %s\n\n", macro_name);
1059
1060 fputs (muscle_find ("tokendef"), out);
1061 fprintf (out, "\
1062 #ifndef YYSTYPE\n\
1063 typedef %s
1064 yystype;\n\
1065 # define YYSTYPE yystype\n\
1066 #endif\n",
1067 muscle_find ("stype"));
1068
1069 if (!pure_parser)
1070 fprintf (out, "\nextern YYSTYPE %slval;\n",
1071 spec_name_prefix);
1072 if (semantic_parser)
1073 {
1074 int i;
1075
1076 for (i = ntokens; i < nsyms; i++)
1077 /* don't make these for dummy nonterminals made by gensym. */
1078 if (*tags[i] != '@')
1079 fprintf (out, "# define NT%s\t%d\n", tags[i], i);
1080 }
1081
1082 fprintf (out, "\n#endif /* not %s */\n", macro_name);
1083 free (macro_name);
1084 xfclose (out);
1085 }
1086
1087
1088 /*----------------------------------------------------------.
1089 | Output the parsing tables and the parser code to ftable. |
1090 `----------------------------------------------------------*/
1091
1092 void
1093 output (void)
1094 {
1095 obstack_init (&format_obstack);
1096
1097 output_token_translations ();
1098 output_gram ();
1099
1100 XFREE (ritem);
1101 if (semantic_parser)
1102 output_stos ();
1103 output_rule_data ();
1104 XFREE (user_toknums);
1105 output_actions ();
1106
1107 prepare ();
1108 /* Copy definitions in directive. */
1109 obstack_1grow (&attrs_obstack, 0);
1110 muscle_insert ("prologue", obstack_finish (&attrs_obstack));
1111
1112 /* Output the parser. */
1113 output_master_parser ();
1114 /* Output the header if needed. */
1115 if (defines_flag)
1116 header_output ();
1117
1118 free (rule_table + 1);
1119 obstack_free (&muscle_obstack, NULL);
1120 obstack_free (&format_obstack, NULL);
1121 obstack_free (&action_obstack, NULL);
1122 obstack_free (&attrs_obstack, NULL);
1123 }