mirror of
https://git.savannah.gnu.org/git/bison.git
synced 2026-03-09 12:23:04 +00:00
* src/symtab.c, src/symtab.c (symbol_class_set)
(symbol_user_token_number_set): New. * src/reader.c (parse_token_decl): Use them. Use a switch instead of ifs. Use a single argument.
This commit is contained in:
86
src/reader.c
86
src/reader.c
@@ -483,14 +483,12 @@ copy_definition (struct obstack *oout)
|
||||
}
|
||||
|
||||
|
||||
/*-------------------------------------------------------------------.
|
||||
| Parse what comes after %token or %nterm. For %token, WHAT_IS is |
|
||||
| token_sym and WHAT_IS_NOT is nterm_sym. For %nterm, the arguments |
|
||||
| are reversed. |
|
||||
`-------------------------------------------------------------------*/
|
||||
/*------------------------------------------.
|
||||
| Parse what comes after %token or %nterm. |
|
||||
`------------------------------------------*/
|
||||
|
||||
static void
|
||||
parse_token_decl (symbol_class what_is, symbol_class what_is_not)
|
||||
parse_token_decl (symbol_class class)
|
||||
{
|
||||
token_t token = tok_undef;
|
||||
char *typename = NULL;
|
||||
@@ -512,60 +510,40 @@ parse_token_decl (symbol_class what_is, symbol_class what_is_not)
|
||||
fatal (_("Premature EOF after %s"), token_buffer);
|
||||
|
||||
token = lex ();
|
||||
if (token == tok_comma)
|
||||
switch (token)
|
||||
{
|
||||
case tok_comma:
|
||||
symbol = NULL;
|
||||
continue;
|
||||
}
|
||||
if (token == tok_typename)
|
||||
{
|
||||
break;
|
||||
|
||||
case tok_typename:
|
||||
typename = xstrdup (token_buffer);
|
||||
symbol = NULL;
|
||||
}
|
||||
else if (token == tok_identifier && *symval->tag == '\"' && symbol)
|
||||
{
|
||||
symbol_make_alias (symbol, symval, typename);
|
||||
symbol = NULL;
|
||||
}
|
||||
else if (token == tok_identifier)
|
||||
{
|
||||
int oldclass = symval->class;
|
||||
symbol = symval;
|
||||
break;
|
||||
|
||||
if (symbol->class == what_is_not)
|
||||
complain (_("symbol %s redefined"), symbol->tag);
|
||||
symbol->class = what_is;
|
||||
if (what_is == nterm_sym && oldclass != nterm_sym)
|
||||
symbol->number = nvars++;
|
||||
if (what_is == token_sym && symbol->number == NUMBER_UNDEFINED)
|
||||
symbol->number = ntokens++;
|
||||
case tok_identifier:
|
||||
if (*symval->tag == '\"' && symbol)
|
||||
{
|
||||
symbol_make_alias (symbol, symval, typename);
|
||||
symbol = NULL;
|
||||
}
|
||||
else
|
||||
{
|
||||
symbol = symval;
|
||||
symbol_class_set (symbol, class);
|
||||
if (typename)
|
||||
symbol_type_set (symbol, typename);
|
||||
}
|
||||
break;
|
||||
|
||||
if (typename)
|
||||
{
|
||||
if (symbol->type_name == NULL)
|
||||
symbol->type_name = typename;
|
||||
else if (strcmp (typename, symbol->type_name) != 0)
|
||||
complain (_("type redeclaration for %s"), symbol->tag);
|
||||
}
|
||||
}
|
||||
else if (symbol && token == tok_number)
|
||||
{
|
||||
symbol->user_token_number = numval;
|
||||
/* User defined EOF token? */
|
||||
if (numval == 0)
|
||||
{
|
||||
eoftoken = symbol;
|
||||
eoftoken->number = 0;
|
||||
/* It is always mapped to 0, so it was already counted in
|
||||
NTOKENS. */
|
||||
--ntokens;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
case tok_number:
|
||||
symbol_user_token_number_set (symbol, numval);
|
||||
break;
|
||||
|
||||
default:
|
||||
complain (_("`%s' is invalid in %s"),
|
||||
token_buffer,
|
||||
(what_is == token_sym) ? "%token" : "%nterm");
|
||||
(class == token_sym) ? "%token" : "%nterm");
|
||||
skip_to_char ('%');
|
||||
}
|
||||
}
|
||||
@@ -922,11 +900,11 @@ read_declarations (void)
|
||||
break;
|
||||
|
||||
case tok_token:
|
||||
parse_token_decl (token_sym, nterm_sym);
|
||||
parse_token_decl (token_sym);
|
||||
break;
|
||||
|
||||
case tok_nterm:
|
||||
parse_token_decl (nterm_sym, token_sym);
|
||||
parse_token_decl (nterm_sym);
|
||||
break;
|
||||
|
||||
case tok_type:
|
||||
|
||||
44
src/symtab.c
44
src/symtab.c
@@ -87,6 +87,50 @@ symbol_precedence_set (symbol_t *symbol,
|
||||
}
|
||||
|
||||
|
||||
/*-------------------------------------.
|
||||
| Set the CLASS associated to SYMBOL. |
|
||||
`-------------------------------------*/
|
||||
|
||||
void
|
||||
symbol_class_set (symbol_t *symbol, symbol_class class)
|
||||
{
|
||||
if (symbol->class != unknown_sym && symbol->class != class)
|
||||
complain (_("symbol %s redefined"), symbol->tag);
|
||||
|
||||
if (class == nterm_sym && symbol->class != nterm_sym)
|
||||
symbol->number = nvars++;
|
||||
else if (class == token_sym && symbol->number == NUMBER_UNDEFINED)
|
||||
symbol->number = ntokens++;
|
||||
|
||||
symbol->class = class;
|
||||
}
|
||||
|
||||
|
||||
/*-------------------------------------------------.
|
||||
| Set the USER_TOKEN_NUMBER associated to SYMBOL. |
|
||||
`-------------------------------------------------*/
|
||||
|
||||
void
|
||||
symbol_user_token_number_set (symbol_t *symbol, int user_token_number)
|
||||
{
|
||||
assert (symbol->class == token_sym);
|
||||
|
||||
if (symbol->user_token_number != USER_NUMBER_UNDEFINED)
|
||||
complain (_("redefining user token number of %s"), symbol->tag);
|
||||
|
||||
symbol->user_token_number = user_token_number;
|
||||
/* User defined EOF token? */
|
||||
if (user_token_number == 0)
|
||||
{
|
||||
eoftoken = symbol;
|
||||
eoftoken->number = 0;
|
||||
/* It is always mapped to 0, so it was already counted in
|
||||
NTOKENS. */
|
||||
--ntokens;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*------------.
|
||||
| Free THIS. |
|
||||
`------------*/
|
||||
|
||||
@@ -96,6 +96,13 @@ void symbol_type_set PARAMS ((symbol_t *symbol, char *type_name));
|
||||
void symbol_precedence_set PARAMS ((symbol_t *symbol,
|
||||
int prec, associativity assoc));
|
||||
|
||||
/* Set the CLASS associated to SYMBOL. */
|
||||
void symbol_class_set PARAMS ((symbol_t *symbol, symbol_class class));
|
||||
|
||||
/* Set the USER_TOKEN_NUMBER associated to SYMBOL. */
|
||||
void symbol_user_token_number_set PARAMS ((symbol_t *symbol, int user_number));
|
||||
|
||||
|
||||
/* Distinguished symbols. AXIOM is the real start symbol, that used
|
||||
by the automaton. STARTSYMBOL is the one specified by the user.
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user