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