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