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