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