]>
git.saurik.com Git - bison.git/blob - src/output.c
1 /* Output the generated parsing program for bison,
2 Copyright 1984, 1986, 1989, 1992, 2000, 2001
3 Free Software Foundation, Inc.
5 This file is part of Bison, the GNU Compiler Compiler.
7 Bison is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 Bison is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with Bison; see the file COPYING. If not, write to the Free
19 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
23 /* The parser tables consist of these tables. Marked ones needed only
24 for the semantic parser. Double marked are output only if switches
27 YYTRANSLATE = vector mapping yylex's token numbers into bison's
30 ++ YYTNAME = vector of string-names indexed by bison token number.
32 ++ YYTOKNUM = vector of yylex token numbers corresponding to
35 YYRLINE = vector of line-numbers of all rules. For yydebug
38 YYRHS = vector of items of all rules. This is exactly what RITEMS
39 contains. For yydebug and for semantic parser.
41 YYPRHS[R] = index in YYRHS of first item for rule R.
43 YYR1[R] = symbol number of symbol that rule R derives.
45 YYR2[R] = number of symbols composing right hand side of rule R.
47 + YYSTOS[S] = the symbol number of the symbol that leads to state
50 YYDEFACT[S] = default rule to reduce with in state s, when YYTABLE
51 doesn't specify something else to do. Zero means the default is an
54 YYDEFGOTO[I] = default state to go to after a reduction of a rule
55 that generates variable NTOKENS + I, except when YYTABLE specifies
58 YYPACT[S] = index in YYTABLE of the portion describing state S.
59 The lookahead token's type is used to index that portion to find
62 If the value in YYTABLE is positive, we shift the token and go to
65 If the value is negative, it is minus a rule number to reduce by.
67 If the value is zero, the default action from YYDEFACT[S] is used.
69 YYPGOTO[I] = the index in YYTABLE of the portion describing what to
70 do after reducing a rule that derives variable I + NTOKENS. This
71 portion is indexed by the parser state number, S, as of before the
72 text for this nonterminal was read. The value from YYTABLE is the
73 state to go to if the corresponding value in YYCHECK is S.
75 YYTABLE = a vector filled with portions for different uses, found
76 via YYPACT and YYPGOTO.
78 YYCHECK = a vector indexed in parallel with YYTABLE. It indicates,
79 in a roundabout way, the bounds of the portion you are trying to
82 Suppose that the portion of yytable starts at index P and the index
83 to be examined within the portion is I. Then if YYCHECK[P+I] != I,
84 I is outside the bounds of what is actually allocated, and the
85 default (from YYDEFACT or YYDEFGOTO) should be used. Otherwise,
86 YYTABLE[P+I] should be used.
88 YYFINAL = the state number of the termination state. YYFLAG = most
89 negative short int. Used to flag ?? */
102 #include "conflicts.h"
103 #include "muscle_tab.h"
104 #include "skeleton.h"
108 static short **froms
= NULL
;
109 static short **tos
= NULL
;
110 static short *tally
= NULL
;
111 static short *width
= NULL
;
112 static short *actrow
= NULL
;
113 static short *state_count
= NULL
;
114 static short *order
= NULL
;
115 static short *base
= NULL
;
116 static short *pos
= NULL
;
117 static short *table
= NULL
;
118 static short *check
= NULL
;
122 struct obstack muscle_obstack
;
123 static struct obstack format_obstack
;
125 int error_verbose
= 0;
127 /* Returns the number of lines of S. */
129 get_lines_number (const char *s
)
134 for (i
= 0; s
[i
]; ++i
)
145 output_table_data (struct obstack
*oout
,
154 obstack_fgrow1 (oout
, "%6d", first
);
155 for (i
= begin
; i
< end
; ++i
)
157 obstack_1grow (oout
, ',');
160 obstack_sgrow (oout
, "\n ");
165 obstack_fgrow1 (oout
, "%6d", table_data
[i
]);
167 obstack_1grow (oout
, 0);
172 output_token_translations (void)
174 output_table_data (&format_obstack
, token_translations
,
175 0, 1, max_user_token_number
+ 1);
176 muscle_insert ("translate", obstack_finish (&format_obstack
));
177 XFREE (token_translations
);
186 short *values
= XCALLOC (short, nrules
+ 1);
187 for (i
= 0; i
< nrules
+ 1; ++i
)
188 values
[i
] = rules
[i
].rhs
;
189 output_table_data (&format_obstack
, values
,
194 muscle_insert ("prhs", obstack_finish (&format_obstack
));
200 yyrhs
= XMALLOC (short, nritems
);
202 for (i
= 1; i
< nritems
; ++i
)
203 yyrhs
[i
] = ritem
[i
] >= 0 ? ritem
[i
] : -1;
205 output_table_data (&format_obstack
, yyrhs
,
206 ritem
[0], 1, nritems
);
207 muscle_insert ("rhs", obstack_finish (&format_obstack
));
213 if (!semantic_parser
)
214 obstack_sgrow (&table_obstack
, "\n#endif\n");
223 short *values
= (short *) alloca (sizeof (short) * nstates
);
224 for (i
= 0; i
< nstates
; ++i
)
225 values
[i
] = states
[i
]->accessing_symbol
;
226 output_table_data (&format_obstack
, values
,
228 muscle_insert ("stos", obstack_finish (&format_obstack
));
233 output_rule_data (void)
237 short *short_tab
= NULL
;
240 short *values
= XCALLOC (short, nrules
+ 1);
241 for (i
= 0; i
< nrules
+ 1; ++i
)
242 values
[i
] = rules
[i
].line
;
243 output_table_data (&format_obstack
, values
,
245 muscle_insert ("rline", obstack_finish (&format_obstack
));
251 for (i
= 0; i
< nsyms
; i
++)
253 /* Be sure not to use twice the same quotearg slot. */
255 quotearg_n_style (1, c_quoting_style
,
256 quotearg_style (escape_quoting_style
, symbols
[i
]->tag
));
257 /* Width of the next token, including the two quotes, the coma
259 int strsize
= strlen (cp
) + 2;
261 if (j
+ strsize
> 75)
263 obstack_sgrow (&format_obstack
, "\n ");
267 obstack_sgrow (&format_obstack
, cp
);
268 obstack_sgrow (&format_obstack
, ", ");
271 /* add a NULL entry to list of tokens */
272 obstack_sgrow (&format_obstack
, "NULL");
274 /* Finish table and store. */
275 obstack_1grow (&format_obstack
, 0);
276 muscle_insert ("tname", obstack_finish (&format_obstack
));
278 /* Output YYTOKNUM. */
280 short *values
= XCALLOC (short, ntokens
+ 1);
281 for (i
= 0; i
< ntokens
+ 1; ++i
)
282 values
[i
] = symbols
[i
]->user_token_number
;
283 output_table_data (&format_obstack
, values
,
285 muscle_insert ("toknum", obstack_finish (&format_obstack
));
292 short *values
= XCALLOC (short, nrules
+ 1);
293 for (i
= 0; i
< nrules
+ 1; ++i
)
294 values
[i
] = rules
[i
].lhs
;
295 output_table_data (&format_obstack
, values
,
297 muscle_insert ("r1", obstack_finish (&format_obstack
));
302 short_tab
= XMALLOC (short, nrules
+ 1);
303 for (i
= 1; i
< nrules
; i
++)
304 short_tab
[i
] = rules
[i
+ 1].rhs
- rules
[i
].rhs
- 1;
305 short_tab
[nrules
] = nritems
- rules
[nrules
].rhs
- 1;
306 output_table_data (&format_obstack
, short_tab
,
308 muscle_insert ("r2", obstack_finish (&format_obstack
));
312 /*------------------------------------------------------------------.
313 | Decide what to do for each type of token if seen as the lookahead |
314 | token in specified state. The value returned is used as the |
315 | default action (yydefact) for the state. In addition, actrow is |
316 | filled with what to do for each kind of token, index by symbol |
317 | number, with zero meaning do the default action. The value |
318 | MINSHORT, a very negative number, means this situation is an |
319 | error. The parser recognizes this value specially. |
321 | This is where conflicts are resolved. The loop over lookahead |
322 | rules considered lower-numbered rules last, and the last rule |
323 | considered that likes a token gets to handle it. |
324 `------------------------------------------------------------------*/
327 action_row (state_t
*state
)
330 int default_rule
= 0;
331 reductions
*redp
= state
->reductions
;
332 shifts
*shiftp
= state
->shifts
;
333 errs
*errp
= state
->errs
;
334 /* set nonzero to inhibit having any default reduction */
337 for (i
= 0; i
< ntokens
; i
++)
340 if (redp
->nreds
>= 1)
343 /* loop over all the rules available here which require
345 for (i
= state
->nlookaheads
- 1; i
>= 0; --i
)
346 /* and find each token which the rule finds acceptable
348 for (j
= 0; j
< ntokens
; j
++)
349 /* and record this rule as the rule to use if that
351 if (BITISSET (LA (state
->lookaheadsp
+ i
), j
))
352 actrow
[j
] = -LAruleno
[state
->lookaheadsp
+ i
];
355 /* Now see which tokens are allowed for shifts in this state. For
356 them, record the shift as the thing to do. So shift is preferred
358 for (i
= 0; i
< shiftp
->nshifts
; i
++)
361 int shift_state
= shiftp
->shifts
[i
];
365 symbol
= states
[shift_state
]->accessing_symbol
;
370 actrow
[symbol
] = shift_state
;
372 /* Do not use any default reduction if there is a shift for
374 if (symbol
== error_token_number
)
378 /* See which tokens are an explicit error in this state (due to
379 %nonassoc). For them, record MINSHORT as the action. */
380 for (i
= 0; i
< errp
->nerrs
; i
++)
382 int symbol
= errp
->errs
[i
];
383 actrow
[symbol
] = MINSHORT
;
386 /* Now find the most common reduction and make it the default action
389 if (redp
->nreds
>= 1 && !nodefault
)
391 if (state
->consistent
)
392 default_rule
= redp
->rules
[0];
396 for (i
= 0; i
< state
->nlookaheads
; i
++)
399 int rule
= -LAruleno
[state
->lookaheadsp
+ i
];
402 for (j
= 0; j
< ntokens
; j
++)
403 if (actrow
[j
] == rule
)
413 /* actions which match the default are replaced with zero,
414 which means "use the default" */
419 for (j
= 0; j
< ntokens
; j
++)
420 if (actrow
[j
] == default_rule
)
423 default_rule
= -default_rule
;
428 /* If have no default rule, the default is an error.
429 So replace any action which says "error" with "use default". */
431 if (default_rule
== 0)
432 for (i
= 0; i
< ntokens
; i
++)
433 if (actrow
[i
] == MINSHORT
)
450 for (i
= 0; i
< ntokens
; i
++)
457 froms
[state
] = sp1
= sp
= XCALLOC (short, count
);
458 tos
[state
] = sp2
= XCALLOC (short, count
);
460 for (i
= 0; i
< ntokens
; i
++)
467 tally
[state
] = count
;
468 width
[state
] = sp1
[-1] - sp
[0] + 1;
472 /*------------------------------------------------------------------.
473 | Figure out the actions for the specified state, indexed by |
474 | lookahead token type. |
476 | The YYDEFACT table is output now. The detailed info is saved for |
477 | putting into YYTABLE later. |
478 `------------------------------------------------------------------*/
484 short *yydefact
= XCALLOC (short, nstates
);
486 actrow
= XCALLOC (short, ntokens
);
487 for (i
= 0; i
< nstates
; ++i
)
489 yydefact
[i
] = action_row (states
[i
]);
493 output_table_data (&format_obstack
, yydefact
,
494 yydefact
[0], 1, nstates
);
495 muscle_insert ("defact", obstack_finish (&format_obstack
));
502 /*-----------------------------.
503 | Output the actions to OOUT. |
504 `-----------------------------*/
507 actions_output (FILE *out
, size_t *line
)
510 for (rule
= 1; rule
< nrules
+ 1; ++rule
)
511 if (rules
[rule
].action
)
513 fprintf (out
, " case %d:\n", rule
);
516 fprintf (out
, muscle_find ("linef"),
517 rules
[rule
].action_line
,
518 quotearg_style (c_quoting_style
,
519 muscle_find ("filename")));
520 /* As a Bison extension, add the ending semicolon. Since some
521 Yacc don't do that, help people using bison as a Yacc
522 finding their missing semicolons. */
523 fprintf (out
, "{ %s%s }\n break;\n\n",
525 yacc_flag
? ";" : "");
527 /* We always output 4 '\n' per action. */
529 /* Plus one if !no_lines_flag. */
532 /* Get the number of lines written by the user. */
533 *line
+= get_lines_number (rules
[rule
].action
);
538 /*----------------------------.
539 | Output the guards to OOUT. |
540 `----------------------------*/
543 guards_output (FILE *out
, size_t *line
)
546 for (rule
= 1; rule
< nrules
+ 1; ++rule
)
547 if (rules
[rule
].action
)
549 fprintf (out
, " case %d:\n", rule
);
552 fprintf (out
, muscle_find ("linef"),
553 rules
[rule
].guard_line
,
554 quotearg_style (c_quoting_style
,
555 muscle_find ("filename")));
556 fprintf (out
, "{ %s; }\n break;\n\n",
559 /* We always output 4 '\n' per action. */
561 /* Plus one if !no_lines_flag. */
564 /* Get the number of lines written by the user. */
565 *line
+= get_lines_number (rules
[rule
].guard
);
570 /*---------------------------------------.
571 | Output the tokens definition to OOUT. |
572 `---------------------------------------*/
575 token_definitions_output (FILE *out
, size_t *line
)
578 for (i
= 0; i
< ntokens
; ++i
)
580 bucket
*symbol
= symbols
[i
];
581 int number
= symbol
->user_token_number
;
583 if (number
== SALIAS
)
585 /* Skip error token. */
586 if (symbol
->value
== error_token_number
)
588 if (symbol
->tag
[0] == '\'')
589 continue; /* skip literal character */
590 if (symbol
->tag
[0] == '\"')
592 /* use literal string only if given a symbol with an alias */
594 symbol
= symbol
->alias
;
599 /* Don't #define nonliteral tokens whose names contain periods
600 or '$' (as does the default value of the EOF token). */
601 if (strchr (symbol
->tag
, '.') || strchr (symbol
->tag
, '$'))
604 fprintf (out
, "# define %s\t%d\n",
605 symbol
->tag
, number
);
609 /* FIXME: This is probably wrong, and should be just as
611 fprintf (out
, "# define T%s\t%d\n", symbol
->tag
, symbol
->value
);
619 save_column (int symbol
, int default_state
)
626 int symno
= symbol
- ntokens
+ nstates
;
628 short begin
= goto_map
[symbol
];
629 short end
= goto_map
[symbol
+ 1];
632 for (i
= begin
; i
< end
; i
++)
633 if (to_state
[i
] != default_state
)
639 froms
[symno
] = sp1
= sp
= XCALLOC (short, count
);
640 tos
[symno
] = sp2
= XCALLOC (short, count
);
642 for (i
= begin
; i
< end
; i
++)
643 if (to_state
[i
] != default_state
)
645 *sp1
++ = from_state
[i
];
646 *sp2
++ = to_state
[i
];
649 tally
[symno
] = count
;
650 width
[symno
] = sp1
[-1] - sp
[0] + 1;
654 default_goto (int symbol
)
657 int m
= goto_map
[symbol
];
658 int n
= goto_map
[symbol
+ 1];
659 int default_state
= -1;
665 for (i
= 0; i
< nstates
; i
++)
668 for (i
= m
; i
< n
; i
++)
669 state_count
[to_state
[i
]]++;
671 for (i
= 0; i
< nstates
; i
++)
672 if (state_count
[i
] > max
)
674 max
= state_count
[i
];
678 return default_state
;
682 /*-------------------------------------------------------------------.
683 | Figure out what to do after reducing with each rule, depending on |
684 | the saved state from before the beginning of parsing the data that |
685 | matched this rule. |
687 | The YYDEFGOTO table is output now. The detailed info is saved for |
688 | putting into YYTABLE later. |
689 `-------------------------------------------------------------------*/
695 short *yydefgoto
= XMALLOC (short, nsyms
- ntokens
);
697 state_count
= XCALLOC (short, nstates
);
698 for (i
= ntokens
; i
< nsyms
; ++i
)
700 int default_state
= default_goto (i
);
701 save_column (i
, default_state
);
702 yydefgoto
[i
- ntokens
] = default_state
;
705 output_table_data (&format_obstack
, yydefgoto
,
706 yydefgoto
[0], 1, nsyms
- ntokens
);
707 muscle_insert ("defgoto", obstack_finish (&format_obstack
));
714 /* The next few functions decide how to pack the actions and gotos
715 information into yytable. */
722 order
= XCALLOC (short, nvectors
);
725 for (i
= 0; i
< nvectors
; i
++)
731 int j
= nentries
- 1;
733 while (j
>= 0 && (width
[order
[j
]] < w
))
736 while (j
>= 0 && (width
[order
[j
]] == w
) && (tally
[order
[j
]] < t
))
739 for (k
= nentries
- 1; k
> j
; k
--)
740 order
[k
+ 1] = order
[k
];
749 matching_state (int vector
)
751 int i
= order
[vector
];
762 for (prev
= vector
- 1; prev
>= 0; prev
--)
768 if (width
[j
] != w
|| tally
[j
] != t
)
771 for (k
= 0; match
&& k
< t
; k
++)
772 if (tos
[j
][k
] != tos
[i
][k
] || froms
[j
][k
] != froms
[i
][k
])
784 pack_vector (int vector
)
786 int i
= order
[vector
];
790 short *from
= froms
[i
];
795 for (j
= lowzero
- from
[0]; j
< MAXTABLE
; j
++)
800 for (k
= 0; ok
&& k
< t
; k
++)
804 fatal (_("maximum table size (%d) exceeded"), MAXTABLE
);
810 for (k
= 0; ok
&& k
< vector
; k
++)
816 for (k
= 0; k
< t
; k
++)
820 check
[loc
] = from
[k
];
823 while (table
[lowzero
] != 0)
832 #define pack_vector_succeeded 0
833 assert (pack_vector_succeeded
);
845 base
= XCALLOC (short, nvectors
);
846 pos
= XCALLOC (short, nentries
);
847 table
= XCALLOC (short, MAXTABLE
);
848 check
= XCALLOC (short, MAXTABLE
);
853 for (i
= 0; i
< nvectors
; i
++)
856 for (i
= 0; i
< MAXTABLE
; i
++)
859 for (i
= 0; i
< nentries
; i
++)
861 state
= matching_state (i
);
864 place
= pack_vector (i
);
869 base
[order
[i
]] = place
;
872 for (i
= 0; i
< nvectors
; i
++)
883 /* the following functions output yytable, yycheck
884 and the vectors whose elements index the portion starts */
890 output_table_data (&format_obstack
, base
,
891 base
[0], 1, nstates
);
892 muscle_insert ("pact", obstack_finish (&format_obstack
));
895 output_table_data (&format_obstack
, base
,
896 base
[nstates
], nstates
+ 1, nvectors
);
897 muscle_insert ("pgoto", obstack_finish (&format_obstack
));
906 output_table_data (&format_obstack
, table
,
907 table
[0], 1, high
+ 1);
908 muscle_insert ("table", obstack_finish (&format_obstack
));
916 output_table_data (&format_obstack
, check
,
917 check
[0], 1, high
+ 1);
918 muscle_insert ("check", obstack_finish (&format_obstack
));
922 /* compute and output yydefact, yydefgoto, yypact, yypgoto, yytable
926 output_actions (void)
929 nvectors
= nstates
+ nvars
;
931 froms
= XCALLOC (short *, nvectors
);
932 tos
= XCALLOC (short *, nvectors
);
933 tally
= XCALLOC (short, nvectors
);
934 width
= XCALLOC (short, nvectors
);
941 XFREE (goto_map
+ ntokens
);
953 for (i
= 0; i
< nstates
; ++i
)
955 free (states
[i
]->shifts
);
956 XFREE (states
[i
]->reductions
);
957 free (states
[i
]->errs
);
964 /*---------------------------.
965 | Call the skeleton parser. |
966 `---------------------------*/
969 output_skeleton (void)
971 /* Find the right skeleton file. */
975 skeleton
= skeleton_find ("BISON_HAIRY", BISON_HAIRY
);
977 skeleton
= skeleton_find ("BISON_SIMPLE", BISON_SIMPLE
);
980 /* Parse the skeleton file and output the needed parsers. */
981 muscle_insert ("skeleton", skeleton
);
982 process_skeleton (skeleton
);
988 MUSCLE_INSERT_INT ("last", high
);
989 MUSCLE_INSERT_INT ("flag", MINSHORT
);
990 MUSCLE_INSERT_INT ("pure", pure_parser
);
991 MUSCLE_INSERT_INT ("nsym", nsyms
);
992 MUSCLE_INSERT_INT ("debug", debug_flag
);
993 MUSCLE_INSERT_INT ("final", final_state
);
994 MUSCLE_INSERT_INT ("maxtok", max_user_token_number
);
995 MUSCLE_INSERT_INT ("error-verbose", error_verbose
);
996 MUSCLE_INSERT_STRING ("prefix", spec_name_prefix
? spec_name_prefix
: "yy");
998 /* FIXME: This is wrong: the muscles should decide whether they hold
999 a copy or not, but the situation is too obscure currently. */
1000 MUSCLE_INSERT_STRING ("output-infix", output_infix
? output_infix
: "");
1001 MUSCLE_INSERT_STRING ("output-prefix", short_base_name
);
1003 MUSCLE_INSERT_INT ("nnts", nvars
);
1004 MUSCLE_INSERT_INT ("nrules", nrules
);
1005 MUSCLE_INSERT_INT ("nstates", nstates
);
1006 MUSCLE_INSERT_INT ("ntokens", ntokens
);
1008 MUSCLE_INSERT_INT ("locations-flag", locations_flag
);
1011 /*-------------------------.
1012 | Output the header file. |
1013 `-------------------------*/
1016 header_output (void)
1019 FILE *out
= xfopen (spec_defines_file
, "w");
1020 char *macro_name
= compute_header_macro ();
1022 fprintf (out
, "#ifndef %s\n", macro_name
);
1023 fprintf (out
, "# define %s\n\n", macro_name
);
1025 token_definitions_output (out
, &dummy_line
);
1030 # define YYSTYPE yystype\n\
1032 muscle_find ("stype"));
1035 fprintf (out
, "\nextern YYSTYPE %slval;\n",
1036 spec_name_prefix
? spec_name_prefix
: "yy");
1040 fputs ("\n\n", out
);
1043 typedef struct yyltype\n\
1046 int first_column;\n\
1050 # define YYLTYPE yyltype\n\
1053 fprintf (out
, "\nextern YYLTYPE %slloc;\n",
1054 spec_name_prefix
? spec_name_prefix
: "yy");
1057 if (semantic_parser
)
1061 for (i
= ntokens
; i
< nsyms
; i
++)
1062 /* don't make these for dummy nonterminals made by gensym. */
1063 if (*symbols
[i
]->tag
!= '@')
1064 fprintf (out
, "# define NT%s\t%d\n", symbols
[i
]->tag
, i
);
1067 fprintf (out
, "\n#endif /* not %s */\n", macro_name
);
1073 /*----------------------------------------------------------.
1074 | Output the parsing tables and the parser code to ftable. |
1075 `----------------------------------------------------------*/
1080 obstack_init (&format_obstack
);
1082 output_token_translations ();
1086 if (semantic_parser
)
1088 output_rule_data ();
1092 /* Copy definitions in directive. */
1093 obstack_1grow (&attrs_obstack
, 0);
1094 muscle_insert ("prologue", obstack_finish (&attrs_obstack
));
1096 /* Process the selected skeleton file. */
1099 /* Output the header if needed. */
1104 obstack_free (&muscle_obstack
, NULL
);
1105 obstack_free (&format_obstack
, NULL
);
1106 obstack_free (&action_obstack
, NULL
);
1107 obstack_free (&attrs_obstack
, NULL
);