regen
[bison.git] / src / output.c
blob4b1bb0d8e765be2513151bdb89fc548107446718
1 /* Output the generated parsing program for Bison.
3 Copyright (C) 1984, 1986, 1989, 1992, 2000-2015, 2018-2021 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 <https://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 /* Define the list of start symbols *if* there are several. Define
466 them by pairs: [START-SYMBOL-NUM, SWITCHING-TOKEN-SYMBOL-NUM]. */
467 static void
468 start_symbols_output (FILE *out)
470 if (start_symbols && start_symbols->next)
472 fputs ("m4_define([b4_start_symbols],\n[", out);
473 for (symbol_list *list = start_symbols; list; list = list->next)
475 const symbol *start = list->content.sym;
476 const symbol *swtok = switching_token (start);
477 fprintf (out, "%s[%d, %d]",
478 list == start_symbols ? "" : ", ",
479 start->content->number, swtok->content->number);
481 fputs ("])\n\n", out);
486 /*-------------------------------------.
487 | The list of all the symbol numbers. |
488 `-------------------------------------*/
490 static void
491 symbol_numbers_output (FILE *out)
493 fputs ("m4_define([b4_symbol_numbers],\n[", out);
494 for (int i = 0; i < nsyms; ++i)
495 fprintf (out, "%s[%d]", i ? ", " : "", i);
496 fputs ("])\n\n", out);
500 /*-------------------------------------------.
501 | Output the user reduction actions to OUT. |
502 `-------------------------------------------*/
504 static void
505 rule_output (const rule *r, FILE *out)
507 output_escaped (out, r->lhs->symbol->tag);
508 fputc (':', out);
509 if (0 <= *r->rhs)
510 for (item_number *rhsp = r->rhs; 0 <= *rhsp; ++rhsp)
512 fputc (' ', out);
513 output_escaped (out, symbols[*rhsp]->tag);
515 else
516 fputs (" %empty", out);
519 static void
520 user_actions_output (FILE *out)
522 fputs ("m4_define([b4_actions], \n[", out);
523 for (rule_number r = 0; r < nrules; ++r)
524 if (rules[r].action)
526 /* The useless "" is there to pacify syntax-check. */
527 fprintf (out, "%s""(%d, [",
528 rules[r].is_predicate ? "b4_predicate_case" : "b4_case",
529 r + 1);
530 if (!no_lines_flag)
532 fprintf (out, "b4_syncline(%d, ",
533 rules[r].action_loc.start.line);
534 string_output (out, map_file_name (rules[r].action_loc.start.file));
535 fprintf (out, ")dnl\n");
537 fprintf (out, "[%*s%s]],\n[[",
538 rules[r].action_loc.start.column - 1, "",
539 rules[r].action);
540 rule_output (&rules[r], out);
541 fprintf (out, "]])\n\n");
543 fputs ("])\n\n", out);
546 /*------------------------------------.
547 | Output the merge functions to OUT. |
548 `------------------------------------*/
550 static void
551 merger_output (FILE *out)
553 fputs ("m4_define([b4_mergers], \n[[", out);
554 int n;
555 merger_list* p;
556 for (n = 1, p = merge_functions; p != NULL; n += 1, p = p->next)
557 fprintf (out, "]b4_call_merger""([%d], [%s], [%d])[\n",
558 n, p->name, p->sym->content->number);
559 fputs ("]])\n\n", out);
563 /*---------------------------------------------.
564 | Prepare the muscles for symbol definitions. |
565 `---------------------------------------------*/
567 static void
568 prepare_symbol_definitions (void)
570 /* Map "orig NUM" to new numbers. See data/README. */
571 for (symbol_number i = ntokens; i < nsyms + nuseless_nonterminals; ++i)
573 obstack_printf (&format_obstack, "symbol""(orig %d, number)", i);
574 const char *key = obstack_finish0 (&format_obstack);
575 MUSCLE_INSERT_INT (key, nterm_map ? nterm_map[i - ntokens] : i);
578 for (int i = 0; i < nsyms; ++i)
580 symbol *sym = symbols[i];
581 const char *key;
583 #define SET_KEY(Entry) \
584 obstack_printf (&format_obstack, "symbol""(%d, %s)", \
585 i, Entry); \
586 key = obstack_finish0 (&format_obstack);
588 #define SET_KEY2(Entry, Suffix) \
589 obstack_printf (&format_obstack, "symbol""(%d, %s_%s)", \
590 i, Entry, Suffix); \
591 key = obstack_finish0 (&format_obstack);
593 /* Whether the symbol has an identifier. */
594 const char *id = symbol_id_get (sym);
595 SET_KEY ("has_id");
596 MUSCLE_INSERT_INT (key, !!id);
598 /* Its identifier. */
599 SET_KEY ("id");
600 MUSCLE_INSERT_STRING (key, id ? id : "");
602 /* Its tag. Typically for documentation purpose. */
603 SET_KEY ("tag");
604 MUSCLE_INSERT_STRING (key, symbol_tag (sym));
606 SET_KEY ("code");
607 MUSCLE_INSERT_INT (key, sym->content->code);
609 SET_KEY ("is_token");
610 MUSCLE_INSERT_INT (key, i < ntokens);
612 SET_KEY ("number");
613 MUSCLE_INSERT_INT (key, sym->content->number);
615 SET_KEY ("has_type");
616 MUSCLE_INSERT_INT (key, !!sym->content->type_name);
618 SET_KEY ("type");
619 MUSCLE_INSERT_STRING (key, sym->content->type_name
620 ? sym->content->type_name : "");
622 for (int j = 0; j < CODE_PROPS_SIZE; ++j)
624 /* "printer", not "%printer". */
625 char const *pname = code_props_type_string (j) + 1;
626 code_props const *p = symbol_code_props_get (sym, j);
627 SET_KEY2 ("has", pname);
628 MUSCLE_INSERT_INT (key, !!p->code);
630 if (p->code)
632 SET_KEY2 (pname, "file");
633 MUSCLE_INSERT_C_STRING (key, map_file_name (p->location.start.file));
635 SET_KEY2 (pname, "line");
636 MUSCLE_INSERT_INT (key, p->location.start.line);
638 SET_KEY2 (pname, "loc");
639 muscle_location_grow (key, p->location);
641 SET_KEY (pname);
642 obstack_printf (&muscle_obstack,
643 "%*s%s", p->location.start.column - 1, "", p->code);
644 muscle_insert (key, obstack_finish0 (&muscle_obstack));
647 #undef SET_KEY2
648 #undef SET_KEY
653 static void
654 prepare_actions (void)
656 /* Figure out the actions for the specified state. */
657 muscle_insert_rule_number_table ("defact", yydefact,
658 yydefact[0], 1, nstates);
660 /* Figure out what to do after reducing with each rule, depending on
661 the saved state from before the beginning of parsing the data
662 that matched this rule. */
663 muscle_insert_state_number_table ("defgoto", yydefgoto,
664 yydefgoto[0], 1, nsyms - ntokens);
667 /* Output PACT. */
668 muscle_insert_base_table ("pact", base,
669 base[0], 1, nstates);
670 MUSCLE_INSERT_INT ("pact_ninf", base_ninf);
672 /* Output PGOTO. */
673 muscle_insert_base_table ("pgoto", base,
674 base[nstates], nstates + 1, nvectors);
676 muscle_insert_base_table ("table", table,
677 table[0], 1, high + 1);
678 MUSCLE_INSERT_INT ("table_ninf", table_ninf);
680 muscle_insert_base_table ("check", check,
681 check[0], 1, high + 1);
683 /* GLR parsing slightly modifies YYTABLE and YYCHECK (and thus
684 YYPACT) so that in states with unresolved conflicts, the default
685 reduction is not used in the conflicted entries, so that there is
686 a place to put a conflict pointer.
688 This means that YYCONFLP and YYCONFL are nonsense for a non-GLR
689 parser, so we could avoid accidents by not writing them out in
690 that case. Nevertheless, it seems even better to be able to use
691 the GLR skeletons even without the non-deterministic tables. */
692 muscle_insert_int_table ("conflict_list_heads", conflict_table,
693 conflict_table[0], 1, high + 1);
694 muscle_insert_int_table ("conflicting_rules", conflict_list,
695 0, 1, conflict_list_cnt);
699 /*--------------------------------------------.
700 | Output the definitions of all the muscles. |
701 `--------------------------------------------*/
703 static void
704 muscles_output (FILE *out)
706 fputs ("m4_init()\n", out);
707 merger_output (out);
708 symbol_numbers_output (out);
709 type_names_output (out);
710 start_symbols_output (out);
711 user_actions_output (out);
712 /* Must be last. */
713 muscles_m4_output (out);
716 /*---------------------------.
717 | Call the skeleton parser. |
718 `---------------------------*/
720 static void
721 output_skeleton (void)
723 /* Compute the names of the package data dir and skeleton files. */
724 char const *m4 = m4path ();
725 char const *datadir = pkgdatadir ();
726 char *skeldir = xpath_join (datadir, "skeletons");
727 char *m4sugar = xpath_join (datadir, "m4sugar/m4sugar.m4");
728 char *m4bison = xpath_join (skeldir, "bison.m4");
729 char *traceon = xpath_join (skeldir, "traceon.m4");
730 char *skel = (IS_PATH_WITH_DIR (skeleton)
731 ? xstrdup (skeleton)
732 : xpath_join (skeldir, skeleton));
734 /* Test whether m4sugar.m4 is readable, to check for proper
735 installation. A faulty installation can cause deadlock, so a
736 cheap sanity check is worthwhile. */
737 xfclose (xfopen (m4sugar, "r"));
739 /* Create an m4 subprocess connected to us via two pipes. */
741 int filter_fd[2];
742 pid_t pid;
744 char const *argv[11];
745 int i = 0;
746 argv[i++] = m4;
748 /* When POSIXLY_CORRECT is set, GNU M4 1.6 and later disable GNU
749 extensions, which Bison's skeletons depend on. With older M4,
750 it has no effect. M4 1.4.12 added a -g/--gnu command-line
751 option to make it explicit that a program wants GNU M4
752 extensions even when POSIXLY_CORRECT is set.
754 See the thread starting at
755 <https://lists.gnu.org/r/bug-bison/2008-07/msg00000.html>
756 for details. */
757 if (*M4_GNU_OPTION)
758 argv[i++] = M4_GNU_OPTION;
760 argv[i++] = "-I";
761 argv[i++] = datadir;
762 /* Some future version of GNU M4 (most likely 1.6) may treat the
763 -dV in a position-dependent manner. See the thread starting at
764 <https://lists.gnu.org/r/bug-bison/2008-07/msg00000.html>
765 for details. */
766 if (trace_flag & trace_m4_early)
767 argv[i++] = "-dV";
768 argv[i++] = m4sugar;
769 argv[i++] = "-";
770 argv[i++] = m4bison;
771 if (trace_flag & trace_m4)
772 argv[i++] = traceon;
773 argv[i++] = skel;
774 argv[i++] = NULL;
775 aver (i <= ARRAY_CARDINALITY (argv));
777 if (trace_flag & trace_tools)
779 fputs ("running:", stderr);
780 for (int j = 0; argv[j]; ++j)
781 fprintf (stderr, " %s", argv[j]);
782 fputc ('\n', stderr);
785 pid = create_pipe_bidi ("m4", m4, argv,
786 /* directory */ NULL,
787 /* null_stderr */ false,
788 /* slave_process */ true,
789 /* exit_on_error */ true,
790 filter_fd);
793 free (skeldir);
794 free (m4sugar);
795 free (m4bison);
796 free (traceon);
797 free (skel);
799 if (trace_flag & trace_muscles)
800 muscles_output (stderr);
802 FILE *out = xfdopen (filter_fd[1], "w");
803 muscles_output (out);
804 xfclose (out);
807 /* Read and process m4's output. */
808 timevar_push (tv_m4);
810 FILE *in = xfdopen (filter_fd[0], "r");
811 scan_skel (in);
812 /* scan_skel should have read all of M4's output. Otherwise, when we
813 close the pipe, we risk letting M4 report a broken-pipe to the
814 Bison user. */
815 aver (feof (in));
816 xfclose (in);
818 wait_subprocess (pid, "m4", false, false, true, true, NULL);
819 timevar_pop (tv_m4);
822 static void
823 prepare (void)
825 /* BISON_USE_PUSH_FOR_PULL is for the test suite and should not be
826 documented for the user. */
827 char const *cp = getenv ("BISON_USE_PUSH_FOR_PULL");
828 bool use_push_for_pull_flag = cp && *cp && strtol (cp, 0, 10);
830 /* Versions. */
831 MUSCLE_INSERT_STRING ("version_string", VERSION);
832 MUSCLE_INSERT_INT ("version", strversion_to_int (VERSION));
833 MUSCLE_INSERT_INT ("required_version", required_version);
835 /* Flags. */
836 MUSCLE_INSERT_BOOL ("header_flag", header_flag);
837 MUSCLE_INSERT_BOOL ("glr_flag", glr_parser);
838 MUSCLE_INSERT_BOOL ("nondeterministic_flag", nondeterministic_parser);
839 MUSCLE_INSERT_BOOL ("synclines_flag", !no_lines_flag);
840 MUSCLE_INSERT_BOOL ("tag_seen_flag", tag_seen);
841 MUSCLE_INSERT_BOOL ("token_table_flag", token_table_flag);
842 MUSCLE_INSERT_BOOL ("use_push_for_pull_flag", use_push_for_pull_flag);
843 MUSCLE_INSERT_BOOL ("yacc_flag", !location_empty (yacc_loc));
845 /* File names. */
846 if (spec_name_prefix)
847 MUSCLE_INSERT_STRING ("prefix", spec_name_prefix);
849 MUSCLE_INSERT_STRING ("file_name_all_but_ext", all_but_ext);
851 const char *spec_mapped_header_file = map_file_name (spec_header_file);
852 const char *mapped_dir_prefix = map_file_name (dir_prefix);
854 #define DEFINE(Name) MUSCLE_INSERT_STRING (#Name, Name ? Name : "")
855 DEFINE (dir_prefix);
856 DEFINE (mapped_dir_prefix);
857 DEFINE (parser_file_name);
858 DEFINE (spec_header_file);
859 DEFINE (spec_mapped_header_file);
860 DEFINE (spec_file_prefix);
861 DEFINE (spec_graph_file);
862 DEFINE (spec_name_prefix);
863 DEFINE (spec_outfile);
864 DEFINE (spec_verbose_file);
865 #undef DEFINE
867 /* Find the right skeleton file, and add muscles about the skeletons. */
868 if (skeleton)
869 MUSCLE_INSERT_C_STRING ("skeleton", skeleton);
870 else
871 skeleton = language->skeleton;
873 /* About the skeletons. */
875 /* b4_skeletonsdir is used inside m4_include in the skeletons, so digraphs
876 would never be expanded. Hopefully no one has M4-special characters in
877 his Bison installation path. */
878 char *skeldir = xpath_join (pkgdatadir (), "skeletons");
879 MUSCLE_INSERT_STRING_RAW ("skeletonsdir", skeldir);
880 free (skeldir);
885 /*----------------------------------------------------------.
886 | Output the parsing tables and the parser code to ftable. |
887 `----------------------------------------------------------*/
889 void
890 output (void)
892 obstack_init (&format_obstack);
894 prepare_symbols ();
895 prepare_rules ();
896 prepare_states ();
897 prepare_actions ();
898 prepare_symbol_definitions ();
900 prepare ();
902 /* Process the selected skeleton file. */
903 output_skeleton ();
905 /* If late errors were generated, destroy the generated source
906 files. */
907 if (complaint_status)
908 unlink_generated_sources ();
910 obstack_free (&format_obstack, NULL);