]> git.saurik.com Git - bison.git/blame - src/output.c
* src/file.h (BISON_SIMPLE, BISON_HAIRY): Move from here...
[bison.git] / src / output.c
CommitLineData
c3e23647 1/* Output the generated parsing program for bison,
8c7ebe49 2 Copyright 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"
8c7ebe49 94#include "obstack.h"
14d3eb9b 95#include "quotearg.h"
ceed8467 96#include "getargs.h"
d7913476 97#include "xalloc.h"
c3e23647
RS
98#include "files.h"
99#include "gram.h"
b2ca4022 100#include "LR0.h"
a0f6b076 101#include "complain.h"
6c89f1c1 102#include "output.h"
720d742f 103#include "lalr.h"
a70083a3 104#include "reader.h"
0619caf0 105#include "conflicts.h"
c3e23647 106
d019d655
AD
107extern void berror PARAMS((const char *));
108
d2729d44 109
c3e23647
RS
110
111static int nvectors;
112static int nentries;
113static short **froms;
114static short **tos;
115static short *tally;
116static short *width;
117static short *actrow;
118static short *state_count;
119static short *order;
120static short *base;
121static short *pos;
122static short *table;
123static short *check;
124static int lowzero;
125static int high;
126
127
128
d019d655 129static inline void
896fe5c1 130output_short_or_char_table (struct obstack *oout,
43591cec
AD
131 const char *comment,
132 const char *type,
133 const char *table_name,
134 short *short_table,
135 short first_value,
136 short begin, short end)
d019d655
AD
137{
138 int i, j;
139
1e9798d5 140 if (comment)
896fe5c1 141 obstack_fgrow1 (oout, "/* %s. */\n", comment);
d019d655 142
896fe5c1
AD
143 obstack_fgrow3 (oout, "static const %s %s[] =\n{\n %6d",
144 type, table_name, first_value);
1e9798d5
AD
145
146 j = 1;
d019d655
AD
147 for (i = begin; i < end; i++)
148 {
896fe5c1 149 obstack_1grow (oout, ',');
d019d655
AD
150
151 if (j >= 10)
152 {
896fe5c1 153 obstack_grow_literal_string (oout, "\n ");
d019d655
AD
154 j = 1;
155 }
156 else
157 {
158 j++;
159 }
160
896fe5c1 161 obstack_fgrow1 (oout, "%6d", short_table[i]);
d019d655
AD
162 }
163
896fe5c1 164 obstack_grow_literal_string (oout, "\n};\n");
d019d655
AD
165}
166
167
43591cec 168static inline void
896fe5c1 169output_short_table (struct obstack *oout,
43591cec
AD
170 const char *comment,
171 const char *table_name,
172 short *short_table,
173 short first_value,
174 short begin, short end)
175{
896fe5c1 176 output_short_or_char_table (oout, comment, "short", table_name, short_table,
43591cec
AD
177 first_value, begin, end);
178}
179
180
d019d655
AD
181/*--------------------------------------------------------------.
182| output_headers -- Output constant strings to the beginning of |
183| certain files. |
184`--------------------------------------------------------------*/
185
14d3eb9b
AD
186/* Don't put the `%s' insides quotes, since it quotearg puts them. */
187
ceed8467
AD
188#define GUARDSTR \
189"\n\
14d3eb9b 190#include %s\n\
ceed8467
AD
191extern int yyerror;\n\
192extern int yycost;\n\
193extern char * yymsg;\n\
194extern YYSTYPE yyval;\n\
195\n\
196yyguard(n, yyvsp, yylsp)\n\
197register int n;\n\
198register YYSTYPE *yyvsp;\n\
c3e23647 199register YYLTYPE *yylsp;\n\
ceed8467
AD
200{\n\
201 yyerror = 0;\n\
202 yycost = 0;\n\
203 yymsg = 0;\n\
204 switch (n)\n\
205 {"
206
207#define ACTSTR \
208"\n\
14d3eb9b 209#include %s\n\
8c7ebe49
AD
210extern YYSTYPE yyval;\n\
211extern int yychar;\n\
212\n\
213yyaction(n, yyvsp, yylsp)\n\
214register int n;\n\
215register YYSTYPE *yyvsp;\n\
216register YYLTYPE *yylsp;\n\
217{\n\
218 switch (n)\n\
219 {"
220
c3e23647
RS
221#define ACTSTR_SIMPLE "\n switch (yyn) {\n"
222
c3e23647 223void
d2729d44 224output_headers (void)
c3e23647 225{
14d3eb9b
AD
226 char *attrsfile_quoted = quotearg_style (c_quoting_style, attrsfile);
227
c3e23647 228 if (semantic_parser)
14d3eb9b 229 fprintf (fguard, GUARDSTR, attrsfile_quoted);
e372befa 230
89cab50d 231 if (no_parser_flag)
ceed8467 232 return;
e372befa 233
8c7ebe49 234 if (semantic_parser)
14d3eb9b 235 obstack_fgrow1 (&action_obstack, ACTSTR, attrsfile_quoted);
8c7ebe49 236 else
14d3eb9b 237 obstack_grow_literal_string (&action_obstack, ACTSTR_SIMPLE);
8c7ebe49 238
c3e23647
RS
239/* if (semantic_parser) JF moved this below
240 fprintf(ftable, "#include \"%s\"\n", attrsfile);
241 fprintf(ftable, "#include <stdio.h>\n\n");
242*/
243
244 /* Rename certain symbols if -p was specified. */
245 if (spec_name_prefix)
246 {
896fe5c1 247 obstack_fgrow1 (&table_obstack,
14d3eb9b 248 "#define yyparse %sparse\n", spec_name_prefix);
896fe5c1 249 obstack_fgrow1 (&table_obstack,
14d3eb9b 250 "#define yylex %slex\n", spec_name_prefix);
896fe5c1 251 obstack_fgrow1 (&table_obstack,
14d3eb9b 252 "#define yyerror %serror\n", spec_name_prefix);
896fe5c1 253 obstack_fgrow1 (&table_obstack,
14d3eb9b 254 "#define yylval %slval\n", spec_name_prefix);
896fe5c1 255 obstack_fgrow1 (&table_obstack,
14d3eb9b 256 "#define yychar %schar\n", spec_name_prefix);
896fe5c1 257 obstack_fgrow1 (&table_obstack,
14d3eb9b 258 "#define yydebug %sdebug\n", spec_name_prefix);
896fe5c1 259 obstack_fgrow1 (&table_obstack,
14d3eb9b 260 "#define yynerrs %snerrs\n", spec_name_prefix);
c3e23647
RS
261 }
262}
263
264
6c89f1c1
AD
265/*-------------------------------------------------------.
266| Output constant strings to the ends of certain files. |
267`-------------------------------------------------------*/
268
c3e23647 269void
d2729d44 270output_trailers (void)
c3e23647
RS
271{
272 if (semantic_parser)
ceed8467 273 fprintf (fguard, "\n }\n}\n");
e372befa 274
8c7ebe49 275 obstack_1grow (&action_obstack, '\n');
9ee3c97b 276
89cab50d 277 if (no_parser_flag)
ceed8467 278 return;
e372befa
RS
279
280 if (semantic_parser)
14d3eb9b
AD
281 obstack_grow_literal_string (&action_obstack, " }\n");
282
283 obstack_grow_literal_string (&action_obstack, "}\n");
c3e23647
RS
284}
285
286
c3e23647 287
4a120d45 288static void
d2729d44 289output_token_translations (void)
c3e23647 290{
896fe5c1 291 obstack_grow_literal_string (&table_obstack, "\
43591cec 292\n\
896fe5c1
AD
293/* YYRANSLATE(YYLEX) -- Bison token number corresponding to YYLEX. */\n");
294
c3e23647
RS
295 if (translations)
296 {
896fe5c1 297 obstack_fgrow2 (&table_obstack,
43591cec
AD
298 "#define YYTRANSLATE(x) ((unsigned)(x) <= %d ? yytranslate[x] : %d)\
299\n\
300\n",
ceed8467 301 max_user_token_number, nsyms);
9ee3c97b 302
896fe5c1 303 output_short_or_char_table (&table_obstack,
43591cec
AD
304 "YYRANSLATE[YYLEX] -- Bison token number corresponding to YYLEX",
305 ntokens < 127 ? "char" : "short",
306 "yytranslate", token_translations,
307 0, 1, max_user_token_number + 1);
c3e23647
RS
308 }
309 else
310 {
896fe5c1
AD
311 obstack_grow_literal_string (&table_obstack,
312 "\n#define YYTRANSLATE(x) (x)\n");
9ee3c97b 313 }
c3e23647
RS
314}
315
316
4a120d45 317static void
d2729d44 318output_gram (void)
c3e23647 319{
9ee3c97b 320 /* With the ordinary parser,
e372befa 321 yyprhs and yyrhs are needed only for yydebug. */
89cab50d
AD
322 /* With the no_parser option, all tables are generated */
323 if (!semantic_parser && !no_parser_flag)
896fe5c1 324 obstack_grow_literal_string (&table_obstack, "\n#if YYDEBUG != 0\n");
c3e23647 325
896fe5c1 326 output_short_table (&table_obstack, NULL, "yyprhs", rrhs,
d019d655 327 0, 1, nrules + 1);
c3e23647 328
1e9798d5
AD
329 {
330 size_t yyrhs_size = 1;
331 short *yyrhs, *sp;
332 int i;
c3e23647 333
1e9798d5
AD
334 for (sp = ritem + 1; *sp; sp++)
335 ++yyrhs_size;
336 yyrhs = XMALLOC (short, yyrhs_size);
c3e23647 337
1e9798d5
AD
338 for (sp = ritem + 1, i = 1; *sp; ++sp, ++i)
339 yyrhs[i] = *sp > 0 ? *sp : 0;
c3e23647 340
896fe5c1 341 output_short_table (&table_obstack, NULL, "yyrhs", yyrhs,
1e9798d5
AD
342 ritem[0], 1, yyrhs_size);
343 XFREE (yyrhs);
344 }
c3e23647 345
89cab50d 346 if (!semantic_parser && !no_parser_flag)
896fe5c1 347 obstack_grow_literal_string (&table_obstack, "\n#endif\n");
c3e23647
RS
348}
349
350
4a120d45 351static void
d2729d44 352output_stos (void)
c3e23647 353{
896fe5c1 354 output_short_table (&table_obstack, NULL, "yystos", accessing_symbol,
d019d655 355 0, 1, nstates);
c3e23647
RS
356}
357
358
4a120d45 359static void
d2729d44 360output_rule_data (void)
c3e23647 361{
6c89f1c1
AD
362 int i;
363 int j;
1e9798d5 364 short *short_tab = NULL;
c3e23647 365
896fe5c1
AD
366 obstack_grow_literal_string (&table_obstack, "\n\
367#if YYDEBUG != 0\n");
c3e23647 368
896fe5c1 369 output_short_table (&table_obstack,
1e9798d5
AD
370 "YYRLINE[YYN] -- source line where rule number YYN was defined",
371 "yyrline", rline,
d019d655 372 0, 1, nrules + 1);
c3e23647 373
896fe5c1 374 obstack_grow_literal_string (&table_obstack, "#endif\n\n");
e372befa 375
89cab50d 376 if (token_table_flag || no_parser_flag)
e372befa 377 {
896fe5c1
AD
378 obstack_fgrow1 (&table_obstack, "#define YYNTOKENS %d\n", ntokens);
379 obstack_fgrow1 (&table_obstack, "#define YYNNTS %d\n", nvars);
380 obstack_fgrow1 (&table_obstack, "#define YYNRULES %d\n", nrules);
381 obstack_fgrow1 (&table_obstack, "#define YYNSTATES %d\n", nstates);
382 obstack_fgrow1 (&table_obstack, "#define YYMAXUTOK %d\n\n",
383 max_user_token_number);
e372befa
RS
384 }
385
c3e23647 386 /* Output the table of symbol names. */
1e9798d5 387 if (!token_table_flag && !no_parser_flag)
896fe5c1
AD
388 obstack_grow_literal_string (&table_obstack,
389 "\n#if YYDEBUG != 0 || defined YYERROR_VERBOSE\n\n");
390 obstack_grow_literal_string (&table_obstack, "\
391/* YYTNAME[TOKEN_NUM] -- String name of the token TOKEN_NUM. */\n");
392 obstack_grow_literal_string (&table_obstack,
1e9798d5 393 "static const char *const yytname[] =\n{\n ");
c3e23647 394
1e9798d5
AD
395 j = 0;
396 for (i = 0; i < nsyms; i++)
ceed8467
AD
397 /* this used to be i<=nsyms, but that output a final "" symbol
398 almost by accident */
c3e23647 399 {
1e9798d5
AD
400 /* Width of the next token, including the two quotes, the coma
401 and the space. */
402 int strsize = 4;
6c89f1c1 403 char *p;
c3e23647 404
1e9798d5
AD
405 for (p = tags[i]; p && *p; p++)
406 if (*p == '"' || *p == '\\' || *p == '\n' || *p == '\t'
407 || *p == '\b')
408 strsize += 2;
409 else if (*p < 040 || *p >= 0177)
410 strsize += 4;
411 else
412 strsize++;
413
414 if (j + strsize > 75)
c3e23647 415 {
896fe5c1 416 obstack_grow_literal_string (&table_obstack, "\n ");
1e9798d5 417 j = 2;
c3e23647
RS
418 }
419
896fe5c1 420 obstack_1grow (&table_obstack, '\"');
c3e23647
RS
421 for (p = tags[i]; p && *p; p++)
422 {
423 if (*p == '"' || *p == '\\')
424 {
896fe5c1 425 obstack_fgrow1 (&table_obstack, "\\%c", *p);
c3e23647
RS
426 }
427 else if (*p == '\n')
428 {
896fe5c1 429 obstack_grow_literal_string (&table_obstack, "\\n");
c3e23647
RS
430 }
431 else if (*p == '\t')
432 {
896fe5c1 433 obstack_grow_literal_string (&table_obstack, "\\t");
c3e23647
RS
434 }
435 else if (*p == '\b')
436 {
896fe5c1 437 obstack_grow_literal_string (&table_obstack, "\\b");
c3e23647
RS
438 }
439 else if (*p < 040 || *p >= 0177)
440 {
896fe5c1 441 obstack_fgrow1 (&table_obstack, "\\%03o", *p);
c3e23647
RS
442 }
443 else
444 {
896fe5c1 445 obstack_1grow (&table_obstack, *p);
c3e23647
RS
446 }
447 }
448
896fe5c1 449 obstack_grow_literal_string (&table_obstack, "\", ");
1e9798d5 450 j += strsize;
c3e23647 451 }
9ee3c97b 452 /* add a NULL entry to list of tokens */
896fe5c1 453 obstack_grow_literal_string (&table_obstack, "NULL\n};\n");
c3e23647 454
89cab50d 455 if (!token_table_flag && !no_parser_flag)
896fe5c1 456 obstack_grow_literal_string (&table_obstack, "#endif\n\n");
e372befa 457
9ee3c97b 458 /* Output YYTOKNUM. */
89cab50d 459 if (token_table_flag)
e372befa 460 {
896fe5c1 461 output_short_table (&table_obstack,
1e9798d5
AD
462 "YYTOKNUM[YYLEX] -- Index in YYTNAME corresponding to YYLEX",
463 "yytoknum", user_toknums,
d019d655 464 0, 1, ntokens + 1);
e372befa
RS
465 }
466
9ee3c97b 467 /* Output YYR1. */
896fe5c1 468 output_short_table (&table_obstack,
1e9798d5
AD
469 "YYR1[YYN] -- Symbol number of symbol that rule YYN derives",
470 "yyr1", rlhs,
d019d655 471 0, 1, nrules + 1);
d7913476 472 XFREE (rlhs + 1);
d019d655 473
896fe5c1 474 obstack_1grow (&table_obstack, '\n');
9ee3c97b
AD
475
476 /* Output YYR2. */
1e9798d5 477 short_tab = XMALLOC (short, nrules + 1);
c3e23647 478 for (i = 1; i < nrules; i++)
1e9798d5
AD
479 short_tab[i] = rrhs[i + 1] - rrhs[i] - 1;
480 short_tab[nrules] = nitems - rrhs[nrules] - 1;
896fe5c1 481 output_short_table (&table_obstack,
1e9798d5
AD
482 "YYR2[YYN] -- Number of symbols composing right hand side of rule YYN",
483 "yyr2", short_tab,
484 0, 1, nrules + 1);
896fe5c1 485 obstack_1grow (&table_obstack, '\n');
c3e23647 486
1e9798d5 487 XFREE (short_tab);
c3e23647 488
d7913476 489 XFREE (rrhs + 1);
c3e23647
RS
490}
491
492
4a120d45 493static void
d2729d44 494output_defines (void)
c3e23647 495{
896fe5c1
AD
496 obstack_fgrow1 (&table_obstack, "\n\n#define\tYYFINAL\t\t%d\n", final_state);
497 obstack_fgrow1 (&table_obstack, "#define\tYYFLAG\t\t%d\n", MINSHORT);
498 obstack_fgrow1 (&table_obstack, "#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
d7913476
AD
705 froms[state] = sp1 = sp = XCALLOC (short, count);
706 tos[state] = sp2 = XCALLOC (short, count);
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;
d7913476 734 short *yydefact = XCALLOC (short, nstates);
c3e23647 735
d7913476 736 actrow = XCALLOC (short, ntokens);
f2acea59 737 for (i = 0; i < nstates; ++i)
c3e23647 738 {
f2acea59 739 yydefact[i] = action_row (i);
6c89f1c1 740 save_row (i);
c3e23647 741 }
d7913476 742 XFREE (actrow);
f2acea59 743
896fe5c1 744 output_short_table (&table_obstack,
43591cec
AD
745 "YYDEFACT[S] -- default rule to reduce with in state S when YYTABLE\n\
746 doesn't specify something else to do. Zero means the default is an\n\
747 error",
748 "yydefact", yydefact,
f2acea59 749 yydefact[0], 1, nstates);
896fe5c1 750 obstack_1grow (&table_obstack, '\n');
d7913476 751 XFREE (yydefact);
c3e23647
RS
752}
753
754
6c89f1c1
AD
755static void
756free_shifts (void)
c3e23647 757{
6c89f1c1 758 shifts *sp, *sptmp; /* JF derefrenced freed ptr */
c3e23647 759
d7913476 760 XFREE (shift_table);
c3e23647 761
6c89f1c1
AD
762 for (sp = first_shift; sp; sp = sptmp)
763 {
764 sptmp = sp->next;
d7913476 765 XFREE (sp);
6c89f1c1
AD
766 }
767}
c3e23647 768
c3e23647 769
6c89f1c1
AD
770static void
771free_reductions (void)
772{
773 reductions *rp, *rptmp; /* JF fixed freed ptr */
c3e23647 774
d7913476 775 XFREE (reduction_table);
c3e23647 776
6c89f1c1 777 for (rp = first_reduction; rp; rp = rptmp)
c3e23647 778 {
6c89f1c1 779 rptmp = rp->next;
d7913476 780 XFREE (rp);
c3e23647 781 }
c3e23647
RS
782}
783
784
6c89f1c1 785
4a120d45 786static void
d2729d44 787save_column (int symbol, int default_state)
c3e23647 788{
6c89f1c1 789 int i;
6c89f1c1
AD
790 short *sp;
791 short *sp1;
792 short *sp2;
793 int count;
794 int symno;
c3e23647 795
43591cec
AD
796 short begin = goto_map[symbol];
797 short end = goto_map[symbol + 1];
c3e23647
RS
798
799 count = 0;
43591cec 800 for (i = begin; i < end; i++)
c3e23647
RS
801 {
802 if (to_state[i] != default_state)
803 count++;
804 }
805
806 if (count == 0)
807 return;
808
809 symno = symbol - ntokens + nstates;
810
d7913476
AD
811 froms[symno] = sp1 = sp = XCALLOC (short, count);
812 tos[symno] = sp2 = XCALLOC (short, count);
c3e23647 813
43591cec 814 for (i = begin; i < end; i++)
c3e23647
RS
815 {
816 if (to_state[i] != default_state)
817 {
818 *sp1++ = from_state[i];
819 *sp2++ = to_state[i];
820 }
821 }
822
823 tally[symno] = count;
824 width[symno] = sp1[-1] - sp[0] + 1;
825}
826
6c89f1c1
AD
827static int
828default_goto (int symbol)
829{
830 int i;
831 int m;
832 int n;
833 int default_state;
834 int max;
835
836 m = goto_map[symbol];
837 n = goto_map[symbol + 1];
838
839 if (m == n)
840 return -1;
841
842 for (i = 0; i < nstates; i++)
843 state_count[i] = 0;
844
845 for (i = m; i < n; i++)
846 state_count[to_state[i]]++;
847
848 max = 0;
849 default_state = -1;
850
851 for (i = 0; i < nstates; i++)
852 {
853 if (state_count[i] > max)
854 {
855 max = state_count[i];
856 default_state = i;
857 }
858 }
859
860 return default_state;
861}
862
863
864/*-------------------------------------------------------------------.
865| Figure out what to do after reducing with each rule, depending on |
866| the saved state from before the beginning of parsing the data that |
867| matched this rule. |
868| |
869| The YYDEFGOTO table is output now. The detailed info is saved for |
870| putting into YYTABLE later. |
871`-------------------------------------------------------------------*/
872
873static void
874goto_actions (void)
875{
43591cec 876 int i;
6c89f1c1 877
43591cec 878 short *yydefgoto = XMALLOC (short, nsyms - ntokens);
d7913476 879 state_count = XCALLOC (short, nstates);
6c89f1c1 880
43591cec 881 for (i = ntokens; i < nsyms; ++i)
6c89f1c1 882 {
43591cec
AD
883 int default_state = default_goto (i);
884 save_column (i, default_state);
885 yydefgoto[i - ntokens] = default_state;
6c89f1c1
AD
886 }
887
896fe5c1 888 output_short_table (&table_obstack, NULL, "yydefgoto", yydefgoto,
43591cec
AD
889 yydefgoto[0], 1, nsyms - ntokens);
890
d7913476 891 XFREE (state_count);
43591cec 892 XFREE (yydefgoto);
6c89f1c1 893}
c3e23647
RS
894
895
9ee3c97b
AD
896/* The next few functions decide how to pack the actions and gotos
897 information into yytable. */
c3e23647 898
4a120d45 899static void
d2729d44 900sort_actions (void)
c3e23647 901{
6c89f1c1
AD
902 int i;
903 int j;
904 int k;
905 int t;
906 int w;
c3e23647 907
d7913476 908 order = XCALLOC (short, nvectors);
c3e23647
RS
909 nentries = 0;
910
911 for (i = 0; i < nvectors; i++)
912 {
913 if (tally[i] > 0)
914 {
915 t = tally[i];
916 w = width[i];
917 j = nentries - 1;
918
919 while (j >= 0 && (width[order[j]] < w))
920 j--;
921
922 while (j >= 0 && (width[order[j]] == w) && (tally[order[j]] < t))
923 j--;
924
925 for (k = nentries - 1; k > j; k--)
926 order[k + 1] = order[k];
927
928 order[j + 1] = i;
929 nentries++;
930 }
931 }
932}
933
934
4a120d45 935static int
d2729d44 936matching_state (int vector)
c3e23647 937{
6c89f1c1
AD
938 int i;
939 int j;
940 int k;
941 int t;
942 int w;
943 int match;
944 int prev;
c3e23647
RS
945
946 i = order[vector];
947 if (i >= nstates)
36281465 948 return -1;
c3e23647
RS
949
950 t = tally[i];
951 w = width[i];
952
953 for (prev = vector - 1; prev >= 0; prev--)
954 {
955 j = order[prev];
956 if (width[j] != w || tally[j] != t)
36281465 957 return -1;
c3e23647
RS
958
959 match = 1;
960 for (k = 0; match && k < t; k++)
961 {
962 if (tos[j][k] != tos[i][k] || froms[j][k] != froms[i][k])
963 match = 0;
964 }
965
966 if (match)
36281465 967 return j;
c3e23647
RS
968 }
969
36281465 970 return -1;
c3e23647
RS
971}
972
973
4a120d45 974static int
d2729d44 975pack_vector (int vector)
c3e23647 976{
6c89f1c1
AD
977 int i;
978 int j;
979 int k;
980 int t;
981 int loc = 0;
982 int ok;
983 short *from;
984 short *to;
c3e23647
RS
985
986 i = order[vector];
987 t = tally[i];
988
340ef489 989 assert (t);
c3e23647
RS
990
991 from = froms[i];
992 to = tos[i];
993
994 for (j = lowzero - from[0]; j < MAXTABLE; j++)
995 {
996 ok = 1;
997
998 for (k = 0; ok && k < t; k++)
999 {
1000 loc = j + from[k];
1001 if (loc > MAXTABLE)
a0f6b076 1002 fatal (_("maximum table size (%d) exceeded"), MAXTABLE);
c3e23647
RS
1003
1004 if (table[loc] != 0)
1005 ok = 0;
1006 }
1007
1008 for (k = 0; ok && k < vector; k++)
1009 {
1010 if (pos[k] == j)
1011 ok = 0;
1012 }
1013
1014 if (ok)
1015 {
1016 for (k = 0; k < t; k++)
1017 {
1018 loc = j + from[k];
1019 table[loc] = to[k];
1020 check[loc] = from[k];
1021 }
1022
1023 while (table[lowzero] != 0)
1024 lowzero++;
1025
1026 if (loc > high)
1027 high = loc;
1028
36281465 1029 return j;
c3e23647
RS
1030 }
1031 }
1032
ceed8467
AD
1033 berror ("pack_vector");
1034 return 0; /* JF keep lint happy */
c3e23647
RS
1035}
1036
1037
6c89f1c1
AD
1038static void
1039pack_table (void)
1040{
1041 int i;
1042 int place;
1043 int state;
1044
d7913476
AD
1045 base = XCALLOC (short, nvectors);
1046 pos = XCALLOC (short, nentries);
1047 table = XCALLOC (short, MAXTABLE);
1048 check = XCALLOC (short, MAXTABLE);
6c89f1c1
AD
1049
1050 lowzero = 0;
1051 high = 0;
1052
1053 for (i = 0; i < nvectors; i++)
1054 base[i] = MINSHORT;
1055
1056 for (i = 0; i < MAXTABLE; i++)
1057 check[i] = -1;
1058
1059 for (i = 0; i < nentries; i++)
1060 {
1061 state = matching_state (i);
1062
1063 if (state < 0)
1064 place = pack_vector (i);
1065 else
1066 place = base[state];
1067
1068 pos[i] = place;
1069 base[order[i]] = place;
1070 }
1071
1072 for (i = 0; i < nvectors; i++)
1073 {
1074 if (froms[i])
d7913476 1075 XFREE (froms[i]);
6c89f1c1 1076 if (tos[i])
d7913476 1077 XFREE (tos[i]);
6c89f1c1
AD
1078 }
1079
d7913476
AD
1080 XFREE (froms);
1081 XFREE (tos);
1082 XFREE (pos);
6c89f1c1 1083}
c3e23647
RS
1084
1085/* the following functions output yytable, yycheck
1086 and the vectors whose elements index the portion starts */
1087
4a120d45 1088static void
d2729d44 1089output_base (void)
c3e23647 1090{
896fe5c1 1091 output_short_table (&table_obstack, NULL, "yypact", base,
d019d655 1092 base[0], 1, nstates);
c3e23647 1093
896fe5c1 1094 obstack_1grow (&table_obstack, '\n');
c3e23647 1095
896fe5c1 1096 output_short_table (&table_obstack, NULL, "yypgoto", base,
d019d655 1097 base[nstates], nstates + 1, nvectors);
c3e23647 1098
d7913476 1099 XFREE (base);
c3e23647
RS
1100}
1101
1102
4a120d45 1103static void
d2729d44 1104output_table (void)
c3e23647 1105{
896fe5c1
AD
1106 obstack_fgrow1 (&table_obstack, "\n\n#define\tYYLAST\t\t%d\n\n\n", high);
1107 output_short_table (&table_obstack, NULL, "yytable", table,
d019d655 1108 table[0], 1, high + 1);
d7913476 1109 XFREE (table);
c3e23647
RS
1110}
1111
1112
4a120d45 1113static void
d2729d44 1114output_check (void)
c3e23647 1115{
896fe5c1 1116 output_short_table (&table_obstack, NULL, "yycheck", check,
d019d655 1117 check[0], 1, high + 1);
d7913476 1118 XFREE (check);
c3e23647
RS
1119}
1120
6c89f1c1
AD
1121/* compute and output yydefact, yydefgoto, yypact, yypgoto, yytable
1122 and yycheck. */
1123
1124static void
1125output_actions (void)
1126{
1127 nvectors = nstates + nvars;
c3e23647 1128
d7913476
AD
1129 froms = XCALLOC (short *, nvectors);
1130 tos = XCALLOC (short *, nvectors);
1131 tally = XCALLOC (short, nvectors);
1132 width = XCALLOC (short, nvectors);
6c89f1c1
AD
1133
1134 token_actions ();
1135 free_shifts ();
1136 free_reductions ();
d7913476
AD
1137 XFREE (lookaheads);
1138 XFREE (LA);
1139 XFREE (LAruleno);
1140 XFREE (accessing_symbol);
6c89f1c1
AD
1141
1142 goto_actions ();
d7913476
AD
1143 XFREE (goto_map + ntokens);
1144 XFREE (from_state);
1145 XFREE (to_state);
6c89f1c1
AD
1146
1147 sort_actions ();
1148 pack_table ();
896fe5c1 1149 obstack_1grow (&table_obstack, '\n');
6c89f1c1
AD
1150 output_base ();
1151 output_table ();
896fe5c1 1152 obstack_1grow (&table_obstack, '\n');
6c89f1c1
AD
1153 output_check ();
1154}
c3e23647
RS
1155
1156/* copy the parser code into the ftable file at the end. */
1157
4a120d45 1158static void
d2729d44 1159output_parser (void)
c3e23647 1160{
6c89f1c1 1161 int c;
71da9eea 1162 static int number_of_dollar_signs = 0;
c3e23647
RS
1163#ifdef DONTDEF
1164 FILE *fpars;
1165#else
1166#define fpars fparser
1167#endif
1168
1169 if (pure_parser)
896fe5c1 1170 obstack_grow_literal_string (&table_obstack, "#define YYPURE 1\n\n");
c3e23647 1171
71da9eea
AD
1172#ifdef DONTDEF
1173 /* JF no longer needed 'cuz open_extra_files changes the currently
1174 open parser from bison.simple to bison.hairy */
c3e23647
RS
1175 if (semantic_parser)
1176 fpars = fparser;
ceed8467
AD
1177 else
1178 fpars = fparser1;
c3e23647
RS
1179#endif
1180
1181 /* Loop over lines in the standard parser file. */
1182
1183 while (1)
1184 {
1185 int write_line = 1;
1186
ceed8467 1187 c = getc (fpars);
c3e23647
RS
1188
1189 /* See if the line starts with `#line.
ceed8467 1190 If so, set write_line to 0. */
89cab50d 1191 if (no_lines_flag)
9ee3c97b 1192 if (c == '#')
c3e23647 1193 {
ceed8467 1194 c = getc (fpars);
c3e23647
RS
1195 if (c == 'l')
1196 {
ceed8467 1197 c = getc (fpars);
c3e23647
RS
1198 if (c == 'i')
1199 {
ceed8467 1200 c = getc (fpars);
c3e23647
RS
1201 if (c == 'n')
1202 {
ceed8467 1203 c = getc (fpars);
c3e23647
RS
1204 if (c == 'e')
1205 write_line = 0;
1206 else
896fe5c1 1207 obstack_grow_literal_string (&table_obstack, "#lin");
c3e23647
RS
1208 }
1209 else
896fe5c1 1210 obstack_grow_literal_string (&table_obstack, "#li");
c3e23647
RS
1211 }
1212 else
896fe5c1 1213 obstack_grow_literal_string (&table_obstack, "#l");
c3e23647
RS
1214 }
1215 else
896fe5c1 1216 obstack_grow_literal_string (&table_obstack, "#");
c3e23647
RS
1217 }
1218
1219 /* now write out the line... */
ceed8467
AD
1220 for (; c != '\n' && c != EOF; c = getc (fpars))
1221 if (write_line)
1222 {
8c7ebe49
AD
1223 /* `$' in the parser file indicates where to put the
1224 actions. Copy them in at this point. */
ceed8467
AD
1225 if (c == '$')
1226 {
8c7ebe49
AD
1227 size_t size = obstack_object_size (&action_obstack);
1228
71da9eea
AD
1229 number_of_dollar_signs++;
1230 assert (number_of_dollar_signs == 1);
896fe5c1
AD
1231 obstack_grow (&table_obstack,
1232 obstack_finish (&action_obstack),
1233 size);
8c7ebe49 1234
71da9eea
AD
1235 /* Skip the end of the line containing `$'. */
1236 write_line = 0;
ceed8467
AD
1237 }
1238 else
896fe5c1 1239 obstack_1grow (&table_obstack, c);
ceed8467 1240 }
c3e23647
RS
1241 if (c == EOF)
1242 break;
896fe5c1 1243 obstack_1grow (&table_obstack, c);
c3e23647 1244 }
71da9eea 1245 assert (number_of_dollar_signs == 1);
c3e23647
RS
1246}
1247
4a120d45 1248static void
d2729d44 1249output_program (void)
c3e23647 1250{
6c89f1c1 1251 int c;
c3e23647 1252
89cab50d 1253 if (!no_lines_flag)
14d3eb9b
AD
1254 obstack_fgrow2 (&table_obstack, "#line %d %s\n",
1255 lineno, quotearg_style (c_quoting_style, infile));
c3e23647 1256
14d3eb9b
AD
1257 while ((c = getc (finput)) != EOF)
1258 obstack_1grow (&table_obstack, c);
c3e23647
RS
1259}
1260
1261
4a120d45 1262static void
d2729d44 1263free_itemsets (void)
c3e23647 1264{
6c89f1c1 1265 core *cp, *cptmp;
c3e23647 1266
d7913476 1267 XFREE (state_table);
c3e23647 1268
36281465
AD
1269 for (cp = first_state; cp; cp = cptmp)
1270 {
ceed8467 1271 cptmp = cp->next;
d7913476 1272 XFREE (cp);
36281465 1273 }
c3e23647
RS
1274}
1275
1276
6c89f1c1
AD
1277/*----------------------------------------------------------.
1278| Output the parsing tables and the parser code to ftable. |
1279`----------------------------------------------------------*/
c3e23647 1280
6c89f1c1
AD
1281void
1282output (void)
1283{
6c89f1c1 1284 /* output_token_defines(ftable); / * JF put out token defines FIRST */
dd60faec
AD
1285
1286 /* If using a simple parser the definition of YYSTYPE are put into
896fe5c1 1287 TABLE_OBSTACK. */
dd60faec 1288 if (!semantic_parser)
36281465 1289 {
dd60faec 1290 size_t size = obstack_object_size (&attrs_obstack);
896fe5c1 1291 obstack_grow (&table_obstack, obstack_finish (&attrs_obstack), size);
36281465 1292 }
896fe5c1 1293 reader_output_yylsp (&table_obstack);
89cab50d 1294 if (debug_flag)
896fe5c1 1295 obstack_grow_literal_string (&table_obstack, "\
6c89f1c1 1296#ifndef YYDEBUG\n\
71da9eea 1297# define YYDEBUG 1\n\
6c89f1c1 1298#endif\n\
896fe5c1 1299\n");
c3e23647 1300
6c89f1c1 1301 if (semantic_parser)
14d3eb9b
AD
1302 obstack_fgrow1 (&table_obstack, "#include %s\n",
1303 quotearg_style (c_quoting_style, attrsfile));
c3e23647 1304
89cab50d 1305 if (!no_parser_flag)
896fe5c1 1306 obstack_grow_literal_string (&table_obstack, "#include <stdio.h>\n\n");
c3e23647 1307
6c89f1c1 1308 /* Make "const" do nothing if not in ANSI C. */
896fe5c1 1309 obstack_grow_literal_string (&table_obstack, "\
6c89f1c1
AD
1310#ifndef __cplusplus\n\
1311# ifndef __STDC__\n\
1312# define const\n\
1313# endif\n\
1314#endif\n\
896fe5c1 1315\n");
c3e23647 1316
6c89f1c1
AD
1317 free_itemsets ();
1318 output_defines ();
1319 output_token_translations ();
1320/* if (semantic_parser) */
1321 /* This is now unconditional because debugging printouts can use it. */
1322 output_gram ();
d7913476 1323 XFREE (ritem);
6c89f1c1
AD
1324 if (semantic_parser)
1325 output_stos ();
1326 output_rule_data ();
1327 output_actions ();
89cab50d 1328 if (!no_parser_flag)
6c89f1c1
AD
1329 output_parser ();
1330 output_program ();
c3e23647 1331}