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