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
29 #include "conflicts.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 short vector_number
;
45 #define state_number_to_vector_number(State) \
46 ((vector_number) State)
47 #define symbol_number_to_vector_number(Symbol) \
48 ((vector_number) (state_number_as_int (nstates) + Symbol - ntokens))
53 /* FROMS and TOS are indexed by vector_number.
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 and action_number,
67 TOS state_number and action_number,
69 WIDTH differences of FROMS.
71 Let base_number be the type of FROMS, TOS, and WIDTH. */
72 #define BASE_MAXIMUM INT_MAX
73 #define BASE_MINIMUM INT_MIN
75 static base_number
**froms
= NULL
;
76 static base_number
**tos
= NULL
;
77 static unsigned int **conflict_tos
= NULL
;
78 static short *tally
= NULL
;
79 static base_number
*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 syntax 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_number
;
89 #define ACTION_NUMBER_MINIMUM SHRT_MIN
91 static action_number
*actrow
= NULL
;
93 /* FROMS and TOS are reordered to be compressed. ORDER[VECTOR] is the
94 new vector number of VECTOR. We skip `empty' vectors (i.e.,
95 TALLY[VECTOR] = 0), and call these `entries'. */
96 static vector_number
*order
= NULL
;
99 base_number
*base
= NULL
;
100 /* A distinguished value of BASE, negative infinite. During the
101 computation equals to BASE_MINIMUM, later mapped to BASE_NINF to
102 keep parser tables small. */
103 base_number base_ninf
= 0;
104 static base_number
*pos
= NULL
;
106 static unsigned int *conflrow
= NULL
;
107 unsigned int *conflict_table
= NULL
;
108 unsigned int *conflict_list
= NULL
;
109 int conflict_list_cnt
;
110 static int conflict_list_free
;
112 /* TABLE_SIZE is the allocated size of both TABLE and CHECK. We start
113 with more or less the original hard-coded value (which was
115 static size_t table_size
= 32768;
116 base_number
*table
= NULL
;
117 base_number
*check
= NULL
;
118 /* The value used in TABLE to denote explicit syntax errors
119 (%nonassoc), a negative infinite. First defaults to ACTION_NUMBER_MININUM,
120 but in order to keep small tables, renumbered as TABLE_ERROR, which
121 is the smallest (non error) value minus 1. */
122 base_number table_ninf
= 0;
126 state_number
*yydefgoto
;
127 rule_number
*yydefact
;
129 /*----------------------------------------------------------------.
130 | If TABLE (and CHECK) appear to be small to be addressed at |
131 | DESIRED, grow them. Note that TABLE[DESIRED] is to be used, so |
132 | the desired size is at least DESIRED + 1. |
133 `----------------------------------------------------------------*/
136 table_grow (size_t desired
)
138 size_t old_size
= table_size
;
140 while (table_size
<= desired
)
143 if (trace_flag
& trace_resource
)
144 fprintf (stderr
, "growing table and check from: %d to %d\n",
145 old_size
, table_size
);
147 table
= XREALLOC (table
, base_number
, table_size
);
148 check
= XREALLOC (check
, base_number
, table_size
);
149 conflict_table
= XREALLOC (conflict_table
, unsigned int, table_size
);
151 for (/* Nothing. */; old_size
< table_size
; ++old_size
)
154 check
[old_size
] = -1;
161 /*-------------------------------------------------------------------.
162 | For GLR parsers, for each conflicted token in S, as indicated |
163 | by non-zero entries in CONFLROW, create a list of possible |
164 | reductions that are alternatives to the shift or reduction |
165 | currently recorded for that token in S. Store the alternative |
166 | reductions followed by a 0 in CONFLICT_LIST, updating |
167 | CONFLICT_LIST_CNT, and storing an index to the start of the list |
168 | back into CONFLROW. |
169 `-------------------------------------------------------------------*/
172 conflict_row (state
*s
)
175 reductions
*reds
= s
->reductions
;
180 for (j
= 0; j
< ntokens
; j
+= 1)
183 conflrow
[j
] = conflict_list_cnt
;
185 /* Find all reductions for token J, and record all that do not
187 for (i
= 0; i
< reds
->num
; i
+= 1)
188 if (bitset_test (reds
->lookaheads
[i
], j
)
190 != rule_number_as_item_number (reds
->rules
[i
]->number
)))
192 if (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 if (conflict_list_free
<= 0)
202 conflict_list_cnt
+= 1;
203 conflict_list_free
-= 1;
208 /*------------------------------------------------------------------.
209 | Decide what to do for each type of token if seen as the lookahead |
210 | token in specified state. The value returned is used as the |
211 | default action (yydefact) for the state. In addition, ACTROW is |
212 | filled with what to do for each kind of token, index by symbol |
213 | number, with zero meaning do the default action. The value |
214 | ACTION_NUMBER_MINIMUM, a very negative number, means this |
215 | situation is an error. The parser recognizes this value |
218 | This is where conflicts are resolved. The loop over lookahead |
219 | rules considered lower-numbered rules last, and the last rule |
220 | considered that likes a token gets to handle it. |
222 | For GLR parsers, also sets CONFLROW[SYM] to an index into |
223 | CONFLICT_LIST iff there is an unresolved conflict (s/r or r/r) |
224 | with symbol SYM. The default reduction is not used for a symbol |
225 | that has any such conflicts. |
226 `------------------------------------------------------------------*/
229 action_row (state
*s
)
232 rule
*default_rule
= NULL
;
233 reductions
*reds
= s
->reductions
;
234 transitions
*trans
= s
->transitions
;
235 errs
*errp
= s
->errs
;
236 /* Set to nonzero to inhibit having any default reduction. */
240 for (i
= 0; i
< ntokens
; i
++)
241 actrow
[i
] = conflrow
[i
] = 0;
243 if (reds
->lookaheads
)
246 bitset_iterator biter
;
247 /* loop over all the rules available here which require
248 lookahead (in reverse order to give precedence to the first
250 for (i
= reds
->num
- 1; i
>= 0; --i
)
251 /* and find each token which the rule finds acceptable
253 BITSET_FOR_EACH (biter
, reds
->lookaheads
[i
], j
, 0)
255 /* and record this rule as the rule to use if that
258 conflicted
= conflrow
[j
] = 1;
259 actrow
[j
] = rule_number_as_item_number (reds
->rules
[i
]->number
);
263 /* Now see which tokens are allowed for shifts in this state. For
264 them, record the shift as the thing to do. So shift is preferred
266 FOR_EACH_SHIFT (trans
, i
)
268 symbol_number sym
= TRANSITION_SYMBOL (trans
, i
);
269 state
*shift_state
= trans
->states
[i
];
271 if (actrow
[sym
] != 0)
272 conflicted
= conflrow
[sym
] = 1;
273 actrow
[sym
] = state_number_as_int (shift_state
->number
);
275 /* Do not use any default reduction if there is a shift for
277 if (sym
== errtoken
->number
)
281 /* See which tokens are an explicit error in this state (due to
282 %nonassoc). For them, record ACTION_NUMBER_MINIMUM as the
284 for (i
= 0; i
< errp
->num
; i
++)
286 symbol
*sym
= errp
->symbols
[i
];
287 actrow
[sym
->number
] = ACTION_NUMBER_MINIMUM
;
290 /* Now find the most common reduction and make it the default action
293 if (reds
->num
>= 1 && !nodefault
)
296 default_rule
= reds
->rules
[0];
300 for (i
= 0; i
< reds
->num
; i
++)
303 rule
*r
= reds
->rules
[i
];
306 for (j
= 0; j
< ntokens
; j
++)
307 if (actrow
[j
] == rule_number_as_item_number (r
->number
))
317 /* GLR parsers need space for conflict lists, so we can't
318 default conflicted entries. For non-conflicted entries
319 or as long as we are not building a GLR parser,
320 actions that match the default are replaced with zero,
321 which means "use the default". */
326 for (j
= 0; j
< ntokens
; j
++)
327 if (actrow
[j
] == rule_number_as_item_number (default_rule
->number
)
328 && ! (glr_parser
&& conflrow
[j
]))
334 /* If have no default rule, the default is an error.
335 So replace any action which says "error" with "use default". */
338 for (i
= 0; i
< ntokens
; i
++)
339 if (actrow
[i
] == ACTION_NUMBER_MINIMUM
)
349 /*----------------------------------------.
350 | Set FROMS, TOS, TALLY and WIDTH for S. |
351 `----------------------------------------*/
354 save_row (state_number s
)
358 base_number
*sp
= NULL
;
359 base_number
*sp1
= NULL
;
360 base_number
*sp2
= NULL
;
361 unsigned int *sp3
= NULL
;
363 /* Number of non default actions in S. */
365 for (i
= 0; i
< ntokens
; i
++)
372 /* Allocate non defaulted actions. */
373 froms
[s
] = sp1
= sp
= XCALLOC (base_number
, count
);
374 tos
[s
] = sp2
= XCALLOC (base_number
, count
);
376 conflict_tos
[s
] = sp3
= XCALLOC (unsigned int, count
);
378 conflict_tos
[s
] = NULL
;
380 /* Store non defaulted actions. */
381 for (i
= 0; i
< ntokens
; i
++)
387 *sp3
++ = conflrow
[i
];
391 width
[s
] = sp1
[-1] - sp
[0] + 1;
395 /*------------------------------------------------------------------.
396 | Figure out the actions for the specified state, indexed by |
397 | lookahead token type. |
399 | The YYDEFACT table is output now. The detailed info is saved for |
400 | putting into YYTABLE later. |
401 `------------------------------------------------------------------*/
410 int nconflict
= glr_parser
? conflicts_total_count () : 0;
412 yydefact
= XCALLOC (rule_number
, nstates
);
414 actrow
= XCALLOC (action_number
, ntokens
);
415 conflrow
= XCALLOC (unsigned int, ntokens
);
417 conflict_list
= XCALLOC (unsigned int, 1 + 2 * nconflict
);
418 conflict_list_free
= 2 * nconflict
;
419 conflict_list_cnt
= 1;
421 /* Find the rules which are reduced. */
423 for (r
= 0; r
< nrules
; ++r
)
424 rules
[r
].useful
= false;
426 for (i
= 0; i
< nstates
; ++i
)
428 rule
*default_rule
= action_row (states
[i
]);
429 yydefact
[i
] = default_rule
? default_rule
->number
+ 1 : 0;
432 /* Now that the parser was computed, we can find which rules are
433 really reduced, and which are not because of SR or RR
437 for (j
= 0; j
< ntokens
; ++j
)
438 if (actrow
[j
] < 0 && actrow
[j
] != ACTION_NUMBER_MINIMUM
)
439 rules
[item_number_as_rule_number (actrow
[j
])].useful
= true;
441 rules
[yydefact
[i
] - 1].useful
= true;
450 /*------------------------------------------------------------------.
451 | Compute FROMS[VECTOR], TOS[VECTOR], TALLY[VECTOR], WIDTH[VECTOR], |
452 | i.e., the information related to non defaulted GOTO on the nterm |
455 | DEFAULT_STATE is the principal destination on SYM, i.e., the |
456 | default GOTO destination on SYM. |
457 `------------------------------------------------------------------*/
460 save_column (symbol_number sym
, state_number default_state
)
467 vector_number symno
= symbol_number_to_vector_number (sym
);
469 goto_number begin
= goto_map
[sym
];
470 goto_number end
= goto_map
[sym
+ 1];
472 /* Number of non default GOTO. */
474 for (i
= begin
; i
< end
; i
++)
475 if (to_state
[i
] != default_state
)
481 /* Allocate room for non defaulted gotos. */
482 froms
[symno
] = sp1
= sp
= XCALLOC (base_number
, count
);
483 tos
[symno
] = sp2
= XCALLOC (base_number
, count
);
485 /* Store the state numbers of the non defaulted gotos. */
486 for (i
= begin
; i
< end
; i
++)
487 if (to_state
[i
] != default_state
)
489 *sp1
++ = from_state
[i
];
490 *sp2
++ = to_state
[i
];
493 tally
[symno
] = count
;
494 width
[symno
] = sp1
[-1] - sp
[0] + 1;
498 /*-------------------------------------------------------------.
499 | Return `the' most common destination GOTO on SYM (a nterm). |
500 `-------------------------------------------------------------*/
503 default_goto (symbol_number sym
, short state_count
[])
507 goto_number m
= goto_map
[sym
];
508 goto_number n
= goto_map
[sym
+ 1];
509 state_number default_state
= (state_number
) -1;
513 return (state_number
) -1;
515 for (s
= 0; s
< nstates
; s
++)
518 for (i
= m
; i
< n
; i
++)
519 state_count
[to_state
[i
]]++;
521 for (s
= 0; s
< nstates
; s
++)
522 if (state_count
[s
] > max
)
524 max
= state_count
[s
];
528 return default_state
;
532 /*-------------------------------------------------------------------.
533 | Figure out what to do after reducing with each rule, depending on |
534 | the saved state from before the beginning of parsing the data that |
535 | matched this rule. |
537 | The YYDEFGOTO table is output now. The detailed info is saved for |
538 | putting into YYTABLE later. |
539 `-------------------------------------------------------------------*/
545 short *state_count
= XCALLOC (short, nstates
);
546 yydefgoto
= XMALLOC (state_number
, nvars
);
548 /* For a given nterm I, STATE_COUNT[S] is the number of times there
549 is a GOTO to S on I. */
550 for (i
= ntokens
; i
< nsyms
; ++i
)
552 state_number default_state
= default_goto (i
, state_count
);
553 save_column (i
, default_state
);
554 yydefgoto
[i
- ntokens
] = default_state
;
560 /*------------------------------------------------------------------.
561 | Compute ORDER, a reordering of vectors, in order to decide how to |
562 | pack the actions and gotos information into yytable. |
563 `------------------------------------------------------------------*/
572 for (i
= 0; i
< nvectors
; i
++)
578 int j
= nentries
- 1;
580 while (j
>= 0 && (width
[order
[j
]] < w
))
583 while (j
>= 0 && (width
[order
[j
]] == w
) && (tally
[order
[j
]] < t
))
586 for (k
= nentries
- 1; k
> j
; k
--)
587 order
[k
+ 1] = order
[k
];
595 /* If VECTOR is a state which actions (reflected by FROMS, TOS, TALLY
596 and WIDTH of VECTOR) are common to a previous state, return this
599 In any other case, return -1. */
602 matching_state (vector_number vector
)
604 vector_number i
= order
[vector
];
609 /* If VECTOR is a nterm, return -1. */
610 if (i
>= (int) nstates
)
616 /* If VECTOR has GLR conflicts, return -1 */
617 if (conflict_tos
[i
] != NULL
)
620 for (j
= 0; j
< t
; j
+= 1)
621 if (conflict_tos
[i
][j
] != 0)
625 for (prev
= vector
- 1; prev
>= 0; prev
--)
627 vector_number j
= order
[prev
];
631 /* Given how ORDER was computed, if the WIDTH or TALLY is
632 different, there cannot be a matching state. */
633 if (width
[j
] != w
|| tally
[j
] != t
)
636 for (k
= 0; match
&& k
< t
; k
++)
637 if (tos
[j
][k
] != tos
[i
][k
] || froms
[j
][k
] != froms
[i
][k
]
638 || (conflict_tos
[j
] != NULL
&& conflict_tos
[j
][k
] != 0))
650 pack_vector (vector_number vector
)
652 vector_number i
= order
[vector
];
656 base_number
*from
= froms
[i
];
657 base_number
*to
= tos
[i
];
658 unsigned int *conflict_to
= conflict_tos
[i
];
663 for (j
= lowzero
- from
[0]; ; j
++)
668 if ((int) table_size
<= j
)
671 for (k
= 0; ok
&& k
< t
; k
++)
673 loc
= j
+ state_number_as_int (from
[k
]);
674 if (loc
>= (int) table_size
)
681 for (k
= 0; ok
&& k
< vector
; k
++)
687 for (k
= 0; k
< t
; k
++)
691 if (glr_parser
&& conflict_to
!= NULL
)
692 conflict_table
[loc
] = conflict_to
[k
];
693 check
[loc
] = from
[k
];
696 while (table
[lowzero
] != 0)
702 if (! (BASE_MINIMUM
<= j
&& j
<= BASE_MAXIMUM
))
710 /*-------------------------------------------------------------.
711 | Remap the negative infinite in TAB from NINF to the greatest |
712 | possible smallest value. Return it. |
714 | In most case this allows us to use shorts instead of ints in |
716 `-------------------------------------------------------------*/
719 table_ninf_remap (base_number tab
[], size_t size
, base_number ninf
)
724 for (i
= 0; i
< size
; i
++)
725 if (tab
[i
] < res
&& tab
[i
] != ninf
)
730 for (i
= 0; i
< size
; i
++)
742 base
= XCALLOC (base_number
, nvectors
);
743 pos
= XCALLOC (base_number
, nentries
);
744 table
= XCALLOC (base_number
, table_size
);
745 conflict_table
= XCALLOC (unsigned int, table_size
);
746 check
= XCALLOC (base_number
, table_size
);
751 for (i
= 0; i
< nvectors
; i
++)
752 base
[i
] = BASE_MINIMUM
;
754 for (i
= 0; i
< (int) table_size
; i
++)
757 for (i
= 0; i
< nentries
; i
++)
759 state_number s
= matching_state (i
);
763 /* A new set of state actions, or a nonterminal. */
764 place
= pack_vector (i
);
766 /* Action of I were already coded for S. */
770 base
[order
[i
]] = place
;
773 /* Use the greatest possible negative infinites. */
774 base_ninf
= table_ninf_remap (base
, nvectors
, BASE_MINIMUM
);
775 table_ninf
= table_ninf_remap (table
, high
+ 1, ACTION_NUMBER_MINIMUM
);
782 /*-----------------------------------------------------------------.
783 | Compute and output yydefact, yydefgoto, yypact, yypgoto, yytable |
785 `-----------------------------------------------------------------*/
788 tables_generate (void)
792 /* This is a poor way to make sure the sizes are properly
793 correlated. In particular the signedness is not taken into
794 account. But it's not useless. */
795 verify (sizes_are_properly_correlated
,
796 (sizeof nstates
<= sizeof nvectors
797 && sizeof nvars
<= sizeof nvectors
));
799 nvectors
= state_number_as_int (nstates
) + nvars
;
801 froms
= XCALLOC (base_number
*, nvectors
);
802 tos
= XCALLOC (base_number
*, nvectors
);
803 conflict_tos
= XCALLOC (unsigned int *, nvectors
);
804 tally
= XCALLOC (short, nvectors
);
805 width
= XCALLOC (base_number
, nvectors
);
810 free (goto_map
+ ntokens
);
814 order
= XCALLOC (vector_number
, nvectors
);
822 for (i
= 0; i
< nvectors
; i
++)
826 XFREE (conflict_tos
[i
]);
835 /*-------------------------.
836 | Free the parser tables. |
837 `-------------------------*/
843 free (conflict_table
);
844 free (conflict_list
);