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