]> git.saurik.com Git - bison.git/blobdiff - src/output.c
* src/files.c, src/files.h (guard_obstack): Remove.
[bison.git] / src / output.c
index c35fa8701c7f2125990cb4e84fbdbf2198b3b783..43935f59697851b734b9dd42c39e8215b24f8d7f 100644 (file)
@@ -130,14 +130,14 @@ static size_t
 get_lines_number (const char *s)
 {
   size_t lines = 0;
-  
+
   size_t i;
   for (i = 0; s[i]; ++i)
     {
       if (s[i] == '\n')
        ++lines;
-    }   
-  
+    }
+
   return lines;
 }
 
@@ -148,8 +148,8 @@ static inline void
 output_table_data (struct obstack *oout,
                   short *table_data,
                   short first,
-                  short begin,
-                  short end)
+                  int begin,
+                  int end)
 {
   int i;
   int j = 1;
@@ -323,16 +323,14 @@ output_rule_data (void)
 `------------------------------------------------------------------*/
 
 static int
-action_row (int state)
+action_row (state_t *state)
 {
   int i;
-  int m = 0;
-  int n = 0;
   int default_rule = 0;
-  reductions *redp = state_table[state]->reductions;
+  reductions *redp = state->reductions;
   int nreds = redp ? redp->nreds : 0;
-  shifts *shiftp = state_table[state]->shifts;
-  errs *errp = state_table[state]->errs;
+  shifts *shiftp = state->shifts;
+  errs *errp = state->errs;
   /* set nonzero to inhibit having any default reduction */
   int nodefault = 0;
 
@@ -344,17 +342,14 @@ action_row (int state)
       int j;
       /* loop over all the rules available here which require
         lookahead */
-      m = state_table[state]->lookaheads;
-      n = state_table[state + 1]->lookaheads;
-
-      for (i = n - 1; i >= m; i--)
+      for (i = state->nlookaheads - 1; i >= 0; --i)
        /* and find each token which the rule finds acceptable
           to come next */
        for (j = 0; j < ntokens; j++)
          /* and record this rule as the rule to use if that
             token follows.  */
-         if (BITISSET (LA (i), j))
-           actrow[j] = -LAruleno[i];
+         if (BITISSET (LA (state->lookaheadsp + i), j))
+           actrow[j] = -LAruleno[state->lookaheadsp + i];
     }
 
   /* Now see which tokens are allowed for shifts in this state.  For
@@ -394,15 +389,15 @@ action_row (int state)
 
   if (nreds >= 1 && !nodefault)
     {
-      if (state_table[state]->consistent)
+      if (state->consistent)
        default_rule = redp->rules[0];
       else
        {
          int max = 0;
-         for (i = m; i < n; i++)
+         for (i = 0; i < state->nlookaheads; i++)
            {
              int count = 0;
-             int rule = -LAruleno[i];
+             int rule = -LAruleno[state->lookaheadsp + i];
              int j;
 
              for (j = 0; j < ntokens; j++)
@@ -492,7 +487,7 @@ token_actions (void)
   actrow = XCALLOC (short, ntokens);
   for (i = 0; i < nstates; ++i)
     {
-      yydefact[i] = action_row (i);
+      yydefact[i] = action_row (state_table[i]);
       save_row (i);
     }
 
@@ -541,6 +536,38 @@ actions_output (FILE *out, size_t *line)
 }
 
 
+/*----------------------------.
+| Output the guards to OOUT.  |
+`----------------------------*/
+
+static void
+guards_output (FILE *out, size_t *line)
+{
+  int rule;
+  for (rule = 1; rule < nrules + 1; ++rule)
+    if (rule_table[rule].action)
+      {
+       fprintf (out, "  case %d:\n", rule);
+
+       if (!no_lines_flag)
+         fprintf (out, muscle_find ("linef"),
+                  rule_table[rule].guard_line,
+                  quotearg_style (c_quoting_style,
+                                  muscle_find ("filename")));
+       fprintf (out, "{ %s; }\n    break;\n\n",
+                rule_table[rule].guard);
+
+       /* We always output 4 '\n' per action.  */
+       *line += 4;
+       /* Plus one if !no_lines_flag.  */
+       if (!no_lines_flag)
+         ++*line;
+       /* Get the number of lines written by the user.  */
+       *line += get_lines_number (rule_table[rule].guard);
+      }
+}
+
+
 static void
 save_column (int symbol, int default_state)
 {
@@ -755,8 +782,8 @@ pack_vector (int vector)
          return j;
        }
     }
-
-  assert (!"pack_vector");
+#define pack_vector_succeeded 0
+  assert (pack_vector_succeeded);
   return 0;
 }
 
@@ -897,19 +924,24 @@ output_parser (const char *skel_filename, FILE *out)
 {
   int c;
   FILE *fskel;
-  size_t line;
+  size_t output_line;
+  size_t skeleton_line;
 
   fskel = xfopen (skel_filename, "r");
 
   /* New output code.  */
-  line = 1;
+  output_line = 1;
+  skeleton_line = 1;
   c = getc (fskel);
   while (c != EOF)
     {
       if (c != '%')
        {
          if (c == '\n')
-           ++line;
+           {
+             ++output_line;
+             ++skeleton_line;
+           }
          putc (c, out);
          c = getc (fskel);
        }
@@ -927,14 +959,18 @@ output_parser (const char *skel_filename, FILE *out)
          muscle_key = obstack_finish (&muscle_obstack);
          muscle_value = muscle_find (muscle_key);
          if (!strcmp (muscle_key, "actions"))
-           actions_output (out, &line);
+           actions_output (out, &output_line);
+         else if (!strcmp (muscle_key, "guards"))
+           guards_output (out, &output_line);
          else if (!strcmp (muscle_key, "line"))
-           fprintf (out, "%d", line);
+           fprintf (out, "%d", output_line);
+         else if (!strcmp (muscle_key, "skeleton-line"))
+           fprintf (out, "%d", skeleton_line);
          else if (muscle_value)
            {
              fputs (muscle_value, out);
-             line += get_lines_number (muscle_value);
-           }     
+             output_line += get_lines_number (muscle_value);
+           }
          else
            {
              fputs ("%%", out);
@@ -1005,7 +1041,6 @@ prepare (void)
   MUSCLE_INSERT_INT ("debug", debug_flag);
   MUSCLE_INSERT_INT ("final", final_state);
   MUSCLE_INSERT_INT ("maxtok", max_user_token_number);
-  MUSCLE_INSERT_INT ("ntbase", ntokens);
   MUSCLE_INSERT_INT ("error-verbose", error_verbose);
   MUSCLE_INSERT_STRING ("prefix", spec_name_prefix);
 
@@ -1050,7 +1085,7 @@ yystype;\n\
       for (i = ntokens; i < nsyms; i++)
        /* don't make these for dummy nonterminals made by gensym.  */
        if (*tags[i] != '@')
-         fprintf (out, "# define\tNT%s\t%d\n", tags[i], i);
+         fprintf (out, "# define NT%s\t%d\n", tags[i], i);
     }
 
   fprintf (out, "\n#endif /* not %s */\n", macro_name);
@@ -1090,7 +1125,8 @@ output (void)
     header_output ();
 
   free (rule_table + 1);
-  obstack_free (&muscle_obstack, 0);
-  obstack_free (&format_obstack, 0);
-  obstack_free (&action_obstack, 0);
+  obstack_free (&muscle_obstack, NULL);
+  obstack_free (&format_obstack, NULL);
+  obstack_free (&action_obstack, NULL);
+  obstack_free (&attrs_obstack, NULL);
 }