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.
25 YYTRANSLATE = vector mapping yylex's token numbers into bison's
28 YYTNAME = vector of string-names indexed by bison token number.
30 YYTOKNUM = vector of yylex token numbers corresponding to entries
33 YYRLINE = vector of line-numbers of all rules. For yydebug
36 YYRHS = vector of items of all rules. This is exactly what RITEMS
37 contains. For yydebug and for semantic parser.
39 YYPRHS[R] = index in YYRHS of first item for rule R.
41 YYR1[R] = symbol number of symbol that rule R derives.
43 YYR2[R] = number of symbols composing right hand side of rule R.
45 YYSTOS[S] = the symbol number of the symbol that leads to state S.
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
51 YYDEFGOTO[I] = default state to go to after a reduction of a rule
52 that generates variable NTOKENS + I, except when YYTABLE specifies
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
59 If the value in YYTABLE is positive, we shift the token and go to
62 If the value is negative, it is minus a rule number to reduce by.
64 If the value is zero, the default action from YYDEFACT[S] is used.
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.
72 YYTABLE = a vector filled with portions for different uses, found
73 via YYPACT and YYPGOTO.
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
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.
85 YYFINAL = the state number of the termination state. YYFLAG = most
86 negative short int. Used to flag ?? */
101 #include "conflicts.h"
102 #include "muscle_tab.h"
104 /* From src/scan-skel.l. */
105 void m4_invoke
PARAMS ((const char *definitions
));
109 static state_number_t
**froms
= NULL
;
110 static state_number_t
**tos
= NULL
;
111 static unsigned int **conflict_tos
= NULL
;
112 static short *tally
= NULL
;
113 static short *width
= NULL
;
114 static short *actrow
= NULL
;
115 static short *conflrow
= NULL
;
116 static short *state_count
= NULL
;
117 static short *order
= NULL
;
118 static short *base
= NULL
;
119 static short *pos
= NULL
;
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
;
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
;
130 static short *table
= NULL
;
131 static short *check
= NULL
;
135 static struct obstack format_obstack
;
137 int error_verbose
= 0;
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 `----------------------------------------------------------------*/
147 table_grow (size_t desired
)
149 size_t old_size
= table_size
;
151 while (table_size
<= desired
)
155 fprintf (stderr
, "growing table and check from: %d to %d\n",
156 old_size
, table_size
);
158 table
= XREALLOC (table
, short, table_size
);
159 check
= XREALLOC (check
, short, table_size
);
161 conflict_table
= XREALLOC (conflict_table
, unsigned int, table_size
);
163 for (/* Nothing. */; old_size
< table_size
; ++old_size
)
166 check
[old_size
] = -1;
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 |
176 `-------------------------------------------------------------------*/
179 #define GENERATE_MUSCLE_INSERT_TABLE(Name, Type) \
182 Name (const char *name, \
192 obstack_fgrow1 (&format_obstack, "%6d", first); \
193 for (i = begin; i < end; ++i) \
195 obstack_1grow (&format_obstack, ','); \
198 obstack_sgrow (&format_obstack, "\n "); \
203 obstack_fgrow1 (&format_obstack, "%6d", table_data[i]); \
204 if (table_data[i] > max) \
205 max = table_data[i]; \
207 obstack_1grow (&format_obstack, 0); \
208 muscle_insert (name, obstack_finish (&format_obstack)); \
210 /* Build `NAME_max' in the obstack. */ \
211 obstack_fgrow1 (&format_obstack, "%s_max", name); \
212 obstack_1grow (&format_obstack, 0); \
213 MUSCLE_INSERT_LONG_INT (obstack_finish (&format_obstack), \
217 GENERATE_MUSCLE_INSERT_TABLE(muscle_insert_unsigned_int_table
, unsigned int)
218 GENERATE_MUSCLE_INSERT_TABLE(muscle_insert_short_table
, short)
219 GENERATE_MUSCLE_INSERT_TABLE(muscle_insert_symbol_number_table
, symbol_number_t
)
220 GENERATE_MUSCLE_INSERT_TABLE(muscle_insert_item_number_table
, item_number_t
)
221 GENERATE_MUSCLE_INSERT_TABLE(muscle_insert_state_number_table
, state_number_t
)
224 /*-----------------------------------------------------------------.
225 | Prepare the muscles related to the tokens: translate, tname, and |
227 `-----------------------------------------------------------------*/
230 prepare_tokens (void)
232 muscle_insert_symbol_number_table ("translate",
234 0, 1, max_user_token_number
+ 1);
239 for (i
= 0; i
< nsyms
; i
++)
241 /* Be sure not to use twice the same QUOTEARG slot:
242 SYMBOL_TAG_GET uses slot 0. */
244 quotearg_n_style (1, c_quoting_style
,
246 /* Width of the next token, including the two quotes, the coma
248 int strsize
= strlen (cp
) + 2;
250 if (j
+ strsize
> 75)
252 obstack_sgrow (&format_obstack
, "\n ");
256 obstack_sgrow (&format_obstack
, cp
);
257 obstack_sgrow (&format_obstack
, ", ");
260 /* Add a NULL entry to list of tokens (well, 0, as NULL might not be
262 obstack_sgrow (&format_obstack
, "0");
264 /* Finish table and store. */
265 obstack_1grow (&format_obstack
, 0);
266 muscle_insert ("tname", obstack_finish (&format_obstack
));
269 /* Output YYTOKNUM. */
272 short *values
= XCALLOC (short, ntokens
+ 1);
273 for (i
= 0; i
< ntokens
+ 1; ++i
)
274 values
[i
] = symbols
[i
]->user_token_number
;
275 muscle_insert_short_table ("toknum", values
,
282 /*-------------------------------------------------------------.
283 | Prepare the muscles related to the rules: rhs, prhs, r1, r2, |
284 | rline, dprec, merger |
285 `-------------------------------------------------------------*/
292 item_number_t
*rhs
= XMALLOC (item_number_t
, nritems
);
293 unsigned int *prhs
= XMALLOC (unsigned int, nrules
+ 1);
294 unsigned int *rline
= XMALLOC (unsigned int, nrules
+ 1);
295 symbol_number_t
*r1
= XMALLOC (symbol_number_t
, nrules
+ 1);
296 unsigned int *r2
= XMALLOC (unsigned int, nrules
+ 1);
297 short *dprec
= XMALLOC (short, nrules
+ 1);
298 short *merger
= XMALLOC (short, nrules
+ 1);
300 for (r
= 1; r
< nrules
+ 1; ++r
)
302 item_number_t
*rhsp
= NULL
;
303 /* Index of rule R in RHS. */
305 /* RHS of the rule R. */
306 for (rhsp
= rules
[r
].rhs
; *rhsp
>= 0; ++rhsp
)
308 /* LHS of the rule R. */
309 r1
[r
] = rules
[r
].lhs
->number
;
310 /* Length of rule R's RHS. */
312 /* Separator in RHS. */
314 /* Line where rule was defined. */
315 rline
[r
] = rules
[r
].location
.first_line
;
316 /* Dynamic precedence (GLR) */
317 dprec
[r
] = rules
[r
].dprec
;
318 /* Merger-function index (GLR) */
319 merger
[r
] = rules
[r
].merger
;
321 assert (i
== nritems
);
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);
326 muscle_insert_symbol_number_table ("r1", r1
, 0, 1, nrules
+ 1);
327 muscle_insert_unsigned_int_table ("r2", r2
, 0, 1, nrules
+ 1);
328 muscle_insert_short_table ("dprec", dprec
, 0, 1, nrules
+ 1);
329 muscle_insert_short_table ("merger", merger
, 0, 1, nrules
+ 1);
340 /*--------------------------------------------.
341 | Prepare the muscles related to the states. |
342 `--------------------------------------------*/
345 prepare_states (void)
348 symbol_number_t
*values
=
349 (symbol_number_t
*) alloca (sizeof (symbol_number_t
) * nstates
);
350 for (i
= 0; i
< nstates
; ++i
)
351 values
[i
] = states
[i
]->accessing_symbol
;
352 muscle_insert_symbol_number_table ("stos", values
,
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 `-------------------------------------------------------------------*/
368 conflict_row (state_t
*state
)
375 for (j
= 0; j
< ntokens
; j
+= 1)
378 conflrow
[j
] = conflict_list_cnt
;
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
)
386 assert (conflict_list_free
> 0);
387 conflict_list
[conflict_list_cnt
]
388 = state
->lookaheads_rule
[i
]->number
;
389 conflict_list_cnt
+= 1;
390 conflict_list_free
-= 1;
393 /* Leave a 0 at the end */
394 assert (conflict_list_free
> 0);
395 conflict_list_cnt
+= 1;
396 conflict_list_free
-= 1;
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 |
407 | SHRT_MIN, a very negative number, means this situation is an |
408 | error. The parser recognizes this value specially. |
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. |
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. |
418 `------------------------------------------------------------------*/
421 action_row (state_t
*state
)
424 rule_number_t default_rule
= 0;
425 reductions_t
*redp
= state
->reductions
;
426 transitions_t
*transitions
= state
->transitions
;
427 errs_t
*errp
= state
->errs
;
428 /* set nonzero to inhibit having any default reduction */
432 for (i
= 0; i
< ntokens
; i
++)
433 actrow
[i
] = conflrow
[i
] = 0;
438 bitset_iterator biter
;
439 /* loop over all the rules available here which require
441 for (i
= state
->nlookaheads
- 1; i
>= 0; --i
)
442 /* and find each token which the rule finds acceptable
444 BITSET_FOR_EACH (biter
, state
->lookaheads
[i
], j
, 0)
446 /* and record this rule as the rule to use if that
449 conflicted
= conflrow
[j
] = 1;
450 actrow
[j
] = -state
->lookaheads_rule
[i
]->number
;
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
457 for (i
= 0; i
< transitions
->num
&& TRANSITION_IS_SHIFT (transitions
, i
); i
++)
458 if (!TRANSITION_IS_DISABLED (transitions
, i
))
460 symbol_number_t symbol
= TRANSITION_SYMBOL (transitions
, i
);
461 state_number_t shift_state
= transitions
->states
[i
];
463 if (actrow
[symbol
] != 0)
464 conflicted
= conflrow
[symbol
] = 1;
465 actrow
[symbol
] = state_number_as_int (shift_state
);
467 /* Do not use any default reduction if there is a shift for
469 if (symbol
== errtoken
->number
)
473 /* See which tokens are an explicit error in this state (due to
474 %nonassoc). For them, record SHRT_MIN as the action. */
475 for (i
= 0; i
< errp
->num
; i
++)
477 symbol_number_t symbol
= errp
->symbols
[i
];
478 actrow
[symbol
] = SHRT_MIN
;
481 /* Now find the most common reduction and make it the default action
484 if (redp
->num
>= 1 && !nodefault
)
486 if (state
->consistent
)
487 default_rule
= redp
->rules
[0];
491 for (i
= 0; i
< state
->nlookaheads
; i
++)
494 rule_number_t rule
= state
->lookaheads_rule
[i
]->number
;
497 for (j
= 0; j
< ntokens
; j
++)
498 if (actrow
[j
] == -rule
)
508 /* GLR parsers need space for conflict lists, so we can't
509 default conflicted entries. For non-conflicted entries
510 or as long as we are not building a GLR parser,
511 actions that match the default are replaced with zero,
512 which means "use the default". */
517 for (j
= 0; j
< ntokens
; j
++)
518 if (actrow
[j
] == -default_rule
519 && ! (glr_parser
&& conflrow
[j
]))
525 /* If have no default rule, the default is an error.
526 So replace any action which says "error" with "use default". */
528 if (default_rule
== 0)
529 for (i
= 0; i
< ntokens
; i
++)
530 if (actrow
[i
] == SHRT_MIN
)
534 conflict_row (state
);
541 save_row (state_number_t state
)
548 unsigned int *sp3
= NULL
;
551 for (i
= 0; i
< ntokens
; i
++)
558 froms
[state
] = sp1
= sp
= XCALLOC (short, count
);
559 tos
[state
] = sp2
= XCALLOC (short, count
);
561 conflict_tos
[state
] = sp3
= XCALLOC (unsigned int, count
);
563 conflict_tos
[state
] = NULL
;
565 for (i
= 0; i
< ntokens
; i
++)
571 *sp3
++ = conflrow
[i
];
574 tally
[state
] = count
;
575 width
[state
] = sp1
[-1] - sp
[0] + 1;
579 /*------------------------------------------------------------------.
580 | Figure out the actions for the specified state, indexed by |
581 | lookahead token type. |
583 | The YYDEFACT table is output now. The detailed info is saved for |
584 | putting into YYTABLE later. |
585 `------------------------------------------------------------------*/
591 int nconflict
= conflicts_total_count ();
593 short *yydefact
= XCALLOC (short, nstates
);
595 actrow
= XCALLOC (short, ntokens
);
597 conflrow
= XCALLOC (short, ntokens
);
600 conflict_list
= XCALLOC (unsigned int, 1 + 2 * nconflict
);
601 conflict_list_free
= 2 * nconflict
;
602 conflict_list_cnt
= 1;
605 conflict_list_free
= conflict_list_cnt
= 0;
607 for (i
= 0; i
< nstates
; ++i
)
609 yydefact
[i
] = action_row (states
[i
]);
613 muscle_insert_short_table ("defact", yydefact
,
614 yydefact
[0], 1, nstates
);
621 /*-----------------------------.
622 | Output the actions to OOUT. |
623 `-----------------------------*/
626 actions_output (FILE *out
)
630 fputs ("m4_define([b4_actions], \n[[", out
);
631 for (r
= 1; r
< nrules
+ 1; ++r
)
634 fprintf (out
, " case %d:\n", r
);
637 fprintf (out
, muscle_find ("linef"),
638 rules
[r
].action_location
.first_line
,
639 quotearg_style (c_quoting_style
,
640 muscle_find ("filename")));
641 fprintf (out
, " %s\n break;\n\n",
644 fputs ("]])\n\n", out
);
647 /*--------------------------------------.
648 | Output the merge functions to OUT. |
649 `--------------------------------------*/
652 merger_output (FILE *out
)
657 fputs ("m4_define([b4_mergers], \n[[", out
);
658 for (n
= 1, p
= merge_functions
; p
!= NULL
; n
+= 1, p
= p
->next
)
660 if (p
->type
[0] == '\0')
661 fprintf (out
, " case %d: yyval = %s (*yy0, *yy1); break;\n",
664 fprintf (out
, " case %d: yyval.%s = %s (*yy0, *yy1); break;\n",
665 n
, p
->type
, p
->name
);
667 fputs ("]])\n\n", out
);
670 /*---------------------------------------.
671 | Output the tokens definition to OOUT. |
672 `---------------------------------------*/
675 token_definitions_output (FILE *out
)
680 fputs ("m4_define([b4_tokens], \n[", out
);
681 for (i
= 0; i
< ntokens
; ++i
)
683 symbol_t
*symbol
= symbols
[i
];
684 int number
= symbol
->user_token_number
;
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
689 assert (number
!= USER_NUMBER_ALIAS
);
691 /* Skip error token. */
692 if (symbol
== errtoken
)
695 /* If this string has an alias, then it is necessarily the alias
696 which is to be output. */
698 symbol
= symbol
->alias
;
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] == '\"')
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
, '$'))
711 fprintf (out
, "%s[[[%s]], [%d]]",
712 first
? "" : ",\n", symbol
->tag
, number
);
716 fputs ("])\n\n", out
);
720 /*----------------------------------------.
721 | Output the symbol destructors to OOUT. |
722 `----------------------------------------*/
725 symbol_destructors_output (FILE *out
)
730 fputs ("m4_define([b4_symbol_destructors], \n[", out
);
731 for (i
= 0; i
< nsyms
; ++i
)
732 if (symbols
[i
]->destructor
)
734 symbol_t
*symbol
= symbols
[i
];
737 Symbol-name, Symbol-number,
738 destructor, typename. */
739 fprintf (out
, "%s[[[%s]], [[%d]], [[%s]], [[%d]], [[%s]], [[%s]]]",
741 infile
, symbol
->destructor_location
.first_line
,
749 fputs ("])\n\n", out
);
753 /*-------------------------------------.
754 | Output the symbol printers to OOUT. |
755 `-------------------------------------*/
758 symbol_printers_output (FILE *out
)
763 fputs ("m4_define([b4_symbol_printers], \n[", out
);
764 for (i
= 0; i
< nsyms
; ++i
)
765 if (symbols
[i
]->destructor
)
767 symbol_t
*symbol
= symbols
[i
];
770 Symbol-name, Symbol-number,
771 destructor, typename. */
772 fprintf (out
, "%s[[[%s]], [[%d]], [[%s]], [[%d]], [[%s]], [[%s]]]",
774 infile
, symbol
->printer_location
.first_line
,
782 fputs ("])\n\n", out
);
787 save_column (symbol_number_t symbol
, state_number_t default_state
)
794 int symno
= symbol
- ntokens
+ state_number_as_int (nstates
);
796 goto_number_t begin
= goto_map
[symbol
];
797 goto_number_t end
= goto_map
[symbol
+ 1];
800 for (i
= begin
; i
< end
; i
++)
801 if (to_state
[i
] != default_state
)
807 froms
[symno
] = sp1
= sp
= XCALLOC (short, count
);
808 tos
[symno
] = sp2
= XCALLOC (short, count
);
810 for (i
= begin
; i
< end
; i
++)
811 if (to_state
[i
] != default_state
)
813 *sp1
++ = from_state
[i
];
814 *sp2
++ = to_state
[i
];
817 tally
[symno
] = count
;
818 width
[symno
] = sp1
[-1] - sp
[0] + 1;
822 static state_number_t
823 default_goto (symbol_number_t symbol
)
827 goto_number_t m
= goto_map
[symbol
];
828 goto_number_t n
= goto_map
[symbol
+ 1];
829 state_number_t default_state
= (state_number_t
) -1;
833 return (state_number_t
) -1;
835 for (s
= 0; s
< nstates
; s
++)
838 for (i
= m
; i
< n
; i
++)
839 state_count
[to_state
[i
]]++;
841 for (s
= 0; s
< nstates
; s
++)
842 if (state_count
[s
] > max
)
844 max
= state_count
[s
];
848 return default_state
;
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. |
857 | The YYDEFGOTO table is output now. The detailed info is saved for |
858 | putting into YYTABLE later. |
859 `-------------------------------------------------------------------*/
865 state_number_t
*yydefgoto
= XMALLOC (state_number_t
, nsyms
- ntokens
);
867 state_count
= XCALLOC (short, nstates
);
868 for (i
= ntokens
; i
< nsyms
; ++i
)
870 state_number_t default_state
= default_goto (i
);
871 save_column (i
, default_state
);
872 yydefgoto
[i
- ntokens
] = default_state
;
875 muscle_insert_state_number_table ("defgoto", yydefgoto
,
876 yydefgoto
[0], 1, nsyms
- ntokens
);
882 /* The next few functions decide how to pack the actions and gotos
883 information into yytable. */
892 for (i
= 0; i
< nvectors
; i
++)
898 int j
= nentries
- 1;
900 while (j
>= 0 && (width
[order
[j
]] < w
))
903 while (j
>= 0 && (width
[order
[j
]] == w
) && (tally
[order
[j
]] < t
))
906 for (k
= nentries
- 1; k
> j
; k
--)
907 order
[k
+ 1] = order
[k
];
916 matching_state (int vector
)
918 int i
= order
[vector
];
923 if (i
>= (int) nstates
)
929 for (prev
= vector
- 1; prev
>= 0; prev
--)
935 if (width
[j
] != w
|| tally
[j
] != t
)
938 for (k
= 0; match
&& k
< t
; k
++)
939 if (tos
[j
][k
] != tos
[i
][k
] || froms
[j
][k
] != froms
[i
][k
])
951 pack_vector (int vector
)
953 int i
= order
[vector
];
957 short *from
= froms
[i
];
959 unsigned int *conflict_to
= conflict_tos
[i
];
963 for (j
= lowzero
- from
[0]; j
< (int) table_size
; j
++)
968 for (k
= 0; ok
&& k
< t
; k
++)
970 loc
= j
+ state_number_as_int (from
[k
]);
971 if (loc
> (int) table_size
)
978 for (k
= 0; ok
&& k
< vector
; k
++)
984 for (k
= 0; k
< t
; k
++)
986 loc
= j
+ state_number_as_int (from
[k
]);
987 table
[loc
] = state_number_as_int (to
[k
]);
988 if (glr_parser
&& conflict_to
!= NULL
)
989 conflict_table
[loc
] = conflict_to
[k
];
990 check
[loc
] = state_number_as_int (from
[k
]);
993 while (table
[lowzero
] != 0)
1002 #define pack_vector_succeeded 0
1003 assert (pack_vector_succeeded
);
1015 base
= XCALLOC (short, nvectors
);
1016 pos
= XCALLOC (short, nentries
);
1017 table
= XCALLOC (short, table_size
);
1019 conflict_table
= XCALLOC (unsigned int, table_size
);
1020 check
= XCALLOC (short, table_size
);
1025 for (i
= 0; i
< nvectors
; i
++)
1028 for (i
= 0; i
< (int) table_size
; i
++)
1031 for (i
= 0; i
< nentries
; i
++)
1033 state
= matching_state (i
);
1036 place
= pack_vector (i
);
1038 place
= base
[state
];
1041 base
[order
[i
]] = place
;
1044 for (i
= 0; i
< nvectors
; i
++)
1048 XFREE (conflict_tos
[i
]);
1053 free (conflict_tos
);
1057 /* the following functions output yytable, yycheck, yyconflp, yyconfl,
1058 and the vectors whose elements index the portion starts */
1064 muscle_insert_short_table ("pact", base
,
1065 base
[0], 1, nstates
);
1068 muscle_insert_short_table ("pgoto", base
,
1069 base
[nstates
], nstates
+ 1, nvectors
);
1077 muscle_insert_short_table ("table", table
,
1078 table
[0], 1, high
+ 1);
1084 output_conflicts (void)
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. */
1095 muscle_insert_unsigned_int_table ("conflict_list_heads", conflict_table
,
1096 conflict_table
[0], 1, high
+1);
1097 muscle_insert_unsigned_int_table ("conflicting_rules", conflict_list
,
1098 conflict_list
[0], 1, conflict_list_cnt
);
1100 XFREE (conflict_table
);
1101 XFREE (conflict_list
);
1108 muscle_insert_short_table ("check", check
,
1109 check
[0], 1, high
+ 1);
1113 /*-----------------------------------------------------------------.
1114 | Compute and output yydefact, yydefgoto, yypact, yypgoto, yytable |
1116 `-----------------------------------------------------------------*/
1119 prepare_actions (void)
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
1124 assert (sizeof (nvectors
) >= sizeof (nstates
));
1125 assert (sizeof (nvectors
) >= sizeof (nvars
));
1127 nvectors
= state_number_as_int (nstates
) + nvars
;
1129 froms
= XCALLOC (short *, nvectors
);
1130 tos
= XCALLOC (short *, nvectors
);
1131 conflict_tos
= XCALLOC (unsigned int *, nvectors
);
1132 tally
= XCALLOC (short, nvectors
);
1133 width
= XCALLOC (short, nvectors
);
1140 XFREE (goto_map
+ ntokens
);
1144 order
= XCALLOC (short, nvectors
);
1154 output_conflicts ();
1160 /*---------------------------.
1161 | Call the skeleton parser. |
1162 `---------------------------*/
1165 output_skeleton (void)
1167 /* Store the definition of all the muscles. */
1168 const char *tempdir
= getenv ("TMPDIR");
1169 char *tempfile
= NULL
;
1173 if (tempdir
== NULL
)
1174 tempdir
= DEFAULT_TMPDIR
;
1175 tempfile
= xmalloc (strlen (tempdir
) + 11);
1176 sprintf (tempfile
, "%s/bsnXXXXXX", tempdir
);
1177 fd
= mkstemp (tempfile
);
1179 error (EXIT_FAILURE
, errno
, "%s", tempfile
);
1181 out
= fdopen (fd
, "w");
1183 error (EXIT_FAILURE
, errno
, "%s", tempfile
);
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
);
1190 actions_output (out
);
1191 merger_output (out
);
1192 token_definitions_output (out
);
1193 symbol_destructors_output (out
);
1194 symbol_printers_output (out
);
1196 muscles_m4_output (out
);
1198 fputs ("m4_wrap([m4_divert_pop(0)])\n", out
);
1199 fputs ("m4_divert_push(0)dnl\n", out
);
1202 m4_invoke (tempfile
);
1204 /* If `debugging', keep this file alive. */
1215 MUSCLE_INSERT_INT ("locations_flag", locations_flag
);
1216 MUSCLE_INSERT_INT ("defines_flag", defines_flag
);
1217 MUSCLE_INSERT_INT ("error_verbose", error_verbose
);
1218 MUSCLE_INSERT_INT ("pure", pure_parser
);
1219 MUSCLE_INSERT_INT ("debug", debug_flag
);
1221 /* FIXME: This is wrong: the muscles should decide whether they hold
1222 a copy or not, but the situation is too obscure currently. */
1223 MUSCLE_INSERT_STRING ("prefix", spec_name_prefix
? spec_name_prefix
: "yy");
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
);
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
);
1236 MUSCLE_INSERT_INT ("rules_number", nrules
);
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
);
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
));
1250 /* Find the right skeleton file. */
1256 skeleton
= "yacc.c";
1259 /* Parse the skeleton file and output the needed parsers. */
1260 muscle_insert ("skeleton", skeleton
);
1264 /*----------------------------------------------------------.
1265 | Output the parsing tables and the parser code to ftable. |
1266 `----------------------------------------------------------*/
1271 obstack_init (&format_obstack
);
1280 /* Process the selected skeleton file. */
1283 obstack_free (&format_obstack
, NULL
);
1284 obstack_free (&pre_prologue_obstack
, NULL
);
1285 obstack_free (&post_prologue_obstack
, NULL
);