1 /* Output the generated parsing program for Bison.
3 Copyright (C) 1984, 1986, 1989, 1992, 2000-2006, 2009-2012 Free
4 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/>. */
27 #include "conflicts.h"
32 #include "muscle-tab.h"
37 /* Several tables are indexed both by state and nonterminal numbers.
38 We call such an index a `vector'; i.e., a vector is either a state
39 or a nonterminal number.
41 Of course vector_number_t ought to be wide enough to contain
42 state_number and symbol_number. */
43 typedef int vector_number
;
45 #if 0 /* Not currently used. */
46 static inline vector_number
47 state_number_to_vector_number (state_number s
)
53 static inline vector_number
54 symbol_number_to_vector_number (symbol_number sym
)
56 return state_number_as_int (nstates
) + sym
- ntokens
;
62 /* FROMS and TOS are indexed by vector_number.
64 If VECTOR is a nonterminal, (FROMS[VECTOR], TOS[VECTOR]) form an
65 array of state numbers of the non defaulted GOTO on VECTOR.
67 If VECTOR is a state, TOS[VECTOR] is the array of actions to do on
68 the (array of) symbols FROMS[VECTOR].
70 In both cases, TALLY[VECTOR] is the size of the arrays
71 FROMS[VECTOR], TOS[VECTOR]; and WIDTH[VECTOR] =
72 (FROMS[VECTOR][SIZE] - FROMS[VECTOR][0] + 1) where SIZE =
75 FROMS therefore contains symbol_number and action_number,
76 TOS state_number and action_number,
78 WIDTH differences of FROMS.
80 Let base_number be the type of FROMS, TOS, and WIDTH. */
81 #define BASE_MAXIMUM INT_MAX
82 #define BASE_MINIMUM INT_MIN
84 static base_number
**froms
;
85 static base_number
**tos
;
86 static unsigned int **conflict_tos
;
88 static base_number
*width
;
91 /* For a given state, N = ACTROW[SYMBOL]:
93 If N = 0, stands for `run the default action'.
94 If N = MIN, stands for `raise a syntax error'.
95 If N > 0, stands for `shift SYMBOL and go to n'.
96 If N < 0, stands for `reduce -N'. */
97 typedef int action_number
;
98 #define ACTION_NUMBER_MINIMUM INT_MIN
100 static action_number
*actrow
;
102 /* FROMS and TOS are reordered to be compressed. ORDER[VECTOR] is the
103 new vector number of VECTOR. We skip `empty' vectors (i.e.,
104 TALLY[VECTOR] = 0), and call these `entries'. */
105 static vector_number
*order
;
108 base_number
*base
= NULL
;
109 /* A distinguished value of BASE, negative infinite. During the
110 computation equals to BASE_MINIMUM, later mapped to BASE_NINF to
111 keep parser tables small. */
112 base_number base_ninf
= 0;
113 static base_number
*pos
= NULL
;
115 static unsigned int *conflrow
;
116 unsigned int *conflict_table
;
117 unsigned int *conflict_list
;
118 int conflict_list_cnt
;
119 static int conflict_list_free
;
121 /* TABLE_SIZE is the allocated size of both TABLE and CHECK. We start
122 with more or less the original hard-coded value (which was
124 static int table_size
= 32768;
127 /* The value used in TABLE to denote explicit syntax errors
128 (%nonassoc), a negative infinite. First defaults to ACTION_NUMBER_MINIMUM,
129 but in order to keep small tables, renumbered as TABLE_ERROR, which
130 is the smallest (non error) value minus 1. */
131 base_number table_ninf
= 0;
135 state_number
*yydefgoto
;
136 rule_number
*yydefact
;
138 /*----------------------------------------------------------------.
139 | If TABLE (and CHECK) appear to be small to be addressed at |
140 | DESIRED, grow them. Note that TABLE[DESIRED] is to be used, so |
141 | the desired size is at least DESIRED + 1. |
142 `----------------------------------------------------------------*/
145 table_grow (int desired
)
147 int old_size
= table_size
;
149 while (table_size
<= desired
)
152 if (trace_flag
& trace_resource
)
153 fprintf (stderr
, "growing table and check from: %d to %d\n",
154 old_size
, table_size
);
156 table
= xnrealloc (table
, table_size
, sizeof *table
);
157 conflict_table
= xnrealloc (conflict_table
, table_size
,
158 sizeof *conflict_table
);
159 check
= xnrealloc (check
, table_size
, sizeof *check
);
161 for (/* Nothing. */; old_size
< table_size
; ++old_size
)
164 conflict_table
[old_size
] = 0;
165 check
[old_size
] = -1;
172 /*-------------------------------------------------------------------.
173 | For GLR parsers, for each conflicted token in S, as indicated |
174 | by non-zero entries in CONFLROW, create a list of possible |
175 | reductions that are alternatives to the shift or reduction |
176 | currently recorded for that token in S. Store the alternative |
177 | reductions followed by a 0 in CONFLICT_LIST, updating |
178 | CONFLICT_LIST_CNT, and storing an index to the start of the list |
179 | back into CONFLROW. |
180 `-------------------------------------------------------------------*/
183 conflict_row (state
*s
)
186 reductions
*reds
= s
->reductions
;
188 if (!nondeterministic_parser
)
191 for (j
= 0; j
< ntokens
; j
+= 1)
194 conflrow
[j
] = conflict_list_cnt
;
196 /* Find all reductions for token J, and record all that do not
198 for (i
= 0; i
< reds
->num
; i
+= 1)
199 if (bitset_test (reds
->lookahead_tokens
[i
], j
)
201 != rule_number_as_item_number (reds
->rules
[i
]->number
)))
203 aver (0 < conflict_list_free
);
204 conflict_list
[conflict_list_cnt
] = reds
->rules
[i
]->number
+ 1;
205 conflict_list_cnt
+= 1;
206 conflict_list_free
-= 1;
209 /* Leave a 0 at the end. */
210 aver (0 < conflict_list_free
);
211 conflict_list
[conflict_list_cnt
] = 0;
212 conflict_list_cnt
+= 1;
213 conflict_list_free
-= 1;
218 /*------------------------------------------------------------------.
219 | Decide what to do for each type of token if seen as the |
220 | lookahead in specified state. The value returned is used as the |
221 | default action (yydefact) for the state. In addition, ACTROW is |
222 | filled with what to do for each kind of token, index by symbol |
223 | number, with zero meaning do the default action. The value |
224 | ACTION_NUMBER_MINIMUM, a very negative number, means this |
225 | situation is an error. The parser recognizes this value |
228 | This is where conflicts are resolved. The loop over lookahead |
229 | rules considered lower-numbered rules last, and the last rule |
230 | considered that likes a token gets to handle it. |
232 | For GLR parsers, also sets CONFLROW[SYM] to an index into |
233 | CONFLICT_LIST iff there is an unresolved conflict (s/r or r/r) |
234 | with symbol SYM. The default reduction is not used for a symbol |
235 | that has any such conflicts. |
236 `------------------------------------------------------------------*/
239 action_row (state
*s
)
242 rule
*default_reduction
= NULL
;
243 reductions
*reds
= s
->reductions
;
244 transitions
*trans
= s
->transitions
;
245 errs
*errp
= s
->errs
;
246 /* Set to nonzero to inhibit having any default reduction. */
247 bool nodefault
= false;
248 bool conflicted
= false;
250 for (i
= 0; i
< ntokens
; i
++)
251 actrow
[i
] = conflrow
[i
] = 0;
253 if (reds
->lookahead_tokens
)
256 bitset_iterator biter
;
257 /* loop over all the rules available here which require
258 lookahead (in reverse order to give precedence to the first
260 for (i
= reds
->num
- 1; i
>= 0; --i
)
261 /* and find each token which the rule finds acceptable
263 BITSET_FOR_EACH (biter
, reds
->lookahead_tokens
[i
], j
, 0)
265 /* and record this rule as the rule to use if that
272 actrow
[j
] = rule_number_as_item_number (reds
->rules
[i
]->number
);
276 /* Now see which tokens are allowed for shifts in this state. For
277 them, record the shift as the thing to do. So shift is preferred
279 FOR_EACH_SHIFT (trans
, i
)
281 symbol_number sym
= TRANSITION_SYMBOL (trans
, i
);
282 state
*shift_state
= trans
->states
[i
];
284 if (actrow
[sym
] != 0)
289 actrow
[sym
] = state_number_as_int (shift_state
->number
);
291 /* Do not use any default reduction if there is a shift for
293 if (sym
== errtoken
->number
)
297 /* See which tokens are an explicit error in this state (due to
298 %nonassoc). For them, record ACTION_NUMBER_MINIMUM as the
300 for (i
= 0; i
< errp
->num
; i
++)
302 symbol
*sym
= errp
->symbols
[i
];
303 actrow
[sym
->number
] = ACTION_NUMBER_MINIMUM
;
306 /* Turn off default reductions where requested by the user. See
307 state_lookahead_tokens_count in lalr.c to understand when states are
308 labeled as consistent. */
310 char *default_reductions
=
311 muscle_percent_define_get ("lr.default-reductions");
312 if (STRNEQ (default_reductions
, "most") && !s
->consistent
)
314 free (default_reductions
);
317 /* Now find the most common reduction and make it the default action
320 if (reds
->num
>= 1 && !nodefault
)
323 default_reduction
= reds
->rules
[0];
327 for (i
= 0; i
< reds
->num
; i
++)
330 rule
*r
= reds
->rules
[i
];
333 for (j
= 0; j
< ntokens
; j
++)
334 if (actrow
[j
] == rule_number_as_item_number (r
->number
))
340 default_reduction
= r
;
344 /* GLR parsers need space for conflict lists, so we can't
345 default conflicted entries. For non-conflicted entries
346 or as long as we are not building a GLR parser,
347 actions that match the default are replaced with zero,
348 which means "use the default". */
353 for (j
= 0; j
< ntokens
; j
++)
355 == rule_number_as_item_number (default_reduction
->number
)
356 && ! (nondeterministic_parser
&& conflrow
[j
]))
362 /* If have no default reduction, the default is an error.
363 So replace any action which says "error" with "use default". */
365 if (!default_reduction
)
366 for (i
= 0; i
< ntokens
; i
++)
367 if (actrow
[i
] == ACTION_NUMBER_MINIMUM
)
373 return default_reduction
;
377 /*----------------------------------------.
378 | Set FROMS, TOS, TALLY and WIDTH for S. |
379 `----------------------------------------*/
382 save_row (state_number s
)
391 /* Number of non default actions in S. */
393 for (i
= 0; i
< ntokens
; i
++)
400 /* Allocate non defaulted actions. */
401 froms
[s
] = sp
= sp1
= xnmalloc (count
, sizeof *sp1
);
402 tos
[s
] = sp2
= xnmalloc (count
, sizeof *sp2
);
403 conflict_tos
[s
] = sp3
=
404 nondeterministic_parser
? xnmalloc (count
, sizeof *sp3
) : NULL
;
406 /* Store non defaulted actions. */
407 for (i
= 0; i
< ntokens
; i
++)
412 if (nondeterministic_parser
)
413 *sp3
++ = conflrow
[i
];
417 width
[s
] = sp1
[-1] - sp
[0] + 1;
421 /*------------------------------------------------------------------.
422 | Figure out the actions for the specified state, indexed by |
423 | lookahead token type. |
425 | The YYDEFACT table is output now. The detailed info is saved for |
426 | putting into YYTABLE later. |
427 `------------------------------------------------------------------*/
436 int nconflict
= nondeterministic_parser
? conflicts_total_count () : 0;
438 yydefact
= xnmalloc (nstates
, sizeof *yydefact
);
440 actrow
= xnmalloc (ntokens
, sizeof *actrow
);
441 conflrow
= xnmalloc (ntokens
, sizeof *conflrow
);
443 conflict_list
= xnmalloc (1 + 2 * nconflict
, sizeof *conflict_list
);
444 conflict_list_free
= 2 * nconflict
;
445 conflict_list_cnt
= 1;
447 /* Find the rules which are reduced. */
448 if (!nondeterministic_parser
)
449 for (r
= 0; r
< nrules
; ++r
)
450 rules
[r
].useful
= false;
452 for (i
= 0; i
< nstates
; ++i
)
454 rule
*default_reduction
= action_row (states
[i
]);
455 yydefact
[i
] = default_reduction
? default_reduction
->number
+ 1 : 0;
458 /* Now that the parser was computed, we can find which rules are
459 really reduced, and which are not because of SR or RR
461 if (!nondeterministic_parser
)
463 for (j
= 0; j
< ntokens
; ++j
)
464 if (actrow
[j
] < 0 && actrow
[j
] != ACTION_NUMBER_MINIMUM
)
465 rules
[item_number_as_rule_number (actrow
[j
])].useful
= true;
467 rules
[yydefact
[i
] - 1].useful
= true;
476 /*------------------------------------------------------------------.
477 | Compute FROMS[VECTOR], TOS[VECTOR], TALLY[VECTOR], WIDTH[VECTOR], |
478 | i.e., the information related to non defaulted GOTO on the nterm |
481 | DEFAULT_STATE is the principal destination on SYM, i.e., the |
482 | default GOTO destination on SYM. |
483 `------------------------------------------------------------------*/
486 save_column (symbol_number sym
, state_number default_state
)
493 vector_number symno
= symbol_number_to_vector_number (sym
);
495 goto_number begin
= goto_map
[sym
- ntokens
];
496 goto_number end
= goto_map
[sym
- ntokens
+ 1];
498 /* Number of non default GOTO. */
500 for (i
= begin
; i
< end
; i
++)
501 if (to_state
[i
] != default_state
)
507 /* Allocate room for non defaulted gotos. */
508 froms
[symno
] = sp
= sp1
= xnmalloc (count
, sizeof *sp1
);
509 tos
[symno
] = sp2
= xnmalloc (count
, sizeof *sp2
);
511 /* Store the state numbers of the non defaulted gotos. */
512 for (i
= begin
; i
< end
; i
++)
513 if (to_state
[i
] != default_state
)
515 *sp1
++ = from_state
[i
];
516 *sp2
++ = to_state
[i
];
519 tally
[symno
] = count
;
520 width
[symno
] = sp1
[-1] - sp
[0] + 1;
524 /*-------------------------------------------------------------.
525 | Return `the' most common destination GOTO on SYM (a nterm). |
526 `-------------------------------------------------------------*/
529 default_goto (symbol_number sym
, size_t state_count
[])
533 goto_number m
= goto_map
[sym
- ntokens
];
534 goto_number n
= goto_map
[sym
- ntokens
+ 1];
535 state_number default_state
= -1;
541 for (s
= 0; s
< nstates
; s
++)
544 for (i
= m
; i
< n
; i
++)
545 state_count
[to_state
[i
]]++;
547 for (s
= 0; s
< nstates
; s
++)
548 if (state_count
[s
] > max
)
550 max
= state_count
[s
];
554 return default_state
;
558 /*-------------------------------------------------------------------.
559 | Figure out what to do after reducing with each rule, depending on |
560 | the saved state from before the beginning of parsing the data that |
561 | matched this rule. |
563 | The YYDEFGOTO table is output now. The detailed info is saved for |
564 | putting into YYTABLE later. |
565 `-------------------------------------------------------------------*/
571 size_t *state_count
= xnmalloc (nstates
, sizeof *state_count
);
572 yydefgoto
= xnmalloc (nvars
, sizeof *yydefgoto
);
574 /* For a given nterm I, STATE_COUNT[S] is the number of times there
575 is a GOTO to S on I. */
576 for (i
= ntokens
; i
< nsyms
; ++i
)
578 state_number default_state
= default_goto (i
, state_count
);
579 save_column (i
, default_state
);
580 yydefgoto
[i
- ntokens
] = default_state
;
586 /*------------------------------------------------------------------.
587 | Compute ORDER, a reordering of vectors, in order to decide how to |
588 | pack the actions and gotos information into yytable. |
589 `------------------------------------------------------------------*/
598 for (i
= 0; i
< nvectors
; i
++)
604 int j
= nentries
- 1;
606 while (j
>= 0 && (width
[order
[j
]] < w
))
609 while (j
>= 0 && (width
[order
[j
]] == w
) && (tally
[order
[j
]] < t
))
612 for (k
= nentries
- 1; k
> j
; k
--)
613 order
[k
+ 1] = order
[k
];
621 /* If VECTOR is a state which actions (reflected by FROMS, TOS, TALLY
622 and WIDTH of VECTOR) are common to a previous state, return this
625 In any other case, return -1. */
628 matching_state (vector_number vector
)
630 vector_number i
= order
[vector
];
635 /* If VECTOR is a nterm, return -1. */
642 /* If VECTOR has GLR conflicts, return -1 */
643 if (conflict_tos
[i
] != NULL
)
646 for (j
= 0; j
< t
; j
+= 1)
647 if (conflict_tos
[i
][j
] != 0)
651 for (prev
= vector
- 1; prev
>= 0; prev
--)
653 vector_number j
= order
[prev
];
657 /* Given how ORDER was computed, if the WIDTH or TALLY is
658 different, there cannot be a matching state. */
659 if (width
[j
] != w
|| tally
[j
] != t
)
662 for (k
= 0; match
&& k
< t
; k
++)
663 if (tos
[j
][k
] != tos
[i
][k
] || froms
[j
][k
] != froms
[i
][k
]
664 || (conflict_tos
[j
] != NULL
&& conflict_tos
[j
][k
] != 0))
676 pack_vector (vector_number vector
)
678 vector_number i
= order
[vector
];
682 base_number
*from
= froms
[i
];
683 base_number
*to
= tos
[i
];
684 unsigned int *conflict_to
= conflict_tos
[i
];
688 for (j
= lowzero
- from
[0]; ; j
++)
693 aver (j
< table_size
);
695 for (k
= 0; ok
&& k
< t
; k
++)
697 loc
= j
+ state_number_as_int (from
[k
]);
698 if (table_size
<= loc
)
705 for (k
= 0; ok
&& k
< vector
; k
++)
711 for (k
= 0; k
< t
; k
++)
715 if (nondeterministic_parser
&& conflict_to
!= NULL
)
716 conflict_table
[loc
] = conflict_to
[k
];
717 check
[loc
] = from
[k
];
720 while (table
[lowzero
] != 0)
726 aver (BASE_MINIMUM
<= j
&& j
<= BASE_MAXIMUM
);
733 /*-------------------------------------------------------------.
734 | Remap the negative infinite in TAB from NINF to the greatest |
735 | possible smallest value. Return it. |
737 | In most case this allows us to use shorts instead of ints in |
739 `-------------------------------------------------------------*/
742 table_ninf_remap (base_number tab
[], int size
, base_number ninf
)
747 for (i
= 0; i
< size
; i
++)
748 if (tab
[i
] < res
&& tab
[i
] != ninf
)
753 for (i
= 0; i
< size
; i
++)
765 base
= xnmalloc (nvectors
, sizeof *base
);
766 pos
= xnmalloc (nentries
, sizeof *pos
);
767 table
= xcalloc (table_size
, sizeof *table
);
768 conflict_table
= xcalloc (table_size
, sizeof *conflict_table
);
769 check
= xnmalloc (table_size
, sizeof *check
);
774 for (i
= 0; i
< nvectors
; i
++)
775 base
[i
] = BASE_MINIMUM
;
777 for (i
= 0; i
< table_size
; i
++)
780 for (i
= 0; i
< nentries
; i
++)
782 state_number s
= matching_state (i
);
786 /* A new set of state actions, or a nonterminal. */
787 place
= pack_vector (i
);
789 /* Action of I were already coded for S. */
793 base
[order
[i
]] = place
;
796 /* Use the greatest possible negative infinites. */
797 base_ninf
= table_ninf_remap (base
, nvectors
, BASE_MINIMUM
);
798 table_ninf
= table_ninf_remap (table
, high
+ 1, ACTION_NUMBER_MINIMUM
);
805 /*-----------------------------------------------------------------.
806 | Compute and output yydefact, yydefgoto, yypact, yypgoto, yytable |
808 `-----------------------------------------------------------------*/
811 tables_generate (void)
815 /* This is a poor way to make sure the sizes are properly
816 correlated. In particular the signedness is not taken into
817 account. But it's not useless. */
818 verify (sizeof nstates
<= sizeof nvectors
819 && sizeof nvars
<= sizeof nvectors
);
821 nvectors
= state_number_as_int (nstates
) + nvars
;
823 froms
= xcalloc (nvectors
, sizeof *froms
);
824 tos
= xcalloc (nvectors
, sizeof *tos
);
825 conflict_tos
= xcalloc (nvectors
, sizeof *conflict_tos
);
826 tally
= xcalloc (nvectors
, sizeof *tally
);
827 width
= xnmalloc (nvectors
, sizeof *width
);
836 order
= xcalloc (nvectors
, sizeof *order
);
844 for (i
= 0; i
< nvectors
; i
++)
848 free (conflict_tos
[i
]);
857 /*-------------------------.
858 | Free the parser tables. |
859 `-------------------------*/
865 free (conflict_table
);
866 free (conflict_list
);