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