]> git.saurik.com Git - bison.git/blame - src/output.c
* lib/hash.c, lib/hash.h: Replace with Fileutils 4.1's version.
[bison.git] / src / output.c
CommitLineData
c3e23647 1/* Output the generated parsing program for bison,
5bb18f9a 2 Copyright (C) 1984, 1986, 1989, 1992, 2000, 2001, 2002
255ef638 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"
1239777d 104#include "skeleton.h"
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 127/* Returns the number of lines of S. */
9b3add5b 128size_t
f0440388
MA
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)
1a2b5d37 188 values[i] = rules[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)
29e88316 225 values[i] = states[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)
1a2b5d37 242 values[i] = rules[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. */
c03ae966
AD
279 {
280 short *values = XCALLOC (short, ntokens + 1);
281 for (i = 0; i < ntokens + 1; ++i)
282 values[i] = symbols[i]->user_token_number;
283 output_table_data (&format_obstack, values,
284 0, 1, ntokens + 1);
285 muscle_insert ("toknum", obstack_finish (&format_obstack));
286 XFREE (values);
287 }
288
e372befa 289
9ee3c97b 290 /* Output YYR1. */
b2ed6e58
AD
291 {
292 short *values = XCALLOC (short, nrules + 1);
293 for (i = 0; i < nrules + 1; ++i)
1a2b5d37 294 values[i] = rules[i].lhs;
f87685c3 295 output_table_data (&format_obstack, values,
b2ed6e58 296 0, 1, nrules + 1);
f87685c3 297 muscle_insert ("r1", obstack_finish (&format_obstack));
b2ed6e58
AD
298 XFREE (values);
299 }
d019d655 300
9ee3c97b 301 /* Output YYR2. */
1e9798d5 302 short_tab = XMALLOC (short, nrules + 1);
c3e23647 303 for (i = 1; i < nrules; i++)
1a2b5d37
AD
304 short_tab[i] = rules[i + 1].rhs - rules[i].rhs - 1;
305 short_tab[nrules] = nritems - rules[nrules].rhs - 1;
f87685c3 306 output_table_data (&format_obstack, short_tab,
26f609ff 307 0, 1, nrules + 1);
f87685c3 308 muscle_insert ("r2", obstack_finish (&format_obstack));
1e9798d5 309 XFREE (short_tab);
c3e23647
RS
310}
311
6c89f1c1
AD
312/*------------------------------------------------------------------.
313| Decide what to do for each type of token if seen as the lookahead |
314| token in specified state. The value returned is used as the |
315| default action (yydefact) for the state. In addition, actrow is |
316| filled with what to do for each kind of token, index by symbol |
317| number, with zero meaning do the default action. The value |
318| MINSHORT, a very negative number, means this situation is an |
319| error. The parser recognizes this value specially. |
320| |
321| This is where conflicts are resolved. The loop over lookahead |
322| rules considered lower-numbered rules last, and the last rule |
323| considered that likes a token gets to handle it. |
324`------------------------------------------------------------------*/
c3e23647 325
4a120d45 326static int
f9507c28 327action_row (state_t *state)
c3e23647 328{
6c89f1c1 329 int i;
837491d8 330 int default_rule = 0;
f9507c28 331 reductions *redp = state->reductions;
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
80dac38c 340 if (redp->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
29e88316 365 symbol = states[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. */
2cec70b9
AD
380 for (i = 0; i < errp->nerrs; i++)
381 {
382 int symbol = errp->errs[i];
383 actrow[symbol] = MINSHORT;
384 }
c3e23647 385
36281465
AD
386 /* Now find the most common reduction and make it the default action
387 for this state. */
c3e23647 388
80dac38c 389 if (redp->nreds >= 1 && !nodefault)
c3e23647 390 {
f9507c28 391 if (state->consistent)
c3e23647
RS
392 default_rule = redp->rules[0];
393 else
394 {
7a5350ba 395 int max = 0;
f9507c28 396 for (i = 0; i < state->nlookaheads; i++)
c3e23647 397 {
7a5350ba 398 int count = 0;
f9507c28 399 int rule = -LAruleno[state->lookaheadsp + i];
837491d8 400 int j;
9ee3c97b 401
c3e23647 402 for (j = 0; j < ntokens; j++)
837491d8
AD
403 if (actrow[j] == rule)
404 count++;
9ee3c97b 405
c3e23647
RS
406 if (count > max)
407 {
408 max = count;
409 default_rule = rule;
410 }
411 }
9ee3c97b 412
c3e23647
RS
413 /* actions which match the default are replaced with zero,
414 which means "use the default" */
9ee3c97b 415
c3e23647
RS
416 if (max > 0)
417 {
837491d8 418 int j;
c3e23647 419 for (j = 0; j < ntokens; j++)
837491d8
AD
420 if (actrow[j] == default_rule)
421 actrow[j] = 0;
9ee3c97b 422
ceed8467 423 default_rule = -default_rule;
c3e23647
RS
424 }
425 }
426 }
427
428 /* If have no default rule, the default is an error.
429 So replace any action which says "error" with "use default". */
430
431 if (default_rule == 0)
837491d8
AD
432 for (i = 0; i < ntokens; i++)
433 if (actrow[i] == MINSHORT)
434 actrow[i] = 0;
c3e23647 435
36281465 436 return default_rule;
c3e23647
RS
437}
438
439
4a120d45 440static void
d2729d44 441save_row (int state)
c3e23647 442{
6c89f1c1
AD
443 int i;
444 int count;
445 short *sp;
446 short *sp1;
447 short *sp2;
c3e23647
RS
448
449 count = 0;
450 for (i = 0; i < ntokens; i++)
796d61fb
AD
451 if (actrow[i] != 0)
452 count++;
c3e23647
RS
453
454 if (count == 0)
455 return;
456
d7913476
AD
457 froms[state] = sp1 = sp = XCALLOC (short, count);
458 tos[state] = sp2 = XCALLOC (short, count);
c3e23647
RS
459
460 for (i = 0; i < ntokens; i++)
796d61fb
AD
461 if (actrow[i] != 0)
462 {
463 *sp1++ = i;
464 *sp2++ = actrow[i];
465 }
c3e23647
RS
466
467 tally[state] = count;
468 width[state] = sp1[-1] - sp[0] + 1;
469}
470
471
6c89f1c1
AD
472/*------------------------------------------------------------------.
473| Figure out the actions for the specified state, indexed by |
474| lookahead token type. |
475| |
f2acea59
AD
476| The YYDEFACT table is output now. The detailed info is saved for |
477| putting into YYTABLE later. |
6c89f1c1 478`------------------------------------------------------------------*/
c3e23647 479
4a120d45 480static void
6c89f1c1 481token_actions (void)
c3e23647 482{
6c89f1c1 483 int i;
d7913476 484 short *yydefact = XCALLOC (short, nstates);
c3e23647 485
d7913476 486 actrow = XCALLOC (short, ntokens);
f2acea59 487 for (i = 0; i < nstates; ++i)
c3e23647 488 {
29e88316 489 yydefact[i] = action_row (states[i]);
6c89f1c1 490 save_row (i);
c3e23647 491 }
bbb5bcc6 492
f87685c3 493 output_table_data (&format_obstack, yydefact,
26f609ff 494 yydefact[0], 1, nstates);
f87685c3 495 muscle_insert ("defact", obstack_finish (&format_obstack));
342b8b6e 496
26f609ff 497 XFREE (actrow);
d7913476 498 XFREE (yydefact);
c3e23647
RS
499}
500
501
3f96f4dc
AD
502/*-----------------------------.
503| Output the actions to OOUT. |
504`-----------------------------*/
505
9b3add5b 506void
f0440388 507actions_output (FILE *out, size_t *line)
3f96f4dc
AD
508{
509 int rule;
510 for (rule = 1; rule < nrules + 1; ++rule)
1a2b5d37 511 if (rules[rule].action)
3f96f4dc 512 {
ea52d706 513 fprintf (out, " case %d:\n", rule);
3f96f4dc
AD
514
515 if (!no_lines_flag)
ea52d706 516 fprintf (out, muscle_find ("linef"),
1a2b5d37 517 rules[rule].action_line,
ea52d706
AD
518 quotearg_style (c_quoting_style,
519 muscle_find ("filename")));
3f96f4dc
AD
520 /* As a Bison extension, add the ending semicolon. Since some
521 Yacc don't do that, help people using bison as a Yacc
522 finding their missing semicolons. */
ea52d706 523 fprintf (out, "{ %s%s }\n break;\n\n",
1a2b5d37 524 rules[rule].action,
ea52d706 525 yacc_flag ? ";" : "");
f0440388 526
fbc8ecb7
MA
527 /* We always output 4 '\n' per action. */
528 *line += 4;
529 /* Plus one if !no_lines_flag. */
530 if (!no_lines_flag)
531 ++*line;
f0440388 532 /* Get the number of lines written by the user. */
1a2b5d37 533 *line += get_lines_number (rules[rule].action);
3f96f4dc
AD
534 }
535}
536
537
f499b062
AD
538/*----------------------------.
539| Output the guards to OOUT. |
540`----------------------------*/
541
9b3add5b 542void
f499b062
AD
543guards_output (FILE *out, size_t *line)
544{
545 int rule;
546 for (rule = 1; rule < nrules + 1; ++rule)
1a2b5d37 547 if (rules[rule].action)
f499b062
AD
548 {
549 fprintf (out, " case %d:\n", rule);
550
551 if (!no_lines_flag)
552 fprintf (out, muscle_find ("linef"),
1a2b5d37 553 rules[rule].guard_line,
f499b062
AD
554 quotearg_style (c_quoting_style,
555 muscle_find ("filename")));
556 fprintf (out, "{ %s; }\n break;\n\n",
1a2b5d37 557 rules[rule].guard);
f499b062
AD
558
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;
564 /* Get the number of lines written by the user. */
1a2b5d37 565 *line += get_lines_number (rules[rule].guard);
f499b062
AD
566 }
567}
568
569
091e20bb
AD
570/*---------------------------------------.
571| Output the tokens definition to OOUT. |
572`---------------------------------------*/
573
9b3add5b 574void
091e20bb
AD
575token_definitions_output (FILE *out, size_t *line)
576{
577 int i;
578 for (i = 0; i < ntokens; ++i)
579 {
580 bucket *symbol = symbols[i];
581 int number = symbol->user_token_number;
582
583 if (number == SALIAS)
584 continue;
585 /* Skip error token. */
586 if (symbol->value == error_token_number)
587 continue;
588 if (symbol->tag[0] == '\'')
589 continue; /* skip literal character */
590 if (symbol->tag[0] == '\"')
591 {
592 /* use literal string only if given a symbol with an alias */
593 if (symbol->alias)
594 symbol = symbol->alias;
595 else
596 continue;
597 }
598
599 /* Don't #define nonliteral tokens whose names contain periods
600 or '$' (as does the default value of the EOF token). */
601 if (strchr (symbol->tag, '.') || strchr (symbol->tag, '$'))
602 continue;
603
604 fprintf (out, "# define %s\t%d\n",
605 symbol->tag, number);
606 ++*line;
607 if (semantic_parser)
608 {
609 /* FIXME: This is probably wrong, and should be just as
610 above. --akim. */
611 fprintf (out, "# define T%s\t%d\n", symbol->tag, symbol->value);
612 ++*line;
613 }
614 }
615}
616
617
4a120d45 618static void
d2729d44 619save_column (int symbol, int default_state)
c3e23647 620{
6c89f1c1 621 int i;
6c89f1c1
AD
622 short *sp;
623 short *sp1;
624 short *sp2;
625 int count;
837491d8 626 int symno = symbol - ntokens + nstates;
c3e23647 627
43591cec
AD
628 short begin = goto_map[symbol];
629 short end = goto_map[symbol + 1];
c3e23647
RS
630
631 count = 0;
43591cec 632 for (i = begin; i < end; i++)
796d61fb
AD
633 if (to_state[i] != default_state)
634 count++;
c3e23647
RS
635
636 if (count == 0)
637 return;
638
d7913476
AD
639 froms[symno] = sp1 = sp = XCALLOC (short, count);
640 tos[symno] = sp2 = XCALLOC (short, count);
c3e23647 641
43591cec 642 for (i = begin; i < end; i++)
796d61fb
AD
643 if (to_state[i] != default_state)
644 {
645 *sp1++ = from_state[i];
646 *sp2++ = to_state[i];
647 }
c3e23647
RS
648
649 tally[symno] = count;
650 width[symno] = sp1[-1] - sp[0] + 1;
651}
652
6c89f1c1
AD
653static int
654default_goto (int symbol)
655{
656 int i;
837491d8
AD
657 int m = goto_map[symbol];
658 int n = goto_map[symbol + 1];
659 int default_state = -1;
660 int max = 0;
6c89f1c1
AD
661
662 if (m == n)
663 return -1;
664
665 for (i = 0; i < nstates; i++)
666 state_count[i] = 0;
667
668 for (i = m; i < n; i++)
669 state_count[to_state[i]]++;
670
6c89f1c1 671 for (i = 0; i < nstates; i++)
796d61fb
AD
672 if (state_count[i] > max)
673 {
674 max = state_count[i];
675 default_state = i;
676 }
6c89f1c1
AD
677
678 return default_state;
679}
680
681
682/*-------------------------------------------------------------------.
683| Figure out what to do after reducing with each rule, depending on |
684| the saved state from before the beginning of parsing the data that |
685| matched this rule. |
686| |
687| The YYDEFGOTO table is output now. The detailed info is saved for |
688| putting into YYTABLE later. |
689`-------------------------------------------------------------------*/
690
691static void
692goto_actions (void)
693{
43591cec 694 int i;
bbb5bcc6 695 short *yydefgoto = XMALLOC (short, nsyms - ntokens);
bbb5bcc6 696
26f609ff 697 state_count = XCALLOC (short, nstates);
43591cec 698 for (i = ntokens; i < nsyms; ++i)
6c89f1c1 699 {
43591cec
AD
700 int default_state = default_goto (i);
701 save_column (i, default_state);
702 yydefgoto[i - ntokens] = default_state;
6c89f1c1
AD
703 }
704
f87685c3 705 output_table_data (&format_obstack, yydefgoto,
26f609ff 706 yydefgoto[0], 1, nsyms - ntokens);
f87685c3 707 muscle_insert ("defgoto", obstack_finish (&format_obstack));
43591cec 708
d7913476 709 XFREE (state_count);
43591cec 710 XFREE (yydefgoto);
6c89f1c1 711}
c3e23647
RS
712
713
9ee3c97b
AD
714/* The next few functions decide how to pack the actions and gotos
715 information into yytable. */
c3e23647 716
4a120d45 717static void
d2729d44 718sort_actions (void)
c3e23647 719{
6c89f1c1 720 int i;
c3e23647 721
d7913476 722 order = XCALLOC (short, nvectors);
c3e23647
RS
723 nentries = 0;
724
725 for (i = 0; i < nvectors; i++)
796d61fb
AD
726 if (tally[i] > 0)
727 {
837491d8
AD
728 int k;
729 int t = tally[i];
730 int w = width[i];
731 int j = nentries - 1;
c3e23647 732
796d61fb
AD
733 while (j >= 0 && (width[order[j]] < w))
734 j--;
c3e23647 735
796d61fb
AD
736 while (j >= 0 && (width[order[j]] == w) && (tally[order[j]] < t))
737 j--;
c3e23647 738
796d61fb
AD
739 for (k = nentries - 1; k > j; k--)
740 order[k + 1] = order[k];
c3e23647 741
796d61fb
AD
742 order[j + 1] = i;
743 nentries++;
744 }
c3e23647
RS
745}
746
747
4a120d45 748static int
d2729d44 749matching_state (int vector)
c3e23647 750{
837491d8 751 int i = order[vector];
6c89f1c1
AD
752 int t;
753 int w;
6c89f1c1 754 int prev;
c3e23647 755
c3e23647 756 if (i >= nstates)
36281465 757 return -1;
c3e23647
RS
758
759 t = tally[i];
760 w = width[i];
761
762 for (prev = vector - 1; prev >= 0; prev--)
763 {
837491d8
AD
764 int j = order[prev];
765 int k;
766 int match = 1;
767
c3e23647 768 if (width[j] != w || tally[j] != t)
36281465 769 return -1;
c3e23647 770
c3e23647 771 for (k = 0; match && k < t; k++)
796d61fb
AD
772 if (tos[j][k] != tos[i][k] || froms[j][k] != froms[i][k])
773 match = 0;
c3e23647
RS
774
775 if (match)
36281465 776 return j;
c3e23647
RS
777 }
778
36281465 779 return -1;
c3e23647
RS
780}
781
782
4a120d45 783static int
d2729d44 784pack_vector (int vector)
c3e23647 785{
837491d8 786 int i = order[vector];
6c89f1c1 787 int j;
837491d8 788 int t = tally[i];
6c89f1c1 789 int loc = 0;
837491d8
AD
790 short *from = froms[i];
791 short *to = tos[i];
c3e23647 792
340ef489 793 assert (t);
c3e23647 794
c3e23647
RS
795 for (j = lowzero - from[0]; j < MAXTABLE; j++)
796 {
837491d8
AD
797 int k;
798 int ok = 1;
c3e23647
RS
799
800 for (k = 0; ok && k < t; k++)
801 {
802 loc = j + from[k];
803 if (loc > MAXTABLE)
a0f6b076 804 fatal (_("maximum table size (%d) exceeded"), MAXTABLE);
c3e23647
RS
805
806 if (table[loc] != 0)
807 ok = 0;
808 }
809
810 for (k = 0; ok && k < vector; k++)
796d61fb
AD
811 if (pos[k] == j)
812 ok = 0;
c3e23647
RS
813
814 if (ok)
815 {
816 for (k = 0; k < t; k++)
817 {
818 loc = j + from[k];
819 table[loc] = to[k];
820 check[loc] = from[k];
821 }
822
823 while (table[lowzero] != 0)
824 lowzero++;
825
826 if (loc > high)
827 high = loc;
828
36281465 829 return j;
c3e23647
RS
830 }
831 }
275fc3ad
AD
832#define pack_vector_succeeded 0
833 assert (pack_vector_succeeded);
3843c413 834 return 0;
c3e23647
RS
835}
836
837
6c89f1c1
AD
838static void
839pack_table (void)
840{
841 int i;
842 int place;
843 int state;
844
d7913476
AD
845 base = XCALLOC (short, nvectors);
846 pos = XCALLOC (short, nentries);
847 table = XCALLOC (short, MAXTABLE);
848 check = XCALLOC (short, MAXTABLE);
6c89f1c1
AD
849
850 lowzero = 0;
851 high = 0;
852
853 for (i = 0; i < nvectors; i++)
854 base[i] = MINSHORT;
855
856 for (i = 0; i < MAXTABLE; i++)
857 check[i] = -1;
858
859 for (i = 0; i < nentries; i++)
860 {
861 state = matching_state (i);
862
863 if (state < 0)
864 place = pack_vector (i);
865 else
866 place = base[state];
867
868 pos[i] = place;
869 base[order[i]] = place;
870 }
871
872 for (i = 0; i < nvectors; i++)
873 {
796d61fb
AD
874 XFREE (froms[i]);
875 XFREE (tos[i]);
6c89f1c1
AD
876 }
877
d7913476
AD
878 XFREE (froms);
879 XFREE (tos);
880 XFREE (pos);
6c89f1c1 881}
c3e23647
RS
882
883/* the following functions output yytable, yycheck
884 and the vectors whose elements index the portion starts */
885
4a120d45 886static void
d2729d44 887output_base (void)
c3e23647 888{
26f609ff 889 /* Output pact. */
f87685c3 890 output_table_data (&format_obstack, base,
26f609ff 891 base[0], 1, nstates);
f87685c3 892 muscle_insert ("pact", obstack_finish (&format_obstack));
c3e23647 893
26f609ff 894 /* Output pgoto. */
f87685c3 895 output_table_data (&format_obstack, base,
26f609ff 896 base[nstates], nstates + 1, nvectors);
f87685c3 897 muscle_insert ("pgoto", obstack_finish (&format_obstack));
c3e23647 898
d7913476 899 XFREE (base);
c3e23647
RS
900}
901
902
4a120d45 903static void
d2729d44 904output_table (void)
c3e23647 905{
f87685c3 906 output_table_data (&format_obstack, table,
26f609ff 907 table[0], 1, high + 1);
f87685c3 908 muscle_insert ("table", obstack_finish (&format_obstack));
d7913476 909 XFREE (table);
c3e23647
RS
910}
911
912
4a120d45 913static void
d2729d44 914output_check (void)
c3e23647 915{
f87685c3 916 output_table_data (&format_obstack, check,
26f609ff 917 check[0], 1, high + 1);
f87685c3 918 muscle_insert ("check", obstack_finish (&format_obstack));
d7913476 919 XFREE (check);
c3e23647
RS
920}
921
6c89f1c1
AD
922/* compute and output yydefact, yydefgoto, yypact, yypgoto, yytable
923 and yycheck. */
924
925static void
926output_actions (void)
927{
49701457 928 int i;
6c89f1c1 929 nvectors = nstates + nvars;
c3e23647 930
d7913476
AD
931 froms = XCALLOC (short *, nvectors);
932 tos = XCALLOC (short *, nvectors);
933 tally = XCALLOC (short, nvectors);
934 width = XCALLOC (short, nvectors);
6c89f1c1
AD
935
936 token_actions ();
d7913476
AD
937 XFREE (LA);
938 XFREE (LAruleno);
6c89f1c1
AD
939
940 goto_actions ();
d7913476
AD
941 XFREE (goto_map + ntokens);
942 XFREE (from_state);
943 XFREE (to_state);
6c89f1c1
AD
944
945 sort_actions ();
946 pack_table ();
4e5caae2 947
6c89f1c1
AD
948 output_base ();
949 output_table ();
4e5caae2 950
6c89f1c1 951 output_check ();
49701457
AD
952
953 for (i = 0; i < nstates; ++i)
954 {
29e88316
AD
955 free (states[i]->shifts);
956 XFREE (states[i]->reductions);
957 free (states[i]->errs);
958 free (states[i]);
49701457 959 }
29e88316 960 XFREE (states);
6c89f1c1 961}
c3e23647 962
652def80 963\f
1239777d
AD
964/*---------------------------.
965| Call the skeleton parser. |
966`---------------------------*/
c3e23647 967
4a120d45 968static void
1239777d 969output_skeleton (void)
9b3add5b
RA
970{
971 /* Find the right skeleton file. */
972 if (!skeleton)
973 {
974 if (semantic_parser)
975 skeleton = skeleton_find ("BISON_HAIRY", BISON_HAIRY);
976 else
977 skeleton = skeleton_find ("BISON_SIMPLE", BISON_SIMPLE);
978 }
26f609ff 979
9b3add5b
RA
980 /* Parse the skeleton file and output the needed parsers. */
981 muscle_insert ("skeleton", skeleton);
1239777d 982 process_skeleton (skeleton);
26f609ff
RA
983}
984
985static void
986prepare (void)
987{
11d82f03
MA
988 MUSCLE_INSERT_INT ("last", high);
989 MUSCLE_INSERT_INT ("flag", MINSHORT);
990 MUSCLE_INSERT_INT ("pure", pure_parser);
991 MUSCLE_INSERT_INT ("nsym", nsyms);
992 MUSCLE_INSERT_INT ("debug", debug_flag);
993 MUSCLE_INSERT_INT ("final", final_state);
994 MUSCLE_INSERT_INT ("maxtok", max_user_token_number);
c7925b99 995 MUSCLE_INSERT_INT ("error-verbose", error_verbose);
b5b61c61 996 MUSCLE_INSERT_STRING ("prefix", spec_name_prefix ? spec_name_prefix : "yy");
11d82f03 997
b85810ae
AD
998 /* FIXME: This is wrong: the muscles should decide whether they hold
999 a copy or not, but the situation is too obscure currently. */
1000 MUSCLE_INSERT_STRING ("output-infix", output_infix ? output_infix : "");
1001 MUSCLE_INSERT_STRING ("output-prefix", short_base_name);
1002
11d82f03
MA
1003 MUSCLE_INSERT_INT ("nnts", nvars);
1004 MUSCLE_INSERT_INT ("nrules", nrules);
1005 MUSCLE_INSERT_INT ("nstates", nstates);
1006 MUSCLE_INSERT_INT ("ntokens", ntokens);
1007
5b5d1929 1008 MUSCLE_INSERT_INT ("locations-flag", locations_flag);
26f609ff 1009}
c3e23647 1010
93ede233
AD
1011/*-------------------------.
1012| Output the header file. |
1013`-------------------------*/
1014
1015static void
1016header_output (void)
1017{
091e20bb 1018 size_t dummy_line;
93ede233
AD
1019 FILE *out = xfopen (spec_defines_file, "w");
1020 char *macro_name = compute_header_macro ();
1021
1022 fprintf (out, "#ifndef %s\n", macro_name);
1023 fprintf (out, "# define %s\n\n", macro_name);
1024
091e20bb 1025 token_definitions_output (out, &dummy_line);
93ede233
AD
1026 fprintf (out, "\
1027#ifndef YYSTYPE\n\
1028typedef %s
1029yystype;\n\
1030# define YYSTYPE yystype\n\
1031#endif\n",
1032 muscle_find ("stype"));
1033
1034 if (!pure_parser)
1035 fprintf (out, "\nextern YYSTYPE %slval;\n",
b5b61c61 1036 spec_name_prefix ? spec_name_prefix : "yy");
b9cecb91
AD
1037
1038 if (locations_flag)
1039 {
1040 fputs ("\n\n", out);
1041 fprintf (out, "\
1042#ifndef YYLTYPE\n\
1043typedef struct yyltype\n\
1044{\n\
1045 int first_line;\n\
1046 int first_column;\n\
1047 int last_line;\n\
1048 int last_column;\n\
1049} yyltype;\n\
1050# define YYLTYPE yyltype\n\
1051#endif\n");
1052 if (!pure_parser)
1053 fprintf (out, "\nextern YYLTYPE %slloc;\n",
b5b61c61 1054 spec_name_prefix ? spec_name_prefix : "yy");
b9cecb91
AD
1055 }
1056
93ede233
AD
1057 if (semantic_parser)
1058 {
1059 int i;
1060
1061 for (i = ntokens; i < nsyms; i++)
1062 /* don't make these for dummy nonterminals made by gensym. */
ad949da9
AD
1063 if (*symbols[i]->tag != '@')
1064 fprintf (out, "# define NT%s\t%d\n", symbols[i]->tag, i);
93ede233
AD
1065 }
1066
1067 fprintf (out, "\n#endif /* not %s */\n", macro_name);
1068 free (macro_name);
1069 xfclose (out);
1070}
1071
1072
6c89f1c1
AD
1073/*----------------------------------------------------------.
1074| Output the parsing tables and the parser code to ftable. |
1075`----------------------------------------------------------*/
c3e23647 1076
6c89f1c1
AD
1077void
1078output (void)
1079{
f87685c3 1080 obstack_init (&format_obstack);
dd60faec 1081
6c89f1c1 1082 output_token_translations ();
6c89f1c1 1083 output_gram ();
26f609ff 1084
d7913476 1085 XFREE (ritem);
6c89f1c1
AD
1086 if (semantic_parser)
1087 output_stos ();
1088 output_rule_data ();
1089 output_actions ();
342b8b6e 1090
26f609ff 1091 prepare ();
d1a2daf7 1092 /* Copy definitions in directive. */
5449dd0f 1093 obstack_1grow (&attrs_obstack, 0);
11d82f03 1094 muscle_insert ("prologue", obstack_finish (&attrs_obstack));
652def80 1095
9b3add5b
RA
1096 /* Process the selected skeleton file. */
1097 output_skeleton ();
1098
93ede233
AD
1099 /* Output the header if needed. */
1100 if (defines_flag)
1101 header_output ();
26f609ff 1102
1a2b5d37 1103 free (rules + 1);
f499b062
AD
1104 obstack_free (&muscle_obstack, NULL);
1105 obstack_free (&format_obstack, NULL);
1106 obstack_free (&action_obstack, NULL);
f499b062 1107 obstack_free (&attrs_obstack, NULL);
c3e23647 1108}