From 091e20bbee87fc6e5b1e9e2938767c6e89b19035 Mon Sep 17 00:00:00 2001 From: Akim Demaille Date: Sat, 29 Dec 2001 14:25:27 +0000 Subject: [PATCH] Don't store the token defs in a muscle, just be ready to output it on command. Now possible via `symbols'. Fixes a memory leak. * src/output.c (token_definitions_output): New. (output_parser, header_output): Use it. * src/reader.c (symbols_save): Remove. --- ChangeLog | 10 +++++++++ src/muscle_tab.c | 3 --- src/output.c | 53 +++++++++++++++++++++++++++++++++++++++++++++++- src/reader.c | 52 ----------------------------------------------- 4 files changed, 62 insertions(+), 56 deletions(-) diff --git a/ChangeLog b/ChangeLog index ebf6b024..70b78344 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,13 @@ +2001-12-29 Akim Demaille + + Don't store the token defs in a muscle, just be ready to output it + on command. Now possible via `symbols'. Fixes a memory leak. + + * src/output.c (token_definitions_output): New. + (output_parser, header_output): Use it. + * src/reader.c (symbols_save): Remove. + + 2001-12-29 Akim Demaille * src/bison.simple: Do not provide a default for YYSTYPE and diff --git a/src/muscle_tab.c b/src/muscle_tab.c index 53f83e46..47f73951 100644 --- a/src/muscle_tab.c +++ b/src/muscle_tab.c @@ -58,9 +58,6 @@ muscle_init (void) muscle_insert ("stype", "int"); muscle_insert ("ltype", "yyltype"); - /* Tokens. */ - muscle_insert ("tokendef", NULL); - /* Tables. */ muscle_insert ("rhs", NULL); muscle_insert ("pact", NULL); diff --git a/src/output.c b/src/output.c index 45af9b7e..60b79790 100644 --- a/src/output.c +++ b/src/output.c @@ -567,6 +567,54 @@ guards_output (FILE *out, size_t *line) } +/*---------------------------------------. +| Output the tokens definition to OOUT. | +`---------------------------------------*/ + +static void +token_definitions_output (FILE *out, size_t *line) +{ + int i; + for (i = 0; i < ntokens; ++i) + { + bucket *symbol = symbols[i]; + int number = symbol->user_token_number; + + if (number == SALIAS) + continue; + /* Skip error token. */ + if (symbol->value == error_token_number) + continue; + if (symbol->tag[0] == '\'') + continue; /* skip literal character */ + if (symbol->tag[0] == '\"') + { + /* use literal string only if given a symbol with an alias */ + if (symbol->alias) + symbol = symbol->alias; + else + continue; + } + + /* Don't #define nonliteral tokens whose names contain periods + or '$' (as does the default value of the EOF token). */ + if (strchr (symbol->tag, '.') || strchr (symbol->tag, '$')) + continue; + + fprintf (out, "# define %s\t%d\n", + symbol->tag, number); + ++*line; + if (semantic_parser) + { + /* FIXME: This is probably wrong, and should be just as + above. --akim. */ + fprintf (out, "# define T%s\t%d\n", symbol->tag, symbol->value); + ++*line; + } + } +} + + static void save_column (int symbol, int default_state) { @@ -963,6 +1011,8 @@ output_parser (const char *skel_filename, FILE *out) guards_output (out, &output_line); else if (!strcmp (muscle_key, "line")) fprintf (out, "%d", output_line); + else if (!strcmp (muscle_key, "tokendef")) + token_definitions_output (out, &output_line); else if (!strcmp (muscle_key, "skeleton-line")) fprintf (out, "%d", skeleton_line); else if (muscle_value) @@ -1059,13 +1109,14 @@ prepare (void) static void header_output (void) { + size_t dummy_line; FILE *out = xfopen (spec_defines_file, "w"); char *macro_name = compute_header_macro (); fprintf (out, "#ifndef %s\n", macro_name); fprintf (out, "# define %s\n\n", macro_name); - fputs (muscle_find ("tokendef"), out); + token_definitions_output (out, &dummy_line); fprintf (out, "\ #ifndef YYSTYPE\n\ typedef %s diff --git a/src/reader.c b/src/reader.c index d5d04b22..9816c61d 100644 --- a/src/reader.c +++ b/src/reader.c @@ -1658,56 +1658,6 @@ packsymbols (void) } -/*---------------------------------------------------------------. -| Save the definition of token names in the `TOKENDEFS' muscle. | -`---------------------------------------------------------------*/ - -static void -symbols_save (void) -{ - struct obstack tokendefs; - bucket *bp; - obstack_init (&tokendefs); - - for (bp = firstsymbol; bp; bp = bp->next) - { - char *symbol = bp->tag; /* get symbol */ - - if (bp->value >= ntokens) - continue; - if (bp->user_token_number == SALIAS) - continue; - if ('\'' == *symbol) - continue; /* skip literal character */ - if (bp == errtoken) - continue; /* skip error token */ - if ('\"' == *symbol) - { - /* use literal string only if given a symbol with an alias */ - if (bp->alias) - symbol = bp->alias->tag; - else - continue; - } - - /* Don't #define nonliteral tokens whose names contain periods. */ - if (strchr (symbol, '.')) - continue; - - obstack_fgrow2 (&tokendefs, "# define %s\t%d\n", - symbol, bp->user_token_number); - if (semantic_parser) - /* FIXME: This is probably wrong, and should be just as - above. --akim. */ - obstack_fgrow2 (&tokendefs, "# define T%s\t%d\n", symbol, bp->value); - } - - obstack_1grow (&tokendefs, 0); - muscle_insert ("tokendef", xstrdup (obstack_finish (&tokendefs))); - obstack_free (&tokendefs, NULL); -} - - /*---------------------------------------------------------------. | Convert the rules into the representation using RRHS, RLHS and | | RITEMS. | @@ -1867,8 +1817,6 @@ reader (void) /* Assign the symbols their symbol numbers. Write #defines for the token symbols into FDEFINES if requested. */ packsymbols (); - /* Save them. */ - symbols_save (); /* Convert the grammar into the format described in gram.h. */ packgram (); -- 2.45.2