]> git.saurik.com Git - bison.git/blob - src/output.c
* data/yacc.c: Guard the declaration of yytoknum also with
[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 "getargs.h"
92 #include "files.h"
93 #include "gram.h"
94 #include "complain.h"
95 #include "output.h"
96 #include "reader.h"
97 #include "symtab.h"
98 #include "tables.h"
99 #include "muscle_tab.h"
100
101 /* From src/scan-skel.l. */
102 void m4_invoke PARAMS ((const char *definitions));
103
104
105 static struct obstack format_obstack;
106
107 int error_verbose = 0;
108
109
110
111 /*-------------------------------------------------------------------.
112 | Create a function NAME which associates to the muscle NAME the |
113 | result of formatting the FIRST and then TABLE_DATA[BEGIN..END[ (of |
114 | TYPE), and to the muscle NAME_max, the max value of the |
115 | TABLE_DATA. |
116 `-------------------------------------------------------------------*/
117
118
119 #define GENERATE_MUSCLE_INSERT_TABLE(Name, Type) \
120 \
121 static void \
122 Name (const char *name, \
123 Type *table_data, \
124 Type first, \
125 int begin, \
126 int end) \
127 { \
128 Type min = first; \
129 Type max = first; \
130 int i; \
131 int j = 1; \
132 \
133 obstack_fgrow1 (&format_obstack, "%6d", first); \
134 for (i = begin; i < end; ++i) \
135 { \
136 obstack_1grow (&format_obstack, ','); \
137 if (j >= 10) \
138 { \
139 obstack_sgrow (&format_obstack, "\n "); \
140 j = 1; \
141 } \
142 else \
143 ++j; \
144 obstack_fgrow1 (&format_obstack, "%6d", table_data[i]); \
145 if (table_data[i] < min) \
146 min = table_data[i]; \
147 if (max < table_data[i]) \
148 max = table_data[i]; \
149 } \
150 obstack_1grow (&format_obstack, 0); \
151 muscle_insert (name, obstack_finish (&format_obstack)); \
152 \
153 /* Build `NAME_min' and `NAME_max' in the obstack. */ \
154 obstack_fgrow1 (&format_obstack, "%s_min", name); \
155 obstack_1grow (&format_obstack, 0); \
156 MUSCLE_INSERT_LONG_INT (obstack_finish (&format_obstack), \
157 (long int) min); \
158 obstack_fgrow1 (&format_obstack, "%s_max", name); \
159 obstack_1grow (&format_obstack, 0); \
160 MUSCLE_INSERT_LONG_INT (obstack_finish (&format_obstack), \
161 (long int) max); \
162 }
163
164 GENERATE_MUSCLE_INSERT_TABLE(muscle_insert_unsigned_int_table, unsigned int)
165 GENERATE_MUSCLE_INSERT_TABLE(muscle_insert_int_table, int)
166 GENERATE_MUSCLE_INSERT_TABLE(muscle_insert_short_table, short)
167 GENERATE_MUSCLE_INSERT_TABLE(muscle_insert_base_table, base_t)
168 GENERATE_MUSCLE_INSERT_TABLE(muscle_insert_rule_number_table, rule_number_t)
169 GENERATE_MUSCLE_INSERT_TABLE(muscle_insert_symbol_number_table, symbol_number_t)
170 GENERATE_MUSCLE_INSERT_TABLE(muscle_insert_item_number_table, item_number_t)
171 GENERATE_MUSCLE_INSERT_TABLE(muscle_insert_state_number_table, state_number_t)
172
173
174 /*-----------------------------------------------------------------.
175 | Prepare the muscles related to the tokens: translate, tname, and |
176 | toknum. |
177 `-----------------------------------------------------------------*/
178
179 static void
180 prepare_tokens (void)
181 {
182 muscle_insert_symbol_number_table ("translate",
183 token_translations,
184 token_translations[0],
185 1, max_user_token_number + 1);
186
187 {
188 int i;
189 int j = 0;
190 for (i = 0; i < nsyms; i++)
191 {
192 /* Be sure not to use twice the same QUOTEARG slot:
193 SYMBOL_TAG_GET uses slot 0. */
194 const char *cp =
195 quotearg_n_style (1, c_quoting_style,
196 symbols[i]->tag);
197 /* Width of the next token, including the two quotes, the coma
198 and the space. */
199 int strsize = strlen (cp) + 2;
200
201 if (j + strsize > 75)
202 {
203 obstack_sgrow (&format_obstack, "\n ");
204 j = 2;
205 }
206
207 obstack_sgrow (&format_obstack, cp);
208 obstack_sgrow (&format_obstack, ", ");
209 j += strsize;
210 }
211 /* Add a NULL entry to list of tokens (well, 0, as NULL might not be
212 defined). */
213 obstack_sgrow (&format_obstack, "0");
214
215 /* Finish table and store. */
216 obstack_1grow (&format_obstack, 0);
217 muscle_insert ("tname", obstack_finish (&format_obstack));
218 }
219
220 /* Output YYTOKNUM. */
221 {
222 int i;
223 int *values = XCALLOC (int, ntokens);
224 for (i = 0; i < ntokens; ++i)
225 values[i] = symbols[i]->user_token_number;
226 muscle_insert_int_table ("toknum", values,
227 values[0], 1, ntokens);
228 free (values);
229 }
230 }
231
232
233 /*-------------------------------------------------------------.
234 | Prepare the muscles related to the rules: rhs, prhs, r1, r2, |
235 | rline, dprec, merger |
236 `-------------------------------------------------------------*/
237
238 static void
239 prepare_rules (void)
240 {
241 rule_number_t r;
242 unsigned int i = 0;
243 item_number_t *rhs = XMALLOC (item_number_t, nritems);
244 unsigned int *prhs = XMALLOC (unsigned int, nrules);
245 unsigned int *rline = XMALLOC (unsigned int, nrules);
246 symbol_number_t *r1 = XMALLOC (symbol_number_t, nrules);
247 unsigned int *r2 = XMALLOC (unsigned int, nrules);
248 short *dprec = XMALLOC (short, nrules);
249 short *merger = XMALLOC (short, nrules);
250
251 for (r = 0; r < nrules; ++r)
252 {
253 item_number_t *rhsp = NULL;
254 /* Index of rule R in RHS. */
255 prhs[r] = i;
256 /* RHS of the rule R. */
257 for (rhsp = rules[r].rhs; *rhsp >= 0; ++rhsp)
258 rhs[i++] = *rhsp;
259 /* LHS of the rule R. */
260 r1[r] = rules[r].lhs->number;
261 /* Length of rule R's RHS. */
262 r2[r] = i - prhs[r];
263 /* Separator in RHS. */
264 rhs[i++] = -1;
265 /* Line where rule was defined. */
266 rline[r] = rules[r].location.first_line;
267 /* Dynamic precedence (GLR) */
268 dprec[r] = rules[r].dprec;
269 /* Merger-function index (GLR) */
270 merger[r] = rules[r].merger;
271 }
272 assert (i == nritems);
273
274 muscle_insert_item_number_table ("rhs", rhs, ritem[0], 1, nritems);
275 muscle_insert_unsigned_int_table ("prhs", prhs, 0, 0, nrules);
276 muscle_insert_unsigned_int_table ("rline", rline, 0, 0, nrules);
277 muscle_insert_symbol_number_table ("r1", r1, 0, 0, nrules);
278 muscle_insert_unsigned_int_table ("r2", r2, 0, 0, nrules);
279 muscle_insert_short_table ("dprec", dprec, 0, 0, nrules);
280 muscle_insert_short_table ("merger", merger, 0, 0, nrules);
281
282 free (rhs);
283 free (prhs);
284 free (rline);
285 free (r1);
286 free (r2);
287 free (dprec);
288 free (merger);
289 }
290
291 /*--------------------------------------------.
292 | Prepare the muscles related to the states. |
293 `--------------------------------------------*/
294
295 static void
296 prepare_states (void)
297 {
298 state_number_t i;
299 symbol_number_t *values =
300 (symbol_number_t *) alloca (sizeof (symbol_number_t) * nstates);
301 for (i = 0; i < nstates; ++i)
302 values[i] = states[i]->accessing_symbol;
303 muscle_insert_symbol_number_table ("stos", values,
304 0, 1, nstates);
305 }
306
307
308
309 /*----------------------------------.
310 | Output the user actions to OOUT. |
311 `----------------------------------*/
312
313 static void
314 user_actions_output (FILE *out)
315 {
316 rule_number_t r;
317
318 fputs ("m4_define([b4_actions], \n[[", out);
319 for (r = 0; r < nrules; ++r)
320 if (rules[r].action)
321 {
322 fprintf (out, " case %d:\n", r + 1);
323
324 if (!no_lines_flag)
325 fprintf (out, muscle_find ("linef"),
326 rules[r].action_location.first_line,
327 quotearg_style (c_quoting_style,
328 muscle_find ("filename")));
329 fprintf (out, " %s\n break;\n\n",
330 rules[r].action);
331 }
332 fputs ("]])\n\n", out);
333 }
334
335 /*--------------------------------------.
336 | Output the merge functions to OUT. |
337 `--------------------------------------*/
338
339 static void
340 merger_output (FILE *out)
341 {
342 int n;
343 merger_list* p;
344
345 fputs ("m4_define([b4_mergers], \n[[", out);
346 for (n = 1, p = merge_functions; p != NULL; n += 1, p = p->next)
347 {
348 if (p->type[0] == '\0')
349 fprintf (out, " case %d: yyval = %s (*yy0, *yy1); break;\n",
350 n, p->name);
351 else
352 fprintf (out, " case %d: yyval.%s = %s (*yy0, *yy1); break;\n",
353 n, p->type, p->name);
354 }
355 fputs ("]])\n\n", out);
356 }
357
358 /*---------------------------------------.
359 | Output the tokens definition to OOUT. |
360 `---------------------------------------*/
361
362 static void
363 token_definitions_output (FILE *out)
364 {
365 int i;
366 int first = 1;
367
368 fputs ("m4_define([b4_tokens], \n[", out);
369 for (i = 0; i < ntokens; ++i)
370 {
371 symbol_t *symbol = symbols[i];
372 int number = symbol->user_token_number;
373
374 /* At this stage, if there are literal aliases, they are part of
375 SYMBOLS, so we should not find symbols which are the aliases
376 here. */
377 assert (number != USER_NUMBER_ALIAS);
378
379 /* Skip error token. */
380 if (symbol == errtoken)
381 continue;
382
383 /* If this string has an alias, then it is necessarily the alias
384 which is to be output. */
385 if (symbol->alias)
386 symbol = symbol->alias;
387
388 /* Don't output literal chars or strings (when defined only as a
389 string). Note that must be done after the alias resolution:
390 think about `%token 'f' "f"'. */
391 if (symbol->tag[0] == '\'' || symbol->tag[0] == '\"')
392 continue;
393
394 /* Don't #define nonliteral tokens whose names contain periods
395 or '$' (as does the default value of the EOF token). */
396 if (strchr (symbol->tag, '.') || strchr (symbol->tag, '$'))
397 continue;
398
399 fprintf (out, "%s[[[%s]], [%d]]",
400 first ? "" : ",\n", symbol->tag, number);
401
402 first = 0;
403 }
404 fputs ("])\n\n", out);
405 }
406
407
408 /*----------------------------------------.
409 | Output the symbol destructors to OOUT. |
410 `----------------------------------------*/
411
412 static void
413 symbol_destructors_output (FILE *out)
414 {
415 int i;
416 int first = 1;
417
418 fputs ("m4_define([b4_symbol_destructors], \n[", out);
419 for (i = 0; i < nsyms; ++i)
420 if (symbols[i]->destructor)
421 {
422 symbol_t *symbol = symbols[i];
423
424 /* Filename, lineno,
425 Symbol-name, Symbol-number,
426 destructor, typename. */
427 fprintf (out, "%s[[[%s]], [[%d]], [[%s]], [[%d]], [[%s]], [[%s]]]",
428 first ? "" : ",\n",
429 infile, symbol->destructor_location.first_line,
430 symbol->tag,
431 symbol->number,
432 symbol->destructor,
433 symbol->type_name);
434
435 first = 0;
436 }
437 fputs ("])\n\n", out);
438 }
439
440
441 /*-------------------------------------.
442 | Output the symbol printers to OOUT. |
443 `-------------------------------------*/
444
445 static void
446 symbol_printers_output (FILE *out)
447 {
448 int i;
449 int first = 1;
450
451 fputs ("m4_define([b4_symbol_printers], \n[", out);
452 for (i = 0; i < nsyms; ++i)
453 if (symbols[i]->destructor)
454 {
455 symbol_t *symbol = symbols[i];
456
457 /* Filename, lineno,
458 Symbol-name, Symbol-number,
459 destructor, typename. */
460 fprintf (out, "%s[[[%s]], [[%d]], [[%s]], [[%d]], [[%s]], [[%s]]]",
461 first ? "" : ",\n",
462 infile, symbol->printer_location.first_line,
463 symbol->tag,
464 symbol->number,
465 symbol->printer,
466 symbol->type_name);
467
468 first = 0;
469 }
470 fputs ("])\n\n", out);
471 }
472
473
474 static void
475 prepare_actions (void)
476 {
477 /* Figure out the actions for the specified state, indexed by
478 lookahead token type. */
479
480 muscle_insert_rule_number_table ("defact", yydefact,
481 yydefact[0], 1, nstates);
482
483 /* Figure out what to do after reducing with each rule, depending on
484 the saved state from before the beginning of parsing the data
485 that matched this rule. */
486 muscle_insert_state_number_table ("defgoto", yydefgoto,
487 yydefgoto[0], 1, nsyms - ntokens);
488
489
490 /* Output PACT. */
491 muscle_insert_base_table ("pact", base,
492 base[0], 1, nstates);
493 MUSCLE_INSERT_INT ("pact_ninf", base_ninf);
494
495 /* Output PGOTO. */
496 muscle_insert_base_table ("pgoto", base,
497 base[nstates], nstates + 1, nvectors);
498
499 muscle_insert_base_table ("table", table,
500 table[0], 1, high + 1);
501 MUSCLE_INSERT_INT ("table_ninf", table_ninf);
502
503 muscle_insert_base_table ("check", check,
504 check[0], 1, high + 1);
505
506 if (glr_parser)
507 {
508 /* GLR parsing slightly modifies yytable and yycheck
509 (and thus yypact) so that in states with unresolved conflicts,
510 the default reduction is not used in the conflicted entries, so
511 that there is a place to put a conflict pointer. This means that
512 yyconflp and yyconfl are nonsense for a non-GLR parser, so we
513 avoid accidents by not writing them out in that case. */
514 muscle_insert_unsigned_int_table ("conflict_list_heads", conflict_table,
515 conflict_table[0], 1, high+1);
516 muscle_insert_unsigned_int_table ("conflicting_rules", conflict_list,
517 conflict_list[0], 1, conflict_list_cnt);
518 }
519 }
520
521 \f
522 /*---------------------------.
523 | Call the skeleton parser. |
524 `---------------------------*/
525
526 static void
527 output_skeleton (void)
528 {
529 /* Store the definition of all the muscles. */
530 const char *tempdir = getenv ("TMPDIR");
531 char *tempfile = NULL;
532 FILE *out = NULL;
533 int fd;
534
535 if (tempdir == NULL)
536 tempdir = DEFAULT_TMPDIR;
537 tempfile = xmalloc (strlen (tempdir) + 11);
538 sprintf (tempfile, "%s/bsnXXXXXX", tempdir);
539 fd = mkstemp (tempfile);
540 if (fd == -1)
541 error (EXIT_FAILURE, errno, "%s", tempfile);
542
543 out = fdopen (fd, "w");
544 if (out == NULL)
545 error (EXIT_FAILURE, errno, "%s", tempfile);
546
547 /* There are no comments, especially not `#': we do want M4 expansion
548 after `#': think of CPP macros! */
549 fputs ("m4_changecom()\n", out);
550 fputs ("m4_init()\n", out);
551
552 user_actions_output (out);
553 merger_output (out);
554 token_definitions_output (out);
555 symbol_destructors_output (out);
556 symbol_printers_output (out);
557
558 muscles_m4_output (out);
559
560 fputs ("m4_wrap([m4_divert_pop(0)])\n", out);
561 fputs ("m4_divert_push(0)dnl\n", out);
562 xfclose (out);
563
564 timevar_push (TV_M4);
565 m4_invoke (tempfile);
566 timevar_pop (TV_M4);
567
568 /* If `debugging', keep this file alive. */
569 if (!(trace_flag & trace_tools))
570 unlink (tempfile);
571
572 free (tempfile);
573 }
574
575 static void
576 prepare (void)
577 {
578 /* Flags. */
579 MUSCLE_INSERT_INT ("locations_flag", locations_flag);
580 MUSCLE_INSERT_INT ("defines_flag", defines_flag);
581 MUSCLE_INSERT_INT ("error_verbose", error_verbose);
582 MUSCLE_INSERT_INT ("pure", pure_parser);
583 MUSCLE_INSERT_INT ("debug", debug_flag);
584
585 /* FIXME: This is wrong: the muscles should decide whether they hold
586 a copy or not, but the situation is too obscure currently. */
587 MUSCLE_INSERT_STRING ("prefix", spec_name_prefix ? spec_name_prefix : "yy");
588 MUSCLE_INSERT_STRING ("output_infix", output_infix ? output_infix : "");
589 MUSCLE_INSERT_STRING ("output_prefix", short_base_name);
590 MUSCLE_INSERT_STRING ("output_parser_name", parser_file_name);
591 MUSCLE_INSERT_STRING ("output_header_name", spec_defines_file);
592
593 /* Symbols. */
594 MUSCLE_INSERT_INT ("tokens_number", ntokens);
595 MUSCLE_INSERT_INT ("nterms_number", nvars);
596 MUSCLE_INSERT_INT ("undef_token_number", undeftoken->number);
597 MUSCLE_INSERT_INT ("user_token_number_max", max_user_token_number);
598
599 /* Rules. */
600 MUSCLE_INSERT_INT ("rules_number", nrules);
601
602 /* States. */
603 MUSCLE_INSERT_INT ("last", high);
604 MUSCLE_INSERT_INT ("final_state_number", final_state->number);
605 MUSCLE_INSERT_INT ("states_number", nstates);
606
607 /* User Code. */
608 obstack_1grow (&pre_prologue_obstack, 0);
609 obstack_1grow (&post_prologue_obstack, 0);
610 muscle_insert ("pre_prologue", obstack_finish (&pre_prologue_obstack));
611 muscle_insert ("post_prologue", obstack_finish (&post_prologue_obstack));
612
613 /* Find the right skeleton file. */
614 if (!skeleton)
615 {
616 if (glr_parser)
617 skeleton = "glr.c";
618 else
619 skeleton = "yacc.c";
620 }
621
622 /* Parse the skeleton file and output the needed parsers. */
623 muscle_insert ("skeleton", skeleton);
624 }
625
626
627 /*----------------------------------------------------------.
628 | Output the parsing tables and the parser code to ftable. |
629 `----------------------------------------------------------*/
630
631 void
632 output (void)
633 {
634 obstack_init (&format_obstack);
635
636 prepare_tokens ();
637 prepare_rules ();
638 prepare_states ();
639 prepare_actions ();
640
641 prepare ();
642
643 /* Process the selected skeleton file. */
644 output_skeleton ();
645
646 obstack_free (&format_obstack, NULL);
647 obstack_free (&pre_prologue_obstack, NULL);
648 obstack_free (&post_prologue_obstack, NULL);
649 }