1 /* Output the generated parsing program for bison,
2 Copyright (C) 1984, 1986, 1989, 1992, 2000, 2001, 2002
3 Free Software Foundation, Inc.
5 This file is part of Bison, the GNU Compiler Compiler.
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)
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.
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
23 /* The parser tables consist of these tables.
25 YYTRANSLATE = vector mapping yylex's token numbers into bison's
28 YYTNAME = vector of string-names indexed by bison token number.
30 YYTOKNUM = vector of yylex token numbers corresponding to entries
33 YYRLINE = vector of line-numbers of all rules. For yydebug
36 YYRHS = vector of items of all rules. This is exactly what RITEMS
37 contains. For yydebug and for semantic parser.
39 YYPRHS[R] = index in YYRHS of first item for rule R.
41 YYR1[R] = symbol number of symbol that rule R derives.
43 YYR2[R] = number of symbols composing right hand side of rule R.
45 YYSTOS[S] = the symbol number of the symbol that leads to state S.
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
51 YYDEFGOTO[I] = default state to go to after a reduction of a rule
52 that generates variable NTOKENS + I, except when YYTABLE specifies
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
59 If the value in YYTABLE is positive, we shift the token and go to
62 If the value is negative, it is minus a rule number to reduce by.
64 If the value is zero, the default action from YYDEFACT[S] is used.
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.
72 YYTABLE = a vector filled with portions for different uses, found
73 via YYPACT and YYPGOTO.
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
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.
85 YYFINAL = the state number of the termination state. YYFLAG = most
86 negative short int. Used to flag ?? */
100 #include "muscle_tab.h"
102 /* From src/scan-skel.l. */
103 void scan_skel (FILE *);
106 static struct obstack format_obstack
;
108 int error_verbose
= 0;
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 |
117 `-------------------------------------------------------------------*/
120 #define GENERATE_MUSCLE_INSERT_TABLE(Name, Type) \
123 Name (const char *name, \
134 obstack_fgrow1 (&format_obstack, "%6d", first); \
135 for (i = begin; i < end; ++i) \
137 obstack_1grow (&format_obstack, ','); \
140 obstack_sgrow (&format_obstack, "\n "); \
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]; \
151 obstack_1grow (&format_obstack, 0); \
152 muscle_insert (name, obstack_finish (&format_obstack)); \
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), \
159 obstack_fgrow1 (&format_obstack, "%s_max", name); \
160 obstack_1grow (&format_obstack, 0); \
161 MUSCLE_INSERT_LONG_INT (obstack_finish (&format_obstack), \
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
)
175 /*-----------------------------------------------------------------.
176 | Prepare the muscles related to the tokens: translate, tname, and |
178 `-----------------------------------------------------------------*/
181 prepare_tokens (void)
183 muscle_insert_symbol_number_table ("translate",
185 token_translations
[0],
186 1, max_user_token_number
+ 1);
191 for (i
= 0; i
< nsyms
; i
++)
193 /* Be sure not to use twice the same QUOTEARG slot:
194 SYMBOL_TAG_GET uses slot 0. */
196 quotearg_n_style (1, c_quoting_style
,
198 /* Width of the next token, including the two quotes, the coma
200 int strsize
= strlen (cp
) + 2;
202 if (j
+ strsize
> 75)
204 obstack_sgrow (&format_obstack
, "\n ");
208 obstack_sgrow (&format_obstack
, cp
);
209 obstack_sgrow (&format_obstack
, ", ");
212 /* Add a NULL entry to list of tokens (well, 0, as NULL might not be
214 obstack_sgrow (&format_obstack
, "0");
216 /* Finish table and store. */
217 obstack_1grow (&format_obstack
, 0);
218 muscle_insert ("tname", obstack_finish (&format_obstack
));
221 /* Output YYTOKNUM. */
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
);
234 /*-------------------------------------------------------------.
235 | Prepare the muscles related to the rules: rhs, prhs, r1, r2, |
236 | rline, dprec, merger |
237 `-------------------------------------------------------------*/
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
);
252 for (r
= 0; r
< nrules
; ++r
)
254 item_number_t
*rhsp
= NULL
;
255 /* Index of rule R in RHS. */
257 /* RHS of the rule R. */
258 for (rhsp
= rules
[r
].rhs
; *rhsp
>= 0; ++rhsp
)
260 /* LHS of the rule R. */
261 r1
[r
] = rules
[r
].lhs
->number
;
262 /* Length of rule R's RHS. */
264 /* Separator in RHS. */
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
;
273 assert (i
== nritems
);
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
);
292 /*--------------------------------------------.
293 | Prepare the muscles related to the states. |
294 `--------------------------------------------*/
297 prepare_states (void)
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
,
310 /*---------------------------------.
311 | Output the user actions to OUT. |
312 `---------------------------------*/
315 user_actions_output (FILE *out
)
319 fputs ("m4_define([b4_actions], \n[[", out
);
320 for (r
= 0; r
< nrules
; ++r
)
323 fprintf (out
, " case %d:\n", r
+ 1);
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",
333 fputs ("]])\n\n", out
);
336 /*--------------------------------------.
337 | Output the merge functions to OUT. |
338 `--------------------------------------*/
341 merger_output (FILE *out
)
346 fputs ("m4_define([b4_mergers], \n[[", out
);
347 for (n
= 1, p
= merge_functions
; p
!= NULL
; n
+= 1, p
= p
->next
)
349 if (p
->type
[0] == '\0')
350 fprintf (out
, " case %d: yyval = %s (*yy0, *yy1); break;\n",
353 fprintf (out
, " case %d: yyval.%s = %s (*yy0, *yy1); break;\n",
354 n
, p
->type
, p
->name
);
356 fputs ("]])\n\n", out
);
359 /*--------------------------------------.
360 | Output the tokens definition to OUT. |
361 `--------------------------------------*/
364 token_definitions_output (FILE *out
)
369 fputs ("m4_define([b4_tokens], \n[", out
);
370 for (i
= 0; i
< ntokens
; ++i
)
372 symbol_t
*symbol
= symbols
[i
];
373 int number
= symbol
->user_token_number
;
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
378 assert (number
!= USER_NUMBER_ALIAS
);
380 /* Skip error token. */
381 if (symbol
== errtoken
)
384 /* If this string has an alias, then it is necessarily the alias
385 which is to be output. */
387 symbol
= symbol
->alias
;
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] == '\"')
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
, '$'))
400 fprintf (out
, "%s[[[%s]], [%d]]",
401 first
? "" : ",\n", symbol
->tag
, number
);
405 fputs ("])\n\n", out
);
409 /*---------------------------------------.
410 | Output the symbol destructors to OUT. |
411 `---------------------------------------*/
414 symbol_destructors_output (FILE *out
)
419 fputs ("m4_define([b4_symbol_destructors], \n[", out
);
420 for (i
= 0; i
< nsyms
; ++i
)
421 if (symbols
[i
]->destructor
)
423 symbol_t
*symbol
= symbols
[i
];
426 Symbol-name, Symbol-number,
427 destructor, typename. */
428 fprintf (out
, "%s[[[%s]], [[%d]], [[%s]], [[%d]], [[%s]], [[%s]]]",
430 infile
, symbol
->destructor_location
.first_line
,
438 fputs ("])\n\n", out
);
442 /*------------------------------------.
443 | Output the symbol printers to OUT. |
444 `------------------------------------*/
447 symbol_printers_output (FILE *out
)
452 fputs ("m4_define([b4_symbol_printers], \n[", out
);
453 for (i
= 0; i
< nsyms
; ++i
)
454 if (symbols
[i
]->destructor
)
456 symbol_t
*symbol
= symbols
[i
];
459 Symbol-name, Symbol-number,
460 printer, typename. */
461 fprintf (out
, "%s[[[%s]], [[%d]], [[%s]], [[%d]], [[%s]], [[%s]]]",
463 infile
, symbol
->printer_location
.first_line
,
471 fputs ("])\n\n", out
);
476 prepare_actions (void)
478 /* Figure out the actions for the specified state, indexed by
479 lookahead token type. */
481 muscle_insert_rule_number_table ("defact", yydefact
,
482 yydefact
[0], 1, nstates
);
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
);
492 muscle_insert_base_table ("pact", base
,
493 base
[0], 1, nstates
);
494 MUSCLE_INSERT_INT ("pact_ninf", base_ninf
);
497 muscle_insert_base_table ("pgoto", base
,
498 base
[nstates
], nstates
+ 1, nvectors
);
500 muscle_insert_base_table ("table", table
,
501 table
[0], 1, high
+ 1);
502 MUSCLE_INSERT_INT ("table_ninf", table_ninf
);
504 muscle_insert_base_table ("check", check
,
505 check
[0], 1, high
+ 1);
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.
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
);
523 /*---------------------------.
524 | Call the skeleton parser. |
525 `---------------------------*/
528 output_skeleton (void)
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";
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] == '/')
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
);
560 /* Create an m4 subprocess connected to us via two pipes. */
562 if (trace_flag
& trace_tools
)
563 fprintf (stderr
, "running: %s -I %s %s - %s\n",
564 m4
, pkgdatadir
, m4sugar
, full_path
);
568 argv
[2] = pkgdatadir
;
575 pid
= create_subpipe (argv
, filter_fd
);
578 out
= fdopen (filter_fd
[0], "w");
580 error (EXIT_FAILURE
, errno
, "fdopen");
582 /* Output the definitions of all the muscles. */
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
);
589 user_actions_output (out
);
591 token_definitions_output (out
);
592 symbol_destructors_output (out
);
593 symbol_printers_output (out
);
595 muscles_m4_output (out
);
597 fputs ("m4_wrap([m4_divert_pop(0)])\n", out
);
598 fputs ("m4_divert_push(0)dnl\n", out
);
600 error (EXIT_FAILURE
, 0, "pipe output error");
603 /* Read and process m4's output. */
604 timevar_push (TV_M4
);
605 in
= fdopen (filter_fd
[1], "r");
607 error (EXIT_FAILURE
, errno
, "fdopen");
610 error (EXIT_FAILURE
, 0, "pipe input error");
612 reap_subpipe (pid
, m4
);
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
);
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
);
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
);
641 MUSCLE_INSERT_INT ("rules_number", nrules
);
644 MUSCLE_INSERT_INT ("last", high
);
645 MUSCLE_INSERT_INT ("final_state_number", final_state
->number
);
646 MUSCLE_INSERT_INT ("states_number", nstates
);
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
));
654 /* Find the right skeleton file. */
663 /* Parse the skeleton file and output the needed parsers. */
664 MUSCLE_INSERT_STRING ("skeleton", skeleton
);
668 /*----------------------------------------------------------.
669 | Output the parsing tables and the parser code to ftable. |
670 `----------------------------------------------------------*/
675 obstack_init (&format_obstack
);
684 /* Process the selected skeleton file. */
687 obstack_free (&format_obstack
, NULL
);
688 obstack_free (&pre_prologue_obstack
, NULL
);
689 obstack_free (&post_prologue_obstack
, NULL
);