1 /* Output the generated parsing program for Bison.
3 Copyright (C) 1984, 1986, 1989, 1992, 2000, 2001, 2002, 2003
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 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 *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 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
;
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
->lookaheads
[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 lookahead |
219 | token 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 lookahead |
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. */
249 for (i
= 0; i
< ntokens
; i
++)
250 actrow
[i
] = conflrow
[i
] = 0;
252 if (reds
->lookaheads
)
255 bitset_iterator biter
;
256 /* loop over all the rules available here which require
257 lookahead (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
->lookaheads
[i
], j
, 0)
264 /* and record this rule as the rule to use if that
267 conflicted
= conflrow
[j
] = 1;
268 actrow
[j
] = rule_number_as_item_number (reds
->rules
[i
]->number
);
272 /* Now see which tokens are allowed for shifts in this state. For
273 them, record the shift as the thing to do. So shift is preferred
275 FOR_EACH_SHIFT (trans
, i
)
277 symbol_number sym
= TRANSITION_SYMBOL (trans
, i
);
278 state
*shift_state
= trans
->states
[i
];
280 if (actrow
[sym
] != 0)
281 conflicted
= conflrow
[sym
] = 1;
282 actrow
[sym
] = state_number_as_int (shift_state
->number
);
284 /* Do not use any default reduction if there is a shift for
286 if (sym
== errtoken
->number
)
290 /* See which tokens are an explicit error in this state (due to
291 %nonassoc). For them, record ACTION_NUMBER_MINIMUM as the
293 for (i
= 0; i
< errp
->num
; i
++)
295 symbol
*sym
= errp
->symbols
[i
];
296 actrow
[sym
->number
] = ACTION_NUMBER_MINIMUM
;
299 /* Now find the most common reduction and make it the default action
302 if (reds
->num
>= 1 && !nodefault
)
305 default_rule
= reds
->rules
[0];
309 for (i
= 0; i
< reds
->num
; i
++)
312 rule
*r
= reds
->rules
[i
];
315 for (j
= 0; j
< ntokens
; j
++)
316 if (actrow
[j
] == rule_number_as_item_number (r
->number
))
326 /* GLR parsers need space for conflict lists, so we can't
327 default conflicted entries. For non-conflicted entries
328 or as long as we are not building a GLR parser,
329 actions that match the default are replaced with zero,
330 which means "use the default". */
335 for (j
= 0; j
< ntokens
; j
++)
336 if (actrow
[j
] == rule_number_as_item_number (default_rule
->number
)
337 && ! (glr_parser
&& conflrow
[j
]))
343 /* If have no default rule, the default is an error.
344 So replace any action which says "error" with "use default". */
347 for (i
= 0; i
< ntokens
; i
++)
348 if (actrow
[i
] == ACTION_NUMBER_MINIMUM
)
358 /*----------------------------------------.
359 | Set FROMS, TOS, TALLY and WIDTH for S. |
360 `----------------------------------------*/
363 save_row (state_number s
)
370 unsigned int *sp3
IF_LINT (= NULL
);
372 /* Number of non default actions in S. */
374 for (i
= 0; i
< ntokens
; i
++)
381 /* Allocate non defaulted actions. */
382 froms
[s
] = sp
= CALLOC (sp1
, count
);
383 tos
[s
] = CALLOC (sp2
, count
);
384 conflict_tos
[s
] = glr_parser
? CALLOC (sp3
, count
) : NULL
;
386 /* Store non defaulted actions. */
387 for (i
= 0; i
< ntokens
; i
++)
393 *sp3
++ = conflrow
[i
];
397 width
[s
] = sp1
[-1] - sp
[0] + 1;
401 /*------------------------------------------------------------------.
402 | Figure out the actions for the specified state, indexed by |
403 | lookahead token type. |
405 | The YYDEFACT table is output now. The detailed info is saved for |
406 | putting into YYTABLE later. |
407 `------------------------------------------------------------------*/
416 int nconflict
= glr_parser
? conflicts_total_count () : 0;
418 CALLOC (yydefact
, nstates
);
420 CALLOC (actrow
, ntokens
);
421 CALLOC (conflrow
, ntokens
);
423 CALLOC (conflict_list
, 1 + 2 * nconflict
);
424 conflict_list_free
= 2 * nconflict
;
425 conflict_list_cnt
= 1;
427 /* Find the rules which are reduced. */
429 for (r
= 0; r
< nrules
; ++r
)
430 rules
[r
].useful
= false;
432 for (i
= 0; i
< nstates
; ++i
)
434 rule
*default_rule
= action_row (states
[i
]);
435 yydefact
[i
] = default_rule
? default_rule
->number
+ 1 : 0;
438 /* Now that the parser was computed, we can find which rules are
439 really reduced, and which are not because of SR or RR
443 for (j
= 0; j
< ntokens
; ++j
)
444 if (actrow
[j
] < 0 && actrow
[j
] != ACTION_NUMBER_MINIMUM
)
445 rules
[item_number_as_rule_number (actrow
[j
])].useful
= true;
447 rules
[yydefact
[i
] - 1].useful
= true;
456 /*------------------------------------------------------------------.
457 | Compute FROMS[VECTOR], TOS[VECTOR], TALLY[VECTOR], WIDTH[VECTOR], |
458 | i.e., the information related to non defaulted GOTO on the nterm |
461 | DEFAULT_STATE is the principal destination on SYM, i.e., the |
462 | default GOTO destination on SYM. |
463 `------------------------------------------------------------------*/
466 save_column (symbol_number sym
, state_number default_state
)
473 vector_number symno
= symbol_number_to_vector_number (sym
);
475 goto_number begin
= goto_map
[sym
- ntokens
];
476 goto_number end
= goto_map
[sym
- ntokens
+ 1];
478 /* Number of non default GOTO. */
480 for (i
= begin
; i
< end
; i
++)
481 if (to_state
[i
] != default_state
)
487 /* Allocate room for non defaulted gotos. */
488 froms
[symno
] = sp
= CALLOC (sp1
, count
);
489 tos
[symno
] = CALLOC (sp2
, count
);
491 /* Store the state numbers of the non defaulted gotos. */
492 for (i
= begin
; i
< end
; i
++)
493 if (to_state
[i
] != default_state
)
495 *sp1
++ = from_state
[i
];
496 *sp2
++ = to_state
[i
];
499 tally
[symno
] = count
;
500 width
[symno
] = sp1
[-1] - sp
[0] + 1;
504 /*-------------------------------------------------------------.
505 | Return `the' most common destination GOTO on SYM (a nterm). |
506 `-------------------------------------------------------------*/
509 default_goto (symbol_number sym
, short state_count
[])
513 goto_number m
= goto_map
[sym
- ntokens
];
514 goto_number n
= goto_map
[sym
- ntokens
+ 1];
515 state_number default_state
= -1;
521 for (s
= 0; s
< nstates
; s
++)
524 for (i
= m
; i
< n
; i
++)
525 state_count
[to_state
[i
]]++;
527 for (s
= 0; s
< nstates
; s
++)
528 if (state_count
[s
] > max
)
530 max
= state_count
[s
];
534 return default_state
;
538 /*-------------------------------------------------------------------.
539 | Figure out what to do after reducing with each rule, depending on |
540 | the saved state from before the beginning of parsing the data that |
541 | matched this rule. |
543 | The YYDEFGOTO table is output now. The detailed info is saved for |
544 | putting into YYTABLE later. |
545 `-------------------------------------------------------------------*/
551 short *state_count
= CALLOC (state_count
, nstates
);
552 MALLOC (yydefgoto
, nvars
);
554 /* For a given nterm I, STATE_COUNT[S] is the number of times there
555 is a GOTO to S on I. */
556 for (i
= ntokens
; i
< nsyms
; ++i
)
558 state_number default_state
= default_goto (i
, state_count
);
559 save_column (i
, default_state
);
560 yydefgoto
[i
- ntokens
] = default_state
;
566 /*------------------------------------------------------------------.
567 | Compute ORDER, a reordering of vectors, in order to decide how to |
568 | pack the actions and gotos information into yytable. |
569 `------------------------------------------------------------------*/
578 for (i
= 0; i
< nvectors
; i
++)
584 int j
= nentries
- 1;
586 while (j
>= 0 && (width
[order
[j
]] < w
))
589 while (j
>= 0 && (width
[order
[j
]] == w
) && (tally
[order
[j
]] < t
))
592 for (k
= nentries
- 1; k
> j
; k
--)
593 order
[k
+ 1] = order
[k
];
601 /* If VECTOR is a state which actions (reflected by FROMS, TOS, TALLY
602 and WIDTH of VECTOR) are common to a previous state, return this
605 In any other case, return -1. */
608 matching_state (vector_number vector
)
610 vector_number i
= order
[vector
];
615 /* If VECTOR is a nterm, return -1. */
622 /* If VECTOR has GLR conflicts, return -1 */
623 if (conflict_tos
[i
] != NULL
)
626 for (j
= 0; j
< t
; j
+= 1)
627 if (conflict_tos
[i
][j
] != 0)
631 for (prev
= vector
- 1; prev
>= 0; prev
--)
633 vector_number j
= order
[prev
];
637 /* Given how ORDER was computed, if the WIDTH or TALLY is
638 different, there cannot be a matching state. */
639 if (width
[j
] != w
|| tally
[j
] != t
)
642 for (k
= 0; match
&& k
< t
; k
++)
643 if (tos
[j
][k
] != tos
[i
][k
] || froms
[j
][k
] != froms
[i
][k
]
644 || (conflict_tos
[j
] != NULL
&& conflict_tos
[j
][k
] != 0))
656 pack_vector (vector_number vector
)
658 vector_number i
= order
[vector
];
662 base_number
*from
= froms
[i
];
663 base_number
*to
= tos
[i
];
664 unsigned int *conflict_to
= conflict_tos
[i
];
669 for (j
= lowzero
- from
[0]; ; j
++)
677 for (k
= 0; ok
&& k
< t
; k
++)
679 loc
= j
+ state_number_as_int (from
[k
]);
680 if (table_size
<= loc
)
687 for (k
= 0; ok
&& k
< vector
; k
++)
693 for (k
= 0; k
< t
; k
++)
697 if (glr_parser
&& conflict_to
!= NULL
)
698 conflict_table
[loc
] = conflict_to
[k
];
699 check
[loc
] = from
[k
];
702 while (table
[lowzero
] != 0)
708 if (! (BASE_MINIMUM
<= j
&& j
<= BASE_MAXIMUM
))
716 /*-------------------------------------------------------------.
717 | Remap the negative infinite in TAB from NINF to the greatest |
718 | possible smallest value. Return it. |
720 | In most case this allows us to use shorts instead of ints in |
722 `-------------------------------------------------------------*/
725 table_ninf_remap (base_number tab
[], int size
, base_number ninf
)
730 for (i
= 0; i
< size
; i
++)
731 if (tab
[i
] < res
&& tab
[i
] != ninf
)
736 for (i
= 0; i
< size
; i
++)
748 CALLOC (base
, nvectors
);
749 CALLOC (pos
, nentries
);
750 CALLOC (table
, table_size
);
751 CALLOC (conflict_table
, table_size
);
752 CALLOC (check
, table_size
);
757 for (i
= 0; i
< nvectors
; i
++)
758 base
[i
] = BASE_MINIMUM
;
760 for (i
= 0; i
< table_size
; i
++)
763 for (i
= 0; i
< nentries
; i
++)
765 state_number s
= matching_state (i
);
769 /* A new set of state actions, or a nonterminal. */
770 place
= pack_vector (i
);
772 /* Action of I were already coded for S. */
776 base
[order
[i
]] = place
;
779 /* Use the greatest possible negative infinites. */
780 base_ninf
= table_ninf_remap (base
, nvectors
, BASE_MINIMUM
);
781 table_ninf
= table_ninf_remap (table
, high
+ 1, ACTION_NUMBER_MINIMUM
);
788 /*-----------------------------------------------------------------.
789 | Compute and output yydefact, yydefgoto, yypact, yypgoto, yytable |
791 `-----------------------------------------------------------------*/
794 tables_generate (void)
798 /* This is a poor way to make sure the sizes are properly
799 correlated. In particular the signedness is not taken into
800 account. But it's not useless. */
801 verify (sizes_are_properly_correlated
,
802 (sizeof nstates
<= sizeof nvectors
803 && sizeof nvars
<= sizeof nvectors
));
805 nvectors
= state_number_as_int (nstates
) + nvars
;
807 CALLOC (froms
, nvectors
);
808 CALLOC (tos
, nvectors
);
809 CALLOC (conflict_tos
, nvectors
);
810 CALLOC (tally
, nvectors
);
811 CALLOC (width
, nvectors
);
820 CALLOC (order
, nvectors
);
828 for (i
= 0; i
< nvectors
; i
++)
832 XFREE (conflict_tos
[i
]);
841 /*-------------------------.
842 | Free the parser tables. |
843 `-------------------------*/
849 free (conflict_table
);
850 free (conflict_list
);