]> git.saurik.com Git - bison.git/blame - src/output.c
* src/files.c (output_files): Free the output_obstack.
[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;
26f609ff 124struct obstack output_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;
133
134 size_t i;
135 for (i = 0; s[i]; ++i)
136 {
137 if (s[i] == '\n')
138 ++lines;
139 }
140
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,
151 short begin,
26f609ff 152 short 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{
342b8b6e 177 output_table_data (&output_obstack, token_translations,
26f609ff 178 0, 1, max_user_token_number + 1);
11d82f03 179 muscle_insert ("translate", obstack_finish (&output_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;
192 output_table_data (&output_obstack, values,
193 0, 1, nrules + 1);
194 XFREE (values);
195 }
196
11d82f03 197 muscle_insert ("prhs", obstack_finish (&output_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
342b8b6e 211 output_table_data (&output_obstack, yyrhs,
26f609ff 212 ritem[0], 1, yyrhs_size);
11d82f03 213 muscle_insert ("rhs", obstack_finish (&output_obstack));
26f609ff 214
1e9798d5
AD
215 XFREE (yyrhs);
216 }
4ec8e00f
MA
217
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;
9703cc49 232 output_table_data (&output_obstack, values,
26f609ff 233 0, 1, nstates);
11d82f03 234 muscle_insert ("stos", obstack_finish (&output_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;
249 output_table_data (&output_obstack, values,
250 0, 1, nrules + 1);
251 muscle_insert ("rline", obstack_finish (&output_obstack));
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 {
26f609ff 269 obstack_sgrow (&output_obstack, "\n ");
1e9798d5 270 j = 2;
c3e23647
RS
271 }
272
92790e5b
AD
273 obstack_sgrow (&output_obstack, cp);
274 obstack_sgrow (&output_obstack, ", ");
1e9798d5 275 j += strsize;
c3e23647 276 }
9ee3c97b 277 /* add a NULL entry to list of tokens */
26f609ff 278 obstack_sgrow (&output_obstack, "NULL");
c3e23647 279
26f609ff
RA
280 /* Finish table and store. */
281 obstack_1grow (&output_obstack, 0);
11d82f03 282 muscle_insert ("tname", obstack_finish (&output_obstack));
e372befa 283
9ee3c97b 284 /* Output YYTOKNUM. */
26f609ff
RA
285 output_table_data (&output_obstack, user_toknums,
286 0, 1, ntokens + 1);
11d82f03 287 muscle_insert ("toknum", obstack_finish (&output_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;
294 output_table_data (&output_obstack, values,
295 0, 1, nrules + 1);
296 muscle_insert ("r1", obstack_finish (&output_obstack));
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;
342b8b6e 305 output_table_data (&output_obstack, short_tab,
26f609ff 306 0, 1, nrules + 1);
11d82f03 307 muscle_insert ("r2", obstack_finish (&output_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
d2729d44 326action_row (int state)
c3e23647 327{
6c89f1c1
AD
328 int i;
329 int j;
330 int k;
331 int m = 0;
332 int n = 0;
6c89f1c1
AD
333 int default_rule;
334 int nreds;
6c89f1c1
AD
335 int rule;
336 int shift_state;
337 int symbol;
6c89f1c1
AD
338 reductions *redp;
339 shifts *shiftp;
340 errs *errp;
ceed8467 341 int nodefault = 0; /* set nonzero to inhibit having any default reduction */
c3e23647
RS
342
343 for (i = 0; i < ntokens; i++)
344 actrow[i] = 0;
345
346 default_rule = 0;
347 nreds = 0;
f693ad14 348 redp = state_table[state]->reductions;
c3e23647
RS
349
350 if (redp)
351 {
352 nreds = redp->nreds;
353
354 if (nreds >= 1)
355 {
36281465
AD
356 /* loop over all the rules available here which require
357 lookahead */
f693ad14
AD
358 m = state_table[state]->lookaheads;
359 n = state_table[state + 1]->lookaheads;
c3e23647
RS
360
361 for (i = n - 1; i >= m; i--)
7a5350ba
AD
362 /* and find each token which the rule finds acceptable
363 to come next */
364 for (j = 0; j < ntokens; j++)
365 /* and record this rule as the rule to use if that
366 token follows. */
367 if (BITISSET (LA (i), j))
368 actrow[j] = -LAruleno[i];
c3e23647
RS
369 }
370 }
371
36281465
AD
372 /* Now see which tokens are allowed for shifts in this state. For
373 them, record the shift as the thing to do. So shift is preferred
374 to reduce. */
f693ad14 375 shiftp = state_table[state]->shifts;
d954473d 376 for (i = 0; i < shiftp->nshifts; i++)
c3e23647 377 {
d954473d
AD
378 shift_state = shiftp->shifts[i];
379 if (!shift_state)
380 continue;
c3e23647 381
f693ad14 382 symbol = state_table[shift_state]->accessing_symbol;
c3e23647 383
d954473d
AD
384 if (ISVAR (symbol))
385 break;
c3e23647 386
d954473d 387 actrow[symbol] = shift_state;
c3e23647 388
d954473d
AD
389 /* Do not use any default reduction if there is a shift for
390 error */
391 if (symbol == error_token_number)
392 nodefault = 1;
c3e23647
RS
393 }
394
36281465
AD
395 /* See which tokens are an explicit error in this state (due to
396 %nonassoc). For them, record MINSHORT as the action. */
f693ad14 397 errp = state_table[state]->errs;
c3e23647
RS
398
399 if (errp)
400 {
401 k = errp->nerrs;
402
403 for (i = 0; i < k; i++)
404 {
405 symbol = errp->errs[i];
406 actrow[symbol] = MINSHORT;
407 }
408 }
409
36281465
AD
410 /* Now find the most common reduction and make it the default action
411 for this state. */
c3e23647 412
ceed8467 413 if (nreds >= 1 && !nodefault)
c3e23647 414 {
f693ad14 415 if (state_table[state]->consistent)
c3e23647
RS
416 default_rule = redp->rules[0];
417 else
418 {
7a5350ba 419 int max = 0;
c3e23647
RS
420 for (i = m; i < n; i++)
421 {
7a5350ba 422 int count = 0;
ceed8467 423 rule = -LAruleno[i];
9ee3c97b 424
c3e23647
RS
425 for (j = 0; j < ntokens; j++)
426 {
427 if (actrow[j] == rule)
428 count++;
429 }
9ee3c97b 430
c3e23647
RS
431 if (count > max)
432 {
433 max = count;
434 default_rule = rule;
435 }
436 }
9ee3c97b 437
c3e23647
RS
438 /* actions which match the default are replaced with zero,
439 which means "use the default" */
9ee3c97b 440
c3e23647
RS
441 if (max > 0)
442 {
443 for (j = 0; j < ntokens; j++)
444 {
445 if (actrow[j] == default_rule)
446 actrow[j] = 0;
447 }
9ee3c97b 448
ceed8467 449 default_rule = -default_rule;
c3e23647
RS
450 }
451 }
452 }
453
454 /* If have no default rule, the default is an error.
455 So replace any action which says "error" with "use default". */
456
457 if (default_rule == 0)
458 for (j = 0; j < ntokens; j++)
459 {
460 if (actrow[j] == MINSHORT)
461 actrow[j] = 0;
462 }
463
36281465 464 return default_rule;
c3e23647
RS
465}
466
467
4a120d45 468static void
d2729d44 469save_row (int state)
c3e23647 470{
6c89f1c1
AD
471 int i;
472 int count;
473 short *sp;
474 short *sp1;
475 short *sp2;
c3e23647
RS
476
477 count = 0;
478 for (i = 0; i < ntokens; i++)
479 {
480 if (actrow[i] != 0)
481 count++;
482 }
483
484 if (count == 0)
485 return;
486
d7913476
AD
487 froms[state] = sp1 = sp = XCALLOC (short, count);
488 tos[state] = sp2 = XCALLOC (short, count);
c3e23647
RS
489
490 for (i = 0; i < ntokens; i++)
491 {
492 if (actrow[i] != 0)
493 {
494 *sp1++ = i;
495 *sp2++ = actrow[i];
496 }
497 }
498
499 tally[state] = count;
500 width[state] = sp1[-1] - sp[0] + 1;
501}
502
503
6c89f1c1
AD
504/*------------------------------------------------------------------.
505| Figure out the actions for the specified state, indexed by |
506| lookahead token type. |
507| |
f2acea59
AD
508| The YYDEFACT table is output now. The detailed info is saved for |
509| putting into YYTABLE later. |
6c89f1c1 510`------------------------------------------------------------------*/
c3e23647 511
4a120d45 512static void
6c89f1c1 513token_actions (void)
c3e23647 514{
6c89f1c1 515 int i;
d7913476 516 short *yydefact = XCALLOC (short, nstates);
c3e23647 517
d7913476 518 actrow = XCALLOC (short, ntokens);
f2acea59 519 for (i = 0; i < nstates; ++i)
c3e23647 520 {
f2acea59 521 yydefact[i] = action_row (i);
6c89f1c1 522 save_row (i);
c3e23647 523 }
bbb5bcc6 524
342b8b6e 525 output_table_data (&output_obstack, yydefact,
26f609ff 526 yydefact[0], 1, nstates);
11d82f03 527 muscle_insert ("defact", obstack_finish (&output_obstack));
342b8b6e 528
26f609ff 529 XFREE (actrow);
d7913476 530 XFREE (yydefact);
c3e23647
RS
531}
532
533
3f96f4dc
AD
534/*-----------------------------.
535| Output the actions to OOUT. |
536`-----------------------------*/
537
538static void
f0440388 539actions_output (FILE *out, size_t *line)
3f96f4dc
AD
540{
541 int rule;
542 for (rule = 1; rule < nrules + 1; ++rule)
543 if (rule_table[rule].action)
544 {
ea52d706 545 fprintf (out, " case %d:\n", rule);
3f96f4dc
AD
546
547 if (!no_lines_flag)
ea52d706
AD
548 fprintf (out, muscle_find ("linef"),
549 rule_table[rule].action_line,
550 quotearg_style (c_quoting_style,
551 muscle_find ("filename")));
3f96f4dc
AD
552 /* As a Bison extension, add the ending semicolon. Since some
553 Yacc don't do that, help people using bison as a Yacc
554 finding their missing semicolons. */
ea52d706
AD
555 fprintf (out, "{ %s%s }\n break;\n\n",
556 rule_table[rule].action,
557 yacc_flag ? ";" : "");
f0440388 558
fbc8ecb7
MA
559 /* We always output 4 '\n' per action. */
560 *line += 4;
561 /* Plus one if !no_lines_flag. */
562 if (!no_lines_flag)
563 ++*line;
f0440388
MA
564 /* Get the number of lines written by the user. */
565 *line += get_lines_number (rule_table[rule].action);
3f96f4dc
AD
566 }
567}
568
569
4a120d45 570static void
d2729d44 571save_column (int symbol, int default_state)
c3e23647 572{
6c89f1c1 573 int i;
6c89f1c1
AD
574 short *sp;
575 short *sp1;
576 short *sp2;
577 int count;
578 int symno;
c3e23647 579
43591cec
AD
580 short begin = goto_map[symbol];
581 short end = goto_map[symbol + 1];
c3e23647
RS
582
583 count = 0;
43591cec 584 for (i = begin; i < end; i++)
c3e23647
RS
585 {
586 if (to_state[i] != default_state)
587 count++;
588 }
589
590 if (count == 0)
591 return;
592
593 symno = symbol - ntokens + nstates;
594
d7913476
AD
595 froms[symno] = sp1 = sp = XCALLOC (short, count);
596 tos[symno] = sp2 = XCALLOC (short, count);
c3e23647 597
43591cec 598 for (i = begin; i < end; i++)
c3e23647
RS
599 {
600 if (to_state[i] != default_state)
601 {
602 *sp1++ = from_state[i];
603 *sp2++ = to_state[i];
604 }
605 }
606
607 tally[symno] = count;
608 width[symno] = sp1[-1] - sp[0] + 1;
609}
610
6c89f1c1
AD
611static int
612default_goto (int symbol)
613{
614 int i;
615 int m;
616 int n;
617 int default_state;
618 int max;
619
620 m = goto_map[symbol];
621 n = goto_map[symbol + 1];
622
623 if (m == n)
624 return -1;
625
626 for (i = 0; i < nstates; i++)
627 state_count[i] = 0;
628
629 for (i = m; i < n; i++)
630 state_count[to_state[i]]++;
631
632 max = 0;
633 default_state = -1;
634
635 for (i = 0; i < nstates; i++)
636 {
637 if (state_count[i] > max)
638 {
639 max = state_count[i];
640 default_state = i;
641 }
642 }
643
644 return default_state;
645}
646
647
648/*-------------------------------------------------------------------.
649| Figure out what to do after reducing with each rule, depending on |
650| the saved state from before the beginning of parsing the data that |
651| matched this rule. |
652| |
653| The YYDEFGOTO table is output now. The detailed info is saved for |
654| putting into YYTABLE later. |
655`-------------------------------------------------------------------*/
656
657static void
658goto_actions (void)
659{
43591cec 660 int i;
bbb5bcc6 661 short *yydefgoto = XMALLOC (short, nsyms - ntokens);
bbb5bcc6 662
26f609ff 663 state_count = XCALLOC (short, nstates);
43591cec 664 for (i = ntokens; i < nsyms; ++i)
6c89f1c1 665 {
43591cec
AD
666 int default_state = default_goto (i);
667 save_column (i, default_state);
668 yydefgoto[i - ntokens] = default_state;
6c89f1c1
AD
669 }
670
342b8b6e 671 output_table_data (&output_obstack, yydefgoto,
26f609ff 672 yydefgoto[0], 1, nsyms - ntokens);
11d82f03 673 muscle_insert ("defgoto", obstack_finish (&output_obstack));
43591cec 674
d7913476 675 XFREE (state_count);
43591cec 676 XFREE (yydefgoto);
6c89f1c1 677}
c3e23647
RS
678
679
9ee3c97b
AD
680/* The next few functions decide how to pack the actions and gotos
681 information into yytable. */
c3e23647 682
4a120d45 683static void
d2729d44 684sort_actions (void)
c3e23647 685{
6c89f1c1
AD
686 int i;
687 int j;
688 int k;
689 int t;
690 int w;
c3e23647 691
d7913476 692 order = XCALLOC (short, nvectors);
c3e23647
RS
693 nentries = 0;
694
695 for (i = 0; i < nvectors; i++)
696 {
697 if (tally[i] > 0)
698 {
699 t = tally[i];
700 w = width[i];
701 j = nentries - 1;
702
703 while (j >= 0 && (width[order[j]] < w))
704 j--;
705
706 while (j >= 0 && (width[order[j]] == w) && (tally[order[j]] < t))
707 j--;
708
709 for (k = nentries - 1; k > j; k--)
710 order[k + 1] = order[k];
711
712 order[j + 1] = i;
713 nentries++;
714 }
715 }
716}
717
718
4a120d45 719static int
d2729d44 720matching_state (int vector)
c3e23647 721{
6c89f1c1
AD
722 int i;
723 int j;
724 int k;
725 int t;
726 int w;
727 int match;
728 int prev;
c3e23647
RS
729
730 i = order[vector];
731 if (i >= nstates)
36281465 732 return -1;
c3e23647
RS
733
734 t = tally[i];
735 w = width[i];
736
737 for (prev = vector - 1; prev >= 0; prev--)
738 {
739 j = order[prev];
740 if (width[j] != w || tally[j] != t)
36281465 741 return -1;
c3e23647
RS
742
743 match = 1;
744 for (k = 0; match && k < t; k++)
745 {
746 if (tos[j][k] != tos[i][k] || froms[j][k] != froms[i][k])
747 match = 0;
748 }
749
750 if (match)
36281465 751 return j;
c3e23647
RS
752 }
753
36281465 754 return -1;
c3e23647
RS
755}
756
757
4a120d45 758static int
d2729d44 759pack_vector (int vector)
c3e23647 760{
6c89f1c1
AD
761 int i;
762 int j;
763 int k;
764 int t;
765 int loc = 0;
766 int ok;
767 short *from;
768 short *to;
c3e23647
RS
769
770 i = order[vector];
771 t = tally[i];
772
340ef489 773 assert (t);
c3e23647
RS
774
775 from = froms[i];
776 to = tos[i];
777
778 for (j = lowzero - from[0]; j < MAXTABLE; j++)
779 {
780 ok = 1;
781
782 for (k = 0; ok && k < t; k++)
783 {
784 loc = j + from[k];
785 if (loc > MAXTABLE)
a0f6b076 786 fatal (_("maximum table size (%d) exceeded"), MAXTABLE);
c3e23647
RS
787
788 if (table[loc] != 0)
789 ok = 0;
790 }
791
792 for (k = 0; ok && k < vector; k++)
793 {
794 if (pos[k] == j)
795 ok = 0;
796 }
797
798 if (ok)
799 {
800 for (k = 0; k < t; k++)
801 {
802 loc = j + from[k];
803 table[loc] = to[k];
804 check[loc] = from[k];
805 }
806
807 while (table[lowzero] != 0)
808 lowzero++;
809
810 if (loc > high)
811 high = loc;
812
36281465 813 return j;
c3e23647
RS
814 }
815 }
816
3843c413
AD
817 assert (!"pack_vector");
818 return 0;
c3e23647
RS
819}
820
821
6c89f1c1
AD
822static void
823pack_table (void)
824{
825 int i;
826 int place;
827 int state;
828
d7913476
AD
829 base = XCALLOC (short, nvectors);
830 pos = XCALLOC (short, nentries);
831 table = XCALLOC (short, MAXTABLE);
832 check = XCALLOC (short, MAXTABLE);
6c89f1c1
AD
833
834 lowzero = 0;
835 high = 0;
836
837 for (i = 0; i < nvectors; i++)
838 base[i] = MINSHORT;
839
840 for (i = 0; i < MAXTABLE; i++)
841 check[i] = -1;
842
843 for (i = 0; i < nentries; i++)
844 {
845 state = matching_state (i);
846
847 if (state < 0)
848 place = pack_vector (i);
849 else
850 place = base[state];
851
852 pos[i] = place;
853 base[order[i]] = place;
854 }
855
856 for (i = 0; i < nvectors; i++)
857 {
858 if (froms[i])
d7913476 859 XFREE (froms[i]);
6c89f1c1 860 if (tos[i])
d7913476 861 XFREE (tos[i]);
6c89f1c1
AD
862 }
863
d7913476
AD
864 XFREE (froms);
865 XFREE (tos);
866 XFREE (pos);
6c89f1c1 867}
c3e23647
RS
868
869/* the following functions output yytable, yycheck
870 and the vectors whose elements index the portion starts */
871
4a120d45 872static void
d2729d44 873output_base (void)
c3e23647 874{
26f609ff 875 /* Output pact. */
342b8b6e 876 output_table_data (&output_obstack, base,
26f609ff 877 base[0], 1, nstates);
11d82f03 878 muscle_insert ("pact", obstack_finish (&output_obstack));
c3e23647 879
26f609ff 880 /* Output pgoto. */
342b8b6e 881 output_table_data (&output_obstack, base,
26f609ff 882 base[nstates], nstates + 1, nvectors);
11d82f03 883 muscle_insert ("pgoto", obstack_finish (&output_obstack));
c3e23647 884
d7913476 885 XFREE (base);
c3e23647
RS
886}
887
888
4a120d45 889static void
d2729d44 890output_table (void)
c3e23647 891{
342b8b6e 892 output_table_data (&output_obstack, table,
26f609ff 893 table[0], 1, high + 1);
11d82f03 894 muscle_insert ("table", obstack_finish (&output_obstack));
d7913476 895 XFREE (table);
c3e23647
RS
896}
897
898
4a120d45 899static void
d2729d44 900output_check (void)
c3e23647 901{
342b8b6e 902 output_table_data (&output_obstack, check,
26f609ff 903 check[0], 1, high + 1);
11d82f03 904 muscle_insert ("check", obstack_finish (&output_obstack));
d7913476 905 XFREE (check);
c3e23647
RS
906}
907
6c89f1c1
AD
908/* compute and output yydefact, yydefgoto, yypact, yypgoto, yytable
909 and yycheck. */
910
911static void
912output_actions (void)
913{
49701457 914 int i;
6c89f1c1 915 nvectors = nstates + nvars;
c3e23647 916
d7913476
AD
917 froms = XCALLOC (short *, nvectors);
918 tos = XCALLOC (short *, nvectors);
919 tally = XCALLOC (short, nvectors);
920 width = XCALLOC (short, nvectors);
6c89f1c1
AD
921
922 token_actions ();
d7913476
AD
923 XFREE (LA);
924 XFREE (LAruleno);
6c89f1c1
AD
925
926 goto_actions ();
d7913476
AD
927 XFREE (goto_map + ntokens);
928 XFREE (from_state);
929 XFREE (to_state);
6c89f1c1
AD
930
931 sort_actions ();
932 pack_table ();
4e5caae2 933
6c89f1c1
AD
934 output_base ();
935 output_table ();
4e5caae2 936
6c89f1c1 937 output_check ();
49701457
AD
938
939 for (i = 0; i < nstates; ++i)
940 {
941 XFREE (state_table[i]->shifts);
942 XFREE (state_table[i]->reductions);
943 XFREE (state_table[i]->errs);
944 free (state_table[i]);
945 }
9703cc49 946 XFREE (state_table);
6c89f1c1 947}
c3e23647 948
652def80
MA
949\f
950/*------------------------------------------------------------.
951| Copy the parser code from SKEL_FILENAME into OOUT obstack. |
952| and do the muscle substitution. |
953`------------------------------------------------------------*/
c3e23647 954
4a120d45 955static void
ea52d706 956output_parser (const char *skel_filename, FILE *out)
c3e23647 957{
6c89f1c1 958 int c;
ff61dabd 959 FILE *fskel;
ef7ddedd 960 size_t line;
c3e23647 961
652def80 962 fskel = xfopen (skel_filename, "r");
ef7ddedd 963
e8cb70b9 964 /* New output code. */
26f609ff
RA
965 line = 1;
966 c = getc (fskel);
967 while (c != EOF)
c3e23647 968 {
26f609ff 969 if (c != '%')
573c1d9f 970 {
26f609ff
RA
971 if (c == '\n')
972 ++line;
ea52d706 973 putc (c, out);
26f609ff 974 c = getc (fskel);
bbb5bcc6 975 }
26f609ff 976 else if ((c = getc (fskel)) == '%')
bbb5bcc6 977 {
11d82f03
MA
978 /* Read the muscle. */
979 const char *muscle_key = 0;
980 const char *muscle_value = 0;
652def80 981
5b5d1929 982 while (isalnum (c = getc (fskel)) || c == '-')
11d82f03
MA
983 obstack_1grow (&muscle_obstack, c);
984 obstack_1grow (&muscle_obstack, 0);
26f609ff 985
e8cb70b9 986 /* Output the right value, or see if it's something special. */
11d82f03
MA
987 muscle_key = obstack_finish (&muscle_obstack);
988 muscle_value = muscle_find (muscle_key);
3f96f4dc 989 if (!strcmp (muscle_key, "actions"))
f0440388 990 actions_output (out, &line);
11d82f03 991 else if (!strcmp (muscle_key, "line"))
f0440388 992 fprintf (out, "%d", line);
3f96f4dc 993 else if (muscle_value)
f0440388
MA
994 {
995 fputs (muscle_value, out);
996 line += get_lines_number (muscle_value);
997 }
bbb5bcc6 998 else
26f609ff 999 {
ea52d706
AD
1000 fputs ("%%", out);
1001 fputs (muscle_key, out);
26f609ff 1002 }
573c1d9f 1003 }
b0cfa28a 1004 else
ea52d706 1005 putc ('%', out);
bbb5bcc6 1006 }
26f609ff 1007
e8cb70b9 1008 /* End. */
ff61dabd 1009 xfclose (fskel);
c3e23647
RS
1010}
1011
652def80
MA
1012/*----------------------------------------.
1013| Prepare the master parser to be output |
1014`----------------------------------------*/
1015
1016static void
1017output_master_parser (void)
1018{
ea52d706 1019 FILE *parser = xfopen (parser_file_name, "w");
652def80
MA
1020 if (!skeleton)
1021 {
1022 if (semantic_parser)
1023 skeleton = skeleton_find ("BISON_HAIRY", BISON_HAIRY);
1024 else
1025 skeleton = skeleton_find ("BISON_SIMPLE", BISON_SIMPLE);
1026 }
3843c413 1027 muscle_insert ("skeleton", skeleton);
f0440388 1028 muscle_insert ("parser-file-name", parser_file_name);
ea52d706
AD
1029
1030 output_parser (skeleton, parser);
1031 xfclose (parser);
652def80
MA
1032}
1033
1034
26f609ff
RA
1035/* FIXME. */
1036
11d82f03
MA
1037#define MUSCLE_INSERT_INT(Key, Value) \
1038{ \
1039 obstack_fgrow1 (&muscle_obstack, "%d", Value); \
1040 obstack_1grow (&muscle_obstack, 0); \
1041 muscle_insert (Key, obstack_finish (&muscle_obstack)); \
26f609ff
RA
1042}
1043
11d82f03
MA
1044#define MUSCLE_INSERT_STRING(Key, Value) \
1045{ \
1046 obstack_sgrow (&muscle_obstack, Value); \
1047 obstack_1grow (&muscle_obstack, 0); \
1048 muscle_insert (Key, obstack_finish (&muscle_obstack)); \
26f609ff
RA
1049}
1050
11d82f03 1051#define MUSCLE_INSERT_PREFIX(Key, Value) \
26f609ff 1052{ \
11d82f03
MA
1053 obstack_fgrow2 (&muscle_obstack, "%s%s", spec_name_prefix, Value); \
1054 obstack_1grow (&muscle_obstack, 0); \
1055 muscle_insert (Key, obstack_finish (&muscle_obstack)); \
26f609ff
RA
1056}
1057
1058static void
1059prepare (void)
1060{
11d82f03
MA
1061 MUSCLE_INSERT_INT ("last", high);
1062 MUSCLE_INSERT_INT ("flag", MINSHORT);
1063 MUSCLE_INSERT_INT ("pure", pure_parser);
1064 MUSCLE_INSERT_INT ("nsym", nsyms);
1065 MUSCLE_INSERT_INT ("debug", debug_flag);
1066 MUSCLE_INSERT_INT ("final", final_state);
1067 MUSCLE_INSERT_INT ("maxtok", max_user_token_number);
1068 MUSCLE_INSERT_INT ("ntbase", ntokens);
c7925b99 1069 MUSCLE_INSERT_INT ("error-verbose", error_verbose);
78af9bbc 1070 MUSCLE_INSERT_STRING ("prefix", spec_name_prefix);
11d82f03
MA
1071
1072 MUSCLE_INSERT_INT ("nnts", nvars);
1073 MUSCLE_INSERT_INT ("nrules", nrules);
1074 MUSCLE_INSERT_INT ("nstates", nstates);
1075 MUSCLE_INSERT_INT ("ntokens", ntokens);
1076
5b5d1929 1077 MUSCLE_INSERT_INT ("locations-flag", locations_flag);
26f609ff 1078}
c3e23647 1079
93ede233
AD
1080
1081/*-------------------------.
1082| Output the header file. |
1083`-------------------------*/
1084
1085static void
1086header_output (void)
1087{
1088 FILE *out = xfopen (spec_defines_file, "w");
1089 char *macro_name = compute_header_macro ();
1090
1091 fprintf (out, "#ifndef %s\n", macro_name);
1092 fprintf (out, "# define %s\n\n", macro_name);
1093
1094 fputs (muscle_find ("tokendef"), out);
1095 fprintf (out, "\
1096#ifndef YYSTYPE\n\
1097typedef %s
1098yystype;\n\
1099# define YYSTYPE yystype\n\
1100#endif\n",
1101 muscle_find ("stype"));
1102
1103 if (!pure_parser)
1104 fprintf (out, "\nextern YYSTYPE %slval;\n",
1105 spec_name_prefix);
1106 if (semantic_parser)
1107 {
1108 int i;
1109
1110 for (i = ntokens; i < nsyms; i++)
1111 /* don't make these for dummy nonterminals made by gensym. */
1112 if (*tags[i] != '@')
1113 fprintf (out, "# define\tNT%s\t%d\n", tags[i], i);
1114 }
1115
1116 fprintf (out, "\n#endif /* not %s */\n", macro_name);
1117 free (macro_name);
1118 xfclose (out);
1119}
1120
1121
6c89f1c1
AD
1122/*----------------------------------------------------------.
1123| Output the parsing tables and the parser code to ftable. |
1124`----------------------------------------------------------*/
c3e23647 1125
6c89f1c1
AD
1126void
1127output (void)
1128{
26f609ff 1129 obstack_init (&output_obstack);
dd60faec 1130
6c89f1c1 1131 output_token_translations ();
6c89f1c1 1132 output_gram ();
26f609ff 1133
d7913476 1134 XFREE (ritem);
6c89f1c1
AD
1135 if (semantic_parser)
1136 output_stos ();
1137 output_rule_data ();
0846f581 1138 XFREE (user_toknums);
6c89f1c1 1139 output_actions ();
342b8b6e 1140
26f609ff 1141 prepare ();
d1a2daf7 1142 /* Copy definitions in directive. */
5449dd0f 1143 obstack_1grow (&attrs_obstack, 0);
11d82f03 1144 muscle_insert ("prologue", obstack_finish (&attrs_obstack));
652def80 1145
93ede233 1146 /* Output the parser. */
652def80 1147 output_master_parser ();
93ede233
AD
1148 /* Output the header if needed. */
1149 if (defines_flag)
1150 header_output ();
26f609ff 1151
3f96f4dc 1152 free (rule_table + 1);
11d82f03 1153 obstack_free (&muscle_obstack, 0);
26f609ff 1154 obstack_free (&output_obstack, 0);
1c8c2190 1155 obstack_free (&action_obstack, 0);
c3e23647 1156}