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. */
34 #include "conflicts.h"
35 #include "muscle_tab.h"
38 static symbol_list
*grammar
= NULL
;
39 static int start_flag
= 0;
41 /* Nonzero if %union has been seen. */
45 symbol_list_new (symbol_t
*sym
)
47 symbol_list
*res
= XMALLOC (symbol_list
, 1);
58 /*--------------------------------------------------------------.
59 | Get the data type (alternative in the union) of the value for |
60 | symbol N in rule RULE. |
61 `--------------------------------------------------------------*/
64 get_type_name (int n
, symbol_list
*rule
)
71 complain (_("invalid $ value"));
81 if (rp
== NULL
|| rp
->sym
== NULL
)
83 complain (_("invalid $ value"));
89 return rp
->sym
->type_name
;
93 /*-----------------------.
94 | Set the start symbol. |
95 `-----------------------*/
98 grammar_start_symbol_set (symbol_t
*s
)
101 complain (_("multiple %s declarations"), "%start");
110 /*----------------------------------------------------------------.
111 | There are two prologues: one before %union, one after. Augment |
113 `----------------------------------------------------------------*/
116 prologue_augment (const char *prologue
, int location
)
118 struct obstack
*oout
=
119 !typed
? &pre_prologue_obstack
: &post_prologue_obstack
;
123 obstack_fgrow2 (oout
, muscle_find ("linef"),
124 location
, quotearg_style (c_quoting_style
,
125 muscle_find ("filename")));
127 obstack_sgrow (oout
, prologue
);
133 /*----------------------.
134 | Handle the epilogue. |
135 `----------------------*/
138 epilogue_set (const char *epilogue
, int location
)
140 struct obstack el_obstack
;
141 obstack_init (&el_obstack
);
145 obstack_fgrow2 (&el_obstack
, muscle_find ("linef"),
146 location
, quotearg_style (c_quoting_style
,
147 muscle_find ("filename")));
149 obstack_sgrow (&el_obstack
, epilogue
);
150 obstack_1grow (&el_obstack
, 0);
151 muscle_insert ("epilogue", obstack_finish (&el_obstack
));
157 /*-------------------------------------------------------------------.
158 | Generate a dummy symbol, a nonterminal, whose name cannot conflict |
159 | with the user's names. |
160 `-------------------------------------------------------------------*/
165 /* Incremented for each generated symbol */
166 static int gensym_count
= 0;
167 static char buf
[256];
171 sprintf (buf
, "@%d", ++gensym_count
);
173 sym
->class = nterm_sym
;
174 sym
->number
= nvars
++;
178 /*-------------------------------------------------------------------.
179 | Parse the input grammar into a one symbol_list structure. Each |
180 | rule is represented by a sequence of symbols: the left hand side |
181 | followed by the contents of the right hand side, followed by a |
182 | null pointer instead of a symbol to terminate the rule. The next |
183 | symbol is the lhs of the following rule. |
185 | All actions are copied out, labelled by the rule number they apply |
188 | Bison used to allow some %directives in the rules sections, but |
189 | this is no longer consider appropriate: (i) the documented grammar |
190 | doesn't claim it, (ii), it would promote bad style, (iii), error |
191 | recovery for %directives consists in skipping the junk until a `%' |
192 | is seen and helrp synchronizing. This scheme is definitely wrong |
193 | in the rules section. |
194 `-------------------------------------------------------------------*/
196 /* The (currently) last symbol of GRAMMAR. */
197 symbol_list
*grammar_end
= NULL
;
199 /* Append S to the GRAMMAR. */
201 grammar_symbol_append (symbol_t
*s
)
203 symbol_list
*p
= symbol_list_new (s
);
206 grammar_end
->next
= p
;
213 /* The rule currently being defined, and the previous rule. Point to
214 the first symbol of each list: their lhs. */
215 symbol_list
*current_rule
= NULL
;
216 symbol_list
*previous_rule
= NULL
;
219 /* Create a new rule for LHS in to the GRAMMAR. */
222 grammar_rule_begin (symbol_t
*lhs
)
230 /* Start a new rule and record its lhs. */
234 previous_rule
= grammar_end
;
235 grammar_symbol_append (lhs
);
236 current_rule
= grammar_end
;
238 /* Mark the rule's lhs as a nonterminal if not already so. */
240 if (lhs
->class == unknown_sym
)
242 lhs
->class = nterm_sym
;
246 else if (lhs
->class == token_sym
)
247 complain (_("rule given for %s, which is a token"), lhs
->tag
);
250 /* Check that the last rule (CURRENT_RULE) is properly defined. For
251 instance, there should be no type clash on the default action. */
254 grammar_current_rule_check (void)
256 symbol_t
*lhs
= current_rule
->sym
;
257 symbol_t
*first_rhs
= current_rule
->next
->sym
;
259 /* If there is an action, then there is nothing we can do: the user
260 is allowed to shoot in her foot. */
261 if (current_rule
->action
)
264 /* If $$ is being set in default way, report if any type mismatch.
268 const char *lhs_type
= lhs
->type_name
? lhs
->type_name
: "";
269 const char *rhs_type
= first_rhs
->type_name
? first_rhs
->type_name
: "";
270 if (strcmp (lhs_type
, rhs_type
))
271 complain (_("type clash (`%s' `%s') on default action"),
274 /* Warn if there is no default for $$ but we need one. */
278 complain (_("empty rule for typed nonterminal, and no action"));
283 /* End the currently being grown rule. */
286 grammar_rule_end (void)
288 /* Put an empty link in the list to mark the end of this rule */
289 grammar_symbol_append (NULL
);
290 grammar_current_rule_check ();
294 /* The previous action turns out the be a mid-rule action. Attach it
295 to the current rule, i.e., create a dummy symbol, attach it this
296 mid-rule action, and append this dummy nonterminal to the current
300 grammar_midrule_action (void)
302 /* Since the action was written out with this rule's number, we must
303 give the new rule this number by inserting the new rule before
306 /* Make a dummy nonterminal, a gensym. */
307 symbol_t
*sdummy
= gensym ();
308 symbol_list
*midrule_action
= symbol_list_new (sdummy
);
310 /* Make a new rule, whose body is empty, before the current one, so
311 that the action just read can belong to it. */
314 /* Attach its lineno to that of the host rule. */
315 midrule_action
->line
= current_rule
->line
;
316 /* Move the action from the host rule to this one. */
317 midrule_action
->action
= current_rule
->action
;
318 midrule_action
->action_line
= current_rule
->action_line
;
319 current_rule
->action
= NULL
;
322 previous_rule
->next
= midrule_action
;
324 grammar
= midrule_action
;
326 /* End of the rule. */
327 previous_rule
= symbol_list_new (NULL
);
328 previous_rule
->next
= current_rule
;
330 midrule_action
->next
= previous_rule
;
332 /* Insert the dummy generated by that rule into this rule. */
334 grammar_symbol_append (sdummy
);
337 /* Set the precedence symbol of the current rule to PRECSYM. */
340 grammar_current_rule_prec_set (symbol_t
*precsym
)
342 if (current_rule
->ruleprec
)
343 complain (_("two @prec's in a row"));
344 current_rule
->ruleprec
= precsym
;
347 /* Attach a SYMBOL to the current rule. If needed, move the previous
348 action as a mid-rule action. */
351 grammar_current_rule_symbol_append (symbol_t
*symbol
)
353 if (current_rule
->action
)
354 grammar_midrule_action ();
356 grammar_symbol_append (symbol
);
360 /* Attach an ACTION to the current rule. If needed, move the previous
361 action as a mid-rule action. */
364 grammar_current_rule_action_append (const char *action
, int action_line
)
366 if (current_rule
->action
)
367 grammar_midrule_action ();
368 current_rule
->action
= action
;
369 current_rule
->action_line
= action_line
;
373 /*---------------------------------------------------------------.
374 | Convert the rules into the representation using RRHS, RLHS and |
376 `---------------------------------------------------------------*/
385 ritem
= XCALLOC (item_number_t
, nritems
);
386 rules
= XCALLOC (rule_t
, nrules
) - 1;
394 symbol_t
*ruleprec
= p
->ruleprec
;
395 rules
[ruleno
].user_number
= ruleno
;
396 rules
[ruleno
].number
= ruleno
;
397 rules
[ruleno
].lhs
= p
->sym
;
398 rules
[ruleno
].rhs
= ritem
+ itemno
;
399 rules
[ruleno
].line
= p
->line
;
400 rules
[ruleno
].useful
= TRUE
;
401 rules
[ruleno
].action
= p
->action
;
402 rules
[ruleno
].action_line
= p
->action_line
;
407 /* item_number_t = symbol_number_t.
408 But the former needs to contain more: negative rule numbers. */
409 ritem
[itemno
++] = symbol_number_as_item_number (p
->sym
->number
);
410 /* A rule gets by default the precedence and associativity
411 of the last token in it. */
412 if (p
->sym
->class == token_sym
)
413 rules
[ruleno
].prec
= p
->sym
;
418 /* If this rule has a %prec,
419 the specified symbol's precedence replaces the default. */
422 rules
[ruleno
].precsym
= ruleprec
;
423 rules
[ruleno
].prec
= ruleprec
;
425 ritem
[itemno
++] = -ruleno
;
432 assert (itemno
== nritems
);
435 ritem_print (stderr
);
438 /*------------------------------------------------------------------.
439 | Read in the grammar specification and record it in the format |
440 | described in gram.h. All actions are copied into ACTION_OBSTACK, |
441 | in each case forming the body of a C function (YYACTION) which |
442 | contains a switch statement to decide which action to execute. |
443 `------------------------------------------------------------------*/
448 gram_control_t gram_control
;
451 /* Initialize the muscle obstack. */
452 obstack_init (&muscle_obstack
);
454 /* Initialize the symbol table. */
457 /* Construct the axiom symbol. */
458 axiom
= getsym ("$axiom");
459 axiom
->class = nterm_sym
;
460 axiom
->number
= nvars
++;
462 /* Construct the error token */
463 errtoken
= getsym ("error");
464 errtoken
->class = token_sym
;
465 errtoken
->number
= ntokens
++;
467 /* Construct a token that represents all undefined literal tokens.
468 It is always token number 2. */
469 undeftoken
= getsym ("$undefined.");
470 undeftoken
->class = token_sym
;
471 undeftoken
->number
= ntokens
++;
473 /* Initialize the obstacks. */
474 obstack_init (&action_obstack
);
475 obstack_init (&output_obstack
);
476 obstack_init (&pre_prologue_obstack
);
477 obstack_init (&post_prologue_obstack
);
479 finput
= xfopen (infile
, "r");
482 gram_debug
= !!getenv ("parse");
483 gram__flex_debug
= !!getenv ("scan");
484 scanner_initialize ();
485 gram_parse (&gram_control
);
487 /* Grammar has been read. Do some checking */
489 fatal (_("no rules in the input grammar"));
491 /* Report any undefined symbols and consider them nonterminals. */
492 symbols_check_defined ();
494 /* If the user did not define her EOFTOKEN, do it now. */
497 eoftoken
= getsym ("$");
498 eoftoken
->class = token_sym
;
499 eoftoken
->number
= 0;
500 /* Value specified by POSIX. */
501 eoftoken
->user_token_number
= 0;
504 /* Insert the initial rule, which line is that of the first rule
505 (not that of the start symbol):
507 axiom: %start EOF. */
509 symbol_list
*p
= symbol_list_new (axiom
);
510 p
->line
= grammar
->line
;
511 p
->next
= symbol_list_new (startsymbol
);
512 p
->next
->next
= symbol_list_new (eoftoken
);
513 p
->next
->next
->next
= symbol_list_new (NULL
);
514 p
->next
->next
->next
->next
= grammar
;
520 if (nsyms
> SHRT_MAX
)
521 fatal (_("too many symbols (tokens plus nonterminals); maximum %d"),
524 assert (nsyms
== ntokens
+ nvars
);
528 /* Assign the symbols their symbol numbers. Write #defines for the
529 token symbols into FDEFINES if requested. */
532 /* Convert the grammar into the format described in gram.h. */
535 /* The grammar as a symbol_list is no longer needed. */
536 LIST_FREE (symbol_list
, grammar
);