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