]> git.saurik.com Git - bison.git/blob - src/output.c
Update to the current gnulib CVS repository, and fix trigraph handling
[bison.git] / src / output.c
1 /* Output the generated parsing program for Bison.
2
3 Copyright (C) 1984, 1986, 1989, 1992, 2000, 2001, 2002, 2003, 2004,
4 2005, 2006, 2007, 2008 Free 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 <http://www.gnu.org/licenses/>. */
20
21 #include <config.h>
22 #include "system.h"
23
24 #include <configmake.h>
25 #include <error.h>
26 #include <get-errno.h>
27 #include <quotearg.h>
28 #include <subpipe.h>
29 #include <timevar.h>
30
31 #include "complain.h"
32 #include "files.h"
33 #include "getargs.h"
34 #include "gram.h"
35 #include "muscle_tab.h"
36 #include "output.h"
37 #include "reader.h"
38 #include "scan-code.h" /* max_left_semantic_context */
39 #include "scan-skel.h"
40 #include "symtab.h"
41 #include "tables.h"
42
43
44 static struct obstack format_obstack;
45
46
47 /*-------------------------------------------------------------------.
48 | Create a function NAME which associates to the muscle NAME the |
49 | result of formatting the FIRST and then TABLE_DATA[BEGIN..END[ (of |
50 | TYPE), and to the muscle NAME_max, the max value of the |
51 | TABLE_DATA. |
52 `-------------------------------------------------------------------*/
53
54
55 #define GENERATE_MUSCLE_INSERT_TABLE(Name, Type) \
56 \
57 static void \
58 Name (char const *name, \
59 Type *table_data, \
60 Type first, \
61 int begin, \
62 int end) \
63 { \
64 Type min = first; \
65 Type max = first; \
66 long int lmin; \
67 long int lmax; \
68 int i; \
69 int j = 1; \
70 \
71 obstack_fgrow1 (&format_obstack, "%6d", first); \
72 for (i = begin; i < end; ++i) \
73 { \
74 obstack_1grow (&format_obstack, ','); \
75 if (j >= 10) \
76 { \
77 obstack_sgrow (&format_obstack, "\n "); \
78 j = 1; \
79 } \
80 else \
81 ++j; \
82 obstack_fgrow1 (&format_obstack, "%6d", table_data[i]); \
83 if (table_data[i] < min) \
84 min = table_data[i]; \
85 if (max < table_data[i]) \
86 max = table_data[i]; \
87 } \
88 obstack_1grow (&format_obstack, 0); \
89 muscle_insert (name, obstack_finish (&format_obstack)); \
90 \
91 lmin = min; \
92 lmax = max; \
93 /* Build `NAME_min' and `NAME_max' in the obstack. */ \
94 obstack_fgrow1 (&format_obstack, "%s_min", name); \
95 obstack_1grow (&format_obstack, 0); \
96 MUSCLE_INSERT_LONG_INT (obstack_finish (&format_obstack), lmin); \
97 obstack_fgrow1 (&format_obstack, "%s_max", name); \
98 obstack_1grow (&format_obstack, 0); \
99 MUSCLE_INSERT_LONG_INT (obstack_finish (&format_obstack), lmax); \
100 }
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_item_number_table, item_number)
108 GENERATE_MUSCLE_INSERT_TABLE(muscle_insert_state_number_table, state_number)
109
110
111 /*--------------------------------------------------------------------.
112 | Print to OUT a representation of STRING escaped both for C and M4. |
113 `--------------------------------------------------------------------*/
114
115 static void
116 escaped_output (FILE *out, char const *string)
117 {
118 char const *p;
119 fprintf (out, "[[");
120
121 for (p = quotearg_style (c_quoting_style, string); *p; p++)
122 switch (*p)
123 {
124 case '$': fputs ("$][", out); break;
125 case '@': fputs ("@@", out); break;
126 case '[': fputs ("@{", out); break;
127 case ']': fputs ("@}", out); break;
128 default: fputc (*p, out); break;
129 }
130
131 fprintf (out, "]]");
132 }
133
134
135 /*------------------------------------------------------------------.
136 | Prepare the muscles related to the symbols: translate, tname, and |
137 | toknum. |
138 `------------------------------------------------------------------*/
139
140 static void
141 prepare_symbols (void)
142 {
143 MUSCLE_INSERT_BOOL ("token_table", token_table_flag);
144 MUSCLE_INSERT_INT ("tokens_number", ntokens);
145 MUSCLE_INSERT_INT ("nterms_number", nvars);
146 MUSCLE_INSERT_INT ("undef_token_number", undeftoken->number);
147 MUSCLE_INSERT_INT ("user_token_number_max", max_user_token_number);
148
149 muscle_insert_symbol_number_table ("translate",
150 token_translations,
151 token_translations[0],
152 1, max_user_token_number + 1);
153
154 /* tname -- token names. */
155 {
156 int i;
157 /* We assume that the table will be output starting at column 2. */
158 int j = 2;
159 struct quoting_options *qo = clone_quoting_options (0);
160 set_quoting_style (qo, c_quoting_style);
161 set_quoting_flags (qo, QA_SPLIT_TRIGRAPHS);
162 for (i = 0; i < nsyms; i++)
163 {
164 char *cp = quotearg_alloc (symbols[i]->tag, -1, qo);
165 /* Width of the next token, including the two quotes, the
166 comma and the space. */
167 int width = strlen (cp) + 2;
168
169 if (j + width > 75)
170 {
171 obstack_sgrow (&format_obstack, "\n ");
172 j = 1;
173 }
174
175 if (i)
176 obstack_1grow (&format_obstack, ' ');
177 MUSCLE_OBSTACK_SGROW (&format_obstack, cp);
178 free (cp);
179 obstack_1grow (&format_obstack, ',');
180 j += width;
181 }
182 free (qo);
183 obstack_sgrow (&format_obstack, " ]b4_null[");
184
185 /* Finish table and store. */
186 obstack_1grow (&format_obstack, 0);
187 muscle_insert ("tname", obstack_finish (&format_obstack));
188 }
189
190 /* Output YYTOKNUM. */
191 {
192 int i;
193 int *values = xnmalloc (ntokens, sizeof *values);
194 for (i = 0; i < ntokens; ++i)
195 values[i] = symbols[i]->user_token_number;
196 muscle_insert_int_table ("toknum", values,
197 values[0], 1, ntokens);
198 free (values);
199 }
200 }
201
202
203 /*-------------------------------------------------------------.
204 | Prepare the muscles related to the rules: rhs, prhs, r1, r2, |
205 | rline, dprec, merger. |
206 `-------------------------------------------------------------*/
207
208 static void
209 prepare_rules (void)
210 {
211 rule_number r;
212 unsigned int i = 0;
213 item_number *rhs = xnmalloc (nritems, sizeof *rhs);
214 unsigned int *prhs = xnmalloc (nrules, sizeof *prhs);
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
221 for (r = 0; r < nrules; ++r)
222 {
223 item_number *rhsp = NULL;
224 /* Index of rule R in RHS. */
225 prhs[r] = i;
226 /* RHS of the rule R. */
227 for (rhsp = rules[r].rhs; *rhsp >= 0; ++rhsp)
228 rhs[i++] = *rhsp;
229 /* LHS of the rule R. */
230 r1[r] = rules[r].lhs->number;
231 /* Length of rule R's RHS. */
232 r2[r] = i - prhs[r];
233 /* Separator in RHS. */
234 rhs[i++] = -1;
235 /* Line where rule was defined. */
236 rline[r] = rules[r].location.start.line;
237 /* Dynamic precedence (GLR). */
238 dprec[r] = rules[r].dprec;
239 /* Merger-function index (GLR). */
240 merger[r] = rules[r].merger;
241 }
242 aver (i == nritems);
243
244 muscle_insert_item_number_table ("rhs", rhs, ritem[0], 1, nritems);
245 muscle_insert_unsigned_int_table ("prhs", prhs, 0, 0, nrules);
246 muscle_insert_unsigned_int_table ("rline", rline, 0, 0, nrules);
247 muscle_insert_symbol_number_table ("r1", r1, 0, 0, nrules);
248 muscle_insert_unsigned_int_table ("r2", r2, 0, 0, nrules);
249 muscle_insert_int_table ("dprec", dprec, 0, 0, nrules);
250 muscle_insert_int_table ("merger", merger, 0, 0, nrules);
251
252 MUSCLE_INSERT_INT ("rules_number", nrules);
253 MUSCLE_INSERT_INT ("max_left_semantic_context", max_left_semantic_context);
254
255 free (rhs);
256 free (prhs);
257 free (rline);
258 free (r1);
259 free (r2);
260 free (dprec);
261 free (merger);
262 }
263
264 /*--------------------------------------------.
265 | Prepare the muscles related to the states. |
266 `--------------------------------------------*/
267
268 static void
269 prepare_states (void)
270 {
271 state_number i;
272 symbol_number *values = xnmalloc (nstates, sizeof *values);
273 for (i = 0; i < nstates; ++i)
274 values[i] = states[i]->accessing_symbol;
275 muscle_insert_symbol_number_table ("stos", values,
276 0, 1, nstates);
277 free (values);
278
279 MUSCLE_INSERT_INT ("last", high);
280 MUSCLE_INSERT_INT ("final_state_number", final_state->number);
281 MUSCLE_INSERT_INT ("states_number", nstates);
282 }
283
284
285
286 /*---------------------------------.
287 | Output the user actions to OUT. |
288 `---------------------------------*/
289
290 static void
291 user_actions_output (FILE *out)
292 {
293 rule_number r;
294
295 fputs ("m4_define([b4_actions], \n[", out);
296 for (r = 0; r < nrules; ++r)
297 if (rules[r].action)
298 {
299 fprintf (out, "b4_case(%d, [b4_syncline(%d, ", r + 1,
300 rules[r].action_location.start.line);
301 escaped_output (out, rules[r].action_location.start.file);
302 fprintf (out, ")\n[ %s]])\n\n", rules[r].action);
303 }
304 fputs ("])\n\n", out);
305 }
306
307 /*--------------------------------------.
308 | Output the merge functions to OUT. |
309 `--------------------------------------*/
310
311 static void
312 merger_output (FILE *out)
313 {
314 int n;
315 merger_list* p;
316
317 fputs ("m4_define([b4_mergers], \n[[", out);
318 for (n = 1, p = merge_functions; p != NULL; n += 1, p = p->next)
319 {
320 if (p->type[0] == '\0')
321 fprintf (out, " case %d: *yy0 = %s (*yy0, *yy1); break;\n",
322 n, p->name);
323 else
324 fprintf (out, " case %d: yy0->%s = %s (*yy0, *yy1); break;\n",
325 n, p->type, p->name);
326 }
327 fputs ("]])\n\n", out);
328 }
329
330 /*--------------------------------------.
331 | Output the tokens definition to OUT. |
332 `--------------------------------------*/
333
334 static void
335 token_definitions_output (FILE *out)
336 {
337 int i;
338 char const *sep = "";
339
340 fputs ("m4_define([b4_tokens], \n[", out);
341 for (i = 0; i < ntokens; ++i)
342 {
343 symbol *sym = symbols[i];
344 int number = sym->user_token_number;
345
346 /* At this stage, if there are literal aliases, they are part of
347 SYMBOLS, so we should not find symbols which are the aliases
348 here. */
349 aver (number != USER_NUMBER_ALIAS);
350
351 /* Skip error token. */
352 if (sym == errtoken)
353 continue;
354
355 /* If this string has an alias, then it is necessarily the alias
356 which is to be output. */
357 if (sym->alias)
358 sym = sym->alias;
359
360 /* Don't output literal chars or strings (when defined only as a
361 string). Note that must be done after the alias resolution:
362 think about `%token 'f' "f"'. */
363 if (sym->tag[0] == '\'' || sym->tag[0] == '\"')
364 continue;
365
366 /* Don't #define nonliteral tokens whose names contain periods
367 or '$' (as does the default value of the EOF token). */
368 if (strchr (sym->tag, '.') || strchr (sym->tag, '$'))
369 continue;
370
371 fprintf (out, "%s[[[%s]], %d]",
372 sep, sym->tag, number);
373 sep = ",\n";
374 }
375 fputs ("])\n\n", out);
376 }
377
378
379 /*---------------------------------------------------.
380 | Output the symbol destructors or printers to OUT. |
381 `---------------------------------------------------*/
382
383 static void
384 symbol_code_props_output (FILE *out, char const *what,
385 code_props const *(*get)(symbol const *))
386 {
387 int i;
388 char const *sep = "";
389
390 fputs ("m4_define([b4_symbol_", out);
391 fputs (what, out);
392 fputs ("], \n[", out);
393 for (i = 0; i < nsyms; ++i)
394 {
395 symbol *sym = symbols[i];
396 char const *code = (*get) (sym)->code;
397 if (code)
398 {
399 location loc = (*get) (sym)->location;
400 /* Filename, lineno,
401 Symbol-name, Symbol-number,
402 code, optional typename. */
403 fprintf (out, "%s[", sep);
404 sep = ",\n";
405 escaped_output (out, loc.start.file);
406 fprintf (out, ", %d, ", loc.start.line);
407 escaped_output (out, sym->tag);
408 fprintf (out, ", %d, [[%s]]", sym->number, code);
409 if (sym->type_name)
410 fprintf (out, ", [[%s]]", sym->type_name);
411 fputc (']', out);
412 }
413 }
414 fputs ("])\n\n", out);
415 }
416
417
418 static void
419 prepare_actions (void)
420 {
421 /* Figure out the actions for the specified state, indexed by
422 lookahead token type. */
423
424 muscle_insert_rule_number_table ("defact", yydefact,
425 yydefact[0], 1, nstates);
426
427 /* Figure out what to do after reducing with each rule, depending on
428 the saved state from before the beginning of parsing the data
429 that matched this rule. */
430 muscle_insert_state_number_table ("defgoto", yydefgoto,
431 yydefgoto[0], 1, nsyms - ntokens);
432
433
434 /* Output PACT. */
435 muscle_insert_base_table ("pact", base,
436 base[0], 1, nstates);
437 MUSCLE_INSERT_INT ("pact_ninf", base_ninf);
438
439 /* Output PGOTO. */
440 muscle_insert_base_table ("pgoto", base,
441 base[nstates], nstates + 1, nvectors);
442
443 muscle_insert_base_table ("table", table,
444 table[0], 1, high + 1);
445 MUSCLE_INSERT_INT ("table_ninf", table_ninf);
446
447 muscle_insert_base_table ("check", check,
448 check[0], 1, high + 1);
449
450 /* GLR parsing slightly modifies YYTABLE and YYCHECK (and thus
451 YYPACT) so that in states with unresolved conflicts, the default
452 reduction is not used in the conflicted entries, so that there is
453 a place to put a conflict pointer.
454
455 This means that YYCONFLP and YYCONFL are nonsense for a non-GLR
456 parser, so we could avoid accidents by not writing them out in
457 that case. Nevertheless, it seems even better to be able to use
458 the GLR skeletons even without the non-deterministic tables. */
459 muscle_insert_unsigned_int_table ("conflict_list_heads", conflict_table,
460 conflict_table[0], 1, high + 1);
461 muscle_insert_unsigned_int_table ("conflicting_rules", conflict_list,
462 0, 1, conflict_list_cnt);
463 }
464
465 \f
466 /*---------------------------.
467 | Call the skeleton parser. |
468 `---------------------------*/
469
470 static void
471 output_skeleton (void)
472 {
473 FILE *in;
474 FILE *out;
475 int filter_fd[2];
476 char const *argv[7];
477 pid_t pid;
478
479 /* Compute the names of the package data dir and skeleton file.
480 Test whether m4sugar.m4 is readable, to check for proper
481 installation. A faulty installation can cause deadlock, so a
482 cheap sanity check is worthwhile. */
483 char const m4sugar[] = "m4sugar/m4sugar.m4";
484 char const m4bison[] = "bison.m4";
485 char *full_m4sugar;
486 char *full_m4bison;
487 char *full_skeleton;
488 char const *p;
489 char const *m4 = (p = getenv ("M4")) ? p : M4;
490 char const *pkgdatadir = compute_pkgdatadir ();
491 size_t skeleton_size = strlen (skeleton) + 1;
492 size_t pkgdatadirlen = strlen (pkgdatadir);
493 while (pkgdatadirlen && pkgdatadir[pkgdatadirlen - 1] == '/')
494 pkgdatadirlen--;
495 full_skeleton = xmalloc (pkgdatadirlen + 1
496 + (skeleton_size < sizeof m4sugar
497 ? sizeof m4sugar : skeleton_size));
498 strncpy (full_skeleton, pkgdatadir, pkgdatadirlen);
499 full_skeleton[pkgdatadirlen] = '/';
500 strcpy (full_skeleton + pkgdatadirlen + 1, m4sugar);
501 full_m4sugar = xstrdup (full_skeleton);
502 strcpy (full_skeleton + pkgdatadirlen + 1, m4bison);
503 full_m4bison = xstrdup (full_skeleton);
504 if (strchr (skeleton, '/'))
505 strcpy (full_skeleton, skeleton);
506 else
507 strcpy (full_skeleton + pkgdatadirlen + 1, skeleton);
508 xfclose (xfopen (full_m4sugar, "r"));
509
510 /* Create an m4 subprocess connected to us via two pipes. */
511
512 if (trace_flag & trace_tools)
513 fprintf (stderr, "running: %s %s - %s %s\n",
514 m4, full_m4sugar, full_m4bison, full_skeleton);
515
516 argv[0] = m4;
517 argv[1] = full_m4sugar;
518 argv[2] = "-";
519 argv[3] = full_m4bison;
520 argv[4] = full_skeleton;
521 argv[5] = trace_flag & trace_m4 ? "-dV" : NULL;
522 argv[6] = NULL;
523
524 init_subpipe ();
525 pid = create_subpipe (argv, filter_fd);
526 free (full_m4bison);
527 free (full_m4sugar);
528 free (full_skeleton);
529
530 out = fdopen (filter_fd[0], "w");
531 if (! out)
532 error (EXIT_FAILURE, get_errno (),
533 "fdopen");
534
535 /* Output the definitions of all the muscles. */
536 fputs ("m4_init()\n", out);
537
538 user_actions_output (out);
539 merger_output (out);
540 token_definitions_output (out);
541 symbol_code_props_output (out, "destructors", &symbol_destructor_get);
542 symbol_code_props_output (out, "printers", &symbol_printer_get);
543
544 muscles_m4_output (out);
545 xfclose (out);
546
547 /* Read and process m4's output. */
548 timevar_push (TV_M4);
549 end_of_output_subpipe (pid, filter_fd);
550 in = fdopen (filter_fd[1], "r");
551 if (! in)
552 error (EXIT_FAILURE, get_errno (),
553 "fdopen");
554 scan_skel (in);
555 xfclose (in);
556 reap_subpipe (pid, m4);
557 timevar_pop (TV_M4);
558 }
559
560 static void
561 prepare (void)
562 {
563 /* BISON_USE_PUSH_FOR_PULL is for the test suite and should not be documented
564 for the user. */
565 char const *use_push_for_pull_env = getenv ("BISON_USE_PUSH_FOR_PULL");
566 bool use_push_for_pull_flag = false;
567 if (use_push_for_pull_env != NULL
568 && use_push_for_pull_env[0] != '\0'
569 && 0 != strcmp (use_push_for_pull_env, "0"))
570 use_push_for_pull_flag = true;
571
572 /* Flags. */
573 MUSCLE_INSERT_BOOL ("debug_flag", debug_flag);
574 MUSCLE_INSERT_BOOL ("defines_flag", defines_flag);
575 MUSCLE_INSERT_BOOL ("error_verbose_flag", error_verbose);
576 MUSCLE_INSERT_BOOL ("glr_flag", glr_parser);
577 MUSCLE_INSERT_BOOL ("locations_flag", locations_flag);
578 MUSCLE_INSERT_BOOL ("nondeterministic_flag", nondeterministic_parser);
579 MUSCLE_INSERT_BOOL ("synclines_flag", !no_lines_flag);
580 MUSCLE_INSERT_BOOL ("tag_seen_flag", tag_seen);
581 MUSCLE_INSERT_BOOL ("use_push_for_pull_flag", use_push_for_pull_flag);
582 MUSCLE_INSERT_BOOL ("yacc_flag", yacc_flag);
583
584 /* File names. */
585 if (spec_name_prefix)
586 MUSCLE_INSERT_STRING ("prefix", spec_name_prefix);
587
588 MUSCLE_INSERT_STRING ("file_name_all_but_ext", all_but_ext);
589
590 #define DEFINE(Name) MUSCLE_INSERT_STRING (#Name, Name ? Name : "")
591 DEFINE (dir_prefix);
592 DEFINE (parser_file_name);
593 DEFINE (spec_defines_file);
594 DEFINE (spec_file_prefix);
595 DEFINE (spec_graph_file);
596 DEFINE (spec_name_prefix);
597 DEFINE (spec_outfile);
598 DEFINE (spec_verbose_file);
599 #undef DEFINE
600
601 /* Find the right skeleton file, and add muscles about the skeletons. */
602 if (skeleton)
603 MUSCLE_INSERT_C_STRING ("skeleton", skeleton);
604 else
605 skeleton = language->skeleton;
606
607 /* About the skeletons. */
608 {
609 /* b4_pkgdatadir is used inside m4_include in the skeletons, so digraphs
610 would never be expanded. Hopefully no one has M4-special characters in
611 his Bison installation path. */
612 MUSCLE_INSERT_STRING_RAW ("pkgdatadir", compute_pkgdatadir ());
613 }
614 }
615
616
617 /*----------------------------------------------------------.
618 | Output the parsing tables and the parser code to ftable. |
619 `----------------------------------------------------------*/
620
621 void
622 output (void)
623 {
624 obstack_init (&format_obstack);
625
626 prepare_symbols ();
627 prepare_rules ();
628 prepare_states ();
629 prepare_actions ();
630
631 prepare ();
632
633 /* Process the selected skeleton file. */
634 output_skeleton ();
635
636 obstack_free (&format_obstack, NULL);
637 }
638
639 char const *
640 compute_pkgdatadir (void)
641 {
642 char const *pkgdatadir = getenv ("BISON_PKGDATADIR");
643 return pkgdatadir ? pkgdatadir : PKGDATADIR;
644 }