Enable declaration of default %printer/%destructor. Make the parser

use these for all user-declared grammar symbols for which the user does
not declare a specific %printer/%destructor.  Thus, the parser uses it
for token 0 if the user declares it but not if Bison generates it as
$end.  Discussed starting at
<http://lists.gnu.org/archive/html/bison-patches/2006-02/msg00064.html>,
<http://lists.gnu.org/archive/html/bison-patches/2006-06/msg00091.html>,
and
<http://lists.gnu.org/archive/html/bison-patches/2006-07/msg00019.html>.
* NEWS (2.3+): Mention.
* doc/bison.texinfo (Actions in Mid-Rule): It's no longer impossible to
declare a %destructor for a mid-rule's semantic value.  It's just
impossible to declare one specific to it.
(Freeing Discarded Symbols): Mention that @$ can be used in %destructor
code.  Describe default %destructor form.
* src/parse-gram.y (grammar_declaration): Parse default
%printer/%destructor declarations.
* src/output.c (symbol_destructors_output): Use symbol_destructor_get
and symbol_destructor_location_get rather than accessing the destructor
and destructor_location members of struct symbol.
(symbol_printers_output): Likewise but for %printer's.
* src/reader.c (symbol_should_be_used): Likewise but for %destructor's
again.
* src/symtab.c (default_destructor, default_destructor_location,
default_printer, default_printer_location): New static global
variables to record the default %destructor and %printer.
(symbol_destructor_get, symbol_destructor_location_get,
symbol_printer_get, symbol_printer_location_get): New functions to
compute the appropriate %destructor and %printer for a symbol.
(default_destructor_set, default_printer_set): New functions to set the
default %destructor and %printer.
* src/symtab.h: Prototype all those new functions.
* tests/actions.at (Default %printer and %destructor): New test to
check that the right %printer and %destructor are called, that they're
not called for $end, and that $$ and @$ work correctly.
(Default %printer and %destructor for user-declared end token): New
test to check that the default %printer and %destructor are called for
a user-declared end token.
* tests/input.at (Default %printer and %destructor redeclared, Unused
values with default %destructor): New tests to check related grammar
warnings and errors.
This commit is contained in:
Joel E. Denny
2006-07-29 05:53:42 +00:00
parent 868d2d9638
commit ec5479ce35
12 changed files with 771 additions and 276 deletions

View File

@@ -390,7 +390,7 @@ symbol_destructors_output (FILE *out)
fputs ("m4_define([b4_symbol_destructors], \n[", out);
for (i = 0; i < nsyms; ++i)
if (symbols[i]->destructor)
if (symbol_destructor_get (symbols[i]))
{
symbol *sym = symbols[i];
@@ -399,10 +399,12 @@ symbol_destructors_output (FILE *out)
destructor, optional typename. */
fprintf (out, "%s[", sep);
sep = ",\n";
escaped_output (out, sym->destructor_location.start.file);
fprintf (out, ", %d, ", sym->destructor_location.start.line);
escaped_output (out, symbol_destructor_location_get (sym).start.file);
fprintf (out, ", %d, ",
symbol_destructor_location_get (sym).start.line);
escaped_output (out, sym->tag);
fprintf (out, ", %d, [[%s]]", sym->number, sym->destructor);
fprintf (out, ", %d, [[%s]]", sym->number,
symbol_destructor_get (sym));
if (sym->type_name)
fprintf (out, ", [[%s]]", sym->type_name);
fputc (']', out);
@@ -423,7 +425,7 @@ symbol_printers_output (FILE *out)
fputs ("m4_define([b4_symbol_printers], \n[", out);
for (i = 0; i < nsyms; ++i)
if (symbols[i]->printer)
if (symbol_printer_get (symbols[i]))
{
symbol *sym = symbols[i];
@@ -432,10 +434,10 @@ symbol_printers_output (FILE *out)
printer, optional typename. */
fprintf (out, "%s[", sep);
sep = ",\n";
escaped_output (out, sym->printer_location.start.file);
fprintf (out, ", %d, ", sym->printer_location.start.line);
escaped_output (out, symbol_printer_location_get (sym).start.file);
fprintf (out, ", %d, ", symbol_printer_location_get (sym).start.line);
escaped_output (out, sym->tag);
fprintf (out, ", %d, [[%s]]", sym->number, sym->printer);
fprintf (out, ", %d, [[%s]]", sym->number, symbol_printer_get (sym));
if (sym->type_name)
fprintf (out, ", [[%s]]", sym->type_name);
fputc (']', out);

File diff suppressed because it is too large Load Diff

View File

@@ -159,7 +159,7 @@
#if ! defined YYSTYPE && ! defined YYSTYPE_IS_DECLARED
typedef union YYSTYPE
{/* Line 1539 of yacc.c. */
{/* Line 1535 of yacc.c. */
#line 97 "parse-gram.y"
symbol *symbol;
@@ -170,7 +170,7 @@ typedef union YYSTYPE
uniqstr uniqstr;
unsigned char character;
}
/* Line 1539 of yacc.c. */
/* Line 1535 of yacc.c. */
#line 175 "parse-gram.h"
YYSTYPE;
# define YYSTYPE_IS_TRIVIAL 1

View File

@@ -261,6 +261,10 @@ grammar_declaration:
{
grammar_start_symbol_set ($2, @2);
}
| "%destructor" "{...}"
{
default_destructor_set (translate_symbol_action ($2, @2), @2);
}
| "%destructor" "{...}" symbols.1
{
symbol_list *list;
@@ -277,6 +281,10 @@ grammar_declaration:
symbol_printer_set (list->sym, action, @2);
symbol_list_free ($3);
}
| "%printer" "{...}"
{
default_printer_set (translate_symbol_action ($2, @2), @2);
}
| "%default-prec"
{
default_prec = true;

View File

@@ -259,7 +259,7 @@ grammar_current_rule_begin (symbol *lhs, location loc)
static bool
symbol_should_be_used (symbol_list const *s)
{
return (s->sym->destructor
return (symbol_destructor_get (s->sym)
|| (s->midrule && s->midrule->used));
}

View File

@@ -41,6 +41,15 @@ symbol *accept = NULL;
symbol *startsymbol = NULL;
location startsymbol_location;
/*-----------------------------------.
| Default %destructor and %printer. |
`-----------------------------------*/
static const char *default_destructor = NULL;
static location default_destructor_location;
static const char *default_printer = NULL;
static location default_printer_location;
/*---------------------------------.
| Create a new symbol, named TAG. |
`---------------------------------*/
@@ -147,6 +156,33 @@ symbol_destructor_set (symbol *sym, const char *destructor, location loc)
}
}
/*---------------------------------------.
| Get the computed %destructor for SYM. |
`---------------------------------------*/
const char *
symbol_destructor_get (symbol *sym)
{
/* Token 0 cannot have a %destructor unless the user renames it. */
if (UNIQSTR_EQ (sym->tag, uniqstr_new ("$end")))
return NULL;
if (sym->destructor != NULL)
return sym->destructor;
return default_destructor;
}
/*---------------------------------------------------------------.
| Get the grammar location of the %destructor computed for SYM. |
`---------------------------------------------------------------*/
location
symbol_destructor_location_get (symbol *sym)
{
if (sym->destructor != NULL)
return sym->destructor_location;
return default_destructor_location;
}
/*---------------------------------------------------------------.
| Set the PRINTER associated with SYM. Do nothing if passed 0. |
@@ -164,6 +200,34 @@ symbol_printer_set (symbol *sym, const char *printer, location loc)
}
}
/*------------------------------------.
| Get the computed %printer for SYM. |
`------------------------------------*/
const char *
symbol_printer_get (symbol *sym)
{
/* Token 0 cannot have a %printer unless the user renames it. */
if (UNIQSTR_EQ (sym->tag, uniqstr_new ("$end")))
return NULL;
if (sym->printer != NULL)
return sym->printer;
return default_printer;
}
/*------------------------------------------------------------.
| Get the grammar location of the %printer computed for SYM. |
`------------------------------------------------------------*/
location
symbol_printer_location_get (symbol *sym)
{
if (sym->printer != NULL)
return sym->printer_location;
return default_printer_location;
}
/*-----------------------------------------------------------------.
| Set the PRECEDENCE associated with SYM. Does nothing if invoked |
@@ -666,3 +730,32 @@ symbols_pack (void)
_("the start symbol %s is a token"),
startsymbol->tag);
}
/*-----------------------------------.
| Set default %destructor/%printer. |
`-----------------------------------*/
void
default_destructor_set (const char *destructor, location loc)
{
if (default_destructor != NULL)
{
complain_at (loc, _("redeclaration for default %%destructor"));
complain_at (default_destructor_location, _("previous declaration"));
}
default_destructor = destructor;
default_destructor_location = loc;
}
void
default_printer_set (const char *printer, location loc)
{
if (default_printer != NULL)
{
complain_at (loc, _("redeclaration for default %%printer"));
complain_at (default_printer_location, _("previous declaration"));
}
default_printer = printer;
default_printer_location = loc;
}

View File

@@ -61,16 +61,35 @@ struct symbol
/** The location of its first occurrence. */
location location;
/** Its %type and associated printer and destructor. */
/** Its \c \%type. */
uniqstr type_name;
/** Its \c \%type's location. */
location type_location;
/** Does not own the memory. */
/** Any \c \%destructor declared specifically for this symbol.
Access this field only through <tt>symbol</tt>'s interface functions. For
example, if <tt>symbol::destructor = NULL</tt>, the default
\c \%destructor or a per-type \c \%destructor might be appropriate, and
\c symbol_destructor_get will compute the correct one. */
const char *destructor;
/** The location of \c symbol::destructor.
Access this field only through <tt>symbol</tt>'s interface functions.
\sa symbol::destructor */
location destructor_location;
/** Printer. */
/** Any \c \%printer declared specifically for this symbol.
Access this field only through <tt>symbol</tt>'s interface functions.
\sa symbol::destructor */
const char *printer;
/** The location of \c symbol::printer.
Access this field only through <tt>symbol</tt>'s interface functions.
\sa symbol::destructor */
location printer_location;
symbol_number number;
@@ -125,9 +144,25 @@ void symbol_type_set (symbol *sym, uniqstr type_name, location loc);
/** Set the \c destructor associated with \c sym. */
void symbol_destructor_set (symbol *sym, const char *destructor, location loc);
/** Get the computed \c \%destructor for \c sym, or \c NULL if none. */
const char *symbol_destructor_get (symbol *sym);
/** Get the grammar location of the computed \c \%destructor for \c sym.
\pre <tt>symbol_destructor_get (sym) != NULL</tt> */
location symbol_destructor_location_get (symbol *sym);
/** Set the \c printer associated with \c sym. */
void symbol_printer_set (symbol *sym, const char *printer, location loc);
/** Get the computed \c \%printer for \c sym, or \c NULL if none. */
const char *symbol_printer_get (symbol *sym);
/** Get the grammar location of the computed \c \%printer for \c sym.
\pre <tt>symbol_printer_get (sym) != NULL</tt> */
location symbol_printer_location_get (symbol *sym);
/* Set the \c precedence associated with \c sym.
Ensure that \a symbol is a terminal.
@@ -155,7 +190,7 @@ extern symbol *accept;
/** The user start symbol. */
extern symbol *startsymbol;
/** The location of the \c %start declaration. */
/** The location of the \c \%start declaration. */
extern location startsymbol_location;
@@ -181,4 +216,15 @@ void symbols_check_defined (void);
#token_translations. */
void symbols_pack (void);
/*-----------------------------------.
| Default %destructor and %printer. |
`-----------------------------------*/
/** Set the default \c \%destructor. */
void default_destructor_set (const char *destructor, location loc);
/** Set the default \c \%printer. */
void default_printer_set (const char *printer, location loc);
#endif /* !SYMTAB_H_ */