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