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>
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 # define ARRAY_CARDINALITY(Array) (sizeof (Array) / sizeof *(Array))
47 static struct obstack format_obstack
;
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 |
55 `-------------------------------------------------------------------*/
58 #define GENERATE_MUSCLE_INSERT_TABLE(Name, Type) \
61 Name (char const *name, \
74 obstack_fgrow1 (&format_obstack, "%6d", first); \
75 for (i = begin; i < end; ++i) \
77 obstack_1grow (&format_obstack, ','); \
80 obstack_sgrow (&format_obstack, "\n "); \
85 obstack_fgrow1 (&format_obstack, "%6d", table_data[i]); \
86 if (table_data[i] < min) \
87 min = table_data[i]; \
88 if (max < table_data[i]) \
89 max = table_data[i]; \
91 obstack_1grow (&format_obstack, 0); \
92 muscle_insert (name, obstack_finish (&format_obstack)); \
96 /* Build `NAME_min' and `NAME_max' in the obstack. */ \
97 obstack_fgrow1 (&format_obstack, "%s_min", name); \
98 obstack_1grow (&format_obstack, 0); \
99 MUSCLE_INSERT_LONG_INT (obstack_finish (&format_obstack), lmin); \
100 obstack_fgrow1 (&format_obstack, "%s_max", name); \
101 obstack_1grow (&format_obstack, 0); \
102 MUSCLE_INSERT_LONG_INT (obstack_finish (&format_obstack), lmax); \
105 GENERATE_MUSCLE_INSERT_TABLE(muscle_insert_unsigned_int_table
, unsigned int)
106 GENERATE_MUSCLE_INSERT_TABLE(muscle_insert_int_table
, int)
107 GENERATE_MUSCLE_INSERT_TABLE(muscle_insert_base_table
, base_number
)
108 GENERATE_MUSCLE_INSERT_TABLE(muscle_insert_rule_number_table
, rule_number
)
109 GENERATE_MUSCLE_INSERT_TABLE(muscle_insert_symbol_number_table
, symbol_number
)
110 GENERATE_MUSCLE_INSERT_TABLE(muscle_insert_state_number_table
, state_number
)
113 /*--------------------------------------------------------------------.
114 | Print to OUT a representation of STRING escaped both for C and M4. |
115 `--------------------------------------------------------------------*/
118 escaped_output (FILE *out
, char const *string
)
123 for (p
= quotearg_style (c_quoting_style
, string
); *p
; p
++)
126 case '$': fputs ("$][", out
); break;
127 case '@': fputs ("@@", out
); break;
128 case '[': fputs ("@{", out
); break;
129 case ']': fputs ("@}", out
); break;
130 default: fputc (*p
, out
); break;
137 /*------------------------------------------------------------------.
138 | Prepare the muscles related to the symbols: translate, tname, and |
140 `------------------------------------------------------------------*/
143 prepare_symbols (void)
145 MUSCLE_INSERT_BOOL ("token_table", token_table_flag
);
146 MUSCLE_INSERT_INT ("tokens_number", ntokens
);
147 MUSCLE_INSERT_INT ("nterms_number", nvars
);
148 MUSCLE_INSERT_INT ("symbols_number", nsyms
);
149 MUSCLE_INSERT_INT ("undef_token_number", undeftoken
->number
);
150 MUSCLE_INSERT_INT ("user_token_number_max", max_user_token_number
);
152 muscle_insert_symbol_number_table ("translate",
154 token_translations
[0],
155 1, max_user_token_number
+ 1);
157 /* tname -- token names. */
160 /* We assume that the table will be output starting at column 2. */
162 struct quoting_options
*qo
= clone_quoting_options (0);
163 set_quoting_style (qo
, c_quoting_style
);
164 set_quoting_flags (qo
, QA_SPLIT_TRIGRAPHS
);
165 for (i
= 0; i
< nsyms
; i
++)
167 char *cp
= quotearg_alloc (symbols
[i
]->tag
, -1, qo
);
168 /* Width of the next token, including the two quotes, the
169 comma and the space. */
170 int width
= strlen (cp
) + 2;
174 obstack_sgrow (&format_obstack
, "\n ");
179 obstack_1grow (&format_obstack
, ' ');
180 MUSCLE_OBSTACK_SGROW (&format_obstack
, cp
);
182 obstack_1grow (&format_obstack
, ',');
186 obstack_sgrow (&format_obstack
, " ]b4_null[");
188 /* Finish table and store. */
189 obstack_1grow (&format_obstack
, 0);
190 muscle_insert ("tname", obstack_finish (&format_obstack
));
193 /* Output YYTOKNUM. */
196 int *values
= xnmalloc (ntokens
, sizeof *values
);
197 for (i
= 0; i
< ntokens
; ++i
)
198 values
[i
] = symbols
[i
]->user_token_number
;
199 muscle_insert_int_table ("toknum", values
,
200 values
[0], 1, ntokens
);
206 /*----------------------------------------------------------------.
207 | Prepare the muscles related to the rules: r1, r2, rline, dprec, |
208 | merger, immediate. |
209 `----------------------------------------------------------------*/
214 unsigned int *rline
= xnmalloc (nrules
, sizeof *rline
);
215 symbol_number
*r1
= xnmalloc (nrules
, sizeof *r1
);
216 unsigned int *r2
= xnmalloc (nrules
, sizeof *r2
);
217 int *dprec
= xnmalloc (nrules
, sizeof *dprec
);
218 int *merger
= xnmalloc (nrules
, sizeof *merger
);
219 int *immediate
= xnmalloc (nrules
, sizeof *immediate
);
222 for (r
= 0; r
< nrules
; ++r
)
224 /* LHS of the rule R. */
225 r1
[r
] = rules
[r
].lhs
->number
;
226 /* Length of rule R's RHS. */
227 r2
[r
] = rule_rhs_length(&rules
[r
]);
228 /* Line where rule was defined. */
229 rline
[r
] = rules
[r
].location
.start
.line
;
230 /* Dynamic precedence (GLR). */
231 dprec
[r
] = rules
[r
].dprec
;
232 /* Merger-function index (GLR). */
233 merger
[r
] = rules
[r
].merger
;
234 /* Immediate reduction flags (GLR). */
235 immediate
[r
] = rules
[r
].is_predicate
;
238 muscle_insert_unsigned_int_table ("rline", rline
, 0, 0, nrules
);
239 muscle_insert_symbol_number_table ("r1", r1
, 0, 0, nrules
);
240 muscle_insert_unsigned_int_table ("r2", r2
, 0, 0, nrules
);
241 muscle_insert_int_table ("dprec", dprec
, 0, 0, nrules
);
242 muscle_insert_int_table ("merger", merger
, 0, 0, nrules
);
243 muscle_insert_int_table ("immediate", immediate
, 0, 0, nrules
);
245 MUSCLE_INSERT_INT ("rules_number", nrules
);
246 MUSCLE_INSERT_INT ("max_left_semantic_context", max_left_semantic_context
);
256 /*--------------------------------------------.
257 | Prepare the muscles related to the states. |
258 `--------------------------------------------*/
261 prepare_states (void)
264 symbol_number
*values
= xnmalloc (nstates
, sizeof *values
);
265 for (i
= 0; i
< nstates
; ++i
)
266 values
[i
] = states
[i
]->accessing_symbol
;
267 muscle_insert_symbol_number_table ("stos", values
,
271 MUSCLE_INSERT_INT ("last", high
);
272 MUSCLE_INSERT_INT ("final_state_number", final_state
->number
);
273 MUSCLE_INSERT_INT ("states_number", nstates
);
277 /*-------------------------------------------------------.
278 | Compare two symbols by type-name, and then by number. |
279 `-------------------------------------------------------*/
282 symbol_type_name_cmp (const symbol
**lhs
, const symbol
**rhs
)
284 int res
= UNIQSTR_CMP((*lhs
)->type_name
, (*rhs
)->type_name
);
287 return (*lhs
)->number
- (*rhs
)->number
;
291 /*----------------------------------------------------------------.
292 | Return a (malloc'ed) table of the symbols sorted by type-name. |
293 `----------------------------------------------------------------*/
296 symbols_by_type_name (void)
298 typedef int (*qcmp_type
) (const void *, const void *);
299 symbol
**res
= xmemdup (symbols
, nsyms
* sizeof *res
);
300 qsort (res
, nsyms
, sizeof *res
, (qcmp_type
) &symbol_type_name_cmp
);
305 /*------------------------------------------------------------------.
306 | Define b4_type_names, which is a list of (lists of the numbers of |
307 | symbols with same type-name). |
308 `------------------------------------------------------------------*/
311 type_names_output (FILE *out
)
314 symbol
**syms
= symbols_by_type_name ();
315 fputs ("m4_define([b4_type_names],\n[", out
);
316 for (i
= 0; i
< nsyms
; /* nothing */)
318 // The index of the first symbol of the current type-name.
320 fputs (i
? ",\n[" : "[", out
);
321 for (; i
< nsyms
&& syms
[i
]->type_name
== syms
[i0
]->type_name
; ++i
)
322 fprintf (out
, "%s%d", i
!= i0
? ", " : "", syms
[i
]->number
);
325 fputs ("])\n\n", out
);
330 /*-------------------------------------.
331 | The list of all the symbol numbers. |
332 `-------------------------------------*/
335 symbol_numbers_output (FILE *out
)
338 fputs ("m4_define([b4_symbol_numbers],\n[", out
);
339 for (i
= 0; i
< nsyms
; ++i
)
340 fprintf (out
, "%s[%d]", i
? ", " : "", i
);
341 fputs ("])\n\n", out
);
345 /*---------------------------------.
346 | Output the user actions to OUT. |
347 `---------------------------------*/
350 user_actions_output (FILE *out
)
354 fputs ("m4_define([b4_actions], \n[", out
);
355 for (r
= 0; r
< nrules
; ++r
)
358 fprintf (out
, "b4_%scase(%d, [b4_syncline(%d, ",
359 rules
[r
].is_predicate
? "predicate_" : "",
360 r
+ 1, rules
[r
].action_location
.start
.line
);
361 escaped_output (out
, rules
[r
].action_location
.start
.file
);
362 fprintf (out
, ")\n[ %s]])\n\n", rules
[r
].action
);
364 fputs ("])\n\n", out
);
367 /*------------------------------------.
368 | Output the merge functions to OUT. |
369 `------------------------------------*/
372 merger_output (FILE *out
)
377 fputs ("m4_define([b4_mergers], \n[[", out
);
378 for (n
= 1, p
= merge_functions
; p
!= NULL
; n
+= 1, p
= p
->next
)
380 if (p
->type
[0] == '\0')
381 fprintf (out
, " case %d: *yy0 = %s (*yy0, *yy1); break;\n",
384 fprintf (out
, " case %d: yy0->%s = %s (*yy0, *yy1); break;\n",
385 n
, p
->type
, p
->name
);
387 fputs ("]])\n\n", out
);
391 /*---------------------------------------------.
392 | Prepare the muscles for symbol definitions. |
393 `---------------------------------------------*/
396 prepare_symbol_definitions (void)
399 for (i
= 0; i
< nsyms
; ++i
)
401 symbol
*sym
= symbols
[i
];
405 #define SET_KEY(Entry) \
406 obstack_fgrow2 (&format_obstack, "symbol(%d, %s)", \
408 obstack_1grow (&format_obstack, 0); \
409 key = obstack_finish (&format_obstack);
411 #define SET_KEY2(Entry, Suffix) \
412 obstack_fgrow3 (&format_obstack, "symbol(%d, %s_%s)", \
414 obstack_1grow (&format_obstack, 0); \
415 key = obstack_finish (&format_obstack);
417 // Whether the symbol has an identifier.
418 value
= symbol_id_get (sym
);
420 MUSCLE_INSERT_INT (key
, !!value
);
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
);
434 MUSCLE_INSERT_INT (key
,
435 i
< ntokens
&& sym
!= errtoken
&& sym
!= undeftoken
);
438 MUSCLE_INSERT_INT (key
, sym
->number
);
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
);
475 /*--------------------------------------.
476 | Output the tokens definition to OUT. |
477 `--------------------------------------*/
480 token_definitions_output (FILE *out
)
483 char const *sep
= "";
485 fputs ("m4_define([b4_tokens], \n[", out
);
486 for (i
= 0; i
< ntokens
; ++i
)
488 symbol
*sym
= symbols
[i
];
489 int number
= sym
->user_token_number
;
490 uniqstr id
= symbol_id_get (sym
);
492 /* At this stage, if there are literal string aliases, they are
493 part of SYMBOLS, so we should not find their aliased symbols
495 aver (number
!= USER_NUMBER_HAS_STRING_ALIAS
);
497 /* Skip error token and tokens without identifier. */
498 if (sym
!= errtoken
&& id
)
500 fprintf (out
, "%s[[[%s]], %d]",
505 fputs ("])\n\n", out
);
510 prepare_actions (void)
512 /* Figure out the actions for the specified state, indexed by
513 lookahead token type. */
515 muscle_insert_rule_number_table ("defact", yydefact
,
516 yydefact
[0], 1, nstates
);
518 /* Figure out what to do after reducing with each rule, depending on
519 the saved state from before the beginning of parsing the data
520 that matched this rule. */
521 muscle_insert_state_number_table ("defgoto", yydefgoto
,
522 yydefgoto
[0], 1, nsyms
- ntokens
);
526 muscle_insert_base_table ("pact", base
,
527 base
[0], 1, nstates
);
528 MUSCLE_INSERT_INT ("pact_ninf", base_ninf
);
531 muscle_insert_base_table ("pgoto", base
,
532 base
[nstates
], nstates
+ 1, nvectors
);
534 muscle_insert_base_table ("table", table
,
535 table
[0], 1, high
+ 1);
536 MUSCLE_INSERT_INT ("table_ninf", table_ninf
);
538 muscle_insert_base_table ("check", check
,
539 check
[0], 1, high
+ 1);
541 /* GLR parsing slightly modifies YYTABLE and YYCHECK (and thus
542 YYPACT) so that in states with unresolved conflicts, the default
543 reduction is not used in the conflicted entries, so that there is
544 a place to put a conflict pointer.
546 This means that YYCONFLP and YYCONFL are nonsense for a non-GLR
547 parser, so we could avoid accidents by not writing them out in
548 that case. Nevertheless, it seems even better to be able to use
549 the GLR skeletons even without the non-deterministic tables. */
550 muscle_insert_unsigned_int_table ("conflict_list_heads", conflict_table
,
551 conflict_table
[0], 1, high
+ 1);
552 muscle_insert_unsigned_int_table ("conflicting_rules", conflict_list
,
553 0, 1, conflict_list_cnt
);
557 /*--------------------------------------------.
558 | Output the definitions of all the muscles. |
559 `--------------------------------------------*/
562 muscles_output (FILE *out
)
564 fputs ("m4_init()\n", out
);
566 symbol_numbers_output (out
);
567 token_definitions_output (out
);
568 type_names_output (out
);
569 user_actions_output (out
);
571 muscles_m4_output (out
);
574 /*---------------------------.
575 | Call the skeleton parser. |
576 `---------------------------*/
579 output_skeleton (void)
584 /* Compute the names of the package data dir and skeleton files. */
585 char const *m4
= (m4
= getenv ("M4")) ? m4
: M4
;
586 char const *datadir
= pkgdatadir ();
587 char *m4sugar
= xconcatenated_filename (datadir
, "m4sugar/m4sugar.m4", NULL
);
588 char *m4bison
= xconcatenated_filename (datadir
, "bison.m4", NULL
);
589 char *skel
= (IS_PATH_WITH_DIR (skeleton
)
591 : xconcatenated_filename (datadir
, skeleton
, NULL
));
593 /* Test whether m4sugar.m4 is readable, to check for proper
594 installation. A faulty installation can cause deadlock, so a
595 cheap sanity check is worthwhile. */
596 xfclose (xfopen (m4sugar
, "r"));
598 /* Create an m4 subprocess connected to us via two pipes. */
600 if (trace_flag
& trace_tools
)
601 fprintf (stderr
, "running: %s %s - %s %s\n",
602 m4
, m4sugar
, m4bison
, skel
);
604 /* Some future version of GNU M4 (most likely 1.6) may treat the -dV in a
605 position-dependent manner. Keep it as the first argument so that all
608 See the thread starting at
609 <http://lists.gnu.org/archive/html/bug-bison/2008-07/msg00000.html>
612 char const *argv
[10];
616 /* When POSIXLY_CORRECT is set, GNU M4 1.6 and later disable GNU
617 extensions, which Bison's skeletons depend on. With older M4,
618 it has no effect. M4 1.4.12 added a -g/--gnu command-line
619 option to make it explicit that a program wants GNU M4
620 extensions even when POSIXLY_CORRECT is set.
622 See the thread starting at
623 <http://lists.gnu.org/archive/html/bug-bison/2008-07/msg00000.html>
626 argv
[i
++] = M4_GNU_OPTION
;
630 if (trace_flag
& trace_m4
)
637 aver (i
<= ARRAY_CARDINALITY (argv
));
639 /* The ugly cast is because gnulib gets the const-ness wrong. */
640 pid
= create_pipe_bidi ("m4", m4
, (char **)(void*)argv
, false, true,
648 if (trace_flag
& trace_muscles
)
649 muscles_output (stderr
);
651 FILE *out
= xfdopen (filter_fd
[1], "w");
652 muscles_output (out
);
656 /* Read and process m4's output. */
657 timevar_push (TV_M4
);
659 FILE *in
= xfdopen (filter_fd
[0], "r");
661 /* scan_skel should have read all of M4's output. Otherwise, when we
662 close the pipe, we risk letting M4 report a broken-pipe to the
667 wait_subprocess (pid
, "m4", false, false, true, true, NULL
);
674 /* BISON_USE_PUSH_FOR_PULL is for the test suite and should not be
675 documented for the user. */
676 char const *cp
= getenv ("BISON_USE_PUSH_FOR_PULL");
677 bool use_push_for_pull_flag
= cp
&& *cp
&& strtol (cp
, 0, 10);
680 MUSCLE_INSERT_BOOL ("defines_flag", defines_flag
);
681 MUSCLE_INSERT_BOOL ("glr_flag", glr_parser
);
682 MUSCLE_INSERT_BOOL ("nondeterministic_flag", nondeterministic_parser
);
683 MUSCLE_INSERT_BOOL ("synclines_flag", !no_lines_flag
);
684 MUSCLE_INSERT_BOOL ("tag_seen_flag", tag_seen
);
685 MUSCLE_INSERT_BOOL ("use_push_for_pull_flag", use_push_for_pull_flag
);
686 MUSCLE_INSERT_BOOL ("yacc_flag", yacc_flag
);
689 if (spec_name_prefix
)
690 MUSCLE_INSERT_STRING ("prefix", spec_name_prefix
);
692 MUSCLE_INSERT_STRING ("file_name_all_but_ext", all_but_ext
);
694 #define DEFINE(Name) MUSCLE_INSERT_STRING (#Name, Name ? Name : "")
696 DEFINE (parser_file_name
);
697 DEFINE (spec_defines_file
);
698 DEFINE (spec_file_prefix
);
699 DEFINE (spec_graph_file
);
700 DEFINE (spec_name_prefix
);
701 DEFINE (spec_outfile
);
702 DEFINE (spec_verbose_file
);
705 /* Find the right skeleton file, and add muscles about the skeletons. */
707 MUSCLE_INSERT_C_STRING ("skeleton", skeleton
);
709 skeleton
= language
->skeleton
;
711 /* About the skeletons. */
713 /* b4_pkgdatadir is used inside m4_include in the skeletons, so digraphs
714 would never be expanded. Hopefully no one has M4-special characters in
715 his Bison installation path. */
716 MUSCLE_INSERT_STRING_RAW ("pkgdatadir", pkgdatadir ());
721 /*----------------------------------------------------------.
722 | Output the parsing tables and the parser code to ftable. |
723 `----------------------------------------------------------*/
728 obstack_init (&format_obstack
);
734 prepare_symbol_definitions ();
738 /* Process the selected skeleton file. */
741 obstack_free (&format_obstack
, NULL
);
747 char const *cp
= getenv ("BISON_PKGDATADIR");
748 return cp
? cp
: PKGDATADIR
;