]> git.saurik.com Git - bison.git/blob - src/output.c
Include "subpipe.h".
[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 /* The parser tables consist of these tables.
24
25 YYTRANSLATE = vector mapping yylex's token numbers into bison's
26 token numbers.
27
28 YYTNAME = vector of string-names indexed by bison token number.
29
30 YYTOKNUM = vector of yylex token numbers corresponding to entries
31 in YYTNAME.
32
33 YYRLINE = vector of line-numbers of all rules. For yydebug
34 printouts.
35
36 YYRHS = vector of items of all rules. This is exactly what RITEMS
37 contains. For yydebug and for semantic parser.
38
39 YYPRHS[R] = index in YYRHS of first item for rule R.
40
41 YYR1[R] = symbol number of symbol that rule R derives.
42
43 YYR2[R] = number of symbols composing right hand side of rule R.
44
45 YYSTOS[S] = the symbol number of the symbol that leads to state S.
46
47 YYDEFACT[S] = default rule to reduce with in state s, when YYTABLE
48 doesn't specify something else to do. Zero means the default is an
49 error.
50
51 YYDEFGOTO[I] = default state to go to after a reduction of a rule
52 that generates variable NTOKENS + I, except when YYTABLE specifies
53 something else to do.
54
55 YYPACT[S] = index in YYTABLE of the portion describing state S.
56 The lookahead token's type is used to index that portion to find
57 out what to do.
58
59 If the value in YYTABLE is positive, we shift the token and go to
60 that state.
61
62 If the value is negative, it is minus a rule number to reduce by.
63
64 If the value is zero, the default action from YYDEFACT[S] is used.
65
66 YYPGOTO[I] = the index in YYTABLE of the portion describing what to
67 do after reducing a rule that derives variable I + NTOKENS. This
68 portion is indexed by the parser state number, S, as of before the
69 text for this nonterminal was read. The value from YYTABLE is the
70 state to go to if the corresponding value in YYCHECK is S.
71
72 YYTABLE = a vector filled with portions for different uses, found
73 via YYPACT and YYPGOTO.
74
75 YYCHECK = a vector indexed in parallel with YYTABLE. It indicates,
76 in a roundabout way, the bounds of the portion you are trying to
77 examine.
78
79 Suppose that the portion of YYTABLE starts at index P and the index
80 to be examined within the portion is I. Then if YYCHECK[P+I] != I,
81 I is outside the bounds of what is actually allocated, and the
82 default (from YYDEFACT or YYDEFGOTO) should be used. Otherwise,
83 YYTABLE[P+I] should be used.
84
85 YYFINAL = the state number of the termination state. YYFLAG = most
86 negative short int. Used to flag ?? */
87
88 #include "system.h"
89 #include "quotearg.h"
90 #include "error.h"
91 #include "subpipe.h"
92 #include "getargs.h"
93 #include "files.h"
94 #include "gram.h"
95 #include "complain.h"
96 #include "output.h"
97 #include "reader.h"
98 #include "symtab.h"
99 #include "tables.h"
100 #include "muscle_tab.h"
101
102 /* From src/scan-skel.l. */
103 void scan_skel (FILE *);
104
105
106 static struct obstack format_obstack;
107
108 int error_verbose = 0;
109
110
111
112 /*-------------------------------------------------------------------.
113 | Create a function NAME which associates to the muscle NAME the |
114 | result of formatting the FIRST and then TABLE_DATA[BEGIN..END[ (of |
115 | TYPE), and to the muscle NAME_max, the max value of the |
116 | TABLE_DATA. |
117 `-------------------------------------------------------------------*/
118
119
120 #define GENERATE_MUSCLE_INSERT_TABLE(Name, Type) \
121 \
122 static void \
123 Name (const char *name, \
124 Type *table_data, \
125 Type first, \
126 int begin, \
127 int end) \
128 { \
129 Type min = first; \
130 Type max = first; \
131 int i; \
132 int j = 1; \
133 \
134 obstack_fgrow1 (&format_obstack, "%6d", first); \
135 for (i = begin; i < end; ++i) \
136 { \
137 obstack_1grow (&format_obstack, ','); \
138 if (j >= 10) \
139 { \
140 obstack_sgrow (&format_obstack, "\n "); \
141 j = 1; \
142 } \
143 else \
144 ++j; \
145 obstack_fgrow1 (&format_obstack, "%6d", table_data[i]); \
146 if (table_data[i] < min) \
147 min = table_data[i]; \
148 if (max < table_data[i]) \
149 max = table_data[i]; \
150 } \
151 obstack_1grow (&format_obstack, 0); \
152 muscle_insert (name, obstack_finish (&format_obstack)); \
153 \
154 /* Build `NAME_min' and `NAME_max' in the obstack. */ \
155 obstack_fgrow1 (&format_obstack, "%s_min", name); \
156 obstack_1grow (&format_obstack, 0); \
157 MUSCLE_INSERT_LONG_INT (obstack_finish (&format_obstack), \
158 (long int) min); \
159 obstack_fgrow1 (&format_obstack, "%s_max", name); \
160 obstack_1grow (&format_obstack, 0); \
161 MUSCLE_INSERT_LONG_INT (obstack_finish (&format_obstack), \
162 (long int) max); \
163 }
164
165 GENERATE_MUSCLE_INSERT_TABLE(muscle_insert_unsigned_int_table, unsigned int)
166 GENERATE_MUSCLE_INSERT_TABLE(muscle_insert_int_table, int)
167 GENERATE_MUSCLE_INSERT_TABLE(muscle_insert_short_table, short)
168 GENERATE_MUSCLE_INSERT_TABLE(muscle_insert_base_table, base_t)
169 GENERATE_MUSCLE_INSERT_TABLE(muscle_insert_rule_number_table, rule_number_t)
170 GENERATE_MUSCLE_INSERT_TABLE(muscle_insert_symbol_number_table, symbol_number_t)
171 GENERATE_MUSCLE_INSERT_TABLE(muscle_insert_item_number_table, item_number_t)
172 GENERATE_MUSCLE_INSERT_TABLE(muscle_insert_state_number_table, state_number_t)
173
174
175 /*-----------------------------------------------------------------.
176 | Prepare the muscles related to the tokens: translate, tname, and |
177 | toknum. |
178 `-----------------------------------------------------------------*/
179
180 static void
181 prepare_tokens (void)
182 {
183 muscle_insert_symbol_number_table ("translate",
184 token_translations,
185 token_translations[0],
186 1, max_user_token_number + 1);
187
188 {
189 int i;
190 int j = 0;
191 for (i = 0; i < nsyms; i++)
192 {
193 /* Be sure not to use twice the same QUOTEARG slot:
194 SYMBOL_TAG_GET uses slot 0. */
195 const char *cp =
196 quotearg_n_style (1, c_quoting_style,
197 symbols[i]->tag);
198 /* Width of the next token, including the two quotes, the coma
199 and the space. */
200 int strsize = strlen (cp) + 2;
201
202 if (j + strsize > 75)
203 {
204 obstack_sgrow (&format_obstack, "\n ");
205 j = 2;
206 }
207
208 obstack_sgrow (&format_obstack, cp);
209 obstack_sgrow (&format_obstack, ", ");
210 j += strsize;
211 }
212 /* Add a NULL entry to list of tokens (well, 0, as NULL might not be
213 defined). */
214 obstack_sgrow (&format_obstack, "0");
215
216 /* Finish table and store. */
217 obstack_1grow (&format_obstack, 0);
218 muscle_insert ("tname", obstack_finish (&format_obstack));
219 }
220
221 /* Output YYTOKNUM. */
222 {
223 int i;
224 int *values = XCALLOC (int, ntokens);
225 for (i = 0; i < ntokens; ++i)
226 values[i] = symbols[i]->user_token_number;
227 muscle_insert_int_table ("toknum", values,
228 values[0], 1, ntokens);
229 free (values);
230 }
231 }
232
233
234 /*-------------------------------------------------------------.
235 | Prepare the muscles related to the rules: rhs, prhs, r1, r2, |
236 | rline, dprec, merger |
237 `-------------------------------------------------------------*/
238
239 static void
240 prepare_rules (void)
241 {
242 rule_number_t r;
243 unsigned int i = 0;
244 item_number_t *rhs = XMALLOC (item_number_t, nritems);
245 unsigned int *prhs = XMALLOC (unsigned int, nrules);
246 unsigned int *rline = XMALLOC (unsigned int, nrules);
247 symbol_number_t *r1 = XMALLOC (symbol_number_t, nrules);
248 unsigned int *r2 = XMALLOC (unsigned int, nrules);
249 short *dprec = XMALLOC (short, nrules);
250 short *merger = XMALLOC (short, nrules);
251
252 for (r = 0; r < nrules; ++r)
253 {
254 item_number_t *rhsp = NULL;
255 /* Index of rule R in RHS. */
256 prhs[r] = i;
257 /* RHS of the rule R. */
258 for (rhsp = rules[r].rhs; *rhsp >= 0; ++rhsp)
259 rhs[i++] = *rhsp;
260 /* LHS of the rule R. */
261 r1[r] = rules[r].lhs->number;
262 /* Length of rule R's RHS. */
263 r2[r] = i - prhs[r];
264 /* Separator in RHS. */
265 rhs[i++] = -1;
266 /* Line where rule was defined. */
267 rline[r] = rules[r].location.first_line;
268 /* Dynamic precedence (GLR) */
269 dprec[r] = rules[r].dprec;
270 /* Merger-function index (GLR) */
271 merger[r] = rules[r].merger;
272 }
273 assert (i == nritems);
274
275 muscle_insert_item_number_table ("rhs", rhs, ritem[0], 1, nritems);
276 muscle_insert_unsigned_int_table ("prhs", prhs, 0, 0, nrules);
277 muscle_insert_unsigned_int_table ("rline", rline, 0, 0, nrules);
278 muscle_insert_symbol_number_table ("r1", r1, 0, 0, nrules);
279 muscle_insert_unsigned_int_table ("r2", r2, 0, 0, nrules);
280 muscle_insert_short_table ("dprec", dprec, 0, 0, nrules);
281 muscle_insert_short_table ("merger", merger, 0, 0, nrules);
282
283 free (rhs);
284 free (prhs);
285 free (rline);
286 free (r1);
287 free (r2);
288 free (dprec);
289 free (merger);
290 }
291
292 /*--------------------------------------------.
293 | Prepare the muscles related to the states. |
294 `--------------------------------------------*/
295
296 static void
297 prepare_states (void)
298 {
299 state_number_t i;
300 symbol_number_t *values =
301 (symbol_number_t *) alloca (sizeof (symbol_number_t) * nstates);
302 for (i = 0; i < nstates; ++i)
303 values[i] = states[i]->accessing_symbol;
304 muscle_insert_symbol_number_table ("stos", values,
305 0, 1, nstates);
306 }
307
308
309
310 /*---------------------------------.
311 | Output the user actions to OUT. |
312 `---------------------------------*/
313
314 static void
315 user_actions_output (FILE *out)
316 {
317 rule_number_t r;
318
319 fputs ("m4_define([b4_actions], \n[[", out);
320 for (r = 0; r < nrules; ++r)
321 if (rules[r].action)
322 {
323 fprintf (out, " case %d:\n", r + 1);
324
325 if (!no_lines_flag)
326 fprintf (out, muscle_find ("linef"),
327 rules[r].action_location.first_line,
328 quotearg_style (c_quoting_style,
329 muscle_find ("filename")));
330 fprintf (out, " %s\n break;\n\n",
331 rules[r].action);
332 }
333 fputs ("]])\n\n", out);
334 }
335
336 /*--------------------------------------.
337 | Output the merge functions to OUT. |
338 `--------------------------------------*/
339
340 static void
341 merger_output (FILE *out)
342 {
343 int n;
344 merger_list* p;
345
346 fputs ("m4_define([b4_mergers], \n[[", out);
347 for (n = 1, p = merge_functions; p != NULL; n += 1, p = p->next)
348 {
349 if (p->type[0] == '\0')
350 fprintf (out, " case %d: yyval = %s (*yy0, *yy1); break;\n",
351 n, p->name);
352 else
353 fprintf (out, " case %d: yyval.%s = %s (*yy0, *yy1); break;\n",
354 n, p->type, p->name);
355 }
356 fputs ("]])\n\n", out);
357 }
358
359 /*--------------------------------------.
360 | Output the tokens definition to OUT. |
361 `--------------------------------------*/
362
363 static void
364 token_definitions_output (FILE *out)
365 {
366 int i;
367 int first = 1;
368
369 fputs ("m4_define([b4_tokens], \n[", out);
370 for (i = 0; i < ntokens; ++i)
371 {
372 symbol_t *symbol = symbols[i];
373 int number = symbol->user_token_number;
374
375 /* At this stage, if there are literal aliases, they are part of
376 SYMBOLS, so we should not find symbols which are the aliases
377 here. */
378 assert (number != USER_NUMBER_ALIAS);
379
380 /* Skip error token. */
381 if (symbol == errtoken)
382 continue;
383
384 /* If this string has an alias, then it is necessarily the alias
385 which is to be output. */
386 if (symbol->alias)
387 symbol = symbol->alias;
388
389 /* Don't output literal chars or strings (when defined only as a
390 string). Note that must be done after the alias resolution:
391 think about `%token 'f' "f"'. */
392 if (symbol->tag[0] == '\'' || symbol->tag[0] == '\"')
393 continue;
394
395 /* Don't #define nonliteral tokens whose names contain periods
396 or '$' (as does the default value of the EOF token). */
397 if (strchr (symbol->tag, '.') || strchr (symbol->tag, '$'))
398 continue;
399
400 fprintf (out, "%s[[[%s]], [%d]]",
401 first ? "" : ",\n", symbol->tag, number);
402
403 first = 0;
404 }
405 fputs ("])\n\n", out);
406 }
407
408
409 /*---------------------------------------.
410 | Output the symbol destructors to OUT. |
411 `---------------------------------------*/
412
413 static void
414 symbol_destructors_output (FILE *out)
415 {
416 int i;
417 int first = 1;
418
419 fputs ("m4_define([b4_symbol_destructors], \n[", out);
420 for (i = 0; i < nsyms; ++i)
421 if (symbols[i]->destructor)
422 {
423 symbol_t *symbol = symbols[i];
424
425 /* Filename, lineno,
426 Symbol-name, Symbol-number,
427 destructor, typename. */
428 fprintf (out, "%s[[[%s]], [[%d]], [[%s]], [[%d]], [[%s]], [[%s]]]",
429 first ? "" : ",\n",
430 infile, symbol->destructor_location.first_line,
431 symbol->tag,
432 symbol->number,
433 symbol->destructor,
434 symbol->type_name);
435
436 first = 0;
437 }
438 fputs ("])\n\n", out);
439 }
440
441
442 /*------------------------------------.
443 | Output the symbol printers to OUT. |
444 `------------------------------------*/
445
446 static void
447 symbol_printers_output (FILE *out)
448 {
449 int i;
450 int first = 1;
451
452 fputs ("m4_define([b4_symbol_printers], \n[", out);
453 for (i = 0; i < nsyms; ++i)
454 if (symbols[i]->destructor)
455 {
456 symbol_t *symbol = symbols[i];
457
458 /* Filename, lineno,
459 Symbol-name, Symbol-number,
460 printer, typename. */
461 fprintf (out, "%s[[[%s]], [[%d]], [[%s]], [[%d]], [[%s]], [[%s]]]",
462 first ? "" : ",\n",
463 infile, symbol->printer_location.first_line,
464 symbol->tag,
465 symbol->number,
466 symbol->printer,
467 symbol->type_name);
468
469 first = 0;
470 }
471 fputs ("])\n\n", out);
472 }
473
474
475 static void
476 prepare_actions (void)
477 {
478 /* Figure out the actions for the specified state, indexed by
479 lookahead token type. */
480
481 muscle_insert_rule_number_table ("defact", yydefact,
482 yydefact[0], 1, nstates);
483
484 /* Figure out what to do after reducing with each rule, depending on
485 the saved state from before the beginning of parsing the data
486 that matched this rule. */
487 muscle_insert_state_number_table ("defgoto", yydefgoto,
488 yydefgoto[0], 1, nsyms - ntokens);
489
490
491 /* Output PACT. */
492 muscle_insert_base_table ("pact", base,
493 base[0], 1, nstates);
494 MUSCLE_INSERT_INT ("pact_ninf", base_ninf);
495
496 /* Output PGOTO. */
497 muscle_insert_base_table ("pgoto", base,
498 base[nstates], nstates + 1, nvectors);
499
500 muscle_insert_base_table ("table", table,
501 table[0], 1, high + 1);
502 MUSCLE_INSERT_INT ("table_ninf", table_ninf);
503
504 muscle_insert_base_table ("check", check,
505 check[0], 1, high + 1);
506
507 /* GLR parsing slightly modifies YYTABLE and YYCHECK (and thus
508 YYPACT) so that in states with unresolved conflicts, the default
509 reduction is not used in the conflicted entries, so that there is
510 a place to put a conflict pointer.
511
512 This means that YYCONFLP and YYCONFL are nonsense for a non-GLR
513 parser, so we could avoid accidents by not writing them out in
514 that case. Nevertheless, it seems even better to be able to use
515 the GLR skeletons even without the non-deterministic tables. */
516 muscle_insert_unsigned_int_table ("conflict_list_heads", conflict_table,
517 conflict_table[0], 1, high+1);
518 muscle_insert_unsigned_int_table ("conflicting_rules", conflict_list,
519 conflict_list[0], 1, conflict_list_cnt);
520 }
521
522 \f
523 /*---------------------------.
524 | Call the skeleton parser. |
525 `---------------------------*/
526
527 static void
528 output_skeleton (void)
529 {
530 FILE *in;
531 FILE *out;
532 int filter_fd[2];
533 char const *argv[7];
534 pid_t pid;
535
536 /* Compute the names of the package data dir and skeleton file.
537 Test whether m4sugar.m4 is readable, to check for proper
538 installation. A faulty installation can cause deadlock, so a
539 cheap sanity check is worthwhile. */
540 char const m4sugar[] = "m4sugar/m4sugar.m4";
541 char *full_path;
542 char const *p;
543 char const *m4 = (p = getenv ("M4")) ? p : M4;
544 char const *pkgdatadir = (p = getenv ("BISON_PKGDATADIR")) ? p : PKGDATADIR;
545 size_t skeleton_size = strlen (skeleton) + 1;
546 size_t pkgdatadirlen = strlen (pkgdatadir);
547 while (pkgdatadirlen && pkgdatadir[pkgdatadirlen - 1] == '/')
548 pkgdatadirlen--;
549 full_path = xmalloc (pkgdatadirlen + 1
550 + (skeleton_size < sizeof m4sugar
551 ? sizeof m4sugar : skeleton_size));
552 strcpy (full_path, pkgdatadir);
553 full_path[pkgdatadirlen] = '/';
554 strcpy (full_path + pkgdatadirlen + 1, m4sugar);
555 in = fopen (full_path, "r");
556 if (! in || fclose (in) != 0)
557 error (EXIT_FAILURE, errno, "%s", full_path);
558 strcpy (full_path + pkgdatadirlen + 1, skeleton);
559
560 /* Create an m4 subprocess connected to us via two pipes. */
561
562 if (trace_flag & trace_tools)
563 fprintf (stderr, "running: %s -I %s %s - %s\n",
564 m4, pkgdatadir, m4sugar, full_path);
565
566 argv[0] = m4;
567 argv[1] = "-I";
568 argv[2] = pkgdatadir;
569 argv[3] = m4sugar;
570 argv[4] = "-";
571 argv[5] = full_path;
572 argv[6] = NULL;
573
574 init_subpipe ();
575 pid = create_subpipe (argv, filter_fd);
576 free (full_path);
577
578 out = fdopen (filter_fd[0], "w");
579 if (! out)
580 error (EXIT_FAILURE, errno, "fdopen");
581
582 /* Output the definitions of all the muscles. */
583
584 /* There are no comments, especially not `#': we do want M4 expansion
585 after `#': think of CPP macros! */
586 fputs ("m4_changecom()\n", out);
587 fputs ("m4_init()\n", out);
588
589 user_actions_output (out);
590 merger_output (out);
591 token_definitions_output (out);
592 symbol_destructors_output (out);
593 symbol_printers_output (out);
594
595 muscles_m4_output (out);
596
597 fputs ("m4_wrap([m4_divert_pop(0)])\n", out);
598 fputs ("m4_divert_push(0)dnl\n", out);
599 if (ferror (out))
600 error (EXIT_FAILURE, 0, "pipe output error");
601 xfclose (out);
602
603 /* Read and process m4's output. */
604 timevar_push (TV_M4);
605 in = fdopen (filter_fd[1], "r");
606 if (! in)
607 error (EXIT_FAILURE, errno, "fdopen");
608 scan_skel (in);
609 if (ferror (in))
610 error (EXIT_FAILURE, 0, "pipe input error");
611 xfclose (in);
612 reap_subpipe (pid, m4);
613 timevar_pop (TV_M4);
614 }
615
616 static void
617 prepare (void)
618 {
619 /* Flags. */
620 MUSCLE_INSERT_INT ("locations_flag", locations_flag);
621 MUSCLE_INSERT_INT ("defines_flag", defines_flag);
622 MUSCLE_INSERT_INT ("error_verbose", error_verbose);
623 MUSCLE_INSERT_INT ("pure", pure_parser);
624 MUSCLE_INSERT_INT ("debug", debug_flag);
625
626 /* FIXME: This is wrong: the muscles should decide whether they hold
627 a copy or not, but the situation is too obscure currently. */
628 MUSCLE_INSERT_STRING ("prefix", spec_name_prefix ? spec_name_prefix : "yy");
629 MUSCLE_INSERT_STRING ("output_infix", output_infix ? output_infix : "");
630 MUSCLE_INSERT_STRING ("output_prefix", short_base_name);
631 MUSCLE_INSERT_STRING ("output_parser_name", parser_file_name);
632 MUSCLE_INSERT_STRING ("output_header_name", spec_defines_file);
633
634 /* Symbols. */
635 MUSCLE_INSERT_INT ("tokens_number", ntokens);
636 MUSCLE_INSERT_INT ("nterms_number", nvars);
637 MUSCLE_INSERT_INT ("undef_token_number", undeftoken->number);
638 MUSCLE_INSERT_INT ("user_token_number_max", max_user_token_number);
639
640 /* Rules. */
641 MUSCLE_INSERT_INT ("rules_number", nrules);
642
643 /* States. */
644 MUSCLE_INSERT_INT ("last", high);
645 MUSCLE_INSERT_INT ("final_state_number", final_state->number);
646 MUSCLE_INSERT_INT ("states_number", nstates);
647
648 /* User Code. */
649 obstack_1grow (&pre_prologue_obstack, 0);
650 obstack_1grow (&post_prologue_obstack, 0);
651 muscle_insert ("pre_prologue", obstack_finish (&pre_prologue_obstack));
652 muscle_insert ("post_prologue", obstack_finish (&post_prologue_obstack));
653
654 /* Find the right skeleton file. */
655 if (!skeleton)
656 {
657 if (glr_parser)
658 skeleton = "glr.c";
659 else
660 skeleton = "yacc.c";
661 }
662
663 /* Parse the skeleton file and output the needed parsers. */
664 muscle_insert ("skeleton", xstrdup (skeleton));
665 }
666
667
668 /*----------------------------------------------------------.
669 | Output the parsing tables and the parser code to ftable. |
670 `----------------------------------------------------------*/
671
672 void
673 output (void)
674 {
675 obstack_init (&format_obstack);
676
677 prepare_tokens ();
678 prepare_rules ();
679 prepare_states ();
680 prepare_actions ();
681
682 prepare ();
683
684 /* Process the selected skeleton file. */
685 output_skeleton ();
686
687 obstack_free (&format_obstack, NULL);
688 obstack_free (&pre_prologue_obstack, NULL);
689 obstack_free (&post_prologue_obstack, NULL);
690 }