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