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