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 ?? */
99 #include "muscle_tab.h"
101 /* From src/scan-skel.l. */
102 void m4_invoke
PARAMS ((const char *definitions
));
105 static struct obstack format_obstack
;
107 int error_verbose
= 0;
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 |
116 `-------------------------------------------------------------------*/
119 #define GENERATE_MUSCLE_INSERT_TABLE(Name, Type) \
122 Name (const char *name, \
133 obstack_fgrow1 (&format_obstack, "%6d", first); \
134 for (i = begin; i < end; ++i) \
136 obstack_1grow (&format_obstack, ','); \
139 obstack_sgrow (&format_obstack, "\n "); \
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]; \
150 obstack_1grow (&format_obstack, 0); \
151 muscle_insert (name, obstack_finish (&format_obstack)); \
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), \
158 obstack_fgrow1 (&format_obstack, "%s_max", name); \
159 obstack_1grow (&format_obstack, 0); \
160 MUSCLE_INSERT_LONG_INT (obstack_finish (&format_obstack), \
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
)
174 /*-----------------------------------------------------------------.
175 | Prepare the muscles related to the tokens: translate, tname, and |
177 `-----------------------------------------------------------------*/
180 prepare_tokens (void)
182 muscle_insert_symbol_number_table ("translate",
184 token_translations
[0],
185 1, max_user_token_number
+ 1);
190 for (i
= 0; i
< nsyms
; i
++)
192 /* Be sure not to use twice the same QUOTEARG slot:
193 SYMBOL_TAG_GET uses slot 0. */
195 quotearg_n_style (1, c_quoting_style
,
197 /* Width of the next token, including the two quotes, the coma
199 int strsize
= strlen (cp
) + 2;
201 if (j
+ strsize
> 75)
203 obstack_sgrow (&format_obstack
, "\n ");
207 obstack_sgrow (&format_obstack
, cp
);
208 obstack_sgrow (&format_obstack
, ", ");
211 /* Add a NULL entry to list of tokens (well, 0, as NULL might not be
213 obstack_sgrow (&format_obstack
, "0");
215 /* Finish table and store. */
216 obstack_1grow (&format_obstack
, 0);
217 muscle_insert ("tname", obstack_finish (&format_obstack
));
220 /* Output YYTOKNUM. */
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
);
233 /*-------------------------------------------------------------.
234 | Prepare the muscles related to the rules: rhs, prhs, r1, r2, |
235 | rline, dprec, merger |
236 `-------------------------------------------------------------*/
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
);
251 for (r
= 0; r
< nrules
; ++r
)
253 item_number_t
*rhsp
= NULL
;
254 /* Index of rule R in RHS. */
256 /* RHS of the rule R. */
257 for (rhsp
= rules
[r
].rhs
; *rhsp
>= 0; ++rhsp
)
259 /* LHS of the rule R. */
260 r1
[r
] = rules
[r
].lhs
->number
;
261 /* Length of rule R's RHS. */
263 /* Separator in RHS. */
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
;
272 assert (i
== nritems
);
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
);
291 /*--------------------------------------------.
292 | Prepare the muscles related to the states. |
293 `--------------------------------------------*/
296 prepare_states (void)
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
,
309 /*----------------------------------.
310 | Output the user actions to OOUT. |
311 `----------------------------------*/
314 user_actions_output (FILE *out
)
318 fputs ("m4_define([b4_actions], \n[[", out
);
319 for (r
= 0; r
< nrules
; ++r
)
322 fprintf (out
, " case %d:\n", r
+ 1);
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",
332 fputs ("]])\n\n", out
);
335 /*--------------------------------------.
336 | Output the merge functions to OUT. |
337 `--------------------------------------*/
340 merger_output (FILE *out
)
345 fputs ("m4_define([b4_mergers], \n[[", out
);
346 for (n
= 1, p
= merge_functions
; p
!= NULL
; n
+= 1, p
= p
->next
)
348 if (p
->type
[0] == '\0')
349 fprintf (out
, " case %d: yyval = %s (*yy0, *yy1); break;\n",
352 fprintf (out
, " case %d: yyval.%s = %s (*yy0, *yy1); break;\n",
353 n
, p
->type
, p
->name
);
355 fputs ("]])\n\n", out
);
358 /*---------------------------------------.
359 | Output the tokens definition to OOUT. |
360 `---------------------------------------*/
363 token_definitions_output (FILE *out
)
368 fputs ("m4_define([b4_tokens], \n[", out
);
369 for (i
= 0; i
< ntokens
; ++i
)
371 symbol_t
*symbol
= symbols
[i
];
372 int number
= symbol
->user_token_number
;
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
377 assert (number
!= USER_NUMBER_ALIAS
);
379 /* Skip error token. */
380 if (symbol
== errtoken
)
383 /* If this string has an alias, then it is necessarily the alias
384 which is to be output. */
386 symbol
= symbol
->alias
;
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] == '\"')
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
, '$'))
399 fprintf (out
, "%s[[[%s]], [%d]]",
400 first
? "" : ",\n", symbol
->tag
, number
);
404 fputs ("])\n\n", out
);
408 /*----------------------------------------.
409 | Output the symbol destructors to OOUT. |
410 `----------------------------------------*/
413 symbol_destructors_output (FILE *out
)
418 fputs ("m4_define([b4_symbol_destructors], \n[", out
);
419 for (i
= 0; i
< nsyms
; ++i
)
420 if (symbols
[i
]->destructor
)
422 symbol_t
*symbol
= symbols
[i
];
425 Symbol-name, Symbol-number,
426 destructor, typename. */
427 fprintf (out
, "%s[[[%s]], [[%d]], [[%s]], [[%d]], [[%s]], [[%s]]]",
429 infile
, symbol
->destructor_location
.first_line
,
437 fputs ("])\n\n", out
);
441 /*-------------------------------------.
442 | Output the symbol printers to OOUT. |
443 `-------------------------------------*/
446 symbol_printers_output (FILE *out
)
451 fputs ("m4_define([b4_symbol_printers], \n[", out
);
452 for (i
= 0; i
< nsyms
; ++i
)
453 if (symbols
[i
]->destructor
)
455 symbol_t
*symbol
= symbols
[i
];
458 Symbol-name, Symbol-number,
459 destructor, typename. */
460 fprintf (out
, "%s[[[%s]], [[%d]], [[%s]], [[%d]], [[%s]], [[%s]]]",
462 infile
, symbol
->printer_location
.first_line
,
470 fputs ("])\n\n", out
);
475 prepare_actions (void)
477 /* Figure out the actions for the specified state, indexed by
478 lookahead token type. */
480 muscle_insert_rule_number_table ("defact", yydefact
,
481 yydefact
[0], 1, nstates
);
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
);
491 muscle_insert_base_table ("pact", base
,
492 base
[0], 1, nstates
);
493 MUSCLE_INSERT_INT ("pact_ninf", base_ninf
);
496 muscle_insert_base_table ("pgoto", base
,
497 base
[nstates
], nstates
+ 1, nvectors
);
499 muscle_insert_base_table ("table", table
,
500 table
[0], 1, high
+ 1);
501 MUSCLE_INSERT_INT ("table_ninf", table_ninf
);
503 muscle_insert_base_table ("check", check
,
504 check
[0], 1, high
+ 1);
506 /* GLR parsing slightly modifies YYTABLE and YYCHECK (and thus
507 YYPACT) so that in states with unresolved conflicts, the default
508 reduction is not used in the conflicted entries, so that there is
509 a place to put a conflict pointer.
511 This means that YYCONFLP and YYCONFL are nonsense for a non-GLR
512 parser, so we could avoid accidents by not writing them out in
513 that case. Nevertheless, it seems even better to be able to use
514 the GLR skeletons even without the non-deterministic tables. */
515 muscle_insert_unsigned_int_table ("conflict_list_heads", conflict_table
,
516 conflict_table
[0], 1, high
+1);
517 muscle_insert_unsigned_int_table ("conflicting_rules", conflict_list
,
518 conflict_list
[0], 1, conflict_list_cnt
);
522 /*---------------------------.
523 | Call the skeleton parser. |
524 `---------------------------*/
527 output_skeleton (void)
529 /* Store the definition of all the muscles. */
530 const char *tempdir
= getenv ("TMPDIR");
531 char *tempfile
= NULL
;
536 tempdir
= DEFAULT_TMPDIR
;
537 tempfile
= xmalloc (strlen (tempdir
) + 11);
538 sprintf (tempfile
, "%s/bsnXXXXXX", tempdir
);
539 fd
= mkstemp (tempfile
);
541 error (EXIT_FAILURE
, errno
, "%s", tempfile
);
543 out
= fdopen (fd
, "w");
545 error (EXIT_FAILURE
, errno
, "%s", tempfile
);
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
);
552 user_actions_output (out
);
554 token_definitions_output (out
);
555 symbol_destructors_output (out
);
556 symbol_printers_output (out
);
558 muscles_m4_output (out
);
560 fputs ("m4_wrap([m4_divert_pop(0)])\n", out
);
561 fputs ("m4_divert_push(0)dnl\n", out
);
564 timevar_push (TV_M4
);
565 m4_invoke (tempfile
);
568 /* If `debugging', keep this file alive. */
569 if (!(trace_flag
& trace_tools
))
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
);
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
);
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
);
600 MUSCLE_INSERT_INT ("rules_number", nrules
);
603 MUSCLE_INSERT_INT ("last", high
);
604 MUSCLE_INSERT_INT ("final_state_number", final_state
->number
);
605 MUSCLE_INSERT_INT ("states_number", nstates
);
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
));
613 /* Find the right skeleton file. */
622 /* Parse the skeleton file and output the needed parsers. */
623 muscle_insert ("skeleton", skeleton
);
627 /*----------------------------------------------------------.
628 | Output the parsing tables and the parser code to ftable. |
629 `----------------------------------------------------------*/
634 obstack_init (&format_obstack
);
643 /* Process the selected skeleton file. */
646 obstack_free (&format_obstack
, NULL
);
647 obstack_free (&pre_prologue_obstack
, NULL
);
648 obstack_free (&post_prologue_obstack
, NULL
);