]>
Commit | Line | Data |
---|---|---|
a932883e | 1 | /* Output the generated parsing program for Bison. |
87ceef73 | 2 | |
e141f4d4 JD |
3 | Copyright (C) 1984, 1986, 1989, 1992, 2000-2010 Free Software |
4 | Foundation, Inc. | |
c3e23647 | 5 | |
9ee3c97b | 6 | This file is part of Bison, the GNU Compiler Compiler. |
c3e23647 | 7 | |
f16b0819 PE |
8 | This program is free software: you can redistribute it and/or modify |
9 | it under the terms of the GNU General Public License as published by | |
10 | the Free Software Foundation, either version 3 of the License, or | |
11 | (at your option) any later version. | |
c3e23647 | 12 | |
f16b0819 PE |
13 | This program is distributed in the hope that it will be useful, |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | GNU General Public License for more details. | |
c3e23647 | 17 | |
9ee3c97b | 18 | You should have received a copy of the GNU General Public License |
f16b0819 | 19 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
c3e23647 | 20 | |
2cec9080 | 21 | #include <config.h> |
c3e23647 | 22 | #include "system.h" |
21fd22d6 | 23 | |
3b2942e6 | 24 | #include <configmake.h> |
21fd22d6 PE |
25 | #include <error.h> |
26 | #include <get-errno.h> | |
f39ab286 | 27 | #include <pipe.h> |
21fd22d6 | 28 | #include <quotearg.h> |
21fd22d6 | 29 | #include <timevar.h> |
f39ab286 | 30 | #include <wait-process.h> |
21fd22d6 PE |
31 | |
32 | #include "complain.h" | |
c3e23647 | 33 | #include "files.h" |
21fd22d6 | 34 | #include "getargs.h" |
c3e23647 | 35 | #include "gram.h" |
00f5d575 | 36 | #include "muscle-tab.h" |
6c89f1c1 | 37 | #include "output.h" |
a70083a3 | 38 | #include "reader.h" |
e9071366 | 39 | #include "scan-code.h" /* max_left_semantic_context */ |
04098407 | 40 | #include "scan-skel.h" |
ad949da9 | 41 | #include "symtab.h" |
c6f1a33c | 42 | #include "tables.h" |
be2a1a68 | 43 | |
b0778bdd | 44 | # define ARRAY_CARDINALITY(Array) (sizeof (Array) / sizeof *(Array)) |
12b0043a | 45 | |
f87685c3 | 46 | static struct obstack format_obstack; |
c3e23647 | 47 | |
133c20e2 | 48 | |
5372019f AD |
49 | /*-------------------------------------------------------------------. |
50 | | Create a function NAME which associates to the muscle NAME the | | |
51 | | result of formatting the FIRST and then TABLE_DATA[BEGIN..END[ (of | | |
52 | | TYPE), and to the muscle NAME_max, the max value of the | | |
53 | | TABLE_DATA. | | |
54 | `-------------------------------------------------------------------*/ | |
62a3e4f0 | 55 | |
62a3e4f0 | 56 | |
5372019f | 57 | #define GENERATE_MUSCLE_INSERT_TABLE(Name, Type) \ |
5fbb0954 | 58 | \ |
5372019f | 59 | static void \ |
9e0876fb | 60 | Name (char const *name, \ |
5fbb0954 AD |
61 | Type *table_data, \ |
62 | Type first, \ | |
63 | int begin, \ | |
64 | int end) \ | |
65 | { \ | |
12b0043a | 66 | Type min = first; \ |
0c2d3f4c | 67 | Type max = first; \ |
87ceef73 PE |
68 | long int lmin; \ |
69 | long int lmax; \ | |
5fbb0954 AD |
70 | int i; \ |
71 | int j = 1; \ | |
72 | \ | |
5372019f | 73 | obstack_fgrow1 (&format_obstack, "%6d", first); \ |
5fbb0954 AD |
74 | for (i = begin; i < end; ++i) \ |
75 | { \ | |
5372019f | 76 | obstack_1grow (&format_obstack, ','); \ |
5fbb0954 AD |
77 | if (j >= 10) \ |
78 | { \ | |
5372019f | 79 | obstack_sgrow (&format_obstack, "\n "); \ |
5fbb0954 AD |
80 | j = 1; \ |
81 | } \ | |
82 | else \ | |
83 | ++j; \ | |
5372019f | 84 | obstack_fgrow1 (&format_obstack, "%6d", table_data[i]); \ |
12b0043a AD |
85 | if (table_data[i] < min) \ |
86 | min = table_data[i]; \ | |
87 | if (max < table_data[i]) \ | |
5fbb0954 AD |
88 | max = table_data[i]; \ |
89 | } \ | |
5372019f AD |
90 | obstack_1grow (&format_obstack, 0); \ |
91 | muscle_insert (name, obstack_finish (&format_obstack)); \ | |
5fbb0954 | 92 | \ |
87ceef73 PE |
93 | lmin = min; \ |
94 | lmax = max; \ | |
12b0043a AD |
95 | /* Build `NAME_min' and `NAME_max' in the obstack. */ \ |
96 | obstack_fgrow1 (&format_obstack, "%s_min", name); \ | |
97 | obstack_1grow (&format_obstack, 0); \ | |
87ceef73 | 98 | MUSCLE_INSERT_LONG_INT (obstack_finish (&format_obstack), lmin); \ |
5372019f AD |
99 | obstack_fgrow1 (&format_obstack, "%s_max", name); \ |
100 | obstack_1grow (&format_obstack, 0); \ | |
87ceef73 | 101 | MUSCLE_INSERT_LONG_INT (obstack_finish (&format_obstack), lmax); \ |
62a3e4f0 AD |
102 | } |
103 | ||
5372019f | 104 | GENERATE_MUSCLE_INSERT_TABLE(muscle_insert_unsigned_int_table, unsigned int) |
12b0043a | 105 | GENERATE_MUSCLE_INSERT_TABLE(muscle_insert_int_table, int) |
21fd22d6 PE |
106 | GENERATE_MUSCLE_INSERT_TABLE(muscle_insert_base_table, base_number) |
107 | GENERATE_MUSCLE_INSERT_TABLE(muscle_insert_rule_number_table, rule_number) | |
108 | GENERATE_MUSCLE_INSERT_TABLE(muscle_insert_symbol_number_table, symbol_number) | |
21fd22d6 | 109 | GENERATE_MUSCLE_INSERT_TABLE(muscle_insert_state_number_table, state_number) |
c3e23647 RS |
110 | |
111 | ||
05ac60f3 PE |
112 | /*--------------------------------------------------------------------. |
113 | | Print to OUT a representation of STRING escaped both for C and M4. | | |
114 | `--------------------------------------------------------------------*/ | |
3813e141 PE |
115 | |
116 | static void | |
05ac60f3 | 117 | escaped_output (FILE *out, char const *string) |
3813e141 PE |
118 | { |
119 | char const *p; | |
2f4f028d | 120 | fprintf (out, "[["); |
3813e141 | 121 | |
05ac60f3 | 122 | for (p = quotearg_style (c_quoting_style, string); *p; p++) |
3813e141 PE |
123 | switch (*p) |
124 | { | |
125 | case '$': fputs ("$][", out); break; | |
126 | case '@': fputs ("@@", out); break; | |
127 | case '[': fputs ("@{", out); break; | |
128 | case ']': fputs ("@}", out); break; | |
129 | default: fputc (*p, out); break; | |
130 | } | |
131 | ||
2f4f028d | 132 | fprintf (out, "]]"); |
3813e141 PE |
133 | } |
134 | ||
135 | ||
39912f52 AD |
136 | /*------------------------------------------------------------------. |
137 | | Prepare the muscles related to the symbols: translate, tname, and | | |
138 | | toknum. | | |
139 | `------------------------------------------------------------------*/ | |
b0940840 | 140 | |
4a120d45 | 141 | static void |
39912f52 | 142 | prepare_symbols (void) |
c3e23647 | 143 | { |
141f5793 | 144 | MUSCLE_INSERT_BOOL ("token_table", token_table_flag); |
39912f52 AD |
145 | MUSCLE_INSERT_INT ("tokens_number", ntokens); |
146 | MUSCLE_INSERT_INT ("nterms_number", nvars); | |
d69c9694 | 147 | MUSCLE_INSERT_INT ("symbols_number", nsyms); |
39912f52 AD |
148 | MUSCLE_INSERT_INT ("undef_token_number", undeftoken->number); |
149 | MUSCLE_INSERT_INT ("user_token_number_max", max_user_token_number); | |
150 | ||
a49aecd5 | 151 | muscle_insert_symbol_number_table ("translate", |
fc5734fe AD |
152 | token_translations, |
153 | token_translations[0], | |
154 | 1, max_user_token_number + 1); | |
c3e23647 | 155 | |
39912f52 | 156 | /* tname -- token names. */ |
b0940840 AD |
157 | { |
158 | int i; | |
d423d460 AD |
159 | /* We assume that the table will be output starting at column 2. */ |
160 | int j = 2; | |
1cfe6375 JD |
161 | struct quoting_options *qo = clone_quoting_options (0); |
162 | set_quoting_style (qo, c_quoting_style); | |
163 | set_quoting_flags (qo, QA_SPLIT_TRIGRAPHS); | |
b0940840 AD |
164 | for (i = 0; i < nsyms; i++) |
165 | { | |
1cfe6375 | 166 | char *cp = quotearg_alloc (symbols[i]->tag, -1, qo); |
d423d460 AD |
167 | /* Width of the next token, including the two quotes, the |
168 | comma and the space. */ | |
21fd22d6 | 169 | int width = strlen (cp) + 2; |
b0940840 | 170 | |
21fd22d6 | 171 | if (j + width > 75) |
b0940840 | 172 | { |
d423d460 AD |
173 | obstack_sgrow (&format_obstack, "\n "); |
174 | j = 1; | |
b0940840 AD |
175 | } |
176 | ||
d423d460 AD |
177 | if (i) |
178 | obstack_1grow (&format_obstack, ' '); | |
3813e141 | 179 | MUSCLE_OBSTACK_SGROW (&format_obstack, cp); |
1cfe6375 | 180 | free (cp); |
d423d460 | 181 | obstack_1grow (&format_obstack, ','); |
21fd22d6 | 182 | j += width; |
b0940840 | 183 | } |
1cfe6375 | 184 | free (qo); |
8e1687ae | 185 | obstack_sgrow (&format_obstack, " ]b4_null["); |
c3e23647 | 186 | |
b0940840 AD |
187 | /* Finish table and store. */ |
188 | obstack_1grow (&format_obstack, 0); | |
189 | muscle_insert ("tname", obstack_finish (&format_obstack)); | |
190 | } | |
191 | ||
12b0043a | 192 | /* Output YYTOKNUM. */ |
b2ed6e58 AD |
193 | { |
194 | int i; | |
da2a7671 | 195 | int *values = xnmalloc (ntokens, sizeof *values); |
3650b4b8 | 196 | for (i = 0; i < ntokens; ++i) |
b0940840 | 197 | values[i] = symbols[i]->user_token_number; |
12b0043a | 198 | muscle_insert_int_table ("toknum", values, |
3650b4b8 | 199 | values[0], 1, ntokens); |
b0940840 | 200 | free (values); |
b2ed6e58 | 201 | } |
b0940840 | 202 | } |
b2ed6e58 | 203 | |
b0940840 | 204 | |
3d3bc1fe AD |
205 | /*----------------------------------------------------------------. |
206 | | Prepare the muscles related to the rules: r1, r2, rline, dprec, | | |
207 | | merger. | | |
208 | `----------------------------------------------------------------*/ | |
b0940840 AD |
209 | |
210 | static void | |
211 | prepare_rules (void) | |
212 | { | |
da2a7671 PE |
213 | unsigned int *rline = xnmalloc (nrules, sizeof *rline); |
214 | symbol_number *r1 = xnmalloc (nrules, sizeof *r1); | |
215 | unsigned int *r2 = xnmalloc (nrules, sizeof *r2); | |
f6fbd3da PE |
216 | int *dprec = xnmalloc (nrules, sizeof *dprec); |
217 | int *merger = xnmalloc (nrules, sizeof *merger); | |
4b3d3a8e | 218 | |
3d3bc1fe | 219 | rule_number r; |
4b3d3a8e | 220 | for (r = 0; r < nrules; ++r) |
b0940840 | 221 | { |
b0940840 AD |
222 | /* LHS of the rule R. */ |
223 | r1[r] = rules[r].lhs->number; | |
224 | /* Length of rule R's RHS. */ | |
3d3bc1fe | 225 | r2[r] = rule_rhs_length(&rules[r]); |
b0940840 | 226 | /* Line where rule was defined. */ |
2073702c | 227 | rline[r] = rules[r].location.start.line; |
95612cfa | 228 | /* Dynamic precedence (GLR). */ |
676385e2 | 229 | dprec[r] = rules[r].dprec; |
95612cfa | 230 | /* Merger-function index (GLR). */ |
676385e2 | 231 | merger[r] = rules[r].merger; |
b0940840 | 232 | } |
b0940840 | 233 | |
4b3d3a8e AD |
234 | muscle_insert_unsigned_int_table ("rline", rline, 0, 0, nrules); |
235 | muscle_insert_symbol_number_table ("r1", r1, 0, 0, nrules); | |
236 | muscle_insert_unsigned_int_table ("r2", r2, 0, 0, nrules); | |
f6fbd3da PE |
237 | muscle_insert_int_table ("dprec", dprec, 0, 0, nrules); |
238 | muscle_insert_int_table ("merger", merger, 0, 0, nrules); | |
796d61fb | 239 | |
39912f52 | 240 | MUSCLE_INSERT_INT ("rules_number", nrules); |
25005f6a | 241 | MUSCLE_INSERT_INT ("max_left_semantic_context", max_left_semantic_context); |
39912f52 | 242 | |
5df5f6d5 AD |
243 | free (rline); |
244 | free (r1); | |
b0940840 | 245 | free (r2); |
676385e2 PH |
246 | free (dprec); |
247 | free (merger); | |
c3e23647 RS |
248 | } |
249 | ||
b0940840 AD |
250 | /*--------------------------------------------. |
251 | | Prepare the muscles related to the states. | | |
252 | `--------------------------------------------*/ | |
c3e23647 | 253 | |
4a120d45 | 254 | static void |
b0940840 | 255 | prepare_states (void) |
c3e23647 | 256 | { |
21fd22d6 | 257 | state_number i; |
da2a7671 | 258 | symbol_number *values = xnmalloc (nstates, sizeof *values); |
9703cc49 | 259 | for (i = 0; i < nstates; ++i) |
29e88316 | 260 | values[i] = states[i]->accessing_symbol; |
a49aecd5 | 261 | muscle_insert_symbol_number_table ("stos", values, |
d57650a5 | 262 | 0, 1, nstates); |
87ceef73 | 263 | free (values); |
39912f52 AD |
264 | |
265 | MUSCLE_INSERT_INT ("last", high); | |
266 | MUSCLE_INSERT_INT ("final_state_number", final_state->number); | |
267 | MUSCLE_INSERT_INT ("states_number", nstates); | |
c3e23647 RS |
268 | } |
269 | ||
270 | ||
4c3cc7da AD |
271 | /*-------------------------------------------------------. |
272 | | Compare two symbols by type-name, and then by number. | | |
273 | `-------------------------------------------------------*/ | |
274 | ||
d73e55e0 | 275 | static int |
4c3cc7da AD |
276 | symbol_type_name_cmp (const symbol **lhs, const symbol **rhs) |
277 | { | |
278 | int res = UNIQSTR_CMP((*lhs)->type_name, (*rhs)->type_name); | |
279 | if (res) | |
280 | return res; | |
281 | return (*lhs)->number - (*rhs)->number; | |
282 | } | |
283 | ||
284 | ||
285 | /*----------------------------------------------------------------. | |
286 | | Return a (malloc'ed) table of the symbols sorted by type-name. | | |
287 | `----------------------------------------------------------------*/ | |
288 | ||
289 | static symbol ** | |
d73e55e0 | 290 | symbols_by_type_name (void) |
4c3cc7da AD |
291 | { |
292 | typedef int (*qcmp_type) (const void *, const void *); | |
293 | symbol **res = xmemdup (symbols, nsyms * sizeof *res); | |
294 | qsort (res, nsyms, sizeof *res, (qcmp_type) &symbol_type_name_cmp); | |
295 | return res; | |
296 | } | |
297 | ||
298 | ||
299 | /*------------------------------------------------------------------. | |
300 | | Define b4_type_names, which is a list of (lists of the numbers of | | |
301 | | symbols with same type-name). | | |
302 | `------------------------------------------------------------------*/ | |
303 | ||
304 | static void | |
305 | type_names_output (FILE *out) | |
306 | { | |
307 | int i; | |
4c3cc7da AD |
308 | symbol **syms = symbols_by_type_name (); |
309 | fputs ("m4_define([b4_type_names],\n[", out); | |
5d731440 AD |
310 | for (i = 0; i < nsyms; /* nothing */) |
311 | { | |
312 | // The index of the first symbol of the current type-name. | |
313 | int i0 = i; | |
314 | fputs (i ? ",\n[" : "[", out); | |
315 | for (; i < nsyms && syms[i]->type_name == syms[i0]->type_name; ++i) | |
316 | fprintf (out, "%s%d", i != i0 ? ", " : "", syms[i]->number); | |
317 | fputs ("]", out); | |
318 | } | |
4c3cc7da AD |
319 | fputs ("])\n\n", out); |
320 | free (syms); | |
321 | } | |
322 | ||
c3e23647 | 323 | |
d69c9694 AD |
324 | /*-------------------------------------. |
325 | | The list of all the symbol numbers. | | |
326 | `-------------------------------------*/ | |
5ab8c47b AD |
327 | |
328 | static void | |
d69c9694 | 329 | symbol_numbers_output (FILE *out) |
5ab8c47b AD |
330 | { |
331 | int i; | |
d69c9694 | 332 | fputs ("m4_define([b4_symbol_numbers],\n[", out); |
5ab8c47b | 333 | for (i = 0; i < nsyms; ++i) |
d69c9694 | 334 | fprintf (out, "%s[%d]", i ? ", " : "", i); |
5ab8c47b AD |
335 | fputs ("])\n\n", out); |
336 | } | |
337 | ||
338 | ||
bb33f19a PE |
339 | /*---------------------------------. |
340 | | Output the user actions to OUT. | | |
341 | `---------------------------------*/ | |
12b0043a | 342 | |
4a120d45 | 343 | static void |
c6f1a33c | 344 | user_actions_output (FILE *out) |
3f96f4dc | 345 | { |
21fd22d6 | 346 | rule_number r; |
dafdc66f | 347 | |
8e1687ae | 348 | fputs ("m4_define([b4_actions], \n[", out); |
4b3d3a8e | 349 | for (r = 0; r < nrules; ++r) |
9222837b | 350 | if (rules[r].action) |
3f96f4dc | 351 | { |
8e1687ae | 352 | fprintf (out, "b4_case(%d, [b4_syncline(%d, ", r + 1, |
2073702c | 353 | rules[r].action_location.start.line); |
05ac60f3 | 354 | escaped_output (out, rules[r].action_location.start.file); |
8e1687ae | 355 | fprintf (out, ")\n[ %s]])\n\n", rules[r].action); |
3f96f4dc | 356 | } |
8e1687ae | 357 | fputs ("])\n\n", out); |
3f96f4dc AD |
358 | } |
359 | ||
1de69d6a AD |
360 | /*------------------------------------. |
361 | | Output the merge functions to OUT. | | |
362 | `------------------------------------*/ | |
676385e2 | 363 | |
41442480 | 364 | static void |
676385e2 PH |
365 | merger_output (FILE *out) |
366 | { | |
367 | int n; | |
368 | merger_list* p; | |
369 | ||
370 | fputs ("m4_define([b4_mergers], \n[[", out); | |
e0e5bf84 | 371 | for (n = 1, p = merge_functions; p != NULL; n += 1, p = p->next) |
676385e2 | 372 | { |
e0e5bf84 | 373 | if (p->type[0] == '\0') |
16caa4f4 | 374 | fprintf (out, " case %d: *yy0 = %s (*yy0, *yy1); break;\n", |
676385e2 PH |
375 | n, p->name); |
376 | else | |
16caa4f4 | 377 | fprintf (out, " case %d: yy0->%s = %s (*yy0, *yy1); break;\n", |
676385e2 PH |
378 | n, p->type, p->name); |
379 | } | |
380 | fputs ("]])\n\n", out); | |
381 | } | |
3f96f4dc | 382 | |
d69c9694 | 383 | |
d73e55e0 DJ |
384 | /*---------------------------------------------. |
385 | | Prepare the muscles for symbol definitions. | | |
386 | `---------------------------------------------*/ | |
d69c9694 AD |
387 | |
388 | static void | |
d73e55e0 | 389 | prepare_symbol_definitions (void) |
d69c9694 AD |
390 | { |
391 | int i; | |
392 | for (i = 0; i < nsyms; ++i) | |
393 | { | |
394 | symbol *sym = symbols[i]; | |
395 | const char *key; | |
aea10ef4 | 396 | const char *value; |
d69c9694 AD |
397 | |
398 | #define SET_KEY(Entry) \ | |
399 | obstack_fgrow2 (&format_obstack, "symbol(%d, %s)", i, Entry); \ | |
400 | obstack_1grow (&format_obstack, 0); \ | |
401 | key = obstack_finish (&format_obstack); | |
402 | ||
aea10ef4 AD |
403 | // Whether the symbol has an identifier. |
404 | value = symbol_id_get (sym); | |
405 | SET_KEY("has_id"); | |
406 | MUSCLE_INSERT_INT (key, !!value); | |
407 | ||
408 | // Its identifier. | |
409 | SET_KEY("id"); | |
410 | MUSCLE_INSERT_STRING (key, value ? value : ""); | |
8be046d7 | 411 | |
aea10ef4 | 412 | // Its tag. Typically for documentation purpose. |
d69c9694 AD |
413 | SET_KEY("tag"); |
414 | MUSCLE_INSERT_STRING (key, sym->tag); | |
415 | ||
416 | SET_KEY("user_number"); | |
417 | MUSCLE_INSERT_INT (key, sym->user_token_number); | |
418 | ||
5d731440 AD |
419 | SET_KEY("is_token"); |
420 | MUSCLE_INSERT_INT (key, | |
421 | i < ntokens && sym != errtoken && sym != undeftoken); | |
422 | ||
d69c9694 AD |
423 | SET_KEY("number"); |
424 | MUSCLE_INSERT_INT (key, sym->number); | |
425 | ||
6659366c | 426 | SET_KEY("has_type"); |
5d731440 AD |
427 | MUSCLE_INSERT_INT (key, !!sym->type_name); |
428 | ||
6659366c | 429 | SET_KEY("type"); |
d69c9694 AD |
430 | MUSCLE_INSERT_STRING (key, sym->type_name ? sym->type_name : ""); |
431 | ||
2bde9113 AD |
432 | #define CODE_PROP(PropName) \ |
433 | do { \ | |
434 | code_props const *p = symbol_ ## PropName ## _get (sym); \ | |
435 | SET_KEY("has_" #PropName); \ | |
436 | MUSCLE_INSERT_INT (key, !!p->code); \ | |
437 | \ | |
438 | if (p->code) \ | |
439 | { \ | |
440 | SET_KEY(#PropName "_file"); \ | |
441 | MUSCLE_INSERT_STRING (key, p->location.start.file); \ | |
442 | \ | |
443 | SET_KEY(#PropName "_line"); \ | |
444 | MUSCLE_INSERT_INT (key, p->location.start.line); \ | |
445 | \ | |
446 | SET_KEY(#PropName); \ | |
447 | MUSCLE_INSERT_STRING_RAW (key, p->code); \ | |
448 | } \ | |
449 | } while (0) | |
450 | ||
451 | CODE_PROP(destructor); | |
452 | CODE_PROP(printer); | |
453 | #undef CODE_PROP | |
d69c9694 AD |
454 | #undef SET_KEY |
455 | } | |
456 | } | |
457 | ||
458 | ||
ae7453f2 AD |
459 | /*--------------------------------------. |
460 | | Output the tokens definition to OUT. | | |
461 | `--------------------------------------*/ | |
091e20bb | 462 | |
c6f1a33c | 463 | static void |
be2a1a68 | 464 | token_definitions_output (FILE *out) |
091e20bb AD |
465 | { |
466 | int i; | |
d0829076 | 467 | char const *sep = ""; |
dafdc66f AD |
468 | |
469 | fputs ("m4_define([b4_tokens], \n[", out); | |
091e20bb AD |
470 | for (i = 0; i < ntokens; ++i) |
471 | { | |
21fd22d6 PE |
472 | symbol *sym = symbols[i]; |
473 | int number = sym->user_token_number; | |
90462b8d | 474 | uniqstr id = symbol_id_get (sym); |
091e20bb | 475 | |
dfaa4860 JD |
476 | /* At this stage, if there are literal string aliases, they are |
477 | part of SYMBOLS, so we should not find their aliased symbols | |
478 | here. */ | |
479 | aver (number != USER_NUMBER_HAS_STRING_ALIAS); | |
b87f8b21 | 480 | |
90462b8d AD |
481 | /* Skip error token and tokens without identifier. */ |
482 | if (sym != errtoken && id) | |
483 | { | |
484 | fprintf (out, "%s[[[%s]], %d]", | |
485 | sep, id, number); | |
486 | sep = ",\n"; | |
487 | } | |
091e20bb | 488 | } |
dafdc66f | 489 | fputs ("])\n\n", out); |
091e20bb AD |
490 | } |
491 | ||
492 | ||
4a120d45 | 493 | static void |
c6f1a33c | 494 | prepare_actions (void) |
6c89f1c1 | 495 | { |
c6f1a33c | 496 | /* Figure out the actions for the specified state, indexed by |
742e4900 | 497 | lookahead token type. */ |
bbb5bcc6 | 498 | |
c6f1a33c AD |
499 | muscle_insert_rule_number_table ("defact", yydefact, |
500 | yydefact[0], 1, nstates); | |
6c89f1c1 | 501 | |
c6f1a33c AD |
502 | /* Figure out what to do after reducing with each rule, depending on |
503 | the saved state from before the beginning of parsing the data | |
504 | that matched this rule. */ | |
d57650a5 AD |
505 | muscle_insert_state_number_table ("defgoto", yydefgoto, |
506 | yydefgoto[0], 1, nsyms - ntokens); | |
12b0043a | 507 | |
12b0043a | 508 | |
12b0043a AD |
509 | /* Output PACT. */ |
510 | muscle_insert_base_table ("pact", base, | |
5372019f | 511 | base[0], 1, nstates); |
12b0043a | 512 | MUSCLE_INSERT_INT ("pact_ninf", base_ninf); |
c3e23647 | 513 | |
12b0043a AD |
514 | /* Output PGOTO. */ |
515 | muscle_insert_base_table ("pgoto", base, | |
5372019f | 516 | base[nstates], nstates + 1, nvectors); |
c3e23647 | 517 | |
12b0043a AD |
518 | muscle_insert_base_table ("table", table, |
519 | table[0], 1, high + 1); | |
520 | MUSCLE_INSERT_INT ("table_ninf", table_ninf); | |
676385e2 | 521 | |
12b0043a AD |
522 | muscle_insert_base_table ("check", check, |
523 | check[0], 1, high + 1); | |
c3e23647 | 524 | |
ea99527d AD |
525 | /* GLR parsing slightly modifies YYTABLE and YYCHECK (and thus |
526 | YYPACT) so that in states with unresolved conflicts, the default | |
527 | reduction is not used in the conflicted entries, so that there is | |
528 | a place to put a conflict pointer. | |
529 | ||
530 | This means that YYCONFLP and YYCONFL are nonsense for a non-GLR | |
531 | parser, so we could avoid accidents by not writing them out in | |
532 | that case. Nevertheless, it seems even better to be able to use | |
533 | the GLR skeletons even without the non-deterministic tables. */ | |
534 | muscle_insert_unsigned_int_table ("conflict_list_heads", conflict_table, | |
39912f52 | 535 | conflict_table[0], 1, high + 1); |
ea99527d | 536 | muscle_insert_unsigned_int_table ("conflicting_rules", conflict_list, |
da2a7671 | 537 | 0, 1, conflict_list_cnt); |
6c89f1c1 | 538 | } |
4175f6bb AD |
539 | |
540 | ||
541 | /*--------------------------------------------. | |
542 | | Output the definitions of all the muscles. | | |
543 | `--------------------------------------------*/ | |
544 | ||
545 | static void | |
546 | muscles_output (FILE *out) | |
547 | { | |
548 | fputs ("m4_init()\n", out); | |
4175f6bb | 549 | merger_output (out); |
d69c9694 AD |
550 | symbol_numbers_output (out); |
551 | token_definitions_output (out); | |
4c3cc7da | 552 | type_names_output (out); |
d69c9694 | 553 | user_actions_output (out); |
4c3cc7da | 554 | // Must be last. |
4175f6bb AD |
555 | muscles_m4_output (out); |
556 | } | |
652def80 | 557 | \f |
1239777d AD |
558 | /*---------------------------. |
559 | | Call the skeleton parser. | | |
560 | `---------------------------*/ | |
c3e23647 | 561 | |
4a120d45 | 562 | static void |
1239777d | 563 | output_skeleton (void) |
9b3add5b | 564 | { |
573312ac | 565 | FILE *in; |
573312ac | 566 | int filter_fd[2]; |
d8911864 | 567 | char const *argv[10]; |
573312ac PE |
568 | pid_t pid; |
569 | ||
a9fc7990 | 570 | /* Compute the names of the package data dir and skeleton files. */ |
573312ac | 571 | char const m4sugar[] = "m4sugar/m4sugar.m4"; |
90b9908d | 572 | char const m4bison[] = "bison.m4"; |
2fd4e131 | 573 | char *full_m4sugar; |
90b9908d | 574 | char *full_m4bison; |
eecf08fe | 575 | char *full_skeleton; |
573312ac PE |
576 | char const *p; |
577 | char const *m4 = (p = getenv ("M4")) ? p : M4; | |
d4bd2295 | 578 | char const *pkgdatadir = compute_pkgdatadir (); |
573312ac PE |
579 | size_t skeleton_size = strlen (skeleton) + 1; |
580 | size_t pkgdatadirlen = strlen (pkgdatadir); | |
581 | while (pkgdatadirlen && pkgdatadir[pkgdatadirlen - 1] == '/') | |
582 | pkgdatadirlen--; | |
eecf08fe PE |
583 | full_skeleton = xmalloc (pkgdatadirlen + 1 |
584 | + (skeleton_size < sizeof m4sugar | |
585 | ? sizeof m4sugar : skeleton_size)); | |
a7867f53 | 586 | strncpy (full_skeleton, pkgdatadir, pkgdatadirlen); |
eecf08fe PE |
587 | full_skeleton[pkgdatadirlen] = '/'; |
588 | strcpy (full_skeleton + pkgdatadirlen + 1, m4sugar); | |
589 | full_m4sugar = xstrdup (full_skeleton); | |
90b9908d PB |
590 | strcpy (full_skeleton + pkgdatadirlen + 1, m4bison); |
591 | full_m4bison = xstrdup (full_skeleton); | |
a7867f53 JD |
592 | if (strchr (skeleton, '/')) |
593 | strcpy (full_skeleton, skeleton); | |
594 | else | |
595 | strcpy (full_skeleton + pkgdatadirlen + 1, skeleton); | |
a9fc7990 JD |
596 | |
597 | /* Test whether m4sugar.m4 is readable, to check for proper | |
598 | installation. A faulty installation can cause deadlock, so a | |
599 | cheap sanity check is worthwhile. */ | |
2fd4e131 | 600 | xfclose (xfopen (full_m4sugar, "r")); |
573312ac PE |
601 | |
602 | /* Create an m4 subprocess connected to us via two pipes. */ | |
603 | ||
604 | if (trace_flag & trace_tools) | |
90b9908d | 605 | fprintf (stderr, "running: %s %s - %s %s\n", |
a9fc7990 | 606 | m4, full_m4sugar, full_m4bison, full_skeleton); |
573312ac | 607 | |
a9fc7990 JD |
608 | /* Some future version of GNU M4 (most likely 1.6) may treat the -dV in a |
609 | position-dependent manner. Keep it as the first argument so that all | |
610 | files are traced. | |
573312ac | 611 | |
a9fc7990 JD |
612 | See the thread starting at |
613 | <http://lists.gnu.org/archive/html/bug-bison/2008-07/msg00000.html> | |
614 | for details. */ | |
615 | { | |
616 | int i = 0; | |
617 | argv[i++] = m4; | |
d8911864 EB |
618 | |
619 | /* When POSIXLY_CORRECT is set, GNU M4 1.6 and later disable GNU | |
620 | extensions, which Bison's skeletons depend on. With older M4, | |
621 | it has no effect. M4 1.4.12 added a -g/--gnu command-line | |
622 | option to make it explicit that a program wants GNU M4 | |
623 | extensions even when POSIXLY_CORRECT is set. | |
624 | ||
625 | See the thread starting at | |
626 | <http://lists.gnu.org/archive/html/bug-bison/2008-07/msg00000.html> | |
627 | for details. */ | |
628 | if (*M4_GNU_OPTION) | |
629 | argv[i++] = M4_GNU_OPTION; | |
630 | ||
d67c52e8 EB |
631 | argv[i++] = "-I"; |
632 | argv[i++] = pkgdatadir; | |
a9fc7990 JD |
633 | if (trace_flag & trace_m4) |
634 | argv[i++] = "-dV"; | |
635 | argv[i++] = full_m4sugar; | |
636 | argv[i++] = "-"; | |
637 | argv[i++] = full_m4bison; | |
638 | argv[i++] = full_skeleton; | |
639 | argv[i++] = NULL; | |
9789acf0 | 640 | aver (i <= ARRAY_CARDINALITY (argv)); |
a9fc7990 | 641 | } |
a9fc7990 | 642 | |
f39ab286 JD |
643 | /* The ugly cast is because gnulib gets the const-ness wrong. */ |
644 | pid = create_pipe_bidi ("m4", m4, (char **)(void*)argv, false, true, | |
645 | true, filter_fd); | |
2fd4e131 | 646 | free (full_m4sugar); |
a9fc7990 | 647 | free (full_m4bison); |
eecf08fe | 648 | free (full_skeleton); |
573312ac | 649 | |
5263bea9 AD |
650 | if (trace_flag & trace_muscles) |
651 | muscles_output (stderr); | |
652 | { | |
f39ab286 | 653 | FILE *out = fdopen (filter_fd[1], "w"); |
5263bea9 AD |
654 | if (! out) |
655 | error (EXIT_FAILURE, get_errno (), | |
656 | "fdopen"); | |
657 | muscles_output (out); | |
658 | xfclose (out); | |
659 | } | |
be2a1a68 | 660 | |
573312ac | 661 | /* Read and process m4's output. */ |
1509d42f | 662 | timevar_push (TV_M4); |
f39ab286 | 663 | in = fdopen (filter_fd[0], "r"); |
573312ac | 664 | if (! in) |
04098407 PE |
665 | error (EXIT_FAILURE, get_errno (), |
666 | "fdopen"); | |
573312ac | 667 | scan_skel (in); |
f39ab286 JD |
668 | /* scan_skel should have read all of M4's output. Otherwise, when we |
669 | close the pipe, we risk letting M4 report a broken-pipe to the | |
670 | Bison user. */ | |
671 | aver (feof (in)); | |
573312ac | 672 | xfclose (in); |
f39ab286 | 673 | wait_subprocess (pid, "m4", false, false, true, true, NULL); |
1509d42f | 674 | timevar_pop (TV_M4); |
26f609ff RA |
675 | } |
676 | ||
677 | static void | |
678 | prepare (void) | |
679 | { | |
945e396c JD |
680 | /* BISON_USE_PUSH_FOR_PULL is for the test suite and should not be documented |
681 | for the user. */ | |
682 | char const *use_push_for_pull_env = getenv ("BISON_USE_PUSH_FOR_PULL"); | |
683 | bool use_push_for_pull_flag = false; | |
684 | if (use_push_for_pull_env != NULL | |
685 | && use_push_for_pull_env[0] != '\0' | |
686 | && 0 != strcmp (use_push_for_pull_env, "0")) | |
687 | use_push_for_pull_flag = true; | |
688 | ||
7db2ed2d | 689 | /* Flags. */ |
d0829076 | 690 | MUSCLE_INSERT_BOOL ("defines_flag", defines_flag); |
90b9908d | 691 | MUSCLE_INSERT_BOOL ("glr_flag", glr_parser); |
90b9908d | 692 | MUSCLE_INSERT_BOOL ("nondeterministic_flag", nondeterministic_parser); |
d0829076 | 693 | MUSCLE_INSERT_BOOL ("synclines_flag", !no_lines_flag); |
ddc8ede1 | 694 | MUSCLE_INSERT_BOOL ("tag_seen_flag", tag_seen); |
95021767 | 695 | MUSCLE_INSERT_BOOL ("use_push_for_pull_flag", use_push_for_pull_flag); |
b931235e | 696 | MUSCLE_INSERT_BOOL ("yacc_flag", yacc_flag); |
11d82f03 | 697 | |
573a6cd3 | 698 | /* File names. */ |
90b9908d PB |
699 | if (spec_name_prefix) |
700 | MUSCLE_INSERT_STRING ("prefix", spec_name_prefix); | |
701 | ||
bd9d212b JD |
702 | MUSCLE_INSERT_STRING ("file_name_all_but_ext", all_but_ext); |
703 | ||
2b81e969 AD |
704 | #define DEFINE(Name) MUSCLE_INSERT_STRING (#Name, Name ? Name : "") |
705 | DEFINE (dir_prefix); | |
706 | DEFINE (parser_file_name); | |
707 | DEFINE (spec_defines_file); | |
708 | DEFINE (spec_file_prefix); | |
709 | DEFINE (spec_graph_file); | |
710 | DEFINE (spec_name_prefix); | |
711 | DEFINE (spec_outfile); | |
712 | DEFINE (spec_verbose_file); | |
713 | #undef DEFINE | |
b85810ae | 714 | |
0e021770 PE |
715 | /* Find the right skeleton file, and add muscles about the skeletons. */ |
716 | if (skeleton) | |
717 | MUSCLE_INSERT_C_STRING ("skeleton", skeleton); | |
718 | else | |
719 | skeleton = language->skeleton; | |
381fb12e | 720 | |
0e021770 | 721 | /* About the skeletons. */ |
cf147260 | 722 | { |
bd9d212b JD |
723 | /* b4_pkgdatadir is used inside m4_include in the skeletons, so digraphs |
724 | would never be expanded. Hopefully no one has M4-special characters in | |
725 | his Bison installation path. */ | |
d4bd2295 | 726 | MUSCLE_INSERT_STRING_RAW ("pkgdatadir", compute_pkgdatadir ()); |
cf147260 | 727 | } |
26f609ff | 728 | } |
c3e23647 | 729 | |
93ede233 | 730 | |
6c89f1c1 AD |
731 | /*----------------------------------------------------------. |
732 | | Output the parsing tables and the parser code to ftable. | | |
733 | `----------------------------------------------------------*/ | |
c3e23647 | 734 | |
6c89f1c1 AD |
735 | void |
736 | output (void) | |
737 | { | |
f87685c3 | 738 | obstack_init (&format_obstack); |
dd60faec | 739 | |
39912f52 | 740 | prepare_symbols (); |
b0940840 AD |
741 | prepare_rules (); |
742 | prepare_states (); | |
536545f3 | 743 | prepare_actions (); |
d73e55e0 | 744 | prepare_symbol_definitions (); |
342b8b6e | 745 | |
26f609ff | 746 | prepare (); |
652def80 | 747 | |
9b3add5b RA |
748 | /* Process the selected skeleton file. */ |
749 | output_skeleton (); | |
750 | ||
f499b062 | 751 | obstack_free (&format_obstack, NULL); |
c3e23647 | 752 | } |
d4bd2295 JD |
753 | |
754 | char const * | |
755 | compute_pkgdatadir (void) | |
756 | { | |
757 | char const *pkgdatadir = getenv ("BISON_PKGDATADIR"); | |
758 | return pkgdatadir ? pkgdatadir : PKGDATADIR; | |
759 | } |