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