]> git.saurik.com Git - bison.git/blobdiff - src/output.c
* src/lex.c (read_typename): New function.
[bison.git] / src / output.c
index 4bb09cd77f45238365ea2d06d578eccb0eef949f..a426ad8f94f5bcddb5706296a49777c1f07103a6 100644 (file)
 
 #include "system.h"
 #include "getargs.h"
-#include "alloc.h"
+#include "xalloc.h"
 #include "files.h"
 #include "gram.h"
-#include "state.h"
+#include "LR0.h"
 #include "complain.h"
 #include "output.h"
+#include "lalr.h"
+#include "reader.h"
+#include "conflicts.h"
+
+extern void berror PARAMS((const char *));
+
 
-extern char **tags;
-extern int *user_toknums;
-extern int tokensetsize;
-extern int final_state;
-extern core **state_table;
-extern shifts **shift_table;
-extern errs **err_table;
-extern reductions **reduction_table;
-extern short *accessing_symbol;
-extern unsigned *LA;
-extern short *LAruleno;
-extern short *lookaheads;
-extern char *consistent;
-extern short *goto_map;
-extern short *from_state;
-extern short *to_state;
-
-extern void reader_output_yylsp PARAMS ((FILE *));
 
 static int nvectors;
 static int nentries;
@@ -136,6 +124,44 @@ static int high;
 
 
 
+static inline void
+output_short_table (FILE *out,
+                   const char *table_name,
+                   short *short_table,
+                   short first_value,
+                   short begin, short end)
+{
+  int i, j;
+
+  fprintf (out, "static const short %s[] = {%6d", table_name, first_value);
+
+  j = 10;
+  for (i = begin; i < end; i++)
+    {
+      putc (',', out);
+
+      if (j >= 10)
+       {
+         putc ('\n', out);
+         j = 1;
+       }
+      else
+       {
+         j++;
+       }
+
+      fprintf (out, "%6d", short_table[i]);
+    }
+
+  fprintf (out, "\n};\n");
+}
+
+
+/*--------------------------------------------------------------.
+| output_headers -- Output constant strings to the beginning of |
+| certain files.                                                |
+`--------------------------------------------------------------*/
+
 #define        GUARDSTR        \
 "\n\
 #include \"%s\"\n\
@@ -171,18 +197,13 @@ register YYLTYPE *yylsp;\n\
 
 #define        ACTSTR_SIMPLE   "\n  switch (yyn) {\n"
 
-
-/*------------------------------------------------------------.
-| Output constant strings to the beginning of certain files.  |
-`------------------------------------------------------------*/
-
 void
 output_headers (void)
 {
   if (semantic_parser)
     fprintf (fguard, GUARDSTR, attrsfile);
 
-  if (noparserflag)
+  if (no_parser_flag)
     return;
 
   fprintf (faction, (semantic_parser ? ACTSTR : ACTSTR_SIMPLE), attrsfile);
@@ -217,7 +238,7 @@ output_trailers (void)
 
   fprintf (faction, "\n");
 
-  if (noparserflag)
+  if (no_parser_flag)
     return;
 
   if (semantic_parser)
@@ -274,37 +295,17 @@ output_token_translations (void)
 static void
 output_gram (void)
 {
-  int i;
   int j;
   short *sp;
 
   /* With the ordinary parser,
      yyprhs and yyrhs are needed only for yydebug. */
-  /* With the noparser option, all tables are generated */
-  if (!semantic_parser && !noparserflag)
-    fprintf (ftable, "\n#if YYDEBUG != 0");
+  /* With the no_parser option, all tables are generated */
+  if (!semantic_parser && !no_parser_flag)
+    fprintf (ftable, "\n#if YYDEBUG != 0\n");
 
-  fprintf (ftable, "\nstatic const short yyprhs[] = {     0");
-
-  j = 10;
-  for (i = 1; i <= nrules; i++)
-    {
-      putc (',', ftable);
-
-      if (j >= 10)
-       {
-         putc ('\n', ftable);
-         j = 1;
-       }
-      else
-       {
-         j++;
-       }
-
-      fprintf (ftable, "%6d", rrhs[i]);
-    }
-
-  fprintf (ftable, "\n};\n");
+  output_short_table (ftable, "yyprhs", rrhs,
+                     0, 1, nrules + 1);
 
   fprintf (ftable, "\nstatic const short yyrhs[] = {%6d", ritem[0]);
 
@@ -331,7 +332,7 @@ output_gram (void)
 
   fprintf (ftable, "\n};\n");
 
-  if (!semantic_parser && !noparserflag)
+  if (!semantic_parser && !no_parser_flag)
     fprintf (ftable, "\n#endif\n");
 }
 
@@ -339,30 +340,8 @@ output_gram (void)
 static void
 output_stos (void)
 {
-  int i;
-  int j;
-
-  fprintf (ftable, "\nstatic const short yystos[] = {     0");
-
-  j = 10;
-  for (i = 1; i < nstates; i++)
-    {
-      putc (',', ftable);
-
-      if (j >= 10)
-       {
-         putc ('\n', ftable);
-         j = 1;
-       }
-      else
-       {
-         j++;
-       }
-
-      fprintf (ftable, "%6d", accessing_symbol[i]);
-    }
-
-  fprintf (ftable, "\n};\n");
+  output_short_table (ftable, "yystos", accessing_symbol,
+                     0, 1, nstates);
 }
 
 
@@ -374,29 +353,15 @@ output_rule_data (void)
 
   fputs ("\n\
 #if YYDEBUG != 0\n\
-/* YYRLINE[yyn]: source line where rule number YYN was defined. */\n\
-static const short yyrline[] = { 0", ftable);
+/* YYRLINE[yyn]: source line where rule number YYN was defined. */\n",
+        ftable);
 
-  j = 10;
-  for (i = 1; i <= nrules; i++)
-    {
-      putc (',', ftable);
+  output_short_table (ftable, "yyrline", rline,
+                     0, 1, nrules + 1);
 
-      if (j >= 10)
-       {
-         putc ('\n', ftable);
-         j = 1;
-       }
-      else
-       {
-         j++;
-       }
-
-      fprintf (ftable, "%6d", rline[i]);
-    }
-  fprintf (ftable, "\n};\n#endif\n\n");
+  fputs ("#endif\n\n", ftable);
 
-  if (toknumflag || noparserflag)
+  if (token_table_flag || no_parser_flag)
     {
       fprintf (ftable, "#define YYNTOKENS %d\n", ntokens);
       fprintf (ftable, "#define YYNNTS %d\n", nvars);
@@ -405,7 +370,7 @@ static const short yyrline[] = { 0", ftable);
       fprintf (ftable, "#define YYMAXUTOK %d\n\n", max_user_token_number);
     }
 
-  if (!toknumflag && !noparserflag)
+  if (!token_table_flag && !no_parser_flag)
     fprintf (ftable, "\n#if YYDEBUG != 0 || defined (YYERROR_VERBOSE)\n\n");
 
   /* Output the table of symbol names.  */
@@ -471,55 +436,25 @@ static const short yyrline[] = { 0", ftable);
   /* add a NULL entry to list of tokens */
   fprintf (ftable, ", NULL\n};\n");
 
-  if (!toknumflag && !noparserflag)
+  if (!token_table_flag && !no_parser_flag)
     fprintf (ftable, "#endif\n\n");
 
   /* Output YYTOKNUM. */
-  if (toknumflag)
+  if (token_table_flag)
     {
-      fprintf (ftable, "static const short yytoknum[] = { 0");
-      j = 10;
-      for (i = 1; i <= ntokens; i++)
-       {
-         putc (',', ftable);
-         if (j >= 10)
-           {
-             putc ('\n', ftable);
-             j = 1;
-           }
-         else
-           j++;
-         fprintf (ftable, "%6d", user_toknums[i]);
-       }
-      fprintf (ftable, "\n};\n\n");
+      output_short_table (ftable, "yytoknum", user_toknums,
+                         0, 1, ntokens + 1);
     }
 
   /* Output YYR1. */
   fputs ("\
-/* YYR1[YYN]: Symbol number of symbol that rule YYN derives. */\n\
-static const short yyr1[] = {     0", ftable);
+/* YYR1[YYN]: Symbol number of symbol that rule YYN derives. */\n", ftable);
 
-  j = 10;
-  for (i = 1; i <= nrules; i++)
-    {
-      putc (',', ftable);
+  output_short_table (ftable, "yyr1", rlhs,
+                     0, 1, nrules + 1);
+  XFREE (rlhs + 1);
 
-      if (j >= 10)
-       {
-         putc ('\n', ftable);
-         j = 1;
-       }
-      else
-       {
-         j++;
-       }
-
-      fprintf (ftable, "%6d", rlhs[i]);
-    }
-  FREE (rlhs + 1);
-  fputs ("\n\
-};\n\
-\n", ftable);
+  putc ('\n', ftable);
 
   /* Output YYR2. */
   fputs ("\
@@ -548,7 +483,7 @@ static const short yyr2[] = {     0", ftable);
     putc ('\n', ftable);
 
   fprintf (ftable, "%6d\n};\n", nitems - rrhs[nrules] - 1);
-  FREE (rrhs + 1);
+  XFREE (rrhs + 1);
 }
 
 
@@ -764,8 +699,8 @@ save_row (int state)
   if (count == 0)
     return;
 
-  froms[state] = sp1 = sp = NEW2 (count, short);
-  tos[state] = sp2 = NEW2 (count, short);
+  froms[state] = sp1 = sp = XCALLOC (short, count);
+  tos[state] = sp2 = XCALLOC (short, count);
 
   for (i = 0; i < ntokens; i++)
     {
@@ -785,45 +720,27 @@ save_row (int state)
 | Figure out the actions for the specified state, indexed by        |
 | lookahead token type.                                             |
 |                                                                   |
-| The yydefact table is output now.  The detailed info is saved for |
-| putting into yytable later.                                       |
+| The YYDEFACT table is output now.  The detailed info is saved for |
+| putting into YYTABLE later.                                       |
 `------------------------------------------------------------------*/
 
 static void
 token_actions (void)
 {
   int i;
-  int j;
-  int k;
-
-  actrow = NEW2 (ntokens, short);
+  short *yydefact = XCALLOC (short, nstates);
 
-  k = action_row (0);
-  fprintf (ftable, "\nstatic const short yydefact[] = {%6d", k);
-  save_row (0);
-
-  j = 10;
-  for (i = 1; i < nstates; i++)
+  actrow = XCALLOC (short, ntokens);
+  for (i = 0; i < nstates; ++i)
     {
-      putc (',', ftable);
-
-      if (j >= 10)
-       {
-         putc ('\n', ftable);
-         j = 1;
-       }
-      else
-       {
-         j++;
-       }
-
-      k = action_row (i);
-      fprintf (ftable, "%6d", k);
+      yydefact[i] = action_row (i);
       save_row (i);
     }
+  XFREE (actrow);
 
-  fprintf (ftable, "\n};\n");
-  FREE (actrow);
+  output_short_table (ftable, "yydefact", yydefact,
+                     yydefact[0], 1, nstates);
+  XFREE (yydefact);
 }
 
 
@@ -832,12 +749,12 @@ free_shifts (void)
 {
   shifts *sp, *sptmp;  /* JF derefrenced freed ptr */
 
-  FREE (shift_table);
+  XFREE (shift_table);
 
   for (sp = first_shift; sp; sp = sptmp)
     {
       sptmp = sp->next;
-      FREE (sp);
+      XFREE (sp);
     }
 }
 
@@ -847,12 +764,12 @@ free_reductions (void)
 {
   reductions *rp, *rptmp;      /* JF fixed freed ptr */
 
-  FREE (reduction_table);
+  XFREE (reduction_table);
 
   for (rp = first_reduction; rp; rp = rptmp)
     {
       rptmp = rp->next;
-      FREE (rp);
+      XFREE (rp);
     }
 }
 
@@ -885,8 +802,8 @@ save_column (int symbol, int default_state)
 
   symno = symbol - ntokens + nstates;
 
-  froms[symno] = sp1 = sp = NEW2 (count, short);
-  tos[symno] = sp2 = NEW2 (count, short);
+  froms[symno] = sp1 = sp = XCALLOC (short, count);
+  tos[symno] = sp2 = XCALLOC (short, count);
 
   for (i = m; i < n; i++)
     {
@@ -952,7 +869,7 @@ goto_actions (void)
 {
   int i, j, k;
 
-  state_count = NEW2 (nstates, short);
+  state_count = XCALLOC (short, nstates);
 
   k = default_goto (ntokens);
   fprintf (ftable, "\nstatic const short yydefgoto[] = {%6d", k);
@@ -979,7 +896,7 @@ goto_actions (void)
     }
 
   fprintf (ftable, "\n};\n");
-  FREE (state_count);
+  XFREE (state_count);
 }
 
 
@@ -995,7 +912,7 @@ sort_actions (void)
   int t;
   int w;
 
-  order = NEW2 (nvectors, short);
+  order = XCALLOC (short, nvectors);
   nentries = 0;
 
   for (i = 0; i < nvectors; i++)
@@ -1076,8 +993,7 @@ pack_vector (int vector)
   i = order[vector];
   t = tally[i];
 
-  if (t == 0)
-    berror ("pack_vector");
+  assert (t);
 
   from = froms[i];
   to = tos[i];
@@ -1133,10 +1049,10 @@ pack_table (void)
   int place;
   int state;
 
-  base = NEW2 (nvectors, short);
-  pos = NEW2 (nentries, short);
-  table = NEW2 (MAXTABLE, short);
-  check = NEW2 (MAXTABLE, short);
+  base = XCALLOC (short, nvectors);
+  pos = XCALLOC (short, nentries);
+  table = XCALLOC (short, MAXTABLE);
+  check = XCALLOC (short, MAXTABLE);
 
   lowzero = 0;
   high = 0;
@@ -1163,14 +1079,14 @@ pack_table (void)
   for (i = 0; i < nvectors; i++)
     {
       if (froms[i])
-       FREE (froms[i]);
+       XFREE (froms[i]);
       if (tos[i])
-       FREE (tos[i]);
+       XFREE (tos[i]);
     }
 
-  FREE (froms);
-  FREE (tos);
-  FREE (pos);
+  XFREE (froms);
+  XFREE (tos);
+  XFREE (pos);
 }
 
 /* the following functions output yytable, yycheck
@@ -1179,115 +1095,34 @@ pack_table (void)
 static void
 output_base (void)
 {
-  int i;
-  int j;
+  output_short_table (ftable, "yypact", base,
+                     base[0], 1, nstates);
 
-  fprintf (ftable, "\nstatic const short yypact[] = {%6d", base[0]);
+  putc ('\n', ftable);
 
-  j = 10;
-  for (i = 1; i < nstates; i++)
-    {
-      putc (',', ftable);
+  output_short_table (ftable, "yypgoto", base,
+                     base[nstates], nstates + 1, nvectors);
 
-      if (j >= 10)
-       {
-         putc ('\n', ftable);
-         j = 1;
-       }
-      else
-       {
-         j++;
-       }
-
-      fprintf (ftable, "%6d", base[i]);
-    }
-
-  fprintf (ftable, "\n};\n\nstatic const short yypgoto[] = {%6d",
-          base[nstates]);
-
-  j = 10;
-  for (i = nstates + 1; i < nvectors; i++)
-    {
-      putc (',', ftable);
-
-      if (j >= 10)
-       {
-         putc ('\n', ftable);
-         j = 1;
-       }
-      else
-       {
-         j++;
-       }
-
-      fprintf (ftable, "%6d", base[i]);
-    }
-
-  fprintf (ftable, "\n};\n");
-  FREE (base);
+  XFREE (base);
 }
 
 
 static void
 output_table (void)
 {
-  int i;
-  int j;
-
-  fprintf (ftable, "\n\n#define\tYYLAST\t\t%d\n\n", high);
-  fprintf (ftable, "\nstatic const short yytable[] = {%6d", table[0]);
-
-  j = 10;
-  for (i = 1; i <= high; i++)
-    {
-      putc (',', ftable);
-
-      if (j >= 10)
-       {
-         putc ('\n', ftable);
-         j = 1;
-       }
-      else
-       {
-         j++;
-       }
-
-      fprintf (ftable, "%6d", table[i]);
-    }
-
-  fprintf (ftable, "\n};\n");
-  FREE (table);
+  fprintf (ftable, "\n\n#define\tYYLAST\t\t%d\n\n\n", high);
+  output_short_table (ftable, "yytable", table,
+                     table[0], 1, high + 1);
+  XFREE (table);
 }
 
 
 static void
 output_check (void)
 {
-  int i;
-  int j;
-
-  fprintf (ftable, "\nstatic const short yycheck[] = {%6d", check[0]);
-
-  j = 10;
-  for (i = 1; i <= high; i++)
-    {
-      putc (',', ftable);
-
-      if (j >= 10)
-       {
-         putc ('\n', ftable);
-         j = 1;
-       }
-      else
-       {
-         j++;
-       }
-
-      fprintf (ftable, "%6d", check[i]);
-    }
-
-  fprintf (ftable, "\n};\n");
-  FREE (check);
+  output_short_table (ftable, "yycheck", check,
+                     check[0], 1, high + 1);
+  XFREE (check);
 }
 
 /* compute and output yydefact, yydefgoto, yypact, yypgoto, yytable
@@ -1298,28 +1133,30 @@ output_actions (void)
 {
   nvectors = nstates + nvars;
 
-  froms = NEW2 (nvectors, short *);
-  tos = NEW2 (nvectors, short *);
-  tally = NEW2 (nvectors, short);
-  width = NEW2 (nvectors, short);
+  froms = XCALLOC (short *, nvectors);
+  tos = XCALLOC (short *, nvectors);
+  tally = XCALLOC (short, nvectors);
+  width = XCALLOC (short, nvectors);
 
   token_actions ();
   free_shifts ();
   free_reductions ();
-  FREE (lookaheads);
-  FREE (LA);
-  FREE (LAruleno);
-  FREE (accessing_symbol);
+  XFREE (lookaheads);
+  XFREE (LA);
+  XFREE (LAruleno);
+  XFREE (accessing_symbol);
 
   goto_actions ();
-  FREE (goto_map + ntokens);
-  FREE (from_state);
-  FREE (to_state);
+  XFREE (goto_map + ntokens);
+  XFREE (from_state);
+  XFREE (to_state);
 
   sort_actions ();
   pack_table ();
+  putc ('\n', ftable);
   output_base ();
   output_table ();
+  putc ('\n', ftable);
   output_check ();
 }
 
@@ -1329,6 +1166,7 @@ static void
 output_parser (void)
 {
   int c;
+  static int number_of_dollar_signs = 0;
 #ifdef DONTDEF
   FILE *fpars;
 #else
@@ -1338,8 +1176,9 @@ output_parser (void)
   if (pure_parser)
     fprintf (ftable, "#define YYPURE 1\n\n");
 
-#ifdef DONTDEF                 /* JF no longer needed 'cuz open_extra_files changes the
-                                  currently open parser from bison.simple to bison.hairy */
+#ifdef DONTDEF
+  /* JF no longer needed 'cuz open_extra_files changes the currently
+     open parser from bison.simple to bison.hairy */
   if (semantic_parser)
     fpars = fparser;
   else
@@ -1356,7 +1195,7 @@ output_parser (void)
 
       /* See if the line starts with `#line.
          If so, set write_line to 0.  */
-      if (nolinesflag)
+      if (no_lines_flag)
        if (c == '#')
          {
            c = getc (fpars);
@@ -1390,11 +1229,15 @@ output_parser (void)
          {
            if (c == '$')
              {
+               number_of_dollar_signs++;
+               assert (number_of_dollar_signs == 1);
                /* `$' in the parser file indicates where to put the actions.
                   Copy them in at this point.  */
                rewind (faction);
                for (c = getc (faction); c != EOF; c = getc (faction))
                  putc (c, ftable);
+               /* Skip the end of the line containing `$'. */
+               write_line = 0;
              }
            else
              putc (c, ftable);
@@ -1403,6 +1246,7 @@ output_parser (void)
        break;
       putc (c, ftable);
     }
+  assert (number_of_dollar_signs == 1);
 }
 
 static void
@@ -1410,7 +1254,7 @@ output_program (void)
 {
   int c;
 
-  if (!nolinesflag)
+  if (!no_lines_flag)
     fprintf (ftable, "#line %d \"%s\"\n", lineno, infile);
 
   c = getc (finput);
@@ -1427,12 +1271,12 @@ free_itemsets (void)
 {
   core *cp, *cptmp;
 
-  FREE (state_table);
+  XFREE (state_table);
 
   for (cp = first_state; cp; cp = cptmp)
     {
       cptmp = cp->next;
-      FREE (cp);
+      XFREE (cp);
     }
 }
 
@@ -1454,10 +1298,10 @@ output (void)
        putc (c, ftable);
     }
   reader_output_yylsp (ftable);
-  if (debugflag)
+  if (debug_flag)
     fputs ("\
 #ifndef YYDEBUG\n\
-#define YYDEBUG 1\n\
+# define YYDEBUG 1\n\
 #endif\n\
 \n",
           ftable);
@@ -1465,7 +1309,7 @@ output (void)
   if (semantic_parser)
     fprintf (ftable, "#include \"%s\"\n", attrsfile);
 
-  if (!noparserflag)
+  if (!no_parser_flag)
     fprintf (ftable, "#include <stdio.h>\n\n");
 
   /* Make "const" do nothing if not in ANSI C.  */
@@ -1484,12 +1328,12 @@ output (void)
 /*   if (semantic_parser) */
   /* This is now unconditional because debugging printouts can use it.  */
   output_gram ();
-  FREE (ritem);
+  XFREE (ritem);
   if (semantic_parser)
     output_stos ();
   output_rule_data ();
   output_actions ();
-  if (!noparserflag)
+  if (!no_parser_flag)
     output_parser ();
   output_program ();
 }