]> git.saurik.com Git - bison.git/blobdiff - src/reader.c
Clean up yesterday's patch.
[bison.git] / src / reader.c
index 0fa162d9ce786c650fad8db949ea0bba2ea3298c..73cbd5d9961bc4e2d9c1a5715d66030a357e55aa 100644 (file)
@@ -22,6 +22,7 @@
 
 #include <config.h>
 #include "system.h"
+#include <assert.h>
 
 #include <quotearg.h>
 
 #include "getargs.h"
 #include "gram.h"
 #include "muscle_tab.h"
-#include "output.h"
 #include "reader.h"
 #include "symlist.h"
 #include "symtab.h"
+#include "scan-gram.h"
+#include "scan-code.h"
 
 static void check_and_convert_grammar (void);
 
@@ -66,18 +68,20 @@ grammar_start_symbol_set (symbol *sym, location loc)
 }
 
 
-/*----------------------------------------------------------------.
-| There are two prologues: one before %union, one after.  Augment |
-| the current one.                                                |
-`----------------------------------------------------------------*/
+/*---------------------------------------------------------------------.
+| There are two prologues: one before the first %union and one after.  |
+|  Augment the one specified by POST.                                  |
+`---------------------------------------------------------------------*/
 
 void
-prologue_augment (const char *prologue, location loc)
+prologue_augment (const char *prologue, location loc, bool post)
 {
   struct obstack *oout =
-    !typed ? &pre_prologue_obstack : &post_prologue_obstack;
+    !post ? &pre_prologue_obstack : &post_prologue_obstack;
 
   obstack_fgrow1 (oout, "]b4_syncline(%d, [[", loc.start.line);
+  /* FIXME: Protection of M4 characters missing here.  See
+     output.c:escaped_output.  */
   MUSCLE_OBSTACK_SGROW (oout,
                        quotearg_style (c_quoting_style, loc.start.file));
   obstack_sgrow (oout, "]])[\n");
@@ -212,17 +216,16 @@ grammar_current_rule_begin (symbol *lhs, location loc)
 }
 
 
-/*-----------------------------------------------------------------.
-| A symbol is typed if it has a declared %type, or if it is a      |
-| mid-rule symbol (i.e., the generated LHS replacing a mid-rule    |
-| action) that was assigned to, as in `exp: { $$ = 1; } { $$ = $1; |
-| }'.                                                              |
-`-----------------------------------------------------------------*/
+/*----------------------------------------------------------------------.
+| A symbol should be used if it has a destructor, or if it is a         |
+| mid-rule symbol (i.e., the generated LHS replacing a mid-rule         |
+| action) that was assigned to, as in "exp: { $$ = 1; } { $$ = $1; }".  |
+`----------------------------------------------------------------------*/
 
 static bool
-symbol_typed_p (const symbol_list *s)
+symbol_should_be_used (symbol_list const *s)
 {
-  return (s->sym->type_name
+  return (s->sym->destructor
          || (s->midrule && s->midrule->used));
 }
 
@@ -261,13 +264,13 @@ grammar_rule_check (const symbol_list *r)
                 _("empty rule for typed nonterminal, and no action"));
     }
 
-  /* Check that typed symbol values are used.  */
+  /* Check that symbol values that should be used are in fact used.  */
   {
     symbol_list const *l = r;
     int n = 0;
     for (; l && l->sym; l = l->next, ++n)
       if (! (l->used
-            || !symbol_typed_p (l)
+            || !symbol_should_be_used (l)
             /* The default action, $$ = $1, `uses' both.  */
             || (!r->action && (n == 0 || n == 1))))
        {
@@ -301,7 +304,7 @@ grammar_current_rule_end (location loc)
 | rule.                                                              |
 `-------------------------------------------------------------------*/
 
-static void
+void
 grammar_midrule_action (void)
 {
   /* Since the action was written out with this rule's number, we must
@@ -395,15 +398,14 @@ grammar_current_rule_symbol_append (symbol *sym, location loc)
   grammar_symbol_append (sym, loc);
 }
 
-/* Attach an ACTION to the current rule.  If needed, move the previous
-   action as a mid-rule action.  */
+/* Attach an ACTION to the current rule.  */
 
 void
 grammar_current_rule_action_append (const char *action, location loc)
 {
   if (current_rule->action)
     grammar_midrule_action ();
-  current_rule->action = action;
+  current_rule->action = translate_rule_action (current_rule, action, loc);
   current_rule->action_location = loc;
 }
 
@@ -429,6 +431,7 @@ packgram (void)
 
   while (p)
     {
+      int rule_length = 0;
       symbol *ruleprec = p->ruleprec;
       rules[ruleno].user_number = ruleno;
       rules[ruleno].number = ruleno;
@@ -443,18 +446,22 @@ packgram (void)
       rules[ruleno].action = p->action;
       rules[ruleno].action_location = p->action_location;
 
-      p = p->next;
-      while (p && p->sym)
+      for (p = p->next; p && p->sym; p = p->next)
        {
+         ++rule_length;
+
+         /* Don't allow rule_length == INT_MAX, since that might
+            cause confusion with strtol if INT_MAX == LONG_MAX.  */
+         if (rule_length == INT_MAX)
+             fatal_at (rules[ruleno].location, _("rule is too long"));
+
          /* item_number = symbol_number.
             But the former needs to contain more: negative rule numbers. */
          ritem[itemno++] = symbol_number_as_item_number (p->sym->number);
          /* A rule gets by default the precedence and associativity
-            of the last token in it.  */
+            of its last token.  */
          if (p->sym->class == token_sym && default_prec)
            rules[ruleno].prec = p->sym;
-         if (p)
-           p = p->next;
        }
 
       /* If this rule has a %prec,
@@ -464,8 +471,11 @@ packgram (void)
          rules[ruleno].precsym = ruleprec;
          rules[ruleno].prec = ruleprec;
        }
+      /* An item ends by the rule number (negated).  */
       ritem[itemno++] = rule_number_as_item_number (ruleno);
+      assert (itemno < ITEM_NUMBER_MAX);
       ++ruleno;
+      assert (ruleno < RULE_NUMBER_MAX);
 
       if (p)
        p = p->next;
@@ -514,7 +524,7 @@ reader (void)
 
   gram__flex_debug = trace_flag & trace_scan;
   gram_debug = trace_flag & trace_parse;
-  scanner_initialize ();
+  gram_scanner_initialize ();
   gram_parse ();
 
   if (! complaint_issued)