]>
Commit | Line | Data |
---|---|---|
c3e23647 | 1 | /* Output the generated parsing program for bison, |
5bb18f9a | 2 | Copyright (C) 1984, 1986, 1989, 1992, 2000, 2001, 2002 |
255ef638 | 3 | Free Software Foundation, Inc. |
c3e23647 | 4 | |
9ee3c97b | 5 | This file is part of Bison, the GNU Compiler Compiler. |
c3e23647 | 6 | |
9ee3c97b AD |
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) | |
10 | any later version. | |
c3e23647 | 11 | |
9ee3c97b AD |
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. | |
c3e23647 | 16 | |
9ee3c97b AD |
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 | |
20 | 02111-1307, USA. */ | |
c3e23647 RS |
21 | |
22 | ||
7db2ed2d | 23 | /* The parser tables consist of these tables. |
c3e23647 | 24 | |
255ef638 AD |
25 | YYTRANSLATE = vector mapping yylex's token numbers into bison's |
26 | token numbers. | |
c3e23647 | 27 | |
7db2ed2d | 28 | YYTNAME = vector of string-names indexed by bison token number. |
c3e23647 | 29 | |
7db2ed2d AD |
30 | YYTOKNUM = vector of yylex token numbers corresponding to entries |
31 | in YYTNAME. | |
c3e23647 | 32 | |
255ef638 AD |
33 | YYRLINE = vector of line-numbers of all rules. For yydebug |
34 | printouts. | |
c3e23647 | 35 | |
255ef638 AD |
36 | YYRHS = vector of items of all rules. This is exactly what RITEMS |
37 | contains. For yydebug and for semantic parser. | |
c3e23647 | 38 | |
255ef638 | 39 | YYPRHS[R] = index in YYRHS of first item for rule R. |
c3e23647 | 40 | |
255ef638 | 41 | YYR1[R] = symbol number of symbol that rule R derives. |
c3e23647 | 42 | |
255ef638 | 43 | YYR2[R] = number of symbols composing right hand side of rule R. |
c3e23647 | 44 | |
7db2ed2d | 45 | YYSTOS[S] = the symbol number of the symbol that leads to state S. |
e372befa | 46 | |
255ef638 AD |
47 | YYDEFACT[S] = default rule to reduce with in state s, when YYTABLE |
48 | doesn't specify something else to do. Zero means the default is an | |
49 | error. | |
c3e23647 | 50 | |
255ef638 AD |
51 | YYDEFGOTO[I] = default state to go to after a reduction of a rule |
52 | that generates variable NTOKENS + I, except when YYTABLE specifies | |
53 | something else to do. | |
c3e23647 | 54 | |
255ef638 AD |
55 | YYPACT[S] = index in YYTABLE of the portion describing state S. |
56 | The lookahead token's type is used to index that portion to find | |
57 | out what to do. | |
c3e23647 | 58 | |
255ef638 AD |
59 | If the value in YYTABLE is positive, we shift the token and go to |
60 | that state. | |
c3e23647 | 61 | |
6c89f1c1 | 62 | If the value is negative, it is minus a rule number to reduce by. |
c3e23647 | 63 | |
255ef638 AD |
64 | If the value is zero, the default action from YYDEFACT[S] is used. |
65 | ||
66 | YYPGOTO[I] = the index in YYTABLE of the portion describing what to | |
67 | do after reducing a rule that derives variable I + NTOKENS. This | |
68 | portion is indexed by the parser state number, S, as of before the | |
69 | text for this nonterminal was read. The value from YYTABLE is the | |
70 | state to go to if the corresponding value in YYCHECK is S. | |
71 | ||
72 | YYTABLE = a vector filled with portions for different uses, found | |
73 | via YYPACT and YYPGOTO. | |
74 | ||
75 | YYCHECK = a vector indexed in parallel with YYTABLE. It indicates, | |
76 | in a roundabout way, the bounds of the portion you are trying to | |
77 | examine. | |
78 | ||
12b0043a | 79 | Suppose that the portion of YYTABLE starts at index P and the index |
255ef638 AD |
80 | to be examined within the portion is I. Then if YYCHECK[P+I] != I, |
81 | I is outside the bounds of what is actually allocated, and the | |
82 | default (from YYDEFACT or YYDEFGOTO) should be used. Otherwise, | |
83 | YYTABLE[P+I] should be used. | |
84 | ||
85 | YYFINAL = the state number of the termination state. YYFLAG = most | |
86 | negative short int. Used to flag ?? */ | |
c3e23647 | 87 | |
c3e23647 | 88 | #include "system.h" |
914feea9 | 89 | #include "bitsetv.h" |
14d3eb9b | 90 | #include "quotearg.h" |
be2a1a68 | 91 | #include "error.h" |
ceed8467 | 92 | #include "getargs.h" |
c3e23647 RS |
93 | #include "files.h" |
94 | #include "gram.h" | |
b2ca4022 | 95 | #include "LR0.h" |
a0f6b076 | 96 | #include "complain.h" |
6c89f1c1 | 97 | #include "output.h" |
720d742f | 98 | #include "lalr.h" |
a70083a3 | 99 | #include "reader.h" |
ad949da9 | 100 | #include "symtab.h" |
0619caf0 | 101 | #include "conflicts.h" |
11d82f03 | 102 | #include "muscle_tab.h" |
be2a1a68 | 103 | |
be2a1a68 | 104 | /* From src/scan-skel.l. */ |
536545f3 | 105 | void m4_invoke PARAMS ((const char *definitions)); |
d019d655 | 106 | |
12b0043a AD |
107 | |
108 | /* Several tables will be indexed both by state and nonterminal | |
109 | numbers. We call `vector' such a thing (= either a state or a | |
110 | symbol number. | |
111 | ||
112 | Of course vector_number_t ought to be wide enough to contain | |
113 | state_number_t and symbol_number_t. */ | |
114 | typedef short vector_number_t; | |
115 | #define VECTOR_NUMBER_MAX ((vector_number_t) SHRT_MAX) | |
116 | #define VECTOR_NUMBER_MIN ((vector_number_t) SHRT_MIN) | |
117 | #define state_number_to_vector_number(State) \ | |
118 | ((vector_number_t) State) | |
119 | #define symbol_number_to_vector_number(Symbol) \ | |
120 | ((vector_number_t) (state_number_as_int (nstates) + Symbol - ntokens)) | |
121 | ||
c3e23647 | 122 | static int nvectors; |
12b0043a AD |
123 | |
124 | ||
125 | /* FROMS and TOS are indexed by vector_number_t. | |
126 | ||
127 | If VECTOR is a nonterminal, (FROMS[VECTOR], TOS[VECTOR]) form an | |
128 | array of state numbers of the non defaulted GOTO on VECTOR. | |
129 | ||
130 | If VECTOR is a state, TOS[VECTOR] is the array of actions to do on | |
131 | the (array of) symbols FROMS[VECTOR]. | |
132 | ||
133 | In both cases, TALLY[VECTOR] is the size of the arrays | |
134 | FROMS[VECTOR], TOS[VECTOR]; and WIDTH[VECTOR] = | |
135 | (FROMS[VECTOR][SIZE] - FROMS[VECTOR][0] + 1) where SIZE = | |
136 | TALLY[VECTOR]. | |
137 | ||
138 | FROMS therefore contains symbol_number_t and action_number_t, | |
139 | TOS state_number_t and action_number_t, | |
140 | TALLY sizes, | |
141 | WIDTH differences of FROMS. | |
142 | ||
143 | Let base_t be the type of FROMS, TOS, and WIDTH. */ | |
144 | typedef int base_t; | |
145 | #define BASE_MAX ((base_t) INT_MAX) | |
146 | #define BASE_MIN ((base_t) INT_MIN) | |
147 | ||
148 | static base_t **froms = NULL; | |
149 | static base_t **tos = NULL; | |
676385e2 | 150 | static unsigned int **conflict_tos = NULL; |
342b8b6e | 151 | static short *tally = NULL; |
12b0043a AD |
152 | static base_t *width = NULL; |
153 | ||
133c20e2 | 154 | |
12b0043a AD |
155 | /* For a given state, N = ACTROW[SYMBOL]: |
156 | ||
157 | If N = 0, stands for `run the default action'. | |
158 | If N = MIN, stands for `raise a parse error'. | |
159 | If N > 0, stands for `shift SYMBOL and go to n'. | |
160 | If N < 0, stands for `reduce -N'. */ | |
161 | typedef short action_t; | |
162 | #define ACTION_MAX ((action_t) SHRT_MAX) | |
163 | #define ACTION_MIN ((action_t) SHRT_MIN) | |
164 | ||
165 | static action_t *actrow = NULL; | |
166 | ||
167 | /* FROMS and TOS are reordered to be compressed. ORDER[VECTOR] is the | |
168 | new vector number of VECTOR. We skip `empty' vectors (i.e., | |
169 | TALLY[VECTOR] = 0), and call these `entries'. */ | |
170 | static vector_number_t *order = NULL; | |
171 | static int nentries; | |
172 | ||
173 | static base_t *base = NULL; | |
174 | /* A distinguished value of BASE, negative infinite. During the | |
175 | computation equals to BASE_MIN, later mapped to BASE_NINF to | |
176 | keep parser tables small. */ | |
177 | base_t base_ninf = 0; | |
178 | static base_t *pos = NULL; | |
179 | ||
180 | static unsigned int *conflrow = NULL; | |
676385e2 PH |
181 | static unsigned int *conflict_table = NULL; |
182 | static unsigned int *conflict_list = NULL; | |
183 | static int conflict_list_cnt; | |
184 | static int conflict_list_free; | |
185 | ||
133c20e2 AD |
186 | /* TABLE_SIZE is the allocated size of both TABLE and CHECK. |
187 | We start with the original hard-coded value: SHRT_MAX | |
188 | (yes, not USHRT_MAX). */ | |
189 | static size_t table_size = SHRT_MAX; | |
12b0043a AD |
190 | static base_t *table = NULL; |
191 | static base_t *check = NULL; | |
192 | /* The value used in TABLE to denote explicit parse errors | |
193 | (%nonassoc), a negative infinite. First defaults to ACTION_MIN, | |
194 | but in order to keep small tables, renumbered as TABLE_ERROR, which | |
195 | is the smallest (non error) value minus 1. */ | |
196 | base_t table_ninf = 0; | |
c3e23647 RS |
197 | static int lowzero; |
198 | static int high; | |
199 | ||
f87685c3 | 200 | static struct obstack format_obstack; |
c3e23647 | 201 | |
c7925b99 MA |
202 | int error_verbose = 0; |
203 | ||
f0440388 | 204 | |
133c20e2 AD |
205 | /*----------------------------------------------------------------. |
206 | | If TABLE (and CHECK) appear to be small to be addressed at | | |
207 | | DESIRED, grow them. Note that TABLE[DESIRED] is to be used, so | | |
208 | | the desired size is at least DESIRED + 1. | | |
209 | `----------------------------------------------------------------*/ | |
210 | ||
211 | static void | |
212 | table_grow (size_t desired) | |
213 | { | |
214 | size_t old_size = table_size; | |
215 | ||
216 | while (table_size <= desired) | |
217 | table_size *= 2; | |
218 | ||
219 | if (trace_flag) | |
220 | fprintf (stderr, "growing table and check from: %d to %d\n", | |
221 | old_size, table_size); | |
222 | ||
12b0043a AD |
223 | table = XREALLOC (table, base_t, table_size); |
224 | check = XREALLOC (check, base_t, table_size); | |
676385e2 PH |
225 | if (glr_parser) |
226 | conflict_table = XREALLOC (conflict_table, unsigned int, table_size); | |
133c20e2 AD |
227 | |
228 | for (/* Nothing. */; old_size < table_size; ++old_size) | |
229 | { | |
230 | table[old_size] = 0; | |
231 | check[old_size] = -1; | |
232 | } | |
233 | } | |
234 | ||
235 | ||
5372019f AD |
236 | /*-------------------------------------------------------------------. |
237 | | Create a function NAME which associates to the muscle NAME the | | |
238 | | result of formatting the FIRST and then TABLE_DATA[BEGIN..END[ (of | | |
239 | | TYPE), and to the muscle NAME_max, the max value of the | | |
240 | | TABLE_DATA. | | |
241 | `-------------------------------------------------------------------*/ | |
62a3e4f0 | 242 | |
62a3e4f0 | 243 | |
5372019f | 244 | #define GENERATE_MUSCLE_INSERT_TABLE(Name, Type) \ |
5fbb0954 | 245 | \ |
5372019f AD |
246 | static void \ |
247 | Name (const char *name, \ | |
5fbb0954 AD |
248 | Type *table_data, \ |
249 | Type first, \ | |
250 | int begin, \ | |
251 | int end) \ | |
252 | { \ | |
12b0043a | 253 | Type min = first; \ |
0c2d3f4c | 254 | Type max = first; \ |
5fbb0954 AD |
255 | int i; \ |
256 | int j = 1; \ | |
257 | \ | |
5372019f | 258 | obstack_fgrow1 (&format_obstack, "%6d", first); \ |
5fbb0954 AD |
259 | for (i = begin; i < end; ++i) \ |
260 | { \ | |
5372019f | 261 | obstack_1grow (&format_obstack, ','); \ |
5fbb0954 AD |
262 | if (j >= 10) \ |
263 | { \ | |
5372019f | 264 | obstack_sgrow (&format_obstack, "\n "); \ |
5fbb0954 AD |
265 | j = 1; \ |
266 | } \ | |
267 | else \ | |
268 | ++j; \ | |
5372019f | 269 | obstack_fgrow1 (&format_obstack, "%6d", table_data[i]); \ |
12b0043a AD |
270 | if (table_data[i] < min) \ |
271 | min = table_data[i]; \ | |
272 | if (max < table_data[i]) \ | |
5fbb0954 AD |
273 | max = table_data[i]; \ |
274 | } \ | |
5372019f AD |
275 | obstack_1grow (&format_obstack, 0); \ |
276 | muscle_insert (name, obstack_finish (&format_obstack)); \ | |
5fbb0954 | 277 | \ |
12b0043a AD |
278 | /* Build `NAME_min' and `NAME_max' in the obstack. */ \ |
279 | obstack_fgrow1 (&format_obstack, "%s_min", name); \ | |
280 | obstack_1grow (&format_obstack, 0); \ | |
281 | MUSCLE_INSERT_LONG_INT (obstack_finish (&format_obstack), \ | |
282 | (long int) min); \ | |
5372019f AD |
283 | obstack_fgrow1 (&format_obstack, "%s_max", name); \ |
284 | obstack_1grow (&format_obstack, 0); \ | |
0c2d3f4c AD |
285 | MUSCLE_INSERT_LONG_INT (obstack_finish (&format_obstack), \ |
286 | (long int) max); \ | |
62a3e4f0 AD |
287 | } |
288 | ||
5372019f | 289 | GENERATE_MUSCLE_INSERT_TABLE(muscle_insert_unsigned_int_table, unsigned int) |
12b0043a | 290 | GENERATE_MUSCLE_INSERT_TABLE(muscle_insert_int_table, int) |
5372019f | 291 | GENERATE_MUSCLE_INSERT_TABLE(muscle_insert_short_table, short) |
12b0043a AD |
292 | GENERATE_MUSCLE_INSERT_TABLE(muscle_insert_base_table, base_t) |
293 | GENERATE_MUSCLE_INSERT_TABLE(muscle_insert_rule_number_table, rule_number_t) | |
a49aecd5 | 294 | GENERATE_MUSCLE_INSERT_TABLE(muscle_insert_symbol_number_table, symbol_number_t) |
5372019f | 295 | GENERATE_MUSCLE_INSERT_TABLE(muscle_insert_item_number_table, item_number_t) |
d57650a5 | 296 | GENERATE_MUSCLE_INSERT_TABLE(muscle_insert_state_number_table, state_number_t) |
c3e23647 RS |
297 | |
298 | ||
b0940840 AD |
299 | /*-----------------------------------------------------------------. |
300 | | Prepare the muscles related to the tokens: translate, tname, and | | |
301 | | toknum. | | |
302 | `-----------------------------------------------------------------*/ | |
303 | ||
4a120d45 | 304 | static void |
b0940840 | 305 | prepare_tokens (void) |
c3e23647 | 306 | { |
a49aecd5 | 307 | muscle_insert_symbol_number_table ("translate", |
5372019f AD |
308 | token_translations, |
309 | 0, 1, max_user_token_number + 1); | |
c3e23647 | 310 | |
b0940840 AD |
311 | { |
312 | int i; | |
313 | int j = 0; | |
314 | for (i = 0; i < nsyms; i++) | |
315 | { | |
6b98e4b5 AD |
316 | /* Be sure not to use twice the same QUOTEARG slot: |
317 | SYMBOL_TAG_GET uses slot 0. */ | |
b0940840 AD |
318 | const char *cp = |
319 | quotearg_n_style (1, c_quoting_style, | |
97650f4e | 320 | symbols[i]->tag); |
b0940840 AD |
321 | /* Width of the next token, including the two quotes, the coma |
322 | and the space. */ | |
323 | int strsize = strlen (cp) + 2; | |
324 | ||
325 | if (j + strsize > 75) | |
326 | { | |
327 | obstack_sgrow (&format_obstack, "\n "); | |
328 | j = 2; | |
329 | } | |
330 | ||
331 | obstack_sgrow (&format_obstack, cp); | |
332 | obstack_sgrow (&format_obstack, ", "); | |
333 | j += strsize; | |
334 | } | |
335 | /* Add a NULL entry to list of tokens (well, 0, as NULL might not be | |
336 | defined). */ | |
337 | obstack_sgrow (&format_obstack, "0"); | |
c3e23647 | 338 | |
b0940840 AD |
339 | /* Finish table and store. */ |
340 | obstack_1grow (&format_obstack, 0); | |
341 | muscle_insert ("tname", obstack_finish (&format_obstack)); | |
342 | } | |
343 | ||
12b0043a | 344 | /* Output YYTOKNUM. */ |
b2ed6e58 AD |
345 | { |
346 | int i; | |
3650b4b8 AD |
347 | int *values = XCALLOC (int, ntokens); |
348 | for (i = 0; i < ntokens; ++i) | |
b0940840 | 349 | values[i] = symbols[i]->user_token_number; |
12b0043a | 350 | muscle_insert_int_table ("toknum", values, |
3650b4b8 | 351 | values[0], 1, ntokens); |
b0940840 | 352 | free (values); |
b2ed6e58 | 353 | } |
b0940840 | 354 | } |
b2ed6e58 | 355 | |
b0940840 AD |
356 | |
357 | /*-------------------------------------------------------------. | |
358 | | Prepare the muscles related to the rules: rhs, prhs, r1, r2, | | |
676385e2 | 359 | | rline, dprec, merger | |
b0940840 AD |
360 | `-------------------------------------------------------------*/ |
361 | ||
362 | static void | |
363 | prepare_rules (void) | |
364 | { | |
9222837b | 365 | rule_number_t r; |
5df5f6d5 | 366 | unsigned int i = 0; |
62a3e4f0 | 367 | item_number_t *rhs = XMALLOC (item_number_t, nritems); |
4b3d3a8e AD |
368 | unsigned int *prhs = XMALLOC (unsigned int, nrules); |
369 | unsigned int *rline = XMALLOC (unsigned int, nrules); | |
370 | symbol_number_t *r1 = XMALLOC (symbol_number_t, nrules); | |
371 | unsigned int *r2 = XMALLOC (unsigned int, nrules); | |
372 | short *dprec = XMALLOC (short, nrules); | |
373 | short *merger = XMALLOC (short, nrules); | |
374 | ||
375 | for (r = 0; r < nrules; ++r) | |
b0940840 | 376 | { |
9222837b | 377 | item_number_t *rhsp = NULL; |
b0940840 AD |
378 | /* Index of rule R in RHS. */ |
379 | prhs[r] = i; | |
380 | /* RHS of the rule R. */ | |
381 | for (rhsp = rules[r].rhs; *rhsp >= 0; ++rhsp) | |
382 | rhs[i++] = *rhsp; | |
383 | /* LHS of the rule R. */ | |
384 | r1[r] = rules[r].lhs->number; | |
385 | /* Length of rule R's RHS. */ | |
386 | r2[r] = i - prhs[r]; | |
387 | /* Separator in RHS. */ | |
388 | rhs[i++] = -1; | |
389 | /* Line where rule was defined. */ | |
8efe435c | 390 | rline[r] = rules[r].location.first_line; |
676385e2 PH |
391 | /* Dynamic precedence (GLR) */ |
392 | dprec[r] = rules[r].dprec; | |
393 | /* Merger-function index (GLR) */ | |
394 | merger[r] = rules[r].merger; | |
b0940840 AD |
395 | } |
396 | assert (i == nritems); | |
397 | ||
5372019f | 398 | muscle_insert_item_number_table ("rhs", rhs, ritem[0], 1, nritems); |
4b3d3a8e AD |
399 | muscle_insert_unsigned_int_table ("prhs", prhs, 0, 0, nrules); |
400 | muscle_insert_unsigned_int_table ("rline", rline, 0, 0, nrules); | |
401 | muscle_insert_symbol_number_table ("r1", r1, 0, 0, nrules); | |
402 | muscle_insert_unsigned_int_table ("r2", r2, 0, 0, nrules); | |
403 | muscle_insert_short_table ("dprec", dprec, 0, 0, nrules); | |
404 | muscle_insert_short_table ("merger", merger, 0, 0, nrules); | |
796d61fb | 405 | |
b0940840 AD |
406 | free (rhs); |
407 | free (prhs); | |
5df5f6d5 AD |
408 | free (rline); |
409 | free (r1); | |
b0940840 | 410 | free (r2); |
676385e2 PH |
411 | free (dprec); |
412 | free (merger); | |
c3e23647 RS |
413 | } |
414 | ||
b0940840 AD |
415 | /*--------------------------------------------. |
416 | | Prepare the muscles related to the states. | | |
417 | `--------------------------------------------*/ | |
c3e23647 | 418 | |
4a120d45 | 419 | static void |
b0940840 | 420 | prepare_states (void) |
c3e23647 | 421 | { |
d57650a5 | 422 | state_number_t i; |
a49aecd5 AD |
423 | symbol_number_t *values = |
424 | (symbol_number_t *) alloca (sizeof (symbol_number_t) * nstates); | |
9703cc49 | 425 | for (i = 0; i < nstates; ++i) |
29e88316 | 426 | values[i] = states[i]->accessing_symbol; |
a49aecd5 | 427 | muscle_insert_symbol_number_table ("stos", values, |
d57650a5 | 428 | 0, 1, nstates); |
c3e23647 RS |
429 | } |
430 | ||
431 | ||
676385e2 PH |
432 | /*-------------------------------------------------------------------. |
433 | | For GLR parsers, for each conflicted token in STATE, as indicated | | |
12b0043a | 434 | | by non-zero entries in CONFLROW, create a list of possible | |
676385e2 PH |
435 | | reductions that are alternatives to the shift or reduction | |
436 | | currently recorded for that token in STATE. Store the alternative | | |
4b3d3a8e AD |
437 | | reductions followed by a 0 in CONFLICT_LIST, updating | |
438 | | CONFLICT_LIST_CNT, and storing an index to the start of the list | | |
12b0043a | 439 | | back into CONFLROW. | |
676385e2 PH |
440 | `-------------------------------------------------------------------*/ |
441 | ||
442 | static void | |
443 | conflict_row (state_t *state) | |
444 | { | |
445 | int i, j; | |
446 | ||
447 | if (! glr_parser) | |
448 | return; | |
449 | ||
e0e5bf84 AD |
450 | for (j = 0; j < ntokens; j += 1) |
451 | if (conflrow[j]) | |
676385e2 PH |
452 | { |
453 | conflrow[j] = conflict_list_cnt; | |
454 | ||
12b0043a AD |
455 | /* Find all reductions for token J, and record all that do not |
456 | match ACTROW[J]. */ | |
676385e2 PH |
457 | for (i = 0; i < state->nlookaheads; i += 1) |
458 | if (bitset_test (state->lookaheads[i], j) | |
12b0043a AD |
459 | && (actrow[j] |
460 | != rule_number_as_item_number (state->lookaheads_rule[i]->number))) | |
e0e5bf84 | 461 | { |
676385e2 | 462 | assert (conflict_list_free > 0); |
e0e5bf84 | 463 | conflict_list[conflict_list_cnt] |
4b3d3a8e | 464 | = state->lookaheads_rule[i]->number + 1; |
676385e2 PH |
465 | conflict_list_cnt += 1; |
466 | conflict_list_free -= 1; | |
467 | } | |
e0e5bf84 | 468 | |
12b0043a | 469 | /* Leave a 0 at the end. */ |
676385e2 PH |
470 | assert (conflict_list_free > 0); |
471 | conflict_list_cnt += 1; | |
472 | conflict_list_free -= 1; | |
473 | } | |
474 | } | |
475 | ||
476 | ||
6c89f1c1 AD |
477 | /*------------------------------------------------------------------. |
478 | | Decide what to do for each type of token if seen as the lookahead | | |
479 | | token in specified state. The value returned is used as the | | |
12b0043a | 480 | | default action (yydefact) for the state. In addition, ACTROW is | |
6c89f1c1 AD |
481 | | filled with what to do for each kind of token, index by symbol | |
482 | | number, with zero meaning do the default action. The value | | |
12b0043a | 483 | | ACTION_MIN, a very negative number, means this situation is an | |
6c89f1c1 AD |
484 | | error. The parser recognizes this value specially. | |
485 | | | | |
486 | | This is where conflicts are resolved. The loop over lookahead | | |
487 | | rules considered lower-numbered rules last, and the last rule | | |
488 | | considered that likes a token gets to handle it. | | |
12b0043a AD |
489 | | | |
490 | | For GLR parsers, also sets CONFLROW[SYM] to an index into | | |
4b3d3a8e | 491 | | CONFLICT_LIST iff there is an unresolved conflict (s/r or r/r) | |
676385e2 | 492 | | with symbol SYM. The default reduction is not used for a symbol | |
12b0043a | 493 | | that has any such conflicts. | |
6c89f1c1 | 494 | `------------------------------------------------------------------*/ |
c3e23647 | 495 | |
12b0043a | 496 | static rule_number_t |
f9507c28 | 497 | action_row (state_t *state) |
c3e23647 | 498 | { |
6c89f1c1 | 499 | int i; |
4b3d3a8e | 500 | rule_number_t default_rule = -1; |
8a731ca8 | 501 | reductions_t *redp = state->reductions; |
8b752b00 | 502 | transitions_t *transitions = state->transitions; |
8a731ca8 | 503 | errs_t *errp = state->errs; |
837491d8 AD |
504 | /* set nonzero to inhibit having any default reduction */ |
505 | int nodefault = 0; | |
676385e2 | 506 | int conflicted = 0; |
c3e23647 RS |
507 | |
508 | for (i = 0; i < ntokens; i++) | |
676385e2 | 509 | actrow[i] = conflrow[i] = 0; |
c3e23647 | 510 | |
d2576365 | 511 | if (redp->num >= 1) |
c3e23647 | 512 | { |
837491d8 | 513 | int j; |
613f5e1a | 514 | bitset_iterator biter; |
837491d8 AD |
515 | /* loop over all the rules available here which require |
516 | lookahead */ | |
f9507c28 | 517 | for (i = state->nlookaheads - 1; i >= 0; --i) |
837491d8 AD |
518 | /* and find each token which the rule finds acceptable |
519 | to come next */ | |
613f5e1a | 520 | BITSET_FOR_EACH (biter, state->lookaheads[i], j, 0) |
574fb2d5 | 521 | { |
837491d8 AD |
522 | /* and record this rule as the rule to use if that |
523 | token follows. */ | |
574fb2d5 AD |
524 | if (actrow[j] != 0) |
525 | conflicted = conflrow[j] = 1; | |
12b0043a | 526 | actrow[j] = rule_number_as_item_number (state->lookaheads_rule[i]->number); |
613f5e1a | 527 | } |
c3e23647 RS |
528 | } |
529 | ||
36281465 AD |
530 | /* Now see which tokens are allowed for shifts in this state. For |
531 | them, record the shift as the thing to do. So shift is preferred | |
532 | to reduce. */ | |
ccaf65bc AD |
533 | for (i = 0; i < transitions->num && TRANSITION_IS_SHIFT (transitions, i); i++) |
534 | if (!TRANSITION_IS_DISABLED (transitions, i)) | |
574fb2d5 | 535 | { |
ccaf65bc AD |
536 | symbol_number_t symbol = TRANSITION_SYMBOL (transitions, i); |
537 | state_number_t shift_state = transitions->states[i]; | |
c3e23647 | 538 | |
574fb2d5 AD |
539 | if (actrow[symbol] != 0) |
540 | conflicted = conflrow[symbol] = 1; | |
541 | actrow[symbol] = state_number_as_int (shift_state); | |
c3e23647 | 542 | |
574fb2d5 AD |
543 | /* Do not use any default reduction if there is a shift for |
544 | error */ | |
545 | if (symbol == errtoken->number) | |
546 | nodefault = 1; | |
547 | } | |
c3e23647 | 548 | |
36281465 | 549 | /* See which tokens are an explicit error in this state (due to |
12b0043a | 550 | %nonassoc). For them, record ACTION_MIN as the action. */ |
d2576365 | 551 | for (i = 0; i < errp->num; i++) |
2cec70b9 | 552 | { |
d2576365 | 553 | symbol_number_t symbol = errp->symbols[i]; |
12b0043a | 554 | actrow[symbol] = ACTION_MIN; |
2cec70b9 | 555 | } |
c3e23647 | 556 | |
36281465 AD |
557 | /* Now find the most common reduction and make it the default action |
558 | for this state. */ | |
c3e23647 | 559 | |
d2576365 | 560 | if (redp->num >= 1 && !nodefault) |
c3e23647 | 561 | { |
f9507c28 | 562 | if (state->consistent) |
c3e23647 RS |
563 | default_rule = redp->rules[0]; |
564 | else | |
565 | { | |
7a5350ba | 566 | int max = 0; |
f9507c28 | 567 | for (i = 0; i < state->nlookaheads; i++) |
c3e23647 | 568 | { |
7a5350ba | 569 | int count = 0; |
53d4308d | 570 | rule_number_t rule = state->lookaheads_rule[i]->number; |
9222837b | 571 | symbol_number_t j; |
9ee3c97b | 572 | |
c3e23647 | 573 | for (j = 0; j < ntokens; j++) |
12b0043a | 574 | if (actrow[j] == rule_number_as_item_number (rule)) |
837491d8 | 575 | count++; |
9ee3c97b | 576 | |
c3e23647 RS |
577 | if (count > max) |
578 | { | |
579 | max = count; | |
580 | default_rule = rule; | |
581 | } | |
582 | } | |
9ee3c97b | 583 | |
676385e2 PH |
584 | /* GLR parsers need space for conflict lists, so we can't |
585 | default conflicted entries. For non-conflicted entries | |
e0e5bf84 | 586 | or as long as we are not building a GLR parser, |
676385e2 PH |
587 | actions that match the default are replaced with zero, |
588 | which means "use the default". */ | |
9ee3c97b | 589 | |
c3e23647 RS |
590 | if (max > 0) |
591 | { | |
837491d8 | 592 | int j; |
c3e23647 | 593 | for (j = 0; j < ntokens; j++) |
12b0043a | 594 | if (actrow[j] == rule_number_as_item_number (default_rule) |
53d4308d | 595 | && ! (glr_parser && conflrow[j])) |
837491d8 | 596 | actrow[j] = 0; |
c3e23647 RS |
597 | } |
598 | } | |
599 | } | |
600 | ||
601 | /* If have no default rule, the default is an error. | |
602 | So replace any action which says "error" with "use default". */ | |
603 | ||
4b3d3a8e | 604 | if (default_rule == -1) |
837491d8 | 605 | for (i = 0; i < ntokens; i++) |
12b0043a | 606 | if (actrow[i] == ACTION_MIN) |
837491d8 | 607 | actrow[i] = 0; |
c3e23647 | 608 | |
676385e2 PH |
609 | if (conflicted) |
610 | conflict_row (state); | |
611 | ||
36281465 | 612 | return default_rule; |
c3e23647 RS |
613 | } |
614 | ||
615 | ||
12b0043a AD |
616 | /*--------------------------------------------. |
617 | | Set FROMS, TOS, TALLY and WIDTH for STATE. | | |
618 | `--------------------------------------------*/ | |
619 | ||
4a120d45 | 620 | static void |
d57650a5 | 621 | save_row (state_number_t state) |
c3e23647 | 622 | { |
d57650a5 | 623 | symbol_number_t i; |
6c89f1c1 | 624 | int count; |
12b0043a AD |
625 | base_t *sp = NULL; |
626 | base_t *sp1 = NULL; | |
627 | base_t *sp2 = NULL; | |
e0e5bf84 | 628 | unsigned int *sp3 = NULL; |
c3e23647 | 629 | |
12b0043a | 630 | /* Number of non default actions in STATE. */ |
c3e23647 RS |
631 | count = 0; |
632 | for (i = 0; i < ntokens; i++) | |
796d61fb AD |
633 | if (actrow[i] != 0) |
634 | count++; | |
c3e23647 RS |
635 | |
636 | if (count == 0) | |
637 | return; | |
638 | ||
12b0043a AD |
639 | /* Allocate non defaulted actions. */ |
640 | froms[state] = sp1 = sp = XCALLOC (base_t, count); | |
641 | tos[state] = sp2 = XCALLOC (base_t, count); | |
676385e2 | 642 | if (glr_parser) |
e0e5bf84 AD |
643 | conflict_tos[state] = sp3 = XCALLOC (unsigned int, count); |
644 | else | |
676385e2 | 645 | conflict_tos[state] = NULL; |
c3e23647 | 646 | |
12b0043a | 647 | /* Store non defaulted actions. */ |
c3e23647 | 648 | for (i = 0; i < ntokens; i++) |
796d61fb AD |
649 | if (actrow[i] != 0) |
650 | { | |
651 | *sp1++ = i; | |
652 | *sp2++ = actrow[i]; | |
676385e2 PH |
653 | if (glr_parser) |
654 | *sp3++ = conflrow[i]; | |
796d61fb | 655 | } |
c3e23647 RS |
656 | |
657 | tally[state] = count; | |
658 | width[state] = sp1[-1] - sp[0] + 1; | |
659 | } | |
660 | ||
661 | ||
6c89f1c1 AD |
662 | /*------------------------------------------------------------------. |
663 | | Figure out the actions for the specified state, indexed by | | |
664 | | lookahead token type. | | |
665 | | | | |
f2acea59 AD |
666 | | The YYDEFACT table is output now. The detailed info is saved for | |
667 | | putting into YYTABLE later. | | |
6c89f1c1 | 668 | `------------------------------------------------------------------*/ |
c3e23647 | 669 | |
4a120d45 | 670 | static void |
6c89f1c1 | 671 | token_actions (void) |
c3e23647 | 672 | { |
d57650a5 | 673 | state_number_t i; |
676385e2 PH |
674 | int nconflict = conflicts_total_count (); |
675 | ||
12b0043a | 676 | rule_number_t *yydefact = XCALLOC (rule_number_t, nstates); |
c3e23647 | 677 | |
12b0043a AD |
678 | actrow = XCALLOC (action_t, ntokens); |
679 | conflrow = XCALLOC (unsigned int, ntokens); | |
676385e2 | 680 | |
676385e2 PH |
681 | if (glr_parser) |
682 | { | |
683 | conflict_list = XCALLOC (unsigned int, 1 + 2 * nconflict); | |
684 | conflict_list_free = 2 * nconflict; | |
685 | conflict_list_cnt = 1; | |
e0e5bf84 AD |
686 | } |
687 | else | |
676385e2 PH |
688 | conflict_list_free = conflict_list_cnt = 0; |
689 | ||
f2acea59 | 690 | for (i = 0; i < nstates; ++i) |
c3e23647 | 691 | { |
4b3d3a8e | 692 | yydefact[i] = action_row (states[i]) + 1; |
6c89f1c1 | 693 | save_row (i); |
c3e23647 | 694 | } |
bbb5bcc6 | 695 | |
12b0043a AD |
696 | muscle_insert_rule_number_table ("defact", yydefact, |
697 | yydefact[0], 1, nstates); | |
26f609ff | 698 | XFREE (actrow); |
676385e2 | 699 | XFREE (conflrow); |
d7913476 | 700 | XFREE (yydefact); |
c3e23647 RS |
701 | } |
702 | ||
703 | ||
3f96f4dc AD |
704 | /*-----------------------------. |
705 | | Output the actions to OOUT. | | |
706 | `-----------------------------*/ | |
707 | ||
9b3add5b | 708 | void |
be2a1a68 | 709 | actions_output (FILE *out) |
3f96f4dc | 710 | { |
9222837b | 711 | rule_number_t r; |
dafdc66f AD |
712 | |
713 | fputs ("m4_define([b4_actions], \n[[", out); | |
4b3d3a8e | 714 | for (r = 0; r < nrules; ++r) |
9222837b | 715 | if (rules[r].action) |
3f96f4dc | 716 | { |
4b3d3a8e | 717 | fprintf (out, " case %d:\n", r + 1); |
3f96f4dc AD |
718 | |
719 | if (!no_lines_flag) | |
ea52d706 | 720 | fprintf (out, muscle_find ("linef"), |
9222837b | 721 | rules[r].action_location.first_line, |
ea52d706 AD |
722 | quotearg_style (c_quoting_style, |
723 | muscle_find ("filename"))); | |
e9955c83 | 724 | fprintf (out, " %s\n break;\n\n", |
9222837b | 725 | rules[r].action); |
3f96f4dc | 726 | } |
dafdc66f | 727 | fputs ("]])\n\n", out); |
3f96f4dc AD |
728 | } |
729 | ||
676385e2 PH |
730 | /*--------------------------------------. |
731 | | Output the merge functions to OUT. | | |
732 | `--------------------------------------*/ | |
733 | ||
41442480 | 734 | static void |
676385e2 PH |
735 | merger_output (FILE *out) |
736 | { | |
737 | int n; | |
738 | merger_list* p; | |
739 | ||
740 | fputs ("m4_define([b4_mergers], \n[[", out); | |
e0e5bf84 | 741 | for (n = 1, p = merge_functions; p != NULL; n += 1, p = p->next) |
676385e2 | 742 | { |
e0e5bf84 | 743 | if (p->type[0] == '\0') |
676385e2 PH |
744 | fprintf (out, " case %d: yyval = %s (*yy0, *yy1); break;\n", |
745 | n, p->name); | |
746 | else | |
747 | fprintf (out, " case %d: yyval.%s = %s (*yy0, *yy1); break;\n", | |
748 | n, p->type, p->name); | |
749 | } | |
750 | fputs ("]])\n\n", out); | |
751 | } | |
3f96f4dc | 752 | |
091e20bb AD |
753 | /*---------------------------------------. |
754 | | Output the tokens definition to OOUT. | | |
755 | `---------------------------------------*/ | |
756 | ||
9b3add5b | 757 | void |
be2a1a68 | 758 | token_definitions_output (FILE *out) |
091e20bb AD |
759 | { |
760 | int i; | |
0d8bed56 | 761 | int first = 1; |
dafdc66f AD |
762 | |
763 | fputs ("m4_define([b4_tokens], \n[", out); | |
091e20bb AD |
764 | for (i = 0; i < ntokens; ++i) |
765 | { | |
db8837cb | 766 | symbol_t *symbol = symbols[i]; |
091e20bb AD |
767 | int number = symbol->user_token_number; |
768 | ||
b87f8b21 AD |
769 | /* At this stage, if there are literal aliases, they are part of |
770 | SYMBOLS, so we should not find symbols which are the aliases | |
771 | here. */ | |
772 | assert (number != USER_NUMBER_ALIAS); | |
773 | ||
091e20bb | 774 | /* Skip error token. */ |
007a50a4 | 775 | if (symbol == errtoken) |
091e20bb | 776 | continue; |
b87f8b21 AD |
777 | |
778 | /* If this string has an alias, then it is necessarily the alias | |
779 | which is to be output. */ | |
780 | if (symbol->alias) | |
781 | symbol = symbol->alias; | |
782 | ||
783 | /* Don't output literal chars or strings (when defined only as a | |
784 | string). Note that must be done after the alias resolution: | |
785 | think about `%token 'f' "f"'. */ | |
786 | if (symbol->tag[0] == '\'' || symbol->tag[0] == '\"') | |
787 | continue; | |
091e20bb AD |
788 | |
789 | /* Don't #define nonliteral tokens whose names contain periods | |
790 | or '$' (as does the default value of the EOF token). */ | |
791 | if (strchr (symbol->tag, '.') || strchr (symbol->tag, '$')) | |
792 | continue; | |
793 | ||
83ccf991 | 794 | fprintf (out, "%s[[[%s]], [%d]]", |
0d8bed56 | 795 | first ? "" : ",\n", symbol->tag, number); |
b87f8b21 | 796 | |
0d8bed56 | 797 | first = 0; |
091e20bb | 798 | } |
dafdc66f | 799 | fputs ("])\n\n", out); |
091e20bb AD |
800 | } |
801 | ||
802 | ||
9280d3ef AD |
803 | /*----------------------------------------. |
804 | | Output the symbol destructors to OOUT. | | |
805 | `----------------------------------------*/ | |
806 | ||
807 | static void | |
808 | symbol_destructors_output (FILE *out) | |
809 | { | |
810 | int i; | |
811 | int first = 1; | |
812 | ||
813 | fputs ("m4_define([b4_symbol_destructors], \n[", out); | |
814 | for (i = 0; i < nsyms; ++i) | |
815 | if (symbols[i]->destructor) | |
816 | { | |
817 | symbol_t *symbol = symbols[i]; | |
818 | ||
24c0aad7 AD |
819 | /* Filename, lineno, |
820 | Symbol-name, Symbol-number, | |
821 | destructor, typename. */ | |
822 | fprintf (out, "%s[[[%s]], [[%d]], [[%s]], [[%d]], [[%s]], [[%s]]]", | |
9280d3ef | 823 | first ? "" : ",\n", |
24c0aad7 | 824 | infile, symbol->destructor_location.first_line, |
97650f4e | 825 | symbol->tag, |
24c0aad7 AD |
826 | symbol->number, |
827 | symbol->destructor, | |
828 | symbol->type_name); | |
9280d3ef AD |
829 | |
830 | first = 0; | |
831 | } | |
832 | fputs ("])\n\n", out); | |
833 | } | |
834 | ||
835 | ||
366eea36 AD |
836 | /*-------------------------------------. |
837 | | Output the symbol printers to OOUT. | | |
838 | `-------------------------------------*/ | |
839 | ||
840 | static void | |
841 | symbol_printers_output (FILE *out) | |
842 | { | |
843 | int i; | |
844 | int first = 1; | |
845 | ||
846 | fputs ("m4_define([b4_symbol_printers], \n[", out); | |
847 | for (i = 0; i < nsyms; ++i) | |
848 | if (symbols[i]->destructor) | |
849 | { | |
850 | symbol_t *symbol = symbols[i]; | |
851 | ||
852 | /* Filename, lineno, | |
853 | Symbol-name, Symbol-number, | |
854 | destructor, typename. */ | |
855 | fprintf (out, "%s[[[%s]], [[%d]], [[%s]], [[%d]], [[%s]], [[%s]]]", | |
856 | first ? "" : ",\n", | |
857 | infile, symbol->printer_location.first_line, | |
97650f4e | 858 | symbol->tag, |
366eea36 AD |
859 | symbol->number, |
860 | symbol->printer, | |
861 | symbol->type_name); | |
862 | ||
863 | first = 0; | |
864 | } | |
865 | fputs ("])\n\n", out); | |
866 | } | |
867 | ||
868 | ||
12b0043a AD |
869 | /*------------------------------------------------------------------. |
870 | | Compute FROMS[VECTOR], TOS[VECTOR], TALLY[VECTOR], WIDTH[VECTOR], | | |
871 | | i.e., the information related to non defaulted GOTO on the nterm | | |
872 | | SYMBOL. | | |
873 | | | | |
874 | | DEFAULT_STATE is the principal destination on SYMBOL, i.e., the | | |
875 | | default GOTO destination on SYMBOL. | | |
876 | `------------------------------------------------------------------*/ | |
877 | ||
4a120d45 | 878 | static void |
d57650a5 | 879 | save_column (symbol_number_t symbol, state_number_t default_state) |
c3e23647 | 880 | { |
6c89f1c1 | 881 | int i; |
12b0043a AD |
882 | base_t *sp; |
883 | base_t *sp1; | |
884 | base_t *sp2; | |
6c89f1c1 | 885 | int count; |
12b0043a | 886 | vector_number_t symno = symbol_number_to_vector_number (symbol); |
c3e23647 | 887 | |
7db2ed2d AD |
888 | goto_number_t begin = goto_map[symbol]; |
889 | goto_number_t end = goto_map[symbol + 1]; | |
c3e23647 | 890 | |
12b0043a | 891 | /* Number of non default GOTO. */ |
c3e23647 | 892 | count = 0; |
43591cec | 893 | for (i = begin; i < end; i++) |
796d61fb AD |
894 | if (to_state[i] != default_state) |
895 | count++; | |
c3e23647 RS |
896 | |
897 | if (count == 0) | |
898 | return; | |
899 | ||
12b0043a AD |
900 | /* Allocate room for non defaulted gotos. */ |
901 | froms[symno] = sp1 = sp = XCALLOC (base_t, count); | |
902 | tos[symno] = sp2 = XCALLOC (base_t, count); | |
c3e23647 | 903 | |
12b0043a | 904 | /* Store the state numbers of the non defaulted gotos. */ |
43591cec | 905 | for (i = begin; i < end; i++) |
796d61fb AD |
906 | if (to_state[i] != default_state) |
907 | { | |
908 | *sp1++ = from_state[i]; | |
909 | *sp2++ = to_state[i]; | |
910 | } | |
c3e23647 RS |
911 | |
912 | tally[symno] = count; | |
913 | width[symno] = sp1[-1] - sp[0] + 1; | |
914 | } | |
915 | ||
d57650a5 | 916 | |
12b0043a AD |
917 | /*----------------------------------------------------------------. |
918 | | Return `the' most common destination GOTO on SYMBOL (a nterm). | | |
919 | `----------------------------------------------------------------*/ | |
920 | ||
d57650a5 | 921 | static state_number_t |
12b0043a | 922 | default_goto (symbol_number_t symbol, short state_count[]) |
6c89f1c1 | 923 | { |
d57650a5 AD |
924 | state_number_t s; |
925 | int i; | |
7db2ed2d AD |
926 | goto_number_t m = goto_map[symbol]; |
927 | goto_number_t n = goto_map[symbol + 1]; | |
d57650a5 | 928 | state_number_t default_state = (state_number_t) -1; |
837491d8 | 929 | int max = 0; |
6c89f1c1 AD |
930 | |
931 | if (m == n) | |
d57650a5 | 932 | return (state_number_t) -1; |
6c89f1c1 | 933 | |
d57650a5 AD |
934 | for (s = 0; s < nstates; s++) |
935 | state_count[s] = 0; | |
6c89f1c1 AD |
936 | |
937 | for (i = m; i < n; i++) | |
938 | state_count[to_state[i]]++; | |
939 | ||
d57650a5 AD |
940 | for (s = 0; s < nstates; s++) |
941 | if (state_count[s] > max) | |
796d61fb | 942 | { |
d57650a5 AD |
943 | max = state_count[s]; |
944 | default_state = s; | |
796d61fb | 945 | } |
6c89f1c1 AD |
946 | |
947 | return default_state; | |
948 | } | |
949 | ||
950 | ||
951 | /*-------------------------------------------------------------------. | |
952 | | Figure out what to do after reducing with each rule, depending on | | |
953 | | the saved state from before the beginning of parsing the data that | | |
954 | | matched this rule. | | |
955 | | | | |
956 | | The YYDEFGOTO table is output now. The detailed info is saved for | | |
957 | | putting into YYTABLE later. | | |
958 | `-------------------------------------------------------------------*/ | |
959 | ||
960 | static void | |
961 | goto_actions (void) | |
962 | { | |
d57650a5 | 963 | symbol_number_t i; |
12b0043a | 964 | state_number_t *yydefgoto = XMALLOC (state_number_t, nvars); |
bbb5bcc6 | 965 | |
12b0043a AD |
966 | /* For a given nterm I, STATE_COUNT[S] is the number of times there |
967 | is a GOTO to S on I. */ | |
968 | short *state_count = XCALLOC (short, nstates); | |
43591cec | 969 | for (i = ntokens; i < nsyms; ++i) |
6c89f1c1 | 970 | { |
12b0043a | 971 | state_number_t default_state = default_goto (i, state_count); |
43591cec AD |
972 | save_column (i, default_state); |
973 | yydefgoto[i - ntokens] = default_state; | |
6c89f1c1 AD |
974 | } |
975 | ||
d57650a5 AD |
976 | muscle_insert_state_number_table ("defgoto", yydefgoto, |
977 | yydefgoto[0], 1, nsyms - ntokens); | |
d7913476 | 978 | XFREE (state_count); |
43591cec | 979 | XFREE (yydefgoto); |
6c89f1c1 | 980 | } |
c3e23647 RS |
981 | |
982 | ||
12b0043a AD |
983 | /*------------------------------------------------------------------. |
984 | | Compute ORDER, a reordering of vectors, in order to decide how to | | |
985 | | pack the actions and gotos information into yytable. | | |
986 | `------------------------------------------------------------------*/ | |
c3e23647 | 987 | |
4a120d45 | 988 | static void |
d2729d44 | 989 | sort_actions (void) |
c3e23647 | 990 | { |
6c89f1c1 | 991 | int i; |
c3e23647 | 992 | |
c3e23647 RS |
993 | nentries = 0; |
994 | ||
995 | for (i = 0; i < nvectors; i++) | |
796d61fb AD |
996 | if (tally[i] > 0) |
997 | { | |
837491d8 AD |
998 | int k; |
999 | int t = tally[i]; | |
1000 | int w = width[i]; | |
1001 | int j = nentries - 1; | |
c3e23647 | 1002 | |
796d61fb AD |
1003 | while (j >= 0 && (width[order[j]] < w)) |
1004 | j--; | |
c3e23647 | 1005 | |
796d61fb AD |
1006 | while (j >= 0 && (width[order[j]] == w) && (tally[order[j]] < t)) |
1007 | j--; | |
c3e23647 | 1008 | |
796d61fb AD |
1009 | for (k = nentries - 1; k > j; k--) |
1010 | order[k + 1] = order[k]; | |
c3e23647 | 1011 | |
796d61fb AD |
1012 | order[j + 1] = i; |
1013 | nentries++; | |
1014 | } | |
c3e23647 RS |
1015 | } |
1016 | ||
1017 | ||
12b0043a AD |
1018 | /* If VECTOR is a state which actions (reflected by FROMS, TOS, TALLY |
1019 | and WIDTH of VECTOR) are common to a previous state, return this | |
1020 | state number. | |
1021 | ||
1022 | In any other case, return -1. */ | |
1023 | ||
1024 | static state_number_t | |
1025 | matching_state (vector_number_t vector) | |
c3e23647 | 1026 | { |
12b0043a | 1027 | vector_number_t i = order[vector]; |
6c89f1c1 AD |
1028 | int t; |
1029 | int w; | |
6c89f1c1 | 1030 | int prev; |
c3e23647 | 1031 | |
12b0043a | 1032 | /* If VECTOR is a nterm, return -1. */ |
602bbf31 | 1033 | if (i >= (int) nstates) |
36281465 | 1034 | return -1; |
c3e23647 RS |
1035 | |
1036 | t = tally[i]; | |
1037 | w = width[i]; | |
1038 | ||
1039 | for (prev = vector - 1; prev >= 0; prev--) | |
1040 | { | |
12b0043a | 1041 | vector_number_t j = order[prev]; |
837491d8 AD |
1042 | int k; |
1043 | int match = 1; | |
1044 | ||
12b0043a AD |
1045 | /* Given how ORDER was computed, if the WIDTH or TALLY is |
1046 | different, there cannot be a matching state. */ | |
c3e23647 | 1047 | if (width[j] != w || tally[j] != t) |
36281465 | 1048 | return -1; |
c3e23647 | 1049 | |
c3e23647 | 1050 | for (k = 0; match && k < t; k++) |
796d61fb AD |
1051 | if (tos[j][k] != tos[i][k] || froms[j][k] != froms[i][k]) |
1052 | match = 0; | |
c3e23647 RS |
1053 | |
1054 | if (match) | |
36281465 | 1055 | return j; |
c3e23647 RS |
1056 | } |
1057 | ||
36281465 | 1058 | return -1; |
c3e23647 RS |
1059 | } |
1060 | ||
1061 | ||
12b0043a AD |
1062 | static base_t |
1063 | pack_vector (vector_number_t vector) | |
c3e23647 | 1064 | { |
12b0043a | 1065 | vector_number_t i = order[vector]; |
6c89f1c1 | 1066 | int j; |
837491d8 | 1067 | int t = tally[i]; |
6c89f1c1 | 1068 | int loc = 0; |
12b0043a AD |
1069 | base_t *from = froms[i]; |
1070 | base_t *to = tos[i]; | |
676385e2 | 1071 | unsigned int *conflict_to = conflict_tos[i]; |
c3e23647 | 1072 | |
340ef489 | 1073 | assert (t); |
c3e23647 | 1074 | |
133c20e2 | 1075 | for (j = lowzero - from[0]; j < (int) table_size; j++) |
c3e23647 | 1076 | { |
837491d8 AD |
1077 | int k; |
1078 | int ok = 1; | |
c3e23647 RS |
1079 | |
1080 | for (k = 0; ok && k < t; k++) | |
1081 | { | |
d57650a5 | 1082 | loc = j + state_number_as_int (from[k]); |
133c20e2 AD |
1083 | if (loc > (int) table_size) |
1084 | table_grow (loc); | |
c3e23647 RS |
1085 | |
1086 | if (table[loc] != 0) | |
1087 | ok = 0; | |
1088 | } | |
1089 | ||
1090 | for (k = 0; ok && k < vector; k++) | |
796d61fb AD |
1091 | if (pos[k] == j) |
1092 | ok = 0; | |
c3e23647 RS |
1093 | |
1094 | if (ok) | |
1095 | { | |
1096 | for (k = 0; k < t; k++) | |
1097 | { | |
12b0043a AD |
1098 | loc = j + from[k]; |
1099 | table[loc] = to[k]; | |
676385e2 PH |
1100 | if (glr_parser && conflict_to != NULL) |
1101 | conflict_table[loc] = conflict_to[k]; | |
12b0043a | 1102 | check[loc] = from[k]; |
c3e23647 RS |
1103 | } |
1104 | ||
1105 | while (table[lowzero] != 0) | |
1106 | lowzero++; | |
1107 | ||
1108 | if (loc > high) | |
1109 | high = loc; | |
1110 | ||
12b0043a AD |
1111 | if (j < BASE_MIN || BASE_MAX < j) |
1112 | fatal ("base_t too small to hold %d\n", j); | |
36281465 | 1113 | return j; |
c3e23647 RS |
1114 | } |
1115 | } | |
275fc3ad AD |
1116 | #define pack_vector_succeeded 0 |
1117 | assert (pack_vector_succeeded); | |
3843c413 | 1118 | return 0; |
c3e23647 RS |
1119 | } |
1120 | ||
1121 | ||
12b0043a AD |
1122 | /*-------------------------------------------------------------. |
1123 | | Remap the negative infinite in TAB from NINF to the greatest | | |
1124 | | possible smallest value. Return it. | | |
1125 | | | | |
1126 | | In most case this allows us to use shorts instead of ints in | | |
1127 | | parsers. | | |
1128 | `-------------------------------------------------------------*/ | |
1129 | ||
1130 | static base_t | |
1131 | table_ninf_remap (base_t tab[], size_t size, base_t ninf) | |
1132 | { | |
1133 | base_t res = 0; | |
1134 | size_t i; | |
1135 | ||
1136 | for (i = 0; i < size; i++) | |
1137 | if (tab[i] < res && tab[i] != ninf) | |
1138 | res = base[i]; | |
1139 | ||
1140 | --res; | |
1141 | ||
1142 | for (i = 0; i < size; i++) | |
1143 | if (tab[i] == ninf) | |
1144 | tab[i] = res; | |
1145 | ||
1146 | return res; | |
1147 | } | |
1148 | ||
6c89f1c1 AD |
1149 | static void |
1150 | pack_table (void) | |
1151 | { | |
1152 | int i; | |
6c89f1c1 | 1153 | |
12b0043a AD |
1154 | base = XCALLOC (base_t, nvectors); |
1155 | pos = XCALLOC (base_t, nentries); | |
1156 | table = XCALLOC (base_t, table_size); | |
676385e2 PH |
1157 | if (glr_parser) |
1158 | conflict_table = XCALLOC (unsigned int, table_size); | |
12b0043a | 1159 | check = XCALLOC (base_t, table_size); |
6c89f1c1 AD |
1160 | |
1161 | lowzero = 0; | |
1162 | high = 0; | |
1163 | ||
1164 | for (i = 0; i < nvectors; i++) | |
12b0043a | 1165 | base[i] = BASE_MIN; |
6c89f1c1 | 1166 | |
133c20e2 | 1167 | for (i = 0; i < (int) table_size; i++) |
6c89f1c1 AD |
1168 | check[i] = -1; |
1169 | ||
1170 | for (i = 0; i < nentries; i++) | |
1171 | { | |
12b0043a AD |
1172 | state_number_t state = matching_state (i); |
1173 | base_t place; | |
6c89f1c1 AD |
1174 | |
1175 | if (state < 0) | |
12b0043a | 1176 | /* A new set of state actions, or a nonterminal. */ |
6c89f1c1 AD |
1177 | place = pack_vector (i); |
1178 | else | |
12b0043a | 1179 | /* Action of I were already coded for STATE. */ |
6c89f1c1 AD |
1180 | place = base[state]; |
1181 | ||
1182 | pos[i] = place; | |
1183 | base[order[i]] = place; | |
1184 | } | |
1185 | ||
12b0043a AD |
1186 | /* Use the greatest possible negative infinites. */ |
1187 | base_ninf = table_ninf_remap (base, nvectors, BASE_MIN); | |
1188 | table_ninf = table_ninf_remap (table, high + 1, ACTION_MIN); | |
1189 | ||
6c89f1c1 AD |
1190 | for (i = 0; i < nvectors; i++) |
1191 | { | |
796d61fb AD |
1192 | XFREE (froms[i]); |
1193 | XFREE (tos[i]); | |
676385e2 | 1194 | XFREE (conflict_tos[i]); |
6c89f1c1 AD |
1195 | } |
1196 | ||
536545f3 AD |
1197 | free (froms); |
1198 | free (tos); | |
1199 | free (conflict_tos); | |
1200 | free (pos); | |
6c89f1c1 | 1201 | } |
c3e23647 | 1202 | |
12b0043a | 1203 | |
676385e2 | 1204 | /* the following functions output yytable, yycheck, yyconflp, yyconfl, |
12b0043a | 1205 | and the vectors whose elements index the portion starts. */ |
c3e23647 | 1206 | |
4a120d45 | 1207 | static void |
d2729d44 | 1208 | output_base (void) |
c3e23647 | 1209 | { |
12b0043a AD |
1210 | /* Output PACT. */ |
1211 | muscle_insert_base_table ("pact", base, | |
5372019f | 1212 | base[0], 1, nstates); |
12b0043a | 1213 | MUSCLE_INSERT_INT ("pact_ninf", base_ninf); |
c3e23647 | 1214 | |
12b0043a AD |
1215 | /* Output PGOTO. */ |
1216 | muscle_insert_base_table ("pgoto", base, | |
5372019f | 1217 | base[nstates], nstates + 1, nvectors); |
d7913476 | 1218 | XFREE (base); |
c3e23647 RS |
1219 | } |
1220 | ||
1221 | ||
4a120d45 | 1222 | static void |
d2729d44 | 1223 | output_table (void) |
c3e23647 | 1224 | { |
12b0043a AD |
1225 | muscle_insert_base_table ("table", table, |
1226 | table[0], 1, high + 1); | |
1227 | MUSCLE_INSERT_INT ("table_ninf", table_ninf); | |
d7913476 | 1228 | XFREE (table); |
c3e23647 RS |
1229 | } |
1230 | ||
1231 | ||
676385e2 PH |
1232 | static void |
1233 | output_conflicts (void) | |
1234 | { | |
1235 | /* GLR parsing slightly modifies yytable and yycheck | |
1236 | (and thus yypact) so that in states with unresolved conflicts, | |
1237 | the default reduction is not used in the conflicted entries, so | |
1238 | that there is a place to put a conflict pointer. This means that | |
1239 | yyconflp and yyconfl are nonsense for a non-GLR parser, so we | |
1240 | avoid accidents by not writing them out in that case. */ | |
1241 | if (! glr_parser) | |
1242 | return; | |
1243 | ||
e0e5bf84 | 1244 | muscle_insert_unsigned_int_table ("conflict_list_heads", conflict_table, |
676385e2 | 1245 | conflict_table[0], 1, high+1); |
e0e5bf84 | 1246 | muscle_insert_unsigned_int_table ("conflicting_rules", conflict_list, |
676385e2 PH |
1247 | conflict_list[0], 1, conflict_list_cnt); |
1248 | ||
1249 | XFREE (conflict_table); | |
1250 | XFREE (conflict_list); | |
1251 | } | |
1252 | ||
1253 | ||
4a120d45 | 1254 | static void |
d2729d44 | 1255 | output_check (void) |
c3e23647 | 1256 | { |
12b0043a AD |
1257 | muscle_insert_base_table ("check", check, |
1258 | check[0], 1, high + 1); | |
d7913476 | 1259 | XFREE (check); |
c3e23647 RS |
1260 | } |
1261 | ||
b0940840 AD |
1262 | /*-----------------------------------------------------------------. |
1263 | | Compute and output yydefact, yydefgoto, yypact, yypgoto, yytable | | |
1264 | | and yycheck. | | |
1265 | `-----------------------------------------------------------------*/ | |
6c89f1c1 AD |
1266 | |
1267 | static void | |
536545f3 | 1268 | prepare_actions (void) |
6c89f1c1 | 1269 | { |
d57650a5 AD |
1270 | /* That's a poor way to make sure the sizes are properly corelated, |
1271 | in particular the signedness is not taking into account, but it's | |
1272 | not useless. */ | |
1273 | assert (sizeof (nvectors) >= sizeof (nstates)); | |
1274 | assert (sizeof (nvectors) >= sizeof (nvars)); | |
1275 | ||
1276 | nvectors = state_number_as_int (nstates) + nvars; | |
c3e23647 | 1277 | |
12b0043a AD |
1278 | froms = XCALLOC (base_t *, nvectors); |
1279 | tos = XCALLOC (base_t *, nvectors); | |
676385e2 | 1280 | conflict_tos = XCALLOC (unsigned int *, nvectors); |
d7913476 | 1281 | tally = XCALLOC (short, nvectors); |
12b0043a | 1282 | width = XCALLOC (base_t, nvectors); |
6c89f1c1 AD |
1283 | |
1284 | token_actions (); | |
914feea9 | 1285 | bitsetv_free (LA); |
b0299a2e | 1286 | free (LArule); |
6c89f1c1 AD |
1287 | |
1288 | goto_actions (); | |
d7913476 AD |
1289 | XFREE (goto_map + ntokens); |
1290 | XFREE (from_state); | |
1291 | XFREE (to_state); | |
6c89f1c1 | 1292 | |
12b0043a | 1293 | order = XCALLOC (vector_number_t, nvectors); |
6c89f1c1 AD |
1294 | sort_actions (); |
1295 | pack_table (); | |
536545f3 AD |
1296 | free (order); |
1297 | ||
1298 | free (tally); | |
1299 | free (width); | |
4e5caae2 | 1300 | |
6c89f1c1 AD |
1301 | output_base (); |
1302 | output_table (); | |
676385e2 | 1303 | output_conflicts (); |
4e5caae2 | 1304 | |
6c89f1c1 AD |
1305 | output_check (); |
1306 | } | |
c3e23647 | 1307 | |
652def80 | 1308 | \f |
1239777d AD |
1309 | /*---------------------------. |
1310 | | Call the skeleton parser. | | |
1311 | `---------------------------*/ | |
c3e23647 | 1312 | |
4a120d45 | 1313 | static void |
1239777d | 1314 | output_skeleton (void) |
9b3add5b | 1315 | { |
be2a1a68 | 1316 | /* Store the definition of all the muscles. */ |
d0039cbc | 1317 | const char *tempdir = getenv ("TMPDIR"); |
381fb12e AD |
1318 | char *tempfile = NULL; |
1319 | FILE *out = NULL; | |
381fb12e AD |
1320 | int fd; |
1321 | ||
1322 | if (tempdir == NULL) | |
1323 | tempdir = DEFAULT_TMPDIR; | |
1324 | tempfile = xmalloc (strlen (tempdir) + 11); | |
1325 | sprintf (tempfile, "%s/bsnXXXXXX", tempdir); | |
1326 | fd = mkstemp (tempfile); | |
1327 | if (fd == -1) | |
1328 | error (EXIT_FAILURE, errno, "%s", tempfile); | |
1329 | ||
1330 | out = fdopen (fd, "w"); | |
1331 | if (out == NULL) | |
1332 | error (EXIT_FAILURE, errno, "%s", tempfile); | |
1333 | ||
1334 | /* There are no comments, especially not `#': we do want M4 expansion | |
1335 | after `#': think of CPP macros! */ | |
1336 | fputs ("m4_changecom()\n", out); | |
1337 | fputs ("m4_init()\n", out); | |
1338 | ||
381fb12e | 1339 | actions_output (out); |
676385e2 | 1340 | merger_output (out); |
381fb12e | 1341 | token_definitions_output (out); |
9280d3ef | 1342 | symbol_destructors_output (out); |
366eea36 | 1343 | symbol_printers_output (out); |
be2a1a68 | 1344 | |
381fb12e | 1345 | muscles_m4_output (out); |
be2a1a68 | 1346 | |
381fb12e AD |
1347 | fputs ("m4_wrap([m4_divert_pop(0)])\n", out); |
1348 | fputs ("m4_divert_push(0)dnl\n", out); | |
1349 | xfclose (out); | |
be2a1a68 | 1350 | |
f958596b AD |
1351 | m4_invoke (tempfile); |
1352 | ||
1353 | /* If `debugging', keep this file alive. */ | |
1354 | if (!trace_flag) | |
1355 | unlink (tempfile); | |
1356 | ||
1357 | free (tempfile); | |
26f609ff RA |
1358 | } |
1359 | ||
1360 | static void | |
1361 | prepare (void) | |
1362 | { | |
7db2ed2d AD |
1363 | /* Flags. */ |
1364 | MUSCLE_INSERT_INT ("locations_flag", locations_flag); | |
1365 | MUSCLE_INSERT_INT ("defines_flag", defines_flag); | |
1366 | MUSCLE_INSERT_INT ("error_verbose", error_verbose); | |
11d82f03 | 1367 | MUSCLE_INSERT_INT ("pure", pure_parser); |
11d82f03 | 1368 | MUSCLE_INSERT_INT ("debug", debug_flag); |
11d82f03 | 1369 | |
b85810ae AD |
1370 | /* FIXME: This is wrong: the muscles should decide whether they hold |
1371 | a copy or not, but the situation is too obscure currently. */ | |
7db2ed2d | 1372 | MUSCLE_INSERT_STRING ("prefix", spec_name_prefix ? spec_name_prefix : "yy"); |
be2a1a68 AD |
1373 | MUSCLE_INSERT_STRING ("output_infix", output_infix ? output_infix : ""); |
1374 | MUSCLE_INSERT_STRING ("output_prefix", short_base_name); | |
1375 | MUSCLE_INSERT_STRING ("output_parser_name", parser_file_name); | |
1376 | MUSCLE_INSERT_STRING ("output_header_name", spec_defines_file); | |
b85810ae | 1377 | |
7db2ed2d AD |
1378 | /* Symbols. */ |
1379 | MUSCLE_INSERT_INT ("tokens_number", ntokens); | |
1380 | MUSCLE_INSERT_INT ("nterms_number", nvars); | |
1381 | MUSCLE_INSERT_INT ("undef_token_number", undeftoken->number); | |
1382 | MUSCLE_INSERT_INT ("user_token_number_max", max_user_token_number); | |
11d82f03 | 1383 | |
7db2ed2d AD |
1384 | /* Rules. */ |
1385 | MUSCLE_INSERT_INT ("rules_number", nrules); | |
1386 | ||
1387 | /* States. */ | |
1388 | MUSCLE_INSERT_INT ("last", high); | |
7db2ed2d AD |
1389 | MUSCLE_INSERT_INT ("final_state_number", final_state->number); |
1390 | MUSCLE_INSERT_INT ("states_number", nstates); | |
381fb12e | 1391 | |
7db2ed2d | 1392 | /* User Code. */ |
0dd1580a RA |
1393 | obstack_1grow (&pre_prologue_obstack, 0); |
1394 | obstack_1grow (&post_prologue_obstack, 0); | |
1395 | muscle_insert ("pre_prologue", obstack_finish (&pre_prologue_obstack)); | |
1396 | muscle_insert ("post_prologue", obstack_finish (&post_prologue_obstack)); | |
381fb12e AD |
1397 | |
1398 | /* Find the right skeleton file. */ | |
1399 | if (!skeleton) | |
676385e2 PH |
1400 | { |
1401 | if (glr_parser) | |
1402 | skeleton = "glr.c"; | |
1403 | else | |
1404 | skeleton = "yacc.c"; | |
1405 | } | |
381fb12e AD |
1406 | |
1407 | /* Parse the skeleton file and output the needed parsers. */ | |
1408 | muscle_insert ("skeleton", skeleton); | |
26f609ff | 1409 | } |
c3e23647 | 1410 | |
93ede233 | 1411 | |
6c89f1c1 AD |
1412 | /*----------------------------------------------------------. |
1413 | | Output the parsing tables and the parser code to ftable. | | |
1414 | `----------------------------------------------------------*/ | |
c3e23647 | 1415 | |
6c89f1c1 AD |
1416 | void |
1417 | output (void) | |
1418 | { | |
f87685c3 | 1419 | obstack_init (&format_obstack); |
dd60faec | 1420 | |
b0940840 AD |
1421 | prepare_tokens (); |
1422 | prepare_rules (); | |
1423 | prepare_states (); | |
536545f3 | 1424 | prepare_actions (); |
342b8b6e | 1425 | |
26f609ff | 1426 | prepare (); |
652def80 | 1427 | |
9b3add5b RA |
1428 | /* Process the selected skeleton file. */ |
1429 | output_skeleton (); | |
1430 | ||
f499b062 | 1431 | obstack_free (&format_obstack, NULL); |
0dd1580a RA |
1432 | obstack_free (&pre_prologue_obstack, NULL); |
1433 | obstack_free (&post_prologue_obstack, NULL); | |
c3e23647 | 1434 | } |