1 /* Input parser for Bison
2 Copyright (C) 1984, 1986, 1989, 1992, 1998, 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
8 it 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,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU 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
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
33 #include "conflicts.h"
34 #include "muscle_tab.h"
36 static symbol_list_t
*grammar
= NULL
;
37 static int start_flag
= 0;
38 merger_list
*merge_functions
;
40 /* Nonzero if %union has been seen. */
43 /*-----------------------.
44 | Set the start symbol. |
45 `-----------------------*/
48 grammar_start_symbol_set (symbol_t
*s
, location_t l
)
51 complain_at (l
, _("multiple %s declarations"), "%start");
56 startsymbol_location
= l
;
61 /*----------------------------------------------------------------.
62 | There are two prologues: one before %union, one after. Augment |
64 `----------------------------------------------------------------*/
67 prologue_augment (const char *prologue
, location_t location
)
69 struct obstack
*oout
=
70 !typed
? &pre_prologue_obstack
: &post_prologue_obstack
;
72 obstack_fgrow1 (oout
, "]b4_syncline([[%d]], [[",
74 MUSCLE_OBSTACK_SGROW (oout
, quotearg_style (c_quoting_style
, location
.file
));
75 obstack_sgrow (oout
, "]])[\n");
76 obstack_sgrow (oout
, prologue
);
82 /*----------------------.
83 | Handle the epilogue. |
84 `----------------------*/
87 epilogue_augment (const char *epilogue
, location_t location
)
89 char *extension
= NULL
;
90 obstack_fgrow1 (&muscle_obstack
, "]b4_syncline([[%d]], [[",
92 MUSCLE_OBSTACK_SGROW (&muscle_obstack
,
93 quotearg_style (c_quoting_style
, location
.file
));
94 obstack_sgrow (&muscle_obstack
, "]])[\n");
95 obstack_sgrow (&muscle_obstack
, epilogue
);
96 obstack_1grow (&muscle_obstack
, 0);
97 extension
= obstack_finish (&muscle_obstack
);
98 muscle_grow ("epilogue", extension
, "");
99 obstack_free (&muscle_obstack
, extension
);
105 /*-------------------------------------------------------------------.
106 | Return the merger index for a merging function named NAME, whose |
107 | arguments have type TYPE. Records the function, if new, in |
109 `-------------------------------------------------------------------*/
112 get_merge_function (struniq_t name
, struniq_t type
, location_t loc
)
122 type
= struniq_new ("");
124 head
.next
= merge_functions
;
125 for (syms
= &head
, n
= 1; syms
->next
!= NULL
; syms
= syms
->next
, n
+= 1)
126 if (STRUNIQ_EQ (name
, syms
->next
->name
))
128 if (syms
->next
== NULL
)
130 syms
->next
= XMALLOC (merger_list
, 1);
131 syms
->next
->name
= struniq_new (name
);
132 syms
->next
->type
= struniq_new (type
);
133 syms
->next
->next
= NULL
;
134 merge_functions
= head
.next
;
136 else if (!STRUNIQ_EQ (type
, syms
->next
->type
))
137 warn_at (loc
, _("result type clash on merge function %s: <%s> != <%s>"),
138 name
, type
, syms
->next
->type
);
142 /*--------------------------------------.
143 | Free all merge-function definitions. |
144 `--------------------------------------*/
147 free_merger_functions (void)
152 L0
= merge_functions
;
155 merger_list
*L1
= L0
->next
;
162 /*-------------------------------------------------------------------.
163 | Parse the input grammar into a one symbol_list_t structure. Each |
164 | rule is represented by a sequence of symbols: the left hand side |
165 | followed by the contents of the right hand side, followed by a |
166 | null pointer instead of a symbol to terminate the rule. The next |
167 | symbol is the lhs of the following rule. |
169 | All actions are copied out, labelled by the rule number they apply |
172 | Bison used to allow some %directives in the rules sections, but |
173 | this is no longer consider appropriate: (i) the documented grammar |
174 | doesn't claim it, (ii), it would promote bad style, (iii), error |
175 | recovery for %directives consists in skipping the junk until a `%' |
176 | is seen and helrp synchronizing. This scheme is definitely wrong |
177 | in the rules section. |
178 `-------------------------------------------------------------------*/
180 /* The (currently) last symbol of GRAMMAR. */
181 symbol_list_t
*grammar_end
= NULL
;
183 /* Append S to the GRAMMAR. */
185 grammar_symbol_append (symbol_t
*symbol
, location_t location
)
187 symbol_list_t
*p
= symbol_list_new (symbol
, location
);
190 grammar_end
->next
= p
;
197 /* The rule currently being defined, and the previous rule.
198 CURRENT_RULE points to the first LHS of the current rule, while
199 PREVIOUS_RULE_END points to the *end* of the previous rule (NULL). */
200 symbol_list_t
*current_rule
= NULL
;
201 symbol_list_t
*previous_rule_end
= NULL
;
204 /*----------------------------------------------.
205 | Create a new rule for LHS in to the GRAMMAR. |
206 `----------------------------------------------*/
209 grammar_rule_begin (symbol_t
*lhs
, location_t location
)
214 startsymbol_location
= location
;
218 /* Start a new rule and record its lhs. */
222 previous_rule_end
= grammar_end
;
223 grammar_symbol_append (lhs
, location
);
224 current_rule
= grammar_end
;
226 /* Mark the rule's lhs as a nonterminal if not already so. */
228 if (lhs
->class == unknown_sym
)
230 lhs
->class = nterm_sym
;
234 else if (lhs
->class == token_sym
)
235 complain_at (location
, _("rule given for %s, which is a token"), lhs
->tag
);
238 /* Check that the last rule (CURRENT_RULE) is properly defined. For
239 instance, there should be no type clash on the default action. */
242 grammar_current_rule_check (void)
244 symbol_t
*lhs
= current_rule
->sym
;
245 char const *lhs_type
= lhs
->type_name
;
246 symbol_t
*first_rhs
= current_rule
->next
->sym
;
248 /* If there is an action, then there is nothing we can do: the user
249 is allowed to shoot herself in the foot. */
250 if (current_rule
->action
)
253 /* Don't worry about the default action if $$ is untyped, since $$'s
254 value can't be used. */
258 /* If $$ is being set in default way, report if any type mismatch. */
261 const char *rhs_type
= first_rhs
->type_name
? first_rhs
->type_name
: "";
262 if (!STRUNIQ_EQ (lhs_type
, rhs_type
))
263 complain_at (current_rule
->location
,
264 _("type clash on default action: <%s> != <%s>"),
267 /* Warn if there is no default for $$ but we need one. */
269 complain_at (current_rule
->location
,
270 _("empty rule for typed nonterminal, and no action"));
274 /*-------------------------------------.
275 | End the currently being grown rule. |
276 `-------------------------------------*/
279 grammar_rule_end (location_t location
)
281 /* Put an empty link in the list to mark the end of this rule */
282 grammar_symbol_append (NULL
, grammar_end
->location
);
283 current_rule
->location
= location
;
284 grammar_current_rule_check ();
288 /*-------------------------------------------------------------------.
289 | The previous action turns out the be a mid-rule action. Attach it |
290 | to the current rule, i.e., create a dummy symbol, attach it this |
291 | mid-rule action, and append this dummy nonterminal to the current |
293 `-------------------------------------------------------------------*/
296 grammar_midrule_action (void)
298 /* Since the action was written out with this rule's number, we must
299 give the new rule this number by inserting the new rule before
302 /* Make a DUMMY nonterminal, whose location is that of the midrule
303 action. Create the MIDRULE. */
304 location_t dummy_location
= current_rule
->action_location
;
305 symbol_t
*dummy
= dummy_symbol_get (dummy_location
);
306 symbol_list_t
*midrule
= symbol_list_new (dummy
, dummy_location
);
308 /* Make a new rule, whose body is empty, before the current one, so
309 that the action just read can belong to it. */
312 /* Attach its location and actions to that of the DUMMY. */
313 midrule
->location
= dummy_location
;
314 midrule
->action
= current_rule
->action
;
315 midrule
->action_location
= dummy_location
;
316 current_rule
->action
= NULL
;
318 if (previous_rule_end
)
319 previous_rule_end
->next
= midrule
;
323 /* End the dummy's rule. */
324 previous_rule_end
= symbol_list_new (NULL
, dummy_location
);
325 previous_rule_end
->next
= current_rule
;
327 midrule
->next
= previous_rule_end
;
329 /* Insert the dummy nonterminal replacing the midrule action into
331 grammar_current_rule_symbol_append (dummy
, dummy_location
);
334 /* Set the precedence symbol of the current rule to PRECSYM. */
337 grammar_current_rule_prec_set (symbol_t
*precsym
, location_t location
)
339 if (current_rule
->ruleprec
)
340 complain_at (location
, _("only one %s allowed per rule"), "%prec");
341 current_rule
->ruleprec
= precsym
;
344 /* Attach dynamic precedence DPREC to the current rule. */
347 grammar_current_rule_dprec_set (int dprec
, location_t location
)
350 warn_at (location
, _("%s affects only GLR parsers"), "%dprec");
352 complain_at (location
,
353 _("%s must be followed by positive number"), "%dprec");
354 else if (current_rule
->dprec
!= 0)
355 complain_at (location
, _("only one %s allowed per rule"), "%dprec");
356 current_rule
->dprec
= dprec
;
359 /* Attach a merge function NAME with argument type TYPE to current
363 grammar_current_rule_merge_set (struniq_t name
, location_t location
)
366 warn_at (location
, _("%s affects only GLR parsers"), "%merge");
367 if (current_rule
->merger
!= 0)
368 complain_at (location
, _("only one %s allowed per rule"), "%merge");
369 current_rule
->merger
=
370 get_merge_function (name
, current_rule
->sym
->type_name
, location
);
373 /* Attach a SYMBOL to the current rule. If needed, move the previous
374 action as a mid-rule action. */
377 grammar_current_rule_symbol_append (symbol_t
*symbol
, location_t location
)
379 if (current_rule
->action
)
380 grammar_midrule_action ();
382 grammar_symbol_append (symbol
, location
);
385 /* Attach an ACTION to the current rule. If needed, move the previous
386 action as a mid-rule action. */
389 grammar_current_rule_action_append (const char *action
, location_t location
)
391 if (current_rule
->action
)
392 grammar_midrule_action ();
393 current_rule
->action
= action
;
394 current_rule
->action_location
= location
;
398 /*---------------------------------------------------------------.
399 | Convert the rules into the representation using RRHS, RLHS and |
401 `---------------------------------------------------------------*/
406 unsigned int itemno
= 0;
407 rule_number_t ruleno
= 0;
408 symbol_list_t
*p
= grammar
;
410 ritem
= XCALLOC (item_number_t
, nritems
);
411 rules
= XCALLOC (rule_t
, nrules
);
415 symbol_t
*ruleprec
= p
->ruleprec
;
416 rules
[ruleno
].user_number
= ruleno
;
417 rules
[ruleno
].number
= ruleno
;
418 rules
[ruleno
].lhs
= p
->sym
;
419 rules
[ruleno
].rhs
= ritem
+ itemno
;
420 rules
[ruleno
].location
= p
->location
;
421 rules
[ruleno
].useful
= true;
422 rules
[ruleno
].action
= p
->action
;
423 rules
[ruleno
].action_location
= p
->action_location
;
424 rules
[ruleno
].dprec
= p
->dprec
;
425 rules
[ruleno
].merger
= p
->merger
;
430 /* item_number_t = symbol_number_t.
431 But the former needs to contain more: negative rule numbers. */
432 ritem
[itemno
++] = symbol_number_as_item_number (p
->sym
->number
);
433 /* A rule gets by default the precedence and associativity
434 of the last token in it. */
435 if (p
->sym
->class == token_sym
)
436 rules
[ruleno
].prec
= p
->sym
;
441 /* If this rule has a %prec,
442 the specified symbol's precedence replaces the default. */
445 rules
[ruleno
].precsym
= ruleprec
;
446 rules
[ruleno
].prec
= ruleprec
;
448 ritem
[itemno
++] = rule_number_as_item_number (ruleno
);
455 if (itemno
!= nritems
)
458 if (trace_flag
& trace_sets
)
459 ritem_print (stderr
);
462 /*------------------------------------------------------------------.
463 | Read in the grammar specification and record it in the format |
464 | described in gram.h. All actions are copied into ACTION_OBSTACK, |
465 | in each case forming the body of a C function (YYACTION) which |
466 | contains a switch statement to decide which action to execute. |
467 `------------------------------------------------------------------*/
472 gram_control_t gram_control
;
474 /* Initialize the symbol table. */
477 /* Construct the accept symbol. */
478 accept
= symbol_get ("$accept", empty_location
);
479 accept
->class = nterm_sym
;
480 accept
->number
= nvars
++;
482 /* Construct the error token */
483 errtoken
= symbol_get ("error", empty_location
);
484 errtoken
->class = token_sym
;
485 errtoken
->number
= ntokens
++;
487 /* Construct a token that represents all undefined literal tokens.
488 It is always token number 2. */
489 undeftoken
= symbol_get ("$undefined", empty_location
);
490 undeftoken
->class = token_sym
;
491 undeftoken
->number
= ntokens
++;
493 /* Initialize the obstacks. */
494 obstack_init (&pre_prologue_obstack
);
495 obstack_init (&post_prologue_obstack
);
497 finput
= xfopen (grammar_file
, "r");
500 gram__flex_debug
= trace_flag
& trace_scan
;
501 gram_debug
= trace_flag
& trace_parse
;
502 scanner_initialize ();
503 gram_parse (&gram_control
);
505 /* If something went wrong during the parsing, don't try to
507 if (complaint_issued
)
510 /* Grammar has been read. Do some checking */
512 fatal (_("no rules in the input grammar"));
514 /* Report any undefined symbols and consider them nonterminals. */
515 symbols_check_defined ();
517 /* If the user did not define her ENDTOKEN, do it now. */
520 endtoken
= symbol_get ("$end", empty_location
);
521 endtoken
->class = token_sym
;
522 endtoken
->number
= 0;
523 /* Value specified by POSIX. */
524 endtoken
->user_token_number
= 0;
527 /* Insert the initial rule, which line is that of the first rule
528 (not that of the start symbol):
530 accept: %start EOF. */
532 symbol_list_t
*p
= symbol_list_new (accept
, empty_location
);
533 p
->location
= grammar
->location
;
534 p
->next
= symbol_list_new (startsymbol
, empty_location
);
535 p
->next
->next
= symbol_list_new (endtoken
, empty_location
);
536 p
->next
->next
->next
= symbol_list_new (NULL
, empty_location
);
537 p
->next
->next
->next
->next
= grammar
;
543 if (! (nsyms
<= SYMBOL_NUMBER_MAX
&& nsyms
== ntokens
+ nvars
))
548 /* Assign the symbols their symbol numbers. Write #defines for the
549 token symbols into FDEFINES if requested. */
552 /* Convert the grammar into the format described in gram.h. */
555 /* The grammar as a symbol_list_t is no longer needed. */
556 LIST_FREE (symbol_list_t
, grammar
);