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