]> git.saurik.com Git - bison.git/blame - src/output.c
* src/gram.c, src/gram.h (sprec, sassoc): Remove.
[bison.git] / src / output.c
CommitLineData
c3e23647 1/* Output the generated parsing program for bison,
255ef638
AD
2 Copyright 1984, 1986, 1989, 1992, 2000, 2001
3 Free Software Foundation, Inc.
c3e23647 4
9ee3c97b 5 This file is part of Bison, the GNU Compiler Compiler.
c3e23647 6
9ee3c97b
AD
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)
10 any later version.
c3e23647 11
9ee3c97b
AD
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.
c3e23647 16
9ee3c97b
AD
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
20 02111-1307, USA. */
c3e23647
RS
21
22
255ef638
AD
23/* The parser tables consist of these tables. Marked ones needed only
24 for the semantic parser. Double marked are output only if switches
25 are set.
c3e23647 26
255ef638
AD
27 YYTRANSLATE = vector mapping yylex's token numbers into bison's
28 token numbers.
c3e23647 29
255ef638 30 ++ YYTNAME = vector of string-names indexed by bison token number.
c3e23647 31
255ef638
AD
32 ++ YYTOKNUM = vector of yylex token numbers corresponding to
33 entries in YYTNAME.
c3e23647 34
255ef638
AD
35 YYRLINE = vector of line-numbers of all rules. For yydebug
36 printouts.
c3e23647 37
255ef638
AD
38 YYRHS = vector of items of all rules. This is exactly what RITEMS
39 contains. For yydebug and for semantic parser.
c3e23647 40
255ef638 41 YYPRHS[R] = index in YYRHS of first item for rule R.
c3e23647 42
255ef638 43 YYR1[R] = symbol number of symbol that rule R derives.
c3e23647 44
255ef638 45 YYR2[R] = number of symbols composing right hand side of rule R.
c3e23647 46
255ef638
AD
47 + YYSTOS[S] = the symbol number of the symbol that leads to state
48 S.
e372befa 49
255ef638
AD
50 YYDEFACT[S] = default rule to reduce with in state s, when YYTABLE
51 doesn't specify something else to do. Zero means the default is an
52 error.
c3e23647 53
255ef638
AD
54 YYDEFGOTO[I] = default state to go to after a reduction of a rule
55 that generates variable NTOKENS + I, except when YYTABLE specifies
56 something else to do.
c3e23647 57
255ef638
AD
58 YYPACT[S] = index in YYTABLE of the portion describing state S.
59 The lookahead token's type is used to index that portion to find
60 out what to do.
c3e23647 61
255ef638
AD
62 If the value in YYTABLE is positive, we shift the token and go to
63 that state.
c3e23647 64
6c89f1c1 65 If the value is negative, it is minus a rule number to reduce by.
c3e23647 66
255ef638
AD
67 If the value is zero, the default action from YYDEFACT[S] is used.
68
69 YYPGOTO[I] = the index in YYTABLE of the portion describing what to
70 do after reducing a rule that derives variable I + NTOKENS. This
71 portion is indexed by the parser state number, S, as of before the
72 text for this nonterminal was read. The value from YYTABLE is the
73 state to go to if the corresponding value in YYCHECK is S.
74
75 YYTABLE = a vector filled with portions for different uses, found
76 via YYPACT and YYPGOTO.
77
78 YYCHECK = a vector indexed in parallel with YYTABLE. It indicates,
79 in a roundabout way, the bounds of the portion you are trying to
80 examine.
81
82 Suppose that the portion of yytable starts at index P and the index
83 to be examined within the portion is I. Then if YYCHECK[P+I] != I,
84 I is outside the bounds of what is actually allocated, and the
85 default (from YYDEFACT or YYDEFGOTO) should be used. Otherwise,
86 YYTABLE[P+I] should be used.
87
88 YYFINAL = the state number of the termination state. YYFLAG = most
89 negative short int. Used to flag ?? */
c3e23647 90
c3e23647 91#include "system.h"
14d3eb9b 92#include "quotearg.h"
ceed8467 93#include "getargs.h"
c3e23647
RS
94#include "files.h"
95#include "gram.h"
b2ca4022 96#include "LR0.h"
a0f6b076 97#include "complain.h"
6c89f1c1 98#include "output.h"
720d742f 99#include "lalr.h"
a70083a3 100#include "reader.h"
ad949da9 101#include "symtab.h"
0619caf0 102#include "conflicts.h"
11d82f03 103#include "muscle_tab.h"
c3e23647 104
d019d655 105
c3e23647
RS
106static int nvectors;
107static int nentries;
342b8b6e
AD
108static short **froms = NULL;
109static short **tos = NULL;
110static short *tally = NULL;
111static short *width = NULL;
112static short *actrow = NULL;
113static short *state_count = NULL;
114static short *order = NULL;
115static short *base = NULL;
116static short *pos = NULL;
117static short *table = NULL;
118static short *check = NULL;
c3e23647
RS
119static int lowzero;
120static int high;
121
11d82f03 122struct obstack muscle_obstack;
f87685c3 123static struct obstack format_obstack;
c3e23647 124
c7925b99
MA
125int error_verbose = 0;
126
f0440388
MA
127/* Returns the number of lines of S. */
128static size_t
129get_lines_number (const char *s)
130{
131 size_t lines = 0;
1fa14068 132
f0440388
MA
133 size_t i;
134 for (i = 0; s[i]; ++i)
d0b0fefa
AD
135 if (s[i] == '\n')
136 ++lines;
1fa14068 137
f0440388
MA
138 return lines;
139}
140
141
26f609ff 142/* FIXME. */
c3e23647 143
d019d655 144static inline void
342b8b6e
AD
145output_table_data (struct obstack *oout,
146 short *table_data,
147 short first,
1fa14068
AD
148 int begin,
149 int end)
d019d655 150{
26f609ff
RA
151 int i;
152 int j = 1;
342b8b6e 153
26f609ff
RA
154 obstack_fgrow1 (oout, "%6d", first);
155 for (i = begin; i < end; ++i)
d019d655 156 {
896fe5c1 157 obstack_1grow (oout, ',');
d019d655
AD
158 if (j >= 10)
159 {
ff4423cc 160 obstack_sgrow (oout, "\n ");
d019d655
AD
161 j = 1;
162 }
163 else
26f609ff 164 ++j;
8f451ef7 165 obstack_fgrow1 (oout, "%6d", table_data[i]);
c3e23647 166 }
26f609ff 167 obstack_1grow (oout, 0);
c3e23647
RS
168}
169
170
4a120d45 171static void
d2729d44 172output_token_translations (void)
c3e23647 173{
f87685c3 174 output_table_data (&format_obstack, token_translations,
26f609ff 175 0, 1, max_user_token_number + 1);
f87685c3 176 muscle_insert ("translate", obstack_finish (&format_obstack));
342b8b6e 177 XFREE (token_translations);
c3e23647
RS
178}
179
180
4a120d45 181static void
d2729d44 182output_gram (void)
c3e23647 183{
b2ed6e58
AD
184 {
185 int i;
186 short *values = XCALLOC (short, nrules + 1);
187 for (i = 0; i < nrules + 1; ++i)
188 values[i] = rule_table[i].rhs;
f87685c3 189 output_table_data (&format_obstack, values,
b2ed6e58
AD
190 0, 1, nrules + 1);
191 XFREE (values);
192 }
193
f87685c3 194 muscle_insert ("prhs", obstack_finish (&format_obstack));
342b8b6e 195
1e9798d5 196 {
3db472b9 197 short *yyrhs;
1e9798d5 198 int i;
c3e23647 199
3db472b9 200 yyrhs = XMALLOC (short, nritems);
c3e23647 201
3db472b9
AD
202 for (i = 1; i < nritems; ++i)
203 yyrhs[i] = ritem[i] >= 0 ? ritem[i] : -1;
c3e23647 204
f87685c3 205 output_table_data (&format_obstack, yyrhs,
3db472b9 206 ritem[0], 1, nritems);
f87685c3 207 muscle_insert ("rhs", obstack_finish (&format_obstack));
26f609ff 208
1e9798d5
AD
209 XFREE (yyrhs);
210 }
796d61fb 211
4ec8e00f
MA
212#if 0
213 if (!semantic_parser)
214 obstack_sgrow (&table_obstack, "\n#endif\n");
215#endif
c3e23647
RS
216}
217
218
4a120d45 219static void
d2729d44 220output_stos (void)
c3e23647 221{
9703cc49
AD
222 int i;
223 short *values = (short *) alloca (sizeof (short) * nstates);
224 for (i = 0; i < nstates; ++i)
f693ad14 225 values[i] = state_table[i]->accessing_symbol;
f87685c3 226 output_table_data (&format_obstack, values,
26f609ff 227 0, 1, nstates);
f87685c3 228 muscle_insert ("stos", obstack_finish (&format_obstack));
c3e23647
RS
229}
230
231
4a120d45 232static void
d2729d44 233output_rule_data (void)
c3e23647 234{
6c89f1c1
AD
235 int i;
236 int j;
1e9798d5 237 short *short_tab = NULL;
c3e23647 238
e41dc700
AD
239 {
240 short *values = XCALLOC (short, nrules + 1);
241 for (i = 0; i < nrules + 1; ++i)
242 values[i] = rule_table[i].line;
f87685c3 243 output_table_data (&format_obstack, values,
e41dc700 244 0, 1, nrules + 1);
f87685c3 245 muscle_insert ("rline", obstack_finish (&format_obstack));
e41dc700
AD
246 XFREE (values);
247 }
248
c3e23647 249
1e9798d5
AD
250 j = 0;
251 for (i = 0; i < nsyms; i++)
c3e23647 252 {
92790e5b
AD
253 /* Be sure not to use twice the same quotearg slot. */
254 const char *cp =
255 quotearg_n_style (1, c_quoting_style,
ad949da9 256 quotearg_style (escape_quoting_style, symbols[i]->tag));
1e9798d5
AD
257 /* Width of the next token, including the two quotes, the coma
258 and the space. */
92790e5b 259 int strsize = strlen (cp) + 2;
1e9798d5
AD
260
261 if (j + strsize > 75)
c3e23647 262 {
f87685c3 263 obstack_sgrow (&format_obstack, "\n ");
1e9798d5 264 j = 2;
c3e23647
RS
265 }
266
f87685c3
AD
267 obstack_sgrow (&format_obstack, cp);
268 obstack_sgrow (&format_obstack, ", ");
1e9798d5 269 j += strsize;
c3e23647 270 }
9ee3c97b 271 /* add a NULL entry to list of tokens */
f87685c3 272 obstack_sgrow (&format_obstack, "NULL");
c3e23647 273
26f609ff 274 /* Finish table and store. */
f87685c3
AD
275 obstack_1grow (&format_obstack, 0);
276 muscle_insert ("tname", obstack_finish (&format_obstack));
e372befa 277
9ee3c97b 278 /* Output YYTOKNUM. */
f87685c3 279 output_table_data (&format_obstack, user_toknums,
26f609ff 280 0, 1, ntokens + 1);
f87685c3 281 muscle_insert ("toknum", obstack_finish (&format_obstack));
e372befa 282
9ee3c97b 283 /* Output YYR1. */
b2ed6e58
AD
284 {
285 short *values = XCALLOC (short, nrules + 1);
286 for (i = 0; i < nrules + 1; ++i)
287 values[i] = rule_table[i].lhs;
f87685c3 288 output_table_data (&format_obstack, values,
b2ed6e58 289 0, 1, nrules + 1);
f87685c3 290 muscle_insert ("r1", obstack_finish (&format_obstack));
b2ed6e58
AD
291 XFREE (values);
292 }
d019d655 293
9ee3c97b 294 /* Output YYR2. */
1e9798d5 295 short_tab = XMALLOC (short, nrules + 1);
c3e23647 296 for (i = 1; i < nrules; i++)
b2ed6e58 297 short_tab[i] = rule_table[i + 1].rhs - rule_table[i].rhs - 1;
9e7f6bbd 298 short_tab[nrules] = nritems - rule_table[nrules].rhs - 1;
f87685c3 299 output_table_data (&format_obstack, short_tab,
26f609ff 300 0, 1, nrules + 1);
f87685c3 301 muscle_insert ("r2", obstack_finish (&format_obstack));
1e9798d5 302 XFREE (short_tab);
c3e23647
RS
303}
304
6c89f1c1
AD
305/*------------------------------------------------------------------.
306| Decide what to do for each type of token if seen as the lookahead |
307| token in specified state. The value returned is used as the |
308| default action (yydefact) for the state. In addition, actrow is |
309| filled with what to do for each kind of token, index by symbol |
310| number, with zero meaning do the default action. The value |
311| MINSHORT, a very negative number, means this situation is an |
312| error. The parser recognizes this value specially. |
313| |
314| This is where conflicts are resolved. The loop over lookahead |
315| rules considered lower-numbered rules last, and the last rule |
316| considered that likes a token gets to handle it. |
317`------------------------------------------------------------------*/
c3e23647 318
4a120d45 319static int
f9507c28 320action_row (state_t *state)
c3e23647 321{
6c89f1c1 322 int i;
837491d8 323 int default_rule = 0;
f9507c28 324 reductions *redp = state->reductions;
f9507c28
AD
325 shifts *shiftp = state->shifts;
326 errs *errp = state->errs;
837491d8
AD
327 /* set nonzero to inhibit having any default reduction */
328 int nodefault = 0;
c3e23647
RS
329
330 for (i = 0; i < ntokens; i++)
331 actrow[i] = 0;
332
80dac38c 333 if (redp->nreds >= 1)
c3e23647 334 {
837491d8
AD
335 int j;
336 /* loop over all the rules available here which require
337 lookahead */
f9507c28 338 for (i = state->nlookaheads - 1; i >= 0; --i)
837491d8
AD
339 /* and find each token which the rule finds acceptable
340 to come next */
341 for (j = 0; j < ntokens; j++)
342 /* and record this rule as the rule to use if that
343 token follows. */
f9507c28
AD
344 if (BITISSET (LA (state->lookaheadsp + i), j))
345 actrow[j] = -LAruleno[state->lookaheadsp + i];
c3e23647
RS
346 }
347
36281465
AD
348 /* Now see which tokens are allowed for shifts in this state. For
349 them, record the shift as the thing to do. So shift is preferred
350 to reduce. */
d954473d 351 for (i = 0; i < shiftp->nshifts; i++)
c3e23647 352 {
837491d8
AD
353 int symbol;
354 int shift_state = shiftp->shifts[i];
d954473d
AD
355 if (!shift_state)
356 continue;
c3e23647 357
f693ad14 358 symbol = state_table[shift_state]->accessing_symbol;
c3e23647 359
d954473d
AD
360 if (ISVAR (symbol))
361 break;
c3e23647 362
d954473d 363 actrow[symbol] = shift_state;
c3e23647 364
d954473d
AD
365 /* Do not use any default reduction if there is a shift for
366 error */
367 if (symbol == error_token_number)
368 nodefault = 1;
c3e23647
RS
369 }
370
36281465
AD
371 /* See which tokens are an explicit error in this state (due to
372 %nonassoc). For them, record MINSHORT as the action. */
2cec70b9
AD
373 for (i = 0; i < errp->nerrs; i++)
374 {
375 int symbol = errp->errs[i];
376 actrow[symbol] = MINSHORT;
377 }
c3e23647 378
36281465
AD
379 /* Now find the most common reduction and make it the default action
380 for this state. */
c3e23647 381
80dac38c 382 if (redp->nreds >= 1 && !nodefault)
c3e23647 383 {
f9507c28 384 if (state->consistent)
c3e23647
RS
385 default_rule = redp->rules[0];
386 else
387 {
7a5350ba 388 int max = 0;
f9507c28 389 for (i = 0; i < state->nlookaheads; i++)
c3e23647 390 {
7a5350ba 391 int count = 0;
f9507c28 392 int rule = -LAruleno[state->lookaheadsp + i];
837491d8 393 int j;
9ee3c97b 394
c3e23647 395 for (j = 0; j < ntokens; j++)
837491d8
AD
396 if (actrow[j] == rule)
397 count++;
9ee3c97b 398
c3e23647
RS
399 if (count > max)
400 {
401 max = count;
402 default_rule = rule;
403 }
404 }
9ee3c97b 405
c3e23647
RS
406 /* actions which match the default are replaced with zero,
407 which means "use the default" */
9ee3c97b 408
c3e23647
RS
409 if (max > 0)
410 {
837491d8 411 int j;
c3e23647 412 for (j = 0; j < ntokens; j++)
837491d8
AD
413 if (actrow[j] == default_rule)
414 actrow[j] = 0;
9ee3c97b 415
ceed8467 416 default_rule = -default_rule;
c3e23647
RS
417 }
418 }
419 }
420
421 /* If have no default rule, the default is an error.
422 So replace any action which says "error" with "use default". */
423
424 if (default_rule == 0)
837491d8
AD
425 for (i = 0; i < ntokens; i++)
426 if (actrow[i] == MINSHORT)
427 actrow[i] = 0;
c3e23647 428
36281465 429 return default_rule;
c3e23647
RS
430}
431
432
4a120d45 433static void
d2729d44 434save_row (int state)
c3e23647 435{
6c89f1c1
AD
436 int i;
437 int count;
438 short *sp;
439 short *sp1;
440 short *sp2;
c3e23647
RS
441
442 count = 0;
443 for (i = 0; i < ntokens; i++)
796d61fb
AD
444 if (actrow[i] != 0)
445 count++;
c3e23647
RS
446
447 if (count == 0)
448 return;
449
d7913476
AD
450 froms[state] = sp1 = sp = XCALLOC (short, count);
451 tos[state] = sp2 = XCALLOC (short, count);
c3e23647
RS
452
453 for (i = 0; i < ntokens; i++)
796d61fb
AD
454 if (actrow[i] != 0)
455 {
456 *sp1++ = i;
457 *sp2++ = actrow[i];
458 }
c3e23647
RS
459
460 tally[state] = count;
461 width[state] = sp1[-1] - sp[0] + 1;
462}
463
464
6c89f1c1
AD
465/*------------------------------------------------------------------.
466| Figure out the actions for the specified state, indexed by |
467| lookahead token type. |
468| |
f2acea59
AD
469| The YYDEFACT table is output now. The detailed info is saved for |
470| putting into YYTABLE later. |
6c89f1c1 471`------------------------------------------------------------------*/
c3e23647 472
4a120d45 473static void
6c89f1c1 474token_actions (void)
c3e23647 475{
6c89f1c1 476 int i;
d7913476 477 short *yydefact = XCALLOC (short, nstates);
c3e23647 478
d7913476 479 actrow = XCALLOC (short, ntokens);
f2acea59 480 for (i = 0; i < nstates; ++i)
c3e23647 481 {
f9507c28 482 yydefact[i] = action_row (state_table[i]);
6c89f1c1 483 save_row (i);
c3e23647 484 }
bbb5bcc6 485
f87685c3 486 output_table_data (&format_obstack, yydefact,
26f609ff 487 yydefact[0], 1, nstates);
f87685c3 488 muscle_insert ("defact", obstack_finish (&format_obstack));
342b8b6e 489
26f609ff 490 XFREE (actrow);
d7913476 491 XFREE (yydefact);
c3e23647
RS
492}
493
494
3f96f4dc
AD
495/*-----------------------------.
496| Output the actions to OOUT. |
497`-----------------------------*/
498
499static void
f0440388 500actions_output (FILE *out, size_t *line)
3f96f4dc
AD
501{
502 int rule;
503 for (rule = 1; rule < nrules + 1; ++rule)
504 if (rule_table[rule].action)
505 {
ea52d706 506 fprintf (out, " case %d:\n", rule);
3f96f4dc
AD
507
508 if (!no_lines_flag)
ea52d706
AD
509 fprintf (out, muscle_find ("linef"),
510 rule_table[rule].action_line,
511 quotearg_style (c_quoting_style,
512 muscle_find ("filename")));
3f96f4dc
AD
513 /* As a Bison extension, add the ending semicolon. Since some
514 Yacc don't do that, help people using bison as a Yacc
515 finding their missing semicolons. */
ea52d706
AD
516 fprintf (out, "{ %s%s }\n break;\n\n",
517 rule_table[rule].action,
518 yacc_flag ? ";" : "");
f0440388 519
fbc8ecb7
MA
520 /* We always output 4 '\n' per action. */
521 *line += 4;
522 /* Plus one if !no_lines_flag. */
523 if (!no_lines_flag)
524 ++*line;
f0440388
MA
525 /* Get the number of lines written by the user. */
526 *line += get_lines_number (rule_table[rule].action);
3f96f4dc
AD
527 }
528}
529
530
f499b062
AD
531/*----------------------------.
532| Output the guards to OOUT. |
533`----------------------------*/
534
535static void
536guards_output (FILE *out, size_t *line)
537{
538 int rule;
539 for (rule = 1; rule < nrules + 1; ++rule)
540 if (rule_table[rule].action)
541 {
542 fprintf (out, " case %d:\n", rule);
543
544 if (!no_lines_flag)
545 fprintf (out, muscle_find ("linef"),
546 rule_table[rule].guard_line,
547 quotearg_style (c_quoting_style,
548 muscle_find ("filename")));
549 fprintf (out, "{ %s; }\n break;\n\n",
550 rule_table[rule].guard);
551
552 /* We always output 4 '\n' per action. */
553 *line += 4;
554 /* Plus one if !no_lines_flag. */
555 if (!no_lines_flag)
556 ++*line;
557 /* Get the number of lines written by the user. */
558 *line += get_lines_number (rule_table[rule].guard);
559 }
560}
561
562
4a120d45 563static void
d2729d44 564save_column (int symbol, int default_state)
c3e23647 565{
6c89f1c1 566 int i;
6c89f1c1
AD
567 short *sp;
568 short *sp1;
569 short *sp2;
570 int count;
837491d8 571 int symno = symbol - ntokens + nstates;
c3e23647 572
43591cec
AD
573 short begin = goto_map[symbol];
574 short end = goto_map[symbol + 1];
c3e23647
RS
575
576 count = 0;
43591cec 577 for (i = begin; i < end; i++)
796d61fb
AD
578 if (to_state[i] != default_state)
579 count++;
c3e23647
RS
580
581 if (count == 0)
582 return;
583
d7913476
AD
584 froms[symno] = sp1 = sp = XCALLOC (short, count);
585 tos[symno] = sp2 = XCALLOC (short, count);
c3e23647 586
43591cec 587 for (i = begin; i < end; i++)
796d61fb
AD
588 if (to_state[i] != default_state)
589 {
590 *sp1++ = from_state[i];
591 *sp2++ = to_state[i];
592 }
c3e23647
RS
593
594 tally[symno] = count;
595 width[symno] = sp1[-1] - sp[0] + 1;
596}
597
6c89f1c1
AD
598static int
599default_goto (int symbol)
600{
601 int i;
837491d8
AD
602 int m = goto_map[symbol];
603 int n = goto_map[symbol + 1];
604 int default_state = -1;
605 int max = 0;
6c89f1c1
AD
606
607 if (m == n)
608 return -1;
609
610 for (i = 0; i < nstates; i++)
611 state_count[i] = 0;
612
613 for (i = m; i < n; i++)
614 state_count[to_state[i]]++;
615
6c89f1c1 616 for (i = 0; i < nstates; i++)
796d61fb
AD
617 if (state_count[i] > max)
618 {
619 max = state_count[i];
620 default_state = i;
621 }
6c89f1c1
AD
622
623 return default_state;
624}
625
626
627/*-------------------------------------------------------------------.
628| Figure out what to do after reducing with each rule, depending on |
629| the saved state from before the beginning of parsing the data that |
630| matched this rule. |
631| |
632| The YYDEFGOTO table is output now. The detailed info is saved for |
633| putting into YYTABLE later. |
634`-------------------------------------------------------------------*/
635
636static void
637goto_actions (void)
638{
43591cec 639 int i;
bbb5bcc6 640 short *yydefgoto = XMALLOC (short, nsyms - ntokens);
bbb5bcc6 641
26f609ff 642 state_count = XCALLOC (short, nstates);
43591cec 643 for (i = ntokens; i < nsyms; ++i)
6c89f1c1 644 {
43591cec
AD
645 int default_state = default_goto (i);
646 save_column (i, default_state);
647 yydefgoto[i - ntokens] = default_state;
6c89f1c1
AD
648 }
649
f87685c3 650 output_table_data (&format_obstack, yydefgoto,
26f609ff 651 yydefgoto[0], 1, nsyms - ntokens);
f87685c3 652 muscle_insert ("defgoto", obstack_finish (&format_obstack));
43591cec 653
d7913476 654 XFREE (state_count);
43591cec 655 XFREE (yydefgoto);
6c89f1c1 656}
c3e23647
RS
657
658
9ee3c97b
AD
659/* The next few functions decide how to pack the actions and gotos
660 information into yytable. */
c3e23647 661
4a120d45 662static void
d2729d44 663sort_actions (void)
c3e23647 664{
6c89f1c1 665 int i;
c3e23647 666
d7913476 667 order = XCALLOC (short, nvectors);
c3e23647
RS
668 nentries = 0;
669
670 for (i = 0; i < nvectors; i++)
796d61fb
AD
671 if (tally[i] > 0)
672 {
837491d8
AD
673 int k;
674 int t = tally[i];
675 int w = width[i];
676 int j = nentries - 1;
c3e23647 677
796d61fb
AD
678 while (j >= 0 && (width[order[j]] < w))
679 j--;
c3e23647 680
796d61fb
AD
681 while (j >= 0 && (width[order[j]] == w) && (tally[order[j]] < t))
682 j--;
c3e23647 683
796d61fb
AD
684 for (k = nentries - 1; k > j; k--)
685 order[k + 1] = order[k];
c3e23647 686
796d61fb
AD
687 order[j + 1] = i;
688 nentries++;
689 }
c3e23647
RS
690}
691
692
4a120d45 693static int
d2729d44 694matching_state (int vector)
c3e23647 695{
837491d8 696 int i = order[vector];
6c89f1c1
AD
697 int t;
698 int w;
6c89f1c1 699 int prev;
c3e23647 700
c3e23647 701 if (i >= nstates)
36281465 702 return -1;
c3e23647
RS
703
704 t = tally[i];
705 w = width[i];
706
707 for (prev = vector - 1; prev >= 0; prev--)
708 {
837491d8
AD
709 int j = order[prev];
710 int k;
711 int match = 1;
712
c3e23647 713 if (width[j] != w || tally[j] != t)
36281465 714 return -1;
c3e23647 715
c3e23647 716 for (k = 0; match && k < t; k++)
796d61fb
AD
717 if (tos[j][k] != tos[i][k] || froms[j][k] != froms[i][k])
718 match = 0;
c3e23647
RS
719
720 if (match)
36281465 721 return j;
c3e23647
RS
722 }
723
36281465 724 return -1;
c3e23647
RS
725}
726
727
4a120d45 728static int
d2729d44 729pack_vector (int vector)
c3e23647 730{
837491d8 731 int i = order[vector];
6c89f1c1 732 int j;
837491d8 733 int t = tally[i];
6c89f1c1 734 int loc = 0;
837491d8
AD
735 short *from = froms[i];
736 short *to = tos[i];
c3e23647 737
340ef489 738 assert (t);
c3e23647 739
c3e23647
RS
740 for (j = lowzero - from[0]; j < MAXTABLE; j++)
741 {
837491d8
AD
742 int k;
743 int ok = 1;
c3e23647
RS
744
745 for (k = 0; ok && k < t; k++)
746 {
747 loc = j + from[k];
748 if (loc > MAXTABLE)
a0f6b076 749 fatal (_("maximum table size (%d) exceeded"), MAXTABLE);
c3e23647
RS
750
751 if (table[loc] != 0)
752 ok = 0;
753 }
754
755 for (k = 0; ok && k < vector; k++)
796d61fb
AD
756 if (pos[k] == j)
757 ok = 0;
c3e23647
RS
758
759 if (ok)
760 {
761 for (k = 0; k < t; k++)
762 {
763 loc = j + from[k];
764 table[loc] = to[k];
765 check[loc] = from[k];
766 }
767
768 while (table[lowzero] != 0)
769 lowzero++;
770
771 if (loc > high)
772 high = loc;
773
36281465 774 return j;
c3e23647
RS
775 }
776 }
275fc3ad
AD
777#define pack_vector_succeeded 0
778 assert (pack_vector_succeeded);
3843c413 779 return 0;
c3e23647
RS
780}
781
782
6c89f1c1
AD
783static void
784pack_table (void)
785{
786 int i;
787 int place;
788 int state;
789
d7913476
AD
790 base = XCALLOC (short, nvectors);
791 pos = XCALLOC (short, nentries);
792 table = XCALLOC (short, MAXTABLE);
793 check = XCALLOC (short, MAXTABLE);
6c89f1c1
AD
794
795 lowzero = 0;
796 high = 0;
797
798 for (i = 0; i < nvectors; i++)
799 base[i] = MINSHORT;
800
801 for (i = 0; i < MAXTABLE; i++)
802 check[i] = -1;
803
804 for (i = 0; i < nentries; i++)
805 {
806 state = matching_state (i);
807
808 if (state < 0)
809 place = pack_vector (i);
810 else
811 place = base[state];
812
813 pos[i] = place;
814 base[order[i]] = place;
815 }
816
817 for (i = 0; i < nvectors; i++)
818 {
796d61fb
AD
819 XFREE (froms[i]);
820 XFREE (tos[i]);
6c89f1c1
AD
821 }
822
d7913476
AD
823 XFREE (froms);
824 XFREE (tos);
825 XFREE (pos);
6c89f1c1 826}
c3e23647
RS
827
828/* the following functions output yytable, yycheck
829 and the vectors whose elements index the portion starts */
830
4a120d45 831static void
d2729d44 832output_base (void)
c3e23647 833{
26f609ff 834 /* Output pact. */
f87685c3 835 output_table_data (&format_obstack, base,
26f609ff 836 base[0], 1, nstates);
f87685c3 837 muscle_insert ("pact", obstack_finish (&format_obstack));
c3e23647 838
26f609ff 839 /* Output pgoto. */
f87685c3 840 output_table_data (&format_obstack, base,
26f609ff 841 base[nstates], nstates + 1, nvectors);
f87685c3 842 muscle_insert ("pgoto", obstack_finish (&format_obstack));
c3e23647 843
d7913476 844 XFREE (base);
c3e23647
RS
845}
846
847
4a120d45 848static void
d2729d44 849output_table (void)
c3e23647 850{
f87685c3 851 output_table_data (&format_obstack, table,
26f609ff 852 table[0], 1, high + 1);
f87685c3 853 muscle_insert ("table", obstack_finish (&format_obstack));
d7913476 854 XFREE (table);
c3e23647
RS
855}
856
857
4a120d45 858static void
d2729d44 859output_check (void)
c3e23647 860{
f87685c3 861 output_table_data (&format_obstack, check,
26f609ff 862 check[0], 1, high + 1);
f87685c3 863 muscle_insert ("check", obstack_finish (&format_obstack));
d7913476 864 XFREE (check);
c3e23647
RS
865}
866
6c89f1c1
AD
867/* compute and output yydefact, yydefgoto, yypact, yypgoto, yytable
868 and yycheck. */
869
870static void
871output_actions (void)
872{
49701457 873 int i;
6c89f1c1 874 nvectors = nstates + nvars;
c3e23647 875
d7913476
AD
876 froms = XCALLOC (short *, nvectors);
877 tos = XCALLOC (short *, nvectors);
878 tally = XCALLOC (short, nvectors);
879 width = XCALLOC (short, nvectors);
6c89f1c1
AD
880
881 token_actions ();
d7913476
AD
882 XFREE (LA);
883 XFREE (LAruleno);
6c89f1c1
AD
884
885 goto_actions ();
d7913476
AD
886 XFREE (goto_map + ntokens);
887 XFREE (from_state);
888 XFREE (to_state);
6c89f1c1
AD
889
890 sort_actions ();
891 pack_table ();
4e5caae2 892
6c89f1c1
AD
893 output_base ();
894 output_table ();
4e5caae2 895
6c89f1c1 896 output_check ();
49701457
AD
897
898 for (i = 0; i < nstates; ++i)
899 {
2cec70b9 900 free (state_table[i]->shifts);
49701457 901 XFREE (state_table[i]->reductions);
2cec70b9 902 free (state_table[i]->errs);
49701457
AD
903 free (state_table[i]);
904 }
9703cc49 905 XFREE (state_table);
6c89f1c1 906}
c3e23647 907
652def80
MA
908\f
909/*------------------------------------------------------------.
910| Copy the parser code from SKEL_FILENAME into OOUT obstack. |
911| and do the muscle substitution. |
912`------------------------------------------------------------*/
c3e23647 913
4a120d45 914static void
ea52d706 915output_parser (const char *skel_filename, FILE *out)
c3e23647 916{
6c89f1c1 917 int c;
ff61dabd 918 FILE *fskel;
897668ee
MA
919 size_t output_line;
920 size_t skeleton_line;
c3e23647 921
652def80 922 fskel = xfopen (skel_filename, "r");
ef7ddedd 923
e8cb70b9 924 /* New output code. */
897668ee
MA
925 output_line = 1;
926 skeleton_line = 1;
26f609ff
RA
927 c = getc (fskel);
928 while (c != EOF)
c3e23647 929 {
26f609ff 930 if (c != '%')
573c1d9f 931 {
26f609ff 932 if (c == '\n')
897668ee
MA
933 {
934 ++output_line;
935 ++skeleton_line;
936 }
ea52d706 937 putc (c, out);
26f609ff 938 c = getc (fskel);
bbb5bcc6 939 }
26f609ff 940 else if ((c = getc (fskel)) == '%')
bbb5bcc6 941 {
11d82f03
MA
942 /* Read the muscle. */
943 const char *muscle_key = 0;
944 const char *muscle_value = 0;
652def80 945
5b5d1929 946 while (isalnum (c = getc (fskel)) || c == '-')
11d82f03
MA
947 obstack_1grow (&muscle_obstack, c);
948 obstack_1grow (&muscle_obstack, 0);
26f609ff 949
e8cb70b9 950 /* Output the right value, or see if it's something special. */
11d82f03
MA
951 muscle_key = obstack_finish (&muscle_obstack);
952 muscle_value = muscle_find (muscle_key);
3f96f4dc 953 if (!strcmp (muscle_key, "actions"))
897668ee 954 actions_output (out, &output_line);
f499b062
AD
955 else if (!strcmp (muscle_key, "guards"))
956 guards_output (out, &output_line);
11d82f03 957 else if (!strcmp (muscle_key, "line"))
897668ee
MA
958 fprintf (out, "%d", output_line);
959 else if (!strcmp (muscle_key, "skeleton-line"))
960 fprintf (out, "%d", skeleton_line);
3f96f4dc 961 else if (muscle_value)
f0440388
MA
962 {
963 fputs (muscle_value, out);
897668ee 964 output_line += get_lines_number (muscle_value);
1fa14068 965 }
bbb5bcc6 966 else
26f609ff 967 {
ea52d706
AD
968 fputs ("%%", out);
969 fputs (muscle_key, out);
26f609ff 970 }
573c1d9f 971 }
b0cfa28a 972 else
ea52d706 973 putc ('%', out);
bbb5bcc6 974 }
26f609ff 975
e8cb70b9 976 /* End. */
ff61dabd 977 xfclose (fskel);
c3e23647
RS
978}
979
652def80
MA
980/*----------------------------------------.
981| Prepare the master parser to be output |
982`----------------------------------------*/
983
984static void
985output_master_parser (void)
986{
ea52d706 987 FILE *parser = xfopen (parser_file_name, "w");
652def80
MA
988 if (!skeleton)
989 {
990 if (semantic_parser)
991 skeleton = skeleton_find ("BISON_HAIRY", BISON_HAIRY);
992 else
993 skeleton = skeleton_find ("BISON_SIMPLE", BISON_SIMPLE);
994 }
3843c413 995 muscle_insert ("skeleton", skeleton);
f0440388 996 muscle_insert ("parser-file-name", parser_file_name);
ea52d706
AD
997
998 output_parser (skeleton, parser);
999 xfclose (parser);
652def80
MA
1000}
1001
1002
26f609ff
RA
1003/* FIXME. */
1004
11d82f03
MA
1005#define MUSCLE_INSERT_INT(Key, Value) \
1006{ \
1007 obstack_fgrow1 (&muscle_obstack, "%d", Value); \
1008 obstack_1grow (&muscle_obstack, 0); \
1009 muscle_insert (Key, obstack_finish (&muscle_obstack)); \
26f609ff
RA
1010}
1011
11d82f03
MA
1012#define MUSCLE_INSERT_STRING(Key, Value) \
1013{ \
1014 obstack_sgrow (&muscle_obstack, Value); \
1015 obstack_1grow (&muscle_obstack, 0); \
1016 muscle_insert (Key, obstack_finish (&muscle_obstack)); \
26f609ff
RA
1017}
1018
11d82f03 1019#define MUSCLE_INSERT_PREFIX(Key, Value) \
26f609ff 1020{ \
11d82f03
MA
1021 obstack_fgrow2 (&muscle_obstack, "%s%s", spec_name_prefix, Value); \
1022 obstack_1grow (&muscle_obstack, 0); \
1023 muscle_insert (Key, obstack_finish (&muscle_obstack)); \
26f609ff
RA
1024}
1025
1026static void
1027prepare (void)
1028{
11d82f03
MA
1029 MUSCLE_INSERT_INT ("last", high);
1030 MUSCLE_INSERT_INT ("flag", MINSHORT);
1031 MUSCLE_INSERT_INT ("pure", pure_parser);
1032 MUSCLE_INSERT_INT ("nsym", nsyms);
1033 MUSCLE_INSERT_INT ("debug", debug_flag);
1034 MUSCLE_INSERT_INT ("final", final_state);
1035 MUSCLE_INSERT_INT ("maxtok", max_user_token_number);
c7925b99 1036 MUSCLE_INSERT_INT ("error-verbose", error_verbose);
78af9bbc 1037 MUSCLE_INSERT_STRING ("prefix", spec_name_prefix);
11d82f03
MA
1038
1039 MUSCLE_INSERT_INT ("nnts", nvars);
1040 MUSCLE_INSERT_INT ("nrules", nrules);
1041 MUSCLE_INSERT_INT ("nstates", nstates);
1042 MUSCLE_INSERT_INT ("ntokens", ntokens);
1043
5b5d1929 1044 MUSCLE_INSERT_INT ("locations-flag", locations_flag);
26f609ff 1045}
c3e23647 1046
93ede233
AD
1047
1048/*-------------------------.
1049| Output the header file. |
1050`-------------------------*/
1051
1052static void
1053header_output (void)
1054{
1055 FILE *out = xfopen (spec_defines_file, "w");
1056 char *macro_name = compute_header_macro ();
1057
1058 fprintf (out, "#ifndef %s\n", macro_name);
1059 fprintf (out, "# define %s\n\n", macro_name);
1060
1061 fputs (muscle_find ("tokendef"), out);
1062 fprintf (out, "\
1063#ifndef YYSTYPE\n\
1064typedef %s
1065yystype;\n\
1066# define YYSTYPE yystype\n\
1067#endif\n",
1068 muscle_find ("stype"));
1069
1070 if (!pure_parser)
1071 fprintf (out, "\nextern YYSTYPE %slval;\n",
1072 spec_name_prefix);
1073 if (semantic_parser)
1074 {
1075 int i;
1076
1077 for (i = ntokens; i < nsyms; i++)
1078 /* don't make these for dummy nonterminals made by gensym. */
ad949da9
AD
1079 if (*symbols[i]->tag != '@')
1080 fprintf (out, "# define NT%s\t%d\n", symbols[i]->tag, i);
93ede233
AD
1081 }
1082
1083 fprintf (out, "\n#endif /* not %s */\n", macro_name);
1084 free (macro_name);
1085 xfclose (out);
1086}
1087
1088
6c89f1c1
AD
1089/*----------------------------------------------------------.
1090| Output the parsing tables and the parser code to ftable. |
1091`----------------------------------------------------------*/
c3e23647 1092
6c89f1c1
AD
1093void
1094output (void)
1095{
f87685c3 1096 obstack_init (&format_obstack);
dd60faec 1097
6c89f1c1 1098 output_token_translations ();
6c89f1c1 1099 output_gram ();
26f609ff 1100
d7913476 1101 XFREE (ritem);
6c89f1c1
AD
1102 if (semantic_parser)
1103 output_stos ();
1104 output_rule_data ();
0846f581 1105 XFREE (user_toknums);
6c89f1c1 1106 output_actions ();
342b8b6e 1107
26f609ff 1108 prepare ();
d1a2daf7 1109 /* Copy definitions in directive. */
5449dd0f 1110 obstack_1grow (&attrs_obstack, 0);
11d82f03 1111 muscle_insert ("prologue", obstack_finish (&attrs_obstack));
652def80 1112
93ede233 1113 /* Output the parser. */
652def80 1114 output_master_parser ();
93ede233
AD
1115 /* Output the header if needed. */
1116 if (defines_flag)
1117 header_output ();
26f609ff 1118
3f96f4dc 1119 free (rule_table + 1);
f499b062
AD
1120 obstack_free (&muscle_obstack, NULL);
1121 obstack_free (&format_obstack, NULL);
1122 obstack_free (&action_obstack, NULL);
f499b062 1123 obstack_free (&attrs_obstack, NULL);
c3e23647 1124}