]> git.saurik.com Git - bison.git/blame - src/output.c
* src/state.h (nstates, final_state, first_state, first_shift)
[bison.git] / src / output.c
CommitLineData
c3e23647 1/* Output the generated parsing program for bison,
9ee3c97b 2 Copyright (C) 1984, 1986, 1989, 1992, 2000 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"
ceed8467 94#include "getargs.h"
7612000c 95#include "alloc.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"
c3e23647 104
d019d655
AD
105extern void berror PARAMS((const char *));
106
d2729d44 107
c3e23647
RS
108
109static int nvectors;
110static int nentries;
111static short **froms;
112static short **tos;
113static short *tally;
114static short *width;
115static short *actrow;
116static short *state_count;
117static short *order;
118static short *base;
119static short *pos;
120static short *table;
121static short *check;
122static int lowzero;
123static int high;
124
125
126
d019d655
AD
127static inline void
128output_short_table (FILE *out,
129 const char *table_name,
130 short *short_table,
131 short first_value,
132 short begin, short end)
133{
134 int i, j;
135
136 fprintf (out, "static const short %s[] = {%6d", table_name, first_value);
137
138 j = 10;
139 for (i = begin; i < end; i++)
140 {
141 putc (',', out);
142
143 if (j >= 10)
144 {
145 putc ('\n', out);
146 j = 1;
147 }
148 else
149 {
150 j++;
151 }
152
153 fprintf (out, "%6d", short_table[i]);
154 }
155
156 fprintf (out, "\n};\n");
157}
158
159
160/*--------------------------------------------------------------.
161| output_headers -- Output constant strings to the beginning of |
162| certain files. |
163`--------------------------------------------------------------*/
164
ceed8467
AD
165#define GUARDSTR \
166"\n\
167#include \"%s\"\n\
168extern int yyerror;\n\
169extern int yycost;\n\
170extern char * yymsg;\n\
171extern YYSTYPE yyval;\n\
172\n\
173yyguard(n, yyvsp, yylsp)\n\
174register int n;\n\
175register YYSTYPE *yyvsp;\n\
c3e23647 176register YYLTYPE *yylsp;\n\
ceed8467
AD
177{\n\
178 yyerror = 0;\n\
179 yycost = 0;\n\
180 yymsg = 0;\n\
181 switch (n)\n\
182 {"
183
184#define ACTSTR \
185"\n\
186#include \"%s\"\n\
187extern YYSTYPE yyval;\n\
188extern int yychar;\n\
189\n\
190yyaction(n, yyvsp, yylsp)\n\
191register int n;\n\
192register YYSTYPE *yyvsp;\n\
193register YYLTYPE *yylsp;\n\
194{\n\
195 switch (n)\n\
196 {"
c3e23647
RS
197
198#define ACTSTR_SIMPLE "\n switch (yyn) {\n"
199
c3e23647 200void
d2729d44 201output_headers (void)
c3e23647
RS
202{
203 if (semantic_parser)
ceed8467 204 fprintf (fguard, GUARDSTR, attrsfile);
e372befa
RS
205
206 if (noparserflag)
ceed8467 207 return;
e372befa 208
ceed8467 209 fprintf (faction, (semantic_parser ? ACTSTR : ACTSTR_SIMPLE), attrsfile);
c3e23647
RS
210/* if (semantic_parser) JF moved this below
211 fprintf(ftable, "#include \"%s\"\n", attrsfile);
212 fprintf(ftable, "#include <stdio.h>\n\n");
213*/
214
215 /* Rename certain symbols if -p was specified. */
216 if (spec_name_prefix)
217 {
ceed8467
AD
218 fprintf (ftable, "#define yyparse %sparse\n", spec_name_prefix);
219 fprintf (ftable, "#define yylex %slex\n", spec_name_prefix);
220 fprintf (ftable, "#define yyerror %serror\n", spec_name_prefix);
221 fprintf (ftable, "#define yylval %slval\n", spec_name_prefix);
222 fprintf (ftable, "#define yychar %schar\n", spec_name_prefix);
223 fprintf (ftable, "#define yydebug %sdebug\n", spec_name_prefix);
224 fprintf (ftable, "#define yynerrs %snerrs\n", spec_name_prefix);
c3e23647
RS
225 }
226}
227
228
6c89f1c1
AD
229/*-------------------------------------------------------.
230| Output constant strings to the ends of certain files. |
231`-------------------------------------------------------*/
232
c3e23647 233void
d2729d44 234output_trailers (void)
c3e23647
RS
235{
236 if (semantic_parser)
ceed8467 237 fprintf (fguard, "\n }\n}\n");
e372befa 238
ceed8467 239 fprintf (faction, "\n");
9ee3c97b
AD
240
241 if (noparserflag)
ceed8467 242 return;
e372befa
RS
243
244 if (semantic_parser)
ceed8467
AD
245 fprintf (faction, " }\n");
246 fprintf (faction, "}\n");
c3e23647
RS
247}
248
249
c3e23647 250
4a120d45 251static void
d2729d44 252output_token_translations (void)
c3e23647 253{
6c89f1c1
AD
254 int i, j;
255/* short *sp; JF unused */
c3e23647
RS
256
257 if (translations)
258 {
ceed8467
AD
259 fprintf (ftable,
260 "\n#define YYTRANSLATE(x) ((unsigned)(x) <= %d ? yytranslate[x] : %d)\n",
261 max_user_token_number, nsyms);
9ee3c97b 262
ceed8467
AD
263 if (ntokens < 127) /* play it very safe; check maximum element value. */
264 fprintf (ftable, "\nstatic const char yytranslate[] = { 0");
c3e23647 265 else
ceed8467 266 fprintf (ftable, "\nstatic const short yytranslate[] = { 0");
9ee3c97b 267
c3e23647
RS
268 j = 10;
269 for (i = 1; i <= max_user_token_number; i++)
270 {
ceed8467 271 putc (',', ftable);
9ee3c97b 272
c3e23647
RS
273 if (j >= 10)
274 {
ceed8467 275 putc ('\n', ftable);
c3e23647
RS
276 j = 1;
277 }
278 else
279 {
280 j++;
281 }
9ee3c97b 282
ceed8467 283 fprintf (ftable, "%6d", token_translations[i]);
c3e23647 284 }
9ee3c97b 285
ceed8467 286 fprintf (ftable, "\n};\n");
c3e23647
RS
287 }
288 else
289 {
ceed8467 290 fprintf (ftable, "\n#define YYTRANSLATE(x) (x)\n");
9ee3c97b 291 }
c3e23647
RS
292}
293
294
4a120d45 295static void
d2729d44 296output_gram (void)
c3e23647 297{
6c89f1c1
AD
298 int j;
299 short *sp;
c3e23647 300
9ee3c97b 301 /* With the ordinary parser,
e372befa
RS
302 yyprhs and yyrhs are needed only for yydebug. */
303 /* With the noparser option, all tables are generated */
ceed8467 304 if (!semantic_parser && !noparserflag)
d019d655 305 fprintf (ftable, "\n#if YYDEBUG != 0\n");
c3e23647 306
d019d655
AD
307 output_short_table (ftable, "yyprhs", rrhs,
308 0, 1, nrules + 1);
c3e23647 309
ceed8467 310 fprintf (ftable, "\nstatic const short yyrhs[] = {%6d", ritem[0]);
c3e23647
RS
311
312 j = 10;
313 for (sp = ritem + 1; *sp; sp++)
314 {
ceed8467 315 putc (',', ftable);
c3e23647
RS
316
317 if (j >= 10)
318 {
ceed8467 319 putc ('\n', ftable);
c3e23647
RS
320 j = 1;
321 }
322 else
323 {
324 j++;
325 }
326
327 if (*sp > 0)
ceed8467 328 fprintf (ftable, "%6d", *sp);
c3e23647 329 else
ceed8467 330 fprintf (ftable, " 0");
c3e23647
RS
331 }
332
ceed8467 333 fprintf (ftable, "\n};\n");
c3e23647 334
ceed8467
AD
335 if (!semantic_parser && !noparserflag)
336 fprintf (ftable, "\n#endif\n");
c3e23647
RS
337}
338
339
4a120d45 340static void
d2729d44 341output_stos (void)
c3e23647 342{
d019d655
AD
343 output_short_table (ftable, "yystos", accessing_symbol,
344 0, 1, nstates);
c3e23647
RS
345}
346
347
4a120d45 348static void
d2729d44 349output_rule_data (void)
c3e23647 350{
6c89f1c1
AD
351 int i;
352 int j;
c3e23647 353
9ee3c97b
AD
354 fputs ("\n\
355#if YYDEBUG != 0\n\
d019d655
AD
356/* YYRLINE[yyn]: source line where rule number YYN was defined. */\n",
357 ftable);
c3e23647 358
d019d655
AD
359 output_short_table (ftable, "yyrline", rline,
360 0, 1, nrules + 1);
c3e23647 361
d019d655 362 fputs ("#endif\n\n", ftable);
e372befa
RS
363
364 if (toknumflag || noparserflag)
365 {
ceed8467
AD
366 fprintf (ftable, "#define YYNTOKENS %d\n", ntokens);
367 fprintf (ftable, "#define YYNNTS %d\n", nvars);
368 fprintf (ftable, "#define YYNRULES %d\n", nrules);
369 fprintf (ftable, "#define YYNSTATES %d\n", nstates);
370 fprintf (ftable, "#define YYMAXUTOK %d\n\n", max_user_token_number);
e372befa
RS
371 }
372
ceed8467
AD
373 if (!toknumflag && !noparserflag)
374 fprintf (ftable, "\n#if YYDEBUG != 0 || defined (YYERROR_VERBOSE)\n\n");
c3e23647
RS
375
376 /* Output the table of symbol names. */
377
ceed8467
AD
378 fprintf (ftable,
379 "static const char * const yytname[] = { \"%s\"", tags[0]);
c3e23647
RS
380
381 j = strlen (tags[0]) + 44;
e372befa 382 for (i = 1; i < nsyms; i++)
ceed8467
AD
383 /* this used to be i<=nsyms, but that output a final "" symbol
384 almost by accident */
c3e23647 385 {
6c89f1c1 386 char *p;
ceed8467 387 putc (',', ftable);
c3e23647
RS
388 j++;
389
390 if (j > 75)
391 {
ceed8467 392 putc ('\n', ftable);
c3e23647
RS
393 j = 0;
394 }
395
396 putc ('\"', ftable);
397 j++;
398
399 for (p = tags[i]; p && *p; p++)
400 {
401 if (*p == '"' || *p == '\\')
402 {
ceed8467 403 fprintf (ftable, "\\%c", *p);
c3e23647
RS
404 j += 2;
405 }
406 else if (*p == '\n')
407 {
ceed8467 408 fprintf (ftable, "\\n");
c3e23647
RS
409 j += 2;
410 }
411 else if (*p == '\t')
412 {
ceed8467 413 fprintf (ftable, "\\t");
c3e23647
RS
414 j += 2;
415 }
416 else if (*p == '\b')
417 {
ceed8467 418 fprintf (ftable, "\\b");
c3e23647
RS
419 j += 2;
420 }
421 else if (*p < 040 || *p >= 0177)
422 {
ceed8467 423 fprintf (ftable, "\\%03o", *p);
c3e23647
RS
424 j += 4;
425 }
426 else
427 {
ceed8467 428 putc (*p, ftable);
c3e23647
RS
429 j++;
430 }
431 }
432
433 putc ('\"', ftable);
434 j++;
435 }
9ee3c97b
AD
436 /* add a NULL entry to list of tokens */
437 fprintf (ftable, ", NULL\n};\n");
c3e23647 438
ceed8467 439 if (!toknumflag && !noparserflag)
9ee3c97b 440 fprintf (ftable, "#endif\n\n");
e372befa 441
9ee3c97b
AD
442 /* Output YYTOKNUM. */
443 if (toknumflag)
e372befa 444 {
d019d655
AD
445 output_short_table (ftable, "yytoknum", user_toknums,
446 0, 1, ntokens + 1);
e372befa
RS
447 }
448
9ee3c97b
AD
449 /* Output YYR1. */
450 fputs ("\
d019d655 451/* YYR1[YYN]: Symbol number of symbol that rule YYN derives. */\n", ftable);
c3e23647 452
d019d655
AD
453 output_short_table (ftable, "yyr1", rlhs,
454 0, 1, nrules + 1);
ceed8467 455 FREE (rlhs + 1);
d019d655
AD
456
457 putc ('\n', ftable);
9ee3c97b
AD
458
459 /* Output YYR2. */
460 fputs ("\
461/* YYR2[YYN]: Number of symbols composing right hand side of rule YYN. */\n\
462static const short yyr2[] = { 0", ftable);
c3e23647
RS
463 j = 10;
464 for (i = 1; i < nrules; i++)
465 {
ceed8467 466 putc (',', ftable);
c3e23647
RS
467
468 if (j >= 10)
469 {
ceed8467 470 putc ('\n', ftable);
c3e23647
RS
471 j = 1;
472 }
473 else
474 {
475 j++;
476 }
477
ceed8467 478 fprintf (ftable, "%6d", rrhs[i + 1] - rrhs[i] - 1);
c3e23647
RS
479 }
480
ceed8467 481 putc (',', ftable);
c3e23647 482 if (j >= 10)
ceed8467 483 putc ('\n', ftable);
c3e23647 484
ceed8467
AD
485 fprintf (ftable, "%6d\n};\n", nitems - rrhs[nrules] - 1);
486 FREE (rrhs + 1);
c3e23647
RS
487}
488
489
4a120d45 490static void
d2729d44 491output_defines (void)
c3e23647 492{
ceed8467
AD
493 fprintf (ftable, "\n\n#define\tYYFINAL\t\t%d\n", final_state);
494 fprintf (ftable, "#define\tYYFLAG\t\t%d\n", MINSHORT);
495 fprintf (ftable, "#define\tYYNTBASE\t%d\n", ntokens);
c3e23647
RS
496}
497
498
6c89f1c1
AD
499/*------------------------------------------------------------------.
500| Decide what to do for each type of token if seen as the lookahead |
501| token in specified state. The value returned is used as the |
502| default action (yydefact) for the state. In addition, actrow is |
503| filled with what to do for each kind of token, index by symbol |
504| number, with zero meaning do the default action. The value |
505| MINSHORT, a very negative number, means this situation is an |
506| error. The parser recognizes this value specially. |
507| |
508| This is where conflicts are resolved. The loop over lookahead |
509| rules considered lower-numbered rules last, and the last rule |
510| considered that likes a token gets to handle it. |
511`------------------------------------------------------------------*/
c3e23647 512
4a120d45 513static int
d2729d44 514action_row (int state)
c3e23647 515{
6c89f1c1
AD
516 int i;
517 int j;
518 int k;
519 int m = 0;
520 int n = 0;
521 int count;
522 int default_rule;
523 int nreds;
524 int max;
525 int rule;
526 int shift_state;
527 int symbol;
528 unsigned mask;
529 unsigned *wordp;
530 reductions *redp;
531 shifts *shiftp;
532 errs *errp;
ceed8467 533 int nodefault = 0; /* set nonzero to inhibit having any default reduction */
c3e23647
RS
534
535 for (i = 0; i < ntokens; i++)
536 actrow[i] = 0;
537
538 default_rule = 0;
539 nreds = 0;
540 redp = reduction_table[state];
541
542 if (redp)
543 {
544 nreds = redp->nreds;
545
546 if (nreds >= 1)
547 {
36281465
AD
548 /* loop over all the rules available here which require
549 lookahead */
c3e23647
RS
550 m = lookaheads[state];
551 n = lookaheads[state + 1];
552
553 for (i = n - 1; i >= m; i--)
554 {
ceed8467 555 rule = -LAruleno[i];
c3e23647
RS
556 wordp = LA + i * tokensetsize;
557 mask = 1;
558
36281465 559 /* and find each token which the rule finds acceptable
ceed8467 560 to come next */
c3e23647
RS
561 for (j = 0; j < ntokens; j++)
562 {
36281465
AD
563 /* and record this rule as the rule to use if that
564 token follows. */
c3e23647
RS
565 if (mask & *wordp)
566 actrow[j] = rule;
567
568 mask <<= 1;
569 if (mask == 0)
570 {
571 mask = 1;
572 wordp++;
573 }
574 }
575 }
576 }
577 }
578
579 shiftp = shift_table[state];
580
36281465
AD
581 /* Now see which tokens are allowed for shifts in this state. For
582 them, record the shift as the thing to do. So shift is preferred
583 to reduce. */
c3e23647
RS
584
585 if (shiftp)
586 {
587 k = shiftp->nshifts;
588
589 for (i = 0; i < k; i++)
590 {
591 shift_state = shiftp->shifts[i];
ceed8467
AD
592 if (!shift_state)
593 continue;
c3e23647
RS
594
595 symbol = accessing_symbol[shift_state];
596
ceed8467 597 if (ISVAR (symbol))
c3e23647
RS
598 break;
599
600 actrow[symbol] = shift_state;
601
36281465
AD
602 /* Do not use any default reduction if there is a shift for
603 error */
604 if (symbol == error_token_number)
605 nodefault = 1;
c3e23647
RS
606 }
607 }
608
609 errp = err_table[state];
610
36281465
AD
611 /* See which tokens are an explicit error in this state (due to
612 %nonassoc). For them, record MINSHORT as the action. */
c3e23647
RS
613
614 if (errp)
615 {
616 k = errp->nerrs;
617
618 for (i = 0; i < k; i++)
619 {
620 symbol = errp->errs[i];
621 actrow[symbol] = MINSHORT;
622 }
623 }
624
36281465
AD
625 /* Now find the most common reduction and make it the default action
626 for this state. */
c3e23647 627
ceed8467 628 if (nreds >= 1 && !nodefault)
c3e23647
RS
629 {
630 if (consistent[state])
631 default_rule = redp->rules[0];
632 else
633 {
634 max = 0;
635 for (i = m; i < n; i++)
636 {
637 count = 0;
ceed8467 638 rule = -LAruleno[i];
9ee3c97b 639
c3e23647
RS
640 for (j = 0; j < ntokens; j++)
641 {
642 if (actrow[j] == rule)
643 count++;
644 }
9ee3c97b 645
c3e23647
RS
646 if (count > max)
647 {
648 max = count;
649 default_rule = rule;
650 }
651 }
9ee3c97b 652
c3e23647
RS
653 /* actions which match the default are replaced with zero,
654 which means "use the default" */
9ee3c97b 655
c3e23647
RS
656 if (max > 0)
657 {
658 for (j = 0; j < ntokens; j++)
659 {
660 if (actrow[j] == default_rule)
661 actrow[j] = 0;
662 }
9ee3c97b 663
ceed8467 664 default_rule = -default_rule;
c3e23647
RS
665 }
666 }
667 }
668
669 /* If have no default rule, the default is an error.
670 So replace any action which says "error" with "use default". */
671
672 if (default_rule == 0)
673 for (j = 0; j < ntokens; j++)
674 {
675 if (actrow[j] == MINSHORT)
676 actrow[j] = 0;
677 }
678
36281465 679 return default_rule;
c3e23647
RS
680}
681
682
4a120d45 683static void
d2729d44 684save_row (int state)
c3e23647 685{
6c89f1c1
AD
686 int i;
687 int count;
688 short *sp;
689 short *sp1;
690 short *sp2;
c3e23647
RS
691
692 count = 0;
693 for (i = 0; i < ntokens; i++)
694 {
695 if (actrow[i] != 0)
696 count++;
697 }
698
699 if (count == 0)
700 return;
701
ceed8467
AD
702 froms[state] = sp1 = sp = NEW2 (count, short);
703 tos[state] = sp2 = NEW2 (count, short);
c3e23647
RS
704
705 for (i = 0; i < ntokens; i++)
706 {
707 if (actrow[i] != 0)
708 {
709 *sp1++ = i;
710 *sp2++ = actrow[i];
711 }
712 }
713
714 tally[state] = count;
715 width[state] = sp1[-1] - sp[0] + 1;
716}
717
718
6c89f1c1
AD
719/*------------------------------------------------------------------.
720| Figure out the actions for the specified state, indexed by |
721| lookahead token type. |
722| |
f2acea59
AD
723| The YYDEFACT table is output now. The detailed info is saved for |
724| putting into YYTABLE later. |
6c89f1c1 725`------------------------------------------------------------------*/
c3e23647 726
4a120d45 727static void
6c89f1c1 728token_actions (void)
c3e23647 729{
6c89f1c1 730 int i;
f2acea59 731 short *yydefact = NEW2 (nstates, short);
c3e23647 732
6c89f1c1 733 actrow = NEW2 (ntokens, short);
f2acea59 734 for (i = 0; i < nstates; ++i)
c3e23647 735 {
f2acea59 736 yydefact[i] = action_row (i);
6c89f1c1 737 save_row (i);
c3e23647 738 }
6c89f1c1 739 FREE (actrow);
f2acea59
AD
740
741 output_short_table (ftable, "yydefact", yydefact,
742 yydefact[0], 1, nstates);
743 FREE (yydefact);
c3e23647
RS
744}
745
746
6c89f1c1
AD
747static void
748free_shifts (void)
c3e23647 749{
6c89f1c1 750 shifts *sp, *sptmp; /* JF derefrenced freed ptr */
c3e23647 751
6c89f1c1 752 FREE (shift_table);
c3e23647 753
6c89f1c1
AD
754 for (sp = first_shift; sp; sp = sptmp)
755 {
756 sptmp = sp->next;
757 FREE (sp);
758 }
759}
c3e23647 760
c3e23647 761
6c89f1c1
AD
762static void
763free_reductions (void)
764{
765 reductions *rp, *rptmp; /* JF fixed freed ptr */
c3e23647 766
6c89f1c1 767 FREE (reduction_table);
c3e23647 768
6c89f1c1 769 for (rp = first_reduction; rp; rp = rptmp)
c3e23647 770 {
6c89f1c1
AD
771 rptmp = rp->next;
772 FREE (rp);
c3e23647 773 }
c3e23647
RS
774}
775
776
6c89f1c1 777
4a120d45 778static void
d2729d44 779save_column (int symbol, int default_state)
c3e23647 780{
6c89f1c1
AD
781 int i;
782 int m;
783 int n;
784 short *sp;
785 short *sp1;
786 short *sp2;
787 int count;
788 int symno;
c3e23647
RS
789
790 m = goto_map[symbol];
791 n = goto_map[symbol + 1];
792
793 count = 0;
794 for (i = m; i < n; i++)
795 {
796 if (to_state[i] != default_state)
797 count++;
798 }
799
800 if (count == 0)
801 return;
802
803 symno = symbol - ntokens + nstates;
804
ceed8467
AD
805 froms[symno] = sp1 = sp = NEW2 (count, short);
806 tos[symno] = sp2 = NEW2 (count, short);
c3e23647
RS
807
808 for (i = m; i < n; i++)
809 {
810 if (to_state[i] != default_state)
811 {
812 *sp1++ = from_state[i];
813 *sp2++ = to_state[i];
814 }
815 }
816
817 tally[symno] = count;
818 width[symno] = sp1[-1] - sp[0] + 1;
819}
820
6c89f1c1
AD
821static int
822default_goto (int symbol)
823{
824 int i;
825 int m;
826 int n;
827 int default_state;
828 int max;
829
830 m = goto_map[symbol];
831 n = goto_map[symbol + 1];
832
833 if (m == n)
834 return -1;
835
836 for (i = 0; i < nstates; i++)
837 state_count[i] = 0;
838
839 for (i = m; i < n; i++)
840 state_count[to_state[i]]++;
841
842 max = 0;
843 default_state = -1;
844
845 for (i = 0; i < nstates; i++)
846 {
847 if (state_count[i] > max)
848 {
849 max = state_count[i];
850 default_state = i;
851 }
852 }
853
854 return default_state;
855}
856
857
858/*-------------------------------------------------------------------.
859| Figure out what to do after reducing with each rule, depending on |
860| the saved state from before the beginning of parsing the data that |
861| matched this rule. |
862| |
863| The YYDEFGOTO table is output now. The detailed info is saved for |
864| putting into YYTABLE later. |
865`-------------------------------------------------------------------*/
866
867static void
868goto_actions (void)
869{
870 int i, j, k;
871
872 state_count = NEW2 (nstates, short);
873
874 k = default_goto (ntokens);
875 fprintf (ftable, "\nstatic const short yydefgoto[] = {%6d", k);
876 save_column (ntokens, k);
877
878 j = 10;
879 for (i = ntokens + 1; i < nsyms; i++)
880 {
881 putc (',', ftable);
882
883 if (j >= 10)
884 {
885 putc ('\n', ftable);
886 j = 1;
887 }
888 else
889 {
890 j++;
891 }
892
893 k = default_goto (i);
894 fprintf (ftable, "%6d", k);
895 save_column (i, k);
896 }
897
898 fprintf (ftable, "\n};\n");
899 FREE (state_count);
900}
c3e23647
RS
901
902
9ee3c97b
AD
903/* The next few functions decide how to pack the actions and gotos
904 information into yytable. */
c3e23647 905
4a120d45 906static void
d2729d44 907sort_actions (void)
c3e23647 908{
6c89f1c1
AD
909 int i;
910 int j;
911 int k;
912 int t;
913 int w;
c3e23647 914
ceed8467 915 order = NEW2 (nvectors, short);
c3e23647
RS
916 nentries = 0;
917
918 for (i = 0; i < nvectors; i++)
919 {
920 if (tally[i] > 0)
921 {
922 t = tally[i];
923 w = width[i];
924 j = nentries - 1;
925
926 while (j >= 0 && (width[order[j]] < w))
927 j--;
928
929 while (j >= 0 && (width[order[j]] == w) && (tally[order[j]] < t))
930 j--;
931
932 for (k = nentries - 1; k > j; k--)
933 order[k + 1] = order[k];
934
935 order[j + 1] = i;
936 nentries++;
937 }
938 }
939}
940
941
4a120d45 942static int
d2729d44 943matching_state (int vector)
c3e23647 944{
6c89f1c1
AD
945 int i;
946 int j;
947 int k;
948 int t;
949 int w;
950 int match;
951 int prev;
c3e23647
RS
952
953 i = order[vector];
954 if (i >= nstates)
36281465 955 return -1;
c3e23647
RS
956
957 t = tally[i];
958 w = width[i];
959
960 for (prev = vector - 1; prev >= 0; prev--)
961 {
962 j = order[prev];
963 if (width[j] != w || tally[j] != t)
36281465 964 return -1;
c3e23647
RS
965
966 match = 1;
967 for (k = 0; match && k < t; k++)
968 {
969 if (tos[j][k] != tos[i][k] || froms[j][k] != froms[i][k])
970 match = 0;
971 }
972
973 if (match)
36281465 974 return j;
c3e23647
RS
975 }
976
36281465 977 return -1;
c3e23647
RS
978}
979
980
4a120d45 981static int
d2729d44 982pack_vector (int vector)
c3e23647 983{
6c89f1c1
AD
984 int i;
985 int j;
986 int k;
987 int t;
988 int loc = 0;
989 int ok;
990 short *from;
991 short *to;
c3e23647
RS
992
993 i = order[vector];
994 t = tally[i];
995
996 if (t == 0)
ceed8467 997 berror ("pack_vector");
c3e23647
RS
998
999 from = froms[i];
1000 to = tos[i];
1001
1002 for (j = lowzero - from[0]; j < MAXTABLE; j++)
1003 {
1004 ok = 1;
1005
1006 for (k = 0; ok && k < t; k++)
1007 {
1008 loc = j + from[k];
1009 if (loc > MAXTABLE)
a0f6b076 1010 fatal (_("maximum table size (%d) exceeded"), MAXTABLE);
c3e23647
RS
1011
1012 if (table[loc] != 0)
1013 ok = 0;
1014 }
1015
1016 for (k = 0; ok && k < vector; k++)
1017 {
1018 if (pos[k] == j)
1019 ok = 0;
1020 }
1021
1022 if (ok)
1023 {
1024 for (k = 0; k < t; k++)
1025 {
1026 loc = j + from[k];
1027 table[loc] = to[k];
1028 check[loc] = from[k];
1029 }
1030
1031 while (table[lowzero] != 0)
1032 lowzero++;
1033
1034 if (loc > high)
1035 high = loc;
1036
36281465 1037 return j;
c3e23647
RS
1038 }
1039 }
1040
ceed8467
AD
1041 berror ("pack_vector");
1042 return 0; /* JF keep lint happy */
c3e23647
RS
1043}
1044
1045
6c89f1c1
AD
1046static void
1047pack_table (void)
1048{
1049 int i;
1050 int place;
1051 int state;
1052
1053 base = NEW2 (nvectors, short);
1054 pos = NEW2 (nentries, short);
1055 table = NEW2 (MAXTABLE, short);
1056 check = NEW2 (MAXTABLE, short);
1057
1058 lowzero = 0;
1059 high = 0;
1060
1061 for (i = 0; i < nvectors; i++)
1062 base[i] = MINSHORT;
1063
1064 for (i = 0; i < MAXTABLE; i++)
1065 check[i] = -1;
1066
1067 for (i = 0; i < nentries; i++)
1068 {
1069 state = matching_state (i);
1070
1071 if (state < 0)
1072 place = pack_vector (i);
1073 else
1074 place = base[state];
1075
1076 pos[i] = place;
1077 base[order[i]] = place;
1078 }
1079
1080 for (i = 0; i < nvectors; i++)
1081 {
1082 if (froms[i])
1083 FREE (froms[i]);
1084 if (tos[i])
1085 FREE (tos[i]);
1086 }
1087
1088 FREE (froms);
1089 FREE (tos);
1090 FREE (pos);
1091}
c3e23647
RS
1092
1093/* the following functions output yytable, yycheck
1094 and the vectors whose elements index the portion starts */
1095
4a120d45 1096static void
d2729d44 1097output_base (void)
c3e23647 1098{
d019d655
AD
1099 output_short_table (ftable, "yypact", base,
1100 base[0], 1, nstates);
c3e23647 1101
d019d655 1102 putc ('\n', ftable);
c3e23647 1103
d019d655
AD
1104 output_short_table (ftable, "yypgoto", base,
1105 base[nstates], nstates + 1, nvectors);
c3e23647 1106
ceed8467 1107 FREE (base);
c3e23647
RS
1108}
1109
1110
4a120d45 1111static void
d2729d44 1112output_table (void)
c3e23647 1113{
d019d655
AD
1114 fprintf (ftable, "\n\n#define\tYYLAST\t\t%d\n\n\n", high);
1115 output_short_table (ftable, "yytable", table,
1116 table[0], 1, high + 1);
ceed8467 1117 FREE (table);
c3e23647
RS
1118}
1119
1120
4a120d45 1121static void
d2729d44 1122output_check (void)
c3e23647 1123{
d019d655
AD
1124 output_short_table (ftable, "yycheck", check,
1125 check[0], 1, high + 1);
ceed8467 1126 FREE (check);
c3e23647
RS
1127}
1128
6c89f1c1
AD
1129/* compute and output yydefact, yydefgoto, yypact, yypgoto, yytable
1130 and yycheck. */
1131
1132static void
1133output_actions (void)
1134{
1135 nvectors = nstates + nvars;
c3e23647 1136
6c89f1c1
AD
1137 froms = NEW2 (nvectors, short *);
1138 tos = NEW2 (nvectors, short *);
1139 tally = NEW2 (nvectors, short);
1140 width = NEW2 (nvectors, short);
1141
1142 token_actions ();
1143 free_shifts ();
1144 free_reductions ();
1145 FREE (lookaheads);
1146 FREE (LA);
1147 FREE (LAruleno);
1148 FREE (accessing_symbol);
1149
1150 goto_actions ();
1151 FREE (goto_map + ntokens);
1152 FREE (from_state);
1153 FREE (to_state);
1154
1155 sort_actions ();
1156 pack_table ();
d019d655 1157 putc ('\n', ftable);
6c89f1c1
AD
1158 output_base ();
1159 output_table ();
d019d655 1160 putc ('\n', ftable);
6c89f1c1
AD
1161 output_check ();
1162}
c3e23647
RS
1163
1164/* copy the parser code into the ftable file at the end. */
1165
4a120d45 1166static void
d2729d44 1167output_parser (void)
c3e23647 1168{
6c89f1c1 1169 int c;
c3e23647
RS
1170#ifdef DONTDEF
1171 FILE *fpars;
1172#else
1173#define fpars fparser
1174#endif
1175
1176 if (pure_parser)
ceed8467 1177 fprintf (ftable, "#define YYPURE 1\n\n");
c3e23647 1178
ceed8467
AD
1179#ifdef DONTDEF /* JF no longer needed 'cuz open_extra_files changes the
1180 currently open parser from bison.simple to bison.hairy */
c3e23647
RS
1181 if (semantic_parser)
1182 fpars = fparser;
ceed8467
AD
1183 else
1184 fpars = fparser1;
c3e23647
RS
1185#endif
1186
1187 /* Loop over lines in the standard parser file. */
1188
1189 while (1)
1190 {
1191 int write_line = 1;
1192
ceed8467 1193 c = getc (fpars);
c3e23647
RS
1194
1195 /* See if the line starts with `#line.
ceed8467 1196 If so, set write_line to 0. */
c3e23647 1197 if (nolinesflag)
9ee3c97b 1198 if (c == '#')
c3e23647 1199 {
ceed8467 1200 c = getc (fpars);
c3e23647
RS
1201 if (c == 'l')
1202 {
ceed8467 1203 c = getc (fpars);
c3e23647
RS
1204 if (c == 'i')
1205 {
ceed8467 1206 c = getc (fpars);
c3e23647
RS
1207 if (c == 'n')
1208 {
ceed8467 1209 c = getc (fpars);
c3e23647
RS
1210 if (c == 'e')
1211 write_line = 0;
1212 else
ceed8467 1213 fprintf (ftable, "#lin");
c3e23647
RS
1214 }
1215 else
ceed8467 1216 fprintf (ftable, "#li");
c3e23647
RS
1217 }
1218 else
ceed8467 1219 fprintf (ftable, "#l");
c3e23647
RS
1220 }
1221 else
ceed8467 1222 fprintf (ftable, "#");
c3e23647
RS
1223 }
1224
1225 /* now write out the line... */
ceed8467
AD
1226 for (; c != '\n' && c != EOF; c = getc (fpars))
1227 if (write_line)
1228 {
1229 if (c == '$')
1230 {
1231 /* `$' in the parser file indicates where to put the actions.
1232 Copy them in at this point. */
1233 rewind (faction);
1234 for (c = getc (faction); c != EOF; c = getc (faction))
1235 putc (c, ftable);
1236 }
1237 else
1238 putc (c, ftable);
1239 }
c3e23647
RS
1240 if (c == EOF)
1241 break;
ceed8467 1242 putc (c, ftable);
c3e23647
RS
1243 }
1244}
1245
4a120d45 1246static void
d2729d44 1247output_program (void)
c3e23647 1248{
6c89f1c1 1249 int c;
c3e23647
RS
1250
1251 if (!nolinesflag)
ceed8467 1252 fprintf (ftable, "#line %d \"%s\"\n", lineno, infile);
c3e23647 1253
ceed8467 1254 c = getc (finput);
c3e23647
RS
1255 while (c != EOF)
1256 {
ceed8467
AD
1257 putc (c, ftable);
1258 c = getc (finput);
c3e23647
RS
1259 }
1260}
1261
1262
4a120d45 1263static void
d2729d44 1264free_itemsets (void)
c3e23647 1265{
6c89f1c1 1266 core *cp, *cptmp;
c3e23647 1267
ceed8467 1268 FREE (state_table);
c3e23647 1269
36281465
AD
1270 for (cp = first_state; cp; cp = cptmp)
1271 {
ceed8467
AD
1272 cptmp = cp->next;
1273 FREE (cp);
36281465 1274 }
c3e23647
RS
1275}
1276
1277
6c89f1c1
AD
1278/*----------------------------------------------------------.
1279| Output the parsing tables and the parser code to ftable. |
1280`----------------------------------------------------------*/
c3e23647 1281
6c89f1c1
AD
1282void
1283output (void)
1284{
1285 int c;
c3e23647 1286
6c89f1c1
AD
1287 /* output_token_defines(ftable); / * JF put out token defines FIRST */
1288 if (!semantic_parser) /* JF Put out other stuff */
36281465 1289 {
6c89f1c1
AD
1290 rewind (fattrs);
1291 while ((c = getc (fattrs)) != EOF)
1292 putc (c, ftable);
36281465 1293 }
6c89f1c1
AD
1294 reader_output_yylsp (ftable);
1295 if (debugflag)
1296 fputs ("\
1297#ifndef YYDEBUG\n\
1298#define YYDEBUG 1\n\
1299#endif\n\
1300\n",
1301 ftable);
c3e23647 1302
6c89f1c1
AD
1303 if (semantic_parser)
1304 fprintf (ftable, "#include \"%s\"\n", attrsfile);
c3e23647 1305
6c89f1c1
AD
1306 if (!noparserflag)
1307 fprintf (ftable, "#include <stdio.h>\n\n");
c3e23647 1308
6c89f1c1
AD
1309 /* Make "const" do nothing if not in ANSI C. */
1310 fputs ("\
1311#ifndef __cplusplus\n\
1312# ifndef __STDC__\n\
1313# define const\n\
1314# endif\n\
1315#endif\n\
1316\n",
1317 ftable);
c3e23647 1318
6c89f1c1
AD
1319 free_itemsets ();
1320 output_defines ();
1321 output_token_translations ();
1322/* if (semantic_parser) */
1323 /* This is now unconditional because debugging printouts can use it. */
1324 output_gram ();
1325 FREE (ritem);
1326 if (semantic_parser)
1327 output_stos ();
1328 output_rule_data ();
1329 output_actions ();
1330 if (!noparserflag)
1331 output_parser ();
1332 output_program ();
c3e23647 1333}