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