]> git.saurik.com Git - bison.git/blame - src/output.c
style: syntax-check fixes
[bison.git] / src / output.c
CommitLineData
a932883e 1/* Output the generated parsing program for Bison.
87ceef73 2
7d6bad19 3 Copyright (C) 1984, 1986, 1989, 1992, 2000-2013 Free Software
575619af 4 Foundation, Inc.
c3e23647 5
9ee3c97b 6 This file is part of Bison, the GNU Compiler Compiler.
c3e23647 7
f16b0819
PE
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.
c3e23647 12
f16b0819
PE
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.
c3e23647 17
9ee3c97b 18 You should have received a copy of the GNU General Public License
f16b0819 19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c3e23647 20
2cec9080 21#include <config.h>
c3e23647 22#include "system.h"
21fd22d6 23
d9a7c07c 24#include <concat-filename.h>
3b2942e6 25#include <configmake.h>
d9a7c07c 26#include <filename.h>
21fd22d6
PE
27#include <get-errno.h>
28#include <quotearg.h>
a801089c 29#include <spawn-pipe.h>
21fd22d6 30#include <timevar.h>
f39ab286 31#include <wait-process.h>
21fd22d6
PE
32
33#include "complain.h"
c3e23647 34#include "files.h"
21fd22d6 35#include "getargs.h"
c3e23647 36#include "gram.h"
00f5d575 37#include "muscle-tab.h"
6c89f1c1 38#include "output.h"
a70083a3 39#include "reader.h"
e9071366 40#include "scan-code.h" /* max_left_semantic_context */
04098407 41#include "scan-skel.h"
ad949da9 42#include "symtab.h"
c6f1a33c 43#include "tables.h"
be2a1a68 44
f87685c3 45static struct obstack format_obstack;
c3e23647 46
133c20e2 47
5372019f
AD
48/*-------------------------------------------------------------------.
49| Create a function NAME which associates to the muscle NAME the |
50| result of formatting the FIRST and then TABLE_DATA[BEGIN..END[ (of |
51| TYPE), and to the muscle NAME_max, the max value of the |
52| TABLE_DATA. |
53`-------------------------------------------------------------------*/
62a3e4f0 54
62a3e4f0 55
e9690142
JD
56#define GENERATE_MUSCLE_INSERT_TABLE(Name, Type) \
57 \
58static void \
59Name (char const *name, \
60 Type *table_data, \
61 Type first, \
62 int begin, \
63 int end) \
64{ \
65 Type min = first; \
66 Type max = first; \
67 long int lmin; \
68 long int lmax; \
69 int i; \
70 int j = 1; \
71 \
cb9ec4fa 72 obstack_printf (&format_obstack, "%6d", first); \
e9690142
JD
73 for (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; \
cb9ec4fa 83 obstack_printf (&format_obstack, "%6d", table_data[i]); \
e9690142
JD
84 if (table_data[i] < min) \
85 min = table_data[i]; \
86 if (max < table_data[i]) \
87 max = table_data[i]; \
88 } \
6fbe73b6 89 muscle_insert (name, obstack_finish0 (&format_obstack)); \
e9690142
JD
90 \
91 lmin = min; \
92 lmax = max; \
45eebca4 93 /* Build 'NAME_min' and 'NAME_max' in the obstack. */ \
cb9ec4fa 94 obstack_printf (&format_obstack, "%s_min", name); \
6fbe73b6 95 MUSCLE_INSERT_LONG_INT (obstack_finish0 (&format_obstack), lmin); \
cb9ec4fa 96 obstack_printf (&format_obstack, "%s_max", name); \
6fbe73b6 97 MUSCLE_INSERT_LONG_INT (obstack_finish0 (&format_obstack), lmax); \
62a3e4f0
AD
98}
99
c0ef22ab
AD
100GENERATE_MUSCLE_INSERT_TABLE (muscle_insert_unsigned_int_table, unsigned int)
101GENERATE_MUSCLE_INSERT_TABLE (muscle_insert_int_table, int)
102GENERATE_MUSCLE_INSERT_TABLE (muscle_insert_base_table, base_number)
103GENERATE_MUSCLE_INSERT_TABLE (muscle_insert_rule_number_table, rule_number)
104GENERATE_MUSCLE_INSERT_TABLE (muscle_insert_symbol_number_table, symbol_number)
105GENERATE_MUSCLE_INSERT_TABLE (muscle_insert_state_number_table, state_number)
c3e23647 106
9a86ee60
AD
107/*----------------------------------------------------------------.
108| Print to OUT a representation of CP quoted and escaped for M4. |
109`----------------------------------------------------------------*/
3813e141
PE
110
111static void
9a86ee60 112quoted_output (FILE *out, char const *cp)
3813e141 113{
2f4f028d 114 fprintf (out, "[[");
3813e141 115
9a86ee60
AD
116 for (; *cp; cp++)
117 switch (*cp)
3813e141
PE
118 {
119 case '$': fputs ("$][", out); break;
120 case '@': fputs ("@@", out); break;
121 case '[': fputs ("@{", out); break;
122 case ']': fputs ("@}", out); break;
9a86ee60 123 default: fputc (*cp, out); break;
3813e141
PE
124 }
125
2f4f028d 126 fprintf (out, "]]");
3813e141
PE
127}
128
9a86ee60
AD
129/*----------------------------------------------------------------.
130| Print to OUT a representation of STRING quoted and escaped both |
131| for C and M4. |
132`----------------------------------------------------------------*/
133
134static void
135string_output (FILE *out, char const *string)
136{
137 quoted_output (out, quotearg_style (c_quoting_style, string));
138}
139
3813e141 140
39912f52
AD
141/*------------------------------------------------------------------.
142| Prepare the muscles related to the symbols: translate, tname, and |
143| toknum. |
144`------------------------------------------------------------------*/
b0940840 145
4a120d45 146static void
39912f52 147prepare_symbols (void)
c3e23647 148{
39912f52
AD
149 MUSCLE_INSERT_INT ("tokens_number", ntokens);
150 MUSCLE_INSERT_INT ("nterms_number", nvars);
d69c9694 151 MUSCLE_INSERT_INT ("symbols_number", nsyms);
39912f52
AD
152 MUSCLE_INSERT_INT ("undef_token_number", undeftoken->number);
153 MUSCLE_INSERT_INT ("user_token_number_max", max_user_token_number);
154
a49aecd5 155 muscle_insert_symbol_number_table ("translate",
e9690142
JD
156 token_translations,
157 token_translations[0],
158 1, max_user_token_number + 1);
c3e23647 159
39912f52 160 /* tname -- token names. */
b0940840
AD
161 {
162 int i;
d423d460
AD
163 /* We assume that the table will be output starting at column 2. */
164 int j = 2;
1cfe6375
JD
165 struct quoting_options *qo = clone_quoting_options (0);
166 set_quoting_style (qo, c_quoting_style);
167 set_quoting_flags (qo, QA_SPLIT_TRIGRAPHS);
b0940840
AD
168 for (i = 0; i < nsyms; i++)
169 {
e9690142
JD
170 char *cp = quotearg_alloc (symbols[i]->tag, -1, qo);
171 /* Width of the next token, including the two quotes, the
172 comma and the space. */
173 int width = strlen (cp) + 2;
174
175 if (j + width > 75)
176 {
177 obstack_sgrow (&format_obstack, "\n ");
178 j = 1;
179 }
180
181 if (i)
182 obstack_1grow (&format_obstack, ' ');
4323e0da 183 obstack_escape (&format_obstack, cp);
1cfe6375 184 free (cp);
e9690142
JD
185 obstack_1grow (&format_obstack, ',');
186 j += width;
b0940840 187 }
1cfe6375 188 free (qo);
8e1687ae 189 obstack_sgrow (&format_obstack, " ]b4_null[");
c3e23647 190
b0940840 191 /* Finish table and store. */
6fbe73b6 192 muscle_insert ("tname", obstack_finish0 (&format_obstack));
b0940840
AD
193 }
194
12b0043a 195 /* Output YYTOKNUM. */
b2ed6e58
AD
196 {
197 int i;
da2a7671 198 int *values = xnmalloc (ntokens, sizeof *values);
3650b4b8 199 for (i = 0; i < ntokens; ++i)
b0940840 200 values[i] = symbols[i]->user_token_number;
12b0043a 201 muscle_insert_int_table ("toknum", values,
e9690142 202 values[0], 1, ntokens);
b0940840 203 free (values);
b2ed6e58 204 }
b0940840 205}
b2ed6e58 206
b0940840 207
3d3bc1fe
AD
208/*----------------------------------------------------------------.
209| Prepare the muscles related to the rules: r1, r2, rline, dprec, |
ca2a6d15 210| merger, immediate. |
3d3bc1fe 211`----------------------------------------------------------------*/
b0940840
AD
212
213static void
214prepare_rules (void)
215{
da2a7671
PE
216 unsigned int *rline = xnmalloc (nrules, sizeof *rline);
217 symbol_number *r1 = xnmalloc (nrules, sizeof *r1);
218 unsigned int *r2 = xnmalloc (nrules, sizeof *r2);
f6fbd3da
PE
219 int *dprec = xnmalloc (nrules, sizeof *dprec);
220 int *merger = xnmalloc (nrules, sizeof *merger);
ca2a6d15 221 int *immediate = xnmalloc (nrules, sizeof *immediate);
4b3d3a8e 222
3d3bc1fe 223 rule_number r;
4b3d3a8e 224 for (r = 0; r < nrules; ++r)
b0940840 225 {
b0940840
AD
226 /* LHS of the rule R. */
227 r1[r] = rules[r].lhs->number;
228 /* Length of rule R's RHS. */
c0ef22ab 229 r2[r] = rule_rhs_length (&rules[r]);
b0940840 230 /* Line where rule was defined. */
2073702c 231 rline[r] = rules[r].location.start.line;
95612cfa 232 /* Dynamic precedence (GLR). */
676385e2 233 dprec[r] = rules[r].dprec;
95612cfa 234 /* Merger-function index (GLR). */
676385e2 235 merger[r] = rules[r].merger;
ca2a6d15
PH
236 /* Immediate reduction flags (GLR). */
237 immediate[r] = rules[r].is_predicate;
b0940840 238 }
b0940840 239
4b3d3a8e
AD
240 muscle_insert_unsigned_int_table ("rline", rline, 0, 0, nrules);
241 muscle_insert_symbol_number_table ("r1", r1, 0, 0, nrules);
242 muscle_insert_unsigned_int_table ("r2", r2, 0, 0, nrules);
f6fbd3da
PE
243 muscle_insert_int_table ("dprec", dprec, 0, 0, nrules);
244 muscle_insert_int_table ("merger", merger, 0, 0, nrules);
ca2a6d15 245 muscle_insert_int_table ("immediate", immediate, 0, 0, nrules);
796d61fb 246
39912f52 247 MUSCLE_INSERT_INT ("rules_number", nrules);
25005f6a 248 MUSCLE_INSERT_INT ("max_left_semantic_context", max_left_semantic_context);
39912f52 249
5df5f6d5
AD
250 free (rline);
251 free (r1);
b0940840 252 free (r2);
676385e2
PH
253 free (dprec);
254 free (merger);
71431084 255 free (immediate);
c3e23647
RS
256}
257
b0940840
AD
258/*--------------------------------------------.
259| Prepare the muscles related to the states. |
260`--------------------------------------------*/
c3e23647 261
4a120d45 262static void
b0940840 263prepare_states (void)
c3e23647 264{
21fd22d6 265 state_number i;
da2a7671 266 symbol_number *values = xnmalloc (nstates, sizeof *values);
9703cc49 267 for (i = 0; i < nstates; ++i)
29e88316 268 values[i] = states[i]->accessing_symbol;
a49aecd5 269 muscle_insert_symbol_number_table ("stos", values,
e9690142 270 0, 1, nstates);
87ceef73 271 free (values);
39912f52
AD
272
273 MUSCLE_INSERT_INT ("last", high);
274 MUSCLE_INSERT_INT ("final_state_number", final_state->number);
275 MUSCLE_INSERT_INT ("states_number", nstates);
c3e23647
RS
276}
277
278
4c3cc7da
AD
279/*-------------------------------------------------------.
280| Compare two symbols by type-name, and then by number. |
281`-------------------------------------------------------*/
282
d73e55e0 283static int
4c3cc7da
AD
284symbol_type_name_cmp (const symbol **lhs, const symbol **rhs)
285{
55439a1c
AD
286 int res = uniqstr_cmp ((*lhs)->type_name, (*rhs)->type_name);
287 if (!res)
288 res = (*lhs)->number - (*rhs)->number;
289 return res;
4c3cc7da
AD
290}
291
292
293/*----------------------------------------------------------------.
294| Return a (malloc'ed) table of the symbols sorted by type-name. |
295`----------------------------------------------------------------*/
296
297static symbol **
d73e55e0 298symbols_by_type_name (void)
4c3cc7da
AD
299{
300 typedef int (*qcmp_type) (const void *, const void *);
301 symbol **res = xmemdup (symbols, nsyms * sizeof *res);
302 qsort (res, nsyms, sizeof *res, (qcmp_type) &symbol_type_name_cmp);
303 return res;
304}
305
306
307/*------------------------------------------------------------------.
308| Define b4_type_names, which is a list of (lists of the numbers of |
309| symbols with same type-name). |
310`------------------------------------------------------------------*/
311
312static void
313type_names_output (FILE *out)
314{
315 int i;
4c3cc7da
AD
316 symbol **syms = symbols_by_type_name ();
317 fputs ("m4_define([b4_type_names],\n[", out);
5d731440
AD
318 for (i = 0; i < nsyms; /* nothing */)
319 {
c5ae8e85 320 /* The index of the first symbol of the current type-name. */
5d731440
AD
321 int i0 = i;
322 fputs (i ? ",\n[" : "[", out);
323 for (; i < nsyms && syms[i]->type_name == syms[i0]->type_name; ++i)
324 fprintf (out, "%s%d", i != i0 ? ", " : "", syms[i]->number);
325 fputs ("]", out);
326 }
4c3cc7da
AD
327 fputs ("])\n\n", out);
328 free (syms);
329}
330
c3e23647 331
d69c9694
AD
332/*-------------------------------------.
333| The list of all the symbol numbers. |
334`-------------------------------------*/
5ab8c47b
AD
335
336static void
d69c9694 337symbol_numbers_output (FILE *out)
5ab8c47b
AD
338{
339 int i;
d69c9694 340 fputs ("m4_define([b4_symbol_numbers],\n[", out);
5ab8c47b 341 for (i = 0; i < nsyms; ++i)
d69c9694 342 fprintf (out, "%s[%d]", i ? ", " : "", i);
5ab8c47b
AD
343 fputs ("])\n\n", out);
344}
345
346
bb33f19a
PE
347/*---------------------------------.
348| Output the user actions to OUT. |
349`---------------------------------*/
12b0043a 350
4a120d45 351static void
c6f1a33c 352user_actions_output (FILE *out)
3f96f4dc 353{
21fd22d6 354 rule_number r;
dafdc66f 355
8e1687ae 356 fputs ("m4_define([b4_actions], \n[", out);
4b3d3a8e 357 for (r = 0; r < nrules; ++r)
9222837b 358 if (rules[r].action)
3f96f4dc 359 {
e9690142
JD
360 fprintf (out, "b4_%scase(%d, [b4_syncline(%d, ",
361 rules[r].is_predicate ? "predicate_" : "",
362 r + 1, rules[r].action_location.start.line);
4323e0da 363 string_output (out, rules[r].action_location.start.file);
e9690142 364 fprintf (out, ")\n[ %s]])\n\n", rules[r].action);
3f96f4dc 365 }
8e1687ae 366 fputs ("])\n\n", out);
3f96f4dc
AD
367}
368
1de69d6a
AD
369/*------------------------------------.
370| Output the merge functions to OUT. |
371`------------------------------------*/
676385e2 372
41442480 373static void
676385e2
PH
374merger_output (FILE *out)
375{
376 int n;
377 merger_list* p;
378
379 fputs ("m4_define([b4_mergers], \n[[", out);
e0e5bf84 380 for (n = 1, p = merge_functions; p != NULL; n += 1, p = p->next)
676385e2 381 {
e0e5bf84 382 if (p->type[0] == '\0')
e9690142
JD
383 fprintf (out, " case %d: *yy0 = %s (*yy0, *yy1); break;\n",
384 n, p->name);
676385e2 385 else
e9690142
JD
386 fprintf (out, " case %d: yy0->%s = %s (*yy0, *yy1); break;\n",
387 n, p->type, p->name);
676385e2
PH
388 }
389 fputs ("]])\n\n", out);
390}
3f96f4dc 391
d69c9694 392
d73e55e0
DJ
393/*---------------------------------------------.
394| Prepare the muscles for symbol definitions. |
395`---------------------------------------------*/
d69c9694
AD
396
397static void
d73e55e0 398prepare_symbol_definitions (void)
d69c9694
AD
399{
400 int i;
401 for (i = 0; i < nsyms; ++i)
402 {
403 symbol *sym = symbols[i];
404 const char *key;
aea10ef4 405 const char *value;
d69c9694 406
6a0655d9 407#define SET_KEY(Entry) \
cb9ec4fa 408 obstack_printf (&format_obstack, "symbol(%d, %s)", \
6a0655d9 409 i, Entry); \
6fbe73b6 410 key = obstack_finish0 (&format_obstack);
6a0655d9
AD
411
412#define SET_KEY2(Entry, Suffix) \
cb9ec4fa 413 obstack_printf (&format_obstack, "symbol(%d, %s_%s)", \
6a0655d9 414 i, Entry, Suffix); \
6fbe73b6 415 key = obstack_finish0 (&format_obstack);
d69c9694 416
c5ae8e85 417 /* Whether the symbol has an identifier. */
aea10ef4 418 value = symbol_id_get (sym);
c0ef22ab 419 SET_KEY ("has_id");
aea10ef4
AD
420 MUSCLE_INSERT_INT (key, !!value);
421
c5ae8e85 422 /* Its identifier. */
c0ef22ab 423 SET_KEY ("id");
aea10ef4 424 MUSCLE_INSERT_STRING (key, value ? value : "");
8be046d7 425
c5ae8e85 426 /* Its tag. Typically for documentation purpose. */
c0ef22ab 427 SET_KEY ("tag");
d69c9694
AD
428 MUSCLE_INSERT_STRING (key, sym->tag);
429
c0ef22ab 430 SET_KEY ("user_number");
d69c9694
AD
431 MUSCLE_INSERT_INT (key, sym->user_token_number);
432
c0ef22ab 433 SET_KEY ("is_token");
5d731440
AD
434 MUSCLE_INSERT_INT (key,
435 i < ntokens && sym != errtoken && sym != undeftoken);
436
c0ef22ab 437 SET_KEY ("number");
d69c9694
AD
438 MUSCLE_INSERT_INT (key, sym->number);
439
c0ef22ab 440 SET_KEY ("has_type");
5d731440
AD
441 MUSCLE_INSERT_INT (key, !!sym->type_name);
442
c0ef22ab 443 SET_KEY ("type");
d69c9694
AD
444 MUSCLE_INSERT_STRING (key, sym->type_name ? sym->type_name : "");
445
6a0655d9
AD
446 {
447 int j;
448 for (j = 0; j < CODE_PROPS_SIZE; ++j)
449 {
423ca9b5
AD
450 /* "printer", not "%printer". */
451 char const *pname = code_props_type_string (j) + 1;
6a0655d9 452 code_props const *p = symbol_code_props_get (sym, j);
c0ef22ab 453 SET_KEY2 ("has", pname);
6a0655d9
AD
454 MUSCLE_INSERT_INT (key, !!p->code);
455
456 if (p->code)
457 {
c0ef22ab 458 SET_KEY2 (pname, "file");
6a0655d9
AD
459 MUSCLE_INSERT_STRING (key, p->location.start.file);
460
c0ef22ab 461 SET_KEY2 (pname, "line");
6a0655d9
AD
462 MUSCLE_INSERT_INT (key, p->location.start.line);
463
c0ef22ab 464 SET_KEY (pname);
6a0655d9
AD
465 MUSCLE_INSERT_STRING_RAW (key, p->code);
466 }
467 }
468 }
469#undef SET_KEY2
d69c9694
AD
470#undef SET_KEY
471 }
472}
473
474
4a120d45 475static void
c6f1a33c 476prepare_actions (void)
6c89f1c1 477{
c6f1a33c 478 /* Figure out the actions for the specified state, indexed by
742e4900 479 lookahead token type. */
bbb5bcc6 480
c6f1a33c 481 muscle_insert_rule_number_table ("defact", yydefact,
e9690142 482 yydefact[0], 1, nstates);
6c89f1c1 483
c6f1a33c
AD
484 /* Figure out what to do after reducing with each rule, depending on
485 the saved state from before the beginning of parsing the data
486 that matched this rule. */
d57650a5 487 muscle_insert_state_number_table ("defgoto", yydefgoto,
e9690142 488 yydefgoto[0], 1, nsyms - ntokens);
12b0043a 489
12b0043a 490
12b0043a
AD
491 /* Output PACT. */
492 muscle_insert_base_table ("pact", base,
e9690142 493 base[0], 1, nstates);
12b0043a 494 MUSCLE_INSERT_INT ("pact_ninf", base_ninf);
c3e23647 495
12b0043a
AD
496 /* Output PGOTO. */
497 muscle_insert_base_table ("pgoto", base,
e9690142 498 base[nstates], nstates + 1, nvectors);
c3e23647 499
12b0043a 500 muscle_insert_base_table ("table", table,
e9690142 501 table[0], 1, high + 1);
12b0043a 502 MUSCLE_INSERT_INT ("table_ninf", table_ninf);
676385e2 503
12b0043a 504 muscle_insert_base_table ("check", check,
e9690142 505 check[0], 1, high + 1);
c3e23647 506
ea99527d
AD
507 /* GLR parsing slightly modifies YYTABLE and YYCHECK (and thus
508 YYPACT) so that in states with unresolved conflicts, the default
509 reduction is not used in the conflicted entries, so that there is
510 a place to put a conflict pointer.
511
512 This means that YYCONFLP and YYCONFL are nonsense for a non-GLR
513 parser, so we could avoid accidents by not writing them out in
514 that case. Nevertheless, it seems even better to be able to use
515 the GLR skeletons even without the non-deterministic tables. */
516 muscle_insert_unsigned_int_table ("conflict_list_heads", conflict_table,
e9690142 517 conflict_table[0], 1, high + 1);
ea99527d 518 muscle_insert_unsigned_int_table ("conflicting_rules", conflict_list,
e9690142 519 0, 1, conflict_list_cnt);
6c89f1c1 520}
4175f6bb
AD
521
522
523/*--------------------------------------------.
524| Output the definitions of all the muscles. |
525`--------------------------------------------*/
526
527static void
528muscles_output (FILE *out)
529{
530 fputs ("m4_init()\n", out);
4175f6bb 531 merger_output (out);
d69c9694 532 symbol_numbers_output (out);
4c3cc7da 533 type_names_output (out);
d69c9694 534 user_actions_output (out);
c5ae8e85 535 /* Must be last. */
4175f6bb
AD
536 muscles_m4_output (out);
537}
652def80 538\f
1239777d
AD
539/*---------------------------.
540| Call the skeleton parser. |
541`---------------------------*/
c3e23647 542
4a120d45 543static void
1239777d 544output_skeleton (void)
9b3add5b 545{
573312ac 546 int filter_fd[2];
573312ac
PE
547 pid_t pid;
548
a9fc7990 549 /* Compute the names of the package data dir and skeleton files. */
d9a7c07c
AD
550 char const *m4 = (m4 = getenv ("M4")) ? m4 : M4;
551 char const *datadir = pkgdatadir ();
552 char *m4sugar = xconcatenated_filename (datadir, "m4sugar/m4sugar.m4", NULL);
553 char *m4bison = xconcatenated_filename (datadir, "bison.m4", NULL);
554 char *skel = (IS_PATH_WITH_DIR (skeleton)
6cf3716c
AD
555 ? xstrdup (skeleton)
556 : xconcatenated_filename (datadir, skeleton, NULL));
a9fc7990
JD
557
558 /* Test whether m4sugar.m4 is readable, to check for proper
559 installation. A faulty installation can cause deadlock, so a
560 cheap sanity check is worthwhile. */
d9a7c07c 561 xfclose (xfopen (m4sugar, "r"));
573312ac
PE
562
563 /* Create an m4 subprocess connected to us via two pipes. */
564
565 if (trace_flag & trace_tools)
90b9908d 566 fprintf (stderr, "running: %s %s - %s %s\n",
d9a7c07c 567 m4, m4sugar, m4bison, skel);
573312ac 568
a9fc7990
JD
569 /* Some future version of GNU M4 (most likely 1.6) may treat the -dV in a
570 position-dependent manner. Keep it as the first argument so that all
571 files are traced.
573312ac 572
a9fc7990
JD
573 See the thread starting at
574 <http://lists.gnu.org/archive/html/bug-bison/2008-07/msg00000.html>
575 for details. */
576 {
22539284 577 char const *argv[10];
a9fc7990
JD
578 int i = 0;
579 argv[i++] = m4;
d8911864
EB
580
581 /* When POSIXLY_CORRECT is set, GNU M4 1.6 and later disable GNU
582 extensions, which Bison's skeletons depend on. With older M4,
583 it has no effect. M4 1.4.12 added a -g/--gnu command-line
584 option to make it explicit that a program wants GNU M4
585 extensions even when POSIXLY_CORRECT is set.
586
587 See the thread starting at
588 <http://lists.gnu.org/archive/html/bug-bison/2008-07/msg00000.html>
589 for details. */
590 if (*M4_GNU_OPTION)
591 argv[i++] = M4_GNU_OPTION;
592
d67c52e8 593 argv[i++] = "-I";
d9a7c07c 594 argv[i++] = datadir;
a9fc7990
JD
595 if (trace_flag & trace_m4)
596 argv[i++] = "-dV";
d9a7c07c 597 argv[i++] = m4sugar;
a9fc7990 598 argv[i++] = "-";
d9a7c07c
AD
599 argv[i++] = m4bison;
600 argv[i++] = skel;
a9fc7990 601 argv[i++] = NULL;
9789acf0 602 aver (i <= ARRAY_CARDINALITY (argv));
22539284
AD
603
604 /* The ugly cast is because gnulib gets the const-ness wrong. */
605 pid = create_pipe_bidi ("m4", m4, (char **)(void*)argv, false, true,
606 true, filter_fd);
a9fc7990 607 }
a9fc7990 608
d9a7c07c
AD
609 free (m4sugar);
610 free (m4bison);
611 free (skel);
573312ac 612
5263bea9
AD
613 if (trace_flag & trace_muscles)
614 muscles_output (stderr);
615 {
22539284 616 FILE *out = xfdopen (filter_fd[1], "w");
5263bea9
AD
617 muscles_output (out);
618 xfclose (out);
619 }
be2a1a68 620
573312ac 621 /* Read and process m4's output. */
1509d42f 622 timevar_push (TV_M4);
d9a7c07c 623 {
22539284 624 FILE *in = xfdopen (filter_fd[0], "r");
d9a7c07c
AD
625 scan_skel (in);
626 /* scan_skel should have read all of M4's output. Otherwise, when we
627 close the pipe, we risk letting M4 report a broken-pipe to the
628 Bison user. */
629 aver (feof (in));
630 xfclose (in);
631 }
f39ab286 632 wait_subprocess (pid, "m4", false, false, true, true, NULL);
1509d42f 633 timevar_pop (TV_M4);
26f609ff
RA
634}
635
636static void
637prepare (void)
638{
4896ede8
AD
639 /* BISON_USE_PUSH_FOR_PULL is for the test suite and should not be
640 documented for the user. */
28126202 641 char const *cp = getenv ("BISON_USE_PUSH_FOR_PULL");
4896ede8 642 bool use_push_for_pull_flag = cp && *cp && strtol (cp, 0, 10);
945e396c 643
7db2ed2d 644 /* Flags. */
d0829076 645 MUSCLE_INSERT_BOOL ("defines_flag", defines_flag);
90b9908d 646 MUSCLE_INSERT_BOOL ("glr_flag", glr_parser);
90b9908d 647 MUSCLE_INSERT_BOOL ("nondeterministic_flag", nondeterministic_parser);
d0829076 648 MUSCLE_INSERT_BOOL ("synclines_flag", !no_lines_flag);
ddc8ede1 649 MUSCLE_INSERT_BOOL ("tag_seen_flag", tag_seen);
258cddbc 650 MUSCLE_INSERT_BOOL ("token_table_flag", token_table_flag);
95021767 651 MUSCLE_INSERT_BOOL ("use_push_for_pull_flag", use_push_for_pull_flag);
b931235e 652 MUSCLE_INSERT_BOOL ("yacc_flag", yacc_flag);
11d82f03 653
573a6cd3 654 /* File names. */
90b9908d
PB
655 if (spec_name_prefix)
656 MUSCLE_INSERT_STRING ("prefix", spec_name_prefix);
657
bd9d212b
JD
658 MUSCLE_INSERT_STRING ("file_name_all_but_ext", all_but_ext);
659
2b81e969
AD
660#define DEFINE(Name) MUSCLE_INSERT_STRING (#Name, Name ? Name : "")
661 DEFINE (dir_prefix);
662 DEFINE (parser_file_name);
663 DEFINE (spec_defines_file);
664 DEFINE (spec_file_prefix);
665 DEFINE (spec_graph_file);
666 DEFINE (spec_name_prefix);
667 DEFINE (spec_outfile);
668 DEFINE (spec_verbose_file);
669#undef DEFINE
b85810ae 670
0e021770
PE
671 /* Find the right skeleton file, and add muscles about the skeletons. */
672 if (skeleton)
673 MUSCLE_INSERT_C_STRING ("skeleton", skeleton);
674 else
675 skeleton = language->skeleton;
381fb12e 676
0e021770 677 /* About the skeletons. */
cf147260 678 {
bd9d212b
JD
679 /* b4_pkgdatadir is used inside m4_include in the skeletons, so digraphs
680 would never be expanded. Hopefully no one has M4-special characters in
681 his Bison installation path. */
d9a7c07c 682 MUSCLE_INSERT_STRING_RAW ("pkgdatadir", pkgdatadir ());
cf147260 683 }
26f609ff 684}
c3e23647 685
93ede233 686
6c89f1c1
AD
687/*----------------------------------------------------------.
688| Output the parsing tables and the parser code to ftable. |
689`----------------------------------------------------------*/
c3e23647 690
6c89f1c1
AD
691void
692output (void)
693{
f87685c3 694 obstack_init (&format_obstack);
dd60faec 695
39912f52 696 prepare_symbols ();
b0940840
AD
697 prepare_rules ();
698 prepare_states ();
536545f3 699 prepare_actions ();
d73e55e0 700 prepare_symbol_definitions ();
342b8b6e 701
26f609ff 702 prepare ();
652def80 703
9b3add5b
RA
704 /* Process the selected skeleton file. */
705 output_skeleton ();
706
f499b062 707 obstack_free (&format_obstack, NULL);
c3e23647 708}
d4bd2295
JD
709
710char const *
d9a7c07c 711pkgdatadir (void)
d4bd2295 712{
d9a7c07c
AD
713 char const *cp = getenv ("BISON_PKGDATADIR");
714 return cp ? cp : PKGDATADIR;
d4bd2295 715}