1 /* Output the generated parsing program for Bison.
3 Copyright (C) 1984, 1986, 1989, 1992, 2000-2013 Free Software
6 This file is part of Bison, the GNU Compiler Compiler.
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.
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.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
24 #include <concat-filename.h>
25 #include <configmake.h>
27 #include <get-errno.h>
29 #include <spawn-pipe.h>
31 #include <wait-process.h>
37 #include "muscle-tab.h"
40 #include "scan-code.h" /* max_left_semantic_context */
41 #include "scan-skel.h"
45 static struct obstack format_obstack
;
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 |
53 `-------------------------------------------------------------------*/
56 #define GENERATE_MUSCLE_INSERT_TABLE(Name, Type) \
59 Name (char const *name, \
72 obstack_printf (&format_obstack, "%6d", first); \
73 for (i = begin; i < end; ++i) \
75 obstack_1grow (&format_obstack, ','); \
78 obstack_sgrow (&format_obstack, "\n "); \
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]; \
89 muscle_insert (name, obstack_finish0 (&format_obstack)); \
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); \
100 GENERATE_MUSCLE_INSERT_TABLE (muscle_insert_unsigned_int_table
, unsigned int)
101 GENERATE_MUSCLE_INSERT_TABLE (muscle_insert_int_table
, int)
102 GENERATE_MUSCLE_INSERT_TABLE (muscle_insert_base_table
, base_number
)
103 GENERATE_MUSCLE_INSERT_TABLE (muscle_insert_rule_number_table
, rule_number
)
104 GENERATE_MUSCLE_INSERT_TABLE (muscle_insert_symbol_number_table
, symbol_number
)
105 GENERATE_MUSCLE_INSERT_TABLE (muscle_insert_state_number_table
, state_number
)
107 /*----------------------------------------------------------------.
108 | Print to OUT a representation of CP quoted and escaped for M4. |
109 `----------------------------------------------------------------*/
112 quoted_output (FILE *out
, char const *cp
)
119 case '$': fputs ("$][", out
); break;
120 case '@': fputs ("@@", out
); break;
121 case '[': fputs ("@{", out
); break;
122 case ']': fputs ("@}", out
); break;
123 default: fputc (*cp
, out
); break;
129 /*----------------------------------------------------------------.
130 | Print to OUT a representation of STRING quoted and escaped both |
132 `----------------------------------------------------------------*/
135 string_output (FILE *out
, char const *string
)
137 quoted_output (out
, quotearg_style (c_quoting_style
, string
));
141 /*------------------------------------------------------------------.
142 | Prepare the muscles related to the symbols: translate, tname, and |
144 `------------------------------------------------------------------*/
147 prepare_symbols (void)
149 MUSCLE_INSERT_INT ("tokens_number", ntokens
);
150 MUSCLE_INSERT_INT ("nterms_number", nvars
);
151 MUSCLE_INSERT_INT ("symbols_number", nsyms
);
152 MUSCLE_INSERT_INT ("undef_token_number", undeftoken
->number
);
153 MUSCLE_INSERT_INT ("user_token_number_max", max_user_token_number
);
155 muscle_insert_symbol_number_table ("translate",
157 token_translations
[0],
158 1, max_user_token_number
+ 1);
160 /* tname -- token names. */
163 /* We assume that the table will be output starting at column 2. */
165 struct quoting_options
*qo
= clone_quoting_options (0);
166 set_quoting_style (qo
, c_quoting_style
);
167 set_quoting_flags (qo
, QA_SPLIT_TRIGRAPHS
);
168 for (i
= 0; i
< nsyms
; i
++)
170 char *cp
= quotearg_alloc (symbols
[i
]->tag
, -1, qo
);
171 /* Width of the next token, including the two quotes, the
172 comma and the space. */
173 int width
= strlen (cp
) + 2;
177 obstack_sgrow (&format_obstack
, "\n ");
182 obstack_1grow (&format_obstack
, ' ');
183 obstack_escape (&format_obstack
, cp
);
185 obstack_1grow (&format_obstack
, ',');
189 obstack_sgrow (&format_obstack
, " ]b4_null[");
191 /* Finish table and store. */
192 muscle_insert ("tname", obstack_finish0 (&format_obstack
));
195 /* Output YYTOKNUM. */
198 int *values
= xnmalloc (ntokens
, sizeof *values
);
199 for (i
= 0; i
< ntokens
; ++i
)
200 values
[i
] = symbols
[i
]->user_token_number
;
201 muscle_insert_int_table ("toknum", values
,
202 values
[0], 1, ntokens
);
208 /*----------------------------------------------------------------.
209 | Prepare the muscles related to the rules: r1, r2, rline, dprec, |
210 | merger, immediate. |
211 `----------------------------------------------------------------*/
216 unsigned int *rline
= xnmalloc (nrules
, sizeof *rline
);
217 symbol_number
*r1
= xnmalloc (nrules
, sizeof *r1
);
218 unsigned int *r2
= xnmalloc (nrules
, sizeof *r2
);
219 int *dprec
= xnmalloc (nrules
, sizeof *dprec
);
220 int *merger
= xnmalloc (nrules
, sizeof *merger
);
221 int *immediate
= xnmalloc (nrules
, sizeof *immediate
);
224 for (r
= 0; r
< nrules
; ++r
)
226 /* LHS of the rule R. */
227 r1
[r
] = rules
[r
].lhs
->number
;
228 /* Length of rule R's RHS. */
229 r2
[r
] = rule_rhs_length (&rules
[r
]);
230 /* Line where rule was defined. */
231 rline
[r
] = rules
[r
].location
.start
.line
;
232 /* Dynamic precedence (GLR). */
233 dprec
[r
] = rules
[r
].dprec
;
234 /* Merger-function index (GLR). */
235 merger
[r
] = rules
[r
].merger
;
236 /* Immediate reduction flags (GLR). */
237 immediate
[r
] = rules
[r
].is_predicate
;
240 muscle_insert_unsigned_int_table ("rline", rline
, 0, 0, nrules
);
241 muscle_insert_symbol_number_table ("r1", r1
, 0, 0, nrules
);
242 muscle_insert_unsigned_int_table ("r2", r2
, 0, 0, nrules
);
243 muscle_insert_int_table ("dprec", dprec
, 0, 0, nrules
);
244 muscle_insert_int_table ("merger", merger
, 0, 0, nrules
);
245 muscle_insert_int_table ("immediate", immediate
, 0, 0, nrules
);
247 MUSCLE_INSERT_INT ("rules_number", nrules
);
248 MUSCLE_INSERT_INT ("max_left_semantic_context", max_left_semantic_context
);
258 /*--------------------------------------------.
259 | Prepare the muscles related to the states. |
260 `--------------------------------------------*/
263 prepare_states (void)
266 symbol_number
*values
= xnmalloc (nstates
, sizeof *values
);
267 for (i
= 0; i
< nstates
; ++i
)
268 values
[i
] = states
[i
]->accessing_symbol
;
269 muscle_insert_symbol_number_table ("stos", values
,
273 MUSCLE_INSERT_INT ("last", high
);
274 MUSCLE_INSERT_INT ("final_state_number", final_state
->number
);
275 MUSCLE_INSERT_INT ("states_number", nstates
);
279 /*-------------------------------------------------------.
280 | Compare two symbols by type-name, and then by number. |
281 `-------------------------------------------------------*/
284 symbol_type_name_cmp (const symbol
**lhs
, const symbol
**rhs
)
286 int res
= uniqstr_cmp ((*lhs
)->type_name
, (*rhs
)->type_name
);
288 res
= (*lhs
)->number
- (*rhs
)->number
;
293 /*----------------------------------------------------------------.
294 | Return a (malloc'ed) table of the symbols sorted by type-name. |
295 `----------------------------------------------------------------*/
298 symbols_by_type_name (void)
300 typedef int (*qcmp_type
) (const void *, const void *);
301 symbol
**res
= xmemdup (symbols
, nsyms
* sizeof *res
);
302 qsort (res
, nsyms
, sizeof *res
, (qcmp_type
) &symbol_type_name_cmp
);
307 /*------------------------------------------------------------------.
308 | Define b4_type_names, which is a list of (lists of the numbers of |
309 | symbols with same type-name). |
310 `------------------------------------------------------------------*/
313 type_names_output (FILE *out
)
316 symbol
**syms
= symbols_by_type_name ();
317 fputs ("m4_define([b4_type_names],\n[", out
);
318 for (i
= 0; i
< nsyms
; /* nothing */)
320 /* The index of the first symbol of the current type-name. */
322 fputs (i
? ",\n[" : "[", out
);
323 for (; i
< nsyms
&& syms
[i
]->type_name
== syms
[i0
]->type_name
; ++i
)
324 fprintf (out
, "%s%d", i
!= i0
? ", " : "", syms
[i
]->number
);
327 fputs ("])\n\n", out
);
332 /*-------------------------------------.
333 | The list of all the symbol numbers. |
334 `-------------------------------------*/
337 symbol_numbers_output (FILE *out
)
340 fputs ("m4_define([b4_symbol_numbers],\n[", out
);
341 for (i
= 0; i
< nsyms
; ++i
)
342 fprintf (out
, "%s[%d]", i
? ", " : "", i
);
343 fputs ("])\n\n", out
);
347 /*---------------------------------.
348 | Output the user actions to OUT. |
349 `---------------------------------*/
352 user_actions_output (FILE *out
)
356 fputs ("m4_define([b4_actions], \n[", out
);
357 for (r
= 0; r
< nrules
; ++r
)
360 fprintf (out
, "b4_%scase(%d, [b4_syncline(%d, ",
361 rules
[r
].is_predicate
? "predicate_" : "",
362 r
+ 1, rules
[r
].action_location
.start
.line
);
363 string_output (out
, rules
[r
].action_location
.start
.file
);
364 fprintf (out
, ")\n[ %s]])\n\n", rules
[r
].action
);
366 fputs ("])\n\n", out
);
369 /*------------------------------------.
370 | Output the merge functions to OUT. |
371 `------------------------------------*/
374 merger_output (FILE *out
)
379 fputs ("m4_define([b4_mergers], \n[[", out
);
380 for (n
= 1, p
= merge_functions
; p
!= NULL
; n
+= 1, p
= p
->next
)
382 if (p
->type
[0] == '\0')
383 fprintf (out
, " case %d: *yy0 = %s (*yy0, *yy1); break;\n",
386 fprintf (out
, " case %d: yy0->%s = %s (*yy0, *yy1); break;\n",
387 n
, p
->type
, p
->name
);
389 fputs ("]])\n\n", out
);
393 /*---------------------------------------------.
394 | Prepare the muscles for symbol definitions. |
395 `---------------------------------------------*/
398 prepare_symbol_definitions (void)
401 for (i
= 0; i
< nsyms
; ++i
)
403 symbol
*sym
= symbols
[i
];
407 #define SET_KEY(Entry) \
408 obstack_printf (&format_obstack, "symbol(%d, %s)", \
410 key = obstack_finish0 (&format_obstack);
412 #define SET_KEY2(Entry, Suffix) \
413 obstack_printf (&format_obstack, "symbol(%d, %s_%s)", \
415 key = obstack_finish0 (&format_obstack);
417 /* Whether the symbol has an identifier. */
418 value
= symbol_id_get (sym
);
420 MUSCLE_INSERT_INT (key
, !!value
);
422 /* Its identifier. */
424 MUSCLE_INSERT_STRING (key
, value
? value
: "");
426 /* Its tag. Typically for documentation purpose. */
428 MUSCLE_INSERT_STRING (key
, sym
->tag
);
430 SET_KEY ("user_number");
431 MUSCLE_INSERT_INT (key
, sym
->user_token_number
);
433 SET_KEY ("is_token");
434 MUSCLE_INSERT_INT (key
,
435 i
< ntokens
&& sym
!= errtoken
&& sym
!= undeftoken
);
438 MUSCLE_INSERT_INT (key
, sym
->number
);
440 SET_KEY ("has_type");
441 MUSCLE_INSERT_INT (key
, !!sym
->type_name
);
444 MUSCLE_INSERT_STRING (key
, sym
->type_name
? sym
->type_name
: "");
448 for (j
= 0; j
< CODE_PROPS_SIZE
; ++j
)
450 /* "printer", not "%printer". */
451 char const *pname
= code_props_type_string (j
) + 1;
452 code_props
const *p
= symbol_code_props_get (sym
, j
);
453 SET_KEY2 ("has", pname
);
454 MUSCLE_INSERT_INT (key
, !!p
->code
);
458 SET_KEY2 (pname
, "file");
459 MUSCLE_INSERT_STRING (key
, p
->location
.start
.file
);
461 SET_KEY2 (pname
, "line");
462 MUSCLE_INSERT_INT (key
, p
->location
.start
.line
);
465 MUSCLE_INSERT_STRING_RAW (key
, p
->code
);
476 prepare_actions (void)
478 /* Figure out the actions for the specified state, indexed by
479 lookahead token type. */
481 muscle_insert_rule_number_table ("defact", yydefact
,
482 yydefact
[0], 1, nstates
);
484 /* Figure out what to do after reducing with each rule, depending on
485 the saved state from before the beginning of parsing the data
486 that matched this rule. */
487 muscle_insert_state_number_table ("defgoto", yydefgoto
,
488 yydefgoto
[0], 1, nsyms
- ntokens
);
492 muscle_insert_base_table ("pact", base
,
493 base
[0], 1, nstates
);
494 MUSCLE_INSERT_INT ("pact_ninf", base_ninf
);
497 muscle_insert_base_table ("pgoto", base
,
498 base
[nstates
], nstates
+ 1, nvectors
);
500 muscle_insert_base_table ("table", table
,
501 table
[0], 1, high
+ 1);
502 MUSCLE_INSERT_INT ("table_ninf", table_ninf
);
504 muscle_insert_base_table ("check", check
,
505 check
[0], 1, high
+ 1);
507 /* GLR parsing slightly modifies YYTABLE and YYCHECK (and thus
508 YYPACT) so that in states with unresolved conflicts, the default
509 reduction is not used in the conflicted entries, so that there is
510 a place to put a conflict pointer.
512 This means that YYCONFLP and YYCONFL are nonsense for a non-GLR
513 parser, so we could avoid accidents by not writing them out in
514 that case. Nevertheless, it seems even better to be able to use
515 the GLR skeletons even without the non-deterministic tables. */
516 muscle_insert_unsigned_int_table ("conflict_list_heads", conflict_table
,
517 conflict_table
[0], 1, high
+ 1);
518 muscle_insert_unsigned_int_table ("conflicting_rules", conflict_list
,
519 0, 1, conflict_list_cnt
);
523 /*--------------------------------------------.
524 | Output the definitions of all the muscles. |
525 `--------------------------------------------*/
528 muscles_output (FILE *out
)
530 fputs ("m4_init()\n", out
);
532 symbol_numbers_output (out
);
533 type_names_output (out
);
534 user_actions_output (out
);
536 muscles_m4_output (out
);
539 /*---------------------------.
540 | Call the skeleton parser. |
541 `---------------------------*/
544 output_skeleton (void)
549 /* Compute the names of the package data dir and skeleton files. */
550 char const *m4
= (m4
= getenv ("M4")) ? m4
: M4
;
551 char const *datadir
= pkgdatadir ();
552 char *m4sugar
= xconcatenated_filename (datadir
, "m4sugar/m4sugar.m4", NULL
);
553 char *m4bison
= xconcatenated_filename (datadir
, "bison.m4", NULL
);
554 char *skel
= (IS_PATH_WITH_DIR (skeleton
)
556 : xconcatenated_filename (datadir
, skeleton
, NULL
));
558 /* Test whether m4sugar.m4 is readable, to check for proper
559 installation. A faulty installation can cause deadlock, so a
560 cheap sanity check is worthwhile. */
561 xfclose (xfopen (m4sugar
, "r"));
563 /* Create an m4 subprocess connected to us via two pipes. */
565 if (trace_flag
& trace_tools
)
566 fprintf (stderr
, "running: %s %s - %s %s\n",
567 m4
, m4sugar
, m4bison
, skel
);
569 /* Some future version of GNU M4 (most likely 1.6) may treat the -dV in a
570 position-dependent manner. Keep it as the first argument so that all
573 See the thread starting at
574 <http://lists.gnu.org/archive/html/bug-bison/2008-07/msg00000.html>
577 char const *argv
[10];
581 /* When POSIXLY_CORRECT is set, GNU M4 1.6 and later disable GNU
582 extensions, which Bison's skeletons depend on. With older M4,
583 it has no effect. M4 1.4.12 added a -g/--gnu command-line
584 option to make it explicit that a program wants GNU M4
585 extensions even when POSIXLY_CORRECT is set.
587 See the thread starting at
588 <http://lists.gnu.org/archive/html/bug-bison/2008-07/msg00000.html>
591 argv
[i
++] = M4_GNU_OPTION
;
595 if (trace_flag
& trace_m4
)
602 aver (i
<= ARRAY_CARDINALITY (argv
));
604 /* The ugly cast is because gnulib gets the const-ness wrong. */
605 pid
= create_pipe_bidi ("m4", m4
, (char **)(void*)argv
, false, true,
613 if (trace_flag
& trace_muscles
)
614 muscles_output (stderr
);
616 FILE *out
= xfdopen (filter_fd
[1], "w");
617 muscles_output (out
);
621 /* Read and process m4's output. */
622 timevar_push (TV_M4
);
624 FILE *in
= xfdopen (filter_fd
[0], "r");
626 /* scan_skel should have read all of M4's output. Otherwise, when we
627 close the pipe, we risk letting M4 report a broken-pipe to the
632 wait_subprocess (pid
, "m4", false, false, true, true, NULL
);
639 /* BISON_USE_PUSH_FOR_PULL is for the test suite and should not be
640 documented for the user. */
641 char const *cp
= getenv ("BISON_USE_PUSH_FOR_PULL");
642 bool use_push_for_pull_flag
= cp
&& *cp
&& strtol (cp
, 0, 10);
645 MUSCLE_INSERT_BOOL ("defines_flag", defines_flag
);
646 MUSCLE_INSERT_BOOL ("glr_flag", glr_parser
);
647 MUSCLE_INSERT_BOOL ("nondeterministic_flag", nondeterministic_parser
);
648 MUSCLE_INSERT_BOOL ("synclines_flag", !no_lines_flag
);
649 MUSCLE_INSERT_BOOL ("tag_seen_flag", tag_seen
);
650 MUSCLE_INSERT_BOOL ("token_table_flag", token_table_flag
);
651 MUSCLE_INSERT_BOOL ("use_push_for_pull_flag", use_push_for_pull_flag
);
652 MUSCLE_INSERT_BOOL ("yacc_flag", yacc_flag
);
655 if (spec_name_prefix
)
656 MUSCLE_INSERT_STRING ("prefix", spec_name_prefix
);
658 MUSCLE_INSERT_STRING ("file_name_all_but_ext", all_but_ext
);
660 #define DEFINE(Name) MUSCLE_INSERT_STRING (#Name, Name ? Name : "")
662 DEFINE (parser_file_name
);
663 DEFINE (spec_defines_file
);
664 DEFINE (spec_file_prefix
);
665 DEFINE (spec_graph_file
);
666 DEFINE (spec_name_prefix
);
667 DEFINE (spec_outfile
);
668 DEFINE (spec_verbose_file
);
671 /* Find the right skeleton file, and add muscles about the skeletons. */
673 MUSCLE_INSERT_C_STRING ("skeleton", skeleton
);
675 skeleton
= language
->skeleton
;
677 /* About the skeletons. */
679 /* b4_pkgdatadir is used inside m4_include in the skeletons, so digraphs
680 would never be expanded. Hopefully no one has M4-special characters in
681 his Bison installation path. */
682 MUSCLE_INSERT_STRING_RAW ("pkgdatadir", pkgdatadir ());
687 /*----------------------------------------------------------.
688 | Output the parsing tables and the parser code to ftable. |
689 `----------------------------------------------------------*/
694 obstack_init (&format_obstack
);
700 prepare_symbol_definitions ();
704 /* Process the selected skeleton file. */
707 /* If late errors were generated, destroy the generated source
709 if (complaint_status
)
710 unlink_generated_sources ();
712 obstack_free (&format_obstack
, NULL
);
718 char const *cp
= getenv ("BISON_PKGDATADIR");
719 return cp
? cp
: PKGDATADIR
;