1 /* Output the generated parsing program for Bison.
3 Copyright (C) 1984, 1986, 1989, 1992, 2000, 2001, 2002, 2003, 2004,
4 2005, 2006, 2009 Free Software Foundation, Inc.
6 This file is part of Bison, the GNU Compiler Compiler.
8 This program is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
28 #include "conflicts.h"
33 #include "muscle-tab.h"
38 /* Several tables are indexed both by state and nonterminal numbers.
39 We call such an index a `vector'; i.e., a vector is either a state
40 or a nonterminal number.
42 Of course vector_number_t ought to be wide enough to contain
43 state_number and symbol_number. */
44 typedef int vector_number
;
46 #if 0 /* Not currently used. */
47 static inline vector_number
48 state_number_to_vector_number (state_number s
)
54 static inline vector_number
55 symbol_number_to_vector_number (symbol_number sym
)
57 return state_number_as_int (nstates
) + sym
- ntokens
;
63 /* FROMS and TOS are indexed by vector_number.
65 If VECTOR is a nonterminal, (FROMS[VECTOR], TOS[VECTOR]) form an
66 array of state numbers of the non defaulted GOTO on VECTOR.
68 If VECTOR is a state, TOS[VECTOR] is the array of actions to do on
69 the (array of) symbols FROMS[VECTOR].
71 In both cases, TALLY[VECTOR] is the size of the arrays
72 FROMS[VECTOR], TOS[VECTOR]; and WIDTH[VECTOR] =
73 (FROMS[VECTOR][SIZE] - FROMS[VECTOR][0] + 1) where SIZE =
76 FROMS therefore contains symbol_number and action_number,
77 TOS state_number and action_number,
79 WIDTH differences of FROMS.
81 Let base_number be the type of FROMS, TOS, and WIDTH. */
82 #define BASE_MAXIMUM INT_MAX
83 #define BASE_MINIMUM INT_MIN
85 static base_number
**froms
;
86 static base_number
**tos
;
87 static unsigned int **conflict_tos
;
89 static base_number
*width
;
92 /* For a given state, N = ACTROW[SYMBOL]:
94 If N = 0, stands for `run the default action'.
95 If N = MIN, stands for `raise a syntax error'.
96 If N > 0, stands for `shift SYMBOL and go to n'.
97 If N < 0, stands for `reduce -N'. */
98 typedef int action_number
;
99 #define ACTION_NUMBER_MINIMUM INT_MIN
101 static action_number
*actrow
;
103 /* FROMS and TOS are reordered to be compressed. ORDER[VECTOR] is the
104 new vector number of VECTOR. We skip `empty' vectors (i.e.,
105 TALLY[VECTOR] = 0), and call these `entries'. */
106 static vector_number
*order
;
109 base_number
*base
= NULL
;
110 /* A distinguished value of BASE, negative infinite. During the
111 computation equals to BASE_MINIMUM, later mapped to BASE_NINF to
112 keep parser tables small. */
113 base_number base_ninf
= 0;
114 static base_number
*pos
= NULL
;
116 static unsigned int *conflrow
;
117 unsigned int *conflict_table
;
118 unsigned int *conflict_list
;
119 int conflict_list_cnt
;
120 static int conflict_list_free
;
122 /* TABLE_SIZE is the allocated size of both TABLE and CHECK. We start
123 with more or less the original hard-coded value (which was
125 static int table_size
= 32768;
128 /* The value used in TABLE to denote explicit syntax errors
129 (%nonassoc), a negative infinite. First defaults to ACTION_NUMBER_MINIMUM,
130 but in order to keep small tables, renumbered as TABLE_ERROR, which
131 is the smallest (non error) value minus 1. */
132 base_number table_ninf
= 0;
136 state_number
*yydefgoto
;
137 rule_number
*yydefact
;
139 /*----------------------------------------------------------------.
140 | If TABLE (and CHECK) appear to be small to be addressed at |
141 | DESIRED, grow them. Note that TABLE[DESIRED] is to be used, so |
142 | the desired size is at least DESIRED + 1. |
143 `----------------------------------------------------------------*/
146 table_grow (int desired
)
148 int old_size
= table_size
;
150 while (table_size
<= desired
)
153 if (trace_flag
& trace_resource
)
154 fprintf (stderr
, "growing table and check from: %d to %d\n",
155 old_size
, table_size
);
157 table
= xnrealloc (table
, table_size
, sizeof *table
);
158 conflict_table
= xnrealloc (conflict_table
, table_size
,
159 sizeof *conflict_table
);
160 check
= xnrealloc (check
, table_size
, sizeof *check
);
162 for (/* Nothing. */; old_size
< table_size
; ++old_size
)
165 conflict_table
[old_size
] = 0;
166 check
[old_size
] = -1;
173 /*-------------------------------------------------------------------.
174 | For GLR parsers, for each conflicted token in S, as indicated |
175 | by non-zero entries in CONFLROW, create a list of possible |
176 | reductions that are alternatives to the shift or reduction |
177 | currently recorded for that token in S. Store the alternative |
178 | reductions followed by a 0 in CONFLICT_LIST, updating |
179 | CONFLICT_LIST_CNT, and storing an index to the start of the list |
180 | back into CONFLROW. |
181 `-------------------------------------------------------------------*/
184 conflict_row (state
*s
)
187 reductions
*reds
= s
->reductions
;
189 if (!nondeterministic_parser
)
192 for (j
= 0; j
< ntokens
; j
+= 1)
195 conflrow
[j
] = conflict_list_cnt
;
197 /* Find all reductions for token J, and record all that do not
199 for (i
= 0; i
< reds
->num
; i
+= 1)
200 if (bitset_test (reds
->lookahead_tokens
[i
], j
)
202 != rule_number_as_item_number (reds
->rules
[i
]->number
)))
204 aver (0 < conflict_list_free
);
205 conflict_list
[conflict_list_cnt
] = reds
->rules
[i
]->number
+ 1;
206 conflict_list_cnt
+= 1;
207 conflict_list_free
-= 1;
210 /* Leave a 0 at the end. */
211 aver (0 < conflict_list_free
);
212 conflict_list
[conflict_list_cnt
] = 0;
213 conflict_list_cnt
+= 1;
214 conflict_list_free
-= 1;
219 /*------------------------------------------------------------------.
220 | Decide what to do for each type of token if seen as the |
221 | lookahead in specified state. The value returned is used as the |
222 | default action (yydefact) for the state. In addition, ACTROW is |
223 | filled with what to do for each kind of token, index by symbol |
224 | number, with zero meaning do the default action. The value |
225 | ACTION_NUMBER_MINIMUM, a very negative number, means this |
226 | situation is an error. The parser recognizes this value |
229 | This is where conflicts are resolved. The loop over lookahead |
230 | rules considered lower-numbered rules last, and the last rule |
231 | considered that likes a token gets to handle it. |
233 | For GLR parsers, also sets CONFLROW[SYM] to an index into |
234 | CONFLICT_LIST iff there is an unresolved conflict (s/r or r/r) |
235 | with symbol SYM. The default reduction is not used for a symbol |
236 | that has any such conflicts. |
237 `------------------------------------------------------------------*/
240 action_row (state
*s
)
243 rule
*default_reduction
= NULL
;
244 reductions
*reds
= s
->reductions
;
245 transitions
*trans
= s
->transitions
;
246 errs
*errp
= s
->errs
;
247 /* Set to nonzero to inhibit having any default reduction. */
248 bool nodefault
= false;
249 bool conflicted
= false;
251 for (i
= 0; i
< ntokens
; i
++)
252 actrow
[i
] = conflrow
[i
] = 0;
254 if (reds
->lookahead_tokens
)
257 bitset_iterator biter
;
258 /* loop over all the rules available here which require
259 lookahead (in reverse order to give precedence to the first
261 for (i
= reds
->num
- 1; i
>= 0; --i
)
262 /* and find each token which the rule finds acceptable
264 BITSET_FOR_EACH (biter
, reds
->lookahead_tokens
[i
], j
, 0)
266 /* and record this rule as the rule to use if that
273 actrow
[j
] = rule_number_as_item_number (reds
->rules
[i
]->number
);
277 /* Now see which tokens are allowed for shifts in this state. For
278 them, record the shift as the thing to do. So shift is preferred
280 FOR_EACH_SHIFT (trans
, i
)
282 symbol_number sym
= TRANSITION_SYMBOL (trans
, i
);
283 state
*shift_state
= trans
->states
[i
];
285 if (actrow
[sym
] != 0)
290 actrow
[sym
] = state_number_as_int (shift_state
->number
);
292 /* Do not use any default reduction if there is a shift for
294 if (sym
== errtoken
->number
)
298 /* See which tokens are an explicit error in this state (due to
299 %nonassoc). For them, record ACTION_NUMBER_MINIMUM as the
301 for (i
= 0; i
< errp
->num
; i
++)
303 symbol
*sym
= errp
->symbols
[i
];
304 actrow
[sym
->number
] = ACTION_NUMBER_MINIMUM
;
307 /* Turn off default reductions where requested by the user. See
308 state_lookahead_tokens_count in lalr.c to understand when states are
309 labeled as consistent. */
311 char *default_reductions
=
312 muscle_percent_define_get ("lr.default-reductions");
313 if (0 != strcmp (default_reductions
, "all") && !s
->consistent
)
315 free (default_reductions
);
318 /* Now find the most common reduction and make it the default action
321 if (reds
->num
>= 1 && !nodefault
)
324 default_reduction
= reds
->rules
[0];
328 for (i
= 0; i
< reds
->num
; i
++)
331 rule
*r
= reds
->rules
[i
];
334 for (j
= 0; j
< ntokens
; j
++)
335 if (actrow
[j
] == rule_number_as_item_number (r
->number
))
341 default_reduction
= r
;
345 /* GLR parsers need space for conflict lists, so we can't
346 default conflicted entries. For non-conflicted entries
347 or as long as we are not building a GLR parser,
348 actions that match the default are replaced with zero,
349 which means "use the default". */
354 for (j
= 0; j
< ntokens
; j
++)
356 == rule_number_as_item_number (default_reduction
->number
)
357 && ! (nondeterministic_parser
&& conflrow
[j
]))
363 /* If have no default reduction, the default is an error.
364 So replace any action which says "error" with "use default". */
366 if (!default_reduction
)
367 for (i
= 0; i
< ntokens
; i
++)
368 if (actrow
[i
] == ACTION_NUMBER_MINIMUM
)
374 return default_reduction
;
378 /*----------------------------------------.
379 | Set FROMS, TOS, TALLY and WIDTH for S. |
380 `----------------------------------------*/
383 save_row (state_number s
)
392 /* Number of non default actions in S. */
394 for (i
= 0; i
< ntokens
; i
++)
401 /* Allocate non defaulted actions. */
402 froms
[s
] = sp
= sp1
= xnmalloc (count
, sizeof *sp1
);
403 tos
[s
] = sp2
= xnmalloc (count
, sizeof *sp2
);
404 conflict_tos
[s
] = sp3
=
405 nondeterministic_parser
? xnmalloc (count
, sizeof *sp3
) : NULL
;
407 /* Store non defaulted actions. */
408 for (i
= 0; i
< ntokens
; i
++)
413 if (nondeterministic_parser
)
414 *sp3
++ = conflrow
[i
];
418 width
[s
] = sp1
[-1] - sp
[0] + 1;
422 /*------------------------------------------------------------------.
423 | Figure out the actions for the specified state, indexed by |
424 | lookahead token type. |
426 | The YYDEFACT table is output now. The detailed info is saved for |
427 | putting into YYTABLE later. |
428 `------------------------------------------------------------------*/
437 int nconflict
= nondeterministic_parser
? conflicts_total_count () : 0;
439 yydefact
= xnmalloc (nstates
, sizeof *yydefact
);
441 actrow
= xnmalloc (ntokens
, sizeof *actrow
);
442 conflrow
= xnmalloc (ntokens
, sizeof *conflrow
);
444 conflict_list
= xnmalloc (1 + 2 * nconflict
, sizeof *conflict_list
);
445 conflict_list_free
= 2 * nconflict
;
446 conflict_list_cnt
= 1;
448 /* Find the rules which are reduced. */
449 if (!nondeterministic_parser
)
450 for (r
= 0; r
< nrules
; ++r
)
451 rules
[r
].useful
= false;
453 for (i
= 0; i
< nstates
; ++i
)
455 rule
*default_reduction
= action_row (states
[i
]);
456 yydefact
[i
] = default_reduction
? default_reduction
->number
+ 1 : 0;
459 /* Now that the parser was computed, we can find which rules are
460 really reduced, and which are not because of SR or RR
462 if (!nondeterministic_parser
)
464 for (j
= 0; j
< ntokens
; ++j
)
465 if (actrow
[j
] < 0 && actrow
[j
] != ACTION_NUMBER_MINIMUM
)
466 rules
[item_number_as_rule_number (actrow
[j
])].useful
= true;
468 rules
[yydefact
[i
] - 1].useful
= true;
477 /*------------------------------------------------------------------.
478 | Compute FROMS[VECTOR], TOS[VECTOR], TALLY[VECTOR], WIDTH[VECTOR], |
479 | i.e., the information related to non defaulted GOTO on the nterm |
482 | DEFAULT_STATE is the principal destination on SYM, i.e., the |
483 | default GOTO destination on SYM. |
484 `------------------------------------------------------------------*/
487 save_column (symbol_number sym
, state_number default_state
)
494 vector_number symno
= symbol_number_to_vector_number (sym
);
496 goto_number begin
= goto_map
[sym
- ntokens
];
497 goto_number end
= goto_map
[sym
- ntokens
+ 1];
499 /* Number of non default GOTO. */
501 for (i
= begin
; i
< end
; i
++)
502 if (to_state
[i
] != default_state
)
508 /* Allocate room for non defaulted gotos. */
509 froms
[symno
] = sp
= sp1
= xnmalloc (count
, sizeof *sp1
);
510 tos
[symno
] = sp2
= xnmalloc (count
, sizeof *sp2
);
512 /* Store the state numbers of the non defaulted gotos. */
513 for (i
= begin
; i
< end
; i
++)
514 if (to_state
[i
] != default_state
)
516 *sp1
++ = from_state
[i
];
517 *sp2
++ = to_state
[i
];
520 tally
[symno
] = count
;
521 width
[symno
] = sp1
[-1] - sp
[0] + 1;
525 /*-------------------------------------------------------------.
526 | Return `the' most common destination GOTO on SYM (a nterm). |
527 `-------------------------------------------------------------*/
530 default_goto (symbol_number sym
, size_t state_count
[])
534 goto_number m
= goto_map
[sym
- ntokens
];
535 goto_number n
= goto_map
[sym
- ntokens
+ 1];
536 state_number default_state
= -1;
542 for (s
= 0; s
< nstates
; s
++)
545 for (i
= m
; i
< n
; i
++)
546 state_count
[to_state
[i
]]++;
548 for (s
= 0; s
< nstates
; s
++)
549 if (state_count
[s
] > max
)
551 max
= state_count
[s
];
555 return default_state
;
559 /*-------------------------------------------------------------------.
560 | Figure out what to do after reducing with each rule, depending on |
561 | the saved state from before the beginning of parsing the data that |
562 | matched this rule. |
564 | The YYDEFGOTO table is output now. The detailed info is saved for |
565 | putting into YYTABLE later. |
566 `-------------------------------------------------------------------*/
572 size_t *state_count
= xnmalloc (nstates
, sizeof *state_count
);
573 yydefgoto
= xnmalloc (nvars
, sizeof *yydefgoto
);
575 /* For a given nterm I, STATE_COUNT[S] is the number of times there
576 is a GOTO to S on I. */
577 for (i
= ntokens
; i
< nsyms
; ++i
)
579 state_number default_state
= default_goto (i
, state_count
);
580 save_column (i
, default_state
);
581 yydefgoto
[i
- ntokens
] = default_state
;
587 /*------------------------------------------------------------------.
588 | Compute ORDER, a reordering of vectors, in order to decide how to |
589 | pack the actions and gotos information into yytable. |
590 `------------------------------------------------------------------*/
599 for (i
= 0; i
< nvectors
; i
++)
605 int j
= nentries
- 1;
607 while (j
>= 0 && (width
[order
[j
]] < w
))
610 while (j
>= 0 && (width
[order
[j
]] == w
) && (tally
[order
[j
]] < t
))
613 for (k
= nentries
- 1; k
> j
; k
--)
614 order
[k
+ 1] = order
[k
];
622 /* If VECTOR is a state which actions (reflected by FROMS, TOS, TALLY
623 and WIDTH of VECTOR) are common to a previous state, return this
626 In any other case, return -1. */
629 matching_state (vector_number vector
)
631 vector_number i
= order
[vector
];
636 /* If VECTOR is a nterm, return -1. */
643 /* If VECTOR has GLR conflicts, return -1 */
644 if (conflict_tos
[i
] != NULL
)
647 for (j
= 0; j
< t
; j
+= 1)
648 if (conflict_tos
[i
][j
] != 0)
652 for (prev
= vector
- 1; prev
>= 0; prev
--)
654 vector_number j
= order
[prev
];
658 /* Given how ORDER was computed, if the WIDTH or TALLY is
659 different, there cannot be a matching state. */
660 if (width
[j
] != w
|| tally
[j
] != t
)
663 for (k
= 0; match
&& k
< t
; k
++)
664 if (tos
[j
][k
] != tos
[i
][k
] || froms
[j
][k
] != froms
[i
][k
]
665 || (conflict_tos
[j
] != NULL
&& conflict_tos
[j
][k
] != 0))
677 pack_vector (vector_number vector
)
679 vector_number i
= order
[vector
];
683 base_number
*from
= froms
[i
];
684 base_number
*to
= tos
[i
];
685 unsigned int *conflict_to
= conflict_tos
[i
];
689 for (j
= lowzero
- from
[0]; ; j
++)
694 aver (j
< table_size
);
696 for (k
= 0; ok
&& k
< t
; k
++)
698 loc
= j
+ state_number_as_int (from
[k
]);
699 if (table_size
<= loc
)
706 for (k
= 0; ok
&& k
< vector
; k
++)
712 for (k
= 0; k
< t
; k
++)
716 if (nondeterministic_parser
&& conflict_to
!= NULL
)
717 conflict_table
[loc
] = conflict_to
[k
];
718 check
[loc
] = from
[k
];
721 while (table
[lowzero
] != 0)
727 aver (BASE_MINIMUM
<= j
&& j
<= BASE_MAXIMUM
);
734 /*-------------------------------------------------------------.
735 | Remap the negative infinite in TAB from NINF to the greatest |
736 | possible smallest value. Return it. |
738 | In most case this allows us to use shorts instead of ints in |
740 `-------------------------------------------------------------*/
743 table_ninf_remap (base_number tab
[], int size
, base_number ninf
)
748 for (i
= 0; i
< size
; i
++)
749 if (tab
[i
] < res
&& tab
[i
] != ninf
)
754 for (i
= 0; i
< size
; i
++)
766 base
= xnmalloc (nvectors
, sizeof *base
);
767 pos
= xnmalloc (nentries
, sizeof *pos
);
768 table
= xcalloc (table_size
, sizeof *table
);
769 conflict_table
= xcalloc (table_size
, sizeof *conflict_table
);
770 check
= xnmalloc (table_size
, sizeof *check
);
775 for (i
= 0; i
< nvectors
; i
++)
776 base
[i
] = BASE_MINIMUM
;
778 for (i
= 0; i
< table_size
; i
++)
781 for (i
= 0; i
< nentries
; i
++)
783 state_number s
= matching_state (i
);
787 /* A new set of state actions, or a nonterminal. */
788 place
= pack_vector (i
);
790 /* Action of I were already coded for S. */
794 base
[order
[i
]] = place
;
797 /* Use the greatest possible negative infinites. */
798 base_ninf
= table_ninf_remap (base
, nvectors
, BASE_MINIMUM
);
799 table_ninf
= table_ninf_remap (table
, high
+ 1, ACTION_NUMBER_MINIMUM
);
806 /*-----------------------------------------------------------------.
807 | Compute and output yydefact, yydefgoto, yypact, yypgoto, yytable |
809 `-----------------------------------------------------------------*/
812 tables_generate (void)
816 /* This is a poor way to make sure the sizes are properly
817 correlated. In particular the signedness is not taken into
818 account. But it's not useless. */
819 verify (sizeof nstates
<= sizeof nvectors
820 && sizeof nvars
<= sizeof nvectors
);
822 nvectors
= state_number_as_int (nstates
) + nvars
;
824 froms
= xcalloc (nvectors
, sizeof *froms
);
825 tos
= xcalloc (nvectors
, sizeof *tos
);
826 conflict_tos
= xcalloc (nvectors
, sizeof *conflict_tos
);
827 tally
= xcalloc (nvectors
, sizeof *tally
);
828 width
= xnmalloc (nvectors
, sizeof *width
);
837 order
= xcalloc (nvectors
, sizeof *order
);
845 for (i
= 0; i
< nvectors
; i
++)
849 free (conflict_tos
[i
]);
858 /*-------------------------.
859 | Free the parser tables. |
860 `-------------------------*/
866 free (conflict_table
);
867 free (conflict_list
);