1 /* Output the generated parsing program for bison,
2 Copyright (C) 1984, 1986, 1989, 1992, 2000, 2001, 2002
3 Free Software Foundation, Inc.
5 This file is part of Bison, the GNU Compiler Compiler.
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)
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.
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
23 /* The parser tables consist of these tables. Marked ones needed only
24 for the semantic parser. Double marked are output only if switches
27 YYTRANSLATE = vector mapping yylex's token numbers into bison's
30 ++ YYTNAME = vector of string-names indexed by bison token number.
32 ++ YYTOKNUM = vector of yylex token numbers corresponding to
35 YYRLINE = vector of line-numbers of all rules. For yydebug
38 YYRHS = vector of items of all rules. This is exactly what RITEMS
39 contains. For yydebug and for semantic parser.
41 YYPRHS[R] = index in YYRHS of first item for rule R.
43 YYR1[R] = symbol number of symbol that rule R derives.
45 YYR2[R] = number of symbols composing right hand side of rule R.
47 + YYSTOS[S] = the symbol number of the symbol that leads to state
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
54 YYDEFGOTO[I] = default state to go to after a reduction of a rule
55 that generates variable NTOKENS + I, except when YYTABLE specifies
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
62 If the value in YYTABLE is positive, we shift the token and go to
65 If the value is negative, it is minus a rule number to reduce by.
67 If the value is zero, the default action from YYDEFACT[S] is used.
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.
75 YYTABLE = a vector filled with portions for different uses, found
76 via YYPACT and YYPGOTO.
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
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.
88 YYFINAL = the state number of the termination state. YYFLAG = most
89 negative short int. Used to flag ?? */
104 #include "conflicts.h"
105 #include "muscle_tab.h"
107 /* From src/scan-skel.l. */
108 void m4_invoke
PARAMS ((const char *definitions
));
112 static short **froms
= NULL
;
113 static short **tos
= NULL
;
114 static unsigned int **conflict_tos
= NULL
;
115 static short *tally
= NULL
;
116 static short *width
= NULL
;
117 static short *actrow
= NULL
;
118 static short *conflrow
= NULL
;
119 static short *state_count
= NULL
;
120 static short *order
= NULL
;
121 static short *base
= NULL
;
122 static short *pos
= NULL
;
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
;
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
;
133 static short *table
= NULL
;
134 static short *check
= NULL
;
138 static struct obstack format_obstack
;
140 int error_verbose
= 0;
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 `----------------------------------------------------------------*/
150 table_grow (size_t desired
)
152 size_t old_size
= table_size
;
154 while (table_size
<= desired
)
158 fprintf (stderr
, "growing table and check from: %d to %d\n",
159 old_size
, table_size
);
161 table
= XREALLOC (table
, short, table_size
);
162 check
= XREALLOC (check
, short, table_size
);
164 conflict_table
= XREALLOC (conflict_table
, unsigned int, table_size
);
166 for (/* Nothing. */; old_size
< table_size
; ++old_size
)
169 check
[old_size
] = -1;
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 |
179 `-------------------------------------------------------------------*/
182 #define GENERATE_MUSCLE_INSERT_TABLE(Name, Type) \
185 Name (const char *name, \
195 obstack_fgrow1 (&format_obstack, "%6d", first); \
196 for (i = begin; i < end; ++i) \
198 obstack_1grow (&format_obstack, ','); \
201 obstack_sgrow (&format_obstack, "\n "); \
206 obstack_fgrow1 (&format_obstack, "%6d", table_data[i]); \
207 if (table_data[i] > max) \
208 max = table_data[i]; \
210 obstack_1grow (&format_obstack, 0); \
211 muscle_insert (name, obstack_finish (&format_obstack)); \
213 /* Build `NAME_max' in the obstack. */ \
214 obstack_fgrow1 (&format_obstack, "%s_max", name); \
215 obstack_1grow (&format_obstack, 0); \
216 MUSCLE_INSERT_LONG_INT (obstack_finish (&format_obstack), \
220 GENERATE_MUSCLE_INSERT_TABLE(muscle_insert_unsigned_int_table
, unsigned int)
221 GENERATE_MUSCLE_INSERT_TABLE(muscle_insert_short_table
, short)
222 GENERATE_MUSCLE_INSERT_TABLE(muscle_insert_symbol_number_table
, symbol_number_t
)
223 GENERATE_MUSCLE_INSERT_TABLE(muscle_insert_item_number_table
, item_number_t
)
224 GENERATE_MUSCLE_INSERT_TABLE(muscle_insert_state_number_table
, state_number_t
)
227 /*-----------------------------------------------------------------.
228 | Prepare the muscles related to the tokens: translate, tname, and |
230 `-----------------------------------------------------------------*/
233 prepare_tokens (void)
235 muscle_insert_symbol_number_table ("translate",
237 0, 1, max_user_token_number
+ 1);
242 for (i
= 0; i
< nsyms
; i
++)
244 /* Be sure not to use twice the same QUOTEARG slot:
245 SYMBOL_TAG_GET uses slot 0. */
247 quotearg_n_style (1, c_quoting_style
,
249 /* Width of the next token, including the two quotes, the coma
251 int strsize
= strlen (cp
) + 2;
253 if (j
+ strsize
> 75)
255 obstack_sgrow (&format_obstack
, "\n ");
259 obstack_sgrow (&format_obstack
, cp
);
260 obstack_sgrow (&format_obstack
, ", ");
263 /* Add a NULL entry to list of tokens (well, 0, as NULL might not be
265 obstack_sgrow (&format_obstack
, "0");
267 /* Finish table and store. */
268 obstack_1grow (&format_obstack
, 0);
269 muscle_insert ("tname", obstack_finish (&format_obstack
));
272 /* Output YYTOKNUM. */
275 short *values
= XCALLOC (short, ntokens
+ 1);
276 for (i
= 0; i
< ntokens
+ 1; ++i
)
277 values
[i
] = symbols
[i
]->user_token_number
;
278 muscle_insert_short_table ("toknum", values
,
285 /*-------------------------------------------------------------.
286 | Prepare the muscles related to the rules: rhs, prhs, r1, r2, |
287 | rline, dprec, merger |
288 `-------------------------------------------------------------*/
295 item_number_t
*rhs
= XMALLOC (item_number_t
, nritems
);
296 unsigned int *prhs
= XMALLOC (unsigned int, nrules
+ 1);
297 unsigned int *rline
= XMALLOC (unsigned int, nrules
+ 1);
298 symbol_number_t
*r1
= XMALLOC (symbol_number_t
, nrules
+ 1);
299 unsigned int *r2
= XMALLOC (unsigned int, nrules
+ 1);
300 short *dprec
= XMALLOC (short, nrules
+ 1);
301 short *merger
= XMALLOC (short, nrules
+ 1);
303 for (r
= 1; r
< nrules
+ 1; ++r
)
305 item_number_t
*rhsp
= NULL
;
306 /* Index of rule R in RHS. */
308 /* RHS of the rule R. */
309 for (rhsp
= rules
[r
].rhs
; *rhsp
>= 0; ++rhsp
)
311 /* LHS of the rule R. */
312 r1
[r
] = rules
[r
].lhs
->number
;
313 /* Length of rule R's RHS. */
315 /* Separator in RHS. */
317 /* Line where rule was defined. */
318 rline
[r
] = rules
[r
].location
.first_line
;
319 /* Dynamic precedence (GLR) */
320 dprec
[r
] = rules
[r
].dprec
;
321 /* Merger-function index (GLR) */
322 merger
[r
] = rules
[r
].merger
;
324 assert (i
== nritems
);
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);
329 muscle_insert_symbol_number_table ("r1", r1
, 0, 1, nrules
+ 1);
330 muscle_insert_unsigned_int_table ("r2", r2
, 0, 1, nrules
+ 1);
331 muscle_insert_short_table ("dprec", dprec
, 0, 1, nrules
+ 1);
332 muscle_insert_short_table ("merger", merger
, 0, 1, nrules
+ 1);
343 /*--------------------------------------------.
344 | Prepare the muscles related to the states. |
345 `--------------------------------------------*/
348 prepare_states (void)
351 symbol_number_t
*values
=
352 (symbol_number_t
*) alloca (sizeof (symbol_number_t
) * nstates
);
353 for (i
= 0; i
< nstates
; ++i
)
354 values
[i
] = states
[i
]->accessing_symbol
;
355 muscle_insert_symbol_number_table ("stos", values
,
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 `-------------------------------------------------------------------*/
371 conflict_row (state_t
*state
)
378 for (j
= 0; j
< ntokens
; j
+= 1)
381 conflrow
[j
] = conflict_list_cnt
;
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
)
389 assert (conflict_list_free
> 0);
390 conflict_list
[conflict_list_cnt
]
391 = state
->lookaheads_rule
[i
]->number
;
392 conflict_list_cnt
+= 1;
393 conflict_list_free
-= 1;
396 /* Leave a 0 at the end */
397 assert (conflict_list_free
> 0);
398 conflict_list_cnt
+= 1;
399 conflict_list_free
-= 1;
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 |
410 | SHRT_MIN, a very negative number, means this situation is an |
411 | error. The parser recognizes this value specially. |
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. |
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. |
421 `------------------------------------------------------------------*/
424 action_row (state_t
*state
)
427 rule_number_t default_rule
= 0;
428 reductions_t
*redp
= state
->reductions
;
429 transitions_t
*transitions
= state
->transitions
;
430 errs_t
*errp
= state
->errs
;
431 /* set nonzero to inhibit having any default reduction */
435 for (i
= 0; i
< ntokens
; i
++)
436 actrow
[i
] = conflrow
[i
] = 0;
441 bitset_iterator biter
;
442 /* loop over all the rules available here which require
444 for (i
= state
->nlookaheads
- 1; i
>= 0; --i
)
445 /* and find each token which the rule finds acceptable
447 BITSET_FOR_EACH (biter
, state
->lookaheads
[i
], j
, 0)
449 /* and record this rule as the rule to use if that
452 conflicted
= conflrow
[j
] = 1;
453 actrow
[j
] = -state
->lookaheads_rule
[i
]->number
;
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
460 for (i
= 0; i
< transitions
->num
&& TRANSITION_IS_SHIFT (transitions
, i
); i
++)
461 if (!TRANSITION_IS_DISABLED (transitions
, i
))
463 symbol_number_t symbol
= TRANSITION_SYMBOL (transitions
, i
);
464 state_number_t shift_state
= transitions
->states
[i
];
466 if (actrow
[symbol
] != 0)
467 conflicted
= conflrow
[symbol
] = 1;
468 actrow
[symbol
] = state_number_as_int (shift_state
);
470 /* Do not use any default reduction if there is a shift for
472 if (symbol
== errtoken
->number
)
476 /* See which tokens are an explicit error in this state (due to
477 %nonassoc). For them, record SHRT_MIN as the action. */
478 for (i
= 0; i
< errp
->num
; i
++)
480 symbol_number_t symbol
= errp
->symbols
[i
];
481 actrow
[symbol
] = SHRT_MIN
;
484 /* Now find the most common reduction and make it the default action
487 if (redp
->num
>= 1 && !nodefault
)
489 if (state
->consistent
)
490 default_rule
= redp
->rules
[0];
494 for (i
= 0; i
< state
->nlookaheads
; i
++)
497 rule_number_t rule
= state
->lookaheads_rule
[i
]->number
;
500 for (j
= 0; j
< ntokens
; j
++)
501 if (actrow
[j
] == -rule
)
511 /* GLR parsers need space for conflict lists, so we can't
512 default conflicted entries. For non-conflicted entries
513 or as long as we are not building a GLR parser,
514 actions that match the default are replaced with zero,
515 which means "use the default". */
520 for (j
= 0; j
< ntokens
; j
++)
521 if (actrow
[j
] == -default_rule
522 && ! (glr_parser
&& conflrow
[j
]))
528 /* If have no default rule, the default is an error.
529 So replace any action which says "error" with "use default". */
531 if (default_rule
== 0)
532 for (i
= 0; i
< ntokens
; i
++)
533 if (actrow
[i
] == SHRT_MIN
)
537 conflict_row (state
);
544 save_row (state_number_t state
)
551 unsigned int *sp3
= NULL
;
554 for (i
= 0; i
< ntokens
; i
++)
561 froms
[state
] = sp1
= sp
= XCALLOC (short, count
);
562 tos
[state
] = sp2
= XCALLOC (short, count
);
564 conflict_tos
[state
] = sp3
= XCALLOC (unsigned int, count
);
566 conflict_tos
[state
] = NULL
;
568 for (i
= 0; i
< ntokens
; i
++)
574 *sp3
++ = conflrow
[i
];
577 tally
[state
] = count
;
578 width
[state
] = sp1
[-1] - sp
[0] + 1;
582 /*------------------------------------------------------------------.
583 | Figure out the actions for the specified state, indexed by |
584 | lookahead token type. |
586 | The YYDEFACT table is output now. The detailed info is saved for |
587 | putting into YYTABLE later. |
588 `------------------------------------------------------------------*/
594 int nconflict
= conflicts_total_count ();
596 short *yydefact
= XCALLOC (short, nstates
);
598 actrow
= XCALLOC (short, ntokens
);
600 conflrow
= XCALLOC (short, ntokens
);
603 conflict_list
= XCALLOC (unsigned int, 1 + 2 * nconflict
);
604 conflict_list_free
= 2 * nconflict
;
605 conflict_list_cnt
= 1;
608 conflict_list_free
= conflict_list_cnt
= 0;
610 for (i
= 0; i
< nstates
; ++i
)
612 yydefact
[i
] = action_row (states
[i
]);
616 muscle_insert_short_table ("defact", yydefact
,
617 yydefact
[0], 1, nstates
);
624 /*-----------------------------.
625 | Output the actions to OOUT. |
626 `-----------------------------*/
629 actions_output (FILE *out
)
633 fputs ("m4_define([b4_actions], \n[[", out
);
634 for (r
= 1; r
< nrules
+ 1; ++r
)
637 fprintf (out
, " case %d:\n", r
);
640 fprintf (out
, muscle_find ("linef"),
641 rules
[r
].action_location
.first_line
,
642 quotearg_style (c_quoting_style
,
643 muscle_find ("filename")));
644 fprintf (out
, " %s\n break;\n\n",
647 fputs ("]])\n\n", out
);
650 /*--------------------------------------.
651 | Output the merge functions to OUT. |
652 `--------------------------------------*/
655 merger_output (FILE *out
)
660 fputs ("m4_define([b4_mergers], \n[[", out
);
661 for (n
= 1, p
= merge_functions
; p
!= NULL
; n
+= 1, p
= p
->next
)
663 if (p
->type
[0] == '\0')
664 fprintf (out
, " case %d: yyval = %s (*yy0, *yy1); break;\n",
667 fprintf (out
, " case %d: yyval.%s = %s (*yy0, *yy1); break;\n",
668 n
, p
->type
, p
->name
);
670 fputs ("]])\n\n", out
);
673 /*---------------------------------------.
674 | Output the tokens definition to OOUT. |
675 `---------------------------------------*/
678 token_definitions_output (FILE *out
)
683 fputs ("m4_define([b4_tokens], \n[", out
);
684 for (i
= 0; i
< ntokens
; ++i
)
686 symbol_t
*symbol
= symbols
[i
];
687 int number
= symbol
->user_token_number
;
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
692 assert (number
!= USER_NUMBER_ALIAS
);
694 /* Skip error token. */
695 if (symbol
== errtoken
)
698 /* If this string has an alias, then it is necessarily the alias
699 which is to be output. */
701 symbol
= symbol
->alias
;
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] == '\"')
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
, '$'))
714 fprintf (out
, "%s[[[%s]], [%d]]",
715 first
? "" : ",\n", symbol
->tag
, number
);
719 fputs ("])\n\n", out
);
723 /*----------------------------------------.
724 | Output the symbol destructors to OOUT. |
725 `----------------------------------------*/
728 symbol_destructors_output (FILE *out
)
733 fputs ("m4_define([b4_symbol_destructors], \n[", out
);
734 for (i
= 0; i
< nsyms
; ++i
)
735 if (symbols
[i
]->destructor
)
737 symbol_t
*symbol
= symbols
[i
];
740 Symbol-name, Symbol-number,
741 destructor, typename. */
742 fprintf (out
, "%s[[[%s]], [[%d]], [[%s]], [[%d]], [[%s]], [[%s]]]",
744 infile
, symbol
->destructor_location
.first_line
,
752 fputs ("])\n\n", out
);
756 /*-------------------------------------.
757 | Output the symbol printers to OOUT. |
758 `-------------------------------------*/
761 symbol_printers_output (FILE *out
)
766 fputs ("m4_define([b4_symbol_printers], \n[", out
);
767 for (i
= 0; i
< nsyms
; ++i
)
768 if (symbols
[i
]->destructor
)
770 symbol_t
*symbol
= symbols
[i
];
773 Symbol-name, Symbol-number,
774 destructor, typename. */
775 fprintf (out
, "%s[[[%s]], [[%d]], [[%s]], [[%d]], [[%s]], [[%s]]]",
777 infile
, symbol
->printer_location
.first_line
,
785 fputs ("])\n\n", out
);
790 save_column (symbol_number_t symbol
, state_number_t default_state
)
797 int symno
= symbol
- ntokens
+ state_number_as_int (nstates
);
799 int begin
= goto_map
[symbol
];
800 int end
= goto_map
[symbol
+ 1];
803 for (i
= begin
; i
< end
; i
++)
804 if (to_state
[i
] != default_state
)
810 froms
[symno
] = sp1
= sp
= XCALLOC (short, count
);
811 tos
[symno
] = sp2
= XCALLOC (short, count
);
813 for (i
= begin
; i
< end
; i
++)
814 if (to_state
[i
] != default_state
)
816 *sp1
++ = from_state
[i
];
817 *sp2
++ = to_state
[i
];
820 tally
[symno
] = count
;
821 width
[symno
] = sp1
[-1] - sp
[0] + 1;
825 static state_number_t
826 default_goto (symbol_number_t symbol
)
830 int m
= goto_map
[symbol
];
831 int n
= goto_map
[symbol
+ 1];
832 state_number_t default_state
= (state_number_t
) -1;
836 return (state_number_t
) -1;
838 for (s
= 0; s
< nstates
; s
++)
841 for (i
= m
; i
< n
; i
++)
842 state_count
[to_state
[i
]]++;
844 for (s
= 0; s
< nstates
; s
++)
845 if (state_count
[s
] > max
)
847 max
= state_count
[s
];
851 return default_state
;
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. |
860 | The YYDEFGOTO table is output now. The detailed info is saved for |
861 | putting into YYTABLE later. |
862 `-------------------------------------------------------------------*/
868 state_number_t
*yydefgoto
= XMALLOC (state_number_t
, nsyms
- ntokens
);
870 state_count
= XCALLOC (short, nstates
);
871 for (i
= ntokens
; i
< nsyms
; ++i
)
873 state_number_t default_state
= default_goto (i
);
874 save_column (i
, default_state
);
875 yydefgoto
[i
- ntokens
] = default_state
;
878 muscle_insert_state_number_table ("defgoto", yydefgoto
,
879 yydefgoto
[0], 1, nsyms
- ntokens
);
885 /* The next few functions decide how to pack the actions and gotos
886 information into yytable. */
895 for (i
= 0; i
< nvectors
; i
++)
901 int j
= nentries
- 1;
903 while (j
>= 0 && (width
[order
[j
]] < w
))
906 while (j
>= 0 && (width
[order
[j
]] == w
) && (tally
[order
[j
]] < t
))
909 for (k
= nentries
- 1; k
> j
; k
--)
910 order
[k
+ 1] = order
[k
];
919 matching_state (int vector
)
921 int i
= order
[vector
];
926 if (i
>= (int) nstates
)
932 for (prev
= vector
- 1; prev
>= 0; prev
--)
938 if (width
[j
] != w
|| tally
[j
] != t
)
941 for (k
= 0; match
&& k
< t
; k
++)
942 if (tos
[j
][k
] != tos
[i
][k
] || froms
[j
][k
] != froms
[i
][k
])
954 pack_vector (int vector
)
956 int i
= order
[vector
];
960 short *from
= froms
[i
];
962 unsigned int *conflict_to
= conflict_tos
[i
];
966 for (j
= lowzero
- from
[0]; j
< (int) table_size
; j
++)
971 for (k
= 0; ok
&& k
< t
; k
++)
973 loc
= j
+ state_number_as_int (from
[k
]);
974 if (loc
> (int) table_size
)
981 for (k
= 0; ok
&& k
< vector
; k
++)
987 for (k
= 0; k
< t
; k
++)
989 loc
= j
+ state_number_as_int (from
[k
]);
990 table
[loc
] = state_number_as_int (to
[k
]);
991 if (glr_parser
&& conflict_to
!= NULL
)
992 conflict_table
[loc
] = conflict_to
[k
];
993 check
[loc
] = state_number_as_int (from
[k
]);
996 while (table
[lowzero
] != 0)
1005 #define pack_vector_succeeded 0
1006 assert (pack_vector_succeeded
);
1018 base
= XCALLOC (short, nvectors
);
1019 pos
= XCALLOC (short, nentries
);
1020 table
= XCALLOC (short, table_size
);
1022 conflict_table
= XCALLOC (unsigned int, table_size
);
1023 check
= XCALLOC (short, table_size
);
1028 for (i
= 0; i
< nvectors
; i
++)
1031 for (i
= 0; i
< (int) table_size
; i
++)
1034 for (i
= 0; i
< nentries
; i
++)
1036 state
= matching_state (i
);
1039 place
= pack_vector (i
);
1041 place
= base
[state
];
1044 base
[order
[i
]] = place
;
1047 for (i
= 0; i
< nvectors
; i
++)
1051 XFREE (conflict_tos
[i
]);
1056 free (conflict_tos
);
1060 /* the following functions output yytable, yycheck, yyconflp, yyconfl,
1061 and the vectors whose elements index the portion starts */
1067 muscle_insert_short_table ("pact", base
,
1068 base
[0], 1, nstates
);
1071 muscle_insert_short_table ("pgoto", base
,
1072 base
[nstates
], nstates
+ 1, nvectors
);
1080 muscle_insert_short_table ("table", table
,
1081 table
[0], 1, high
+ 1);
1087 output_conflicts (void)
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. */
1098 muscle_insert_unsigned_int_table ("conflict_list_heads", conflict_table
,
1099 conflict_table
[0], 1, high
+1);
1100 muscle_insert_unsigned_int_table ("conflicting_rules", conflict_list
,
1101 conflict_list
[0], 1, conflict_list_cnt
);
1103 XFREE (conflict_table
);
1104 XFREE (conflict_list
);
1111 muscle_insert_short_table ("check", check
,
1112 check
[0], 1, high
+ 1);
1116 /*-----------------------------------------------------------------.
1117 | Compute and output yydefact, yydefgoto, yypact, yypgoto, yytable |
1119 `-----------------------------------------------------------------*/
1122 prepare_actions (void)
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
1127 assert (sizeof (nvectors
) >= sizeof (nstates
));
1128 assert (sizeof (nvectors
) >= sizeof (nvars
));
1130 nvectors
= state_number_as_int (nstates
) + nvars
;
1132 froms
= XCALLOC (short *, nvectors
);
1133 tos
= XCALLOC (short *, nvectors
);
1134 conflict_tos
= XCALLOC (unsigned int *, nvectors
);
1135 tally
= XCALLOC (short, nvectors
);
1136 width
= XCALLOC (short, nvectors
);
1143 XFREE (goto_map
+ ntokens
);
1147 order
= XCALLOC (short, nvectors
);
1157 output_conflicts ();
1163 /*---------------------------.
1164 | Call the skeleton parser. |
1165 `---------------------------*/
1168 output_skeleton (void)
1170 /* Store the definition of all the muscles. */
1171 const char *tempdir
= getenv ("TMPDIR");
1172 char *tempfile
= NULL
;
1176 if (tempdir
== NULL
)
1177 tempdir
= DEFAULT_TMPDIR
;
1178 tempfile
= xmalloc (strlen (tempdir
) + 11);
1179 sprintf (tempfile
, "%s/bsnXXXXXX", tempdir
);
1180 fd
= mkstemp (tempfile
);
1182 error (EXIT_FAILURE
, errno
, "%s", tempfile
);
1184 out
= fdopen (fd
, "w");
1186 error (EXIT_FAILURE
, errno
, "%s", tempfile
);
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
);
1193 actions_output (out
);
1194 merger_output (out
);
1195 token_definitions_output (out
);
1196 symbol_destructors_output (out
);
1197 symbol_printers_output (out
);
1199 muscles_m4_output (out
);
1201 fputs ("m4_wrap([m4_divert_pop(0)])\n", out
);
1202 fputs ("m4_divert_push(0)dnl\n", out
);
1205 m4_invoke (tempfile
);
1207 /* If `debugging', keep this file alive. */
1217 MUSCLE_INSERT_INT ("last", high
);
1218 MUSCLE_INSERT_INT ("flag", SHRT_MIN
);
1219 MUSCLE_INSERT_INT ("pure", pure_parser
);
1220 MUSCLE_INSERT_INT ("nsym", nsyms
);
1221 MUSCLE_INSERT_INT ("debug", debug_flag
);
1222 MUSCLE_INSERT_INT ("final", final_state
->number
);
1223 MUSCLE_INSERT_INT ("undef_token_number", undeftoken
->number
);
1224 MUSCLE_INSERT_INT ("user_token_number_max", max_user_token_number
);
1225 MUSCLE_INSERT_INT ("error_verbose", error_verbose
);
1226 MUSCLE_INSERT_STRING ("prefix", spec_name_prefix
? spec_name_prefix
: "yy");
1228 /* FIXME: This is wrong: the muscles should decide whether they hold
1229 a copy or not, but the situation is too obscure currently. */
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
);
1235 MUSCLE_INSERT_INT ("nnts", nvars
);
1236 MUSCLE_INSERT_INT ("nrules", nrules
);
1237 MUSCLE_INSERT_INT ("nstates", nstates
);
1238 MUSCLE_INSERT_INT ("ntokens", ntokens
);
1240 MUSCLE_INSERT_INT ("locations_flag", locations_flag
);
1241 MUSCLE_INSERT_INT ("defines_flag", defines_flag
);
1243 /* Copy definitions in directive. */
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
));
1249 /* Find the right skeleton file. */
1255 skeleton
= "yacc.c";
1258 /* Parse the skeleton file and output the needed parsers. */
1259 muscle_insert ("skeleton", skeleton
);
1263 /*----------------------------------------------------------.
1264 | Output the parsing tables and the parser code to ftable. |
1265 `----------------------------------------------------------*/
1270 obstack_init (&format_obstack
);
1279 /* Process the selected skeleton file. */
1282 obstack_free (&format_obstack
, NULL
);
1283 obstack_free (&pre_prologue_obstack
, NULL
);
1284 obstack_free (&post_prologue_obstack
, NULL
);