cex: send traces to stderr, not stdout
[bison.git] / src / output.c
blob753b1bd7f79d79c7642a32a96cf67207a283cc52
1 /* Output the generated parsing program for Bison.
3 Copyright (C) 1984, 1986, 1989, 1992, 2000-2015, 2018-2020 Free
4 Software Foundation, Inc.
6 This file is part of Bison, the GNU Compiler Compiler.
8 This program is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21 #include <config.h>
22 #include "system.h"
24 #include <filename.h> /* IS_PATH_WITH_DIR */
25 #include <get-errno.h>
26 #include <mbswidth.h>
27 #include <path-join.h>
28 #include <quotearg.h>
29 #include <spawn-pipe.h>
30 #include <timevar.h>
31 #include <wait-process.h>
33 #include "complain.h"
34 #include "files.h"
35 #include "getargs.h"
36 #include "gram.h"
37 #include "muscle-tab.h"
38 #include "output.h"
39 #include "reader.h"
40 #include "reduce.h"
41 #include "scan-code.h" /* max_left_semantic_context */
42 #include "scan-skel.h"
43 #include "symtab.h"
44 #include "tables.h"
45 #include "strversion.h"
47 static struct obstack format_obstack;
50 /*-------------------------------------------------------------------.
51 | Create a function NAME which associates to the muscle NAME the |
52 | result of formatting the FIRST and then TABLE_DATA[BEGIN..END[ (of |
53 | TYPE), and to the muscle NAME_max, the max value of the |
54 | TABLE_DATA. |
55 | |
56 | For the typical case of outputting a complete table from 0, pass |
57 | TABLE[0] as FIRST, and 1 as BEGIN. For instance |
58 | muscle_insert_base_table ("pact", base, base[0], 1, nstates); |
59 `-------------------------------------------------------------------*/
62 #define GENERATE_MUSCLE_INSERT_TABLE(Name, Type) \
64 static void \
65 Name (char const *name, Type *table_data, Type first, \
66 int begin, int end) \
67 { \
68 Type min = first; \
69 Type max = first; \
70 int j = 1; \
72 obstack_printf (&format_obstack, "%6d", first); \
73 for (int i = begin; i < end; ++i) \
74 { \
75 obstack_1grow (&format_obstack, ','); \
76 if (j >= 10) \
77 { \
78 obstack_sgrow (&format_obstack, "\n "); \
79 j = 1; \
80 } \
81 else \
82 ++j; \
83 obstack_printf (&format_obstack, "%6d", table_data[i]); \
84 if (table_data[i] < min) \
85 min = table_data[i]; \
86 if (max < table_data[i]) \
87 max = table_data[i]; \
88 } \
89 muscle_insert (name, obstack_finish0 (&format_obstack)); \
91 long lmin = min; \
92 long lmax = max; \
93 /* Build 'NAME_min' and 'NAME_max' in the obstack. */ \
94 obstack_printf (&format_obstack, "%s_min", name); \
95 MUSCLE_INSERT_LONG_INT (obstack_finish0 (&format_obstack), lmin); \
96 obstack_printf (&format_obstack, "%s_max", name); \
97 MUSCLE_INSERT_LONG_INT (obstack_finish0 (&format_obstack), lmax); \
100 GENERATE_MUSCLE_INSERT_TABLE (muscle_insert_int_table, int)
101 GENERATE_MUSCLE_INSERT_TABLE (muscle_insert_base_table, base_number)
102 GENERATE_MUSCLE_INSERT_TABLE (muscle_insert_rule_number_table, rule_number)
103 GENERATE_MUSCLE_INSERT_TABLE (muscle_insert_symbol_number_table, symbol_number)
104 GENERATE_MUSCLE_INSERT_TABLE (muscle_insert_item_number_table, item_number)
105 GENERATE_MUSCLE_INSERT_TABLE (muscle_insert_state_number_table, state_number)
107 /*----------------------------------------------------------------.
108 | Print to OUT a representation of CP quoted and escaped for M4. |
109 `----------------------------------------------------------------*/
111 static void
112 output_escaped (FILE *out, const char *cp)
114 for (; *cp; cp++)
115 switch (*cp)
117 case '$': fputs ("$][", out); break;
118 case '@': fputs ("@@", out); break;
119 case '[': fputs ("@{", out); break;
120 case ']': fputs ("@}", out); break;
121 default: fputc (*cp, out); break;
125 static void
126 output_quoted (FILE *out, char const *cp)
128 fprintf (out, "[[");
129 output_escaped (out, cp);
130 fprintf (out, "]]");
133 /*----------------------------------------------------------------.
134 | Print to OUT a representation of STRING quoted and escaped both |
135 | for C and M4. |
136 `----------------------------------------------------------------*/
138 static void
139 string_output (FILE *out, char const *string)
141 output_quoted (out, quotearg_style (c_quoting_style, string));
145 /* Store in BUFFER a copy of SRC where trigraphs are escaped, return
146 the size of the result (including the final NUL). If called with
147 BUFFERSIZE = 0, returns the needed size for BUFFER. */
148 static ptrdiff_t
149 escape_trigraphs (char *buffer, ptrdiff_t buffersize, const char *src)
151 #define STORE(c) \
152 do \
154 if (res < buffersize) \
155 buffer[res] = (c); \
156 ++res; \
158 while (0)
159 ptrdiff_t res = 0;
160 for (ptrdiff_t i = 0, len = strlen (src); i < len; ++i)
162 if (i + 2 < len
163 && src[i] == '?' && src[i+1] == '?')
165 switch (src[i+2])
167 case '!': case '\'':
168 case '(': case ')': case '-': case '/':
169 case '<': case '=': case '>':
170 i += 1;
171 STORE ('?');
172 STORE ('"');
173 STORE ('"');
174 STORE ('?');
175 continue;
178 STORE (src[i]);
180 STORE ('\0');
181 #undef STORE
182 return res;
185 /* Same as xstrdup, except that trigraphs are escaped. */
186 static char *
187 xescape_trigraphs (const char *src)
189 ptrdiff_t bufsize = escape_trigraphs (NULL, 0, src);
190 char *buf = xcharalloc (bufsize);
191 escape_trigraphs (buf, bufsize, src);
192 return buf;
195 /* The tag to show in the generated parsers. Use "end of file" rather
196 than "$end". But keep "$end" in the reports, it's shorter and more
197 consistent. Support i18n if the user already uses it. */
198 static const char *
199 symbol_tag (const symbol *sym)
201 const bool eof_is_user_defined
202 = !eoftoken->alias || STRNEQ (eoftoken->alias->tag, "$end");
204 if (!eof_is_user_defined && sym->content == eoftoken->content)
205 return "\"end of file\"";
206 else if (sym->content == undeftoken->content)
207 return "\"invalid token\"";
208 else
209 return sym->tag;
212 /* Generate the b4_<MUSCLE_NAME> (e.g., b4_tname) table with the
213 symbol names (aka tags). */
215 static void
216 prepare_symbol_names (char const *muscle_name)
218 // Whether to add a pair of quotes around the name.
219 const bool quote = STREQ (muscle_name, "tname");
220 bool has_translations = false;
222 /* We assume that the table will be output starting at column 2. */
223 int col = 2;
224 struct quoting_options *qo = clone_quoting_options (0);
225 set_quoting_style (qo, c_quoting_style);
226 set_quoting_flags (qo, QA_SPLIT_TRIGRAPHS);
227 for (int i = 0; i < nsyms; i++)
229 const char *tag = symbol_tag (symbols[i]);
230 bool translatable = !quote && symbols[i]->translatable;
231 if (translatable)
232 has_translations = true;
234 char *cp
235 = tag[0] == '"' && !quote
236 ? xescape_trigraphs (tag)
237 : quotearg_alloc (tag, -1, qo);
238 /* Width of the next token, including the two quotes, the
239 comma and the space. */
240 int width
241 = mbswidth (cp, 0) + 2
242 + (translatable ? strlen ("N_()") : 0);
244 if (col + width > 75)
246 obstack_sgrow (&format_obstack, "\n ");
247 col = 1;
250 if (i)
251 obstack_1grow (&format_obstack, ' ');
252 if (translatable)
253 obstack_sgrow (&format_obstack, "]b4_symbol_translate""([");
254 obstack_escape (&format_obstack, cp);
255 if (translatable)
256 obstack_sgrow (&format_obstack, "])[");
257 free (cp);
258 obstack_1grow (&format_obstack, ',');
259 col += width;
261 free (qo);
262 obstack_sgrow (&format_obstack, " ]b4_null[");
264 /* Finish table and store. */
265 muscle_insert (muscle_name, obstack_finish0 (&format_obstack));
267 /* Announce whether translation support is needed. */
268 MUSCLE_INSERT_BOOL ("has_translations_flag", has_translations);
272 /*------------------------------------------------------------------.
273 | Prepare the muscles related to the symbols: translate, tname, and |
274 | toknum. |
275 `------------------------------------------------------------------*/
277 static void
278 prepare_symbols (void)
280 MUSCLE_INSERT_INT ("tokens_number", ntokens);
281 MUSCLE_INSERT_INT ("nterms_number", nnterms);
282 MUSCLE_INSERT_INT ("symbols_number", nsyms);
283 MUSCLE_INSERT_INT ("code_max", max_code);
285 muscle_insert_symbol_number_table ("translate",
286 token_translations,
287 token_translations[0],
288 1, max_code + 1);
290 /* tname -- token names. */
291 prepare_symbol_names ("tname");
292 prepare_symbol_names ("symbol_names");
294 /* translatable -- whether a token is translatable. */
296 bool translatable = false;
297 for (int i = 0; i < ntokens; ++i)
298 if (symbols[i]->translatable)
300 translatable = true;
301 break;
303 if (translatable)
305 int *values = xnmalloc (nsyms, sizeof *values);
306 for (int i = 0; i < ntokens; ++i)
307 values[i] = symbols[i]->translatable;
308 muscle_insert_int_table ("translatable", values,
309 values[0], 1, ntokens);
310 free (values);
314 /* Output YYTOKNUM. */
316 int *values = xnmalloc (ntokens, sizeof *values);
317 for (int i = 0; i < ntokens; ++i)
318 values[i] = symbols[i]->content->code;
319 muscle_insert_int_table ("toknum", values,
320 values[0], 1, ntokens);
321 free (values);
326 /*-------------------------------------------------------------.
327 | Prepare the muscles related to the rules: rhs, prhs, r1, r2, |
328 | rline, dprec, merger, immediate. |
329 `-------------------------------------------------------------*/
331 static void
332 prepare_rules (void)
334 int *prhs = xnmalloc (nrules, sizeof *prhs);
335 item_number *rhs = xnmalloc (nritems, sizeof *rhs);
336 int *rline = xnmalloc (nrules, sizeof *rline);
337 symbol_number *r1 = xnmalloc (nrules, sizeof *r1);
338 int *r2 = xnmalloc (nrules, sizeof *r2);
339 int *dprec = xnmalloc (nrules, sizeof *dprec);
340 int *merger = xnmalloc (nrules, sizeof *merger);
341 int *immediate = xnmalloc (nrules, sizeof *immediate);
343 /* Index in RHS. */
344 int i = 0;
345 for (rule_number r = 0; r < nrules; ++r)
347 /* Index of rule R in RHS. */
348 prhs[r] = i;
349 /* RHS of the rule R. */
350 for (item_number *rhsp = rules[r].rhs; 0 <= *rhsp; ++rhsp)
351 rhs[i++] = *rhsp;
352 /* Separator in RHS. */
353 rhs[i++] = -1;
355 /* Line where rule was defined. */
356 rline[r] = rules[r].location.start.line;
357 /* LHS of the rule R. */
358 r1[r] = rules[r].lhs->number;
359 /* Length of rule R's RHS. */
360 r2[r] = rule_rhs_length (&rules[r]);
361 /* Dynamic precedence (GLR). */
362 dprec[r] = rules[r].dprec;
363 /* Merger-function index (GLR). */
364 merger[r] = rules[r].merger;
365 /* Immediate reduction flags (GLR). */
366 immediate[r] = rules[r].is_predicate;
368 aver (i == nritems);
370 muscle_insert_item_number_table ("rhs", rhs, ritem[0], 1, nritems);
371 muscle_insert_int_table ("prhs", prhs, 0, 0, nrules);
372 muscle_insert_int_table ("rline", rline, 0, 0, nrules);
373 muscle_insert_symbol_number_table ("r1", r1, 0, 0, nrules);
374 muscle_insert_int_table ("r2", r2, 0, 0, nrules);
375 muscle_insert_int_table ("dprec", dprec, 0, 0, nrules);
376 muscle_insert_int_table ("merger", merger, 0, 0, nrules);
377 muscle_insert_int_table ("immediate", immediate, 0, 0, nrules);
379 MUSCLE_INSERT_INT ("rules_number", nrules);
380 MUSCLE_INSERT_INT ("max_left_semantic_context", max_left_semantic_context);
382 free (prhs);
383 free (rhs);
384 free (rline);
385 free (r1);
386 free (r2);
387 free (dprec);
388 free (merger);
389 free (immediate);
392 /*--------------------------------------------.
393 | Prepare the muscles related to the states. |
394 `--------------------------------------------*/
396 static void
397 prepare_states (void)
399 symbol_number *values = xnmalloc (nstates, sizeof *values);
400 for (state_number i = 0; i < nstates; ++i)
401 values[i] = states[i]->accessing_symbol;
402 muscle_insert_symbol_number_table ("stos", values,
403 0, 1, nstates);
404 free (values);
406 MUSCLE_INSERT_INT ("last", high);
407 MUSCLE_INSERT_INT ("final_state_number", final_state->number);
408 MUSCLE_INSERT_INT ("states_number", nstates);
412 /*-------------------------------------------------------.
413 | Compare two symbols by type-name, and then by number. |
414 `-------------------------------------------------------*/
416 static int
417 symbol_type_name_cmp (const symbol **lhs, const symbol **rhs)
419 int res = uniqstr_cmp ((*lhs)->content->type_name, (*rhs)->content->type_name);
420 if (!res)
421 res = (*lhs)->content->number - (*rhs)->content->number;
422 return res;
426 /*----------------------------------------------------------------.
427 | Return a (malloc'ed) table of the symbols sorted by type-name. |
428 `----------------------------------------------------------------*/
430 static symbol **
431 symbols_by_type_name (void)
433 typedef int (*qcmp_type) (const void *, const void *);
434 symbol **res = xmemdup (symbols, nsyms * sizeof *res);
435 qsort (res, nsyms, sizeof *res, (qcmp_type) &symbol_type_name_cmp);
436 return res;
440 /*------------------------------------------------------------------.
441 | Define b4_type_names, which is a list of (lists of the numbers of |
442 | symbols with same type-name). |
443 `------------------------------------------------------------------*/
445 static void
446 type_names_output (FILE *out)
448 symbol **syms = symbols_by_type_name ();
449 fputs ("m4_define([b4_type_names],\n[", out);
450 for (int i = 0; i < nsyms; /* nothing */)
452 /* The index of the first symbol of the current type-name. */
453 int i0 = i;
454 fputs (i ? ",\n[" : "[", out);
455 for (; i < nsyms
456 && syms[i]->content->type_name == syms[i0]->content->type_name; ++i)
457 fprintf (out, "%s%d", i != i0 ? ", " : "", syms[i]->content->number);
458 fputs ("]", out);
460 fputs ("])\n\n", out);
461 free (syms);
465 /*-------------------------------------.
466 | The list of all the symbol numbers. |
467 `-------------------------------------*/
469 static void
470 symbol_numbers_output (FILE *out)
472 fputs ("m4_define([b4_symbol_numbers],\n[", out);
473 for (int i = 0; i < nsyms; ++i)
474 fprintf (out, "%s[%d]", i ? ", " : "", i);
475 fputs ("])\n\n", out);
479 /*-------------------------------------------.
480 | Output the user reduction actions to OUT. |
481 `-------------------------------------------*/
483 static void
484 rule_output (const rule *r, FILE *out)
486 output_escaped (out, r->lhs->symbol->tag);
487 fputc (':', out);
488 if (0 <= *r->rhs)
489 for (item_number *rhsp = r->rhs; 0 <= *rhsp; ++rhsp)
491 fputc (' ', out);
492 output_escaped (out, symbols[*rhsp]->tag);
494 else
495 fputs (" %empty", out);
498 static void
499 user_actions_output (FILE *out)
501 fputs ("m4_define([b4_actions], \n[", out);
502 for (rule_number r = 0; r < nrules; ++r)
503 if (rules[r].action)
505 /* The useless "" is there to pacify syntax-check. */
506 fprintf (out, "%s""(%d, [",
507 rules[r].is_predicate ? "b4_predicate_case" : "b4_case",
508 r + 1);
509 if (!no_lines_flag)
511 fprintf (out, "b4_syncline(%d, ",
512 rules[r].action_loc.start.line);
513 string_output (out, rules[r].action_loc.start.file);
514 fprintf (out, ")dnl\n");
516 fprintf (out, "[%*s%s]],\n[[",
517 rules[r].action_loc.start.column - 1, "",
518 rules[r].action);
519 rule_output (&rules[r], out);
520 fprintf (out, "]])\n\n");
522 fputs ("])\n\n", out);
525 /*------------------------------------.
526 | Output the merge functions to OUT. |
527 `------------------------------------*/
529 static void
530 merger_output (FILE *out)
532 fputs ("m4_define([b4_mergers], \n[[", out);
533 int n;
534 merger_list* p;
535 for (n = 1, p = merge_functions; p != NULL; n += 1, p = p->next)
537 if (p->type[0] == '\0')
538 fprintf (out, " case %d: *yy0 = %s (*yy0, *yy1); break;\n",
539 n, p->name);
540 else
541 fprintf (out, " case %d: yy0->%s = %s (*yy0, *yy1); break;\n",
542 n, p->type, p->name);
544 fputs ("]])\n\n", out);
548 /*---------------------------------------------.
549 | Prepare the muscles for symbol definitions. |
550 `---------------------------------------------*/
552 static void
553 prepare_symbol_definitions (void)
555 /* Map "orig NUM" to new numbers. See data/README. */
556 for (symbol_number i = ntokens; i < nsyms + nuseless_nonterminals; ++i)
558 obstack_printf (&format_obstack, "symbol""(orig %d, number)", i);
559 const char *key = obstack_finish0 (&format_obstack);
560 MUSCLE_INSERT_INT (key, nterm_map ? nterm_map[i - ntokens] : i);
563 for (int i = 0; i < nsyms; ++i)
565 symbol *sym = symbols[i];
566 const char *key;
568 #define SET_KEY(Entry) \
569 obstack_printf (&format_obstack, "symbol""(%d, %s)", \
570 i, Entry); \
571 key = obstack_finish0 (&format_obstack);
573 #define SET_KEY2(Entry, Suffix) \
574 obstack_printf (&format_obstack, "symbol""(%d, %s_%s)", \
575 i, Entry, Suffix); \
576 key = obstack_finish0 (&format_obstack);
578 /* Whether the symbol has an identifier. */
579 const char *id = symbol_id_get (sym);
580 SET_KEY ("has_id");
581 MUSCLE_INSERT_INT (key, !!id);
583 /* Its identifier. */
584 SET_KEY ("id");
585 MUSCLE_INSERT_STRING (key, id ? id : "");
587 /* Its tag. Typically for documentation purpose. */
588 SET_KEY ("tag");
589 MUSCLE_INSERT_STRING (key, symbol_tag (sym));
591 SET_KEY ("code");
592 MUSCLE_INSERT_INT (key, sym->content->code);
594 SET_KEY ("is_token");
595 MUSCLE_INSERT_INT (key, i < ntokens);
597 SET_KEY ("number");
598 MUSCLE_INSERT_INT (key, sym->content->number);
600 SET_KEY ("has_type");
601 MUSCLE_INSERT_INT (key, !!sym->content->type_name);
603 SET_KEY ("type");
604 MUSCLE_INSERT_STRING (key, sym->content->type_name
605 ? sym->content->type_name : "");
607 for (int j = 0; j < CODE_PROPS_SIZE; ++j)
609 /* "printer", not "%printer". */
610 char const *pname = code_props_type_string (j) + 1;
611 code_props const *p = symbol_code_props_get (sym, j);
612 SET_KEY2 ("has", pname);
613 MUSCLE_INSERT_INT (key, !!p->code);
615 if (p->code)
617 SET_KEY2 (pname, "file");
618 MUSCLE_INSERT_C_STRING (key, p->location.start.file);
620 SET_KEY2 (pname, "line");
621 MUSCLE_INSERT_INT (key, p->location.start.line);
623 SET_KEY2 (pname, "loc");
624 muscle_location_grow (key, p->location);
626 SET_KEY (pname);
627 obstack_printf (&muscle_obstack,
628 "%*s%s", p->location.start.column - 1, "", p->code);
629 muscle_insert (key, obstack_finish0 (&muscle_obstack));
632 #undef SET_KEY2
633 #undef SET_KEY
638 static void
639 prepare_actions (void)
641 /* Figure out the actions for the specified state. */
642 muscle_insert_rule_number_table ("defact", yydefact,
643 yydefact[0], 1, nstates);
645 /* Figure out what to do after reducing with each rule, depending on
646 the saved state from before the beginning of parsing the data
647 that matched this rule. */
648 muscle_insert_state_number_table ("defgoto", yydefgoto,
649 yydefgoto[0], 1, nsyms - ntokens);
652 /* Output PACT. */
653 muscle_insert_base_table ("pact", base,
654 base[0], 1, nstates);
655 MUSCLE_INSERT_INT ("pact_ninf", base_ninf);
657 /* Output PGOTO. */
658 muscle_insert_base_table ("pgoto", base,
659 base[nstates], nstates + 1, nvectors);
661 muscle_insert_base_table ("table", table,
662 table[0], 1, high + 1);
663 MUSCLE_INSERT_INT ("table_ninf", table_ninf);
665 muscle_insert_base_table ("check", check,
666 check[0], 1, high + 1);
668 /* GLR parsing slightly modifies YYTABLE and YYCHECK (and thus
669 YYPACT) so that in states with unresolved conflicts, the default
670 reduction is not used in the conflicted entries, so that there is
671 a place to put a conflict pointer.
673 This means that YYCONFLP and YYCONFL are nonsense for a non-GLR
674 parser, so we could avoid accidents by not writing them out in
675 that case. Nevertheless, it seems even better to be able to use
676 the GLR skeletons even without the non-deterministic tables. */
677 muscle_insert_int_table ("conflict_list_heads", conflict_table,
678 conflict_table[0], 1, high + 1);
679 muscle_insert_int_table ("conflicting_rules", conflict_list,
680 0, 1, conflict_list_cnt);
684 /*--------------------------------------------.
685 | Output the definitions of all the muscles. |
686 `--------------------------------------------*/
688 static void
689 muscles_output (FILE *out)
691 fputs ("m4_init()\n", out);
692 merger_output (out);
693 symbol_numbers_output (out);
694 type_names_output (out);
695 user_actions_output (out);
696 /* Must be last. */
697 muscles_m4_output (out);
700 /*---------------------------.
701 | Call the skeleton parser. |
702 `---------------------------*/
704 static void
705 output_skeleton (void)
707 /* Compute the names of the package data dir and skeleton files. */
708 char const *m4 = m4path ();
709 char const *datadir = pkgdatadir ();
710 char *skeldir = xpath_join (datadir, "skeletons");
711 char *m4sugar = xpath_join (datadir, "m4sugar/m4sugar.m4");
712 char *m4bison = xpath_join (skeldir, "bison.m4");
713 char *traceon = xpath_join (skeldir, "traceon.m4");
714 char *skel = (IS_PATH_WITH_DIR (skeleton)
715 ? xstrdup (skeleton)
716 : xpath_join (skeldir, skeleton));
718 /* Test whether m4sugar.m4 is readable, to check for proper
719 installation. A faulty installation can cause deadlock, so a
720 cheap sanity check is worthwhile. */
721 xfclose (xfopen (m4sugar, "r"));
723 /* Create an m4 subprocess connected to us via two pipes. */
725 int filter_fd[2];
726 pid_t pid;
728 char const *argv[11];
729 int i = 0;
730 argv[i++] = m4;
732 /* When POSIXLY_CORRECT is set, GNU M4 1.6 and later disable GNU
733 extensions, which Bison's skeletons depend on. With older M4,
734 it has no effect. M4 1.4.12 added a -g/--gnu command-line
735 option to make it explicit that a program wants GNU M4
736 extensions even when POSIXLY_CORRECT is set.
738 See the thread starting at
739 <http://lists.gnu.org/archive/html/bug-bison/2008-07/msg00000.html>
740 for details. */
741 if (*M4_GNU_OPTION)
742 argv[i++] = M4_GNU_OPTION;
744 argv[i++] = "-I";
745 argv[i++] = datadir;
746 /* Some future version of GNU M4 (most likely 1.6) may treat the
747 -dV in a position-dependent manner. See the thread starting at
748 <http://lists.gnu.org/archive/html/bug-bison/2008-07/msg00000.html>
749 for details. */
750 if (trace_flag & trace_m4_early)
751 argv[i++] = "-dV";
752 argv[i++] = m4sugar;
753 argv[i++] = "-";
754 argv[i++] = m4bison;
755 if (trace_flag & trace_m4)
756 argv[i++] = traceon;
757 argv[i++] = skel;
758 argv[i++] = NULL;
759 aver (i <= ARRAY_CARDINALITY (argv));
761 if (trace_flag & trace_tools)
763 fputs ("running:", stderr);
764 for (int j = 0; argv[j]; ++j)
765 fprintf (stderr, " %s", argv[j]);
766 fputc ('\n', stderr);
769 /* The ugly cast is because gnulib gets the const-ness wrong. */
770 pid = create_pipe_bidi ("m4", m4, (char **)(void*)argv, false, true,
771 true, filter_fd);
774 free (skeldir);
775 free (m4sugar);
776 free (m4bison);
777 free (traceon);
778 free (skel);
780 if (trace_flag & trace_muscles)
781 muscles_output (stderr);
783 FILE *out = xfdopen (filter_fd[1], "w");
784 muscles_output (out);
785 xfclose (out);
788 /* Read and process m4's output. */
789 timevar_push (tv_m4);
791 FILE *in = xfdopen (filter_fd[0], "r");
792 scan_skel (in);
793 /* scan_skel should have read all of M4's output. Otherwise, when we
794 close the pipe, we risk letting M4 report a broken-pipe to the
795 Bison user. */
796 aver (feof (in));
797 xfclose (in);
799 wait_subprocess (pid, "m4", false, false, true, true, NULL);
800 timevar_pop (tv_m4);
803 static void
804 prepare (void)
806 /* BISON_USE_PUSH_FOR_PULL is for the test suite and should not be
807 documented for the user. */
808 char const *cp = getenv ("BISON_USE_PUSH_FOR_PULL");
809 bool use_push_for_pull_flag = cp && *cp && strtol (cp, 0, 10);
811 /* Versions. */
812 MUSCLE_INSERT_STRING ("version_string", VERSION);
813 MUSCLE_INSERT_INT ("version", strversion_to_int (VERSION));
814 MUSCLE_INSERT_INT ("required_version", required_version);
816 /* Flags. */
817 MUSCLE_INSERT_BOOL ("defines_flag", defines_flag);
818 MUSCLE_INSERT_BOOL ("glr_flag", glr_parser);
819 MUSCLE_INSERT_BOOL ("nondeterministic_flag", nondeterministic_parser);
820 MUSCLE_INSERT_BOOL ("synclines_flag", !no_lines_flag);
821 MUSCLE_INSERT_BOOL ("tag_seen_flag", tag_seen);
822 MUSCLE_INSERT_BOOL ("token_table_flag", token_table_flag);
823 MUSCLE_INSERT_BOOL ("use_push_for_pull_flag", use_push_for_pull_flag);
824 MUSCLE_INSERT_BOOL ("yacc_flag", !location_empty (yacc_loc));
826 /* File names. */
827 if (spec_name_prefix)
828 MUSCLE_INSERT_STRING ("prefix", spec_name_prefix);
830 MUSCLE_INSERT_STRING ("file_name_all_but_ext", all_but_ext);
832 #define DEFINE(Name) MUSCLE_INSERT_STRING (#Name, Name ? Name : "")
833 DEFINE (dir_prefix);
834 DEFINE (mapped_dir_prefix);
835 DEFINE (parser_file_name);
836 DEFINE (spec_header_file);
837 DEFINE (spec_mapped_header_file);
838 DEFINE (spec_file_prefix);
839 DEFINE (spec_graph_file);
840 DEFINE (spec_name_prefix);
841 DEFINE (spec_outfile);
842 DEFINE (spec_verbose_file);
843 #undef DEFINE
845 /* Find the right skeleton file, and add muscles about the skeletons. */
846 if (skeleton)
847 MUSCLE_INSERT_C_STRING ("skeleton", skeleton);
848 else
849 skeleton = language->skeleton;
851 /* About the skeletons. */
853 /* b4_skeletonsdir is used inside m4_include in the skeletons, so digraphs
854 would never be expanded. Hopefully no one has M4-special characters in
855 his Bison installation path. */
856 char *skeldir = xpath_join (pkgdatadir (), "skeletons");
857 MUSCLE_INSERT_STRING_RAW ("skeletonsdir", skeldir);
858 free (skeldir);
863 /*----------------------------------------------------------.
864 | Output the parsing tables and the parser code to ftable. |
865 `----------------------------------------------------------*/
867 void
868 output (void)
870 obstack_init (&format_obstack);
872 prepare_symbols ();
873 prepare_rules ();
874 prepare_states ();
875 prepare_actions ();
876 prepare_symbol_definitions ();
878 prepare ();
880 /* Process the selected skeleton file. */
881 output_skeleton ();
883 /* If late errors were generated, destroy the generated source
884 files. */
885 if (complaint_status)
886 unlink_generated_sources ();
888 obstack_free (&format_obstack, NULL);