]>
Commit | Line | Data |
---|---|---|
a932883e | 1 | /* Output the generated parsing program for Bison. |
87ceef73 | 2 | |
e2a21b6f PE |
3 | Copyright (C) 1984, 1986, 1989, 1992, 2000, 2001, 2002, 2003, 2004, |
4 | 2005, 2006 Free Software Foundation, Inc. | |
c3e23647 | 5 | |
9ee3c97b | 6 | This file is part of Bison, the GNU Compiler Compiler. |
c3e23647 | 7 | |
9ee3c97b AD |
8 | Bison is free software; you can redistribute it and/or modify it |
9 | under the terms of the GNU General Public License as published by | |
10 | the Free Software Foundation; either version 2, or (at your option) | |
11 | any later version. | |
c3e23647 | 12 | |
9ee3c97b AD |
13 | Bison is distributed in the hope that it will be useful, but |
14 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 | General Public License for more details. | |
c3e23647 | 17 | |
9ee3c97b AD |
18 | You should have received a copy of the GNU General Public License |
19 | along with Bison; see the file COPYING. If not, write to the Free | |
0fb669f9 PE |
20 | Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
21 | 02110-1301, USA. */ | |
c3e23647 | 22 | |
2cec9080 | 23 | #include <config.h> |
c3e23647 | 24 | #include "system.h" |
21fd22d6 PE |
25 | |
26 | #include <error.h> | |
27 | #include <get-errno.h> | |
28 | #include <quotearg.h> | |
29 | #include <subpipe.h> | |
30 | #include <timevar.h> | |
31 | ||
32 | #include "complain.h" | |
c3e23647 | 33 | #include "files.h" |
21fd22d6 | 34 | #include "getargs.h" |
c3e23647 | 35 | #include "gram.h" |
21fd22d6 | 36 | #include "muscle_tab.h" |
6c89f1c1 | 37 | #include "output.h" |
a70083a3 | 38 | #include "reader.h" |
04098407 | 39 | #include "scan-skel.h" |
ad949da9 | 40 | #include "symtab.h" |
c6f1a33c | 41 | #include "tables.h" |
be2a1a68 | 42 | |
12b0043a | 43 | |
f87685c3 | 44 | static struct obstack format_obstack; |
c3e23647 | 45 | |
133c20e2 | 46 | |
5372019f AD |
47 | /*-------------------------------------------------------------------. |
48 | | Create a function NAME which associates to the muscle NAME the | | |
49 | | result of formatting the FIRST and then TABLE_DATA[BEGIN..END[ (of | | |
50 | | TYPE), and to the muscle NAME_max, the max value of the | | |
51 | | TABLE_DATA. | | |
52 | `-------------------------------------------------------------------*/ | |
62a3e4f0 | 53 | |
62a3e4f0 | 54 | |
5372019f | 55 | #define GENERATE_MUSCLE_INSERT_TABLE(Name, Type) \ |
5fbb0954 | 56 | \ |
5372019f | 57 | static void \ |
9e0876fb | 58 | Name (char const *name, \ |
5fbb0954 AD |
59 | Type *table_data, \ |
60 | Type first, \ | |
61 | int begin, \ | |
62 | int end) \ | |
63 | { \ | |
12b0043a | 64 | Type min = first; \ |
0c2d3f4c | 65 | Type max = first; \ |
87ceef73 PE |
66 | long int lmin; \ |
67 | long int lmax; \ | |
5fbb0954 AD |
68 | int i; \ |
69 | int j = 1; \ | |
70 | \ | |
5372019f | 71 | obstack_fgrow1 (&format_obstack, "%6d", first); \ |
5fbb0954 AD |
72 | for (i = begin; i < end; ++i) \ |
73 | { \ | |
5372019f | 74 | obstack_1grow (&format_obstack, ','); \ |
5fbb0954 AD |
75 | if (j >= 10) \ |
76 | { \ | |
5372019f | 77 | obstack_sgrow (&format_obstack, "\n "); \ |
5fbb0954 AD |
78 | j = 1; \ |
79 | } \ | |
80 | else \ | |
81 | ++j; \ | |
5372019f | 82 | obstack_fgrow1 (&format_obstack, "%6d", table_data[i]); \ |
12b0043a AD |
83 | if (table_data[i] < min) \ |
84 | min = table_data[i]; \ | |
85 | if (max < table_data[i]) \ | |
5fbb0954 AD |
86 | max = table_data[i]; \ |
87 | } \ | |
5372019f AD |
88 | obstack_1grow (&format_obstack, 0); \ |
89 | muscle_insert (name, obstack_finish (&format_obstack)); \ | |
5fbb0954 | 90 | \ |
87ceef73 PE |
91 | lmin = min; \ |
92 | lmax = max; \ | |
12b0043a AD |
93 | /* Build `NAME_min' and `NAME_max' in the obstack. */ \ |
94 | obstack_fgrow1 (&format_obstack, "%s_min", name); \ | |
95 | obstack_1grow (&format_obstack, 0); \ | |
87ceef73 | 96 | MUSCLE_INSERT_LONG_INT (obstack_finish (&format_obstack), lmin); \ |
5372019f AD |
97 | obstack_fgrow1 (&format_obstack, "%s_max", name); \ |
98 | obstack_1grow (&format_obstack, 0); \ | |
87ceef73 | 99 | MUSCLE_INSERT_LONG_INT (obstack_finish (&format_obstack), lmax); \ |
62a3e4f0 AD |
100 | } |
101 | ||
5372019f | 102 | GENERATE_MUSCLE_INSERT_TABLE(muscle_insert_unsigned_int_table, unsigned int) |
12b0043a | 103 | GENERATE_MUSCLE_INSERT_TABLE(muscle_insert_int_table, int) |
21fd22d6 PE |
104 | GENERATE_MUSCLE_INSERT_TABLE(muscle_insert_base_table, base_number) |
105 | GENERATE_MUSCLE_INSERT_TABLE(muscle_insert_rule_number_table, rule_number) | |
106 | GENERATE_MUSCLE_INSERT_TABLE(muscle_insert_symbol_number_table, symbol_number) | |
107 | GENERATE_MUSCLE_INSERT_TABLE(muscle_insert_item_number_table, item_number) | |
108 | GENERATE_MUSCLE_INSERT_TABLE(muscle_insert_state_number_table, state_number) | |
c3e23647 RS |
109 | |
110 | ||
05ac60f3 PE |
111 | /*--------------------------------------------------------------------. |
112 | | Print to OUT a representation of STRING escaped both for C and M4. | | |
113 | `--------------------------------------------------------------------*/ | |
3813e141 PE |
114 | |
115 | static void | |
05ac60f3 | 116 | escaped_output (FILE *out, char const *string) |
3813e141 PE |
117 | { |
118 | char const *p; | |
2f4f028d | 119 | fprintf (out, "[["); |
3813e141 | 120 | |
05ac60f3 | 121 | for (p = quotearg_style (c_quoting_style, string); *p; p++) |
3813e141 PE |
122 | switch (*p) |
123 | { | |
124 | case '$': fputs ("$][", out); break; | |
125 | case '@': fputs ("@@", out); break; | |
126 | case '[': fputs ("@{", out); break; | |
127 | case ']': fputs ("@}", out); break; | |
128 | default: fputc (*p, out); break; | |
129 | } | |
130 | ||
2f4f028d | 131 | fprintf (out, "]]"); |
3813e141 PE |
132 | } |
133 | ||
134 | ||
39912f52 AD |
135 | /*------------------------------------------------------------------. |
136 | | Prepare the muscles related to the symbols: translate, tname, and | | |
137 | | toknum. | | |
138 | `------------------------------------------------------------------*/ | |
b0940840 | 139 | |
4a120d45 | 140 | static void |
39912f52 | 141 | prepare_symbols (void) |
c3e23647 | 142 | { |
141f5793 | 143 | MUSCLE_INSERT_BOOL ("token_table", token_table_flag); |
39912f52 AD |
144 | MUSCLE_INSERT_INT ("tokens_number", ntokens); |
145 | MUSCLE_INSERT_INT ("nterms_number", nvars); | |
146 | MUSCLE_INSERT_INT ("undef_token_number", undeftoken->number); | |
147 | MUSCLE_INSERT_INT ("user_token_number_max", max_user_token_number); | |
148 | ||
a49aecd5 | 149 | muscle_insert_symbol_number_table ("translate", |
fc5734fe AD |
150 | token_translations, |
151 | token_translations[0], | |
152 | 1, max_user_token_number + 1); | |
c3e23647 | 153 | |
39912f52 | 154 | /* tname -- token names. */ |
b0940840 AD |
155 | { |
156 | int i; | |
d423d460 AD |
157 | /* We assume that the table will be output starting at column 2. */ |
158 | int j = 2; | |
b0940840 AD |
159 | for (i = 0; i < nsyms; i++) |
160 | { | |
9e0876fb | 161 | char const *cp = quotearg_style (c_quoting_style, symbols[i]->tag); |
d423d460 AD |
162 | /* Width of the next token, including the two quotes, the |
163 | comma and the space. */ | |
21fd22d6 | 164 | int width = strlen (cp) + 2; |
b0940840 | 165 | |
21fd22d6 | 166 | if (j + width > 75) |
b0940840 | 167 | { |
d423d460 AD |
168 | obstack_sgrow (&format_obstack, "\n "); |
169 | j = 1; | |
b0940840 AD |
170 | } |
171 | ||
d423d460 AD |
172 | if (i) |
173 | obstack_1grow (&format_obstack, ' '); | |
3813e141 | 174 | MUSCLE_OBSTACK_SGROW (&format_obstack, cp); |
d423d460 | 175 | obstack_1grow (&format_obstack, ','); |
21fd22d6 | 176 | j += width; |
b0940840 AD |
177 | } |
178 | /* Add a NULL entry to list of tokens (well, 0, as NULL might not be | |
179 | defined). */ | |
d423d460 | 180 | obstack_sgrow (&format_obstack, " 0"); |
c3e23647 | 181 | |
b0940840 AD |
182 | /* Finish table and store. */ |
183 | obstack_1grow (&format_obstack, 0); | |
184 | muscle_insert ("tname", obstack_finish (&format_obstack)); | |
185 | } | |
186 | ||
12b0043a | 187 | /* Output YYTOKNUM. */ |
b2ed6e58 AD |
188 | { |
189 | int i; | |
da2a7671 | 190 | int *values = xnmalloc (ntokens, sizeof *values); |
3650b4b8 | 191 | for (i = 0; i < ntokens; ++i) |
b0940840 | 192 | values[i] = symbols[i]->user_token_number; |
12b0043a | 193 | muscle_insert_int_table ("toknum", values, |
3650b4b8 | 194 | values[0], 1, ntokens); |
b0940840 | 195 | free (values); |
b2ed6e58 | 196 | } |
b0940840 | 197 | } |
b2ed6e58 | 198 | |
b0940840 AD |
199 | |
200 | /*-------------------------------------------------------------. | |
201 | | Prepare the muscles related to the rules: rhs, prhs, r1, r2, | | |
95612cfa | 202 | | rline, dprec, merger. | |
b0940840 AD |
203 | `-------------------------------------------------------------*/ |
204 | ||
205 | static void | |
206 | prepare_rules (void) | |
207 | { | |
21fd22d6 | 208 | rule_number r; |
5df5f6d5 | 209 | unsigned int i = 0; |
da2a7671 PE |
210 | item_number *rhs = xnmalloc (nritems, sizeof *rhs); |
211 | unsigned int *prhs = xnmalloc (nrules, sizeof *prhs); | |
212 | unsigned int *rline = xnmalloc (nrules, sizeof *rline); | |
213 | symbol_number *r1 = xnmalloc (nrules, sizeof *r1); | |
214 | unsigned int *r2 = xnmalloc (nrules, sizeof *r2); | |
f6fbd3da PE |
215 | int *dprec = xnmalloc (nrules, sizeof *dprec); |
216 | int *merger = xnmalloc (nrules, sizeof *merger); | |
4b3d3a8e AD |
217 | |
218 | for (r = 0; r < nrules; ++r) | |
b0940840 | 219 | { |
21fd22d6 | 220 | item_number *rhsp = NULL; |
b0940840 AD |
221 | /* Index of rule R in RHS. */ |
222 | prhs[r] = i; | |
223 | /* RHS of the rule R. */ | |
224 | for (rhsp = rules[r].rhs; *rhsp >= 0; ++rhsp) | |
225 | rhs[i++] = *rhsp; | |
226 | /* LHS of the rule R. */ | |
227 | r1[r] = rules[r].lhs->number; | |
228 | /* Length of rule R's RHS. */ | |
229 | r2[r] = i - prhs[r]; | |
230 | /* Separator in RHS. */ | |
231 | rhs[i++] = -1; | |
232 | /* Line where rule was defined. */ | |
2073702c | 233 | rline[r] = rules[r].location.start.line; |
95612cfa | 234 | /* Dynamic precedence (GLR). */ |
676385e2 | 235 | dprec[r] = rules[r].dprec; |
95612cfa | 236 | /* Merger-function index (GLR). */ |
676385e2 | 237 | merger[r] = rules[r].merger; |
b0940840 | 238 | } |
68cae94e | 239 | assert (i == nritems); |
b0940840 | 240 | |
5372019f | 241 | muscle_insert_item_number_table ("rhs", rhs, ritem[0], 1, nritems); |
4b3d3a8e AD |
242 | muscle_insert_unsigned_int_table ("prhs", prhs, 0, 0, nrules); |
243 | muscle_insert_unsigned_int_table ("rline", rline, 0, 0, nrules); | |
244 | muscle_insert_symbol_number_table ("r1", r1, 0, 0, nrules); | |
245 | muscle_insert_unsigned_int_table ("r2", r2, 0, 0, nrules); | |
f6fbd3da PE |
246 | muscle_insert_int_table ("dprec", dprec, 0, 0, nrules); |
247 | muscle_insert_int_table ("merger", merger, 0, 0, nrules); | |
796d61fb | 248 | |
39912f52 | 249 | MUSCLE_INSERT_INT ("rules_number", nrules); |
25005f6a | 250 | MUSCLE_INSERT_INT ("max_left_semantic_context", max_left_semantic_context); |
39912f52 | 251 | |
b0940840 AD |
252 | free (rhs); |
253 | free (prhs); | |
5df5f6d5 AD |
254 | free (rline); |
255 | free (r1); | |
b0940840 | 256 | free (r2); |
676385e2 PH |
257 | free (dprec); |
258 | free (merger); | |
c3e23647 RS |
259 | } |
260 | ||
b0940840 AD |
261 | /*--------------------------------------------. |
262 | | Prepare the muscles related to the states. | | |
263 | `--------------------------------------------*/ | |
c3e23647 | 264 | |
4a120d45 | 265 | static void |
b0940840 | 266 | prepare_states (void) |
c3e23647 | 267 | { |
21fd22d6 | 268 | state_number i; |
da2a7671 | 269 | symbol_number *values = xnmalloc (nstates, sizeof *values); |
9703cc49 | 270 | for (i = 0; i < nstates; ++i) |
29e88316 | 271 | values[i] = states[i]->accessing_symbol; |
a49aecd5 | 272 | muscle_insert_symbol_number_table ("stos", values, |
d57650a5 | 273 | 0, 1, nstates); |
87ceef73 | 274 | free (values); |
39912f52 AD |
275 | |
276 | MUSCLE_INSERT_INT ("last", high); | |
277 | MUSCLE_INSERT_INT ("final_state_number", final_state->number); | |
278 | MUSCLE_INSERT_INT ("states_number", nstates); | |
c3e23647 RS |
279 | } |
280 | ||
281 | ||
c3e23647 | 282 | |
bb33f19a PE |
283 | /*---------------------------------. |
284 | | Output the user actions to OUT. | | |
285 | `---------------------------------*/ | |
12b0043a | 286 | |
4a120d45 | 287 | static void |
c6f1a33c | 288 | user_actions_output (FILE *out) |
3f96f4dc | 289 | { |
21fd22d6 | 290 | rule_number r; |
dafdc66f AD |
291 | |
292 | fputs ("m4_define([b4_actions], \n[[", out); | |
4b3d3a8e | 293 | for (r = 0; r < nrules; ++r) |
9222837b | 294 | if (rules[r].action) |
3f96f4dc | 295 | { |
4b3d3a8e | 296 | fprintf (out, " case %d:\n", r + 1); |
3f96f4dc | 297 | |
05ac60f3 | 298 | fprintf (out, "]b4_syncline(%d, ", |
2073702c | 299 | rules[r].action_location.start.line); |
05ac60f3 | 300 | escaped_output (out, rules[r].action_location.start.file); |
2f4f028d PE |
301 | fprintf (out, ")[\n"); |
302 | fprintf (out, " %s\n break;\n\n", | |
9222837b | 303 | rules[r].action); |
3f96f4dc | 304 | } |
dafdc66f | 305 | fputs ("]])\n\n", out); |
3f96f4dc AD |
306 | } |
307 | ||
676385e2 PH |
308 | /*--------------------------------------. |
309 | | Output the merge functions to OUT. | | |
310 | `--------------------------------------*/ | |
311 | ||
41442480 | 312 | static void |
676385e2 PH |
313 | merger_output (FILE *out) |
314 | { | |
315 | int n; | |
316 | merger_list* p; | |
317 | ||
318 | fputs ("m4_define([b4_mergers], \n[[", out); | |
e0e5bf84 | 319 | for (n = 1, p = merge_functions; p != NULL; n += 1, p = p->next) |
676385e2 | 320 | { |
e0e5bf84 | 321 | if (p->type[0] == '\0') |
16caa4f4 | 322 | fprintf (out, " case %d: *yy0 = %s (*yy0, *yy1); break;\n", |
676385e2 PH |
323 | n, p->name); |
324 | else | |
16caa4f4 | 325 | fprintf (out, " case %d: yy0->%s = %s (*yy0, *yy1); break;\n", |
676385e2 PH |
326 | n, p->type, p->name); |
327 | } | |
328 | fputs ("]])\n\n", out); | |
329 | } | |
3f96f4dc | 330 | |
ae7453f2 AD |
331 | /*--------------------------------------. |
332 | | Output the tokens definition to OUT. | | |
333 | `--------------------------------------*/ | |
091e20bb | 334 | |
c6f1a33c | 335 | static void |
be2a1a68 | 336 | token_definitions_output (FILE *out) |
091e20bb AD |
337 | { |
338 | int i; | |
d0829076 | 339 | char const *sep = ""; |
dafdc66f AD |
340 | |
341 | fputs ("m4_define([b4_tokens], \n[", out); | |
091e20bb AD |
342 | for (i = 0; i < ntokens; ++i) |
343 | { | |
21fd22d6 PE |
344 | symbol *sym = symbols[i]; |
345 | int number = sym->user_token_number; | |
091e20bb | 346 | |
b87f8b21 AD |
347 | /* At this stage, if there are literal aliases, they are part of |
348 | SYMBOLS, so we should not find symbols which are the aliases | |
349 | here. */ | |
68cae94e | 350 | assert (number != USER_NUMBER_ALIAS); |
b87f8b21 | 351 | |
091e20bb | 352 | /* Skip error token. */ |
21fd22d6 | 353 | if (sym == errtoken) |
091e20bb | 354 | continue; |
b87f8b21 AD |
355 | |
356 | /* If this string has an alias, then it is necessarily the alias | |
357 | which is to be output. */ | |
21fd22d6 PE |
358 | if (sym->alias) |
359 | sym = sym->alias; | |
b87f8b21 AD |
360 | |
361 | /* Don't output literal chars or strings (when defined only as a | |
362 | string). Note that must be done after the alias resolution: | |
363 | think about `%token 'f' "f"'. */ | |
21fd22d6 | 364 | if (sym->tag[0] == '\'' || sym->tag[0] == '\"') |
b87f8b21 | 365 | continue; |
091e20bb AD |
366 | |
367 | /* Don't #define nonliteral tokens whose names contain periods | |
368 | or '$' (as does the default value of the EOF token). */ | |
21fd22d6 | 369 | if (strchr (sym->tag, '.') || strchr (sym->tag, '$')) |
091e20bb AD |
370 | continue; |
371 | ||
05ac60f3 | 372 | fprintf (out, "%s[[[%s]], %d]", |
d0829076 PE |
373 | sep, sym->tag, number); |
374 | sep = ",\n"; | |
091e20bb | 375 | } |
dafdc66f | 376 | fputs ("])\n\n", out); |
091e20bb AD |
377 | } |
378 | ||
379 | ||
ae7453f2 AD |
380 | /*---------------------------------------. |
381 | | Output the symbol destructors to OUT. | | |
382 | `---------------------------------------*/ | |
9280d3ef AD |
383 | |
384 | static void | |
385 | symbol_destructors_output (FILE *out) | |
386 | { | |
387 | int i; | |
d0829076 | 388 | char const *sep = ""; |
9280d3ef AD |
389 | |
390 | fputs ("m4_define([b4_symbol_destructors], \n[", out); | |
391 | for (i = 0; i < nsyms; ++i) | |
392 | if (symbols[i]->destructor) | |
393 | { | |
21fd22d6 | 394 | symbol *sym = symbols[i]; |
9280d3ef | 395 | |
24c0aad7 AD |
396 | /* Filename, lineno, |
397 | Symbol-name, Symbol-number, | |
ee42c616 | 398 | destructor, optional typename. */ |
d0829076 PE |
399 | fprintf (out, "%s[", sep); |
400 | sep = ",\n"; | |
05ac60f3 PE |
401 | escaped_output (out, sym->destructor_location.start.file); |
402 | fprintf (out, ", %d, ", sym->destructor_location.start.line); | |
403 | escaped_output (out, sym->tag); | |
404 | fprintf (out, ", %d, [[%s]]", sym->number, sym->destructor); | |
ee42c616 PE |
405 | if (sym->type_name) |
406 | fprintf (out, ", [[%s]]", sym->type_name); | |
407 | fputc (']', out); | |
9280d3ef AD |
408 | } |
409 | fputs ("])\n\n", out); | |
410 | } | |
411 | ||
412 | ||
ae7453f2 AD |
413 | /*------------------------------------. |
414 | | Output the symbol printers to OUT. | | |
415 | `------------------------------------*/ | |
366eea36 AD |
416 | |
417 | static void | |
418 | symbol_printers_output (FILE *out) | |
419 | { | |
420 | int i; | |
d0829076 | 421 | char const *sep = ""; |
366eea36 AD |
422 | |
423 | fputs ("m4_define([b4_symbol_printers], \n[", out); | |
424 | for (i = 0; i < nsyms; ++i) | |
34e1d160 | 425 | if (symbols[i]->printer) |
366eea36 | 426 | { |
21fd22d6 | 427 | symbol *sym = symbols[i]; |
366eea36 AD |
428 | |
429 | /* Filename, lineno, | |
430 | Symbol-name, Symbol-number, | |
ee42c616 | 431 | printer, optional typename. */ |
d0829076 PE |
432 | fprintf (out, "%s[", sep); |
433 | sep = ",\n"; | |
05ac60f3 PE |
434 | escaped_output (out, sym->printer_location.start.file); |
435 | fprintf (out, ", %d, ", sym->printer_location.start.line); | |
436 | escaped_output (out, sym->tag); | |
437 | fprintf (out, ", %d, [[%s]]", sym->number, sym->printer); | |
ee42c616 PE |
438 | if (sym->type_name) |
439 | fprintf (out, ", [[%s]]", sym->type_name); | |
440 | fputc (']', out); | |
366eea36 AD |
441 | } |
442 | fputs ("])\n\n", out); | |
443 | } | |
444 | ||
445 | ||
4a120d45 | 446 | static void |
c6f1a33c | 447 | prepare_actions (void) |
6c89f1c1 | 448 | { |
c6f1a33c | 449 | /* Figure out the actions for the specified state, indexed by |
8dd162d3 | 450 | look-ahead token type. */ |
bbb5bcc6 | 451 | |
c6f1a33c AD |
452 | muscle_insert_rule_number_table ("defact", yydefact, |
453 | yydefact[0], 1, nstates); | |
6c89f1c1 | 454 | |
c6f1a33c AD |
455 | /* Figure out what to do after reducing with each rule, depending on |
456 | the saved state from before the beginning of parsing the data | |
457 | that matched this rule. */ | |
d57650a5 AD |
458 | muscle_insert_state_number_table ("defgoto", yydefgoto, |
459 | yydefgoto[0], 1, nsyms - ntokens); | |
12b0043a | 460 | |
12b0043a | 461 | |
12b0043a AD |
462 | /* Output PACT. */ |
463 | muscle_insert_base_table ("pact", base, | |
5372019f | 464 | base[0], 1, nstates); |
12b0043a | 465 | MUSCLE_INSERT_INT ("pact_ninf", base_ninf); |
c3e23647 | 466 | |
12b0043a AD |
467 | /* Output PGOTO. */ |
468 | muscle_insert_base_table ("pgoto", base, | |
5372019f | 469 | base[nstates], nstates + 1, nvectors); |
c3e23647 | 470 | |
12b0043a AD |
471 | muscle_insert_base_table ("table", table, |
472 | table[0], 1, high + 1); | |
473 | MUSCLE_INSERT_INT ("table_ninf", table_ninf); | |
676385e2 | 474 | |
12b0043a AD |
475 | muscle_insert_base_table ("check", check, |
476 | check[0], 1, high + 1); | |
c3e23647 | 477 | |
ea99527d AD |
478 | /* GLR parsing slightly modifies YYTABLE and YYCHECK (and thus |
479 | YYPACT) so that in states with unresolved conflicts, the default | |
480 | reduction is not used in the conflicted entries, so that there is | |
481 | a place to put a conflict pointer. | |
482 | ||
483 | This means that YYCONFLP and YYCONFL are nonsense for a non-GLR | |
484 | parser, so we could avoid accidents by not writing them out in | |
485 | that case. Nevertheless, it seems even better to be able to use | |
486 | the GLR skeletons even without the non-deterministic tables. */ | |
487 | muscle_insert_unsigned_int_table ("conflict_list_heads", conflict_table, | |
39912f52 | 488 | conflict_table[0], 1, high + 1); |
ea99527d | 489 | muscle_insert_unsigned_int_table ("conflicting_rules", conflict_list, |
da2a7671 | 490 | 0, 1, conflict_list_cnt); |
6c89f1c1 | 491 | } |
c3e23647 | 492 | |
652def80 | 493 | \f |
1239777d AD |
494 | /*---------------------------. |
495 | | Call the skeleton parser. | | |
496 | `---------------------------*/ | |
c3e23647 | 497 | |
4a120d45 | 498 | static void |
1239777d | 499 | output_skeleton (void) |
9b3add5b | 500 | { |
573312ac PE |
501 | FILE *in; |
502 | FILE *out; | |
503 | int filter_fd[2]; | |
3953ed88 | 504 | char const *argv[5]; |
573312ac PE |
505 | pid_t pid; |
506 | ||
507 | /* Compute the names of the package data dir and skeleton file. | |
508 | Test whether m4sugar.m4 is readable, to check for proper | |
509 | installation. A faulty installation can cause deadlock, so a | |
510 | cheap sanity check is worthwhile. */ | |
511 | char const m4sugar[] = "m4sugar/m4sugar.m4"; | |
2fd4e131 | 512 | char *full_m4sugar; |
eecf08fe | 513 | char *full_skeleton; |
573312ac PE |
514 | char const *p; |
515 | char const *m4 = (p = getenv ("M4")) ? p : M4; | |
516 | char const *pkgdatadir = (p = getenv ("BISON_PKGDATADIR")) ? p : PKGDATADIR; | |
517 | size_t skeleton_size = strlen (skeleton) + 1; | |
518 | size_t pkgdatadirlen = strlen (pkgdatadir); | |
519 | while (pkgdatadirlen && pkgdatadir[pkgdatadirlen - 1] == '/') | |
520 | pkgdatadirlen--; | |
eecf08fe PE |
521 | full_skeleton = xmalloc (pkgdatadirlen + 1 |
522 | + (skeleton_size < sizeof m4sugar | |
523 | ? sizeof m4sugar : skeleton_size)); | |
524 | strcpy (full_skeleton, pkgdatadir); | |
525 | full_skeleton[pkgdatadirlen] = '/'; | |
526 | strcpy (full_skeleton + pkgdatadirlen + 1, m4sugar); | |
527 | full_m4sugar = xstrdup (full_skeleton); | |
eecf08fe | 528 | strcpy (full_skeleton + pkgdatadirlen + 1, skeleton); |
2fd4e131 | 529 | xfclose (xfopen (full_m4sugar, "r")); |
573312ac PE |
530 | |
531 | /* Create an m4 subprocess connected to us via two pipes. */ | |
532 | ||
533 | if (trace_flag & trace_tools) | |
3953ed88 AD |
534 | fprintf (stderr, "running: %s %s - %s\n", |
535 | m4, full_m4sugar, full_skeleton); | |
573312ac PE |
536 | |
537 | argv[0] = m4; | |
2fd4e131 PE |
538 | argv[1] = full_m4sugar; |
539 | argv[2] = "-"; | |
3953ed88 AD |
540 | argv[3] = full_skeleton; |
541 | argv[4] = NULL; | |
573312ac PE |
542 | |
543 | init_subpipe (); | |
544 | pid = create_subpipe (argv, filter_fd); | |
2fd4e131 | 545 | free (full_m4sugar); |
eecf08fe | 546 | free (full_skeleton); |
573312ac PE |
547 | |
548 | out = fdopen (filter_fd[0], "w"); | |
549 | if (! out) | |
04098407 PE |
550 | error (EXIT_FAILURE, get_errno (), |
551 | "fdopen"); | |
573312ac PE |
552 | |
553 | /* Output the definitions of all the muscles. */ | |
381fb12e AD |
554 | fputs ("m4_init()\n", out); |
555 | ||
c6f1a33c | 556 | user_actions_output (out); |
676385e2 | 557 | merger_output (out); |
381fb12e | 558 | token_definitions_output (out); |
9280d3ef | 559 | symbol_destructors_output (out); |
366eea36 | 560 | symbol_printers_output (out); |
be2a1a68 | 561 | |
381fb12e | 562 | muscles_m4_output (out); |
be2a1a68 | 563 | |
381fb12e AD |
564 | fputs ("m4_wrap([m4_divert_pop(0)])\n", out); |
565 | fputs ("m4_divert_push(0)dnl\n", out); | |
566 | xfclose (out); | |
be2a1a68 | 567 | |
573312ac | 568 | /* Read and process m4's output. */ |
1509d42f | 569 | timevar_push (TV_M4); |
ec4d88f4 | 570 | end_of_output_subpipe (pid, filter_fd); |
573312ac PE |
571 | in = fdopen (filter_fd[1], "r"); |
572 | if (! in) | |
04098407 PE |
573 | error (EXIT_FAILURE, get_errno (), |
574 | "fdopen"); | |
573312ac | 575 | scan_skel (in); |
573312ac PE |
576 | xfclose (in); |
577 | reap_subpipe (pid, m4); | |
1509d42f | 578 | timevar_pop (TV_M4); |
26f609ff RA |
579 | } |
580 | ||
581 | static void | |
582 | prepare (void) | |
583 | { | |
7db2ed2d | 584 | /* Flags. */ |
d0829076 PE |
585 | MUSCLE_INSERT_BOOL ("debug", debug_flag); |
586 | MUSCLE_INSERT_BOOL ("defines_flag", defines_flag); | |
587 | MUSCLE_INSERT_BOOL ("error_verbose", error_verbose); | |
588 | MUSCLE_INSERT_BOOL ("locations_flag", locations_flag); | |
589 | MUSCLE_INSERT_BOOL ("pure", pure_parser); | |
590 | MUSCLE_INSERT_BOOL ("synclines_flag", !no_lines_flag); | |
11d82f03 | 591 | |
573a6cd3 | 592 | /* File names. */ |
7db2ed2d | 593 | MUSCLE_INSERT_STRING ("prefix", spec_name_prefix ? spec_name_prefix : "yy"); |
2b81e969 AD |
594 | #define DEFINE(Name) MUSCLE_INSERT_STRING (#Name, Name ? Name : "") |
595 | DEFINE (dir_prefix); | |
596 | DEFINE (parser_file_name); | |
597 | DEFINE (spec_defines_file); | |
598 | DEFINE (spec_file_prefix); | |
599 | DEFINE (spec_graph_file); | |
600 | DEFINE (spec_name_prefix); | |
601 | DEFINE (spec_outfile); | |
602 | DEFINE (spec_verbose_file); | |
603 | #undef DEFINE | |
b85810ae | 604 | |
7db2ed2d | 605 | /* User Code. */ |
0dd1580a RA |
606 | obstack_1grow (&pre_prologue_obstack, 0); |
607 | obstack_1grow (&post_prologue_obstack, 0); | |
608 | muscle_insert ("pre_prologue", obstack_finish (&pre_prologue_obstack)); | |
609 | muscle_insert ("post_prologue", obstack_finish (&post_prologue_obstack)); | |
381fb12e AD |
610 | |
611 | /* Find the right skeleton file. */ | |
612 | if (!skeleton) | |
676385e2 | 613 | { |
916708d5 | 614 | if (glr_parser || nondeterministic_parser) |
676385e2 PH |
615 | skeleton = "glr.c"; |
616 | else | |
617 | skeleton = "yacc.c"; | |
618 | } | |
381fb12e | 619 | |
cf147260 AD |
620 | /* About the skeletons. */ |
621 | { | |
1bd0deda PE |
622 | char const *pkgdatadir = getenv ("BISON_PKGDATADIR"); |
623 | MUSCLE_INSERT_STRING ("pkgdatadir", pkgdatadir ? pkgdatadir : PKGDATADIR); | |
cf147260 AD |
624 | MUSCLE_INSERT_C_STRING ("skeleton", skeleton); |
625 | } | |
26f609ff | 626 | } |
c3e23647 | 627 | |
93ede233 | 628 | |
6c89f1c1 AD |
629 | /*----------------------------------------------------------. |
630 | | Output the parsing tables and the parser code to ftable. | | |
631 | `----------------------------------------------------------*/ | |
c3e23647 | 632 | |
6c89f1c1 AD |
633 | void |
634 | output (void) | |
635 | { | |
f87685c3 | 636 | obstack_init (&format_obstack); |
dd60faec | 637 | |
39912f52 | 638 | prepare_symbols (); |
b0940840 AD |
639 | prepare_rules (); |
640 | prepare_states (); | |
536545f3 | 641 | prepare_actions (); |
342b8b6e | 642 | |
26f609ff | 643 | prepare (); |
652def80 | 644 | |
9b3add5b RA |
645 | /* Process the selected skeleton file. */ |
646 | output_skeleton (); | |
647 | ||
f499b062 | 648 | obstack_free (&format_obstack, NULL); |
0dd1580a RA |
649 | obstack_free (&pre_prologue_obstack, NULL); |
650 | obstack_free (&post_prologue_obstack, NULL); | |
c3e23647 | 651 | } |