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