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
;
74 obstack_fgrow2 (oout
, muscle_find ("linef"),
76 quotearg_style (c_quoting_style
,
77 muscle_find ("filename")));
79 obstack_sgrow (oout
, prologue
);
85 /*----------------------.
86 | Handle the epilogue. |
87 `----------------------*/
90 epilogue_set (const char *epilogue
, location_t location
)
94 obstack_fgrow2 (&muscle_obstack
, muscle_find ("linef"),
96 quotearg_style (c_quoting_style
,
97 muscle_find ("filename")));
99 obstack_sgrow (&muscle_obstack
, epilogue
);
100 obstack_1grow (&muscle_obstack
, 0);
101 muscle_insert ("epilogue", obstack_finish (&muscle_obstack
));
107 /*-------------------------------------------------------------------.
108 | Return the merger index for a merging function named NAME, whose |
109 | arguments have type TYPE. Records the function, if new, in |
111 `-------------------------------------------------------------------*/
114 get_merge_function (const char* name
, const char* type
,
127 head
.next
= merge_functions
;
128 for (syms
= &head
, n
= 1; syms
->next
!= NULL
; syms
= syms
->next
, n
+= 1)
129 if (strcmp (name
, syms
->next
->name
) == 0)
131 if (syms
->next
== NULL
)
133 syms
->next
= XMALLOC (merger_list
, 1);
134 syms
->next
->name
= xstrdup (name
);
135 syms
->next
->type
= xstrdup (type
);
136 syms
->next
->next
= NULL
;
137 merge_functions
= head
.next
;
139 else if (strcmp (type
, syms
->next
->type
) != 0)
140 warn_at (loc
, _("result type clash (`%s' `%s') on merge function %s"),
141 type
, syms
->next
->type
, name
);
145 /*--------------------------------------.
146 | Free all merge-function definitions. |
147 `--------------------------------------*/
150 free_merger_functions (void)
155 L0
= merge_functions
;
158 merger_list
*L1
= L0
->next
;
165 /*-------------------------------------------------------------------.
166 | Parse the input grammar into a one symbol_list_t structure. Each |
167 | rule is represented by a sequence of symbols: the left hand side |
168 | followed by the contents of the right hand side, followed by a |
169 | null pointer instead of a symbol to terminate the rule. The next |
170 | symbol is the lhs of the following rule. |
172 | All actions are copied out, labelled by the rule number they apply |
175 | Bison used to allow some %directives in the rules sections, but |
176 | this is no longer consider appropriate: (i) the documented grammar |
177 | doesn't claim it, (ii), it would promote bad style, (iii), error |
178 | recovery for %directives consists in skipping the junk until a `%' |
179 | is seen and helrp synchronizing. This scheme is definitely wrong |
180 | in the rules section. |
181 `-------------------------------------------------------------------*/
183 /* The (currently) last symbol of GRAMMAR. */
184 symbol_list_t
*grammar_end
= NULL
;
186 /* Append S to the GRAMMAR. */
188 grammar_symbol_append (symbol_t
*symbol
, location_t location
)
190 symbol_list_t
*p
= symbol_list_new (symbol
, location
);
193 grammar_end
->next
= p
;
200 /* The rule currently being defined, and the previous rule.
201 CURRENT_RULE points to the first LHS of the current rule, while
202 PREVIOUS_RULE_END points to the *end* of the previous rule (NULL). */
203 symbol_list_t
*current_rule
= NULL
;
204 symbol_list_t
*previous_rule_end
= NULL
;
207 /*----------------------------------------------.
208 | Create a new rule for LHS in to the GRAMMAR. |
209 `----------------------------------------------*/
212 grammar_rule_begin (symbol_t
*lhs
, location_t location
)
217 startsymbol_location
= location
;
221 /* Start a new rule and record its lhs. */
225 previous_rule_end
= grammar_end
;
226 grammar_symbol_append (lhs
, location
);
227 current_rule
= grammar_end
;
229 /* Mark the rule's lhs as a nonterminal if not already so. */
231 if (lhs
->class == unknown_sym
)
233 lhs
->class = nterm_sym
;
237 else if (lhs
->class == token_sym
)
238 complain_at (location
, _("rule given for %s, which is a token"), lhs
->tag
);
241 /* Check that the last rule (CURRENT_RULE) is properly defined. For
242 instance, there should be no type clash on the default action. */
245 grammar_current_rule_check (void)
247 symbol_t
*lhs
= current_rule
->sym
;
248 char const *lhs_type
= lhs
->type_name
;
249 symbol_t
*first_rhs
= current_rule
->next
->sym
;
251 /* If there is an action, then there is nothing we can do: the user
252 is allowed to shoot herself in the foot. */
253 if (current_rule
->action
)
256 /* Don't worry about the default action if $$ is untyped, since $$'s
257 value can't be used. */
261 /* If $$ is being set in default way, report if any type mismatch. */
264 const char *rhs_type
= first_rhs
->type_name
? first_rhs
->type_name
: "";
265 if (strcmp (lhs_type
, rhs_type
))
266 complain_at (current_rule
->location
,
267 _("type clash (`%s' `%s') on default action"),
270 /* Warn if there is no default for $$ but we need one. */
272 complain_at (current_rule
->location
,
273 _("empty rule for typed nonterminal, and no action"));
277 /*-------------------------------------.
278 | End the currently being grown rule. |
279 `-------------------------------------*/
282 grammar_rule_end (location_t location
)
284 /* Put an empty link in the list to mark the end of this rule */
285 grammar_symbol_append (NULL
, grammar_end
->location
);
286 current_rule
->location
= location
;
287 grammar_current_rule_check ();
291 /*-------------------------------------------------------------------.
292 | The previous action turns out the be a mid-rule action. Attach it |
293 | to the current rule, i.e., create a dummy symbol, attach it this |
294 | mid-rule action, and append this dummy nonterminal to the current |
296 `-------------------------------------------------------------------*/
299 grammar_midrule_action (void)
301 /* Since the action was written out with this rule's number, we must
302 give the new rule this number by inserting the new rule before
305 /* Make a DUMMY nonterminal, whose location is that of the midrule
306 action. Create the MIDRULE. */
307 location_t dummy_location
= current_rule
->action_location
;
308 symbol_t
*dummy
= dummy_symbol_get (dummy_location
);
309 symbol_list_t
*midrule
= symbol_list_new (dummy
, dummy_location
);
311 /* Make a new rule, whose body is empty, before the current one, so
312 that the action just read can belong to it. */
315 /* Attach its location and actions to that of the DUMMY. */
316 midrule
->location
= dummy_location
;
317 midrule
->action
= current_rule
->action
;
318 midrule
->action_location
= dummy_location
;
319 current_rule
->action
= NULL
;
321 if (previous_rule_end
)
322 previous_rule_end
->next
= midrule
;
326 /* End the dummy's rule. */
327 previous_rule_end
= symbol_list_new (NULL
, dummy_location
);
328 previous_rule_end
->next
= current_rule
;
330 midrule
->next
= previous_rule_end
;
332 /* Insert the dummy nonterminal replacing the midrule action into
334 grammar_current_rule_symbol_append (dummy
, dummy_location
);
337 /* Set the precedence symbol of the current rule to PRECSYM. */
340 grammar_current_rule_prec_set (symbol_t
*precsym
, location_t location
)
342 if (current_rule
->ruleprec
)
343 complain_at (location
, _("only one %s allowed per rule"), "%prec");
344 current_rule
->ruleprec
= precsym
;
347 /* Attach dynamic precedence DPREC to the current rule. */
350 grammar_current_rule_dprec_set (int dprec
, location_t location
)
353 warn_at (location
, _("%s affects only GLR parsers"), "%dprec");
355 complain_at (location
,
356 _("%s must be followed by positive number"), "%dprec");
357 else if (current_rule
->dprec
!= 0)
358 complain_at (location
, _("only one %s allowed per rule"), "%dprec");
359 current_rule
->dprec
= dprec
;
362 /* Attach a merge function NAME with argument type TYPE to current
366 grammar_current_rule_merge_set (const char* name
, location_t location
)
369 warn_at (location
, _("%s affects only GLR parsers"), "%merge");
370 if (current_rule
->merger
!= 0)
371 complain_at (location
, _("only one %s allowed per rule"), "%merge");
372 current_rule
->merger
=
373 get_merge_function (name
, current_rule
->sym
->type_name
, location
);
376 /* Attach a SYMBOL to the current rule. If needed, move the previous
377 action as a mid-rule action. */
380 grammar_current_rule_symbol_append (symbol_t
*symbol
, location_t location
)
382 if (current_rule
->action
)
383 grammar_midrule_action ();
385 grammar_symbol_append (symbol
, location
);
388 /* Attach an ACTION to the current rule. If needed, move the previous
389 action as a mid-rule action. */
392 grammar_current_rule_action_append (const char *action
, location_t location
)
394 if (current_rule
->action
)
395 grammar_midrule_action ();
396 current_rule
->action
= action
;
397 current_rule
->action_location
= location
;
401 /*---------------------------------------------------------------.
402 | Convert the rules into the representation using RRHS, RLHS and |
404 `---------------------------------------------------------------*/
409 unsigned int itemno
= 0;
410 rule_number_t ruleno
= 0;
411 symbol_list_t
*p
= grammar
;
413 ritem
= XCALLOC (item_number_t
, nritems
);
414 rules
= XCALLOC (rule_t
, nrules
);
418 symbol_t
*ruleprec
= p
->ruleprec
;
419 rules
[ruleno
].user_number
= ruleno
;
420 rules
[ruleno
].number
= ruleno
;
421 rules
[ruleno
].lhs
= p
->sym
;
422 rules
[ruleno
].rhs
= ritem
+ itemno
;
423 rules
[ruleno
].location
= p
->location
;
424 rules
[ruleno
].useful
= true;
425 rules
[ruleno
].action
= p
->action
;
426 rules
[ruleno
].action_location
= p
->action_location
;
427 rules
[ruleno
].dprec
= p
->dprec
;
428 rules
[ruleno
].merger
= p
->merger
;
433 /* item_number_t = symbol_number_t.
434 But the former needs to contain more: negative rule numbers. */
435 ritem
[itemno
++] = symbol_number_as_item_number (p
->sym
->number
);
436 /* A rule gets by default the precedence and associativity
437 of the last token in it. */
438 if (p
->sym
->class == token_sym
)
439 rules
[ruleno
].prec
= p
->sym
;
444 /* If this rule has a %prec,
445 the specified symbol's precedence replaces the default. */
448 rules
[ruleno
].precsym
= ruleprec
;
449 rules
[ruleno
].prec
= ruleprec
;
451 ritem
[itemno
++] = rule_number_as_item_number (ruleno
);
458 assert (itemno
== nritems
);
460 if (trace_flag
& trace_sets
)
461 ritem_print (stderr
);
464 /*------------------------------------------------------------------.
465 | Read in the grammar specification and record it in the format |
466 | described in gram.h. All actions are copied into ACTION_OBSTACK, |
467 | in each case forming the body of a C function (YYACTION) which |
468 | contains a switch statement to decide which action to execute. |
469 `------------------------------------------------------------------*/
474 gram_control_t gram_control
;
476 /* Initialize the symbol table. */
479 /* Construct the accept symbol. */
480 accept
= symbol_get ("$accept", empty_location
);
481 accept
->class = nterm_sym
;
482 accept
->number
= nvars
++;
484 /* Construct the error token */
485 errtoken
= symbol_get ("error", empty_location
);
486 errtoken
->class = token_sym
;
487 errtoken
->number
= ntokens
++;
489 /* Construct a token that represents all undefined literal tokens.
490 It is always token number 2. */
491 undeftoken
= symbol_get ("$undefined", empty_location
);
492 undeftoken
->class = token_sym
;
493 undeftoken
->number
= ntokens
++;
495 /* Initialize the obstacks. */
496 obstack_init (&pre_prologue_obstack
);
497 obstack_init (&post_prologue_obstack
);
499 finput
= xfopen (infile
, "r");
502 gram__flex_debug
= trace_flag
& trace_scan
;
503 gram_debug
= trace_flag
& trace_parse
;
504 scanner_initialize ();
505 gram_parse (&gram_control
);
507 /* If something went wrong during the parsing, don't try to
509 if (complaint_issued
)
512 /* Grammar has been read. Do some checking */
514 fatal (_("no rules in the input grammar"));
516 /* Report any undefined symbols and consider them nonterminals. */
517 symbols_check_defined ();
519 /* If the user did not define her ENDTOKEN, do it now. */
522 endtoken
= symbol_get ("$end", empty_location
);
523 endtoken
->class = token_sym
;
524 endtoken
->number
= 0;
525 /* Value specified by POSIX. */
526 endtoken
->user_token_number
= 0;
529 /* Insert the initial rule, which line is that of the first rule
530 (not that of the start symbol):
532 accept: %start EOF. */
534 symbol_list_t
*p
= symbol_list_new (accept
, empty_location
);
535 p
->location
= grammar
->location
;
536 p
->next
= symbol_list_new (startsymbol
, empty_location
);
537 p
->next
->next
= symbol_list_new (endtoken
, empty_location
);
538 p
->next
->next
->next
= symbol_list_new (NULL
, empty_location
);
539 p
->next
->next
->next
->next
= grammar
;
545 if (SYMBOL_NUMBER_MAX
< nsyms
)
546 fatal (_("too many symbols (tokens plus nonterminals); maximum %d"),
549 assert (nsyms
== ntokens
+ nvars
);
553 /* Assign the symbols their symbol numbers. Write #defines for the
554 token symbols into FDEFINES if requested. */
557 /* Convert the grammar into the format described in gram.h. */
560 /* The grammar as a symbol_list_t is no longer needed. */
561 LIST_FREE (symbol_list_t
, grammar
);