1 /* Output the generated parsing program for Bison.
3 Copyright (C) 1984, 1986, 1989, 1992, 2000, 2001, 2002, 2003, 2004
4 Free Software Foundation, Inc.
6 This file is part of Bison, the GNU Compiler Compiler.
8 Bison is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
13 Bison is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with Bison; see the file COPYING. If not, write to the Free
20 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
30 #include "conflicts.h"
39 /* Several tables are indexed both by state and nonterminal numbers.
40 We call such an index a `vector'; i.e., a vector is either a state
41 or a nonterminal number.
43 Of course vector_number_t ought to be wide enough to contain
44 state_number and symbol_number. */
45 typedef short int vector_number
;
47 static inline vector_number
48 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
= NULL
;
85 static base_number
**tos
= NULL
;
86 static unsigned int **conflict_tos
= NULL
;
87 static short int *tally
= NULL
;
88 static base_number
*width
= NULL
;
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 short int action_number
;
98 #define ACTION_NUMBER_MINIMUM SHRT_MIN
100 static action_number
*actrow
= NULL
;
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
= NULL
;
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
= NULL
;
116 unsigned int *conflict_table
= NULL
;
117 unsigned int *conflict_list
= NULL
;
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;
125 base_number
*table
= NULL
;
126 base_number
*check
= NULL
;
127 /* The value used in TABLE to denote explicit syntax errors
128 (%nonassoc), a negative infinite. First defaults to ACTION_NUMBER_MININUM,
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 REALLOC (table
, table_size
);
157 REALLOC (check
, table_size
);
158 REALLOC (conflict_table
, table_size
);
160 for (/* Nothing. */; old_size
< table_size
; ++old_size
)
163 check
[old_size
] = -1;
170 /*-------------------------------------------------------------------.
171 | For GLR parsers, for each conflicted token in S, as indicated |
172 | by non-zero entries in CONFLROW, create a list of possible |
173 | reductions that are alternatives to the shift or reduction |
174 | currently recorded for that token in S. Store the alternative |
175 | reductions followed by a 0 in CONFLICT_LIST, updating |
176 | CONFLICT_LIST_CNT, and storing an index to the start of the list |
177 | back into CONFLROW. |
178 `-------------------------------------------------------------------*/
181 conflict_row (state
*s
)
184 reductions
*reds
= s
->reductions
;
186 if (!nondeterministic_parser
)
189 for (j
= 0; j
< ntokens
; j
+= 1)
192 conflrow
[j
] = conflict_list_cnt
;
194 /* Find all reductions for token J, and record all that do not
196 for (i
= 0; i
< reds
->num
; i
+= 1)
197 if (bitset_test (reds
->look_ahead_tokens
[i
], j
)
199 != rule_number_as_item_number (reds
->rules
[i
]->number
)))
201 if (conflict_list_free
<= 0)
203 conflict_list
[conflict_list_cnt
] = reds
->rules
[i
]->number
+ 1;
204 conflict_list_cnt
+= 1;
205 conflict_list_free
-= 1;
208 /* Leave a 0 at the end. */
209 if (conflict_list_free
<= 0)
211 conflict_list_cnt
+= 1;
212 conflict_list_free
-= 1;
217 /*------------------------------------------------------------------.
218 | Decide what to do for each type of token if seen as the |
219 | look-ahead in specified state. The value returned is used as the |
220 | default action (yydefact) for the state. In addition, ACTROW is |
221 | filled with what to do for each kind of token, index by symbol |
222 | number, with zero meaning do the default action. The value |
223 | ACTION_NUMBER_MINIMUM, a very negative number, means this |
224 | situation is an error. The parser recognizes this value |
227 | This is where conflicts are resolved. The loop over look-ahead |
228 | rules considered lower-numbered rules last, and the last rule |
229 | considered that likes a token gets to handle it. |
231 | For GLR parsers, also sets CONFLROW[SYM] to an index into |
232 | CONFLICT_LIST iff there is an unresolved conflict (s/r or r/r) |
233 | with symbol SYM. The default reduction is not used for a symbol |
234 | that has any such conflicts. |
235 `------------------------------------------------------------------*/
238 action_row (state
*s
)
241 rule
*default_rule
= NULL
;
242 reductions
*reds
= s
->reductions
;
243 transitions
*trans
= s
->transitions
;
244 errs
*errp
= s
->errs
;
245 /* Set to nonzero to inhibit having any default reduction. */
246 bool nodefault
= false;
247 bool conflicted
= false;
249 for (i
= 0; i
< ntokens
; i
++)
250 actrow
[i
] = conflrow
[i
] = 0;
252 if (reds
->look_ahead_tokens
)
255 bitset_iterator biter
;
256 /* loop over all the rules available here which require
257 look-ahead (in reverse order to give precedence to the first
259 for (i
= reds
->num
- 1; i
>= 0; --i
)
260 /* and find each token which the rule finds acceptable
262 BITSET_FOR_EACH (biter
, reds
->look_ahead_tokens
[i
], j
, 0)
264 /* and record this rule as the rule to use if that
271 actrow
[j
] = rule_number_as_item_number (reds
->rules
[i
]->number
);
275 /* Now see which tokens are allowed for shifts in this state. For
276 them, record the shift as the thing to do. So shift is preferred
278 FOR_EACH_SHIFT (trans
, i
)
280 symbol_number sym
= TRANSITION_SYMBOL (trans
, i
);
281 state
*shift_state
= trans
->states
[i
];
283 if (actrow
[sym
] != 0)
288 actrow
[sym
] = state_number_as_int (shift_state
->number
);
290 /* Do not use any default reduction if there is a shift for
292 if (sym
== errtoken
->number
)
296 /* See which tokens are an explicit error in this state (due to
297 %nonassoc). For them, record ACTION_NUMBER_MINIMUM as the
299 for (i
= 0; i
< errp
->num
; i
++)
301 symbol
*sym
= errp
->symbols
[i
];
302 actrow
[sym
->number
] = ACTION_NUMBER_MINIMUM
;
305 /* Now find the most common reduction and make it the default action
308 if (reds
->num
>= 1 && !nodefault
)
311 default_rule
= reds
->rules
[0];
315 for (i
= 0; i
< reds
->num
; i
++)
318 rule
*r
= reds
->rules
[i
];
321 for (j
= 0; j
< ntokens
; j
++)
322 if (actrow
[j
] == rule_number_as_item_number (r
->number
))
332 /* GLR parsers need space for conflict lists, so we can't
333 default conflicted entries. For non-conflicted entries
334 or as long as we are not building a GLR parser,
335 actions that match the default are replaced with zero,
336 which means "use the default". */
341 for (j
= 0; j
< ntokens
; j
++)
342 if (actrow
[j
] == rule_number_as_item_number (default_rule
->number
)
343 && ! (nondeterministic_parser
&& conflrow
[j
]))
349 /* If have no default rule, the default is an error.
350 So replace any action which says "error" with "use default". */
353 for (i
= 0; i
< ntokens
; i
++)
354 if (actrow
[i
] == ACTION_NUMBER_MINIMUM
)
364 /*----------------------------------------.
365 | Set FROMS, TOS, TALLY and WIDTH for S. |
366 `----------------------------------------*/
369 save_row (state_number s
)
376 unsigned int *sp3
IF_LINT (= NULL
);
378 /* Number of non default actions in S. */
380 for (i
= 0; i
< ntokens
; i
++)
387 /* Allocate non defaulted actions. */
388 froms
[s
] = sp
= CALLOC (sp1
, count
);
389 tos
[s
] = CALLOC (sp2
, count
);
390 conflict_tos
[s
] = nondeterministic_parser
? CALLOC (sp3
, count
) : NULL
;
392 /* Store non defaulted actions. */
393 for (i
= 0; i
< ntokens
; i
++)
398 if (nondeterministic_parser
)
399 *sp3
++ = conflrow
[i
];
403 width
[s
] = sp1
[-1] - sp
[0] + 1;
407 /*------------------------------------------------------------------.
408 | Figure out the actions for the specified state, indexed by |
409 | look-ahead token type. |
411 | The YYDEFACT table is output now. The detailed info is saved for |
412 | putting into YYTABLE later. |
413 `------------------------------------------------------------------*/
422 int nconflict
= nondeterministic_parser
? conflicts_total_count () : 0;
424 CALLOC (yydefact
, nstates
);
426 CALLOC (actrow
, ntokens
);
427 CALLOC (conflrow
, ntokens
);
429 CALLOC (conflict_list
, 1 + 2 * nconflict
);
430 conflict_list_free
= 2 * nconflict
;
431 conflict_list_cnt
= 1;
433 /* Find the rules which are reduced. */
434 if (!nondeterministic_parser
)
435 for (r
= 0; r
< nrules
; ++r
)
436 rules
[r
].useful
= false;
438 for (i
= 0; i
< nstates
; ++i
)
440 rule
*default_rule
= action_row (states
[i
]);
441 yydefact
[i
] = default_rule
? default_rule
->number
+ 1 : 0;
444 /* Now that the parser was computed, we can find which rules are
445 really reduced, and which are not because of SR or RR
447 if (!nondeterministic_parser
)
449 for (j
= 0; j
< ntokens
; ++j
)
450 if (actrow
[j
] < 0 && actrow
[j
] != ACTION_NUMBER_MINIMUM
)
451 rules
[item_number_as_rule_number (actrow
[j
])].useful
= true;
453 rules
[yydefact
[i
] - 1].useful
= true;
462 /*------------------------------------------------------------------.
463 | Compute FROMS[VECTOR], TOS[VECTOR], TALLY[VECTOR], WIDTH[VECTOR], |
464 | i.e., the information related to non defaulted GOTO on the nterm |
467 | DEFAULT_STATE is the principal destination on SYM, i.e., the |
468 | default GOTO destination on SYM. |
469 `------------------------------------------------------------------*/
472 save_column (symbol_number sym
, state_number default_state
)
479 vector_number symno
= symbol_number_to_vector_number (sym
);
481 goto_number begin
= goto_map
[sym
- ntokens
];
482 goto_number end
= goto_map
[sym
- ntokens
+ 1];
484 /* Number of non default GOTO. */
486 for (i
= begin
; i
< end
; i
++)
487 if (to_state
[i
] != default_state
)
493 /* Allocate room for non defaulted gotos. */
494 froms
[symno
] = sp
= CALLOC (sp1
, count
);
495 tos
[symno
] = CALLOC (sp2
, count
);
497 /* Store the state numbers of the non defaulted gotos. */
498 for (i
= begin
; i
< end
; i
++)
499 if (to_state
[i
] != default_state
)
501 *sp1
++ = from_state
[i
];
502 *sp2
++ = to_state
[i
];
505 tally
[symno
] = count
;
506 width
[symno
] = sp1
[-1] - sp
[0] + 1;
510 /*-------------------------------------------------------------.
511 | Return `the' most common destination GOTO on SYM (a nterm). |
512 `-------------------------------------------------------------*/
515 default_goto (symbol_number sym
, short int state_count
[])
519 goto_number m
= goto_map
[sym
- ntokens
];
520 goto_number n
= goto_map
[sym
- ntokens
+ 1];
521 state_number default_state
= -1;
527 for (s
= 0; s
< nstates
; s
++)
530 for (i
= m
; i
< n
; i
++)
531 state_count
[to_state
[i
]]++;
533 for (s
= 0; s
< nstates
; s
++)
534 if (state_count
[s
] > max
)
536 max
= state_count
[s
];
540 return default_state
;
544 /*-------------------------------------------------------------------.
545 | Figure out what to do after reducing with each rule, depending on |
546 | the saved state from before the beginning of parsing the data that |
547 | matched this rule. |
549 | The YYDEFGOTO table is output now. The detailed info is saved for |
550 | putting into YYTABLE later. |
551 `-------------------------------------------------------------------*/
557 short int *state_count
= CALLOC (state_count
, nstates
);
558 MALLOC (yydefgoto
, nvars
);
560 /* For a given nterm I, STATE_COUNT[S] is the number of times there
561 is a GOTO to S on I. */
562 for (i
= ntokens
; i
< nsyms
; ++i
)
564 state_number default_state
= default_goto (i
, state_count
);
565 save_column (i
, default_state
);
566 yydefgoto
[i
- ntokens
] = default_state
;
572 /*------------------------------------------------------------------.
573 | Compute ORDER, a reordering of vectors, in order to decide how to |
574 | pack the actions and gotos information into yytable. |
575 `------------------------------------------------------------------*/
584 for (i
= 0; i
< nvectors
; i
++)
590 int j
= nentries
- 1;
592 while (j
>= 0 && (width
[order
[j
]] < w
))
595 while (j
>= 0 && (width
[order
[j
]] == w
) && (tally
[order
[j
]] < t
))
598 for (k
= nentries
- 1; k
> j
; k
--)
599 order
[k
+ 1] = order
[k
];
607 /* If VECTOR is a state which actions (reflected by FROMS, TOS, TALLY
608 and WIDTH of VECTOR) are common to a previous state, return this
611 In any other case, return -1. */
614 matching_state (vector_number vector
)
616 vector_number i
= order
[vector
];
621 /* If VECTOR is a nterm, return -1. */
628 /* If VECTOR has GLR conflicts, return -1 */
629 if (conflict_tos
[i
] != NULL
)
632 for (j
= 0; j
< t
; j
+= 1)
633 if (conflict_tos
[i
][j
] != 0)
637 for (prev
= vector
- 1; prev
>= 0; prev
--)
639 vector_number j
= order
[prev
];
643 /* Given how ORDER was computed, if the WIDTH or TALLY is
644 different, there cannot be a matching state. */
645 if (width
[j
] != w
|| tally
[j
] != t
)
648 for (k
= 0; match
&& k
< t
; k
++)
649 if (tos
[j
][k
] != tos
[i
][k
] || froms
[j
][k
] != froms
[i
][k
]
650 || (conflict_tos
[j
] != NULL
&& conflict_tos
[j
][k
] != 0))
662 pack_vector (vector_number vector
)
664 vector_number i
= order
[vector
];
668 base_number
*from
= froms
[i
];
669 base_number
*to
= tos
[i
];
670 unsigned int *conflict_to
= conflict_tos
[i
];
675 for (j
= lowzero
- from
[0]; ; j
++)
683 for (k
= 0; ok
&& k
< t
; k
++)
685 loc
= j
+ state_number_as_int (from
[k
]);
686 if (table_size
<= loc
)
693 for (k
= 0; ok
&& k
< vector
; k
++)
699 for (k
= 0; k
< t
; k
++)
703 if (nondeterministic_parser
&& conflict_to
!= NULL
)
704 conflict_table
[loc
] = conflict_to
[k
];
705 check
[loc
] = from
[k
];
708 while (table
[lowzero
] != 0)
714 if (! (BASE_MINIMUM
<= j
&& j
<= BASE_MAXIMUM
))
722 /*-------------------------------------------------------------.
723 | Remap the negative infinite in TAB from NINF to the greatest |
724 | possible smallest value. Return it. |
726 | In most case this allows us to use shorts instead of ints in |
728 `-------------------------------------------------------------*/
731 table_ninf_remap (base_number tab
[], int size
, base_number ninf
)
736 for (i
= 0; i
< size
; i
++)
737 if (tab
[i
] < res
&& tab
[i
] != ninf
)
742 for (i
= 0; i
< size
; i
++)
754 CALLOC (base
, nvectors
);
755 CALLOC (pos
, nentries
);
756 CALLOC (table
, table_size
);
757 CALLOC (conflict_table
, table_size
);
758 CALLOC (check
, table_size
);
763 for (i
= 0; i
< nvectors
; i
++)
764 base
[i
] = BASE_MINIMUM
;
766 for (i
= 0; i
< table_size
; i
++)
769 for (i
= 0; i
< nentries
; i
++)
771 state_number s
= matching_state (i
);
775 /* A new set of state actions, or a nonterminal. */
776 place
= pack_vector (i
);
778 /* Action of I were already coded for S. */
782 base
[order
[i
]] = place
;
785 /* Use the greatest possible negative infinites. */
786 base_ninf
= table_ninf_remap (base
, nvectors
, BASE_MINIMUM
);
787 table_ninf
= table_ninf_remap (table
, high
+ 1, ACTION_NUMBER_MINIMUM
);
794 /*-----------------------------------------------------------------.
795 | Compute and output yydefact, yydefgoto, yypact, yypgoto, yytable |
797 `-----------------------------------------------------------------*/
800 tables_generate (void)
804 /* This is a poor way to make sure the sizes are properly
805 correlated. In particular the signedness is not taken into
806 account. But it's not useless. */
807 verify (sizes_are_properly_correlated
,
808 (sizeof nstates
<= sizeof nvectors
809 && sizeof nvars
<= sizeof nvectors
));
811 nvectors
= state_number_as_int (nstates
) + nvars
;
813 CALLOC (froms
, nvectors
);
814 CALLOC (tos
, nvectors
);
815 CALLOC (conflict_tos
, nvectors
);
816 CALLOC (tally
, nvectors
);
817 CALLOC (width
, nvectors
);
826 CALLOC (order
, nvectors
);
834 for (i
= 0; i
< nvectors
; i
++)
838 XFREE (conflict_tos
[i
]);
847 /*-------------------------.
848 | Free the parser tables. |
849 `-------------------------*/
855 free (conflict_table
);
856 free (conflict_list
);