]>
Commit | Line | Data |
---|---|---|
c3e23647 | 1 | /* Output the generated parsing program for bison, |
fabd3b43 | 2 | Copyright 1984, 1986, 1989, 1992, 2000, 2001 Free Software Foundation, Inc. |
c3e23647 | 3 | |
9ee3c97b | 4 | This file is part of Bison, the GNU Compiler Compiler. |
c3e23647 | 5 | |
9ee3c97b AD |
6 | Bison is free software; you can redistribute it and/or modify it |
7 | under the terms of the GNU General Public License as published by | |
8 | the Free Software Foundation; either version 2, or (at your option) | |
9 | any later version. | |
c3e23647 | 10 | |
9ee3c97b AD |
11 | Bison is distributed in the hope that it will be useful, but |
12 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | General Public License for more details. | |
c3e23647 | 15 | |
9ee3c97b AD |
16 | You should have received a copy of the GNU General Public License |
17 | along with Bison; see the file COPYING. If not, write to the Free | |
18 | Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA | |
19 | 02111-1307, USA. */ | |
c3e23647 RS |
20 | |
21 | ||
6c89f1c1 AD |
22 | /* The parser tables consist of these tables. |
23 | Starred ones needed only for the semantic parser. | |
24 | Double starred are output only if switches are set. | |
c3e23647 | 25 | |
6c89f1c1 AD |
26 | yytranslate = vector mapping yylex's token numbers into bison's token |
27 | numbers. | |
c3e23647 | 28 | |
6c89f1c1 | 29 | ** yytname = vector of string-names indexed by bison token number |
c3e23647 | 30 | |
6c89f1c1 AD |
31 | ** yytoknum = vector of yylex token numbers corresponding to entries |
32 | in yytname | |
c3e23647 | 33 | |
6c89f1c1 | 34 | yyrline = vector of line-numbers of all rules. For yydebug printouts. |
c3e23647 | 35 | |
6c89f1c1 AD |
36 | yyrhs = vector of items of all rules. |
37 | This is exactly what ritems contains. For yydebug and for semantic | |
38 | parser. | |
c3e23647 | 39 | |
6c89f1c1 | 40 | yyprhs[r] = index in yyrhs of first item for rule r. |
c3e23647 | 41 | |
6c89f1c1 | 42 | yyr1[r] = symbol number of symbol that rule r derives. |
c3e23647 | 43 | |
6c89f1c1 | 44 | yyr2[r] = number of symbols composing right hand side of rule r. |
c3e23647 | 45 | |
6c89f1c1 | 46 | * yystos[s] = the symbol number of the symbol that leads to state s. |
e372befa | 47 | |
6c89f1c1 AD |
48 | yydefact[s] = default rule to reduce with in state s, |
49 | when yytable doesn't specify something else to do. | |
50 | Zero means the default is an error. | |
c3e23647 | 51 | |
6c89f1c1 AD |
52 | yydefgoto[i] = default state to go to after a reduction of a rule that |
53 | generates variable ntokens + i, except when yytable | |
54 | specifies something else to do. | |
c3e23647 | 55 | |
6c89f1c1 AD |
56 | yypact[s] = index in yytable of the portion describing state s. |
57 | The lookahead token's type is used to index that portion | |
58 | to find out what to do. | |
c3e23647 | 59 | |
6c89f1c1 AD |
60 | If the value in yytable is positive, |
61 | we shift the token and go to that state. | |
c3e23647 | 62 | |
6c89f1c1 | 63 | If the value is negative, it is minus a rule number to reduce by. |
c3e23647 | 64 | |
6c89f1c1 | 65 | If the value is zero, the default action from yydefact[s] is used. |
c3e23647 | 66 | |
6c89f1c1 AD |
67 | yypgoto[i] = the index in yytable of the portion describing |
68 | what to do after reducing a rule that derives variable i + ntokens. | |
69 | This portion is indexed by the parser state number, s, | |
70 | as of before the text for this nonterminal was read. | |
71 | The value from yytable is the state to go to if | |
72 | the corresponding value in yycheck is s. | |
c3e23647 | 73 | |
6c89f1c1 AD |
74 | yytable = a vector filled with portions for different uses, |
75 | found via yypact and yypgoto. | |
c3e23647 | 76 | |
6c89f1c1 AD |
77 | yycheck = a vector indexed in parallel with yytable. |
78 | It indicates, in a roundabout way, the bounds of the | |
79 | portion you are trying to examine. | |
c3e23647 | 80 | |
6c89f1c1 AD |
81 | Suppose that the portion of yytable starts at index p |
82 | and the index to be examined within the portion is i. | |
83 | Then if yycheck[p+i] != i, i is outside the bounds | |
84 | of what is actually allocated, and the default | |
85 | (from yydefact or yydefgoto) should be used. | |
86 | Otherwise, yytable[p+i] should be used. | |
c3e23647 | 87 | |
6c89f1c1 AD |
88 | YYFINAL = the state number of the termination state. |
89 | YYFLAG = most negative short int. Used to flag ?? | |
90 | YYNTBASE = ntokens. | |
c3e23647 RS |
91 | */ |
92 | ||
c3e23647 | 93 | #include "system.h" |
8c7ebe49 | 94 | #include "obstack.h" |
14d3eb9b | 95 | #include "quotearg.h" |
ceed8467 | 96 | #include "getargs.h" |
d7913476 | 97 | #include "xalloc.h" |
c3e23647 RS |
98 | #include "files.h" |
99 | #include "gram.h" | |
b2ca4022 | 100 | #include "LR0.h" |
a0f6b076 | 101 | #include "complain.h" |
6c89f1c1 | 102 | #include "output.h" |
720d742f | 103 | #include "lalr.h" |
a70083a3 | 104 | #include "reader.h" |
0619caf0 | 105 | #include "conflicts.h" |
83cd972a | 106 | #include "macrotab.h" |
c3e23647 | 107 | |
d019d655 AD |
108 | extern void berror PARAMS((const char *)); |
109 | ||
c3e23647 RS |
110 | static int nvectors; |
111 | static int nentries; | |
112 | static short **froms; | |
113 | static short **tos; | |
114 | static short *tally; | |
115 | static short *width; | |
116 | static short *actrow; | |
117 | static short *state_count; | |
118 | static short *order; | |
119 | static short *base; | |
120 | static short *pos; | |
121 | static short *table; | |
122 | static short *check; | |
123 | static int lowzero; | |
124 | static int high; | |
125 | ||
83cd972a RA |
126 | struct obstack macro_obstack; |
127 | struct obstack output_obstack; | |
c3e23647 | 128 | |
83cd972a | 129 | /* FIXME. */ |
c3e23647 | 130 | |
d019d655 | 131 | static inline void |
83cd972a RA |
132 | output_table_data (struct obstack* oout, |
133 | short* table, | |
134 | short first, | |
135 | short begin, | |
136 | short end) | |
d019d655 | 137 | { |
83cd972a RA |
138 | int i; |
139 | int j = 1; | |
140 | ||
141 | obstack_fgrow1 (oout, "%6d", first); | |
142 | for (i = begin; i < end; ++i) | |
d019d655 | 143 | { |
896fe5c1 | 144 | obstack_1grow (oout, ','); |
d019d655 AD |
145 | if (j >= 10) |
146 | { | |
ff4423cc | 147 | obstack_sgrow (oout, "\n "); |
d019d655 AD |
148 | j = 1; |
149 | } | |
150 | else | |
83cd972a RA |
151 | ++j; |
152 | obstack_fgrow1 (oout, "%6d", table[i]); | |
c3e23647 | 153 | } |
83cd972a | 154 | obstack_1grow (oout, 0); |
c3e23647 RS |
155 | } |
156 | ||
157 | ||
4a120d45 | 158 | static void |
d2729d44 | 159 | output_token_translations (void) |
c3e23647 | 160 | { |
83cd972a RA |
161 | output_table_data (&output_obstack, token_translations, |
162 | 0, 1, max_user_token_number + 1); | |
163 | macro_insert ("translate", obstack_finish (&output_obstack)); | |
c3e23647 RS |
164 | } |
165 | ||
166 | ||
4a120d45 | 167 | static void |
d2729d44 | 168 | output_gram (void) |
c3e23647 | 169 | { |
83cd972a RA |
170 | output_table_data (&output_obstack, rrhs, |
171 | 0, 1, nrules + 1); | |
172 | macro_insert ("prhs", obstack_finish (&output_obstack)); | |
173 | ||
1e9798d5 AD |
174 | { |
175 | size_t yyrhs_size = 1; | |
176 | short *yyrhs, *sp; | |
177 | int i; | |
c3e23647 | 178 | |
1e9798d5 AD |
179 | for (sp = ritem + 1; *sp; sp++) |
180 | ++yyrhs_size; | |
181 | yyrhs = XMALLOC (short, yyrhs_size); | |
c3e23647 | 182 | |
1e9798d5 AD |
183 | for (sp = ritem + 1, i = 1; *sp; ++sp, ++i) |
184 | yyrhs[i] = *sp > 0 ? *sp : 0; | |
c3e23647 | 185 | |
83cd972a RA |
186 | output_table_data (&output_obstack, yyrhs, |
187 | ritem[0], 1, yyrhs_size); | |
188 | macro_insert ("rhs", obstack_finish (&output_obstack)); | |
189 | ||
1e9798d5 AD |
190 | XFREE (yyrhs); |
191 | } | |
c3e23647 | 192 | |
83cd972a RA |
193 | /* if (!semantic_parser && !no_parser_flag) |
194 | obstack_sgrow (&table_obstack, "\n#endif\n"); */ | |
c3e23647 RS |
195 | } |
196 | ||
197 | ||
4a120d45 | 198 | static void |
d2729d44 | 199 | output_stos (void) |
c3e23647 | 200 | { |
83cd972a RA |
201 | output_table_data (&output_obstack, accessing_symbol, |
202 | 0, 1, nstates); | |
203 | macro_insert ("stos", obstack_finish (&output_obstack)); | |
c3e23647 RS |
204 | } |
205 | ||
206 | ||
4a120d45 | 207 | static void |
d2729d44 | 208 | output_rule_data (void) |
c3e23647 | 209 | { |
6c89f1c1 AD |
210 | int i; |
211 | int j; | |
1e9798d5 | 212 | short *short_tab = NULL; |
c3e23647 | 213 | |
83cd972a RA |
214 | output_table_data (&output_obstack, rline, |
215 | 0, 1, nrules + 1); | |
216 | macro_insert ("rline", obstack_finish (&output_obstack)); | |
c3e23647 | 217 | |
1e9798d5 AD |
218 | j = 0; |
219 | for (i = 0; i < nsyms; i++) | |
ceed8467 AD |
220 | /* this used to be i<=nsyms, but that output a final "" symbol |
221 | almost by accident */ | |
c3e23647 | 222 | { |
1e9798d5 AD |
223 | /* Width of the next token, including the two quotes, the coma |
224 | and the space. */ | |
225 | int strsize = 4; | |
6c89f1c1 | 226 | char *p; |
c3e23647 | 227 | |
1e9798d5 AD |
228 | for (p = tags[i]; p && *p; p++) |
229 | if (*p == '"' || *p == '\\' || *p == '\n' || *p == '\t' | |
230 | || *p == '\b') | |
231 | strsize += 2; | |
232 | else if (*p < 040 || *p >= 0177) | |
233 | strsize += 4; | |
234 | else | |
235 | strsize++; | |
236 | ||
237 | if (j + strsize > 75) | |
c3e23647 | 238 | { |
83cd972a | 239 | obstack_sgrow (&output_obstack, "\n "); |
1e9798d5 | 240 | j = 2; |
c3e23647 RS |
241 | } |
242 | ||
83cd972a | 243 | obstack_1grow (&output_obstack, '\"'); |
c3e23647 RS |
244 | for (p = tags[i]; p && *p; p++) |
245 | { | |
246 | if (*p == '"' || *p == '\\') | |
83cd972a | 247 | obstack_fgrow1 (&output_obstack, "\\%c", *p); |
c3e23647 | 248 | else if (*p == '\n') |
83cd972a | 249 | obstack_sgrow (&output_obstack, "\\n"); |
c3e23647 | 250 | else if (*p == '\t') |
83cd972a | 251 | obstack_sgrow (&output_obstack, "\\t"); |
c3e23647 | 252 | else if (*p == '\b') |
83cd972a | 253 | obstack_sgrow (&output_obstack, "\\b"); |
c3e23647 | 254 | else if (*p < 040 || *p >= 0177) |
83cd972a | 255 | obstack_fgrow1 (&output_obstack, "\\%03o", *p); |
c3e23647 | 256 | else |
83cd972a | 257 | obstack_1grow (&output_obstack, *p); |
c3e23647 RS |
258 | } |
259 | ||
83cd972a | 260 | obstack_sgrow (&output_obstack, "\", "); |
1e9798d5 | 261 | j += strsize; |
c3e23647 | 262 | } |
9ee3c97b | 263 | /* add a NULL entry to list of tokens */ |
83cd972a | 264 | obstack_sgrow (&output_obstack, "NULL"); |
c3e23647 | 265 | |
83cd972a RA |
266 | /* Finish table and store. */ |
267 | obstack_1grow (&output_obstack, 0); | |
268 | macro_insert ("tname", obstack_finish (&output_obstack)); | |
e372befa | 269 | |
9ee3c97b | 270 | /* Output YYTOKNUM. */ |
83cd972a RA |
271 | output_table_data (&output_obstack, user_toknums, |
272 | 0, 1, ntokens + 1); | |
273 | macro_insert ("toknum", obstack_finish (&output_obstack)); | |
e372befa | 274 | |
9ee3c97b | 275 | /* Output YYR1. */ |
83cd972a RA |
276 | output_table_data (&output_obstack, rlhs, |
277 | 0, 1, nrules + 1); | |
278 | macro_insert ("r1", obstack_finish (&output_obstack)); | |
d7913476 | 279 | XFREE (rlhs + 1); |
d019d655 | 280 | |
9ee3c97b | 281 | /* Output YYR2. */ |
1e9798d5 | 282 | short_tab = XMALLOC (short, nrules + 1); |
c3e23647 | 283 | for (i = 1; i < nrules; i++) |
1e9798d5 AD |
284 | short_tab[i] = rrhs[i + 1] - rrhs[i] - 1; |
285 | short_tab[nrules] = nitems - rrhs[nrules] - 1; | |
83cd972a RA |
286 | output_table_data (&output_obstack, short_tab, |
287 | 0, 1, nrules + 1); | |
288 | macro_insert ("r2", obstack_finish (&output_obstack)); | |
1e9798d5 | 289 | XFREE (short_tab); |
c3e23647 | 290 | |
d7913476 | 291 | XFREE (rrhs + 1); |
c3e23647 RS |
292 | } |
293 | ||
6c89f1c1 AD |
294 | /*------------------------------------------------------------------. |
295 | | Decide what to do for each type of token if seen as the lookahead | | |
296 | | token in specified state. The value returned is used as the | | |
297 | | default action (yydefact) for the state. In addition, actrow is | | |
298 | | filled with what to do for each kind of token, index by symbol | | |
299 | | number, with zero meaning do the default action. The value | | |
300 | | MINSHORT, a very negative number, means this situation is an | | |
301 | | error. The parser recognizes this value specially. | | |
302 | | | | |
303 | | This is where conflicts are resolved. The loop over lookahead | | |
304 | | rules considered lower-numbered rules last, and the last rule | | |
305 | | considered that likes a token gets to handle it. | | |
306 | `------------------------------------------------------------------*/ | |
c3e23647 | 307 | |
4a120d45 | 308 | static int |
d2729d44 | 309 | action_row (int state) |
c3e23647 | 310 | { |
6c89f1c1 AD |
311 | int i; |
312 | int j; | |
313 | int k; | |
314 | int m = 0; | |
315 | int n = 0; | |
316 | int count; | |
317 | int default_rule; | |
318 | int nreds; | |
319 | int max; | |
320 | int rule; | |
321 | int shift_state; | |
322 | int symbol; | |
323 | unsigned mask; | |
324 | unsigned *wordp; | |
325 | reductions *redp; | |
326 | shifts *shiftp; | |
327 | errs *errp; | |
ceed8467 | 328 | int nodefault = 0; /* set nonzero to inhibit having any default reduction */ |
c3e23647 RS |
329 | |
330 | for (i = 0; i < ntokens; i++) | |
331 | actrow[i] = 0; | |
332 | ||
333 | default_rule = 0; | |
334 | nreds = 0; | |
335 | redp = reduction_table[state]; | |
336 | ||
337 | if (redp) | |
338 | { | |
339 | nreds = redp->nreds; | |
340 | ||
341 | if (nreds >= 1) | |
342 | { | |
36281465 AD |
343 | /* loop over all the rules available here which require |
344 | lookahead */ | |
c3e23647 RS |
345 | m = lookaheads[state]; |
346 | n = lookaheads[state + 1]; | |
347 | ||
348 | for (i = n - 1; i >= m; i--) | |
349 | { | |
ceed8467 | 350 | rule = -LAruleno[i]; |
c3e23647 RS |
351 | wordp = LA + i * tokensetsize; |
352 | mask = 1; | |
353 | ||
36281465 | 354 | /* and find each token which the rule finds acceptable |
ceed8467 | 355 | to come next */ |
c3e23647 RS |
356 | for (j = 0; j < ntokens; j++) |
357 | { | |
36281465 AD |
358 | /* and record this rule as the rule to use if that |
359 | token follows. */ | |
c3e23647 RS |
360 | if (mask & *wordp) |
361 | actrow[j] = rule; | |
362 | ||
363 | mask <<= 1; | |
364 | if (mask == 0) | |
365 | { | |
366 | mask = 1; | |
367 | wordp++; | |
368 | } | |
369 | } | |
370 | } | |
371 | } | |
372 | } | |
373 | ||
374 | shiftp = shift_table[state]; | |
375 | ||
36281465 AD |
376 | /* Now see which tokens are allowed for shifts in this state. For |
377 | them, record the shift as the thing to do. So shift is preferred | |
378 | to reduce. */ | |
c3e23647 RS |
379 | |
380 | if (shiftp) | |
381 | { | |
382 | k = shiftp->nshifts; | |
383 | ||
384 | for (i = 0; i < k; i++) | |
385 | { | |
386 | shift_state = shiftp->shifts[i]; | |
ceed8467 AD |
387 | if (!shift_state) |
388 | continue; | |
c3e23647 RS |
389 | |
390 | symbol = accessing_symbol[shift_state]; | |
391 | ||
ceed8467 | 392 | if (ISVAR (symbol)) |
c3e23647 RS |
393 | break; |
394 | ||
395 | actrow[symbol] = shift_state; | |
396 | ||
36281465 AD |
397 | /* Do not use any default reduction if there is a shift for |
398 | error */ | |
399 | if (symbol == error_token_number) | |
400 | nodefault = 1; | |
c3e23647 RS |
401 | } |
402 | } | |
403 | ||
404 | errp = err_table[state]; | |
405 | ||
36281465 AD |
406 | /* See which tokens are an explicit error in this state (due to |
407 | %nonassoc). For them, record MINSHORT as the action. */ | |
c3e23647 RS |
408 | |
409 | if (errp) | |
410 | { | |
411 | k = errp->nerrs; | |
412 | ||
413 | for (i = 0; i < k; i++) | |
414 | { | |
415 | symbol = errp->errs[i]; | |
416 | actrow[symbol] = MINSHORT; | |
417 | } | |
418 | } | |
419 | ||
36281465 AD |
420 | /* Now find the most common reduction and make it the default action |
421 | for this state. */ | |
c3e23647 | 422 | |
ceed8467 | 423 | if (nreds >= 1 && !nodefault) |
c3e23647 RS |
424 | { |
425 | if (consistent[state]) | |
426 | default_rule = redp->rules[0]; | |
427 | else | |
428 | { | |
429 | max = 0; | |
430 | for (i = m; i < n; i++) | |
431 | { | |
432 | count = 0; | |
ceed8467 | 433 | rule = -LAruleno[i]; |
9ee3c97b | 434 | |
c3e23647 RS |
435 | for (j = 0; j < ntokens; j++) |
436 | { | |
437 | if (actrow[j] == rule) | |
438 | count++; | |
439 | } | |
9ee3c97b | 440 | |
c3e23647 RS |
441 | if (count > max) |
442 | { | |
443 | max = count; | |
444 | default_rule = rule; | |
445 | } | |
446 | } | |
9ee3c97b | 447 | |
c3e23647 RS |
448 | /* actions which match the default are replaced with zero, |
449 | which means "use the default" */ | |
9ee3c97b | 450 | |
c3e23647 RS |
451 | if (max > 0) |
452 | { | |
453 | for (j = 0; j < ntokens; j++) | |
454 | { | |
455 | if (actrow[j] == default_rule) | |
456 | actrow[j] = 0; | |
457 | } | |
9ee3c97b | 458 | |
ceed8467 | 459 | default_rule = -default_rule; |
c3e23647 RS |
460 | } |
461 | } | |
462 | } | |
463 | ||
464 | /* If have no default rule, the default is an error. | |
465 | So replace any action which says "error" with "use default". */ | |
466 | ||
467 | if (default_rule == 0) | |
468 | for (j = 0; j < ntokens; j++) | |
469 | { | |
470 | if (actrow[j] == MINSHORT) | |
471 | actrow[j] = 0; | |
472 | } | |
473 | ||
36281465 | 474 | return default_rule; |
c3e23647 RS |
475 | } |
476 | ||
477 | ||
4a120d45 | 478 | static void |
d2729d44 | 479 | save_row (int state) |
c3e23647 | 480 | { |
6c89f1c1 AD |
481 | int i; |
482 | int count; | |
483 | short *sp; | |
484 | short *sp1; | |
485 | short *sp2; | |
c3e23647 RS |
486 | |
487 | count = 0; | |
488 | for (i = 0; i < ntokens; i++) | |
489 | { | |
490 | if (actrow[i] != 0) | |
491 | count++; | |
492 | } | |
493 | ||
494 | if (count == 0) | |
495 | return; | |
496 | ||
d7913476 AD |
497 | froms[state] = sp1 = sp = XCALLOC (short, count); |
498 | tos[state] = sp2 = XCALLOC (short, count); | |
c3e23647 RS |
499 | |
500 | for (i = 0; i < ntokens; i++) | |
501 | { | |
502 | if (actrow[i] != 0) | |
503 | { | |
504 | *sp1++ = i; | |
505 | *sp2++ = actrow[i]; | |
506 | } | |
507 | } | |
508 | ||
509 | tally[state] = count; | |
510 | width[state] = sp1[-1] - sp[0] + 1; | |
511 | } | |
512 | ||
513 | ||
6c89f1c1 AD |
514 | /*------------------------------------------------------------------. |
515 | | Figure out the actions for the specified state, indexed by | | |
516 | | lookahead token type. | | |
517 | | | | |
f2acea59 AD |
518 | | The YYDEFACT table is output now. The detailed info is saved for | |
519 | | putting into YYTABLE later. | | |
6c89f1c1 | 520 | `------------------------------------------------------------------*/ |
c3e23647 | 521 | |
4a120d45 | 522 | static void |
6c89f1c1 | 523 | token_actions (void) |
c3e23647 | 524 | { |
6c89f1c1 | 525 | int i; |
d7913476 | 526 | short *yydefact = XCALLOC (short, nstates); |
c3e23647 | 527 | |
d7913476 | 528 | actrow = XCALLOC (short, ntokens); |
f2acea59 | 529 | for (i = 0; i < nstates; ++i) |
c3e23647 | 530 | { |
f2acea59 | 531 | yydefact[i] = action_row (i); |
6c89f1c1 | 532 | save_row (i); |
c3e23647 | 533 | } |
f2acea59 | 534 | |
83cd972a RA |
535 | output_table_data (&output_obstack, yydefact, |
536 | yydefact[0], 1, nstates); | |
537 | macro_insert ("defact", obstack_finish (&output_obstack)); | |
538 | ||
539 | XFREE (actrow); | |
d7913476 | 540 | XFREE (yydefact); |
c3e23647 RS |
541 | } |
542 | ||
543 | ||
6c89f1c1 AD |
544 | static void |
545 | free_shifts (void) | |
c3e23647 | 546 | { |
6c89f1c1 | 547 | shifts *sp, *sptmp; /* JF derefrenced freed ptr */ |
c3e23647 | 548 | |
d7913476 | 549 | XFREE (shift_table); |
c3e23647 | 550 | |
6c89f1c1 AD |
551 | for (sp = first_shift; sp; sp = sptmp) |
552 | { | |
553 | sptmp = sp->next; | |
d7913476 | 554 | XFREE (sp); |
6c89f1c1 AD |
555 | } |
556 | } | |
c3e23647 | 557 | |
c3e23647 | 558 | |
6c89f1c1 AD |
559 | static void |
560 | free_reductions (void) | |
561 | { | |
562 | reductions *rp, *rptmp; /* JF fixed freed ptr */ | |
c3e23647 | 563 | |
d7913476 | 564 | XFREE (reduction_table); |
c3e23647 | 565 | |
6c89f1c1 | 566 | for (rp = first_reduction; rp; rp = rptmp) |
c3e23647 | 567 | { |
6c89f1c1 | 568 | rptmp = rp->next; |
d7913476 | 569 | XFREE (rp); |
c3e23647 | 570 | } |
c3e23647 RS |
571 | } |
572 | ||
573 | ||
6c89f1c1 | 574 | |
4a120d45 | 575 | static void |
d2729d44 | 576 | save_column (int symbol, int default_state) |
c3e23647 | 577 | { |
6c89f1c1 | 578 | int i; |
6c89f1c1 AD |
579 | short *sp; |
580 | short *sp1; | |
581 | short *sp2; | |
582 | int count; | |
583 | int symno; | |
c3e23647 | 584 | |
43591cec AD |
585 | short begin = goto_map[symbol]; |
586 | short end = goto_map[symbol + 1]; | |
c3e23647 RS |
587 | |
588 | count = 0; | |
43591cec | 589 | for (i = begin; i < end; i++) |
c3e23647 RS |
590 | { |
591 | if (to_state[i] != default_state) | |
592 | count++; | |
593 | } | |
594 | ||
595 | if (count == 0) | |
596 | return; | |
597 | ||
598 | symno = symbol - ntokens + nstates; | |
599 | ||
d7913476 AD |
600 | froms[symno] = sp1 = sp = XCALLOC (short, count); |
601 | tos[symno] = sp2 = XCALLOC (short, count); | |
c3e23647 | 602 | |
43591cec | 603 | for (i = begin; i < end; i++) |
c3e23647 RS |
604 | { |
605 | if (to_state[i] != default_state) | |
606 | { | |
607 | *sp1++ = from_state[i]; | |
608 | *sp2++ = to_state[i]; | |
609 | } | |
610 | } | |
611 | ||
612 | tally[symno] = count; | |
613 | width[symno] = sp1[-1] - sp[0] + 1; | |
614 | } | |
615 | ||
6c89f1c1 AD |
616 | static int |
617 | default_goto (int symbol) | |
618 | { | |
619 | int i; | |
620 | int m; | |
621 | int n; | |
622 | int default_state; | |
623 | int max; | |
624 | ||
625 | m = goto_map[symbol]; | |
626 | n = goto_map[symbol + 1]; | |
627 | ||
628 | if (m == n) | |
629 | return -1; | |
630 | ||
631 | for (i = 0; i < nstates; i++) | |
632 | state_count[i] = 0; | |
633 | ||
634 | for (i = m; i < n; i++) | |
635 | state_count[to_state[i]]++; | |
636 | ||
637 | max = 0; | |
638 | default_state = -1; | |
639 | ||
640 | for (i = 0; i < nstates; i++) | |
641 | { | |
642 | if (state_count[i] > max) | |
643 | { | |
644 | max = state_count[i]; | |
645 | default_state = i; | |
646 | } | |
647 | } | |
648 | ||
649 | return default_state; | |
650 | } | |
651 | ||
652 | ||
653 | /*-------------------------------------------------------------------. | |
654 | | Figure out what to do after reducing with each rule, depending on | | |
655 | | the saved state from before the beginning of parsing the data that | | |
656 | | matched this rule. | | |
657 | | | | |
658 | | The YYDEFGOTO table is output now. The detailed info is saved for | | |
659 | | putting into YYTABLE later. | | |
660 | `-------------------------------------------------------------------*/ | |
661 | ||
662 | static void | |
663 | goto_actions (void) | |
664 | { | |
43591cec | 665 | int i; |
43591cec | 666 | short *yydefgoto = XMALLOC (short, nsyms - ntokens); |
6c89f1c1 | 667 | |
83cd972a | 668 | state_count = XCALLOC (short, nstates); |
43591cec | 669 | for (i = ntokens; i < nsyms; ++i) |
6c89f1c1 | 670 | { |
43591cec AD |
671 | int default_state = default_goto (i); |
672 | save_column (i, default_state); | |
673 | yydefgoto[i - ntokens] = default_state; | |
6c89f1c1 AD |
674 | } |
675 | ||
83cd972a RA |
676 | output_table_data (&output_obstack, yydefgoto, |
677 | yydefgoto[0], 1, nsyms - ntokens); | |
678 | macro_insert ("defgoto", obstack_finish (&output_obstack)); | |
43591cec | 679 | |
d7913476 | 680 | XFREE (state_count); |
43591cec | 681 | XFREE (yydefgoto); |
6c89f1c1 | 682 | } |
c3e23647 RS |
683 | |
684 | ||
9ee3c97b AD |
685 | /* The next few functions decide how to pack the actions and gotos |
686 | information into yytable. */ | |
c3e23647 | 687 | |
4a120d45 | 688 | static void |
d2729d44 | 689 | sort_actions (void) |
c3e23647 | 690 | { |
6c89f1c1 AD |
691 | int i; |
692 | int j; | |
693 | int k; | |
694 | int t; | |
695 | int w; | |
c3e23647 | 696 | |
d7913476 | 697 | order = XCALLOC (short, nvectors); |
c3e23647 RS |
698 | nentries = 0; |
699 | ||
700 | for (i = 0; i < nvectors; i++) | |
701 | { | |
702 | if (tally[i] > 0) | |
703 | { | |
704 | t = tally[i]; | |
705 | w = width[i]; | |
706 | j = nentries - 1; | |
707 | ||
708 | while (j >= 0 && (width[order[j]] < w)) | |
709 | j--; | |
710 | ||
711 | while (j >= 0 && (width[order[j]] == w) && (tally[order[j]] < t)) | |
712 | j--; | |
713 | ||
714 | for (k = nentries - 1; k > j; k--) | |
715 | order[k + 1] = order[k]; | |
716 | ||
717 | order[j + 1] = i; | |
718 | nentries++; | |
719 | } | |
720 | } | |
721 | } | |
722 | ||
723 | ||
4a120d45 | 724 | static int |
d2729d44 | 725 | matching_state (int vector) |
c3e23647 | 726 | { |
6c89f1c1 AD |
727 | int i; |
728 | int j; | |
729 | int k; | |
730 | int t; | |
731 | int w; | |
732 | int match; | |
733 | int prev; | |
c3e23647 RS |
734 | |
735 | i = order[vector]; | |
736 | if (i >= nstates) | |
36281465 | 737 | return -1; |
c3e23647 RS |
738 | |
739 | t = tally[i]; | |
740 | w = width[i]; | |
741 | ||
742 | for (prev = vector - 1; prev >= 0; prev--) | |
743 | { | |
744 | j = order[prev]; | |
745 | if (width[j] != w || tally[j] != t) | |
36281465 | 746 | return -1; |
c3e23647 RS |
747 | |
748 | match = 1; | |
749 | for (k = 0; match && k < t; k++) | |
750 | { | |
751 | if (tos[j][k] != tos[i][k] || froms[j][k] != froms[i][k]) | |
752 | match = 0; | |
753 | } | |
754 | ||
755 | if (match) | |
36281465 | 756 | return j; |
c3e23647 RS |
757 | } |
758 | ||
36281465 | 759 | return -1; |
c3e23647 RS |
760 | } |
761 | ||
762 | ||
4a120d45 | 763 | static int |
d2729d44 | 764 | pack_vector (int vector) |
c3e23647 | 765 | { |
6c89f1c1 AD |
766 | int i; |
767 | int j; | |
768 | int k; | |
769 | int t; | |
770 | int loc = 0; | |
771 | int ok; | |
772 | short *from; | |
773 | short *to; | |
c3e23647 RS |
774 | |
775 | i = order[vector]; | |
776 | t = tally[i]; | |
777 | ||
340ef489 | 778 | assert (t); |
c3e23647 RS |
779 | |
780 | from = froms[i]; | |
781 | to = tos[i]; | |
782 | ||
783 | for (j = lowzero - from[0]; j < MAXTABLE; j++) | |
784 | { | |
785 | ok = 1; | |
786 | ||
787 | for (k = 0; ok && k < t; k++) | |
788 | { | |
789 | loc = j + from[k]; | |
790 | if (loc > MAXTABLE) | |
a0f6b076 | 791 | fatal (_("maximum table size (%d) exceeded"), MAXTABLE); |
c3e23647 RS |
792 | |
793 | if (table[loc] != 0) | |
794 | ok = 0; | |
795 | } | |
796 | ||
797 | for (k = 0; ok && k < vector; k++) | |
798 | { | |
799 | if (pos[k] == j) | |
800 | ok = 0; | |
801 | } | |
802 | ||
803 | if (ok) | |
804 | { | |
805 | for (k = 0; k < t; k++) | |
806 | { | |
807 | loc = j + from[k]; | |
808 | table[loc] = to[k]; | |
809 | check[loc] = from[k]; | |
810 | } | |
811 | ||
812 | while (table[lowzero] != 0) | |
813 | lowzero++; | |
814 | ||
815 | if (loc > high) | |
816 | high = loc; | |
817 | ||
36281465 | 818 | return j; |
c3e23647 RS |
819 | } |
820 | } | |
821 | ||
ceed8467 AD |
822 | berror ("pack_vector"); |
823 | return 0; /* JF keep lint happy */ | |
c3e23647 RS |
824 | } |
825 | ||
826 | ||
6c89f1c1 AD |
827 | static void |
828 | pack_table (void) | |
829 | { | |
830 | int i; | |
831 | int place; | |
832 | int state; | |
833 | ||
d7913476 AD |
834 | base = XCALLOC (short, nvectors); |
835 | pos = XCALLOC (short, nentries); | |
836 | table = XCALLOC (short, MAXTABLE); | |
837 | check = XCALLOC (short, MAXTABLE); | |
6c89f1c1 AD |
838 | |
839 | lowzero = 0; | |
840 | high = 0; | |
841 | ||
842 | for (i = 0; i < nvectors; i++) | |
843 | base[i] = MINSHORT; | |
844 | ||
845 | for (i = 0; i < MAXTABLE; i++) | |
846 | check[i] = -1; | |
847 | ||
848 | for (i = 0; i < nentries; i++) | |
849 | { | |
850 | state = matching_state (i); | |
851 | ||
852 | if (state < 0) | |
853 | place = pack_vector (i); | |
854 | else | |
855 | place = base[state]; | |
856 | ||
857 | pos[i] = place; | |
858 | base[order[i]] = place; | |
859 | } | |
860 | ||
861 | for (i = 0; i < nvectors; i++) | |
862 | { | |
863 | if (froms[i]) | |
d7913476 | 864 | XFREE (froms[i]); |
6c89f1c1 | 865 | if (tos[i]) |
d7913476 | 866 | XFREE (tos[i]); |
6c89f1c1 AD |
867 | } |
868 | ||
d7913476 AD |
869 | XFREE (froms); |
870 | XFREE (tos); | |
871 | XFREE (pos); | |
6c89f1c1 | 872 | } |
c3e23647 RS |
873 | |
874 | /* the following functions output yytable, yycheck | |
875 | and the vectors whose elements index the portion starts */ | |
876 | ||
4a120d45 | 877 | static void |
d2729d44 | 878 | output_base (void) |
c3e23647 | 879 | { |
83cd972a RA |
880 | /* Output pact. */ |
881 | output_table_data (&output_obstack, base, | |
882 | base[0], 1, nstates); | |
883 | macro_insert ("pact", obstack_finish (&output_obstack)); | |
c3e23647 | 884 | |
83cd972a RA |
885 | /* Output pgoto. */ |
886 | output_table_data (&output_obstack, base, | |
887 | base[nstates], nstates + 1, nvectors); | |
888 | macro_insert ("pgoto", obstack_finish (&output_obstack)); | |
c3e23647 | 889 | |
d7913476 | 890 | XFREE (base); |
c3e23647 RS |
891 | } |
892 | ||
893 | ||
4a120d45 | 894 | static void |
d2729d44 | 895 | output_table (void) |
c3e23647 | 896 | { |
83cd972a RA |
897 | output_table_data (&output_obstack, table, |
898 | table[0], 1, high + 1); | |
899 | macro_insert ("table", obstack_finish (&output_obstack)); | |
d7913476 | 900 | XFREE (table); |
c3e23647 RS |
901 | } |
902 | ||
903 | ||
4a120d45 | 904 | static void |
d2729d44 | 905 | output_check (void) |
c3e23647 | 906 | { |
83cd972a RA |
907 | output_table_data (&output_obstack, check, |
908 | check[0], 1, high + 1); | |
909 | macro_insert ("check", obstack_finish (&output_obstack)); | |
d7913476 | 910 | XFREE (check); |
c3e23647 RS |
911 | } |
912 | ||
6c89f1c1 AD |
913 | /* compute and output yydefact, yydefgoto, yypact, yypgoto, yytable |
914 | and yycheck. */ | |
915 | ||
916 | static void | |
917 | output_actions (void) | |
918 | { | |
919 | nvectors = nstates + nvars; | |
c3e23647 | 920 | |
d7913476 AD |
921 | froms = XCALLOC (short *, nvectors); |
922 | tos = XCALLOC (short *, nvectors); | |
923 | tally = XCALLOC (short, nvectors); | |
924 | width = XCALLOC (short, nvectors); | |
6c89f1c1 AD |
925 | |
926 | token_actions (); | |
927 | free_shifts (); | |
928 | free_reductions (); | |
d7913476 AD |
929 | XFREE (lookaheads); |
930 | XFREE (LA); | |
931 | XFREE (LAruleno); | |
932 | XFREE (accessing_symbol); | |
6c89f1c1 AD |
933 | |
934 | goto_actions (); | |
d7913476 AD |
935 | XFREE (goto_map + ntokens); |
936 | XFREE (from_state); | |
937 | XFREE (to_state); | |
6c89f1c1 AD |
938 | |
939 | sort_actions (); | |
940 | pack_table (); | |
83cd972a RA |
941 | /* FIXME: See if this is useful. */ |
942 | /* obstack_1grow (&table_obstack, '\n'); */ | |
6c89f1c1 AD |
943 | output_base (); |
944 | output_table (); | |
83cd972a RA |
945 | /* FIXME: See if this is useful. */ |
946 | /* obstack_1grow (&table_obstack, '\n'); */ | |
6c89f1c1 AD |
947 | output_check (); |
948 | } | |
c3e23647 | 949 | |
ff61dabd AD |
950 | /*------------------------------------------. |
951 | | Copy the parser code into TABLE_OBSTACK. | | |
952 | `------------------------------------------*/ | |
c3e23647 | 953 | |
4a120d45 | 954 | static void |
d2729d44 | 955 | output_parser (void) |
c3e23647 | 956 | { |
6c89f1c1 | 957 | int c; |
ff61dabd | 958 | FILE *fskel; |
ef7ddedd | 959 | size_t line; |
573c1d9f | 960 | int actions_dumped = 0; |
c3e23647 | 961 | |
ff61dabd | 962 | /* Loop over lines in the standard parser file. */ |
cd5bd6ac AD |
963 | if (!skeleton) |
964 | { | |
965 | if (semantic_parser) | |
966 | skeleton = skeleton_find ("BISON_HAIRY", BISON_HAIRY); | |
967 | else | |
968 | skeleton = skeleton_find ("BISON_SIMPLE", BISON_SIMPLE); | |
969 | } | |
ef7ddedd AD |
970 | fskel = xfopen (skeleton, "r"); |
971 | ||
83cd972a RA |
972 | /* New output code. */ |
973 | line = 1; | |
974 | c = getc (fskel); | |
975 | while (c != EOF) | |
c3e23647 | 976 | { |
83cd972a | 977 | if (c != '%') |
573c1d9f | 978 | { |
83cd972a RA |
979 | if (c == '\n') |
980 | ++line; | |
981 | obstack_1grow (&table_obstack, c); | |
982 | c = getc (fskel); | |
573c1d9f | 983 | } |
83cd972a | 984 | else if ((c = getc (fskel)) == '%') |
573c1d9f | 985 | { |
83cd972a RA |
986 | /* Read the macro. */ |
987 | char* macro_key = 0; | |
988 | char* macro_value = 0; | |
989 | while (isalnum (c = getc (fskel)) || c == '_') | |
990 | obstack_1grow (¯o_obstack, c); | |
991 | obstack_1grow (¯o_obstack, 0); | |
992 | ||
993 | /* Output the right value, or see if it's something special. */ | |
994 | macro_key = obstack_finish (¯o_obstack); | |
995 | macro_value = macro_find (macro_key); | |
996 | if (macro_value) | |
997 | obstack_sgrow (&table_obstack, macro_value); | |
998 | else if (!strcmp (macro_key, "line")) | |
999 | obstack_fgrow1 (&table_obstack, "%d", line + 1); | |
1000 | else if (!strcmp (macro_key, "action")) | |
1001 | { | |
1002 | size_t size = obstack_object_size (&action_obstack); | |
1003 | obstack_grow (&table_obstack, | |
1004 | obstack_finish (&action_obstack), size); | |
1005 | } | |
573c1d9f | 1006 | else |
83cd972a RA |
1007 | { |
1008 | obstack_sgrow (&table_obstack, "%%"); | |
1009 | obstack_sgrow (&table_obstack, macro_key); | |
1010 | } | |
573c1d9f | 1011 | } |
c3e23647 | 1012 | } |
83cd972a RA |
1013 | |
1014 | /* End. */ | |
ff61dabd | 1015 | xfclose (fskel); |
c3e23647 RS |
1016 | } |
1017 | ||
4a120d45 | 1018 | static void |
d2729d44 | 1019 | output_program (void) |
c3e23647 | 1020 | { |
6c89f1c1 | 1021 | int c; |
c3e23647 | 1022 | |
14d3eb9b AD |
1023 | while ((c = getc (finput)) != EOF) |
1024 | obstack_1grow (&table_obstack, c); | |
c3e23647 RS |
1025 | } |
1026 | ||
1027 | ||
4a120d45 | 1028 | static void |
d2729d44 | 1029 | free_itemsets (void) |
c3e23647 | 1030 | { |
6c89f1c1 | 1031 | core *cp, *cptmp; |
c3e23647 | 1032 | |
d7913476 | 1033 | XFREE (state_table); |
c3e23647 | 1034 | |
36281465 AD |
1035 | for (cp = first_state; cp; cp = cptmp) |
1036 | { | |
ceed8467 | 1037 | cptmp = cp->next; |
d7913476 | 1038 | XFREE (cp); |
36281465 | 1039 | } |
c3e23647 RS |
1040 | } |
1041 | ||
83cd972a RA |
1042 | /* FIXME. */ |
1043 | ||
1044 | #define MACRO_INSERT_INT(Key, Value) \ | |
1045 | { \ | |
1046 | obstack_fgrow1 (¯o_obstack, "%d", Value); \ | |
1047 | obstack_1grow (¯o_obstack, 0); \ | |
1048 | macro_insert (Key, obstack_finish (¯o_obstack)); \ | |
1049 | } | |
1050 | ||
1051 | #define MACRO_INSERT_STRING(Key, Value) \ | |
1052 | { \ | |
1053 | obstack_sgrow (¯o_obstack, Value); \ | |
1054 | obstack_1grow (¯o_obstack, 0); \ | |
1055 | macro_insert (Key, obstack_finish (¯o_obstack)); \ | |
1056 | } | |
1057 | ||
1058 | #define MACRO_INSERT_PREFIX(Key, Value) \ | |
1059 | { \ | |
1060 | obstack_fgrow2 (¯o_obstack, "%s%s", spec_name_prefix, Value); \ | |
1061 | obstack_1grow (¯o_obstack, 0); \ | |
1062 | macro_insert (Key, obstack_finish (¯o_obstack)); \ | |
1063 | } | |
1064 | ||
1065 | static void | |
1066 | prepare (void) | |
1067 | { | |
1068 | MACRO_INSERT_INT ("last", high); | |
1069 | MACRO_INSERT_INT ("flag", MINSHORT); | |
1070 | MACRO_INSERT_INT ("pure", pure_parser); | |
1071 | MACRO_INSERT_INT ("nsym", nsyms); | |
1072 | MACRO_INSERT_INT ("debug", debug_flag); | |
1073 | MACRO_INSERT_INT ("final", final_state); | |
1074 | MACRO_INSERT_INT ("maxtok", max_user_token_number); | |
1075 | MACRO_INSERT_INT ("ntbase", ntokens); | |
1076 | MACRO_INSERT_INT ("verbose", 0); | |
1077 | ||
1078 | MACRO_INSERT_STRING ("filename", infile); | |
1079 | ||
1080 | MACRO_INSERT_INT ("nnts", nvars); | |
1081 | MACRO_INSERT_INT ("nrules", nrules); | |
1082 | MACRO_INSERT_INT ("nstates", nstates); | |
1083 | MACRO_INSERT_INT ("ntokens", ntokens); | |
1084 | ||
1085 | if (spec_name_prefix) | |
1086 | { | |
1087 | MACRO_INSERT_PREFIX ("yylex", "lex"); | |
1088 | MACRO_INSERT_PREFIX ("yychar", "char"); | |
1089 | MACRO_INSERT_PREFIX ("yylval", "lval"); | |
1090 | MACRO_INSERT_PREFIX ("yydebug", "debug"); | |
1091 | MACRO_INSERT_PREFIX ("yyerror", "error"); | |
1092 | MACRO_INSERT_PREFIX ("yynerrs", "nerrs"); | |
1093 | MACRO_INSERT_PREFIX ("yyparse", "parse"); | |
1094 | } | |
1095 | } | |
c3e23647 | 1096 | |
6c89f1c1 AD |
1097 | /*----------------------------------------------------------. |
1098 | | Output the parsing tables and the parser code to ftable. | | |
1099 | `----------------------------------------------------------*/ | |
c3e23647 | 1100 | |
6c89f1c1 AD |
1101 | void |
1102 | output (void) | |
1103 | { | |
83cd972a RA |
1104 | obstack_init (¯o_obstack); |
1105 | obstack_init (&output_obstack); | |
dd60faec | 1106 | |
83cd972a | 1107 | #if 0 |
dd60faec | 1108 | /* If using a simple parser the definition of YYSTYPE are put into |
896fe5c1 | 1109 | TABLE_OBSTACK. */ |
dd60faec | 1110 | if (!semantic_parser) |
36281465 | 1111 | { |
dd60faec | 1112 | size_t size = obstack_object_size (&attrs_obstack); |
896fe5c1 | 1113 | obstack_grow (&table_obstack, obstack_finish (&attrs_obstack), size); |
36281465 | 1114 | } |
83cd972a | 1115 | #endif |
c3e23647 | 1116 | |
83cd972a | 1117 | /* reader_output_yylsp (&table_obstack); */ |
6c89f1c1 | 1118 | free_itemsets (); |
83cd972a | 1119 | |
6c89f1c1 | 1120 | output_token_translations (); |
6c89f1c1 | 1121 | output_gram (); |
83cd972a | 1122 | |
d7913476 | 1123 | XFREE (ritem); |
6c89f1c1 AD |
1124 | if (semantic_parser) |
1125 | output_stos (); | |
1126 | output_rule_data (); | |
1127 | output_actions (); | |
83cd972a RA |
1128 | |
1129 | /* if (!no_parser_flag) */ | |
1130 | prepare (); | |
1131 | output_parser (); | |
6c89f1c1 | 1132 | output_program (); |
83cd972a RA |
1133 | |
1134 | obstack_free (¯o_obstack, 0); | |
1135 | obstack_free (&output_obstack, 0); | |
c3e23647 | 1136 | } |