1 /* Output the generated parsing program for bison,
2 Copyright (C) 1984, 1986, 1989, 1992, 2000, 2001, 2002
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.
25 YYTRANSLATE = vector mapping yylex's token numbers into bison's
28 YYTNAME = vector of string-names indexed by bison token number.
30 YYTOKNUM = vector of yylex token numbers corresponding to entries
33 YYRLINE = vector of line-numbers of all rules. For yydebug
36 YYRHS = vector of items of all rules. This is exactly what RITEMS
37 contains. For yydebug and for semantic parser.
39 YYPRHS[R] = index in YYRHS of first item for rule R.
41 YYR1[R] = symbol number of symbol that rule R derives.
43 YYR2[R] = number of symbols composing right hand side of rule R.
45 YYSTOS[S] = the symbol number of the symbol that leads to state S.
47 YYDEFACT[S] = default rule to reduce with in state s, when YYTABLE
48 doesn't specify something else to do. Zero means the default is an
51 YYDEFGOTO[I] = default state to go to after a reduction of a rule
52 that generates variable NTOKENS + I, except when YYTABLE specifies
55 YYPACT[S] = index in YYTABLE of the portion describing state S.
56 The lookahead token's type is used to index that portion to find
59 If the value in YYTABLE is positive, we shift the token and go to
62 If the value is negative, it is minus a rule number to reduce by.
64 If the value is zero, the default action from YYDEFACT[S] is used.
66 YYPGOTO[I] = the index in YYTABLE of the portion describing what to
67 do after reducing a rule that derives variable I + NTOKENS. This
68 portion is indexed by the parser state number, S, as of before the
69 text for this nonterminal was read. The value from YYTABLE is the
70 state to go to if the corresponding value in YYCHECK is S.
72 YYTABLE = a vector filled with portions for different uses, found
73 via YYPACT and YYPGOTO.
75 YYCHECK = a vector indexed in parallel with YYTABLE. It indicates,
76 in a roundabout way, the bounds of the portion you are trying to
79 Suppose that the portion of YYTABLE starts at index P and the index
80 to be examined within the portion is I. Then if YYCHECK[P+I] != I,
81 I is outside the bounds of what is actually allocated, and the
82 default (from YYDEFACT or YYDEFGOTO) should be used. Otherwise,
83 YYTABLE[P+I] should be used.
85 YYFINAL = the state number of the termination state. YYFLAG = most
86 negative short int. Used to flag ?? */
98 #include "conflicts.h"
101 /* Several tables will be indexed both by state and nonterminal
102 numbers. We call `vector' such a thing (= either a state or a
105 Of course vector_number_t ought to be wide enough to contain
106 state_number_t and symbol_number_t. */
107 typedef short vector_number_t
;
108 #define VECTOR_NUMBER_MAX ((vector_number_t) SHRT_MAX)
109 #define VECTOR_NUMBER_MIN ((vector_number_t) SHRT_MIN)
110 #define state_number_to_vector_number(State) \
111 ((vector_number_t) State)
112 #define symbol_number_to_vector_number(Symbol) \
113 ((vector_number_t) (state_number_as_int (nstates) + Symbol - ntokens))
118 /* FROMS and TOS are indexed by vector_number_t.
120 If VECTOR is a nonterminal, (FROMS[VECTOR], TOS[VECTOR]) form an
121 array of state numbers of the non defaulted GOTO on VECTOR.
123 If VECTOR is a state, TOS[VECTOR] is the array of actions to do on
124 the (array of) symbols FROMS[VECTOR].
126 In both cases, TALLY[VECTOR] is the size of the arrays
127 FROMS[VECTOR], TOS[VECTOR]; and WIDTH[VECTOR] =
128 (FROMS[VECTOR][SIZE] - FROMS[VECTOR][0] + 1) where SIZE =
131 FROMS therefore contains symbol_number_t and action_number_t,
132 TOS state_number_t and action_number_t,
134 WIDTH differences of FROMS.
136 Let base_t be the type of FROMS, TOS, and WIDTH. */
137 #define BASE_MAX ((base_t) INT_MAX)
138 #define BASE_MIN ((base_t) INT_MIN)
140 static base_t
**froms
= NULL
;
141 static base_t
**tos
= NULL
;
142 static unsigned int **conflict_tos
= NULL
;
143 static short *tally
= NULL
;
144 static base_t
*width
= NULL
;
147 /* For a given state, N = ACTROW[SYMBOL]:
149 If N = 0, stands for `run the default action'.
150 If N = MIN, stands for `raise a parse error'.
151 If N > 0, stands for `shift SYMBOL and go to n'.
152 If N < 0, stands for `reduce -N'. */
153 typedef short action_t
;
154 #define ACTION_MAX ((action_t) SHRT_MAX)
155 #define ACTION_MIN ((action_t) SHRT_MIN)
157 static action_t
*actrow
= NULL
;
159 /* FROMS and TOS are reordered to be compressed. ORDER[VECTOR] is the
160 new vector number of VECTOR. We skip `empty' vectors (i.e.,
161 TALLY[VECTOR] = 0), and call these `entries'. */
162 static vector_number_t
*order
= NULL
;
166 /* A distinguished value of BASE, negative infinite. During the
167 computation equals to BASE_MIN, later mapped to BASE_NINF to
168 keep parser tables small. */
169 base_t base_ninf
= 0;
170 static base_t
*pos
= NULL
;
172 static unsigned int *conflrow
= NULL
;
173 unsigned int *conflict_table
= NULL
;
174 unsigned int *conflict_list
= NULL
;
175 int conflict_list_cnt
;
176 static int conflict_list_free
;
178 /* TABLE_SIZE is the allocated size of both TABLE and CHECK. We start
179 with more or less the original hard-coded value (which was
181 static size_t table_size
= 32768;
182 base_t
*table
= NULL
;
183 base_t
*check
= NULL
;
184 /* The value used in TABLE to denote explicit parse errors
185 (%nonassoc), a negative infinite. First defaults to ACTION_MIN,
186 but in order to keep small tables, renumbered as TABLE_ERROR, which
187 is the smallest (non error) value minus 1. */
188 base_t table_ninf
= 0;
192 state_number_t
*yydefgoto
;
193 rule_number_t
*yydefact
;
195 /*----------------------------------------------------------------.
196 | If TABLE (and CHECK) appear to be small to be addressed at |
197 | DESIRED, grow them. Note that TABLE[DESIRED] is to be used, so |
198 | the desired size is at least DESIRED + 1. |
199 `----------------------------------------------------------------*/
202 table_grow (size_t desired
)
204 size_t old_size
= table_size
;
206 while (table_size
<= desired
)
209 if (trace_flag
& trace_resource
)
210 fprintf (stderr
, "growing table and check from: %d to %d\n",
211 old_size
, table_size
);
213 table
= XREALLOC (table
, base_t
, table_size
);
214 check
= XREALLOC (check
, base_t
, table_size
);
216 conflict_table
= XREALLOC (conflict_table
, unsigned int, table_size
);
218 for (/* Nothing. */; old_size
< table_size
; ++old_size
)
221 check
[old_size
] = -1;
228 /*-------------------------------------------------------------------.
229 | For GLR parsers, for each conflicted token in STATE, as indicated |
230 | by non-zero entries in CONFLROW, create a list of possible |
231 | reductions that are alternatives to the shift or reduction |
232 | currently recorded for that token in STATE. Store the alternative |
233 | reductions followed by a 0 in CONFLICT_LIST, updating |
234 | CONFLICT_LIST_CNT, and storing an index to the start of the list |
235 | back into CONFLROW. |
236 `-------------------------------------------------------------------*/
239 conflict_row (state_t
*state
)
246 for (j
= 0; j
< ntokens
; j
+= 1)
249 conflrow
[j
] = conflict_list_cnt
;
251 /* Find all reductions for token J, and record all that do not
253 for (i
= 0; i
< state
->nlookaheads
; i
+= 1)
254 if (bitset_test (state
->lookaheads
[i
], j
)
256 != rule_number_as_item_number (state
->lookaheads_rule
[i
]->number
)))
258 assert (conflict_list_free
> 0);
259 conflict_list
[conflict_list_cnt
]
260 = state
->lookaheads_rule
[i
]->number
+ 1;
261 conflict_list_cnt
+= 1;
262 conflict_list_free
-= 1;
265 /* Leave a 0 at the end. */
266 assert (conflict_list_free
> 0);
267 conflict_list_cnt
+= 1;
268 conflict_list_free
-= 1;
273 /*------------------------------------------------------------------.
274 | Decide what to do for each type of token if seen as the lookahead |
275 | token in specified state. The value returned is used as the |
276 | default action (yydefact) for the state. In addition, ACTROW is |
277 | filled with what to do for each kind of token, index by symbol |
278 | number, with zero meaning do the default action. The value |
279 | ACTION_MIN, a very negative number, means this situation is an |
280 | error. The parser recognizes this value specially. |
282 | This is where conflicts are resolved. The loop over lookahead |
283 | rules considered lower-numbered rules last, and the last rule |
284 | considered that likes a token gets to handle it. |
286 | For GLR parsers, also sets CONFLROW[SYM] to an index into |
287 | CONFLICT_LIST iff there is an unresolved conflict (s/r or r/r) |
288 | with symbol SYM. The default reduction is not used for a symbol |
289 | that has any such conflicts. |
290 `------------------------------------------------------------------*/
293 action_row (state_t
*state
)
296 rule_t
*default_rule
= NULL
;
297 reductions_t
*redp
= state
->reductions
;
298 transitions_t
*transitions
= state
->transitions
;
299 errs_t
*errp
= state
->errs
;
300 /* Set to nonzero to inhibit having any default reduction. */
304 for (i
= 0; i
< ntokens
; i
++)
305 actrow
[i
] = conflrow
[i
] = 0;
310 bitset_iterator biter
;
311 /* loop over all the rules available here which require
313 for (i
= state
->nlookaheads
- 1; i
>= 0; --i
)
314 /* and find each token which the rule finds acceptable
316 BITSET_FOR_EACH (biter
, state
->lookaheads
[i
], j
, 0)
318 /* and record this rule as the rule to use if that
321 conflicted
= conflrow
[j
] = 1;
322 actrow
[j
] = rule_number_as_item_number (state
->lookaheads_rule
[i
]->number
);
326 /* Now see which tokens are allowed for shifts in this state. For
327 them, record the shift as the thing to do. So shift is preferred
329 FOR_EACH_SHIFT (transitions
, i
)
331 symbol_number_t symbol
= TRANSITION_SYMBOL (transitions
, i
);
332 state_t
*shift_state
= transitions
->states
[i
];
334 if (actrow
[symbol
] != 0)
335 conflicted
= conflrow
[symbol
] = 1;
336 actrow
[symbol
] = state_number_as_int (shift_state
->number
);
338 /* Do not use any default reduction if there is a shift for
340 if (symbol
== errtoken
->number
)
344 /* See which tokens are an explicit error in this state (due to
345 %nonassoc). For them, record ACTION_MIN as the action. */
346 for (i
= 0; i
< errp
->num
; i
++)
348 symbol_t
*symbol
= errp
->symbols
[i
];
349 actrow
[symbol
->number
] = ACTION_MIN
;
352 /* Now find the most common reduction and make it the default action
355 if (redp
->num
>= 1 && !nodefault
)
357 if (state
->consistent
)
358 default_rule
= redp
->rules
[0];
362 for (i
= 0; i
< state
->nlookaheads
; i
++)
365 rule_t
*rule
= state
->lookaheads_rule
[i
];
368 for (j
= 0; j
< ntokens
; j
++)
369 if (actrow
[j
] == rule_number_as_item_number (rule
->number
))
379 /* GLR parsers need space for conflict lists, so we can't
380 default conflicted entries. For non-conflicted entries
381 or as long as we are not building a GLR parser,
382 actions that match the default are replaced with zero,
383 which means "use the default". */
388 for (j
= 0; j
< ntokens
; j
++)
389 if (actrow
[j
] == rule_number_as_item_number (default_rule
->number
)
390 && ! (glr_parser
&& conflrow
[j
]))
396 /* Find the rules which are reduced. */
399 for (i
= 0; i
< ntokens
; i
++)
400 if (actrow
[i
] < 0 && actrow
[i
] != ACTION_MIN
)
401 rules
[item_number_as_rule_number (actrow
[i
])].useful
= TRUE
;
403 default_rule
->useful
= TRUE
;
406 /* If have no default rule, the default is an error.
407 So replace any action which says "error" with "use default". */
410 for (i
= 0; i
< ntokens
; i
++)
411 if (actrow
[i
] == ACTION_MIN
)
415 conflict_row (state
);
421 /*--------------------------------------------.
422 | Set FROMS, TOS, TALLY and WIDTH for STATE. |
423 `--------------------------------------------*/
426 save_row (state_number_t state
)
433 unsigned int *sp3
= NULL
;
435 /* Number of non default actions in STATE. */
437 for (i
= 0; i
< ntokens
; i
++)
444 /* Allocate non defaulted actions. */
445 froms
[state
] = sp1
= sp
= XCALLOC (base_t
, count
);
446 tos
[state
] = sp2
= XCALLOC (base_t
, count
);
448 conflict_tos
[state
] = sp3
= XCALLOC (unsigned int, count
);
450 conflict_tos
[state
] = NULL
;
452 /* Store non defaulted actions. */
453 for (i
= 0; i
< ntokens
; i
++)
459 *sp3
++ = conflrow
[i
];
462 tally
[state
] = count
;
463 width
[state
] = sp1
[-1] - sp
[0] + 1;
467 /*------------------------------------------------------------------.
468 | Figure out the actions for the specified state, indexed by |
469 | lookahead token type. |
471 | The YYDEFACT table is output now. The detailed info is saved for |
472 | putting into YYTABLE later. |
473 `------------------------------------------------------------------*/
480 int nconflict
= conflicts_total_count ();
482 yydefact
= XCALLOC (rule_number_t
, nstates
);
484 actrow
= XCALLOC (action_t
, ntokens
);
485 conflrow
= XCALLOC (unsigned int, ntokens
);
487 /* Now that the parser was computed, we can find which rules are
488 really reduced, and which are not because of SR or RR conflicts.
491 for (r
= 0; r
< nrules
; ++r
)
492 rules
[r
].useful
= FALSE
;
496 conflict_list
= XCALLOC (unsigned int, 1 + 2 * nconflict
);
497 conflict_list_free
= 2 * nconflict
;
498 conflict_list_cnt
= 1;
501 conflict_list_free
= conflict_list_cnt
= 0;
503 for (i
= 0; i
< nstates
; ++i
)
505 rule_t
*default_rule
= action_row (states
[i
]);
506 yydefact
[i
] = default_rule
? default_rule
->number
+ 1 : 0;
511 for (r
= 0; r
< nrules
; ++r
)
512 if (!rules
[r
].useful
)
514 LOCATION_PRINT (stderr
, rules
[r
].location
);
515 fprintf (stderr
, ": %s: %s: ",
516 _("warning"), _("rule never reduced because of conflicts"));
517 rule_print (&rules
[r
], stderr
);
525 /*------------------------------------------------------------------.
526 | Compute FROMS[VECTOR], TOS[VECTOR], TALLY[VECTOR], WIDTH[VECTOR], |
527 | i.e., the information related to non defaulted GOTO on the nterm |
530 | DEFAULT_STATE is the principal destination on SYMBOL, i.e., the |
531 | default GOTO destination on SYMBOL. |
532 `------------------------------------------------------------------*/
535 save_column (symbol_number_t symbol
, state_number_t default_state
)
542 vector_number_t symno
= symbol_number_to_vector_number (symbol
);
544 goto_number_t begin
= goto_map
[symbol
];
545 goto_number_t end
= goto_map
[symbol
+ 1];
547 /* Number of non default GOTO. */
549 for (i
= begin
; i
< end
; i
++)
550 if (to_state
[i
] != default_state
)
556 /* Allocate room for non defaulted gotos. */
557 froms
[symno
] = sp1
= sp
= XCALLOC (base_t
, count
);
558 tos
[symno
] = sp2
= XCALLOC (base_t
, count
);
560 /* Store the state numbers of the non defaulted gotos. */
561 for (i
= begin
; i
< end
; i
++)
562 if (to_state
[i
] != default_state
)
564 *sp1
++ = from_state
[i
];
565 *sp2
++ = to_state
[i
];
568 tally
[symno
] = count
;
569 width
[symno
] = sp1
[-1] - sp
[0] + 1;
573 /*----------------------------------------------------------------.
574 | Return `the' most common destination GOTO on SYMBOL (a nterm). |
575 `----------------------------------------------------------------*/
577 static state_number_t
578 default_goto (symbol_number_t symbol
, short state_count
[])
582 goto_number_t m
= goto_map
[symbol
];
583 goto_number_t n
= goto_map
[symbol
+ 1];
584 state_number_t default_state
= (state_number_t
) -1;
588 return (state_number_t
) -1;
590 for (s
= 0; s
< nstates
; s
++)
593 for (i
= m
; i
< n
; i
++)
594 state_count
[to_state
[i
]]++;
596 for (s
= 0; s
< nstates
; s
++)
597 if (state_count
[s
] > max
)
599 max
= state_count
[s
];
603 return default_state
;
607 /*-------------------------------------------------------------------.
608 | Figure out what to do after reducing with each rule, depending on |
609 | the saved state from before the beginning of parsing the data that |
610 | matched this rule. |
612 | The YYDEFGOTO table is output now. The detailed info is saved for |
613 | putting into YYTABLE later. |
614 `-------------------------------------------------------------------*/
620 short *state_count
= XCALLOC (short, nstates
);
621 yydefgoto
= XMALLOC (state_number_t
, nvars
);
623 /* For a given nterm I, STATE_COUNT[S] is the number of times there
624 is a GOTO to S on I. */
625 for (i
= ntokens
; i
< nsyms
; ++i
)
627 state_number_t default_state
= default_goto (i
, state_count
);
628 save_column (i
, default_state
);
629 yydefgoto
[i
- ntokens
] = default_state
;
635 /*------------------------------------------------------------------.
636 | Compute ORDER, a reordering of vectors, in order to decide how to |
637 | pack the actions and gotos information into yytable. |
638 `------------------------------------------------------------------*/
647 for (i
= 0; i
< nvectors
; i
++)
653 int j
= nentries
- 1;
655 while (j
>= 0 && (width
[order
[j
]] < w
))
658 while (j
>= 0 && (width
[order
[j
]] == w
) && (tally
[order
[j
]] < t
))
661 for (k
= nentries
- 1; k
> j
; k
--)
662 order
[k
+ 1] = order
[k
];
670 /* If VECTOR is a state which actions (reflected by FROMS, TOS, TALLY
671 and WIDTH of VECTOR) are common to a previous state, return this
674 In any other case, return -1. */
676 static state_number_t
677 matching_state (vector_number_t vector
)
679 vector_number_t i
= order
[vector
];
684 /* If VECTOR is a nterm, return -1. */
685 if (i
>= (int) nstates
)
691 for (prev
= vector
- 1; prev
>= 0; prev
--)
693 vector_number_t j
= order
[prev
];
697 /* Given how ORDER was computed, if the WIDTH or TALLY is
698 different, there cannot be a matching state. */
699 if (width
[j
] != w
|| tally
[j
] != t
)
702 for (k
= 0; match
&& k
< t
; k
++)
703 if (tos
[j
][k
] != tos
[i
][k
] || froms
[j
][k
] != froms
[i
][k
])
715 pack_vector (vector_number_t vector
)
717 vector_number_t i
= order
[vector
];
721 base_t
*from
= froms
[i
];
723 unsigned int *conflict_to
= conflict_tos
[i
];
727 for (j
= lowzero
- from
[0]; j
< (int) table_size
; j
++)
732 for (k
= 0; ok
&& k
< t
; k
++)
734 loc
= j
+ state_number_as_int (from
[k
]);
735 if (loc
> (int) table_size
)
742 for (k
= 0; ok
&& k
< vector
; k
++)
748 for (k
= 0; k
< t
; k
++)
752 if (glr_parser
&& conflict_to
!= NULL
)
753 conflict_table
[loc
] = conflict_to
[k
];
754 check
[loc
] = from
[k
];
757 while (table
[lowzero
] != 0)
763 if (j
< BASE_MIN
|| BASE_MAX
< j
)
764 fatal ("base_t too small to hold %d\n", j
);
768 #define pack_vector_succeeded 0
769 assert (pack_vector_succeeded
);
774 /*-------------------------------------------------------------.
775 | Remap the negative infinite in TAB from NINF to the greatest |
776 | possible smallest value. Return it. |
778 | In most case this allows us to use shorts instead of ints in |
780 `-------------------------------------------------------------*/
783 table_ninf_remap (base_t tab
[], size_t size
, base_t ninf
)
788 for (i
= 0; i
< size
; i
++)
789 if (tab
[i
] < res
&& tab
[i
] != ninf
)
794 for (i
= 0; i
< size
; i
++)
806 base
= XCALLOC (base_t
, nvectors
);
807 pos
= XCALLOC (base_t
, nentries
);
808 table
= XCALLOC (base_t
, table_size
);
810 conflict_table
= XCALLOC (unsigned int, table_size
);
811 check
= XCALLOC (base_t
, table_size
);
816 for (i
= 0; i
< nvectors
; i
++)
819 for (i
= 0; i
< (int) table_size
; i
++)
822 for (i
= 0; i
< nentries
; i
++)
824 state_number_t state
= matching_state (i
);
828 /* A new set of state actions, or a nonterminal. */
829 place
= pack_vector (i
);
831 /* Action of I were already coded for STATE. */
835 base
[order
[i
]] = place
;
838 /* Use the greatest possible negative infinites. */
839 base_ninf
= table_ninf_remap (base
, nvectors
, BASE_MIN
);
840 table_ninf
= table_ninf_remap (table
, high
+ 1, ACTION_MIN
);
842 for (i
= 0; i
< nvectors
; i
++)
846 XFREE (conflict_tos
[i
]);
857 /*-----------------------------------------------------------------.
858 | Compute and output yydefact, yydefgoto, yypact, yypgoto, yytable |
860 `-----------------------------------------------------------------*/
863 tables_generate (void)
865 /* That's a poor way to make sure the sizes are properly corelated,
866 in particular the signedness is not taking into account, but it's
868 assert (sizeof (nvectors
) >= sizeof (nstates
));
869 assert (sizeof (nvectors
) >= sizeof (nvars
));
871 nvectors
= state_number_as_int (nstates
) + nvars
;
873 froms
= XCALLOC (base_t
*, nvectors
);
874 tos
= XCALLOC (base_t
*, nvectors
);
875 conflict_tos
= XCALLOC (unsigned int *, nvectors
);
876 tally
= XCALLOC (short, nvectors
);
877 width
= XCALLOC (base_t
, nvectors
);
884 XFREE (goto_map
+ ntokens
);
888 order
= XCALLOC (vector_number_t
, nvectors
);
898 /*-------------------------.
899 | Free the parser tables. |
900 `-------------------------*/
906 free (conflict_table
);
907 free (conflict_list
);