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