bison-patches
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

25-fyi-tokendef.patch


From: Akim Demaille
Subject: 25-fyi-tokendef.patch
Date: Sat, 29 Dec 2001 15:15:18 +0100

Index: ChangeLog
from  Akim Demaille  <address@hidden>
        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.
        
        
Index: src/muscle_tab.c
--- src/muscle_tab.c Sat, 08 Dec 2001 17:14:16 +0100 akim
+++ src/muscle_tab.c Fri, 28 Dec 2001 18:05:12 +0100 akim
@@ -58,9 +58,6 @@
   muscle_insert ("stype", "int");
   muscle_insert ("ltype", "yyltype");
 
-  /* Tokens.  */
-  muscle_insert ("tokendef", NULL);
-
   /* Tables.  */
   muscle_insert ("rhs", NULL);
   muscle_insert ("pact", NULL);
Index: src/output.c
--- src/output.c Fri, 28 Dec 2001 16:47:09 +0100 akim
+++ src/output.c Fri, 28 Dec 2001 18:42:41 +0100 akim
@@ -567,6 +567,54 @@
 }
 
 
+/*---------------------------------------.
+| 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 @@
            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 @@
 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
Index: src/reader.c
--- src/reader.c Fri, 28 Dec 2001 17:49:11 +0100 akim
+++ src/reader.c Fri, 28 Dec 2001 18:04:02 +0100 akim
@@ -1659,56 +1659,6 @@
 
 
 /*---------------------------------------------------------------.
-| 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 @@
   /* 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 ();



reply via email to

[Prev in Thread] Current Thread [Next in Thread]