1 /* Output the generated parsing program for Bison.
2
3 Copyright (C) 1984, 1986, 1989, 1992, 2000-2015, 2018-2021 Free
4 Software Foundation, Inc.
5
6 This file is part of Bison, the GNU Compiler Compiler.
7
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.
12
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.
17
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <https://www.gnu.org/licenses/>. */
20
21 #include <config.h>
22 #include "system.h"
23
24 #include <filename.h> /* IS_PATH_WITH_DIR */
25 #include <get-errno.h>
26 #include <mbswidth.h>
27 #include <path-join.h>
28 #include <quotearg.h>
29 #include <spawn-pipe.h>
30 #include <timevar.h>
31 #include <wait-process.h>
32
33 #include "complain.h"
34 #include "files.h"
35 #include "getargs.h"
36 #include "gram.h"
37 #include "muscle-tab.h"
38 #include "output.h"
39 #include "reader.h"
40 #include "reduce.h"
41 #include "scan-code.h" /* max_left_semantic_context */
42 #include "scan-skel.h"
43 #include "symtab.h"
44 #include "tables.h"
45 #include "strversion.h"
46
47 static struct obstack format_obstack;
48
49
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 | |
56 | For the typical case of outputting a complete table from 0, pass |
57 | TABLE[0] as FIRST, and 1 as BEGIN. For instance |
58 | muscle_insert_base_table ("pact", base, base[0], 1, nstates); |
59 `-------------------------------------------------------------------*/
60
61
62 #define GENERATE_MUSCLE_INSERT_TABLE(Name, Type) \
63 \
64 static void \
65 Name (char const *name, Type *table_data, Type first, \
66 int begin, int end) \
67 { \
68 Type min = first; \
69 Type max = first; \
70 int j = 1; \
71 \
72 obstack_printf (&format_obstack, "%6d", first); \
73 for (int i = begin; i < end; ++i) \
74 { \
75 obstack_1grow (&format_obstack, ','); \
76 if (j >= 10) \
77 { \
78 obstack_sgrow (&format_obstack, "\n "); \
79 j = 1; \
80 } \
81 else \
82 ++j; \
83 obstack_printf (&format_obstack, "%6d", table_data[i]); \
84 if (table_data[i] < min) \
85 min = table_data[i]; \
86 if (max < table_data[i]) \
87 max = table_data[i]; \
88 } \
89 muscle_insert (name, obstack_finish0 (&format_obstack)); \
90 \
91 long lmin = min; \
92 long lmax = max; \
93 /* Build 'NAME_min' and 'NAME_max' in the obstack. */ \
94 obstack_printf (&format_obstack, "%s_min", name); \
95 MUSCLE_INSERT_LONG_INT (obstack_finish0 (&format_obstack), lmin); \
96 obstack_printf (&format_obstack, "%s_max", name); \
97 MUSCLE_INSERT_LONG_INT (obstack_finish0 (&format_obstack), lmax); \
98 }
99
GENERATE_MUSCLE_INSERT_TABLE(muscle_insert_int_table,int)100 GENERATE_MUSCLE_INSERT_TABLE (muscle_insert_int_table, int)
101 GENERATE_MUSCLE_INSERT_TABLE (muscle_insert_base_table, base_number)
102 GENERATE_MUSCLE_INSERT_TABLE (muscle_insert_rule_number_table, rule_number)
103 GENERATE_MUSCLE_INSERT_TABLE (muscle_insert_symbol_number_table, symbol_number)
104 GENERATE_MUSCLE_INSERT_TABLE (muscle_insert_item_number_table, item_number)
105 GENERATE_MUSCLE_INSERT_TABLE (muscle_insert_state_number_table, state_number)
106
107 /*----------------------------------------------------------------.
108 | Print to OUT a representation of CP quoted and escaped for M4. |
109 `----------------------------------------------------------------*/
110
111 static void
112 output_escaped (FILE *out, const char *cp)
113 {
114 for (; *cp; cp++)
115 switch (*cp)
116 {
117 case '$': fputs ("$][", out); break;
118 case '@': fputs ("@@", out); break;
119 case '[': fputs ("@{", out); break;
120 case ']': fputs ("@}", out); break;
121 default: fputc (*cp, out); break;
122 }
123 }
124
125 static void
output_quoted(FILE * out,char const * cp)126 output_quoted (FILE *out, char const *cp)
127 {
128 fprintf (out, "[[");
129 output_escaped (out, cp);
130 fprintf (out, "]]");
131 }
132
133 /*----------------------------------------------------------------.
134 | Print to OUT a representation of STRING quoted and escaped both |
135 | for C and M4. |
136 `----------------------------------------------------------------*/
137
138 static void
string_output(FILE * out,char const * string)139 string_output (FILE *out, char const *string)
140 {
141 output_quoted (out, quotearg_style (c_quoting_style, string));
142 }
143
144
145 /* Store in BUFFER a copy of SRC where trigraphs are escaped, return
146 the size of the result (including the final NUL). If called with
147 BUFFERSIZE = 0, returns the needed size for BUFFER. */
148 static ptrdiff_t
escape_trigraphs(char * buffer,ptrdiff_t buffersize,const char * src)149 escape_trigraphs (char *buffer, ptrdiff_t buffersize, const char *src)
150 {
151 #define STORE(c) \
152 do \
153 { \
154 if (res < buffersize) \
155 buffer[res] = (c); \
156 ++res; \
157 } \
158 while (0)
159 ptrdiff_t res = 0;
160 for (ptrdiff_t i = 0, len = strlen (src); i < len; ++i)
161 {
162 if (i + 2 < len
163 && src[i] == '?' && src[i+1] == '?')
164 {
165 switch (src[i+2])
166 {
167 case '!': case '\'':
168 case '(': case ')': case '-': case '/':
169 case '<': case '=': case '>':
170 i += 1;
171 STORE ('?');
172 STORE ('"');
173 STORE ('"');
174 STORE ('?');
175 continue;
176 }
177 }
178 STORE (src[i]);
179 }
180 STORE ('\0');
181 #undef STORE
182 return res;
183 }
184
185 /* Same as xstrdup, except that trigraphs are escaped. */
186 static char *
xescape_trigraphs(const char * src)187 xescape_trigraphs (const char *src)
188 {
189 ptrdiff_t bufsize = escape_trigraphs (NULL, 0, src);
190 char *buf = xcharalloc (bufsize);
191 escape_trigraphs (buf, bufsize, src);
192 return buf;
193 }
194
195 /* The tag to show in the generated parsers. Use "end of file" rather
196 than "$end". But keep "$end" in the reports, it's shorter and more
197 consistent. Support i18n if the user already uses it. */
198 static const char *
symbol_tag(const symbol * sym)199 symbol_tag (const symbol *sym)
200 {
201 const bool eof_is_user_defined
202 = !eoftoken->alias || STRNEQ (eoftoken->alias->tag, "$end");
203
204 if (!eof_is_user_defined && sym->content == eoftoken->content)
205 return "\"end of file\"";
206 else if (sym->content == undeftoken->content)
207 return "\"invalid token\"";
208 else
209 return sym->tag;
210 }
211
212 /* Generate the b4_<MUSCLE_NAME> (e.g., b4_tname) table with the
213 symbol names (aka tags). */
214
215 static void
prepare_symbol_names(char const * muscle_name)216 prepare_symbol_names (char const *muscle_name)
217 {
218 // Whether to add a pair of quotes around the name.
219 const bool quote = STREQ (muscle_name, "tname");
220 bool has_translations = false;
221
222 /* We assume that the table will be output starting at column 2. */
223 int col = 2;
224 struct quoting_options *qo = clone_quoting_options (0);
225 set_quoting_style (qo, c_quoting_style);
226 set_quoting_flags (qo, QA_SPLIT_TRIGRAPHS);
227 for (int i = 0; i < nsyms; i++)
228 {
229 const char *tag = symbol_tag (symbols[i]);
230 bool translatable = !quote && symbols[i]->translatable;
231 if (translatable)
232 has_translations = true;
233
234 char *cp
235 = tag[0] == '"' && !quote
236 ? xescape_trigraphs (tag)
237 : quotearg_alloc (tag, -1, qo);
238 /* Width of the next token, including the two quotes, the
239 comma and the space. */
240 int width
241 = mbswidth (cp, 0) + 2
242 + (translatable ? strlen ("N_()") : 0);
243
244 if (col + width > 75)
245 {
246 obstack_sgrow (&format_obstack, "\n ");
247 col = 1;
248 }
249
250 if (i)
251 obstack_1grow (&format_obstack, ' ');
252 if (translatable)
253 obstack_sgrow (&format_obstack, "]b4_symbol_translate""([");
254 obstack_escape (&format_obstack, cp);
255 if (translatable)
256 obstack_sgrow (&format_obstack, "])[");
257 free (cp);
258 obstack_1grow (&format_obstack, ',');
259 col += width;
260 }
261 free (qo);
262 obstack_sgrow (&format_obstack, " ]b4_null[");
263
264 /* Finish table and store. */
265 muscle_insert (muscle_name, obstack_finish0 (&format_obstack));
266
267 /* Announce whether translation support is needed. */
268 MUSCLE_INSERT_BOOL ("has_translations_flag", has_translations);
269 }
270
271
272 /*------------------------------------------------------------------.
273 | Prepare the muscles related to the symbols: translate, tname, and |
274 | toknum. |
275 `------------------------------------------------------------------*/
276
277 static void
prepare_symbols(void)278 prepare_symbols (void)
279 {
280 MUSCLE_INSERT_INT ("tokens_number", ntokens);
281 MUSCLE_INSERT_INT ("nterms_number", nnterms);
282 MUSCLE_INSERT_INT ("symbols_number", nsyms);
283 MUSCLE_INSERT_INT ("code_max", max_code);
284
285 muscle_insert_symbol_number_table ("translate",
286 token_translations,
287 token_translations[0],
288 1, max_code + 1);
289
290 /* tname -- token names. */
291 prepare_symbol_names ("tname");
292 prepare_symbol_names ("symbol_names");
293
294 /* translatable -- whether a token is translatable. */
295 {
296 bool translatable = false;
297 for (int i = 0; i < ntokens; ++i)
298 if (symbols[i]->translatable)
299 {
300 translatable = true;
301 break;
302 }
303 if (translatable)
304 {
305 int *values = xnmalloc (nsyms, sizeof *values);
306 for (int i = 0; i < ntokens; ++i)
307 values[i] = symbols[i]->translatable;
308 muscle_insert_int_table ("translatable", values,
309 values[0], 1, ntokens);
310 free (values);
311 }
312 }
313
314 /* Output YYTOKNUM. */
315 {
316 int *values = xnmalloc (ntokens, sizeof *values);
317 for (int i = 0; i < ntokens; ++i)
318 values[i] = symbols[i]->content->code;
319 muscle_insert_int_table ("toknum", values,
320 values[0], 1, ntokens);
321 free (values);
322 }
323 }
324
325
326 /*-------------------------------------------------------------.
327 | Prepare the muscles related to the rules: rhs, prhs, r1, r2, |
328 | rline, dprec, merger, immediate. |
329 `-------------------------------------------------------------*/
330
331 static void
prepare_rules(void)332 prepare_rules (void)
333 {
334 int *prhs = xnmalloc (nrules, sizeof *prhs);
335 item_number *rhs = xnmalloc (nritems, sizeof *rhs);
336 int *rline = xnmalloc (nrules, sizeof *rline);
337 symbol_number *r1 = xnmalloc (nrules, sizeof *r1);
338 int *r2 = xnmalloc (nrules, sizeof *r2);
339 int *dprec = xnmalloc (nrules, sizeof *dprec);
340 int *merger = xnmalloc (nrules, sizeof *merger);
341 int *immediate = xnmalloc (nrules, sizeof *immediate);
342
343 /* Index in RHS. */
344 int i = 0;
345 for (rule_number r = 0; r < nrules; ++r)
346 {
347 /* Index of rule R in RHS. */
348 prhs[r] = i;
349 /* RHS of the rule R. */
350 for (item_number *rhsp = rules[r].rhs; 0 <= *rhsp; ++rhsp)
351 rhs[i++] = *rhsp;
352 /* Separator in RHS. */
353 rhs[i++] = -1;
354
355 /* Line where rule was defined. */
356 rline[r] = rules[r].location.start.line;
357 /* LHS of the rule R. */
358 r1[r] = rules[r].lhs->number;
359 /* Length of rule R's RHS. */
360 r2[r] = rule_rhs_length (&rules[r]);
361 /* Dynamic precedence (GLR). */
362 dprec[r] = rules[r].dprec;
363 /* Merger-function index (GLR). */
364 merger[r] = rules[r].merger;
365 /* Immediate reduction flags (GLR). */
366 immediate[r] = rules[r].is_predicate;
367 }
368 aver (i == nritems);
369
370 muscle_insert_item_number_table ("rhs", rhs, ritem[0], 1, nritems);
371 muscle_insert_int_table ("prhs", prhs, 0, 0, nrules);
372 muscle_insert_int_table ("rline", rline, 0, 0, nrules);
373 muscle_insert_symbol_number_table ("r1", r1, 0, 0, nrules);
374 muscle_insert_int_table ("r2", r2, 0, 0, nrules);
375 muscle_insert_int_table ("dprec", dprec, 0, 0, nrules);
376 muscle_insert_int_table ("merger", merger, 0, 0, nrules);
377 muscle_insert_int_table ("immediate", immediate, 0, 0, nrules);
378
379 MUSCLE_INSERT_INT ("rules_number", nrules);
380 MUSCLE_INSERT_INT ("max_left_semantic_context", max_left_semantic_context);
381
382 free (prhs);
383 free (rhs);
384 free (rline);
385 free (r1);
386 free (r2);
387 free (dprec);
388 free (merger);
389 free (immediate);
390 }
391
392 /*--------------------------------------------.
393 | Prepare the muscles related to the states. |
394 `--------------------------------------------*/
395
396 static void
prepare_states(void)397 prepare_states (void)
398 {
399 symbol_number *values = xnmalloc (nstates, sizeof *values);
400 for (state_number i = 0; i < nstates; ++i)
401 values[i] = states[i]->accessing_symbol;
402 muscle_insert_symbol_number_table ("stos", values,
403 0, 1, nstates);
404 free (values);
405
406 MUSCLE_INSERT_INT ("last", high);
407 MUSCLE_INSERT_INT ("final_state_number", final_state->number);
408 MUSCLE_INSERT_INT ("states_number", nstates);
409 }
410
411
412 /*-------------------------------------------------------.
413 | Compare two symbols by type-name, and then by number. |
414 `-------------------------------------------------------*/
415
416 static int
symbol_type_name_cmp(const symbol ** lhs,const symbol ** rhs)417 symbol_type_name_cmp (const symbol **lhs, const symbol **rhs)
418 {
419 int res = uniqstr_cmp ((*lhs)->content->type_name, (*rhs)->content->type_name);
420 if (!res)
421 res = (*lhs)->content->number - (*rhs)->content->number;
422 return res;
423 }
424
425
426 /*----------------------------------------------------------------.
427 | Return a (malloc'ed) table of the symbols sorted by type-name. |
428 `----------------------------------------------------------------*/
429
430 static symbol **
symbols_by_type_name(void)431 symbols_by_type_name (void)
432 {
433 typedef int (*qcmp_type) (const void *, const void *);
434 symbol **res = xmemdup (symbols, nsyms * sizeof *res);
435 qsort (res, nsyms, sizeof *res, (qcmp_type) &symbol_type_name_cmp);
436 return res;
437 }
438
439
440 /*------------------------------------------------------------------.
441 | Define b4_type_names, which is a list of (lists of the numbers of |
442 | symbols with same type-name). |
443 `------------------------------------------------------------------*/
444
445 static void
type_names_output(FILE * out)446 type_names_output (FILE *out)
447 {
448 symbol **syms = symbols_by_type_name ();
449 fputs ("m4_define([b4_type_names],\n[", out);
450 for (int i = 0; i < nsyms; /* nothing */)
451 {
452 /* The index of the first symbol of the current type-name. */
453 int i0 = i;
454 fputs (i ? ",\n[" : "[", out);
455 for (; i < nsyms
456 && syms[i]->content->type_name == syms[i0]->content->type_name; ++i)
457 fprintf (out, "%s%d", i != i0 ? ", " : "", syms[i]->content->number);
458 fputs ("]", out);
459 }
460 fputs ("])\n\n", out);
461 free (syms);
462 }
463
464
465 /*-------------------------------------.
466 | The list of all the symbol numbers. |
467 `-------------------------------------*/
468
469 static void
symbol_numbers_output(FILE * out)470 symbol_numbers_output (FILE *out)
471 {
472 fputs ("m4_define([b4_symbol_numbers],\n[", out);
473 for (int i = 0; i < nsyms; ++i)
474 fprintf (out, "%s[%d]", i ? ", " : "", i);
475 fputs ("])\n\n", out);
476 }
477
478
479 /*-------------------------------------------.
480 | Output the user reduction actions to OUT. |
481 `-------------------------------------------*/
482
483 static void
rule_output(const rule * r,FILE * out)484 rule_output (const rule *r, FILE *out)
485 {
486 output_escaped (out, r->lhs->symbol->tag);
487 fputc (':', out);
488 if (0 <= *r->rhs)
489 for (item_number *rhsp = r->rhs; 0 <= *rhsp; ++rhsp)
490 {
491 fputc (' ', out);
492 output_escaped (out, symbols[*rhsp]->tag);
493 }
494 else
495 fputs (" %empty", out);
496 }
497
498 static void
user_actions_output(FILE * out)499 user_actions_output (FILE *out)
500 {
501 fputs ("m4_define([b4_actions], \n[", out);
502 for (rule_number r = 0; r < nrules; ++r)
503 if (rules[r].action)
504 {
505 /* The useless "" is there to pacify syntax-check. */
506 fprintf (out, "%s""(%d, [",
507 rules[r].is_predicate ? "b4_predicate_case" : "b4_case",
508 r + 1);
509 if (!no_lines_flag)
510 {
511 fprintf (out, "b4_syncline(%d, ",
512 rules[r].action_loc.start.line);
513 string_output (out, rules[r].action_loc.start.file);
514 fprintf (out, ")dnl\n");
515 }
516 fprintf (out, "[%*s%s]],\n[[",
517 rules[r].action_loc.start.column - 1, "",
518 rules[r].action);
519 rule_output (&rules[r], out);
520 fprintf (out, "]])\n\n");
521 }
522 fputs ("])\n\n", out);
523 }
524
525 /*------------------------------------.
526 | Output the merge functions to OUT. |
527 `------------------------------------*/
528
529 static void
merger_output(FILE * out)530 merger_output (FILE *out)
531 {
532 fputs ("m4_define([b4_mergers], \n[[", out);
533 int n;
534 merger_list* p;
535 for (n = 1, p = merge_functions; p != NULL; n += 1, p = p->next)
536 fprintf (out, "]b4_call_merger""([%d], [%s], [%d])[\n",
537 n, p->name, p->sym->content->number);
538 fputs ("]])\n\n", out);
539 }
540
541
542 /*---------------------------------------------.
543 | Prepare the muscles for symbol definitions. |
544 `---------------------------------------------*/
545
546 static void
prepare_symbol_definitions(void)547 prepare_symbol_definitions (void)
548 {
549 /* Map "orig NUM" to new numbers. See data/README. */
550 for (symbol_number i = ntokens; i < nsyms + nuseless_nonterminals; ++i)
551 {
552 obstack_printf (&format_obstack, "symbol""(orig %d, number)", i);
553 const char *key = obstack_finish0 (&format_obstack);
554 MUSCLE_INSERT_INT (key, nterm_map ? nterm_map[i - ntokens] : i);
555 }
556
557 for (int i = 0; i < nsyms; ++i)
558 {
559 symbol *sym = symbols[i];
560 const char *key;
561
562 #define SET_KEY(Entry) \
563 obstack_printf (&format_obstack, "symbol""(%d, %s)", \
564 i, Entry); \
565 key = obstack_finish0 (&format_obstack);
566
567 #define SET_KEY2(Entry, Suffix) \
568 obstack_printf (&format_obstack, "symbol""(%d, %s_%s)", \
569 i, Entry, Suffix); \
570 key = obstack_finish0 (&format_obstack);
571
572 /* Whether the symbol has an identifier. */
573 const char *id = symbol_id_get (sym);
574 SET_KEY ("has_id");
575 MUSCLE_INSERT_INT (key, !!id);
576
577 /* Its identifier. */
578 SET_KEY ("id");
579 MUSCLE_INSERT_STRING (key, id ? id : "");
580
581 /* Its tag. Typically for documentation purpose. */
582 SET_KEY ("tag");
583 MUSCLE_INSERT_STRING (key, symbol_tag (sym));
584
585 SET_KEY ("code");
586 MUSCLE_INSERT_INT (key, sym->content->code);
587
588 SET_KEY ("is_token");
589 MUSCLE_INSERT_INT (key, i < ntokens);
590
591 SET_KEY ("number");
592 MUSCLE_INSERT_INT (key, sym->content->number);
593
594 SET_KEY ("has_type");
595 MUSCLE_INSERT_INT (key, !!sym->content->type_name);
596
597 SET_KEY ("type");
598 MUSCLE_INSERT_STRING (key, sym->content->type_name
599 ? sym->content->type_name : "");
600
601 for (int j = 0; j < CODE_PROPS_SIZE; ++j)
602 {
603 /* "printer", not "%printer". */
604 char const *pname = code_props_type_string (j) + 1;
605 code_props const *p = symbol_code_props_get (sym, j);
606 SET_KEY2 ("has", pname);
607 MUSCLE_INSERT_INT (key, !!p->code);
608
609 if (p->code)
610 {
611 SET_KEY2 (pname, "file");
612 MUSCLE_INSERT_C_STRING (key, p->location.start.file);
613
614 SET_KEY2 (pname, "line");
615 MUSCLE_INSERT_INT (key, p->location.start.line);
616
617 SET_KEY2 (pname, "loc");
618 muscle_location_grow (key, p->location);
619
620 SET_KEY (pname);
621 obstack_printf (&muscle_obstack,
622 "%*s%s", p->location.start.column - 1, "", p->code);
623 muscle_insert (key, obstack_finish0 (&muscle_obstack));
624 }
625 }
626 #undef SET_KEY2
627 #undef SET_KEY
628 }
629 }
630
631
632 static void
prepare_actions(void)633 prepare_actions (void)
634 {
635 /* Figure out the actions for the specified state. */
636 muscle_insert_rule_number_table ("defact", yydefact,
637 yydefact[0], 1, nstates);
638
639 /* Figure out what to do after reducing with each rule, depending on
640 the saved state from before the beginning of parsing the data
641 that matched this rule. */
642 muscle_insert_state_number_table ("defgoto", yydefgoto,
643 yydefgoto[0], 1, nsyms - ntokens);
644
645
646 /* Output PACT. */
647 muscle_insert_base_table ("pact", base,
648 base[0], 1, nstates);
649 MUSCLE_INSERT_INT ("pact_ninf", base_ninf);
650
651 /* Output PGOTO. */
652 muscle_insert_base_table ("pgoto", base,
653 base[nstates], nstates + 1, nvectors);
654
655 muscle_insert_base_table ("table", table,
656 table[0], 1, high + 1);
657 MUSCLE_INSERT_INT ("table_ninf", table_ninf);
658
659 muscle_insert_base_table ("check", check,
660 check[0], 1, high + 1);
661
662 /* GLR parsing slightly modifies YYTABLE and YYCHECK (and thus
663 YYPACT) so that in states with unresolved conflicts, the default
664 reduction is not used in the conflicted entries, so that there is
665 a place to put a conflict pointer.
666
667 This means that YYCONFLP and YYCONFL are nonsense for a non-GLR
668 parser, so we could avoid accidents by not writing them out in
669 that case. Nevertheless, it seems even better to be able to use
670 the GLR skeletons even without the non-deterministic tables. */
671 muscle_insert_int_table ("conflict_list_heads", conflict_table,
672 conflict_table[0], 1, high + 1);
673 muscle_insert_int_table ("conflicting_rules", conflict_list,
674 0, 1, conflict_list_cnt);
675 }
676
677
678 /*--------------------------------------------.
679 | Output the definitions of all the muscles. |
680 `--------------------------------------------*/
681
682 static void
muscles_output(FILE * out)683 muscles_output (FILE *out)
684 {
685 fputs ("m4_init()\n", out);
686 merger_output (out);
687 symbol_numbers_output (out);
688 type_names_output (out);
689 user_actions_output (out);
690 /* Must be last. */
691 muscles_m4_output (out);
692 }
693
694 /*---------------------------.
695 | Call the skeleton parser. |
696 `---------------------------*/
697
698 static void
output_skeleton(void)699 output_skeleton (void)
700 {
701 /* Compute the names of the package data dir and skeleton files. */
702 char const *m4 = m4path ();
703 char const *datadir = pkgdatadir ();
704 char *skeldir = xpath_join (datadir, "skeletons");
705 char *m4sugar = xpath_join (datadir, "m4sugar/m4sugar.m4");
706 char *m4bison = xpath_join (skeldir, "bison.m4");
707 char *traceon = xpath_join (skeldir, "traceon.m4");
708 char *skel = (IS_PATH_WITH_DIR (skeleton)
709 ? xstrdup (skeleton)
710 : xpath_join (skeldir, skeleton));
711
712 /* Test whether m4sugar.m4 is readable, to check for proper
713 installation. A faulty installation can cause deadlock, so a
714 cheap sanity check is worthwhile. */
715 xfclose (xfopen (m4sugar, "r"));
716
717 /* Create an m4 subprocess connected to us via two pipes. */
718
719 int filter_fd[2];
720 pid_t pid;
721 {
722 char const *argv[11];
723 int i = 0;
724 argv[i++] = m4;
725
726 /* When POSIXLY_CORRECT is set, GNU M4 1.6 and later disable GNU
727 extensions, which Bison's skeletons depend on. With older M4,
728 it has no effect. M4 1.4.12 added a -g/--gnu command-line
729 option to make it explicit that a program wants GNU M4
730 extensions even when POSIXLY_CORRECT is set.
731
732 See the thread starting at
733 <https://lists.gnu.org/r/bug-bison/2008-07/msg00000.html>
734 for details. */
735 if (*M4_GNU_OPTION)
736 argv[i++] = M4_GNU_OPTION;
737
738 argv[i++] = "-I";
739 argv[i++] = datadir;
740 /* Some future version of GNU M4 (most likely 1.6) may treat the
741 -dV in a position-dependent manner. See the thread starting at
742 <https://lists.gnu.org/r/bug-bison/2008-07/msg00000.html>
743 for details. */
744 if (trace_flag & trace_m4_early)
745 argv[i++] = "-dV";
746 argv[i++] = m4sugar;
747 argv[i++] = "-";
748 argv[i++] = m4bison;
749 if (trace_flag & trace_m4)
750 argv[i++] = traceon;
751 argv[i++] = skel;
752 argv[i++] = NULL;
753 aver (i <= ARRAY_CARDINALITY (argv));
754
755 if (trace_flag & trace_tools)
756 {
757 fputs ("running:", stderr);
758 for (int j = 0; argv[j]; ++j)
759 fprintf (stderr, " %s", argv[j]);
760 fputc ('\n', stderr);
761 }
762
763 /* The ugly cast is because gnulib gets the const-ness wrong. */
764 pid = create_pipe_bidi ("m4", m4, (char **)(void*)argv, false, true,
765 true, filter_fd);
766 }
767
768 free (skeldir);
769 free (m4sugar);
770 free (m4bison);
771 free (traceon);
772 free (skel);
773
774 if (trace_flag & trace_muscles)
775 muscles_output (stderr);
776 {
777 FILE *out = xfdopen (filter_fd[1], "w");
778 muscles_output (out);
779 xfclose (out);
780 }
781
782 /* Read and process m4's output. */
783 timevar_push (tv_m4);
784 {
785 FILE *in = xfdopen (filter_fd[0], "r");
786 scan_skel (in);
787 /* scan_skel should have read all of M4's output. Otherwise, when we
788 close the pipe, we risk letting M4 report a broken-pipe to the
789 Bison user. */
790 aver (feof (in));
791 xfclose (in);
792 }
793 wait_subprocess (pid, "m4", false, false, true, true, NULL);
794 timevar_pop (tv_m4);
795 }
796
797 static void
prepare(void)798 prepare (void)
799 {
800 /* BISON_USE_PUSH_FOR_PULL is for the test suite and should not be
801 documented for the user. */
802 char const *cp = getenv ("BISON_USE_PUSH_FOR_PULL");
803 bool use_push_for_pull_flag = cp && *cp && strtol (cp, 0, 10);
804
805 /* Versions. */
806 MUSCLE_INSERT_STRING ("version_string", VERSION);
807 MUSCLE_INSERT_INT ("version", strversion_to_int (VERSION));
808 MUSCLE_INSERT_INT ("required_version", required_version);
809
810 /* Flags. */
811 MUSCLE_INSERT_BOOL ("defines_flag", defines_flag);
812 MUSCLE_INSERT_BOOL ("glr_flag", glr_parser);
813 MUSCLE_INSERT_BOOL ("nondeterministic_flag", nondeterministic_parser);
814 MUSCLE_INSERT_BOOL ("synclines_flag", !no_lines_flag);
815 MUSCLE_INSERT_BOOL ("tag_seen_flag", tag_seen);
816 MUSCLE_INSERT_BOOL ("token_table_flag", token_table_flag);
817 MUSCLE_INSERT_BOOL ("use_push_for_pull_flag", use_push_for_pull_flag);
818 MUSCLE_INSERT_BOOL ("yacc_flag", !location_empty (yacc_loc));
819
820 /* File names. */
821 if (spec_name_prefix)
822 MUSCLE_INSERT_STRING ("prefix", spec_name_prefix);
823
824 MUSCLE_INSERT_STRING ("file_name_all_but_ext", all_but_ext);
825
826 #define DEFINE(Name) MUSCLE_INSERT_STRING (#Name, Name ? Name : "")
827 DEFINE (dir_prefix);
828 DEFINE (mapped_dir_prefix);
829 DEFINE (parser_file_name);
830 DEFINE (spec_header_file);
831 DEFINE (spec_mapped_header_file);
832 DEFINE (spec_file_prefix);
833 DEFINE (spec_graph_file);
834 DEFINE (spec_name_prefix);
835 DEFINE (spec_outfile);
836 DEFINE (spec_verbose_file);
837 #undef DEFINE
838
839 /* Find the right skeleton file, and add muscles about the skeletons. */
840 if (skeleton)
841 MUSCLE_INSERT_C_STRING ("skeleton", skeleton);
842 else
843 skeleton = language->skeleton;
844
845 /* About the skeletons. */
846 {
847 /* b4_skeletonsdir is used inside m4_include in the skeletons, so digraphs
848 would never be expanded. Hopefully no one has M4-special characters in
849 his Bison installation path. */
850 char *skeldir = xpath_join (pkgdatadir (), "skeletons");
851 MUSCLE_INSERT_STRING_RAW ("skeletonsdir", skeldir);
852 free (skeldir);
853 }
854 }
855
856
857 /*----------------------------------------------------------.
858 | Output the parsing tables and the parser code to ftable. |
859 `----------------------------------------------------------*/
860
861 void
output(void)862 output (void)
863 {
864 obstack_init (&format_obstack);
865
866 prepare_symbols ();
867 prepare_rules ();
868 prepare_states ();
869 prepare_actions ();
870 prepare_symbol_definitions ();
871
872 prepare ();
873
874 /* Process the selected skeleton file. */
875 output_skeleton ();
876
877 /* If late errors were generated, destroy the generated source
878 files. */
879 if (complaint_status)
880 unlink_generated_sources ();
881
882 obstack_free (&format_obstack, NULL);
883 }
884