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
33 #include "conflicts.h"
36 /* Several tables will be indexed both by state and nonterminal
37 numbers. We call `vector' such a thing (= either a state or a
40 Of course vector_number_t ought to be wide enough to contain
41 state_number_t and symbol_number_t. */
42 typedef short vector_number_t
;
43 #define VECTOR_NUMBER_MAX ((vector_number_t) SHRT_MAX)
44 #define VECTOR_NUMBER_MIN ((vector_number_t) SHRT_MIN)
45 #define state_number_to_vector_number(State) \
46 ((vector_number_t) State)
47 #define symbol_number_to_vector_number(Symbol) \
48 ((vector_number_t) (state_number_as_int (nstates) + Symbol - ntokens))
53 /* FROMS and TOS are indexed by vector_number_t.
55 If VECTOR is a nonterminal, (FROMS[VECTOR], TOS[VECTOR]) form an
56 array of state numbers of the non defaulted GOTO on VECTOR.
58 If VECTOR is a state, TOS[VECTOR] is the array of actions to do on
59 the (array of) symbols FROMS[VECTOR].
61 In both cases, TALLY[VECTOR] is the size of the arrays
62 FROMS[VECTOR], TOS[VECTOR]; and WIDTH[VECTOR] =
63 (FROMS[VECTOR][SIZE] - FROMS[VECTOR][0] + 1) where SIZE =
66 FROMS therefore contains symbol_number_t and action_number_t,
67 TOS state_number_t and action_number_t,
69 WIDTH differences of FROMS.
71 Let base_t be the type of FROMS, TOS, and WIDTH. */
72 #define BASE_MAX ((base_t) INT_MAX)
73 #define BASE_MIN ((base_t) INT_MIN)
75 static base_t
**froms
= NULL
;
76 static base_t
**tos
= NULL
;
77 static unsigned int **conflict_tos
= NULL
;
78 static short *tally
= NULL
;
79 static base_t
*width
= NULL
;
82 /* For a given state, N = ACTROW[SYMBOL]:
84 If N = 0, stands for `run the default action'.
85 If N = MIN, stands for `raise a parse error'.
86 If N > 0, stands for `shift SYMBOL and go to n'.
87 If N < 0, stands for `reduce -N'. */
88 typedef short action_t
;
89 #define ACTION_MAX ((action_t) SHRT_MAX)
90 #define ACTION_MIN ((action_t) SHRT_MIN)
92 static action_t
*actrow
= NULL
;
94 /* FROMS and TOS are reordered to be compressed. ORDER[VECTOR] is the
95 new vector number of VECTOR. We skip `empty' vectors (i.e.,
96 TALLY[VECTOR] = 0), and call these `entries'. */
97 static vector_number_t
*order
= NULL
;
101 /* A distinguished value of BASE, negative infinite. During the
102 computation equals to BASE_MIN, later mapped to BASE_NINF to
103 keep parser tables small. */
104 base_t base_ninf
= 0;
105 static base_t
*pos
= NULL
;
107 static unsigned int *conflrow
= NULL
;
108 unsigned int *conflict_table
= NULL
;
109 unsigned int *conflict_list
= NULL
;
110 int conflict_list_cnt
;
111 static int conflict_list_free
;
113 /* TABLE_SIZE is the allocated size of both TABLE and CHECK. We start
114 with more or less the original hard-coded value (which was
116 static size_t table_size
= 32768;
117 base_t
*table
= NULL
;
118 base_t
*check
= NULL
;
119 /* The value used in TABLE to denote explicit parse errors
120 (%nonassoc), a negative infinite. First defaults to ACTION_MIN,
121 but in order to keep small tables, renumbered as TABLE_ERROR, which
122 is the smallest (non error) value minus 1. */
123 base_t table_ninf
= 0;
127 state_number_t
*yydefgoto
;
128 rule_number_t
*yydefact
;
130 /*----------------------------------------------------------------.
131 | If TABLE (and CHECK) appear to be small to be addressed at |
132 | DESIRED, grow them. Note that TABLE[DESIRED] is to be used, so |
133 | the desired size is at least DESIRED + 1. |
134 `----------------------------------------------------------------*/
137 table_grow (size_t desired
)
139 size_t old_size
= table_size
;
141 while (table_size
<= desired
)
144 if (trace_flag
& trace_resource
)
145 fprintf (stderr
, "growing table and check from: %d to %d\n",
146 old_size
, table_size
);
148 table
= XREALLOC (table
, base_t
, table_size
);
149 check
= XREALLOC (check
, base_t
, table_size
);
150 conflict_table
= XREALLOC (conflict_table
, unsigned int, table_size
);
152 for (/* Nothing. */; old_size
< table_size
; ++old_size
)
155 check
[old_size
] = -1;
162 /*-------------------------------------------------------------------.
163 | For GLR parsers, for each conflicted token in STATE, as indicated |
164 | by non-zero entries in CONFLROW, create a list of possible |
165 | reductions that are alternatives to the shift or reduction |
166 | currently recorded for that token in STATE. Store the alternative |
167 | reductions followed by a 0 in CONFLICT_LIST, updating |
168 | CONFLICT_LIST_CNT, and storing an index to the start of the list |
169 | back into CONFLROW. |
170 `-------------------------------------------------------------------*/
173 conflict_row (state_t
*state
)
176 reductions_t
*reds
= state
->reductions
;
181 for (j
= 0; j
< ntokens
; j
+= 1)
184 conflrow
[j
] = conflict_list_cnt
;
186 /* Find all reductions for token J, and record all that do not
188 for (i
= 0; i
< reds
->num
; i
+= 1)
189 if (bitset_test (reds
->lookaheads
[i
], j
)
191 != rule_number_as_item_number (reds
->rules
[i
]->number
)))
193 assert (conflict_list_free
> 0);
194 conflict_list
[conflict_list_cnt
] = reds
->rules
[i
]->number
+ 1;
195 conflict_list_cnt
+= 1;
196 conflict_list_free
-= 1;
199 /* Leave a 0 at the end. */
200 assert (conflict_list_free
> 0);
201 conflict_list_cnt
+= 1;
202 conflict_list_free
-= 1;
207 /*------------------------------------------------------------------.
208 | Decide what to do for each type of token if seen as the lookahead |
209 | token in specified state. The value returned is used as the |
210 | default action (yydefact) for the state. In addition, ACTROW is |
211 | filled with what to do for each kind of token, index by symbol |
212 | number, with zero meaning do the default action. The value |
213 | ACTION_MIN, a very negative number, means this situation is an |
214 | error. The parser recognizes this value specially. |
216 | This is where conflicts are resolved. The loop over lookahead |
217 | rules considered lower-numbered rules last, and the last rule |
218 | considered that likes a token gets to handle it. |
220 | For GLR parsers, also sets CONFLROW[SYM] to an index into |
221 | CONFLICT_LIST iff there is an unresolved conflict (s/r or r/r) |
222 | with symbol SYM. The default reduction is not used for a symbol |
223 | that has any such conflicts. |
224 `------------------------------------------------------------------*/
227 action_row (state_t
*state
)
230 rule_t
*default_rule
= NULL
;
231 reductions_t
*redp
= state
->reductions
;
232 transitions_t
*transitions
= state
->transitions
;
233 errs_t
*errp
= state
->errs
;
234 /* Set to nonzero to inhibit having any default reduction. */
238 for (i
= 0; i
< ntokens
; i
++)
239 actrow
[i
] = conflrow
[i
] = 0;
241 if (redp
->lookaheads
)
244 bitset_iterator biter
;
245 /* loop over all the rules available here which require
246 lookahead (in reverse order to give precedence to the first
248 for (i
= redp
->num
- 1; i
>= 0; --i
)
249 /* and find each token which the rule finds acceptable
251 BITSET_FOR_EACH (biter
, redp
->lookaheads
[i
], j
, 0)
253 /* and record this rule as the rule to use if that
256 conflicted
= conflrow
[j
] = 1;
257 actrow
[j
] = rule_number_as_item_number (redp
->rules
[i
]->number
);
261 /* Now see which tokens are allowed for shifts in this state. For
262 them, record the shift as the thing to do. So shift is preferred
264 FOR_EACH_SHIFT (transitions
, i
)
266 symbol_number_t symbol
= TRANSITION_SYMBOL (transitions
, i
);
267 state_t
*shift_state
= transitions
->states
[i
];
269 if (actrow
[symbol
] != 0)
270 conflicted
= conflrow
[symbol
] = 1;
271 actrow
[symbol
] = state_number_as_int (shift_state
->number
);
273 /* Do not use any default reduction if there is a shift for
275 if (symbol
== errtoken
->number
)
279 /* See which tokens are an explicit error in this state (due to
280 %nonassoc). For them, record ACTION_MIN as the action. */
281 for (i
= 0; i
< errp
->num
; i
++)
283 symbol_t
*symbol
= errp
->symbols
[i
];
284 actrow
[symbol
->number
] = ACTION_MIN
;
287 /* Now find the most common reduction and make it the default action
290 if (redp
->num
>= 1 && !nodefault
)
292 if (state
->consistent
)
293 default_rule
= redp
->rules
[0];
297 for (i
= 0; i
< redp
->num
; i
++)
300 rule_t
*rule
= redp
->rules
[i
];
303 for (j
= 0; j
< ntokens
; j
++)
304 if (actrow
[j
] == rule_number_as_item_number (rule
->number
))
314 /* GLR parsers need space for conflict lists, so we can't
315 default conflicted entries. For non-conflicted entries
316 or as long as we are not building a GLR parser,
317 actions that match the default are replaced with zero,
318 which means "use the default". */
323 for (j
= 0; j
< ntokens
; j
++)
324 if (actrow
[j
] == rule_number_as_item_number (default_rule
->number
)
325 && ! (glr_parser
&& conflrow
[j
]))
331 /* If have no default rule, the default is an error.
332 So replace any action which says "error" with "use default". */
335 for (i
= 0; i
< ntokens
; i
++)
336 if (actrow
[i
] == ACTION_MIN
)
340 conflict_row (state
);
346 /*--------------------------------------------.
347 | Set FROMS, TOS, TALLY and WIDTH for STATE. |
348 `--------------------------------------------*/
351 save_row (state_number_t state
)
358 unsigned int *sp3
= NULL
;
360 /* Number of non default actions in STATE. */
362 for (i
= 0; i
< ntokens
; i
++)
369 /* Allocate non defaulted actions. */
370 froms
[state
] = sp1
= sp
= XCALLOC (base_t
, count
);
371 tos
[state
] = sp2
= XCALLOC (base_t
, count
);
373 conflict_tos
[state
] = sp3
= XCALLOC (unsigned int, count
);
375 conflict_tos
[state
] = NULL
;
377 /* Store non defaulted actions. */
378 for (i
= 0; i
< ntokens
; i
++)
384 *sp3
++ = conflrow
[i
];
387 tally
[state
] = count
;
388 width
[state
] = sp1
[-1] - sp
[0] + 1;
392 /*------------------------------------------------------------------.
393 | Figure out the actions for the specified state, indexed by |
394 | lookahead token type. |
396 | The YYDEFACT table is output now. The detailed info is saved for |
397 | putting into YYTABLE later. |
398 `------------------------------------------------------------------*/
407 int nconflict
= glr_parser
? conflicts_total_count () : 0;
409 yydefact
= XCALLOC (rule_number_t
, nstates
);
411 actrow
= XCALLOC (action_t
, ntokens
);
412 conflrow
= XCALLOC (unsigned int, ntokens
);
414 conflict_list
= XCALLOC (unsigned int, 1 + 2 * nconflict
);
415 conflict_list_free
= 2 * nconflict
;
416 conflict_list_cnt
= 1;
418 /* Find the rules which are reduced. */
420 for (r
= 0; r
< nrules
; ++r
)
421 rules
[r
].useful
= false;
423 for (i
= 0; i
< nstates
; ++i
)
425 rule_t
*default_rule
= action_row (states
[i
]);
426 yydefact
[i
] = default_rule
? default_rule
->number
+ 1 : 0;
429 /* Now that the parser was computed, we can find which rules are
430 really reduced, and which are not because of SR or RR
434 for (j
= 0; j
< ntokens
; ++j
)
435 if (actrow
[j
] < 0 && actrow
[j
] != ACTION_MIN
)
436 rules
[item_number_as_rule_number (actrow
[j
])].useful
= true;
438 rules
[yydefact
[i
] - 1].useful
= true;
447 /*------------------------------------------------------------------.
448 | Compute FROMS[VECTOR], TOS[VECTOR], TALLY[VECTOR], WIDTH[VECTOR], |
449 | i.e., the information related to non defaulted GOTO on the nterm |
452 | DEFAULT_STATE is the principal destination on SYMBOL, i.e., the |
453 | default GOTO destination on SYMBOL. |
454 `------------------------------------------------------------------*/
457 save_column (symbol_number_t symbol
, state_number_t default_state
)
464 vector_number_t symno
= symbol_number_to_vector_number (symbol
);
466 goto_number_t begin
= goto_map
[symbol
];
467 goto_number_t end
= goto_map
[symbol
+ 1];
469 /* Number of non default GOTO. */
471 for (i
= begin
; i
< end
; i
++)
472 if (to_state
[i
] != default_state
)
478 /* Allocate room for non defaulted gotos. */
479 froms
[symno
] = sp1
= sp
= XCALLOC (base_t
, count
);
480 tos
[symno
] = sp2
= XCALLOC (base_t
, count
);
482 /* Store the state numbers of the non defaulted gotos. */
483 for (i
= begin
; i
< end
; i
++)
484 if (to_state
[i
] != default_state
)
486 *sp1
++ = from_state
[i
];
487 *sp2
++ = to_state
[i
];
490 tally
[symno
] = count
;
491 width
[symno
] = sp1
[-1] - sp
[0] + 1;
495 /*----------------------------------------------------------------.
496 | Return `the' most common destination GOTO on SYMBOL (a nterm). |
497 `----------------------------------------------------------------*/
499 static state_number_t
500 default_goto (symbol_number_t symbol
, short state_count
[])
504 goto_number_t m
= goto_map
[symbol
];
505 goto_number_t n
= goto_map
[symbol
+ 1];
506 state_number_t default_state
= (state_number_t
) -1;
510 return (state_number_t
) -1;
512 for (s
= 0; s
< nstates
; s
++)
515 for (i
= m
; i
< n
; i
++)
516 state_count
[to_state
[i
]]++;
518 for (s
= 0; s
< nstates
; s
++)
519 if (state_count
[s
] > max
)
521 max
= state_count
[s
];
525 return default_state
;
529 /*-------------------------------------------------------------------.
530 | Figure out what to do after reducing with each rule, depending on |
531 | the saved state from before the beginning of parsing the data that |
532 | matched this rule. |
534 | The YYDEFGOTO table is output now. The detailed info is saved for |
535 | putting into YYTABLE later. |
536 `-------------------------------------------------------------------*/
542 short *state_count
= XCALLOC (short, nstates
);
543 yydefgoto
= XMALLOC (state_number_t
, nvars
);
545 /* For a given nterm I, STATE_COUNT[S] is the number of times there
546 is a GOTO to S on I. */
547 for (i
= ntokens
; i
< nsyms
; ++i
)
549 state_number_t default_state
= default_goto (i
, state_count
);
550 save_column (i
, default_state
);
551 yydefgoto
[i
- ntokens
] = default_state
;
557 /*------------------------------------------------------------------.
558 | Compute ORDER, a reordering of vectors, in order to decide how to |
559 | pack the actions and gotos information into yytable. |
560 `------------------------------------------------------------------*/
569 for (i
= 0; i
< nvectors
; i
++)
575 int j
= nentries
- 1;
577 while (j
>= 0 && (width
[order
[j
]] < w
))
580 while (j
>= 0 && (width
[order
[j
]] == w
) && (tally
[order
[j
]] < t
))
583 for (k
= nentries
- 1; k
> j
; k
--)
584 order
[k
+ 1] = order
[k
];
592 /* If VECTOR is a state which actions (reflected by FROMS, TOS, TALLY
593 and WIDTH of VECTOR) are common to a previous state, return this
596 In any other case, return -1. */
598 static state_number_t
599 matching_state (vector_number_t vector
)
601 vector_number_t i
= order
[vector
];
606 /* If VECTOR is a nterm, return -1. */
607 if (i
>= (int) nstates
)
613 for (prev
= vector
- 1; prev
>= 0; prev
--)
615 vector_number_t j
= order
[prev
];
619 /* Given how ORDER was computed, if the WIDTH or TALLY is
620 different, there cannot be a matching state. */
621 if (width
[j
] != w
|| tally
[j
] != t
)
624 for (k
= 0; match
&& k
< t
; k
++)
625 if (tos
[j
][k
] != tos
[i
][k
] || froms
[j
][k
] != froms
[i
][k
])
637 pack_vector (vector_number_t vector
)
639 vector_number_t i
= order
[vector
];
643 base_t
*from
= froms
[i
];
645 unsigned int *conflict_to
= conflict_tos
[i
];
649 for (j
= lowzero
- from
[0]; j
< (int) table_size
; j
++)
654 for (k
= 0; ok
&& k
< t
; k
++)
656 loc
= j
+ state_number_as_int (from
[k
]);
657 if (loc
>= (int) table_size
)
664 for (k
= 0; ok
&& k
< vector
; k
++)
670 for (k
= 0; k
< t
; k
++)
674 if (glr_parser
&& conflict_to
!= NULL
)
675 conflict_table
[loc
] = conflict_to
[k
];
676 check
[loc
] = from
[k
];
679 while (table
[lowzero
] != 0)
685 if (j
< BASE_MIN
|| BASE_MAX
< j
)
686 fatal ("base_t too small to hold %d\n", j
);
690 #define pack_vector_succeeded 0
691 assert (pack_vector_succeeded
);
696 /*-------------------------------------------------------------.
697 | Remap the negative infinite in TAB from NINF to the greatest |
698 | possible smallest value. Return it. |
700 | In most case this allows us to use shorts instead of ints in |
702 `-------------------------------------------------------------*/
705 table_ninf_remap (base_t tab
[], size_t size
, base_t ninf
)
710 for (i
= 0; i
< size
; i
++)
711 if (tab
[i
] < res
&& tab
[i
] != ninf
)
716 for (i
= 0; i
< size
; i
++)
728 base
= XCALLOC (base_t
, nvectors
);
729 pos
= XCALLOC (base_t
, nentries
);
730 table
= XCALLOC (base_t
, table_size
);
731 conflict_table
= XCALLOC (unsigned int, table_size
);
732 check
= XCALLOC (base_t
, table_size
);
737 for (i
= 0; i
< nvectors
; i
++)
740 for (i
= 0; i
< (int) table_size
; i
++)
743 for (i
= 0; i
< nentries
; i
++)
745 state_number_t state
= matching_state (i
);
749 /* A new set of state actions, or a nonterminal. */
750 place
= pack_vector (i
);
752 /* Action of I were already coded for STATE. */
756 base
[order
[i
]] = place
;
759 /* Use the greatest possible negative infinites. */
760 base_ninf
= table_ninf_remap (base
, nvectors
, BASE_MIN
);
761 table_ninf
= table_ninf_remap (table
, high
+ 1, ACTION_MIN
);
768 /*-----------------------------------------------------------------.
769 | Compute and output yydefact, yydefgoto, yypact, yypgoto, yytable |
771 `-----------------------------------------------------------------*/
774 tables_generate (void)
778 /* That's a poor way to make sure the sizes are properly corelated,
779 in particular the signedness is not taking into account, but it's
781 assert (sizeof (nvectors
) >= sizeof (nstates
));
782 assert (sizeof (nvectors
) >= sizeof (nvars
));
784 nvectors
= state_number_as_int (nstates
) + nvars
;
786 froms
= XCALLOC (base_t
*, nvectors
);
787 tos
= XCALLOC (base_t
*, nvectors
);
788 conflict_tos
= XCALLOC (unsigned int *, nvectors
);
789 tally
= XCALLOC (short, nvectors
);
790 width
= XCALLOC (base_t
, nvectors
);
795 XFREE (goto_map
+ ntokens
);
799 order
= XCALLOC (vector_number_t
, nvectors
);
807 for (i
= 0; i
< nvectors
; i
++)
811 XFREE (conflict_tos
[i
]);
820 /*-------------------------.
821 | Free the parser tables. |
822 `-------------------------*/
828 free (conflict_table
);
829 free (conflict_list
);