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