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