]> git.saurik.com Git - bison.git/blob - src/output.c
5175fb3f67afdd25c7e7fd749925d02562c2617b
[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 unsigned int **conflict_tos = NULL;
119 static short *tally = NULL;
120 static short *width = NULL;
121 static short *actrow = NULL;
122 static short *conflrow = NULL;
123 static short *state_count = NULL;
124 static short *order = NULL;
125 static short *base = NULL;
126 static short *pos = NULL;
127
128 static unsigned int *conflict_table = NULL;
129 static unsigned int *conflict_list = NULL;
130 static int conflict_list_cnt;
131 static int conflict_list_free;
132
133 /* TABLE_SIZE is the allocated size of both TABLE and CHECK.
134 We start with the original hard-coded value: SHRT_MAX
135 (yes, not USHRT_MAX). */
136 static size_t table_size = SHRT_MAX;
137 static short *table = NULL;
138 static short *check = NULL;
139 static int lowzero;
140 static int high;
141
142 static struct obstack format_obstack;
143
144 int error_verbose = 0;
145
146
147 /*----------------------------------------------------------------.
148 | If TABLE (and CHECK) appear to be small to be addressed at |
149 | DESIRED, grow them. Note that TABLE[DESIRED] is to be used, so |
150 | the desired size is at least DESIRED + 1. |
151 `----------------------------------------------------------------*/
152
153 static void
154 table_grow (size_t desired)
155 {
156 size_t old_size = table_size;
157
158 while (table_size <= desired)
159 table_size *= 2;
160
161 if (trace_flag)
162 fprintf (stderr, "growing table and check from: %d to %d\n",
163 old_size, table_size);
164
165 table = XREALLOC (table, short, table_size);
166 check = XREALLOC (check, short, table_size);
167 if (glr_parser)
168 conflict_table = XREALLOC (conflict_table, unsigned int, table_size);
169
170 for (/* Nothing. */; old_size < table_size; ++old_size)
171 {
172 table[old_size] = 0;
173 check[old_size] = -1;
174 }
175 }
176
177
178 /*-------------------------------------------------------------------.
179 | Create a function NAME which associates to the muscle NAME the |
180 | result of formatting the FIRST and then TABLE_DATA[BEGIN..END[ (of |
181 | TYPE), and to the muscle NAME_max, the max value of the |
182 | TABLE_DATA. |
183 `-------------------------------------------------------------------*/
184
185
186 #define GENERATE_MUSCLE_INSERT_TABLE(Name, Type) \
187 \
188 static void \
189 Name (const char *name, \
190 Type *table_data, \
191 Type first, \
192 int begin, \
193 int end) \
194 { \
195 Type max = first; \
196 int i; \
197 int j = 1; \
198 \
199 obstack_fgrow1 (&format_obstack, "%6d", first); \
200 for (i = begin; i < end; ++i) \
201 { \
202 obstack_1grow (&format_obstack, ','); \
203 if (j >= 10) \
204 { \
205 obstack_sgrow (&format_obstack, "\n "); \
206 j = 1; \
207 } \
208 else \
209 ++j; \
210 obstack_fgrow1 (&format_obstack, "%6d", table_data[i]); \
211 if (table_data[i] > max) \
212 max = table_data[i]; \
213 } \
214 obstack_1grow (&format_obstack, 0); \
215 muscle_insert (name, obstack_finish (&format_obstack)); \
216 \
217 /* Build `NAME_max' in the obstack. */ \
218 obstack_fgrow1 (&format_obstack, "%s_max", name); \
219 obstack_1grow (&format_obstack, 0); \
220 MUSCLE_INSERT_LONG_INT (obstack_finish (&format_obstack), \
221 (long int) max); \
222 }
223
224 GENERATE_MUSCLE_INSERT_TABLE(muscle_insert_unsigned_int_table, unsigned int)
225 GENERATE_MUSCLE_INSERT_TABLE(muscle_insert_short_table, short)
226 GENERATE_MUSCLE_INSERT_TABLE(muscle_insert_symbol_number_table, symbol_number_t)
227 GENERATE_MUSCLE_INSERT_TABLE(muscle_insert_item_number_table, item_number_t)
228
229
230 /*-----------------------------------------------------------------.
231 | Prepare the muscles related to the tokens: translate, tname, and |
232 | toknum. |
233 `-----------------------------------------------------------------*/
234
235 static void
236 prepare_tokens (void)
237 {
238 muscle_insert_symbol_number_table ("translate",
239 token_translations,
240 0, 1, max_user_token_number + 1);
241
242 {
243 int i;
244 int j = 0;
245 for (i = 0; i < nsyms; i++)
246 {
247 /* Be sure not to use twice the same QUOTEARG slot:
248 SYMBOL_TAG_GET uses slot 0. */
249 const char *cp =
250 quotearg_n_style (1, c_quoting_style,
251 symbol_tag_get (symbols[i]));
252 /* Width of the next token, including the two quotes, the coma
253 and the space. */
254 int strsize = strlen (cp) + 2;
255
256 if (j + strsize > 75)
257 {
258 obstack_sgrow (&format_obstack, "\n ");
259 j = 2;
260 }
261
262 obstack_sgrow (&format_obstack, cp);
263 obstack_sgrow (&format_obstack, ", ");
264 j += strsize;
265 }
266 /* Add a NULL entry to list of tokens (well, 0, as NULL might not be
267 defined). */
268 obstack_sgrow (&format_obstack, "0");
269
270 /* Finish table and store. */
271 obstack_1grow (&format_obstack, 0);
272 muscle_insert ("tname", obstack_finish (&format_obstack));
273 }
274
275 /* Output YYTOKNUM. */
276 {
277 int i;
278 short *values = XCALLOC (short, ntokens + 1);
279 for (i = 0; i < ntokens + 1; ++i)
280 values[i] = symbols[i]->user_token_number;
281 muscle_insert_short_table ("toknum", values,
282 0, 1, ntokens + 1);
283 free (values);
284 }
285 }
286
287
288 /*-------------------------------------------------------------.
289 | Prepare the muscles related to the rules: rhs, prhs, r1, r2, |
290 | rline, dprec, merger |
291 `-------------------------------------------------------------*/
292
293 static void
294 prepare_rules (void)
295 {
296 int r;
297 unsigned int i = 0;
298 item_number_t *rhs = XMALLOC (item_number_t, nritems);
299 unsigned int *prhs = XMALLOC (unsigned int, nrules + 1);
300 unsigned int *rline = XMALLOC (unsigned int, nrules + 1);
301 symbol_number_t *r1 = XMALLOC (symbol_number_t, nrules + 1);
302 unsigned int *r2 = XMALLOC (unsigned int, nrules + 1);
303 short *dprec = XMALLOC (short, nrules + 1);
304 short *merger = XMALLOC (short, nrules + 1);
305
306 for (r = 1; r < nrules + 1; ++r)
307 {
308 item_number_t *rhsp;
309 /* Index of rule R in RHS. */
310 prhs[r] = i;
311 /* RHS of the rule R. */
312 for (rhsp = rules[r].rhs; *rhsp >= 0; ++rhsp)
313 rhs[i++] = *rhsp;
314 /* LHS of the rule R. */
315 r1[r] = rules[r].lhs->number;
316 /* Length of rule R's RHS. */
317 r2[r] = i - prhs[r];
318 /* Separator in RHS. */
319 rhs[i++] = -1;
320 /* Line where rule was defined. */
321 rline[r] = rules[r].location.first_line;
322 /* Dynamic precedence (GLR) */
323 dprec[r] = rules[r].dprec;
324 /* Merger-function index (GLR) */
325 merger[r] = rules[r].merger;
326 }
327 assert (i == nritems);
328
329 muscle_insert_item_number_table ("rhs", rhs, ritem[0], 1, nritems);
330 muscle_insert_unsigned_int_table ("prhs", prhs, 0, 1, nrules + 1);
331 muscle_insert_unsigned_int_table ("rline", rline, 0, 1, nrules + 1);
332 muscle_insert_symbol_number_table ("r1", r1, 0, 1, nrules + 1);
333 muscle_insert_unsigned_int_table ("r2", r2, 0, 1, nrules + 1);
334 muscle_insert_short_table ("dprec", dprec, 0, 1, nrules + 1);
335 muscle_insert_short_table ("merger", merger, 0, 1, nrules + 1);
336
337 free (rhs);
338 free (prhs);
339 free (rline);
340 free (r1);
341 free (r2);
342 free (dprec);
343 free (merger);
344 }
345
346 /*--------------------------------------------.
347 | Prepare the muscles related to the states. |
348 `--------------------------------------------*/
349
350 static void
351 prepare_states (void)
352 {
353 size_t i;
354 symbol_number_t *values =
355 (symbol_number_t *) alloca (sizeof (symbol_number_t) * nstates);
356 for (i = 0; i < nstates; ++i)
357 values[i] = states[i]->accessing_symbol;
358 muscle_insert_symbol_number_table ("stos", values,
359 0, 1, nstates);
360 }
361
362
363 /*-------------------------------------------------------------------.
364 | For GLR parsers, for each conflicted token in STATE, as indicated |
365 | by non-zero entries in conflrow, create a list of possible |
366 | reductions that are alternatives to the shift or reduction |
367 | currently recorded for that token in STATE. Store the alternative |
368 | reductions followed by a 0 in conflict_list, updating |
369 | conflict_list_cnt, and storing an index to the start of the list |
370 | back into conflrow. |
371 `-------------------------------------------------------------------*/
372
373 static void
374 conflict_row (state_t *state)
375 {
376 int i, j;
377
378 if (! glr_parser)
379 return;
380
381 for (j = 0; j < ntokens; j += 1)
382 if (conflrow[j])
383 {
384 conflrow[j] = conflict_list_cnt;
385
386 /* find all reductions for token j, and record all that do
387 * not match actrow[j] */
388 for (i = 0; i < state->nlookaheads; i += 1)
389 if (bitset_test (state->lookaheads[i], j)
390 && actrow[j] != -state->lookaheads_rule[i]->number)
391 {
392 assert (conflict_list_free > 0);
393 conflict_list[conflict_list_cnt]
394 = state->lookaheads_rule[i]->number;
395 conflict_list_cnt += 1;
396 conflict_list_free -= 1;
397 }
398
399 /* Leave a 0 at the end */
400 assert (conflict_list_free > 0);
401 conflict_list_cnt += 1;
402 conflict_list_free -= 1;
403 }
404 }
405
406
407 /*------------------------------------------------------------------.
408 | Decide what to do for each type of token if seen as the lookahead |
409 | token in specified state. The value returned is used as the |
410 | default action (yydefact) for the state. In addition, actrow is |
411 | filled with what to do for each kind of token, index by symbol |
412 | number, with zero meaning do the default action. The value |
413 | SHRT_MIN, a very negative number, means this situation is an |
414 | error. The parser recognizes this value specially. |
415 | |
416 | This is where conflicts are resolved. The loop over lookahead |
417 | rules considered lower-numbered rules last, and the last rule |
418 | considered that likes a token gets to handle it. |
419 | |
420 | For GLR parsers, also sets conflrow[SYM] to an index into |
421 | conflict_list iff there is an unresolved conflict (s/r or r/r) |
422 | with symbol SYM. The default reduction is not used for a symbol |
423 | that has any such conflicts. |
424 `------------------------------------------------------------------*/
425
426 static int
427 action_row (state_t *state)
428 {
429 int i;
430 int default_rule = 0;
431 reductions *redp = state->reductions;
432 shifts *shiftp = state->shifts;
433 errs *errp = state->errs;
434 /* set nonzero to inhibit having any default reduction */
435 int nodefault = 0;
436 int conflicted = 0;
437
438 for (i = 0; i < ntokens; i++)
439 actrow[i] = conflrow[i] = 0;
440
441 if (redp->nreds >= 1)
442 {
443 int j;
444 /* loop over all the rules available here which require
445 lookahead */
446 for (i = state->nlookaheads - 1; i >= 0; --i)
447 /* and find each token which the rule finds acceptable
448 to come next */
449 for (j = 0; j < ntokens; j++)
450 /* and record this rule as the rule to use if that
451 token follows. */
452 if (bitset_test (state->lookaheads[i], j))
453 {
454 if (actrow[j] != 0)
455 conflicted = conflrow[j] = 1;
456 actrow[j] = -state->lookaheads_rule[i]->number;
457 }
458 }
459
460 /* Now see which tokens are allowed for shifts in this state. For
461 them, record the shift as the thing to do. So shift is preferred
462 to reduce. */
463 for (i = 0; i < shiftp->nshifts; i++)
464 {
465 symbol_number_t symbol;
466 int shift_state = shiftp->shifts[i];
467 if (!shift_state)
468 continue;
469
470 symbol = states[shift_state]->accessing_symbol;
471
472 if (ISVAR (symbol))
473 break;
474
475 if (actrow[symbol] != 0)
476 conflicted = conflrow[symbol] = 1;
477 actrow[symbol] = shift_state;
478
479 /* Do not use any default reduction if there is a shift for
480 error */
481 if (symbol == errtoken->number)
482 nodefault = 1;
483 }
484
485 /* See which tokens are an explicit error in this state (due to
486 %nonassoc). For them, record SHRT_MIN as the action. */
487 for (i = 0; i < errp->nerrs; i++)
488 {
489 int symbol = errp->errs[i];
490 actrow[symbol] = SHRT_MIN;
491 }
492
493 /* Now find the most common reduction and make it the default action
494 for this state. */
495
496 if (redp->nreds >= 1 && !nodefault)
497 {
498 if (state->consistent)
499 default_rule = redp->rules[0];
500 else
501 {
502 int max = 0;
503 for (i = 0; i < state->nlookaheads; i++)
504 {
505 int count = 0;
506 int rule = -state->lookaheads_rule[i]->number;
507 int j;
508
509 for (j = 0; j < ntokens; j++)
510 if (actrow[j] == rule)
511 count++;
512
513 if (count > max)
514 {
515 max = count;
516 default_rule = rule;
517 }
518 }
519
520 /* GLR parsers need space for conflict lists, so we can't
521 default conflicted entries. For non-conflicted entries
522 or as long as we are not building a GLR parser,
523 actions that match the default are replaced with zero,
524 which means "use the default". */
525
526 if (max > 0)
527 {
528 int j;
529 for (j = 0; j < ntokens; j++)
530 if (actrow[j] == default_rule && ! (glr_parser && conflrow[j]))
531 actrow[j] = 0;
532 }
533 default_rule = -default_rule;
534 }
535 }
536
537 /* If have no default rule, the default is an error.
538 So replace any action which says "error" with "use default". */
539
540 if (default_rule == 0)
541 for (i = 0; i < ntokens; i++)
542 if (actrow[i] == SHRT_MIN)
543 actrow[i] = 0;
544
545 if (conflicted)
546 conflict_row (state);
547
548 return default_rule;
549 }
550
551
552 static void
553 save_row (int state)
554 {
555 int i;
556 int count;
557 short *sp;
558 short *sp1;
559 short *sp2;
560 unsigned int *sp3;
561
562 count = 0;
563 for (i = 0; i < ntokens; i++)
564 if (actrow[i] != 0)
565 count++;
566
567 if (count == 0)
568 return;
569
570 froms[state] = sp1 = sp = XCALLOC (short, count);
571 tos[state] = sp2 = XCALLOC (short, count);
572 if (glr_parser)
573 conflict_tos[state] = sp3 = XCALLOC (unsigned int, count);
574 else
575 conflict_tos[state] = NULL;
576
577 for (i = 0; i < ntokens; i++)
578 if (actrow[i] != 0)
579 {
580 *sp1++ = i;
581 *sp2++ = actrow[i];
582 if (glr_parser)
583 *sp3++ = conflrow[i];
584 }
585
586 tally[state] = count;
587 width[state] = sp1[-1] - sp[0] + 1;
588 }
589
590
591 /*------------------------------------------------------------------.
592 | Figure out the actions for the specified state, indexed by |
593 | lookahead token type. |
594 | |
595 | The YYDEFACT table is output now. The detailed info is saved for |
596 | putting into YYTABLE later. |
597 `------------------------------------------------------------------*/
598
599 static void
600 token_actions (void)
601 {
602 size_t i;
603 int nconflict = conflicts_total_count ();
604
605 short *yydefact = XCALLOC (short, nstates);
606
607 actrow = XCALLOC (short, ntokens);
608
609 conflrow = XCALLOC (short, ntokens);
610 if (glr_parser)
611 {
612 conflict_list = XCALLOC (unsigned int, 1 + 2 * nconflict);
613 conflict_list_free = 2 * nconflict;
614 conflict_list_cnt = 1;
615 }
616 else
617 conflict_list_free = conflict_list_cnt = 0;
618
619 for (i = 0; i < nstates; ++i)
620 {
621 yydefact[i] = action_row (states[i]);
622 save_row (i);
623 }
624
625 muscle_insert_short_table ("defact", yydefact,
626 yydefact[0], 1, nstates);
627 XFREE (actrow);
628 XFREE (conflrow);
629 XFREE (yydefact);
630 }
631
632
633 /*-----------------------------.
634 | Output the actions to OOUT. |
635 `-----------------------------*/
636
637 void
638 actions_output (FILE *out)
639 {
640 int rule;
641
642 fputs ("m4_define([b4_actions], \n[[", out);
643 for (rule = 1; rule < nrules + 1; ++rule)
644 if (rules[rule].action)
645 {
646 fprintf (out, " case %d:\n", rule);
647
648 if (!no_lines_flag)
649 fprintf (out, muscle_find ("linef"),
650 rules[rule].action_location.first_line,
651 quotearg_style (c_quoting_style,
652 muscle_find ("filename")));
653 fprintf (out, " %s\n break;\n\n",
654 rules[rule].action);
655 }
656 fputs ("]])\n\n", out);
657 }
658
659 /*--------------------------------------.
660 | Output the merge functions to OUT. |
661 `--------------------------------------*/
662
663 void
664 merger_output (FILE *out)
665 {
666 int n;
667 merger_list* p;
668
669 fputs ("m4_define([b4_mergers], \n[[", out);
670 for (n = 1, p = merge_functions; p != NULL; n += 1, p = p->next)
671 {
672 if (p->type[0] == '\0')
673 fprintf (out, " case %d: yyval = %s (*yy0, *yy1); break;\n",
674 n, p->name);
675 else
676 fprintf (out, " case %d: yyval.%s = %s (*yy0, *yy1); break;\n",
677 n, p->type, p->name);
678 }
679 fputs ("]])\n\n", out);
680 }
681
682 /*---------------------------------------.
683 | Output the tokens definition to OOUT. |
684 `---------------------------------------*/
685
686 void
687 token_definitions_output (FILE *out)
688 {
689 int i;
690 int first = 1;
691
692 fputs ("m4_define([b4_tokens], \n[", out);
693 for (i = 0; i < ntokens; ++i)
694 {
695 symbol_t *symbol = symbols[i];
696 int number = symbol->user_token_number;
697
698 /* At this stage, if there are literal aliases, they are part of
699 SYMBOLS, so we should not find symbols which are the aliases
700 here. */
701 assert (number != USER_NUMBER_ALIAS);
702
703 /* Skip error token. */
704 if (symbol == errtoken)
705 continue;
706
707 /* If this string has an alias, then it is necessarily the alias
708 which is to be output. */
709 if (symbol->alias)
710 symbol = symbol->alias;
711
712 /* Don't output literal chars or strings (when defined only as a
713 string). Note that must be done after the alias resolution:
714 think about `%token 'f' "f"'. */
715 if (symbol->tag[0] == '\'' || symbol->tag[0] == '\"')
716 continue;
717
718 /* Don't #define nonliteral tokens whose names contain periods
719 or '$' (as does the default value of the EOF token). */
720 if (strchr (symbol->tag, '.') || strchr (symbol->tag, '$'))
721 continue;
722
723 fprintf (out, "%s[[[%s]], [%d]]",
724 first ? "" : ",\n", symbol->tag, number);
725
726 first = 0;
727 }
728 fputs ("])\n\n", out);
729 }
730
731
732 /*----------------------------------------.
733 | Output the symbol destructors to OOUT. |
734 `----------------------------------------*/
735
736 static void
737 symbol_destructors_output (FILE *out)
738 {
739 int i;
740 int first = 1;
741
742 fputs ("m4_define([b4_symbol_destructors], \n[", out);
743 for (i = 0; i < nsyms; ++i)
744 if (symbols[i]->destructor)
745 {
746 symbol_t *symbol = symbols[i];
747
748 /* Filename, lineno,
749 Symbol-name, Symbol-number,
750 destructor, typename. */
751 fprintf (out, "%s[[[%s]], [[%d]], [[%s]], [[%d]], [[%s]], [[%s]]]",
752 first ? "" : ",\n",
753 infile, symbol->destructor_location.first_line,
754 symbol_tag_get (symbol),
755 symbol->number,
756 symbol->destructor,
757 symbol->type_name);
758
759 first = 0;
760 }
761 fputs ("])\n\n", out);
762 }
763
764
765 /*-------------------------------------.
766 | Output the symbol printers to OOUT. |
767 `-------------------------------------*/
768
769 static void
770 symbol_printers_output (FILE *out)
771 {
772 int i;
773 int first = 1;
774
775 fputs ("m4_define([b4_symbol_printers], \n[", out);
776 for (i = 0; i < nsyms; ++i)
777 if (symbols[i]->destructor)
778 {
779 symbol_t *symbol = symbols[i];
780
781 /* Filename, lineno,
782 Symbol-name, Symbol-number,
783 destructor, typename. */
784 fprintf (out, "%s[[[%s]], [[%d]], [[%s]], [[%d]], [[%s]], [[%s]]]",
785 first ? "" : ",\n",
786 infile, symbol->printer_location.first_line,
787 symbol_tag_get (symbol),
788 symbol->number,
789 symbol->printer,
790 symbol->type_name);
791
792 first = 0;
793 }
794 fputs ("])\n\n", out);
795 }
796
797
798 static void
799 save_column (int symbol, int default_state)
800 {
801 int i;
802 short *sp;
803 short *sp1;
804 short *sp2;
805 int count;
806 int symno = symbol - ntokens + nstates;
807
808 short begin = goto_map[symbol];
809 short end = goto_map[symbol + 1];
810
811 count = 0;
812 for (i = begin; i < end; i++)
813 if (to_state[i] != default_state)
814 count++;
815
816 if (count == 0)
817 return;
818
819 froms[symno] = sp1 = sp = XCALLOC (short, count);
820 tos[symno] = sp2 = XCALLOC (short, count);
821
822 for (i = begin; i < end; i++)
823 if (to_state[i] != default_state)
824 {
825 *sp1++ = from_state[i];
826 *sp2++ = to_state[i];
827 }
828
829 tally[symno] = count;
830 width[symno] = sp1[-1] - sp[0] + 1;
831 }
832
833 static int
834 default_goto (int symbol)
835 {
836 size_t i;
837 size_t m = goto_map[symbol];
838 size_t n = goto_map[symbol + 1];
839 int default_state = -1;
840 int max = 0;
841
842 if (m == n)
843 return -1;
844
845 for (i = 0; i < nstates; i++)
846 state_count[i] = 0;
847
848 for (i = m; i < n; i++)
849 state_count[to_state[i]]++;
850
851 for (i = 0; i < nstates; i++)
852 if (state_count[i] > max)
853 {
854 max = state_count[i];
855 default_state = i;
856 }
857
858 return default_state;
859 }
860
861
862 /*-------------------------------------------------------------------.
863 | Figure out what to do after reducing with each rule, depending on |
864 | the saved state from before the beginning of parsing the data that |
865 | matched this rule. |
866 | |
867 | The YYDEFGOTO table is output now. The detailed info is saved for |
868 | putting into YYTABLE later. |
869 `-------------------------------------------------------------------*/
870
871 static void
872 goto_actions (void)
873 {
874 int i;
875 short *yydefgoto = XMALLOC (short, nsyms - ntokens);
876
877 state_count = XCALLOC (short, nstates);
878 for (i = ntokens; i < nsyms; ++i)
879 {
880 int default_state = default_goto (i);
881 save_column (i, default_state);
882 yydefgoto[i - ntokens] = default_state;
883 }
884
885 muscle_insert_short_table ("defgoto", yydefgoto,
886 yydefgoto[0], 1, nsyms - ntokens);
887 XFREE (state_count);
888 XFREE (yydefgoto);
889 }
890
891
892 /* The next few functions decide how to pack the actions and gotos
893 information into yytable. */
894
895 static void
896 sort_actions (void)
897 {
898 int i;
899
900 order = XCALLOC (short, nvectors);
901 nentries = 0;
902
903 for (i = 0; i < nvectors; i++)
904 if (tally[i] > 0)
905 {
906 int k;
907 int t = tally[i];
908 int w = width[i];
909 int j = nentries - 1;
910
911 while (j >= 0 && (width[order[j]] < w))
912 j--;
913
914 while (j >= 0 && (width[order[j]] == w) && (tally[order[j]] < t))
915 j--;
916
917 for (k = nentries - 1; k > j; k--)
918 order[k + 1] = order[k];
919
920 order[j + 1] = i;
921 nentries++;
922 }
923 }
924
925
926 static int
927 matching_state (int vector)
928 {
929 int i = order[vector];
930 int t;
931 int w;
932 int prev;
933
934 if (i >= (int) nstates)
935 return -1;
936
937 t = tally[i];
938 w = width[i];
939
940 for (prev = vector - 1; prev >= 0; prev--)
941 {
942 int j = order[prev];
943 int k;
944 int match = 1;
945
946 if (width[j] != w || tally[j] != t)
947 return -1;
948
949 for (k = 0; match && k < t; k++)
950 if (tos[j][k] != tos[i][k] || froms[j][k] != froms[i][k])
951 match = 0;
952
953 if (match)
954 return j;
955 }
956
957 return -1;
958 }
959
960
961 static int
962 pack_vector (int vector)
963 {
964 int i = order[vector];
965 int j;
966 int t = tally[i];
967 int loc = 0;
968 short *from = froms[i];
969 short *to = tos[i];
970 unsigned int *conflict_to = conflict_tos[i];
971
972 assert (t);
973
974 for (j = lowzero - from[0]; j < (int) table_size; j++)
975 {
976 int k;
977 int ok = 1;
978
979 for (k = 0; ok && k < t; k++)
980 {
981 loc = j + from[k];
982 if (loc > (int) table_size)
983 table_grow (loc);
984
985 if (table[loc] != 0)
986 ok = 0;
987 }
988
989 for (k = 0; ok && k < vector; k++)
990 if (pos[k] == j)
991 ok = 0;
992
993 if (ok)
994 {
995 for (k = 0; k < t; k++)
996 {
997 loc = j + from[k];
998 table[loc] = to[k];
999 if (glr_parser && conflict_to != NULL)
1000 conflict_table[loc] = conflict_to[k];
1001 check[loc] = from[k];
1002 }
1003
1004 while (table[lowzero] != 0)
1005 lowzero++;
1006
1007 if (loc > high)
1008 high = loc;
1009
1010 return j;
1011 }
1012 }
1013 #define pack_vector_succeeded 0
1014 assert (pack_vector_succeeded);
1015 return 0;
1016 }
1017
1018
1019 static void
1020 pack_table (void)
1021 {
1022 int i;
1023 int place;
1024 int state;
1025
1026 base = XCALLOC (short, nvectors);
1027 pos = XCALLOC (short, nentries);
1028 table = XCALLOC (short, table_size);
1029 if (glr_parser)
1030 conflict_table = XCALLOC (unsigned int, table_size);
1031 check = XCALLOC (short, table_size);
1032
1033 lowzero = 0;
1034 high = 0;
1035
1036 for (i = 0; i < nvectors; i++)
1037 base[i] = SHRT_MIN;
1038
1039 for (i = 0; i < (int) table_size; i++)
1040 check[i] = -1;
1041
1042 for (i = 0; i < nentries; i++)
1043 {
1044 state = matching_state (i);
1045
1046 if (state < 0)
1047 place = pack_vector (i);
1048 else
1049 place = base[state];
1050
1051 pos[i] = place;
1052 base[order[i]] = place;
1053 }
1054
1055 for (i = 0; i < nvectors; i++)
1056 {
1057 XFREE (froms[i]);
1058 XFREE (tos[i]);
1059 XFREE (conflict_tos[i]);
1060 }
1061
1062 XFREE (froms);
1063 XFREE (tos);
1064 XFREE (conflict_tos);
1065 XFREE (pos);
1066 }
1067
1068 /* the following functions output yytable, yycheck, yyconflp, yyconfl,
1069 and the vectors whose elements index the portion starts */
1070
1071 static void
1072 output_base (void)
1073 {
1074 /* Output pact. */
1075 muscle_insert_short_table ("pact", base,
1076 base[0], 1, nstates);
1077
1078 /* Output pgoto. */
1079 muscle_insert_short_table ("pgoto", base,
1080 base[nstates], nstates + 1, nvectors);
1081 XFREE (base);
1082 }
1083
1084
1085 static void
1086 output_table (void)
1087 {
1088 muscle_insert_short_table ("table", table,
1089 table[0], 1, high + 1);
1090 XFREE (table);
1091 }
1092
1093
1094 static void
1095 output_conflicts (void)
1096 {
1097 /* GLR parsing slightly modifies yytable and yycheck
1098 (and thus yypact) so that in states with unresolved conflicts,
1099 the default reduction is not used in the conflicted entries, so
1100 that there is a place to put a conflict pointer. This means that
1101 yyconflp and yyconfl are nonsense for a non-GLR parser, so we
1102 avoid accidents by not writing them out in that case. */
1103 if (! glr_parser)
1104 return;
1105
1106 muscle_insert_unsigned_int_table ("conflict_list_heads", conflict_table,
1107 conflict_table[0], 1, high+1);
1108 muscle_insert_unsigned_int_table ("conflicting_rules", conflict_list,
1109 conflict_list[0], 1, conflict_list_cnt);
1110
1111 XFREE (conflict_table);
1112 XFREE (conflict_list);
1113 }
1114
1115
1116 static void
1117 output_check (void)
1118 {
1119 muscle_insert_short_table ("check", check,
1120 check[0], 1, high + 1);
1121 XFREE (check);
1122 }
1123
1124 /*-----------------------------------------------------------------.
1125 | Compute and output yydefact, yydefgoto, yypact, yypgoto, yytable |
1126 | and yycheck. |
1127 `-----------------------------------------------------------------*/
1128
1129 static void
1130 output_actions (void)
1131 {
1132 size_t i;
1133 nvectors = nstates + nvars;
1134
1135 froms = XCALLOC (short *, nvectors);
1136 tos = XCALLOC (short *, nvectors);
1137 conflict_tos = XCALLOC (unsigned int *, nvectors);
1138 tally = XCALLOC (short, nvectors);
1139 width = XCALLOC (short, nvectors);
1140
1141 token_actions ();
1142 bitsetv_free (LA);
1143 free (LArule);
1144
1145 goto_actions ();
1146 XFREE (goto_map + ntokens);
1147 XFREE (from_state);
1148 XFREE (to_state);
1149
1150 sort_actions ();
1151 pack_table ();
1152
1153 output_base ();
1154 output_table ();
1155 output_conflicts ();
1156
1157 output_check ();
1158
1159 for (i = 0; i < nstates; ++i)
1160 {
1161 free (states[i]->shifts);
1162 XFREE (states[i]->reductions);
1163 free (states[i]->errs);
1164 free (states[i]);
1165 }
1166 XFREE (states);
1167 }
1168
1169 \f
1170 /*----------------------.
1171 | Run our backend, M4. |
1172 `----------------------*/
1173
1174 static void
1175 m4_invoke (const char *definitions)
1176 {
1177 /* Invoke m4 on the definition of the muscles, and the skeleton. */
1178 const char *bison_pkgdatadir = getenv ("BISON_PKGDATADIR");
1179 const char *m4 = getenv ("M4");
1180 int pkg_data_len;
1181 char *full_skeleton;
1182
1183 if (!m4)
1184 m4 = M4;
1185 if (!bison_pkgdatadir)
1186 bison_pkgdatadir = PKGDATADIR;
1187 pkg_data_len = strlen (bison_pkgdatadir);
1188 full_skeleton = XMALLOC (char, pkg_data_len + strlen (skeleton) + 2);
1189 if (bison_pkgdatadir[pkg_data_len-1] == '/')
1190 sprintf (full_skeleton, "%s%s", bison_pkgdatadir, skeleton);
1191 else
1192 sprintf (full_skeleton, "%s/%s", bison_pkgdatadir, skeleton);
1193 if (trace_flag)
1194 fprintf (stderr,
1195 "running: %s -I %s m4sugar/m4sugar.m4 %s %s\n",
1196 m4, bison_pkgdatadir, definitions, full_skeleton);
1197 skel_in = readpipe (m4,
1198 "-I", bison_pkgdatadir,
1199 "m4sugar/m4sugar.m4",
1200 definitions,
1201 full_skeleton,
1202 NULL);
1203 XFREE (full_skeleton);
1204 if (!skel_in)
1205 error (EXIT_FAILURE, errno, "cannot run m4");
1206 skel_lex ();
1207
1208 }
1209
1210 /*---------------------------.
1211 | Call the skeleton parser. |
1212 `---------------------------*/
1213
1214 static void
1215 output_skeleton (void)
1216 {
1217 /* Store the definition of all the muscles. */
1218 const char *tempdir = getenv ("TMPDIR");
1219 char *tempfile = NULL;
1220 FILE *out = NULL;
1221 int fd;
1222
1223 if (tempdir == NULL)
1224 tempdir = DEFAULT_TMPDIR;
1225 tempfile = xmalloc (strlen (tempdir) + 11);
1226 sprintf (tempfile, "%s/bsnXXXXXX", tempdir);
1227 fd = mkstemp (tempfile);
1228 if (fd == -1)
1229 error (EXIT_FAILURE, errno, "%s", tempfile);
1230
1231 out = fdopen (fd, "w");
1232 if (out == NULL)
1233 error (EXIT_FAILURE, errno, "%s", tempfile);
1234
1235 /* There are no comments, especially not `#': we do want M4 expansion
1236 after `#': think of CPP macros! */
1237 fputs ("m4_changecom()\n", out);
1238 fputs ("m4_init()\n", out);
1239
1240 actions_output (out);
1241 merger_output (out);
1242 token_definitions_output (out);
1243 symbol_destructors_output (out);
1244 symbol_printers_output (out);
1245
1246 muscles_m4_output (out);
1247
1248 fputs ("m4_wrap([m4_divert_pop(0)])\n", out);
1249 fputs ("m4_divert_push(0)dnl\n", out);
1250 xfclose (out);
1251
1252 m4_invoke (tempfile);
1253
1254 /* If `debugging', keep this file alive. */
1255 if (!trace_flag)
1256 unlink (tempfile);
1257
1258 free (tempfile);
1259 }
1260
1261 static void
1262 prepare (void)
1263 {
1264 MUSCLE_INSERT_INT ("last", high);
1265 MUSCLE_INSERT_INT ("flag", SHRT_MIN);
1266 MUSCLE_INSERT_INT ("pure", pure_parser);
1267 MUSCLE_INSERT_INT ("nsym", nsyms);
1268 MUSCLE_INSERT_INT ("debug", debug_flag);
1269 MUSCLE_INSERT_INT ("final", final_state);
1270 MUSCLE_INSERT_INT ("undef_token_number", undeftoken->number);
1271 MUSCLE_INSERT_INT ("user_token_number_max", max_user_token_number);
1272 MUSCLE_INSERT_INT ("error_verbose", error_verbose);
1273 MUSCLE_INSERT_STRING ("prefix", spec_name_prefix ? spec_name_prefix : "yy");
1274
1275 /* FIXME: This is wrong: the muscles should decide whether they hold
1276 a copy or not, but the situation is too obscure currently. */
1277 MUSCLE_INSERT_STRING ("output_infix", output_infix ? output_infix : "");
1278 MUSCLE_INSERT_STRING ("output_prefix", short_base_name);
1279 MUSCLE_INSERT_STRING ("output_parser_name", parser_file_name);
1280 MUSCLE_INSERT_STRING ("output_header_name", spec_defines_file);
1281
1282 MUSCLE_INSERT_INT ("nnts", nvars);
1283 MUSCLE_INSERT_INT ("nrules", nrules);
1284 MUSCLE_INSERT_INT ("nstates", nstates);
1285 MUSCLE_INSERT_INT ("ntokens", ntokens);
1286
1287 MUSCLE_INSERT_INT ("locations_flag", locations_flag);
1288 MUSCLE_INSERT_INT ("defines_flag", defines_flag);
1289
1290 /* Copy definitions in directive. */
1291 obstack_1grow (&pre_prologue_obstack, 0);
1292 obstack_1grow (&post_prologue_obstack, 0);
1293 muscle_insert ("pre_prologue", obstack_finish (&pre_prologue_obstack));
1294 muscle_insert ("post_prologue", obstack_finish (&post_prologue_obstack));
1295
1296 /* Find the right skeleton file. */
1297 if (!skeleton)
1298 {
1299 if (glr_parser)
1300 skeleton = "glr.c";
1301 else
1302 skeleton = "yacc.c";
1303 }
1304
1305 /* Parse the skeleton file and output the needed parsers. */
1306 muscle_insert ("skeleton", skeleton);
1307 }
1308
1309
1310 /*----------------------------------------------------------.
1311 | Output the parsing tables and the parser code to ftable. |
1312 `----------------------------------------------------------*/
1313
1314 void
1315 output (void)
1316 {
1317 obstack_init (&format_obstack);
1318
1319 prepare_tokens ();
1320 prepare_rules ();
1321 prepare_states ();
1322 output_actions ();
1323
1324 prepare ();
1325
1326 /* Process the selected skeleton file. */
1327 output_skeleton ();
1328
1329 obstack_free (&format_obstack, NULL);
1330 obstack_free (&pre_prologue_obstack, NULL);
1331 obstack_free (&post_prologue_obstack, NULL);
1332 }