From 44536b35c464a01320ce13a835e6ba75d2c6fd00 Mon Sep 17 00:00:00 2001 From: Akim Demaille Date: Mon, 10 Jun 2002 08:37:55 +0000 Subject: [PATCH] * 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. --- ChangeLog | 8 +++++ src/reader.c | 82 +++++++++++++++++++--------------------------------- src/symtab.c | 44 ++++++++++++++++++++++++++++ src/symtab.h | 7 +++++ 4 files changed, 89 insertions(+), 52 deletions(-) diff --git a/ChangeLog b/ChangeLog index 19a390d9..77ce89f5 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,11 @@ +2002-06-10 Akim Demaille + + * 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. + 2002-06-10 Akim Demaille Remove `%thong' support as it is undocumented, unused, duplicates diff --git a/src/reader.c b/src/reader.c index 8447f904..5587c32a 100644 --- a/src/reader.c +++ b/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; - - 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++; - - if (typename) + break; + + case tok_identifier: + if (*symval->tag == '\"' && symbol) { - if (symbol->type_name == NULL) - symbol->type_name = typename; - else if (strcmp (typename, symbol->type_name) != 0) - complain (_("type redeclaration for %s"), symbol->tag); + symbol_make_alias (symbol, symval, typename); + symbol = NULL; } - } - else if (symbol && token == tok_number) - { - symbol->user_token_number = numval; - /* User defined EOF token? */ - if (numval == 0) + else { - eoftoken = symbol; - eoftoken->number = 0; - /* It is always mapped to 0, so it was already counted in - NTOKENS. */ - --ntokens; + symbol = symval; + symbol_class_set (symbol, class); + if (typename) + symbol_type_set (symbol, typename); } - } - else - { + break; + + 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: diff --git a/src/symtab.c b/src/symtab.c index 08571d83..c921133d 100644 --- a/src/symtab.c +++ b/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. | `------------*/ diff --git a/src/symtab.h b/src/symtab.h index 4ca81465..eb45f1de 100644 --- a/src/symtab.h +++ b/src/symtab.h @@ -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. */ -- 2.45.2