* src/symlist.h, src/symlist.c (symbol_list_length): New.

* src/scan-gram.l (handle_dollar, handle_at): Compute the
rule_length only when needed.
* src/output.c (actions_output, token_definitions_output): Output
the full M4 block.
* src/symtab.c: Don't access directly to the symbol tag, use
symbol_tag_get.
* src/parse-gram.y: Use symbol_list_free.
This commit is contained in:
Akim Demaille
2002-06-17 07:05:12 +00:00
parent 56c4720342
commit dafdc66ff0
9 changed files with 77 additions and 58 deletions

View File

@@ -1,3 +1,15 @@
2002-06-17 Akim Demaille <akim@epita.fr>
* src/symlist.h, src/symlist.c (symbol_list_length): New.
* src/scan-gram.l (handle_dollar, handle_at): Compute the
rule_length only when needed.
* src/output.c (actions_output, token_definitions_output): Output
the full M4 block.
* src/symtab.c: Don't access directly to the symbol tag, use
symbol_tag_get.
* src/parse-gram.y: Use symbol_list_free.
2002-06-17 Akim Demaille <akim@epita.fr> 2002-06-17 Akim Demaille <akim@epita.fr>
* src/reader.h, src/reader.c (symbol_list, symbol_list_new) * src/reader.h, src/reader.c (symbol_list, symbol_list_new)

View File

@@ -537,6 +537,8 @@ void
actions_output (FILE *out) actions_output (FILE *out)
{ {
int rule; int rule;
fputs ("m4_define([b4_actions], \n[[", out);
for (rule = 1; rule < nrules + 1; ++rule) for (rule = 1; rule < nrules + 1; ++rule)
if (rules[rule].action) if (rules[rule].action)
{ {
@@ -550,6 +552,7 @@ actions_output (FILE *out)
fprintf (out, " %s\n break;\n\n", fprintf (out, " %s\n break;\n\n",
rules[rule].action); rules[rule].action);
} }
fputs ("]])\n\n", out);
} }
@@ -562,6 +565,8 @@ token_definitions_output (FILE *out)
{ {
int i; int i;
int first = 1; int first = 1;
fputs ("m4_define([b4_tokens], \n[", out);
for (i = 0; i < ntokens; ++i) for (i = 0; i < ntokens; ++i)
{ {
symbol_t *symbol = symbols[i]; symbol_t *symbol = symbols[i];
@@ -597,6 +602,7 @@ token_definitions_output (FILE *out)
first = 0; first = 0;
} }
fputs ("])\n\n", out);
} }
@@ -1011,13 +1017,8 @@ output_skeleton (void)
fputs ("m4_changecom()\n", out); fputs ("m4_changecom()\n", out);
fputs ("m4_init()\n", out); fputs ("m4_init()\n", out);
fputs ("m4_define([b4_actions], \n[[", out);
actions_output (out); actions_output (out);
fputs ("]])\n\n", out);
fputs ("m4_define([b4_tokens], \n[", out);
token_definitions_output (out); token_definitions_output (out);
fputs ("])\n\n", out);
muscles_m4_output (out); muscles_m4_output (out);

View File

@@ -1319,7 +1319,7 @@ yyreduce:
symbol_list_t *list; symbol_list_t *list;
for (list = yyvsp[0].list; list; list = list->next) for (list = yyvsp[0].list; list; list = list->next)
symbol_type_set (list->sym, list->location, yyvsp[-1].string); symbol_type_set (list->sym, list->location, yyvsp[-1].string);
LIST_FREE (symbol_list_t, yyvsp[0].list); symbol_list_free (yyvsp[0].list);
} }
break; break;
@@ -1333,7 +1333,7 @@ yyreduce:
symbol_type_set (list->sym, list->location, current_type); symbol_type_set (list->sym, list->location, current_type);
symbol_precedence_set (list->sym, list->location, current_prec, yyvsp[-2].assoc); symbol_precedence_set (list->sym, list->location, current_prec, yyvsp[-2].assoc);
} }
LIST_FREE (symbol_list_t, yyvsp[0].list); symbol_list_free (yyvsp[0].list);
current_type = NULL; current_type = NULL;
} }
break; break;

View File

@@ -218,7 +218,7 @@ symbol_declaration:
symbol_list_t *list; symbol_list_t *list;
for (list = $3; list; list = list->next) for (list = $3; list; list = list->next)
symbol_type_set (list->sym, list->location, $2); symbol_type_set (list->sym, list->location, $2);
LIST_FREE (symbol_list_t, $3); symbol_list_free ($3);
} }
; ;
@@ -232,7 +232,7 @@ precedence_declaration:
symbol_type_set (list->sym, list->location, current_type); symbol_type_set (list->sym, list->location, current_type);
symbol_precedence_set (list->sym, list->location, current_prec, $1); symbol_precedence_set (list->sym, list->location, current_prec, $1);
} }
LIST_FREE (symbol_list_t, $3); symbol_list_free ($3);
current_type = NULL; current_type = NULL;
} }
; ;

View File

@@ -2705,15 +2705,6 @@ handle_dollar (char *cp, location_t location)
{ {
const char *type_name = NULL; const char *type_name = NULL;
/* RULE_LENGTH is the number of values in the current rule so far,
which says where to find `$0' with respect to the top of the
stack. It is not the same as the rule->length in the case of mid
rule actions. */
int rule_length = 0;
symbol_list_t *rhs;
for (rhs = current_rule->next; rhs; rhs = rhs->next)
++rule_length;
++cp; ++cp;
/* Get the type name if explicit. */ /* Get the type name if explicit. */
@@ -2740,6 +2731,11 @@ handle_dollar (char *cp, location_t location)
} }
else if (isdigit (*cp) || *cp == '-') else if (isdigit (*cp) || *cp == '-')
{ {
/* RULE_LENGTH is the number of values in the current rule so
far, which says where to find `$0' with respect to the top of
the stack. It is not the same as the rule->length in the
case of mid rule actions. */
int rule_length = symbol_list_length (current_rule->next);
int n = strtol (cp, &cp, 10); int n = strtol (cp, &cp, 10);
if (n > rule_length) if (n > rule_length)
@@ -2775,15 +2771,6 @@ handle_dollar (char *cp, location_t location)
static void static void
handle_at (char *cp) handle_at (char *cp)
{ {
/* RULE_LENGTH is the number of values in the current rule so far,
which says where to find `$0' with respect to the top of the
stack. It is not the same as the rule->length in the case of mid
rule actions. */
int rule_length = 0;
symbol_list_t *rhs;
for (rhs = current_rule->next; rhs; rhs = rhs->next)
++rule_length;
locations_flag = 1; locations_flag = 1;
++cp; ++cp;
@@ -2793,7 +2780,13 @@ handle_at (char *cp)
} }
else if (isdigit (*cp) || *cp == '-') else if (isdigit (*cp) || *cp == '-')
{ {
/* RULE_LENGTH is the number of values in the current rule so
far, which says where to find `$0' with respect to the top of
the stack. It is not the same as the rule->length in the
case of mid rule actions. */
int rule_length = symbol_list_length (current_rule->next);
int n = strtol (cp, &cp, 10); int n = strtol (cp, &cp, 10);
if (n > rule_length) if (n > rule_length)
complain (_("invalid value: %s%d"), "@", n); complain (_("invalid value: %s%d"), "@", n);
else else

View File

@@ -524,15 +524,6 @@ handle_dollar (char *cp, location_t location)
{ {
const char *type_name = NULL; const char *type_name = NULL;
/* RULE_LENGTH is the number of values in the current rule so far,
which says where to find `$0' with respect to the top of the
stack. It is not the same as the rule->length in the case of mid
rule actions. */
int rule_length = 0;
symbol_list_t *rhs;
for (rhs = current_rule->next; rhs; rhs = rhs->next)
++rule_length;
++cp; ++cp;
/* Get the type name if explicit. */ /* Get the type name if explicit. */
@@ -559,6 +550,11 @@ handle_dollar (char *cp, location_t location)
} }
else if (isdigit (*cp) || *cp == '-') else if (isdigit (*cp) || *cp == '-')
{ {
/* RULE_LENGTH is the number of values in the current rule so
far, which says where to find `$0' with respect to the top of
the stack. It is not the same as the rule->length in the
case of mid rule actions. */
int rule_length = symbol_list_length (current_rule->next);
int n = strtol (cp, &cp, 10); int n = strtol (cp, &cp, 10);
if (n > rule_length) if (n > rule_length)
@@ -594,15 +590,6 @@ handle_dollar (char *cp, location_t location)
static void static void
handle_at (char *cp) handle_at (char *cp)
{ {
/* RULE_LENGTH is the number of values in the current rule so far,
which says where to find `$0' with respect to the top of the
stack. It is not the same as the rule->length in the case of mid
rule actions. */
int rule_length = 0;
symbol_list_t *rhs;
for (rhs = current_rule->next; rhs; rhs = rhs->next)
++rule_length;
locations_flag = 1; locations_flag = 1;
++cp; ++cp;
@@ -612,7 +599,13 @@ handle_at (char *cp)
} }
else if (isdigit (*cp) || *cp == '-') else if (isdigit (*cp) || *cp == '-')
{ {
/* RULE_LENGTH is the number of values in the current rule so
far, which says where to find `$0' with respect to the top of
the stack. It is not the same as the rule->length in the
case of mid rule actions. */
int rule_length = symbol_list_length (current_rule->next);
int n = strtol (cp, &cp, 10); int n = strtol (cp, &cp, 10);
if (n > rule_length) if (n > rule_length)
complain (_("invalid value: %s%d"), "@", n); complain (_("invalid value: %s%d"), "@", n);
else else

View File

@@ -64,6 +64,20 @@ symbol_list_free (symbol_list_t *list)
} }
/*--------------------.
| Return its length. |
`--------------------*/
unsigned int
symbol_list_length (symbol_list_t *list)
{
int res = 0;
for (/* Nothing. */; list; list = list->next)
++res;
return res;
}
/*--------------------------------------------------------------. /*--------------------------------------------------------------.
| Get the data type (alternative in the union) of the value for | | Get the data type (alternative in the union) of the value for |
| symbol N in rule RULE. | | symbol N in rule RULE. |

View File

@@ -51,6 +51,8 @@ symbol_list_t * symbol_list_prepend PARAMS ((symbol_list_t *list,
/* Free the LIST, but not the symbols it contains. */ /* Free the LIST, but not the symbols it contains. */
void symbol_list_free PARAMS ((symbol_list_t *list)); void symbol_list_free PARAMS ((symbol_list_t *list));
/* Return its length. */
unsigned int symbol_list_length PARAMS ((symbol_list_t *list));
/* Get the data type (alternative in the union) of the value for /* Get the data type (alternative in the union) of the value for
symbol N in rule RULE. */ symbol N in rule RULE. */

View File

@@ -108,7 +108,7 @@ symbol_type_set (symbol_t *symbol, location_t location, char *type_name)
{ {
if (symbol->type_name) if (symbol->type_name)
complain_at (location, complain_at (location,
_("type redeclaration for %s"), symbol->tag); _("type redeclaration for %s"), symbol_tag_get (symbol));
symbol->type_name = type_name; symbol->type_name = type_name;
} }
} }
@@ -127,7 +127,8 @@ symbol_precedence_set (symbol_t *symbol, location_t location,
{ {
if (symbol->prec != 0) if (symbol->prec != 0)
complain_at (location, complain_at (location,
_("redefining precedence of %s"), symbol->tag); _("redefining precedence of %s"),
symbol_tag_get (symbol));
symbol->prec = prec; symbol->prec = prec;
symbol->assoc = assoc; symbol->assoc = assoc;
} }
@@ -145,7 +146,7 @@ void
symbol_class_set (symbol_t *symbol, symbol_class class) symbol_class_set (symbol_t *symbol, symbol_class class)
{ {
if (symbol->class != unknown_sym && symbol->class != class) if (symbol->class != unknown_sym && symbol->class != class)
complain (_("symbol %s redefined"), symbol->tag); complain (_("symbol %s redefined"), symbol_tag_get (symbol));
if (class == nterm_sym && symbol->class != nterm_sym) if (class == nterm_sym && symbol->class != nterm_sym)
symbol->number = nvars++; symbol->number = nvars++;
@@ -167,7 +168,8 @@ symbol_user_token_number_set (symbol_t *symbol, int user_token_number)
if (symbol->user_token_number != USER_NUMBER_UNDEFINED if (symbol->user_token_number != USER_NUMBER_UNDEFINED
&& symbol->user_token_number != user_token_number) && symbol->user_token_number != user_token_number)
complain (_("redefining user token number of %s"), symbol->tag); complain (_("redefining user token number of %s"),
symbol_tag_get (symbol));
symbol->user_token_number = user_token_number; symbol->user_token_number = user_token_number;
/* User defined EOF token? */ /* User defined EOF token? */
@@ -212,7 +214,7 @@ symbol_check_defined (symbol_t *this)
complain_at complain_at
(this->location, (this->location,
_("symbol %s is used, but is not defined as a token and has no rules"), _("symbol %s is used, but is not defined as a token and has no rules"),
this->tag); symbol_tag_get (this));
this->class = nterm_sym; this->class = nterm_sym;
this->number = nvars++; this->number = nvars++;
} }
@@ -231,10 +233,10 @@ symbol_make_alias (symbol_t *symbol, symbol_t *symval)
{ {
if (symval->alias) if (symval->alias)
warn (_("symbol `%s' used more than once as a literal string"), warn (_("symbol `%s' used more than once as a literal string"),
symval->tag); symbol_tag_get (symval));
else if (symbol->alias) else if (symbol->alias)
warn (_("symbol `%s' given more than one literal string"), warn (_("symbol `%s' given more than one literal string"),
symbol->tag); symbol_tag_get (symbol));
else else
{ {
symval->class = token_sym; symval->class = token_sym;
@@ -267,7 +269,7 @@ symbol_check_alias_consistence (symbol_t *this)
{ {
if (this->prec != 0 && this->alias->prec != 0) if (this->prec != 0 && this->alias->prec != 0)
complain (_("conflicting precedences for %s and %s"), complain (_("conflicting precedences for %s and %s"),
this->tag, this->alias->tag); symbol_tag_get (this), symbol_tag_get (this->alias));
if (this->prec != 0) if (this->prec != 0)
this->alias->prec = this->prec; this->alias->prec = this->prec;
else else
@@ -282,7 +284,7 @@ symbol_check_alias_consistence (symbol_t *this)
if (this->assoc != right_assoc if (this->assoc != right_assoc
&& this->alias->assoc != right_assoc) && this->alias->assoc != right_assoc)
complain (_("conflicting associativities for %s and %s"), complain (_("conflicting associativities for %s and %s"),
this->tag, this->alias->tag); symbol_tag_get (this), symbol_tag_get (this->alias));
if (this->assoc != 0) if (this->assoc != 0)
this->alias->assoc = this->assoc; this->alias->assoc = this->assoc;
else else
@@ -350,8 +352,8 @@ symbol_translation (symbol_t *this)
/* A token which translation has already been set? */ /* A token which translation has already been set? */
if (token_translations[this->user_token_number] != undeftoken->number) if (token_translations[this->user_token_number] != undeftoken->number)
complain (_("tokens %s and %s both assigned number %d"), complain (_("tokens %s and %s both assigned number %d"),
symbols[token_translations[this->user_token_number]]->tag, symbol_tag_get (symbols[token_translations[this->user_token_number]]),
this->tag, this->user_token_number); symbol_tag_get (this), this->user_token_number);
token_translations[this->user_token_number] = this->number; token_translations[this->user_token_number] = this->number;
} }
@@ -529,8 +531,10 @@ symbols_pack (void)
if (startsymbol->class == unknown_sym) if (startsymbol->class == unknown_sym)
fatal_at (startsymbol_location, fatal_at (startsymbol_location,
_("the start symbol %s is undefined"), startsymbol->tag); _("the start symbol %s is undefined"),
symbol_tag_get (startsymbol));
else if (startsymbol->class == token_sym) else if (startsymbol->class == token_sym)
fatal_at (startsymbol_location, fatal_at (startsymbol_location,
_("the start symbol %s is a token"), startsymbol->tag); _("the start symbol %s is a token"),
symbol_tag_get (startsymbol));
} }