X-Git-Url: https://git.saurik.com/bison.git/blobdiff_plain/e3233bf6b6d787d3d7d69ed62c7eb435a9b15934..d2a1a60ab6b257c875146a8a23a5a2d1f9ede3be:/src/reader.c diff --git a/src/reader.c b/src/reader.c index 877e2c71..33b62c2c 100644 --- a/src/reader.c +++ b/src/reader.c @@ -1,7 +1,7 @@ /* Input parser for Bison Copyright (C) 1984, 1986, 1989, 1992, 1998, 2000, 2001, 2002, 2003, - 2005 Free Software Foundation, Inc. + 2005, 2006 Free Software Foundation, Inc. This file is part of Bison, the GNU Compiler Compiler. @@ -22,6 +22,7 @@ #include #include "system.h" +#include #include @@ -31,10 +32,13 @@ #include "getargs.h" #include "gram.h" #include "muscle_tab.h" -#include "output.h" #include "reader.h" #include "symlist.h" #include "symtab.h" +#include "scan-gram.h" +#include "scan-code.h" + +static void check_and_convert_grammar (void); static symbol_list *grammar = NULL; static bool start_flag = false; @@ -76,6 +80,8 @@ prologue_augment (const char *prologue, location loc) !typed ? &pre_prologue_obstack : &post_prologue_obstack; obstack_fgrow1 (oout, "]b4_syncline(%d, [[", loc.start.line); + /* FIXME: Protection of M4 characters missing here. See + output.c:escaped_output. */ MUSCLE_OBSTACK_SGROW (oout, quotearg_style (c_quoting_style, loc.start.file)); obstack_sgrow (oout, "]])[\n"); @@ -210,6 +216,19 @@ grammar_current_rule_begin (symbol *lhs, location loc) } +/*----------------------------------------------------------------------. +| A symbol should be used if it has a destructor, or if it is a | +| mid-rule symbol (i.e., the generated LHS replacing a mid-rule | +| action) that was assigned to, as in "exp: { $$ = 1; } { $$ = $1; }". | +`----------------------------------------------------------------------*/ + +static bool +symbol_should_be_used (symbol_list const *s) +{ + return (s->sym->destructor + || (s->midrule && s->midrule->used)); +} + /*----------------------------------------------------------------. | Check that the rule R is properly defined. For instance, there | | should be no type clash on the default action. | @@ -245,19 +264,21 @@ grammar_rule_check (const symbol_list *r) _("empty rule for typed nonterminal, and no action")); } - /* Check that typed symbol values are used. */ + /* Check that symbol values that should be used are in fact used. */ { - symbol_list *l = r; + symbol_list const *l = r; int n = 0; for (; l && l->sym; l = l->next, ++n) if (! (l->used - || !l->sym->type_name + || !symbol_should_be_used (l) /* The default action, $$ = $1, `uses' both. */ - || !r->action && (n == 0 || n == 1))) - if (n) - warn_at (r->location, _("unused value: $%d"), n); - else - warn_at (r->location, _("unset value: $$")); + || (!r->action && (n == 0 || n == 1)))) + { + if (n) + warn_at (r->location, _("unused value: $%d"), n); + else + warn_at (r->location, _("unset value: $$")); + } } } @@ -283,7 +304,7 @@ grammar_current_rule_end (location loc) | rule. | `-------------------------------------------------------------------*/ -static void +void grammar_midrule_action (void) { /* Since the action was written out with this rule's number, we must @@ -305,6 +326,10 @@ grammar_midrule_action (void) midrule->action = current_rule->action; midrule->action_location = dummy_location; current_rule->action = NULL; + /* If $$ was used in the action, the LHS of the enclosing rule was + incorrectly flagged as used. */ + midrule->used = current_rule->used; + current_rule->used = false; if (previous_rule_end) previous_rule_end->next = midrule; @@ -312,14 +337,16 @@ grammar_midrule_action (void) grammar = midrule; /* End the dummy's rule. */ - previous_rule_end = symbol_list_new (NULL, dummy_location); - previous_rule_end->next = current_rule; + midrule->next = symbol_list_new (NULL, dummy_location); + grammar_rule_check (midrule); + midrule->next->next = current_rule; - midrule->next = previous_rule_end; + previous_rule_end = midrule->next; /* Insert the dummy nonterminal replacing the midrule action into - the current rule. */ + the current rule. Bind it to its dedicated rule. */ grammar_current_rule_symbol_append (dummy, dummy_location); + grammar_end->midrule = midrule; } /* Set the precedence symbol of the current rule to PRECSYM. */ @@ -371,15 +398,12 @@ grammar_current_rule_symbol_append (symbol *sym, location loc) grammar_symbol_append (sym, loc); } -/* Attach an ACTION to the current rule. If needed, move the previous - action as a mid-rule action. */ +/* Attach an ACTION to the current rule. */ void grammar_current_rule_action_append (const char *action, location loc) { - if (current_rule->action) - grammar_midrule_action (); - current_rule->action = action; + current_rule->action = translate_rule_action (current_rule, action, loc); current_rule->action_location = loc; } @@ -396,11 +420,16 @@ packgram (void) rule_number ruleno = 0; symbol_list *p = grammar; - ritem = xnmalloc (nritems, sizeof *ritem); + ritem = xnmalloc (nritems + 1, sizeof *ritem); + + /* This sentinel is used by build_relations in gram.c. */ + *ritem++ = 0; + rules = xnmalloc (nrules, sizeof *rules); while (p) { + int rule_length = 0; symbol *ruleprec = p->ruleprec; rules[ruleno].user_number = ruleno; rules[ruleno].number = ruleno; @@ -415,18 +444,22 @@ packgram (void) rules[ruleno].action = p->action; rules[ruleno].action_location = p->action_location; - p = p->next; - while (p && p->sym) + for (p = p->next; p && p->sym; p = p->next) { + ++rule_length; + + /* Don't allow rule_length == INT_MAX, since that might + cause confusion with strtol if INT_MAX == LONG_MAX. */ + if (rule_length == INT_MAX) + fatal_at (rules[ruleno].location, _("rule is too long")); + /* item_number = symbol_number. But the former needs to contain more: negative rule numbers. */ ritem[itemno++] = symbol_number_as_item_number (p->sym->number); /* A rule gets by default the precedence and associativity - of the last token in it. */ + of its last token. */ if (p->sym->class == token_sym && default_prec) rules[ruleno].prec = p->sym; - if (p) - p = p->next; } /* If this rule has a %prec, @@ -436,15 +469,17 @@ packgram (void) rules[ruleno].precsym = ruleprec; rules[ruleno].prec = ruleprec; } + /* An item ends by the rule number (negated). */ ritem[itemno++] = rule_number_as_item_number (ruleno); + assert (itemno < ITEM_NUMBER_MAX); ++ruleno; + assert (ruleno < RULE_NUMBER_MAX); if (p) p = p->next; } - if (itemno != nritems) - abort (); + assert (itemno == nritems); if (trace_flag & trace_sets) ritem_print (stderr); @@ -487,15 +522,25 @@ reader (void) gram__flex_debug = trace_flag & trace_scan; gram_debug = trace_flag & trace_parse; - scanner_initialize (); + gram_scanner_initialize (); gram_parse (); - /* If something went wrong during the parsing, don't try to - continue. */ - if (complaint_issued) - return; + if (! complaint_issued) + check_and_convert_grammar (); + + xfclose (gram_in); +} + + +/*-------------------------------------------------------------. +| Check the grammar that has just been read, and convert it to | +| internal form. | +`-------------------------------------------------------------*/ - /* Grammar has been read. Do some checking */ +static void +check_and_convert_grammar (void) +{ + /* Grammar has been read. Do some checking. */ if (nrules == 0) fatal (_("no rules in the input grammar")); @@ -512,7 +557,7 @@ reader (void) endtoken->user_token_number = 0; } - /* Insert the initial rule, which line is that of the first rule + /* Insert the initial rule, whose line is that of the first rule (not that of the start symbol): accept: %start EOF. */ @@ -528,10 +573,7 @@ reader (void) grammar = p; } - if (! (nsyms <= SYMBOL_NUMBER_MAXIMUM && nsyms == ntokens + nvars)) - abort (); - - xfclose (gram_in); + assert (nsyms <= SYMBOL_NUMBER_MAXIMUM && nsyms == ntokens + nvars); /* Assign the symbols their symbol numbers. Write #defines for the token symbols into FDEFINES if requested. */