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