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
,
75 location
.start
.file
));
76 obstack_sgrow (oout
, "]])[\n");
77 obstack_sgrow (oout
, prologue
);
83 /*----------------------.
84 | Handle the epilogue. |
85 `----------------------*/
88 epilogue_augment (const char *epilogue
, location_t location
)
90 char *extension
= NULL
;
91 obstack_fgrow1 (&muscle_obstack
, "]b4_syncline([[%d]], [[",
93 MUSCLE_OBSTACK_SGROW (&muscle_obstack
,
94 quotearg_style (c_quoting_style
, location
.start
.file
));
95 obstack_sgrow (&muscle_obstack
, "]])[\n");
96 obstack_sgrow (&muscle_obstack
, epilogue
);
97 obstack_1grow (&muscle_obstack
, 0);
98 extension
= obstack_finish (&muscle_obstack
);
99 muscle_grow ("epilogue", extension
, "");
100 obstack_free (&muscle_obstack
, extension
);
106 /*-------------------------------------------------------------------.
107 | Return the merger index for a merging function named NAME, whose |
108 | arguments have type TYPE. Records the function, if new, in |
110 `-------------------------------------------------------------------*/
113 get_merge_function (struniq_t name
, struniq_t type
, location_t loc
)
123 type
= struniq_new ("");
125 head
.next
= merge_functions
;
126 for (syms
= &head
, n
= 1; syms
->next
!= NULL
; syms
= syms
->next
, n
+= 1)
127 if (STRUNIQ_EQ (name
, syms
->next
->name
))
129 if (syms
->next
== NULL
)
131 syms
->next
= XMALLOC (merger_list
, 1);
132 syms
->next
->name
= struniq_new (name
);
133 syms
->next
->type
= struniq_new (type
);
134 syms
->next
->next
= NULL
;
135 merge_functions
= head
.next
;
137 else if (!STRUNIQ_EQ (type
, syms
->next
->type
))
138 warn_at (loc
, _("result type clash on merge function %s: <%s> != <%s>"),
139 name
, type
, syms
->next
->type
);
143 /*--------------------------------------.
144 | Free all merge-function definitions. |
145 `--------------------------------------*/
148 free_merger_functions (void)
153 L0
= merge_functions
;
156 merger_list
*L1
= L0
->next
;
163 /*-------------------------------------------------------------------.
164 | Parse the input grammar into a one symbol_list_t structure. Each |
165 | rule is represented by a sequence of symbols: the left hand side |
166 | followed by the contents of the right hand side, followed by a |
167 | null pointer instead of a symbol to terminate the rule. The next |
168 | symbol is the lhs of the following rule. |
170 | All actions are copied out, labelled by the rule number they apply |
173 | Bison used to allow some %directives in the rules sections, but |
174 | this is no longer consider appropriate: (i) the documented grammar |
175 | doesn't claim it, (ii), it would promote bad style, (iii), error |
176 | recovery for %directives consists in skipping the junk until a `%' |
177 | is seen and helrp synchronizing. This scheme is definitely wrong |
178 | in the rules section. |
179 `-------------------------------------------------------------------*/
181 /* The (currently) last symbol of GRAMMAR. */
182 symbol_list_t
*grammar_end
= NULL
;
184 /* Append S to the GRAMMAR. */
186 grammar_symbol_append (symbol_t
*symbol
, location_t location
)
188 symbol_list_t
*p
= symbol_list_new (symbol
, location
);
191 grammar_end
->next
= p
;
198 /* The rule currently being defined, and the previous rule.
199 CURRENT_RULE points to the first LHS of the current rule, while
200 PREVIOUS_RULE_END points to the *end* of the previous rule (NULL). */
201 symbol_list_t
*current_rule
= NULL
;
202 symbol_list_t
*previous_rule_end
= NULL
;
205 /*----------------------------------------------.
206 | Create a new rule for LHS in to the GRAMMAR. |
207 `----------------------------------------------*/
210 grammar_rule_begin (symbol_t
*lhs
, location_t location
)
215 startsymbol_location
= location
;
219 /* Start a new rule and record its lhs. */
223 previous_rule_end
= grammar_end
;
224 grammar_symbol_append (lhs
, location
);
225 current_rule
= grammar_end
;
227 /* Mark the rule's lhs as a nonterminal if not already so. */
229 if (lhs
->class == unknown_sym
)
231 lhs
->class = nterm_sym
;
235 else if (lhs
->class == token_sym
)
236 complain_at (location
, _("rule given for %s, which is a token"), lhs
->tag
);
239 /* Check that the last rule (CURRENT_RULE) is properly defined. For
240 instance, there should be no type clash on the default action. */
243 grammar_current_rule_check (void)
245 symbol_t
*lhs
= current_rule
->sym
;
246 char const *lhs_type
= lhs
->type_name
;
247 symbol_t
*first_rhs
= current_rule
->next
->sym
;
249 /* If there is an action, then there is nothing we can do: the user
250 is allowed to shoot herself in the foot. */
251 if (current_rule
->action
)
254 /* Don't worry about the default action if $$ is untyped, since $$'s
255 value can't be used. */
259 /* If $$ is being set in default way, report if any type mismatch. */
262 const char *rhs_type
= first_rhs
->type_name
? first_rhs
->type_name
: "";
263 if (!STRUNIQ_EQ (lhs_type
, rhs_type
))
264 warn_at (current_rule
->location
,
265 _("type clash on default action: <%s> != <%s>"),
268 /* Warn if there is no default for $$ but we need one. */
270 warn_at (current_rule
->location
,
271 _("empty rule for typed nonterminal, and no action"));
275 /*-------------------------------------.
276 | End the currently being grown rule. |
277 `-------------------------------------*/
280 grammar_rule_end (location_t location
)
282 /* Put an empty link in the list to mark the end of this rule */
283 grammar_symbol_append (NULL
, grammar_end
->location
);
284 current_rule
->location
= location
;
285 grammar_current_rule_check ();
289 /*-------------------------------------------------------------------.
290 | The previous action turns out the be a mid-rule action. Attach it |
291 | to the current rule, i.e., create a dummy symbol, attach it this |
292 | mid-rule action, and append this dummy nonterminal to the current |
294 `-------------------------------------------------------------------*/
297 grammar_midrule_action (void)
299 /* Since the action was written out with this rule's number, we must
300 give the new rule this number by inserting the new rule before
303 /* Make a DUMMY nonterminal, whose location is that of the midrule
304 action. Create the MIDRULE. */
305 location_t dummy_location
= current_rule
->action_location
;
306 symbol_t
*dummy
= dummy_symbol_get (dummy_location
);
307 symbol_list_t
*midrule
= symbol_list_new (dummy
, dummy_location
);
309 /* Make a new rule, whose body is empty, before the current one, so
310 that the action just read can belong to it. */
313 /* Attach its location and actions to that of the DUMMY. */
314 midrule
->location
= dummy_location
;
315 midrule
->action
= current_rule
->action
;
316 midrule
->action_location
= dummy_location
;
317 current_rule
->action
= NULL
;
319 if (previous_rule_end
)
320 previous_rule_end
->next
= midrule
;
324 /* End the dummy's rule. */
325 previous_rule_end
= symbol_list_new (NULL
, dummy_location
);
326 previous_rule_end
->next
= current_rule
;
328 midrule
->next
= previous_rule_end
;
330 /* Insert the dummy nonterminal replacing the midrule action into
332 grammar_current_rule_symbol_append (dummy
, dummy_location
);
335 /* Set the precedence symbol of the current rule to PRECSYM. */
338 grammar_current_rule_prec_set (symbol_t
*precsym
, location_t location
)
340 if (current_rule
->ruleprec
)
341 complain_at (location
, _("only one %s allowed per rule"), "%prec");
342 current_rule
->ruleprec
= precsym
;
345 /* Attach dynamic precedence DPREC to the current rule. */
348 grammar_current_rule_dprec_set (int dprec
, location_t location
)
351 warn_at (location
, _("%s affects only GLR parsers"), "%dprec");
353 complain_at (location
,
354 _("%s must be followed by positive number"), "%dprec");
355 else if (current_rule
->dprec
!= 0)
356 complain_at (location
, _("only one %s allowed per rule"), "%dprec");
357 current_rule
->dprec
= dprec
;
360 /* Attach a merge function NAME with argument type TYPE to current
364 grammar_current_rule_merge_set (struniq_t name
, location_t location
)
367 warn_at (location
, _("%s affects only GLR parsers"), "%merge");
368 if (current_rule
->merger
!= 0)
369 complain_at (location
, _("only one %s allowed per rule"), "%merge");
370 current_rule
->merger
=
371 get_merge_function (name
, current_rule
->sym
->type_name
, location
);
374 /* Attach a SYMBOL to the current rule. If needed, move the previous
375 action as a mid-rule action. */
378 grammar_current_rule_symbol_append (symbol_t
*symbol
, location_t location
)
380 if (current_rule
->action
)
381 grammar_midrule_action ();
383 grammar_symbol_append (symbol
, location
);
386 /* Attach an ACTION to the current rule. If needed, move the previous
387 action as a mid-rule action. */
390 grammar_current_rule_action_append (const char *action
, location_t location
)
392 if (current_rule
->action
)
393 grammar_midrule_action ();
394 current_rule
->action
= action
;
395 current_rule
->action_location
= location
;
399 /*---------------------------------------------------------------.
400 | Convert the rules into the representation using RRHS, RLHS and |
402 `---------------------------------------------------------------*/
407 unsigned int itemno
= 0;
408 rule_number_t ruleno
= 0;
409 symbol_list_t
*p
= grammar
;
411 ritem
= XCALLOC (item_number_t
, nritems
);
412 rules
= XCALLOC (rule_t
, nrules
);
416 symbol_t
*ruleprec
= p
->ruleprec
;
417 rules
[ruleno
].user_number
= ruleno
;
418 rules
[ruleno
].number
= ruleno
;
419 rules
[ruleno
].lhs
= p
->sym
;
420 rules
[ruleno
].rhs
= ritem
+ itemno
;
421 rules
[ruleno
].location
= p
->location
;
422 rules
[ruleno
].useful
= true;
423 rules
[ruleno
].action
= p
->action
;
424 rules
[ruleno
].action_location
= p
->action_location
;
425 rules
[ruleno
].dprec
= p
->dprec
;
426 rules
[ruleno
].merger
= p
->merger
;
431 /* item_number_t = symbol_number_t.
432 But the former needs to contain more: negative rule numbers. */
433 ritem
[itemno
++] = symbol_number_as_item_number (p
->sym
->number
);
434 /* A rule gets by default the precedence and associativity
435 of the last token in it. */
436 if (p
->sym
->class == token_sym
)
437 rules
[ruleno
].prec
= p
->sym
;
442 /* If this rule has a %prec,
443 the specified symbol's precedence replaces the default. */
446 rules
[ruleno
].precsym
= ruleprec
;
447 rules
[ruleno
].prec
= ruleprec
;
449 ritem
[itemno
++] = rule_number_as_item_number (ruleno
);
456 if (itemno
!= nritems
)
459 if (trace_flag
& trace_sets
)
460 ritem_print (stderr
);
463 /*------------------------------------------------------------------.
464 | Read in the grammar specification and record it in the format |
465 | described in gram.h. All actions are copied into ACTION_OBSTACK, |
466 | in each case forming the body of a C function (YYACTION) which |
467 | contains a switch statement to decide which action to execute. |
468 `------------------------------------------------------------------*/
473 /* Initialize the symbol table. */
476 /* Construct the accept symbol. */
477 accept
= symbol_get ("$accept", empty_location
);
478 accept
->class = nterm_sym
;
479 accept
->number
= nvars
++;
481 /* Construct the error token */
482 errtoken
= symbol_get ("error", empty_location
);
483 errtoken
->class = token_sym
;
484 errtoken
->number
= ntokens
++;
486 /* Construct a token that represents all undefined literal tokens.
487 It is always token number 2. */
488 undeftoken
= symbol_get ("$undefined", empty_location
);
489 undeftoken
->class = token_sym
;
490 undeftoken
->number
= ntokens
++;
492 /* Initialize the obstacks. */
493 obstack_init (&pre_prologue_obstack
);
494 obstack_init (&post_prologue_obstack
);
496 finput
= xfopen (grammar_file
, "r");
499 gram__flex_debug
= trace_flag
& trace_scan
;
500 gram_debug
= trace_flag
& trace_parse
;
501 scanner_initialize ();
504 /* If something went wrong during the parsing, don't try to
506 if (complaint_issued
)
509 /* Grammar has been read. Do some checking */
511 fatal (_("no rules in the input grammar"));
513 /* Report any undefined symbols and consider them nonterminals. */
514 symbols_check_defined ();
516 /* If the user did not define her ENDTOKEN, do it now. */
519 endtoken
= symbol_get ("$end", empty_location
);
520 endtoken
->class = token_sym
;
521 endtoken
->number
= 0;
522 /* Value specified by POSIX. */
523 endtoken
->user_token_number
= 0;
526 /* Insert the initial rule, which line is that of the first rule
527 (not that of the start symbol):
529 accept: %start EOF. */
531 symbol_list_t
*p
= symbol_list_new (accept
, empty_location
);
532 p
->location
= grammar
->location
;
533 p
->next
= symbol_list_new (startsymbol
, empty_location
);
534 p
->next
->next
= symbol_list_new (endtoken
, empty_location
);
535 p
->next
->next
->next
= symbol_list_new (NULL
, empty_location
);
536 p
->next
->next
->next
->next
= grammar
;
542 if (! (nsyms
<= SYMBOL_NUMBER_MAX
&& nsyms
== ntokens
+ nvars
))
547 /* Assign the symbols their symbol numbers. Write #defines for the
548 token symbols into FDEFINES if requested. */
551 /* Convert the grammar into the format described in gram.h. */
554 /* The grammar as a symbol_list_t is no longer needed. */
555 LIST_FREE (symbol_list_t
, grammar
);