]>
git.saurik.com Git - bison.git/blob - src/reader.c
1 /* Input parser for Bison
3 Copyright (C) 1984, 1986, 1989, 1992, 1998, 2000, 2001, 2002
4 Free Software Foundation, Inc.
6 This file is part of Bison, the GNU Compiler Compiler.
8 Bison is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
13 Bison is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with Bison; see the file COPYING. If not, write to
20 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
28 #include "conflicts.h"
32 #include "muscle_tab.h"
38 static symbol_list
*grammar
= NULL
;
39 static int start_flag
= 0;
40 merger_list
*merge_functions
;
42 /* Nonzero if %union has been seen. */
45 /*-----------------------.
46 | Set the start symbol. |
47 `-----------------------*/
50 grammar_start_symbol_set (symbol
*s
, location loc
)
53 complain_at (loc
, _("multiple %s declarations"), "%start");
58 startsymbol_location
= loc
;
63 /*----------------------------------------------------------------.
64 | There are two prologues: one before %union, one after. Augment |
66 `----------------------------------------------------------------*/
69 prologue_augment (const char *prologue
, location loc
)
71 struct obstack
*oout
=
72 !typed
? &pre_prologue_obstack
: &post_prologue_obstack
;
74 obstack_fgrow1 (oout
, "]b4_syncline([[%d]], [[", loc
.start
.line
);
75 MUSCLE_OBSTACK_SGROW (oout
,
76 quotearg_style (c_quoting_style
, loc
.start
.file
));
77 obstack_sgrow (oout
, "]])[\n");
78 obstack_sgrow (oout
, prologue
);
84 /*----------------------.
85 | Handle the epilogue. |
86 `----------------------*/
89 epilogue_augment (const char *epilogue
, location loc
)
91 char *extension
= NULL
;
92 obstack_fgrow1 (&muscle_obstack
, "]b4_syncline([[%d]], [[", loc
.start
.line
);
93 MUSCLE_OBSTACK_SGROW (&muscle_obstack
,
94 quotearg_style (c_quoting_style
, loc
.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 (uniqstr name
, uniqstr type
, location loc
)
123 type
= uniqstr_new ("");
125 head
.next
= merge_functions
;
126 for (syms
= &head
, n
= 1; syms
->next
!= NULL
; syms
= syms
->next
, n
+= 1)
127 if (UNIQSTR_EQ (name
, syms
->next
->name
))
129 if (syms
->next
== NULL
)
131 MALLOC (syms
->next
, 1);
132 syms
->next
->name
= uniqstr_new (name
);
133 syms
->next
->type
= uniqstr_new (type
);
134 syms
->next
->next
= NULL
;
135 merge_functions
= head
.next
;
137 else if (!UNIQSTR_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 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
*grammar_end
= NULL
;
184 /* Append S to the GRAMMAR. */
186 grammar_symbol_append (symbol
*sym
, location loc
)
188 symbol_list
*p
= symbol_list_new (sym
, loc
);
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
*current_rule
= NULL
;
202 symbol_list
*previous_rule_end
= NULL
;
205 /*----------------------------------------------.
206 | Create a new rule for LHS in to the GRAMMAR. |
207 `----------------------------------------------*/
210 grammar_rule_begin (symbol
*lhs
, location loc
)
215 startsymbol_location
= loc
;
219 /* Start a new rule and record its lhs. */
223 previous_rule_end
= grammar_end
;
224 grammar_symbol_append (lhs
, loc
);
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 (loc
, _("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
*lhs
= current_rule
->sym
;
246 char const *lhs_type
= lhs
->type_name
;
247 symbol
*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 (!UNIQSTR_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 loc
)
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
= loc
;
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 dummy_location
= current_rule
->action_location
;
306 symbol
*dummy
= dummy_symbol_get (dummy_location
);
307 symbol_list
*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
*precsym
, location loc
)
340 if (current_rule
->ruleprec
)
341 complain_at (loc
, _("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 loc
)
351 warn_at (loc
, _("%s affects only GLR parsers"), "%dprec");
353 complain_at (loc
, _("%s must be followed by positive number"), "%dprec");
354 else if (current_rule
->dprec
!= 0)
355 complain_at (loc
, _("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 (uniqstr name
, location loc
)
366 warn_at (loc
, _("%s affects only GLR parsers"), "%merge");
367 if (current_rule
->merger
!= 0)
368 complain_at (loc
, _("only one %s allowed per rule"), "%merge");
369 current_rule
->merger
=
370 get_merge_function (name
, current_rule
->sym
->type_name
, loc
);
373 /* Attach SYM to the current rule. If needed, move the previous
374 action as a mid-rule action. */
377 grammar_current_rule_symbol_append (symbol
*sym
, location loc
)
379 if (current_rule
->action
)
380 grammar_midrule_action ();
382 grammar_symbol_append (sym
, loc
);
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 loc
)
391 if (current_rule
->action
)
392 grammar_midrule_action ();
393 current_rule
->action
= action
;
394 current_rule
->action_location
= loc
;
398 /*---------------------------------------------------------------.
399 | Convert the rules into the representation using RRHS, RLHS and |
401 `---------------------------------------------------------------*/
406 unsigned int itemno
= 0;
407 rule_number ruleno
= 0;
408 symbol_list
*p
= grammar
;
410 CALLOC (ritem
, nritems
);
411 CALLOC (rules
, nrules
);
415 symbol
*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 = symbol_number.
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 /* Initialize the symbol table. */
475 /* Construct the accept symbol. */
476 accept
= symbol_get ("$accept", empty_location
);
477 accept
->class = nterm_sym
;
478 accept
->number
= nvars
++;
480 /* Construct the error token */
481 errtoken
= symbol_get ("error", empty_location
);
482 errtoken
->class = token_sym
;
483 errtoken
->number
= ntokens
++;
485 /* Construct a token that represents all undefined literal tokens.
486 It is always token number 2. */
487 undeftoken
= symbol_get ("$undefined", empty_location
);
488 undeftoken
->class = token_sym
;
489 undeftoken
->number
= ntokens
++;
491 /* Initialize the obstacks. */
492 obstack_init (&pre_prologue_obstack
);
493 obstack_init (&post_prologue_obstack
);
495 finput
= xfopen (grammar_file
, "r");
498 gram__flex_debug
= trace_flag
& trace_scan
;
499 gram_debug
= trace_flag
& trace_parse
;
500 scanner_initialize ();
503 /* If something went wrong during the parsing, don't try to
505 if (complaint_issued
)
508 /* Grammar has been read. Do some checking */
510 fatal (_("no rules in the input grammar"));
512 /* Report any undefined symbols and consider them nonterminals. */
513 symbols_check_defined ();
515 /* If the user did not define her ENDTOKEN, do it now. */
518 endtoken
= symbol_get ("$end", empty_location
);
519 endtoken
->class = token_sym
;
520 endtoken
->number
= 0;
521 /* Value specified by POSIX. */
522 endtoken
->user_token_number
= 0;
525 /* Insert the initial rule, which line is that of the first rule
526 (not that of the start symbol):
528 accept: %start EOF. */
530 symbol_list
*p
= symbol_list_new (accept
, empty_location
);
531 p
->location
= grammar
->location
;
532 p
->next
= symbol_list_new (startsymbol
, empty_location
);
533 p
->next
->next
= symbol_list_new (endtoken
, empty_location
);
534 p
->next
->next
->next
= symbol_list_new (NULL
, empty_location
);
535 p
->next
->next
->next
->next
= grammar
;
541 if (! (nsyms
<= SYMBOL_NUMBER_MAXIMUM
&& nsyms
== ntokens
+ nvars
))
546 /* Assign the symbols their symbol numbers. Write #defines for the
547 token symbols into FDEFINES if requested. */
550 /* Convert the grammar into the format described in gram.h. */
553 /* The grammar as a symbol_list is no longer needed. */
554 LIST_FREE (symbol_list
, grammar
);