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