1 /* Output the generated parsing program for Bison.
3 Copyright (C) 1984, 1986, 1989, 1992, 2000-2012 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>
28 #include <get-errno.h>
30 #include <spawn-pipe.h>
32 #include <wait-process.h>
38 #include "muscle-tab.h"
41 #include "scan-code.h" /* max_left_semantic_context */
42 #include "scan-skel.h"
46 # define ARRAY_CARDINALITY(Array) (sizeof (Array) / sizeof *(Array))
48 static struct obstack format_obstack
;
51 /*-------------------------------------------------------------------.
52 | Create a function NAME which associates to the muscle NAME the |
53 | result of formatting the FIRST and then TABLE_DATA[BEGIN..END[ (of |
54 | TYPE), and to the muscle NAME_max, the max value of the |
56 `-------------------------------------------------------------------*/
59 #define GENERATE_MUSCLE_INSERT_TABLE(Name, Type) \
62 Name (char const *name, \
75 obstack_fgrow1 (&format_obstack, "%6d", first); \
76 for (i = begin; i < end; ++i) \
78 obstack_1grow (&format_obstack, ','); \
81 obstack_sgrow (&format_obstack, "\n "); \
86 obstack_fgrow1 (&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]; \
92 obstack_1grow (&format_obstack, 0); \
93 muscle_insert (name, obstack_finish (&format_obstack)); \
97 /* Build `NAME_min' and `NAME_max' in the obstack. */ \
98 obstack_fgrow1 (&format_obstack, "%s_min", name); \
99 obstack_1grow (&format_obstack, 0); \
100 MUSCLE_INSERT_LONG_INT (obstack_finish (&format_obstack), lmin); \
101 obstack_fgrow1 (&format_obstack, "%s_max", name); \
102 obstack_1grow (&format_obstack, 0); \
103 MUSCLE_INSERT_LONG_INT (obstack_finish (&format_obstack), lmax); \
106 GENERATE_MUSCLE_INSERT_TABLE(muscle_insert_unsigned_int_table
, unsigned int)
107 GENERATE_MUSCLE_INSERT_TABLE(muscle_insert_int_table
, int)
108 GENERATE_MUSCLE_INSERT_TABLE(muscle_insert_base_table
, base_number
)
109 GENERATE_MUSCLE_INSERT_TABLE(muscle_insert_rule_number_table
, rule_number
)
110 GENERATE_MUSCLE_INSERT_TABLE(muscle_insert_symbol_number_table
, symbol_number
)
111 GENERATE_MUSCLE_INSERT_TABLE(muscle_insert_state_number_table
, state_number
)
114 /*--------------------------------------------------------------------.
115 | Print to OUT a representation of STRING escaped both for C and M4. |
116 `--------------------------------------------------------------------*/
119 escaped_output (FILE *out
, char const *string
)
124 for (p
= quotearg_style (c_quoting_style
, string
); *p
; p
++)
127 case '$': fputs ("$][", out
); break;
128 case '@': fputs ("@@", out
); break;
129 case '[': fputs ("@{", out
); break;
130 case ']': fputs ("@}", out
); break;
131 default: fputc (*p
, out
); break;
138 /*------------------------------------------------------------------.
139 | Prepare the muscles related to the symbols: translate, tname, and |
141 `------------------------------------------------------------------*/
144 prepare_symbols (void)
146 MUSCLE_INSERT_BOOL ("token_table", token_table_flag
);
147 MUSCLE_INSERT_INT ("tokens_number", ntokens
);
148 MUSCLE_INSERT_INT ("nterms_number", nvars
);
149 MUSCLE_INSERT_INT ("symbols_number", nsyms
);
150 MUSCLE_INSERT_INT ("undef_token_number", undeftoken
->number
);
151 MUSCLE_INSERT_INT ("user_token_number_max", max_user_token_number
);
153 muscle_insert_symbol_number_table ("translate",
155 token_translations
[0],
156 1, max_user_token_number
+ 1);
158 /* tname -- token names. */
161 /* We assume that the table will be output starting at column 2. */
163 struct quoting_options
*qo
= clone_quoting_options (0);
164 set_quoting_style (qo
, c_quoting_style
);
165 set_quoting_flags (qo
, QA_SPLIT_TRIGRAPHS
);
166 for (i
= 0; i
< nsyms
; i
++)
168 char *cp
= quotearg_alloc (symbols
[i
]->tag
, -1, qo
);
169 /* Width of the next token, including the two quotes, the
170 comma and the space. */
171 int width
= strlen (cp
) + 2;
175 obstack_sgrow (&format_obstack
, "\n ");
180 obstack_1grow (&format_obstack
, ' ');
181 MUSCLE_OBSTACK_SGROW (&format_obstack
, cp
);
183 obstack_1grow (&format_obstack
, ',');
187 obstack_sgrow (&format_obstack
, " ]b4_null[");
189 /* Finish table and store. */
190 obstack_1grow (&format_obstack
, 0);
191 muscle_insert ("tname", obstack_finish (&format_obstack
));
194 /* Output YYTOKNUM. */
197 int *values
= xnmalloc (ntokens
, sizeof *values
);
198 for (i
= 0; i
< ntokens
; ++i
)
199 values
[i
] = symbols
[i
]->user_token_number
;
200 muscle_insert_int_table ("toknum", values
,
201 values
[0], 1, ntokens
);
207 /*----------------------------------------------------------------.
208 | Prepare the muscles related to the rules: r1, r2, rline, dprec, |
209 | merger, immediate. |
210 `----------------------------------------------------------------*/
215 unsigned int *rline
= xnmalloc (nrules
, sizeof *rline
);
216 symbol_number
*r1
= xnmalloc (nrules
, sizeof *r1
);
217 unsigned int *r2
= xnmalloc (nrules
, sizeof *r2
);
218 int *dprec
= xnmalloc (nrules
, sizeof *dprec
);
219 int *merger
= xnmalloc (nrules
, sizeof *merger
);
220 int *immediate
= xnmalloc (nrules
, sizeof *immediate
);
223 for (r
= 0; r
< nrules
; ++r
)
225 /* LHS of the rule R. */
226 r1
[r
] = rules
[r
].lhs
->number
;
227 /* Length of rule R's RHS. */
228 r2
[r
] = rule_rhs_length(&rules
[r
]);
229 /* Line where rule was defined. */
230 rline
[r
] = rules
[r
].location
.start
.line
;
231 /* Dynamic precedence (GLR). */
232 dprec
[r
] = rules
[r
].dprec
;
233 /* Merger-function index (GLR). */
234 merger
[r
] = rules
[r
].merger
;
235 /* Immediate reduction flags (GLR). */
236 immediate
[r
] = rules
[r
].is_predicate
;
239 muscle_insert_unsigned_int_table ("rline", rline
, 0, 0, nrules
);
240 muscle_insert_symbol_number_table ("r1", r1
, 0, 0, nrules
);
241 muscle_insert_unsigned_int_table ("r2", r2
, 0, 0, nrules
);
242 muscle_insert_int_table ("dprec", dprec
, 0, 0, nrules
);
243 muscle_insert_int_table ("merger", merger
, 0, 0, nrules
);
244 muscle_insert_int_table ("immediate", immediate
, 0, 0, nrules
);
246 MUSCLE_INSERT_INT ("rules_number", nrules
);
247 MUSCLE_INSERT_INT ("max_left_semantic_context", max_left_semantic_context
);
257 /*--------------------------------------------.
258 | Prepare the muscles related to the states. |
259 `--------------------------------------------*/
262 prepare_states (void)
265 symbol_number
*values
= xnmalloc (nstates
, sizeof *values
);
266 for (i
= 0; i
< nstates
; ++i
)
267 values
[i
] = states
[i
]->accessing_symbol
;
268 muscle_insert_symbol_number_table ("stos", values
,
272 MUSCLE_INSERT_INT ("last", high
);
273 MUSCLE_INSERT_INT ("final_state_number", final_state
->number
);
274 MUSCLE_INSERT_INT ("states_number", nstates
);
278 /*-------------------------------------------------------.
279 | Compare two symbols by type-name, and then by number. |
280 `-------------------------------------------------------*/
283 symbol_type_name_cmp (const symbol
**lhs
, const symbol
**rhs
)
285 int res
= UNIQSTR_CMP((*lhs
)->type_name
, (*rhs
)->type_name
);
288 return (*lhs
)->number
- (*rhs
)->number
;
292 /*----------------------------------------------------------------.
293 | Return a (malloc'ed) table of the symbols sorted by type-name. |
294 `----------------------------------------------------------------*/
297 symbols_by_type_name (void)
299 typedef int (*qcmp_type
) (const void *, const void *);
300 symbol
**res
= xmemdup (symbols
, nsyms
* sizeof *res
);
301 qsort (res
, nsyms
, sizeof *res
, (qcmp_type
) &symbol_type_name_cmp
);
306 /*------------------------------------------------------------------.
307 | Define b4_type_names, which is a list of (lists of the numbers of |
308 | symbols with same type-name). |
309 `------------------------------------------------------------------*/
312 type_names_output (FILE *out
)
315 symbol
**syms
= symbols_by_type_name ();
316 fputs ("m4_define([b4_type_names],\n[", out
);
317 for (i
= 0; i
< nsyms
; /* nothing */)
319 // The index of the first symbol of the current type-name.
321 fputs (i
? ",\n[" : "[", out
);
322 for (; i
< nsyms
&& syms
[i
]->type_name
== syms
[i0
]->type_name
; ++i
)
323 fprintf (out
, "%s%d", i
!= i0
? ", " : "", syms
[i
]->number
);
326 fputs ("])\n\n", out
);
331 /*-------------------------------------.
332 | The list of all the symbol numbers. |
333 `-------------------------------------*/
336 symbol_numbers_output (FILE *out
)
339 fputs ("m4_define([b4_symbol_numbers],\n[", out
);
340 for (i
= 0; i
< nsyms
; ++i
)
341 fprintf (out
, "%s[%d]", i
? ", " : "", i
);
342 fputs ("])\n\n", out
);
346 /*---------------------------------.
347 | Output the user actions to OUT. |
348 `---------------------------------*/
351 user_actions_output (FILE *out
)
355 fputs ("m4_define([b4_actions], \n[", out
);
356 for (r
= 0; r
< nrules
; ++r
)
359 fprintf (out
, "b4_%scase(%d, [b4_syncline(%d, ",
360 rules
[r
].is_predicate
? "predicate_" : "",
361 r
+ 1, rules
[r
].action_location
.start
.line
);
362 escaped_output (out
, rules
[r
].action_location
.start
.file
);
363 fprintf (out
, ")\n[ %s]])\n\n", rules
[r
].action
);
365 fputs ("])\n\n", out
);
368 /*------------------------------------.
369 | Output the merge functions to OUT. |
370 `------------------------------------*/
373 merger_output (FILE *out
)
378 fputs ("m4_define([b4_mergers], \n[[", out
);
379 for (n
= 1, p
= merge_functions
; p
!= NULL
; n
+= 1, p
= p
->next
)
381 if (p
->type
[0] == '\0')
382 fprintf (out
, " case %d: *yy0 = %s (*yy0, *yy1); break;\n",
385 fprintf (out
, " case %d: yy0->%s = %s (*yy0, *yy1); break;\n",
386 n
, p
->type
, p
->name
);
388 fputs ("]])\n\n", out
);
392 /*---------------------------------------------.
393 | Prepare the muscles for symbol definitions. |
394 `---------------------------------------------*/
397 prepare_symbol_definitions (void)
400 for (i
= 0; i
< nsyms
; ++i
)
402 symbol
*sym
= symbols
[i
];
406 #define SET_KEY(Entry) \
407 obstack_fgrow2 (&format_obstack, "symbol(%d, %s)", i, Entry); \
408 obstack_1grow (&format_obstack, 0); \
409 key = obstack_finish (&format_obstack);
411 // Whether the symbol has an identifier.
412 value
= symbol_id_get (sym
);
414 MUSCLE_INSERT_INT (key
, !!value
);
418 MUSCLE_INSERT_STRING (key
, value
? value
: "");
420 // Its tag. Typically for documentation purpose.
422 MUSCLE_INSERT_STRING (key
, sym
->tag
);
424 SET_KEY("user_number");
425 MUSCLE_INSERT_INT (key
, sym
->user_token_number
);
428 MUSCLE_INSERT_INT (key
,
429 i
< ntokens
&& sym
!= errtoken
&& sym
!= undeftoken
);
432 MUSCLE_INSERT_INT (key
, sym
->number
);
435 MUSCLE_INSERT_INT (key
, !!sym
->type_name
);
438 MUSCLE_INSERT_STRING (key
, sym
->type_name
? sym
->type_name
: "");
440 #define CODE_PROP(PropName) \
442 code_props const *p = symbol_ ## PropName ## _get (sym); \
443 SET_KEY("has_" #PropName); \
444 MUSCLE_INSERT_INT (key, !!p->code); \
448 SET_KEY(#PropName "_file"); \
449 MUSCLE_INSERT_STRING (key, p->location.start.file); \
451 SET_KEY(#PropName "_line"); \
452 MUSCLE_INSERT_INT (key, p->location.start.line); \
454 SET_KEY(#PropName); \
455 MUSCLE_INSERT_STRING_RAW (key, p->code); \
459 CODE_PROP(destructor
);
467 /*--------------------------------------.
468 | Output the tokens definition to OUT. |
469 `--------------------------------------*/
472 token_definitions_output (FILE *out
)
475 char const *sep
= "";
477 fputs ("m4_define([b4_tokens], \n[", out
);
478 for (i
= 0; i
< ntokens
; ++i
)
480 symbol
*sym
= symbols
[i
];
481 int number
= sym
->user_token_number
;
482 uniqstr id
= symbol_id_get (sym
);
484 /* At this stage, if there are literal string aliases, they are
485 part of SYMBOLS, so we should not find their aliased symbols
487 aver (number
!= USER_NUMBER_HAS_STRING_ALIAS
);
489 /* Skip error token and tokens without identifier. */
490 if (sym
!= errtoken
&& id
)
492 fprintf (out
, "%s[[[%s]], %d]",
497 fputs ("])\n\n", out
);
502 prepare_actions (void)
504 /* Figure out the actions for the specified state, indexed by
505 lookahead token type. */
507 muscle_insert_rule_number_table ("defact", yydefact
,
508 yydefact
[0], 1, nstates
);
510 /* Figure out what to do after reducing with each rule, depending on
511 the saved state from before the beginning of parsing the data
512 that matched this rule. */
513 muscle_insert_state_number_table ("defgoto", yydefgoto
,
514 yydefgoto
[0], 1, nsyms
- ntokens
);
518 muscle_insert_base_table ("pact", base
,
519 base
[0], 1, nstates
);
520 MUSCLE_INSERT_INT ("pact_ninf", base_ninf
);
523 muscle_insert_base_table ("pgoto", base
,
524 base
[nstates
], nstates
+ 1, nvectors
);
526 muscle_insert_base_table ("table", table
,
527 table
[0], 1, high
+ 1);
528 MUSCLE_INSERT_INT ("table_ninf", table_ninf
);
530 muscle_insert_base_table ("check", check
,
531 check
[0], 1, high
+ 1);
533 /* GLR parsing slightly modifies YYTABLE and YYCHECK (and thus
534 YYPACT) so that in states with unresolved conflicts, the default
535 reduction is not used in the conflicted entries, so that there is
536 a place to put a conflict pointer.
538 This means that YYCONFLP and YYCONFL are nonsense for a non-GLR
539 parser, so we could avoid accidents by not writing them out in
540 that case. Nevertheless, it seems even better to be able to use
541 the GLR skeletons even without the non-deterministic tables. */
542 muscle_insert_unsigned_int_table ("conflict_list_heads", conflict_table
,
543 conflict_table
[0], 1, high
+ 1);
544 muscle_insert_unsigned_int_table ("conflicting_rules", conflict_list
,
545 0, 1, conflict_list_cnt
);
549 /*--------------------------------------------.
550 | Output the definitions of all the muscles. |
551 `--------------------------------------------*/
554 muscles_output (FILE *out
)
556 fputs ("m4_init()\n", out
);
558 symbol_numbers_output (out
);
559 token_definitions_output (out
);
560 type_names_output (out
);
561 user_actions_output (out
);
563 muscles_m4_output (out
);
566 /*---------------------------.
567 | Call the skeleton parser. |
568 `---------------------------*/
571 output_skeleton (void)
574 char const *argv
[10];
577 /* Compute the names of the package data dir and skeleton files. */
578 char const *m4
= (m4
= getenv ("M4")) ? m4
: M4
;
579 char const *datadir
= pkgdatadir ();
580 char *m4sugar
= xconcatenated_filename (datadir
, "m4sugar/m4sugar.m4", NULL
);
581 char *m4bison
= xconcatenated_filename (datadir
, "bison.m4", NULL
);
582 char *skel
= (IS_PATH_WITH_DIR (skeleton
)
584 : xconcatenated_filename (datadir
, skeleton
, NULL
));
586 /* Test whether m4sugar.m4 is readable, to check for proper
587 installation. A faulty installation can cause deadlock, so a
588 cheap sanity check is worthwhile. */
589 xfclose (xfopen (m4sugar
, "r"));
591 /* Create an m4 subprocess connected to us via two pipes. */
593 if (trace_flag
& trace_tools
)
594 fprintf (stderr
, "running: %s %s - %s %s\n",
595 m4
, m4sugar
, m4bison
, skel
);
597 /* Some future version of GNU M4 (most likely 1.6) may treat the -dV in a
598 position-dependent manner. Keep it as the first argument so that all
601 See the thread starting at
602 <http://lists.gnu.org/archive/html/bug-bison/2008-07/msg00000.html>
608 /* When POSIXLY_CORRECT is set, GNU M4 1.6 and later disable GNU
609 extensions, which Bison's skeletons depend on. With older M4,
610 it has no effect. M4 1.4.12 added a -g/--gnu command-line
611 option to make it explicit that a program wants GNU M4
612 extensions even when POSIXLY_CORRECT is set.
614 See the thread starting at
615 <http://lists.gnu.org/archive/html/bug-bison/2008-07/msg00000.html>
618 argv
[i
++] = M4_GNU_OPTION
;
622 if (trace_flag
& trace_m4
)
629 aver (i
<= ARRAY_CARDINALITY (argv
));
632 /* The ugly cast is because gnulib gets the const-ness wrong. */
633 pid
= create_pipe_bidi ("m4", m4
, (char **)(void*)argv
, false, true,
639 if (trace_flag
& trace_muscles
)
640 muscles_output (stderr
);
642 FILE *out
= fdopen (filter_fd
[1], "w");
644 error (EXIT_FAILURE
, get_errno (),
646 muscles_output (out
);
650 /* Read and process m4's output. */
651 timevar_push (TV_M4
);
653 FILE *in
= fdopen (filter_fd
[0], "r");
655 error (EXIT_FAILURE
, get_errno (),
658 /* scan_skel should have read all of M4's output. Otherwise, when we
659 close the pipe, we risk letting M4 report a broken-pipe to the
664 wait_subprocess (pid
, "m4", false, false, true, true, NULL
);
671 /* BISON_USE_PUSH_FOR_PULL is for the test suite and should not be documented
673 char const *cp
= getenv ("BISON_USE_PUSH_FOR_PULL");
674 bool use_push_for_pull_flag
= cp
&& *cp
&& STRNEQ (cp
, "0");
677 MUSCLE_INSERT_BOOL ("defines_flag", defines_flag
);
678 MUSCLE_INSERT_BOOL ("glr_flag", glr_parser
);
679 MUSCLE_INSERT_BOOL ("nondeterministic_flag", nondeterministic_parser
);
680 MUSCLE_INSERT_BOOL ("synclines_flag", !no_lines_flag
);
681 MUSCLE_INSERT_BOOL ("tag_seen_flag", tag_seen
);
682 MUSCLE_INSERT_BOOL ("use_push_for_pull_flag", use_push_for_pull_flag
);
683 MUSCLE_INSERT_BOOL ("yacc_flag", yacc_flag
);
686 if (spec_name_prefix
)
687 MUSCLE_INSERT_STRING ("prefix", spec_name_prefix
);
689 MUSCLE_INSERT_STRING ("file_name_all_but_ext", all_but_ext
);
691 #define DEFINE(Name) MUSCLE_INSERT_STRING (#Name, Name ? Name : "")
693 DEFINE (parser_file_name
);
694 DEFINE (spec_defines_file
);
695 DEFINE (spec_file_prefix
);
696 DEFINE (spec_graph_file
);
697 DEFINE (spec_name_prefix
);
698 DEFINE (spec_outfile
);
699 DEFINE (spec_verbose_file
);
702 /* Find the right skeleton file, and add muscles about the skeletons. */
704 MUSCLE_INSERT_C_STRING ("skeleton", skeleton
);
706 skeleton
= language
->skeleton
;
708 /* About the skeletons. */
710 /* b4_pkgdatadir is used inside m4_include in the skeletons, so digraphs
711 would never be expanded. Hopefully no one has M4-special characters in
712 his Bison installation path. */
713 MUSCLE_INSERT_STRING_RAW ("pkgdatadir", pkgdatadir ());
718 /*----------------------------------------------------------.
719 | Output the parsing tables and the parser code to ftable. |
720 `----------------------------------------------------------*/
725 obstack_init (&format_obstack
);
731 prepare_symbol_definitions ();
735 /* Process the selected skeleton file. */
738 obstack_free (&format_obstack
, NULL
);
744 char const *cp
= getenv ("BISON_PKGDATADIR");
745 return cp
? cp
: PKGDATADIR
;