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_set (const char *epilogue
, location_t location
)
89 obstack_fgrow1 (&muscle_obstack
, "]b4_syncline([[%d]], [[",
91 MUSCLE_OBSTACK_SGROW (&muscle_obstack
,
92 quotearg_style (c_quoting_style
, location
.file
));
93 obstack_sgrow (&muscle_obstack
, "]])[\n");
94 obstack_sgrow (&muscle_obstack
, epilogue
);
95 obstack_1grow (&muscle_obstack
, 0);
96 muscle_insert ("epilogue", obstack_finish (&muscle_obstack
));
102 /*-------------------------------------------------------------------.
103 | Return the merger index for a merging function named NAME, whose |
104 | arguments have type TYPE. Records the function, if new, in |
106 `-------------------------------------------------------------------*/
109 get_merge_function (struniq_t name
, struniq_t type
, location_t loc
)
119 type
= struniq_new ("");
121 head
.next
= merge_functions
;
122 for (syms
= &head
, n
= 1; syms
->next
!= NULL
; syms
= syms
->next
, n
+= 1)
123 if (STRUNIQ_EQ (name
, syms
->next
->name
))
125 if (syms
->next
== NULL
)
127 syms
->next
= XMALLOC (merger_list
, 1);
128 syms
->next
->name
= struniq_new (name
);
129 syms
->next
->type
= struniq_new (type
);
130 syms
->next
->next
= NULL
;
131 merge_functions
= head
.next
;
133 else if (!STRUNIQ_EQ (type
, syms
->next
->type
))
134 warn_at (loc
, _("result type clash on merge function %s: <%s> != <%s>"),
135 name
, type
, syms
->next
->type
);
139 /*--------------------------------------.
140 | Free all merge-function definitions. |
141 `--------------------------------------*/
144 free_merger_functions (void)
149 L0
= merge_functions
;
152 merger_list
*L1
= L0
->next
;
159 /*-------------------------------------------------------------------.
160 | Parse the input grammar into a one symbol_list_t structure. Each |
161 | rule is represented by a sequence of symbols: the left hand side |
162 | followed by the contents of the right hand side, followed by a |
163 | null pointer instead of a symbol to terminate the rule. The next |
164 | symbol is the lhs of the following rule. |
166 | All actions are copied out, labelled by the rule number they apply |
169 | Bison used to allow some %directives in the rules sections, but |
170 | this is no longer consider appropriate: (i) the documented grammar |
171 | doesn't claim it, (ii), it would promote bad style, (iii), error |
172 | recovery for %directives consists in skipping the junk until a `%' |
173 | is seen and helrp synchronizing. This scheme is definitely wrong |
174 | in the rules section. |
175 `-------------------------------------------------------------------*/
177 /* The (currently) last symbol of GRAMMAR. */
178 symbol_list_t
*grammar_end
= NULL
;
180 /* Append S to the GRAMMAR. */
182 grammar_symbol_append (symbol_t
*symbol
, location_t location
)
184 symbol_list_t
*p
= symbol_list_new (symbol
, location
);
187 grammar_end
->next
= p
;
194 /* The rule currently being defined, and the previous rule.
195 CURRENT_RULE points to the first LHS of the current rule, while
196 PREVIOUS_RULE_END points to the *end* of the previous rule (NULL). */
197 symbol_list_t
*current_rule
= NULL
;
198 symbol_list_t
*previous_rule_end
= NULL
;
201 /*----------------------------------------------.
202 | Create a new rule for LHS in to the GRAMMAR. |
203 `----------------------------------------------*/
206 grammar_rule_begin (symbol_t
*lhs
, location_t location
)
211 startsymbol_location
= location
;
215 /* Start a new rule and record its lhs. */
219 previous_rule_end
= grammar_end
;
220 grammar_symbol_append (lhs
, location
);
221 current_rule
= grammar_end
;
223 /* Mark the rule's lhs as a nonterminal if not already so. */
225 if (lhs
->class == unknown_sym
)
227 lhs
->class = nterm_sym
;
231 else if (lhs
->class == token_sym
)
232 complain_at (location
, _("rule given for %s, which is a token"), lhs
->tag
);
235 /* Check that the last rule (CURRENT_RULE) is properly defined. For
236 instance, there should be no type clash on the default action. */
239 grammar_current_rule_check (void)
241 symbol_t
*lhs
= current_rule
->sym
;
242 char const *lhs_type
= lhs
->type_name
;
243 symbol_t
*first_rhs
= current_rule
->next
->sym
;
245 /* If there is an action, then there is nothing we can do: the user
246 is allowed to shoot herself in the foot. */
247 if (current_rule
->action
)
250 /* Don't worry about the default action if $$ is untyped, since $$'s
251 value can't be used. */
255 /* If $$ is being set in default way, report if any type mismatch. */
258 const char *rhs_type
= first_rhs
->type_name
? first_rhs
->type_name
: "";
259 if (!STRUNIQ_EQ (lhs_type
, rhs_type
))
260 complain_at (current_rule
->location
,
261 _("type clash on default action: <%s> != <%s>"),
264 /* Warn if there is no default for $$ but we need one. */
266 complain_at (current_rule
->location
,
267 _("empty rule for typed nonterminal, and no action"));
271 /*-------------------------------------.
272 | End the currently being grown rule. |
273 `-------------------------------------*/
276 grammar_rule_end (location_t location
)
278 /* Put an empty link in the list to mark the end of this rule */
279 grammar_symbol_append (NULL
, grammar_end
->location
);
280 current_rule
->location
= location
;
281 grammar_current_rule_check ();
285 /*-------------------------------------------------------------------.
286 | The previous action turns out the be a mid-rule action. Attach it |
287 | to the current rule, i.e., create a dummy symbol, attach it this |
288 | mid-rule action, and append this dummy nonterminal to the current |
290 `-------------------------------------------------------------------*/
293 grammar_midrule_action (void)
295 /* Since the action was written out with this rule's number, we must
296 give the new rule this number by inserting the new rule before
299 /* Make a DUMMY nonterminal, whose location is that of the midrule
300 action. Create the MIDRULE. */
301 location_t dummy_location
= current_rule
->action_location
;
302 symbol_t
*dummy
= dummy_symbol_get (dummy_location
);
303 symbol_list_t
*midrule
= symbol_list_new (dummy
, dummy_location
);
305 /* Make a new rule, whose body is empty, before the current one, so
306 that the action just read can belong to it. */
309 /* Attach its location and actions to that of the DUMMY. */
310 midrule
->location
= dummy_location
;
311 midrule
->action
= current_rule
->action
;
312 midrule
->action_location
= dummy_location
;
313 current_rule
->action
= NULL
;
315 if (previous_rule_end
)
316 previous_rule_end
->next
= midrule
;
320 /* End the dummy's rule. */
321 previous_rule_end
= symbol_list_new (NULL
, dummy_location
);
322 previous_rule_end
->next
= current_rule
;
324 midrule
->next
= previous_rule_end
;
326 /* Insert the dummy nonterminal replacing the midrule action into
328 grammar_current_rule_symbol_append (dummy
, dummy_location
);
331 /* Set the precedence symbol of the current rule to PRECSYM. */
334 grammar_current_rule_prec_set (symbol_t
*precsym
, location_t location
)
336 if (current_rule
->ruleprec
)
337 complain_at (location
, _("only one %s allowed per rule"), "%prec");
338 current_rule
->ruleprec
= precsym
;
341 /* Attach dynamic precedence DPREC to the current rule. */
344 grammar_current_rule_dprec_set (int dprec
, location_t location
)
347 warn_at (location
, _("%s affects only GLR parsers"), "%dprec");
349 complain_at (location
,
350 _("%s must be followed by positive number"), "%dprec");
351 else if (current_rule
->dprec
!= 0)
352 complain_at (location
, _("only one %s allowed per rule"), "%dprec");
353 current_rule
->dprec
= dprec
;
356 /* Attach a merge function NAME with argument type TYPE to current
360 grammar_current_rule_merge_set (struniq_t name
, location_t location
)
363 warn_at (location
, _("%s affects only GLR parsers"), "%merge");
364 if (current_rule
->merger
!= 0)
365 complain_at (location
, _("only one %s allowed per rule"), "%merge");
366 current_rule
->merger
=
367 get_merge_function (name
, current_rule
->sym
->type_name
, location
);
370 /* Attach a SYMBOL to the current rule. If needed, move the previous
371 action as a mid-rule action. */
374 grammar_current_rule_symbol_append (symbol_t
*symbol
, location_t location
)
376 if (current_rule
->action
)
377 grammar_midrule_action ();
379 grammar_symbol_append (symbol
, location
);
382 /* Attach an ACTION to the current rule. If needed, move the previous
383 action as a mid-rule action. */
386 grammar_current_rule_action_append (const char *action
, location_t location
)
388 if (current_rule
->action
)
389 grammar_midrule_action ();
390 current_rule
->action
= action
;
391 current_rule
->action_location
= location
;
395 /*---------------------------------------------------------------.
396 | Convert the rules into the representation using RRHS, RLHS and |
398 `---------------------------------------------------------------*/
403 unsigned int itemno
= 0;
404 rule_number_t ruleno
= 0;
405 symbol_list_t
*p
= grammar
;
407 ritem
= XCALLOC (item_number_t
, nritems
);
408 rules
= XCALLOC (rule_t
, nrules
);
412 symbol_t
*ruleprec
= p
->ruleprec
;
413 rules
[ruleno
].user_number
= ruleno
;
414 rules
[ruleno
].number
= ruleno
;
415 rules
[ruleno
].lhs
= p
->sym
;
416 rules
[ruleno
].rhs
= ritem
+ itemno
;
417 rules
[ruleno
].location
= p
->location
;
418 rules
[ruleno
].useful
= true;
419 rules
[ruleno
].action
= p
->action
;
420 rules
[ruleno
].action_location
= p
->action_location
;
421 rules
[ruleno
].dprec
= p
->dprec
;
422 rules
[ruleno
].merger
= p
->merger
;
427 /* item_number_t = symbol_number_t.
428 But the former needs to contain more: negative rule numbers. */
429 ritem
[itemno
++] = symbol_number_as_item_number (p
->sym
->number
);
430 /* A rule gets by default the precedence and associativity
431 of the last token in it. */
432 if (p
->sym
->class == token_sym
)
433 rules
[ruleno
].prec
= p
->sym
;
438 /* If this rule has a %prec,
439 the specified symbol's precedence replaces the default. */
442 rules
[ruleno
].precsym
= ruleprec
;
443 rules
[ruleno
].prec
= ruleprec
;
445 ritem
[itemno
++] = rule_number_as_item_number (ruleno
);
452 assert (itemno
== nritems
);
454 if (trace_flag
& trace_sets
)
455 ritem_print (stderr
);
458 /*------------------------------------------------------------------.
459 | Read in the grammar specification and record it in the format |
460 | described in gram.h. All actions are copied into ACTION_OBSTACK, |
461 | in each case forming the body of a C function (YYACTION) which |
462 | contains a switch statement to decide which action to execute. |
463 `------------------------------------------------------------------*/
468 gram_control_t gram_control
;
470 /* Initialize the symbol table. */
473 /* Construct the accept symbol. */
474 accept
= symbol_get ("$accept", empty_location
);
475 accept
->class = nterm_sym
;
476 accept
->number
= nvars
++;
478 /* Construct the error token */
479 errtoken
= symbol_get ("error", empty_location
);
480 errtoken
->class = token_sym
;
481 errtoken
->number
= ntokens
++;
483 /* Construct a token that represents all undefined literal tokens.
484 It is always token number 2. */
485 undeftoken
= symbol_get ("$undefined", empty_location
);
486 undeftoken
->class = token_sym
;
487 undeftoken
->number
= ntokens
++;
489 /* Initialize the obstacks. */
490 obstack_init (&pre_prologue_obstack
);
491 obstack_init (&post_prologue_obstack
);
493 finput
= xfopen (grammar_file
, "r");
496 gram__flex_debug
= trace_flag
& trace_scan
;
497 gram_debug
= trace_flag
& trace_parse
;
498 scanner_initialize ();
499 gram_parse (&gram_control
);
501 /* If something went wrong during the parsing, don't try to
503 if (complaint_issued
)
506 /* Grammar has been read. Do some checking */
508 fatal (_("no rules in the input grammar"));
510 /* Report any undefined symbols and consider them nonterminals. */
511 symbols_check_defined ();
513 /* If the user did not define her ENDTOKEN, do it now. */
516 endtoken
= symbol_get ("$end", empty_location
);
517 endtoken
->class = token_sym
;
518 endtoken
->number
= 0;
519 /* Value specified by POSIX. */
520 endtoken
->user_token_number
= 0;
523 /* Insert the initial rule, which line is that of the first rule
524 (not that of the start symbol):
526 accept: %start EOF. */
528 symbol_list_t
*p
= symbol_list_new (accept
, empty_location
);
529 p
->location
= grammar
->location
;
530 p
->next
= symbol_list_new (startsymbol
, empty_location
);
531 p
->next
->next
= symbol_list_new (endtoken
, empty_location
);
532 p
->next
->next
->next
= symbol_list_new (NULL
, empty_location
);
533 p
->next
->next
->next
->next
= grammar
;
539 if (SYMBOL_NUMBER_MAX
< nsyms
)
540 fatal (_("too many symbols (tokens plus nonterminals); maximum %d"),
543 assert (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
);