output.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736
  1. /* Output the generated parsing program for Bison.
  2. Copyright (C) 1984, 1986, 1989, 1992, 2000-2013 Free Software
  3. Foundation, Inc.
  4. This file is part of Bison, the GNU Compiler Compiler.
  5. This program is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  15. #include <config.h>
  16. #include "system.h"
  17. #include <concat-filename.h>
  18. #include <configmake.h>
  19. #include <filename.h>
  20. #include <get-errno.h>
  21. #include <quotearg.h>
  22. #include <spawn-pipe.h>
  23. #include <timevar.h>
  24. #include <wait-process.h>
  25. #include "complain.h"
  26. #include "files.h"
  27. #include "getargs.h"
  28. #include "gram.h"
  29. #include "muscle-tab.h"
  30. #include "output.h"
  31. #include "reader.h"
  32. #include "scan-code.h" /* max_left_semantic_context */
  33. #include "scan-skel.h"
  34. #include "symtab.h"
  35. #include "tables.h"
  36. #include "uniqstr.h"
  37. #include <contrib/tools/bison/arcadia_root.h>
  38. #undef M4
  39. #define M4 (m4_path)
  40. #define STR(a) XSTR(a)
  41. #define XSTR(a) #a
  42. const char*
  43. default_pkgdatadir()
  44. {
  45. const char* arc_path = getenv("ARCADIA_ROOT_DISTBUILD");
  46. if (arc_path == NULL)
  47. arc_path = ArcadiaRoot();
  48. return uniqstr_vsprintf("%s/" STR(BISON_DATA_DIR), arc_path);
  49. }
  50. #undef PKGDATADIR
  51. #define PKGDATADIR (default_pkgdatadir())
  52. static struct obstack format_obstack;
  53. /*-------------------------------------------------------------------.
  54. | Create a function NAME which associates to the muscle NAME the |
  55. | result of formatting the FIRST and then TABLE_DATA[BEGIN..END[ (of |
  56. | TYPE), and to the muscle NAME_max, the max value of the |
  57. | TABLE_DATA. |
  58. `-------------------------------------------------------------------*/
  59. #define GENERATE_MUSCLE_INSERT_TABLE(Name, Type) \
  60. \
  61. static void \
  62. Name (char const *name, \
  63. Type *table_data, \
  64. Type first, \
  65. int begin, \
  66. int end) \
  67. { \
  68. Type min = first; \
  69. Type max = first; \
  70. long int lmin; \
  71. long int lmax; \
  72. int i; \
  73. int j = 1; \
  74. \
  75. obstack_printf (&format_obstack, "%6d", first); \
  76. for (i = begin; i < end; ++i) \
  77. { \
  78. obstack_1grow (&format_obstack, ','); \
  79. if (j >= 10) \
  80. { \
  81. obstack_sgrow (&format_obstack, "\n "); \
  82. j = 1; \
  83. } \
  84. else \
  85. ++j; \
  86. obstack_printf (&format_obstack, "%6d", table_data[i]); \
  87. if (table_data[i] < min) \
  88. min = table_data[i]; \
  89. if (max < table_data[i]) \
  90. max = table_data[i]; \
  91. } \
  92. muscle_insert (name, obstack_finish0 (&format_obstack)); \
  93. \
  94. lmin = min; \
  95. lmax = max; \
  96. /* Build 'NAME_min' and 'NAME_max' in the obstack. */ \
  97. obstack_printf (&format_obstack, "%s_min", name); \
  98. MUSCLE_INSERT_LONG_INT (obstack_finish0 (&format_obstack), lmin); \
  99. obstack_printf (&format_obstack, "%s_max", name); \
  100. MUSCLE_INSERT_LONG_INT (obstack_finish0 (&format_obstack), lmax); \
  101. }
  102. GENERATE_MUSCLE_INSERT_TABLE (muscle_insert_unsigned_int_table, unsigned int)
  103. GENERATE_MUSCLE_INSERT_TABLE (muscle_insert_int_table, int)
  104. GENERATE_MUSCLE_INSERT_TABLE (muscle_insert_base_table, base_number)
  105. GENERATE_MUSCLE_INSERT_TABLE (muscle_insert_rule_number_table, rule_number)
  106. GENERATE_MUSCLE_INSERT_TABLE (muscle_insert_symbol_number_table, symbol_number)
  107. GENERATE_MUSCLE_INSERT_TABLE (muscle_insert_state_number_table, state_number)
  108. /*----------------------------------------------------------------.
  109. | Print to OUT a representation of CP quoted and escaped for M4. |
  110. `----------------------------------------------------------------*/
  111. static void
  112. quoted_output (FILE *out, char const *cp)
  113. {
  114. fprintf (out, "[[");
  115. for (; *cp; cp++)
  116. switch (*cp)
  117. {
  118. case '$': fputs ("$][", out); break;
  119. case '@': fputs ("@@", out); break;
  120. case '[': fputs ("@{", out); break;
  121. case ']': fputs ("@}", out); break;
  122. default: fputc (*cp, out); break;
  123. }
  124. fprintf (out, "]]");
  125. }
  126. /*----------------------------------------------------------------.
  127. | Print to OUT a representation of STRING quoted and escaped both |
  128. | for C and M4. |
  129. `----------------------------------------------------------------*/
  130. static void
  131. string_output (FILE *out, char const *string)
  132. {
  133. quoted_output (out, quotearg_style (c_quoting_style, string));
  134. }
  135. /*------------------------------------------------------------------.
  136. | Prepare the muscles related to the symbols: translate, tname, and |
  137. | toknum. |
  138. `------------------------------------------------------------------*/
  139. static void
  140. prepare_symbols (void)
  141. {
  142. MUSCLE_INSERT_INT ("tokens_number", ntokens);
  143. MUSCLE_INSERT_INT ("nterms_number", nvars);
  144. MUSCLE_INSERT_INT ("symbols_number", nsyms);
  145. MUSCLE_INSERT_INT ("undef_token_number", undeftoken->number);
  146. MUSCLE_INSERT_INT ("user_token_number_max", max_user_token_number);
  147. muscle_insert_symbol_number_table ("translate",
  148. token_translations,
  149. token_translations[0],
  150. 1, max_user_token_number + 1);
  151. /* tname -- token names. */
  152. {
  153. int i;
  154. /* We assume that the table will be output starting at column 2. */
  155. int j = 2;
  156. struct quoting_options *qo = clone_quoting_options (0);
  157. set_quoting_style (qo, c_quoting_style);
  158. set_quoting_flags (qo, QA_SPLIT_TRIGRAPHS);
  159. for (i = 0; i < nsyms; i++)
  160. {
  161. char *cp = quotearg_alloc (symbols[i]->tag, -1, qo);
  162. /* Width of the next token, including the two quotes, the
  163. comma and the space. */
  164. int width = strlen (cp) + 2;
  165. if (j + width > 75)
  166. {
  167. obstack_sgrow (&format_obstack, "\n ");
  168. j = 1;
  169. }
  170. if (i)
  171. obstack_1grow (&format_obstack, ' ');
  172. obstack_escape (&format_obstack, cp);
  173. free (cp);
  174. obstack_1grow (&format_obstack, ',');
  175. j += width;
  176. }
  177. free (qo);
  178. obstack_sgrow (&format_obstack, " ]b4_null[");
  179. /* Finish table and store. */
  180. muscle_insert ("tname", obstack_finish0 (&format_obstack));
  181. }
  182. /* Output YYTOKNUM. */
  183. {
  184. int i;
  185. int *values = xnmalloc (ntokens, sizeof *values);
  186. for (i = 0; i < ntokens; ++i)
  187. values[i] = symbols[i]->user_token_number;
  188. muscle_insert_int_table ("toknum", values,
  189. values[0], 1, ntokens);
  190. free (values);
  191. }
  192. }
  193. /*----------------------------------------------------------------.
  194. | Prepare the muscles related to the rules: r1, r2, rline, dprec, |
  195. | merger, immediate. |
  196. `----------------------------------------------------------------*/
  197. static void
  198. prepare_rules (void)
  199. {
  200. unsigned int *rline = xnmalloc (nrules, sizeof *rline);
  201. symbol_number *r1 = xnmalloc (nrules, sizeof *r1);
  202. unsigned int *r2 = xnmalloc (nrules, sizeof *r2);
  203. int *dprec = xnmalloc (nrules, sizeof *dprec);
  204. int *merger = xnmalloc (nrules, sizeof *merger);
  205. int *immediate = xnmalloc (nrules, sizeof *immediate);
  206. rule_number r;
  207. for (r = 0; r < nrules; ++r)
  208. {
  209. /* LHS of the rule R. */
  210. r1[r] = rules[r].lhs->number;
  211. /* Length of rule R's RHS. */
  212. r2[r] = rule_rhs_length (&rules[r]);
  213. /* Line where rule was defined. */
  214. rline[r] = rules[r].location.start.line;
  215. /* Dynamic precedence (GLR). */
  216. dprec[r] = rules[r].dprec;
  217. /* Merger-function index (GLR). */
  218. merger[r] = rules[r].merger;
  219. /* Immediate reduction flags (GLR). */
  220. immediate[r] = rules[r].is_predicate;
  221. }
  222. muscle_insert_unsigned_int_table ("rline", rline, 0, 0, nrules);
  223. muscle_insert_symbol_number_table ("r1", r1, 0, 0, nrules);
  224. muscle_insert_unsigned_int_table ("r2", r2, 0, 0, nrules);
  225. muscle_insert_int_table ("dprec", dprec, 0, 0, nrules);
  226. muscle_insert_int_table ("merger", merger, 0, 0, nrules);
  227. muscle_insert_int_table ("immediate", immediate, 0, 0, nrules);
  228. MUSCLE_INSERT_INT ("rules_number", nrules);
  229. MUSCLE_INSERT_INT ("max_left_semantic_context", max_left_semantic_context);
  230. free (rline);
  231. free (r1);
  232. free (r2);
  233. free (dprec);
  234. free (merger);
  235. free (immediate);
  236. }
  237. /*--------------------------------------------.
  238. | Prepare the muscles related to the states. |
  239. `--------------------------------------------*/
  240. static void
  241. prepare_states (void)
  242. {
  243. state_number i;
  244. symbol_number *values = xnmalloc (nstates, sizeof *values);
  245. for (i = 0; i < nstates; ++i)
  246. values[i] = states[i]->accessing_symbol;
  247. muscle_insert_symbol_number_table ("stos", values,
  248. 0, 1, nstates);
  249. free (values);
  250. MUSCLE_INSERT_INT ("last", high);
  251. MUSCLE_INSERT_INT ("final_state_number", final_state->number);
  252. MUSCLE_INSERT_INT ("states_number", nstates);
  253. }
  254. /*-------------------------------------------------------.
  255. | Compare two symbols by type-name, and then by number. |
  256. `-------------------------------------------------------*/
  257. static int
  258. symbol_type_name_cmp (const symbol **lhs, const symbol **rhs)
  259. {
  260. int res = uniqstr_cmp ((*lhs)->type_name, (*rhs)->type_name);
  261. if (!res)
  262. res = (*lhs)->number - (*rhs)->number;
  263. return res;
  264. }
  265. /*----------------------------------------------------------------.
  266. | Return a (malloc'ed) table of the symbols sorted by type-name. |
  267. `----------------------------------------------------------------*/
  268. static symbol **
  269. symbols_by_type_name (void)
  270. {
  271. typedef int (*qcmp_type) (const void *, const void *);
  272. symbol **res = xmemdup (symbols, nsyms * sizeof *res);
  273. qsort (res, nsyms, sizeof *res, (qcmp_type) &symbol_type_name_cmp);
  274. return res;
  275. }
  276. /*------------------------------------------------------------------.
  277. | Define b4_type_names, which is a list of (lists of the numbers of |
  278. | symbols with same type-name). |
  279. `------------------------------------------------------------------*/
  280. static void
  281. type_names_output (FILE *out)
  282. {
  283. int i;
  284. symbol **syms = symbols_by_type_name ();
  285. fputs ("m4_define([b4_type_names],\n[", out);
  286. for (i = 0; i < nsyms; /* nothing */)
  287. {
  288. /* The index of the first symbol of the current type-name. */
  289. int i0 = i;
  290. fputs (i ? ",\n[" : "[", out);
  291. for (; i < nsyms && syms[i]->type_name == syms[i0]->type_name; ++i)
  292. fprintf (out, "%s%d", i != i0 ? ", " : "", syms[i]->number);
  293. fputs ("]", out);
  294. }
  295. fputs ("])\n\n", out);
  296. free (syms);
  297. }
  298. /*-------------------------------------.
  299. | The list of all the symbol numbers. |
  300. `-------------------------------------*/
  301. static void
  302. symbol_numbers_output (FILE *out)
  303. {
  304. int i;
  305. fputs ("m4_define([b4_symbol_numbers],\n[", out);
  306. for (i = 0; i < nsyms; ++i)
  307. fprintf (out, "%s[%d]", i ? ", " : "", i);
  308. fputs ("])\n\n", out);
  309. }
  310. /*---------------------------------.
  311. | Output the user actions to OUT. |
  312. `---------------------------------*/
  313. static void
  314. user_actions_output (FILE *out)
  315. {
  316. rule_number r;
  317. fputs ("m4_define([b4_actions], \n[", out);
  318. for (r = 0; r < nrules; ++r)
  319. if (rules[r].action)
  320. {
  321. fprintf (out, "b4_%scase(%d, [b4_syncline(%d, ",
  322. rules[r].is_predicate ? "predicate_" : "",
  323. r + 1, rules[r].action_location.start.line);
  324. string_output (out, rules[r].action_location.start.file);
  325. fprintf (out, ")\n[ %s]])\n\n", rules[r].action);
  326. }
  327. fputs ("])\n\n", out);
  328. }
  329. /*------------------------------------.
  330. | Output the merge functions to OUT. |
  331. `------------------------------------*/
  332. static void
  333. merger_output (FILE *out)
  334. {
  335. int n;
  336. merger_list* p;
  337. fputs ("m4_define([b4_mergers], \n[[", out);
  338. for (n = 1, p = merge_functions; p != NULL; n += 1, p = p->next)
  339. {
  340. if (p->type[0] == '\0')
  341. fprintf (out, " case %d: *yy0 = %s (*yy0, *yy1); break;\n",
  342. n, p->name);
  343. else
  344. fprintf (out, " case %d: yy0->%s = %s (*yy0, *yy1); break;\n",
  345. n, p->type, p->name);
  346. }
  347. fputs ("]])\n\n", out);
  348. }
  349. /*---------------------------------------------.
  350. | Prepare the muscles for symbol definitions. |
  351. `---------------------------------------------*/
  352. static void
  353. prepare_symbol_definitions (void)
  354. {
  355. int i;
  356. for (i = 0; i < nsyms; ++i)
  357. {
  358. symbol *sym = symbols[i];
  359. const char *key;
  360. const char *value;
  361. #define SET_KEY(Entry) \
  362. obstack_printf (&format_obstack, "symbol(%d, %s)", \
  363. i, Entry); \
  364. key = obstack_finish0 (&format_obstack);
  365. #define SET_KEY2(Entry, Suffix) \
  366. obstack_printf (&format_obstack, "symbol(%d, %s_%s)", \
  367. i, Entry, Suffix); \
  368. key = obstack_finish0 (&format_obstack);
  369. /* Whether the symbol has an identifier. */
  370. value = symbol_id_get (sym);
  371. SET_KEY ("has_id");
  372. MUSCLE_INSERT_INT (key, !!value);
  373. /* Its identifier. */
  374. SET_KEY ("id");
  375. MUSCLE_INSERT_STRING (key, value ? value : "");
  376. /* Its tag. Typically for documentation purpose. */
  377. SET_KEY ("tag");
  378. MUSCLE_INSERT_STRING (key, sym->tag);
  379. SET_KEY ("user_number");
  380. MUSCLE_INSERT_INT (key, sym->user_token_number);
  381. SET_KEY ("is_token");
  382. MUSCLE_INSERT_INT (key,
  383. i < ntokens && sym != errtoken && sym != undeftoken);
  384. SET_KEY ("number");
  385. MUSCLE_INSERT_INT (key, sym->number);
  386. SET_KEY ("has_type");
  387. MUSCLE_INSERT_INT (key, !!sym->type_name);
  388. SET_KEY ("type");
  389. MUSCLE_INSERT_STRING (key, sym->type_name ? sym->type_name : "");
  390. {
  391. int j;
  392. for (j = 0; j < CODE_PROPS_SIZE; ++j)
  393. {
  394. /* "printer", not "%printer". */
  395. char const *pname = code_props_type_string (j) + 1;
  396. code_props const *p = symbol_code_props_get (sym, j);
  397. SET_KEY2 ("has", pname);
  398. MUSCLE_INSERT_INT (key, !!p->code);
  399. if (p->code)
  400. {
  401. SET_KEY2 (pname, "file");
  402. MUSCLE_INSERT_STRING (key, p->location.start.file);
  403. SET_KEY2 (pname, "line");
  404. MUSCLE_INSERT_INT (key, p->location.start.line);
  405. SET_KEY (pname);
  406. MUSCLE_INSERT_STRING_RAW (key, p->code);
  407. }
  408. }
  409. }
  410. #undef SET_KEY2
  411. #undef SET_KEY
  412. }
  413. }
  414. static void
  415. prepare_actions (void)
  416. {
  417. /* Figure out the actions for the specified state, indexed by
  418. lookahead token type. */
  419. muscle_insert_rule_number_table ("defact", yydefact,
  420. yydefact[0], 1, nstates);
  421. /* Figure out what to do after reducing with each rule, depending on
  422. the saved state from before the beginning of parsing the data
  423. that matched this rule. */
  424. muscle_insert_state_number_table ("defgoto", yydefgoto,
  425. yydefgoto[0], 1, nsyms - ntokens);
  426. /* Output PACT. */
  427. muscle_insert_base_table ("pact", base,
  428. base[0], 1, nstates);
  429. MUSCLE_INSERT_INT ("pact_ninf", base_ninf);
  430. /* Output PGOTO. */
  431. muscle_insert_base_table ("pgoto", base,
  432. base[nstates], nstates + 1, nvectors);
  433. muscle_insert_base_table ("table", table,
  434. table[0], 1, high + 1);
  435. MUSCLE_INSERT_INT ("table_ninf", table_ninf);
  436. muscle_insert_base_table ("check", check,
  437. check[0], 1, high + 1);
  438. /* GLR parsing slightly modifies YYTABLE and YYCHECK (and thus
  439. YYPACT) so that in states with unresolved conflicts, the default
  440. reduction is not used in the conflicted entries, so that there is
  441. a place to put a conflict pointer.
  442. This means that YYCONFLP and YYCONFL are nonsense for a non-GLR
  443. parser, so we could avoid accidents by not writing them out in
  444. that case. Nevertheless, it seems even better to be able to use
  445. the GLR skeletons even without the non-deterministic tables. */
  446. muscle_insert_unsigned_int_table ("conflict_list_heads", conflict_table,
  447. conflict_table[0], 1, high + 1);
  448. muscle_insert_unsigned_int_table ("conflicting_rules", conflict_list,
  449. 0, 1, conflict_list_cnt);
  450. }
  451. /*--------------------------------------------.
  452. | Output the definitions of all the muscles. |
  453. `--------------------------------------------*/
  454. static void
  455. muscles_output (FILE *out)
  456. {
  457. fputs ("m4_init()\n", out);
  458. merger_output (out);
  459. symbol_numbers_output (out);
  460. type_names_output (out);
  461. user_actions_output (out);
  462. /* Must be last. */
  463. muscles_m4_output (out);
  464. }
  465. /*---------------------------.
  466. | Call the skeleton parser. |
  467. `---------------------------*/
  468. static void
  469. output_skeleton (void)
  470. {
  471. int filter_fd[2];
  472. pid_t pid;
  473. /* Compute the names of the package data dir and skeleton files. */
  474. char const *m4 = (m4 = getenv ("M4")) ? m4 : M4;
  475. char const *datadir = pkgdatadir ();
  476. char *m4sugar = xconcatenated_filename (datadir, "m4sugar/m4sugar.m4", NULL);
  477. char *m4bison = xconcatenated_filename (datadir, "bison.m4", NULL);
  478. char *skel = (IS_PATH_WITH_DIR (skeleton)
  479. ? xstrdup (skeleton)
  480. : xconcatenated_filename (datadir, skeleton, NULL));
  481. /* Test whether m4sugar.m4 is readable, to check for proper
  482. installation. A faulty installation can cause deadlock, so a
  483. cheap sanity check is worthwhile. */
  484. xfclose (xfopen (m4sugar, "r"));
  485. /* Create an m4 subprocess connected to us via two pipes. */
  486. if (trace_flag & trace_tools)
  487. fprintf (stderr, "running: %s %s - %s %s\n",
  488. m4, m4sugar, m4bison, skel);
  489. /* Some future version of GNU M4 (most likely 1.6) may treat the -dV in a
  490. position-dependent manner. Keep it as the first argument so that all
  491. files are traced.
  492. See the thread starting at
  493. <http://lists.gnu.org/archive/html/bug-bison/2008-07/msg00000.html>
  494. for details. */
  495. {
  496. char const *argv[10];
  497. int i = 0;
  498. argv[i++] = m4;
  499. /* When POSIXLY_CORRECT is set, GNU M4 1.6 and later disable GNU
  500. extensions, which Bison's skeletons depend on. With older M4,
  501. it has no effect. M4 1.4.12 added a -g/--gnu command-line
  502. option to make it explicit that a program wants GNU M4
  503. extensions even when POSIXLY_CORRECT is set.
  504. See the thread starting at
  505. <http://lists.gnu.org/archive/html/bug-bison/2008-07/msg00000.html>
  506. for details. */
  507. if (*M4_GNU_OPTION)
  508. argv[i++] = M4_GNU_OPTION;
  509. argv[i++] = "-I";
  510. argv[i++] = datadir;
  511. if (trace_flag & trace_m4)
  512. argv[i++] = "-dV";
  513. argv[i++] = m4sugar;
  514. argv[i++] = "-";
  515. argv[i++] = m4bison;
  516. argv[i++] = skel;
  517. argv[i++] = NULL;
  518. aver (i <= ARRAY_CARDINALITY (argv));
  519. /* The ugly cast is because gnulib gets the const-ness wrong. */
  520. pid = create_pipe_bidi ("m4", m4, (char **)(void*)argv, false, true,
  521. true, filter_fd);
  522. }
  523. free (m4sugar);
  524. free (m4bison);
  525. free (skel);
  526. if (trace_flag & trace_muscles)
  527. muscles_output (stderr);
  528. {
  529. FILE *out = xfdopen (filter_fd[1], "w");
  530. muscles_output (out);
  531. xfclose (out);
  532. }
  533. /* Read and process m4's output. */
  534. timevar_push (TV_M4);
  535. {
  536. FILE *in = xfdopen (filter_fd[0], "r");
  537. scan_skel (in);
  538. /* scan_skel should have read all of M4's output. Otherwise, when we
  539. close the pipe, we risk letting M4 report a broken-pipe to the
  540. Bison user. */
  541. aver (feof (in));
  542. xfclose (in);
  543. }
  544. wait_subprocess (pid, "m4", false, false, true, true, NULL);
  545. timevar_pop (TV_M4);
  546. }
  547. static void
  548. prepare (void)
  549. {
  550. /* BISON_USE_PUSH_FOR_PULL is for the test suite and should not be
  551. documented for the user. */
  552. char const *cp = getenv ("BISON_USE_PUSH_FOR_PULL");
  553. bool use_push_for_pull_flag = cp && *cp && strtol (cp, 0, 10);
  554. /* Flags. */
  555. MUSCLE_INSERT_BOOL ("defines_flag", defines_flag);
  556. MUSCLE_INSERT_BOOL ("glr_flag", glr_parser);
  557. MUSCLE_INSERT_BOOL ("nondeterministic_flag", nondeterministic_parser);
  558. MUSCLE_INSERT_BOOL ("synclines_flag", !no_lines_flag);
  559. MUSCLE_INSERT_BOOL ("tag_seen_flag", tag_seen);
  560. MUSCLE_INSERT_BOOL ("token_table_flag", token_table_flag);
  561. MUSCLE_INSERT_BOOL ("use_push_for_pull_flag", use_push_for_pull_flag);
  562. MUSCLE_INSERT_BOOL ("yacc_flag", yacc_flag);
  563. /* File names. */
  564. if (spec_name_prefix)
  565. MUSCLE_INSERT_STRING ("prefix", spec_name_prefix);
  566. MUSCLE_INSERT_STRING ("file_name_all_but_ext", all_but_ext);
  567. #define DEFINE(Name) MUSCLE_INSERT_STRING (#Name, Name ? Name : "")
  568. DEFINE (dir_prefix);
  569. DEFINE (parser_file_name);
  570. DEFINE (spec_defines_file);
  571. DEFINE (spec_file_prefix);
  572. DEFINE (spec_graph_file);
  573. DEFINE (spec_name_prefix);
  574. DEFINE (spec_outfile);
  575. DEFINE (spec_verbose_file);
  576. #undef DEFINE
  577. /* Find the right skeleton file, and add muscles about the skeletons. */
  578. if (skeleton)
  579. MUSCLE_INSERT_C_STRING ("skeleton", skeleton);
  580. else
  581. skeleton = language->skeleton;
  582. /* About the skeletons. */
  583. {
  584. /* b4_pkgdatadir is used inside m4_include in the skeletons, so digraphs
  585. would never be expanded. Hopefully no one has M4-special characters in
  586. his Bison installation path. */
  587. MUSCLE_INSERT_STRING_RAW ("pkgdatadir", pkgdatadir ());
  588. }
  589. }
  590. /*----------------------------------------------------------.
  591. | Output the parsing tables and the parser code to ftable. |
  592. `----------------------------------------------------------*/
  593. void
  594. output (void)
  595. {
  596. obstack_init (&format_obstack);
  597. prepare_symbols ();
  598. prepare_rules ();
  599. prepare_states ();
  600. prepare_actions ();
  601. prepare_symbol_definitions ();
  602. prepare ();
  603. /* Process the selected skeleton file. */
  604. output_skeleton ();
  605. obstack_free (&format_obstack, NULL);
  606. }
  607. char const *
  608. pkgdatadir (void)
  609. {
  610. char const *cp = getenv ("BISON_PKGDATADIR");
  611. return cp ? cp : PKGDATADIR;
  612. }