]> git.saurik.com Git - bison.git/blame - src/output.c
* data/glr.c (yyuserMerge, yyreportAmbiguity, yyreportSyntaxError):
[bison.git] / src / output.c
CommitLineData
a932883e 1/* Output the generated parsing program for Bison.
87ceef73 2
05ac60f3 3 Copyright (C) 1984, 1986, 1989, 1992, 2000, 2001, 2002, 2003, 2004, 2005
255ef638 4 Free Software Foundation, Inc.
c3e23647 5
9ee3c97b 6 This file is part of Bison, the GNU Compiler Compiler.
c3e23647 7
9ee3c97b
AD
8 Bison is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
c3e23647 12
9ee3c97b
AD
13 Bison is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
c3e23647 17
9ee3c97b
AD
18 You should have received a copy of the GNU General Public License
19 along with Bison; see the file COPYING. If not, write to the Free
0fb669f9
PE
20 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 02110-1301, USA. */
c3e23647
RS
22
23
c3e23647 24#include "system.h"
21fd22d6
PE
25
26#include <error.h>
27#include <get-errno.h>
28#include <quotearg.h>
29#include <subpipe.h>
30#include <timevar.h>
31
32#include "complain.h"
c3e23647 33#include "files.h"
21fd22d6 34#include "getargs.h"
c3e23647 35#include "gram.h"
21fd22d6 36#include "muscle_tab.h"
6c89f1c1 37#include "output.h"
a70083a3 38#include "reader.h"
04098407 39#include "scan-skel.h"
ad949da9 40#include "symtab.h"
c6f1a33c 41#include "tables.h"
be2a1a68 42
12b0043a 43
f87685c3 44static struct obstack format_obstack;
c3e23647 45
d0829076 46bool error_verbose = false;
c7925b99 47
f0440388 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
5372019f 58#define GENERATE_MUSCLE_INSERT_TABLE(Name, Type) \
5fbb0954 59 \
5372019f 60static void \
9e0876fb 61Name (char const *name, \
5fbb0954
AD
62 Type *table_data, \
63 Type first, \
64 int begin, \
65 int end) \
66{ \
12b0043a 67 Type min = first; \
0c2d3f4c 68 Type max = first; \
87ceef73
PE
69 long int lmin; \
70 long int lmax; \
5fbb0954
AD
71 int i; \
72 int j = 1; \
73 \
5372019f 74 obstack_fgrow1 (&format_obstack, "%6d", first); \
5fbb0954
AD
75 for (i = begin; i < end; ++i) \
76 { \
5372019f 77 obstack_1grow (&format_obstack, ','); \
5fbb0954
AD
78 if (j >= 10) \
79 { \
5372019f 80 obstack_sgrow (&format_obstack, "\n "); \
5fbb0954
AD
81 j = 1; \
82 } \
83 else \
84 ++j; \
5372019f 85 obstack_fgrow1 (&format_obstack, "%6d", table_data[i]); \
12b0043a
AD
86 if (table_data[i] < min) \
87 min = table_data[i]; \
88 if (max < table_data[i]) \
5fbb0954
AD
89 max = table_data[i]; \
90 } \
5372019f
AD
91 obstack_1grow (&format_obstack, 0); \
92 muscle_insert (name, obstack_finish (&format_obstack)); \
5fbb0954 93 \
87ceef73
PE
94 lmin = min; \
95 lmax = max; \
12b0043a
AD
96 /* Build `NAME_min' and `NAME_max' in the obstack. */ \
97 obstack_fgrow1 (&format_obstack, "%s_min", name); \
98 obstack_1grow (&format_obstack, 0); \
87ceef73 99 MUSCLE_INSERT_LONG_INT (obstack_finish (&format_obstack), lmin); \
5372019f
AD
100 obstack_fgrow1 (&format_obstack, "%s_max", name); \
101 obstack_1grow (&format_obstack, 0); \
87ceef73 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)
110GENERATE_MUSCLE_INSERT_TABLE(muscle_insert_item_number_table, item_number)
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;
fb9c0b33 122 fputs ("[[", 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
fb9c0b33 134 fputs ("]]", 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);
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",
fc5734fe
AD
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;
b0940840
AD
162 for (i = 0; i < nsyms; i++)
163 {
9e0876fb 164 char const *cp = quotearg_style (c_quoting_style, symbols[i]->tag);
d423d460
AD
165 /* Width of the next token, including the two quotes, the
166 comma and the space. */
21fd22d6 167 int width = strlen (cp) + 2;
b0940840 168
21fd22d6 169 if (j + width > 75)
b0940840 170 {
d423d460
AD
171 obstack_sgrow (&format_obstack, "\n ");
172 j = 1;
b0940840
AD
173 }
174
d423d460
AD
175 if (i)
176 obstack_1grow (&format_obstack, ' ');
3813e141 177 MUSCLE_OBSTACK_SGROW (&format_obstack, cp);
d423d460 178 obstack_1grow (&format_obstack, ',');
21fd22d6 179 j += width;
b0940840
AD
180 }
181 /* Add a NULL entry to list of tokens (well, 0, as NULL might not be
182 defined). */
d423d460 183 obstack_sgrow (&format_obstack, " 0");
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,
3650b4b8 197 values[0], 1, ntokens);
b0940840 198 free (values);
b2ed6e58 199 }
b0940840 200}
b2ed6e58 201
b0940840
AD
202
203/*-------------------------------------------------------------.
204| Prepare the muscles related to the rules: rhs, prhs, r1, r2, |
95612cfa 205| rline, dprec, merger. |
b0940840
AD
206`-------------------------------------------------------------*/
207
208static void
209prepare_rules (void)
210{
21fd22d6 211 rule_number r;
5df5f6d5 212 unsigned int i = 0;
da2a7671
PE
213 item_number *rhs = xnmalloc (nritems, sizeof *rhs);
214 unsigned int *prhs = xnmalloc (nrules, sizeof *prhs);
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);
4b3d3a8e
AD
220
221 for (r = 0; r < nrules; ++r)
b0940840 222 {
21fd22d6 223 item_number *rhsp = NULL;
b0940840
AD
224 /* Index of rule R in RHS. */
225 prhs[r] = i;
226 /* RHS of the rule R. */
227 for (rhsp = rules[r].rhs; *rhsp >= 0; ++rhsp)
228 rhs[i++] = *rhsp;
229 /* LHS of the rule R. */
230 r1[r] = rules[r].lhs->number;
231 /* Length of rule R's RHS. */
232 r2[r] = i - prhs[r];
233 /* Separator in RHS. */
234 rhs[i++] = -1;
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;
b0940840 241 }
a932883e
PE
242 if (i != nritems)
243 abort ();
b0940840 244
5372019f 245 muscle_insert_item_number_table ("rhs", rhs, ritem[0], 1, nritems);
4b3d3a8e
AD
246 muscle_insert_unsigned_int_table ("prhs", prhs, 0, 0, nrules);
247 muscle_insert_unsigned_int_table ("rline", rline, 0, 0, nrules);
248 muscle_insert_symbol_number_table ("r1", r1, 0, 0, nrules);
249 muscle_insert_unsigned_int_table ("r2", r2, 0, 0, nrules);
f6fbd3da
PE
250 muscle_insert_int_table ("dprec", dprec, 0, 0, nrules);
251 muscle_insert_int_table ("merger", merger, 0, 0, nrules);
796d61fb 252
39912f52 253 MUSCLE_INSERT_INT ("rules_number", nrules);
25005f6a 254 MUSCLE_INSERT_INT ("max_left_semantic_context", max_left_semantic_context);
39912f52 255
b0940840
AD
256 free (rhs);
257 free (prhs);
5df5f6d5
AD
258 free (rline);
259 free (r1);
b0940840 260 free (r2);
676385e2
PH
261 free (dprec);
262 free (merger);
c3e23647
RS
263}
264
b0940840
AD
265/*--------------------------------------------.
266| Prepare the muscles related to the states. |
267`--------------------------------------------*/
c3e23647 268
4a120d45 269static void
b0940840 270prepare_states (void)
c3e23647 271{
21fd22d6 272 state_number i;
da2a7671 273 symbol_number *values = xnmalloc (nstates, sizeof *values);
9703cc49 274 for (i = 0; i < nstates; ++i)
29e88316 275 values[i] = states[i]->accessing_symbol;
a49aecd5 276 muscle_insert_symbol_number_table ("stos", values,
d57650a5 277 0, 1, nstates);
87ceef73 278 free (values);
39912f52
AD
279
280 MUSCLE_INSERT_INT ("last", high);
281 MUSCLE_INSERT_INT ("final_state_number", final_state->number);
282 MUSCLE_INSERT_INT ("states_number", nstates);
c3e23647
RS
283}
284
285
c3e23647 286
bb33f19a
PE
287/*---------------------------------.
288| Output the user actions to OUT. |
289`---------------------------------*/
12b0043a 290
4a120d45 291static void
c6f1a33c 292user_actions_output (FILE *out)
3f96f4dc 293{
21fd22d6 294 rule_number r;
dafdc66f
AD
295
296 fputs ("m4_define([b4_actions], \n[[", out);
4b3d3a8e 297 for (r = 0; r < nrules; ++r)
9222837b 298 if (rules[r].action)
3f96f4dc 299 {
4b3d3a8e 300 fprintf (out, " case %d:\n", r + 1);
3f96f4dc 301
05ac60f3 302 fprintf (out, "]b4_syncline(%d, ",
2073702c 303 rules[r].action_location.start.line);
05ac60f3 304 escaped_output (out, rules[r].action_location.start.file);
fb9c0b33 305 fprintf (out, ")[\n %s\n break;\n\n",
9222837b 306 rules[r].action);
3f96f4dc 307 }
dafdc66f 308 fputs ("]])\n\n", out);
3f96f4dc
AD
309}
310
676385e2
PH
311/*--------------------------------------.
312| Output the merge functions to OUT. |
313`--------------------------------------*/
314
41442480 315static void
676385e2
PH
316merger_output (FILE *out)
317{
318 int n;
319 merger_list* p;
320
321 fputs ("m4_define([b4_mergers], \n[[", out);
e0e5bf84 322 for (n = 1, p = merge_functions; p != NULL; n += 1, p = p->next)
676385e2 323 {
e0e5bf84 324 if (p->type[0] == '\0')
16caa4f4 325 fprintf (out, " case %d: *yy0 = %s (*yy0, *yy1); break;\n",
676385e2
PH
326 n, p->name);
327 else
16caa4f4 328 fprintf (out, " case %d: yy0->%s = %s (*yy0, *yy1); break;\n",
676385e2
PH
329 n, p->type, p->name);
330 }
331 fputs ("]])\n\n", out);
332}
3f96f4dc 333
ae7453f2
AD
334/*--------------------------------------.
335| Output the tokens definition to OUT. |
336`--------------------------------------*/
091e20bb 337
c6f1a33c 338static void
be2a1a68 339token_definitions_output (FILE *out)
091e20bb
AD
340{
341 int i;
d0829076 342 char const *sep = "";
dafdc66f
AD
343
344 fputs ("m4_define([b4_tokens], \n[", out);
091e20bb
AD
345 for (i = 0; i < ntokens; ++i)
346 {
21fd22d6
PE
347 symbol *sym = symbols[i];
348 int number = sym->user_token_number;
091e20bb 349
b87f8b21
AD
350 /* At this stage, if there are literal aliases, they are part of
351 SYMBOLS, so we should not find symbols which are the aliases
352 here. */
a932883e
PE
353 if (number == USER_NUMBER_ALIAS)
354 abort ();
b87f8b21 355
091e20bb 356 /* Skip error token. */
21fd22d6 357 if (sym == errtoken)
091e20bb 358 continue;
b87f8b21
AD
359
360 /* If this string has an alias, then it is necessarily the alias
361 which is to be output. */
21fd22d6
PE
362 if (sym->alias)
363 sym = sym->alias;
b87f8b21
AD
364
365 /* Don't output literal chars or strings (when defined only as a
366 string). Note that must be done after the alias resolution:
367 think about `%token 'f' "f"'. */
21fd22d6 368 if (sym->tag[0] == '\'' || sym->tag[0] == '\"')
b87f8b21 369 continue;
091e20bb
AD
370
371 /* Don't #define nonliteral tokens whose names contain periods
372 or '$' (as does the default value of the EOF token). */
21fd22d6 373 if (strchr (sym->tag, '.') || strchr (sym->tag, '$'))
091e20bb
AD
374 continue;
375
05ac60f3 376 fprintf (out, "%s[[[%s]], %d]",
d0829076
PE
377 sep, sym->tag, number);
378 sep = ",\n";
091e20bb 379 }
dafdc66f 380 fputs ("])\n\n", out);
091e20bb
AD
381}
382
383
ae7453f2
AD
384/*---------------------------------------.
385| Output the symbol destructors to OUT. |
386`---------------------------------------*/
9280d3ef
AD
387
388static void
389symbol_destructors_output (FILE *out)
390{
391 int i;
d0829076 392 char const *sep = "";
9280d3ef
AD
393
394 fputs ("m4_define([b4_symbol_destructors], \n[", out);
395 for (i = 0; i < nsyms; ++i)
396 if (symbols[i]->destructor)
397 {
21fd22d6 398 symbol *sym = symbols[i];
9280d3ef 399
24c0aad7
AD
400 /* Filename, lineno,
401 Symbol-name, Symbol-number,
ee42c616 402 destructor, optional typename. */
d0829076
PE
403 fprintf (out, "%s[", sep);
404 sep = ",\n";
05ac60f3
PE
405 escaped_output (out, sym->destructor_location.start.file);
406 fprintf (out, ", %d, ", sym->destructor_location.start.line);
407 escaped_output (out, sym->tag);
408 fprintf (out, ", %d, [[%s]]", sym->number, sym->destructor);
ee42c616
PE
409 if (sym->type_name)
410 fprintf (out, ", [[%s]]", sym->type_name);
411 fputc (']', out);
9280d3ef
AD
412 }
413 fputs ("])\n\n", out);
414}
415
416
ae7453f2
AD
417/*------------------------------------.
418| Output the symbol printers to OUT. |
419`------------------------------------*/
366eea36
AD
420
421static void
422symbol_printers_output (FILE *out)
423{
424 int i;
d0829076 425 char const *sep = "";
366eea36
AD
426
427 fputs ("m4_define([b4_symbol_printers], \n[", out);
428 for (i = 0; i < nsyms; ++i)
34e1d160 429 if (symbols[i]->printer)
366eea36 430 {
21fd22d6 431 symbol *sym = symbols[i];
366eea36
AD
432
433 /* Filename, lineno,
434 Symbol-name, Symbol-number,
ee42c616 435 printer, optional typename. */
d0829076
PE
436 fprintf (out, "%s[", sep);
437 sep = ",\n";
05ac60f3
PE
438 escaped_output (out, sym->printer_location.start.file);
439 fprintf (out, ", %d, ", sym->printer_location.start.line);
440 escaped_output (out, sym->tag);
441 fprintf (out, ", %d, [[%s]]", sym->number, sym->printer);
ee42c616
PE
442 if (sym->type_name)
443 fprintf (out, ", [[%s]]", sym->type_name);
444 fputc (']', out);
366eea36
AD
445 }
446 fputs ("])\n\n", out);
447}
448
449
4a120d45 450static void
c6f1a33c 451prepare_actions (void)
6c89f1c1 452{
c6f1a33c 453 /* Figure out the actions for the specified state, indexed by
8dd162d3 454 look-ahead token type. */
bbb5bcc6 455
c6f1a33c
AD
456 muscle_insert_rule_number_table ("defact", yydefact,
457 yydefact[0], 1, nstates);
6c89f1c1 458
c6f1a33c
AD
459 /* Figure out what to do after reducing with each rule, depending on
460 the saved state from before the beginning of parsing the data
461 that matched this rule. */
d57650a5
AD
462 muscle_insert_state_number_table ("defgoto", yydefgoto,
463 yydefgoto[0], 1, nsyms - ntokens);
12b0043a 464
12b0043a 465
12b0043a
AD
466 /* Output PACT. */
467 muscle_insert_base_table ("pact", base,
5372019f 468 base[0], 1, nstates);
12b0043a 469 MUSCLE_INSERT_INT ("pact_ninf", base_ninf);
c3e23647 470
12b0043a
AD
471 /* Output PGOTO. */
472 muscle_insert_base_table ("pgoto", base,
5372019f 473 base[nstates], nstates + 1, nvectors);
c3e23647 474
12b0043a
AD
475 muscle_insert_base_table ("table", table,
476 table[0], 1, high + 1);
477 MUSCLE_INSERT_INT ("table_ninf", table_ninf);
676385e2 478
12b0043a
AD
479 muscle_insert_base_table ("check", check,
480 check[0], 1, high + 1);
c3e23647 481
ea99527d
AD
482 /* GLR parsing slightly modifies YYTABLE and YYCHECK (and thus
483 YYPACT) so that in states with unresolved conflicts, the default
484 reduction is not used in the conflicted entries, so that there is
485 a place to put a conflict pointer.
486
487 This means that YYCONFLP and YYCONFL are nonsense for a non-GLR
488 parser, so we could avoid accidents by not writing them out in
489 that case. Nevertheless, it seems even better to be able to use
490 the GLR skeletons even without the non-deterministic tables. */
491 muscle_insert_unsigned_int_table ("conflict_list_heads", conflict_table,
39912f52 492 conflict_table[0], 1, high + 1);
ea99527d 493 muscle_insert_unsigned_int_table ("conflicting_rules", conflict_list,
da2a7671 494 0, 1, conflict_list_cnt);
6c89f1c1 495}
c3e23647 496
652def80 497\f
1239777d
AD
498/*---------------------------.
499| Call the skeleton parser. |
500`---------------------------*/
c3e23647 501
4a120d45 502static void
1239777d 503output_skeleton (void)
9b3add5b 504{
573312ac
PE
505 FILE *in;
506 FILE *out;
507 int filter_fd[2];
2fd4e131 508 char const *argv[6];
573312ac
PE
509 pid_t pid;
510
511 /* Compute the names of the package data dir and skeleton file.
512 Test whether m4sugar.m4 is readable, to check for proper
513 installation. A faulty installation can cause deadlock, so a
514 cheap sanity check is worthwhile. */
515 char const m4sugar[] = "m4sugar/m4sugar.m4";
2fd4e131
PE
516 char *full_m4sugar;
517 char *full_cm4;
eecf08fe 518 char *full_skeleton;
573312ac
PE
519 char const *p;
520 char const *m4 = (p = getenv ("M4")) ? p : M4;
521 char const *pkgdatadir = (p = getenv ("BISON_PKGDATADIR")) ? p : PKGDATADIR;
522 size_t skeleton_size = strlen (skeleton) + 1;
523 size_t pkgdatadirlen = strlen (pkgdatadir);
524 while (pkgdatadirlen && pkgdatadir[pkgdatadirlen - 1] == '/')
525 pkgdatadirlen--;
eecf08fe
PE
526 full_skeleton = xmalloc (pkgdatadirlen + 1
527 + (skeleton_size < sizeof m4sugar
528 ? sizeof m4sugar : skeleton_size));
529 strcpy (full_skeleton, pkgdatadir);
530 full_skeleton[pkgdatadirlen] = '/';
531 strcpy (full_skeleton + pkgdatadirlen + 1, m4sugar);
532 full_m4sugar = xstrdup (full_skeleton);
533 strcpy (full_skeleton + pkgdatadirlen + 1, "c.m4");
534 full_cm4 = xstrdup (full_skeleton);
535 strcpy (full_skeleton + pkgdatadirlen + 1, skeleton);
2fd4e131 536 xfclose (xfopen (full_m4sugar, "r"));
573312ac
PE
537
538 /* Create an m4 subprocess connected to us via two pipes. */
539
540 if (trace_flag & trace_tools)
2fd4e131 541 fprintf (stderr, "running: %s %s - %s %s\n",
eecf08fe 542 m4, full_m4sugar, full_cm4, full_skeleton);
573312ac
PE
543
544 argv[0] = m4;
2fd4e131
PE
545 argv[1] = full_m4sugar;
546 argv[2] = "-";
547 argv[3] = full_cm4;
eecf08fe 548 argv[4] = full_skeleton;
2fd4e131 549 argv[5] = NULL;
573312ac
PE
550
551 init_subpipe ();
552 pid = create_subpipe (argv, filter_fd);
2fd4e131
PE
553 free (full_m4sugar);
554 free (full_cm4);
eecf08fe 555 free (full_skeleton);
573312ac
PE
556
557 out = fdopen (filter_fd[0], "w");
558 if (! out)
04098407
PE
559 error (EXIT_FAILURE, get_errno (),
560 "fdopen");
573312ac
PE
561
562 /* Output the definitions of all the muscles. */
381fb12e
AD
563 fputs ("m4_init()\n", out);
564
c6f1a33c 565 user_actions_output (out);
676385e2 566 merger_output (out);
381fb12e 567 token_definitions_output (out);
9280d3ef 568 symbol_destructors_output (out);
366eea36 569 symbol_printers_output (out);
be2a1a68 570
381fb12e 571 muscles_m4_output (out);
be2a1a68 572
381fb12e
AD
573 fputs ("m4_wrap([m4_divert_pop(0)])\n", out);
574 fputs ("m4_divert_push(0)dnl\n", out);
575 xfclose (out);
be2a1a68 576
573312ac 577 /* Read and process m4's output. */
1509d42f 578 timevar_push (TV_M4);
ec4d88f4 579 end_of_output_subpipe (pid, filter_fd);
573312ac
PE
580 in = fdopen (filter_fd[1], "r");
581 if (! in)
04098407
PE
582 error (EXIT_FAILURE, get_errno (),
583 "fdopen");
573312ac 584 scan_skel (in);
573312ac
PE
585 xfclose (in);
586 reap_subpipe (pid, m4);
1509d42f 587 timevar_pop (TV_M4);
26f609ff
RA
588}
589
590static void
591prepare (void)
592{
7db2ed2d 593 /* Flags. */
d0829076
PE
594 MUSCLE_INSERT_BOOL ("debug", debug_flag);
595 MUSCLE_INSERT_BOOL ("defines_flag", defines_flag);
596 MUSCLE_INSERT_BOOL ("error_verbose", error_verbose);
597 MUSCLE_INSERT_BOOL ("locations_flag", locations_flag);
598 MUSCLE_INSERT_BOOL ("pure", pure_parser);
599 MUSCLE_INSERT_BOOL ("synclines_flag", !no_lines_flag);
11d82f03 600
573a6cd3 601 /* File names. */
7db2ed2d 602 MUSCLE_INSERT_STRING ("prefix", spec_name_prefix ? spec_name_prefix : "yy");
2b81e969
AD
603#define DEFINE(Name) MUSCLE_INSERT_STRING (#Name, Name ? Name : "")
604 DEFINE (dir_prefix);
605 DEFINE (parser_file_name);
606 DEFINE (spec_defines_file);
607 DEFINE (spec_file_prefix);
608 DEFINE (spec_graph_file);
609 DEFINE (spec_name_prefix);
610 DEFINE (spec_outfile);
611 DEFINE (spec_verbose_file);
612#undef DEFINE
b85810ae 613
7db2ed2d 614 /* User Code. */
0dd1580a
RA
615 obstack_1grow (&pre_prologue_obstack, 0);
616 obstack_1grow (&post_prologue_obstack, 0);
617 muscle_insert ("pre_prologue", obstack_finish (&pre_prologue_obstack));
618 muscle_insert ("post_prologue", obstack_finish (&post_prologue_obstack));
381fb12e
AD
619
620 /* Find the right skeleton file. */
621 if (!skeleton)
676385e2 622 {
916708d5 623 if (glr_parser || nondeterministic_parser)
676385e2
PH
624 skeleton = "glr.c";
625 else
626 skeleton = "yacc.c";
627 }
381fb12e 628
cf147260
AD
629 /* About the skeletons. */
630 {
1bd0deda
PE
631 char const *pkgdatadir = getenv ("BISON_PKGDATADIR");
632 MUSCLE_INSERT_STRING ("pkgdatadir", pkgdatadir ? pkgdatadir : PKGDATADIR);
cf147260
AD
633 MUSCLE_INSERT_C_STRING ("skeleton", skeleton);
634 }
26f609ff 635}
c3e23647 636
93ede233 637
6c89f1c1
AD
638/*----------------------------------------------------------.
639| Output the parsing tables and the parser code to ftable. |
640`----------------------------------------------------------*/
c3e23647 641
6c89f1c1
AD
642void
643output (void)
644{
f87685c3 645 obstack_init (&format_obstack);
dd60faec 646
39912f52 647 prepare_symbols ();
b0940840
AD
648 prepare_rules ();
649 prepare_states ();
536545f3 650 prepare_actions ();
342b8b6e 651
26f609ff 652 prepare ();
652def80 653
9b3add5b
RA
654 /* Process the selected skeleton file. */
655 output_skeleton ();
656
f499b062 657 obstack_free (&format_obstack, NULL);
0dd1580a
RA
658 obstack_free (&pre_prologue_obstack, NULL);
659 obstack_free (&post_prologue_obstack, NULL);
c3e23647 660}