bison-patches
[Top][All Lists]
Advanced

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

C++: ostreams


From: Akim Demaille
Subject: C++: ostreams
Date: 28 May 2002 14:09:10 +0200
User-agent: Gnus/5.0808 (Gnus v5.8.8) XEmacs/21.4 (Honest Recruiter)

I'm insterested in Robert's comments here.  Robert, btw, the state
stack is reported upside down.  Since I don't remember if you plan to
reverse it or not, I have not change the iterator.  And anyway,
factoring the display of the parse stack might be welcome.

Also, we need to settle down the interface of print_ (), which is
quite wrong, and doesn't allow the display of nonterminals (which some
people would like to do).

Index: ChangeLog
from  Akim Demaille  <address@hidden>

        * data/bison.c++: Use C++ ostreams.
        (cdebug_): New member.

Index: data/bison.c++
===================================================================
RCS file: /cvsroot/bison/bison/data/bison.c++,v
retrieving revision 1.32
diff -u -u -r1.32 bison.c++
--- data/bison.c++ 28 May 2002 09:53:35 -0000 1.32
+++ data/bison.c++ 28 May 2002 12:06:24 -0000
@@ -137,6 +137,7 @@
 #include "location.hh"
 
 #include <string>
+#include <iostream>
 
 /* Using locations.  */
 #define YYLSP_NEEDED b4_locations_flag
@@ -228,10 +229,12 @@
     b4_name (bool debug,
            LocationType initlocation[]b4_param) :
       b4_constructor[]debug_ (debug),
+      cdebug_ (std::cerr),
       initlocation_ (initlocation)
 #else
     b4_name (bool debug[]b4_param) :
-      b4_constructor[]debug_ (debug)
+      b4_constructor[]debug_ (debug),
+      cdebug_ (std::cerr)
 #endif
     {
     }
@@ -297,9 +300,12 @@
     /* State.  */
     int n_;
     int len_;
-    int debug_;
     int state_;
 
+    /* Debugging.  */
+    int debug_;
+    std::ostream &cdebug_;
+
     /* Lookahead and lookahead in internal form.  */
     int looka_;
     int ilooka_;
@@ -329,15 +335,9 @@
 
 /* Enable debugging if requested.  */
 #if YYDEBUG
-# include <cstdio>
-# define YYFPRINTF std::fprintf
-# define YYDPRINTF(Args)                       \
-do {                                           \
-  if (debug_)                                  \
-    YYFPRINTF Args;                            \
-} while (0)
+# define YYCDEBUG    if (debug_) cdebug_
 #else /* !YYDEBUG */
-# define YYDPRINTF(Args)
+# define YYCDEBUG    if (0) cdebug_
 #endif /* !YYDEBUG */
 
 int
@@ -357,12 +357,12 @@
 #if YYLSP_NEEDED
   location = initlocation_;
 #endif
-  YYDPRINTF ((stderr, "Starting parse\n"));
+  YYCDEBUG << "Starting parse" << std::endl;
 
   /* New state.  */
  yynewstate:
   state_stack_.push (state_);
-  YYDPRINTF ((stderr, "Entering state %d\n", state_));
+  YYCDEBUG << "Entering state " << state_ << std::endl;
   goto yybackup;
 
   /* Backup.  */
@@ -376,7 +376,7 @@
   /* Read a lookahead token.  */
   if (looka_ == empty_)
     {
-      YYDPRINTF ((stderr, "Reading a token: "));
+      YYCDEBUG << "Reading a token: ";
       lex_ ();
     }
 
@@ -385,7 +385,7 @@
     {
       looka_ = eof_;
       ilooka_ = 0;
-      YYDPRINTF ((stderr, "Now at end of input.\n"));
+      YYCDEBUG << "Now at end of input." << std::endl;
     }
   else
     {
@@ -393,9 +393,10 @@
 #if YYDEBUG
       if (debug_)
        {
-         YYFPRINTF (stderr, "Next token is %d (%s", looka_, name_[[ilooka_]]);
+         YYCDEBUG << "Next token is " << looka_
+                << " (" << name_[[ilooka_]];
          print_ ();
-         YYFPRINTF (stderr, ")\n");
+         YYCDEBUG << ')' << std::endl;
        }
 #endif
     }
@@ -424,7 +425,8 @@
     goto yyacceptlab;
 
   /* Shift the lookahead token.  */
-  YYDPRINTF ((stderr, "Shifting token %d (%s), ", looka_, name_[[ilooka_]]));
+  YYCDEBUG << "Shifting token " << looka_
+        << " (" << name_[[ilooka_]] << "), ";
 
   /* Discard the token being shifted unless it is eof.  */
   if (looka_ != eof_)
@@ -465,11 +467,12 @@
 #if YYDEBUG
   if (debug_)
     {
-      YYFPRINTF (stderr, "Reducing via rule %d (line %d), ", n_ - 1, 
rline_[[n_]]);
+      YYCDEBUG << "Reducing via rule " << n_ - 1
+            << " (line " << rline_[[n_]] << "), ";
       for (b4_uint_type(b4_prhs_max) i = prhs_[[n_]];
           rhs_[[i]] >= 0; ++i)
-       YYFPRINTF (stderr, "%s ", name_[[rhs_[i]]]);
-      YYFPRINTF (stderr, "-> %s\n", name_[[r1_[n_]]]);
+       YYCDEBUG << name_[[rhs_[i]]] << ' ';
+      YYCDEBUG << "-> " << name_[[r1_[n_]]] << std::endl;
     }
 #endif
 
@@ -494,11 +497,11 @@
 #if YYDEBUG
   if (debug_)
     {
-      YYFPRINTF (stderr, "state stack now");
+      YYCDEBUG << "state stack now";
       for (StateStack::ConstIterator i = state_stack_.begin ();
           i != state_stack_.end (); ++i)
-       YYFPRINTF (stderr, " %d", *i);
-      YYFPRINTF (stderr, "\n");
+       YYCDEBUG << ' ' << *i;
+      YYCDEBUG << std::endl;
     }
 #endif
 
@@ -561,7 +564,8 @@
       /* Return failure if at end of input.  */
       if (looka_ == eof_)
        goto yyabortlab;
-      YYDPRINTF ((stderr, "Discarding token %d (%s).\n", looka_, 
name_[[ilooka_]]));
+      YYCDEBUG << "Discarding token " << looka_
+            << " (" << name_[[ilooka_]] << ")." << std::endl;
       looka_ = empty_;
     }
 
@@ -593,19 +597,19 @@
        {
          if (stos_[[state_]] < ntokens_)
            {
-             YYFPRINTF (stderr, "Error: popping token %d (%s",
-                        token_number_[[stos_[state_]]],
-                        name_[[stos_[state_]]]);
+             YYCDEBUG << "Error: popping token "
+                    << token_number_[[stos_[state_]]]
+                    << " (" << name_[[stos_[state_]]];
 # ifdef YYPRINT
              YYPRINT (stderr, token_number_[[stos_[state_]]],
                       semantic_stack_.top ());
 # endif
-             YYFPRINTF (stderr, ")\n");
+             YYCDEBUG << ')' << std::endl;
            }
          else
            {
-             YYFPRINTF (stderr, "Error: popping nonterminal (%s)\n",
-                        name_[[stos_[state_]]]);
+             YYCDEBUG << "Error: popping nonterminal ("
+                    << name_[[stos_[state_]]] << ')' << std::endl;
            }
        }
 #endif
@@ -617,11 +621,11 @@
 #if YYDEBUG
       if (debug_)
        {
-         YYFPRINTF (stderr, "Error: state stack now");
+         YYCDEBUG << "Error: state stack now";
          for (StateStack::ConstIterator i = state_stack_.begin ();
               i != state_stack_.end (); ++i)
-           YYFPRINTF (stderr, " %d", *i);
-         YYFPRINTF (stderr, "\n");
+           YYCDEBUG << ' ' << *i;
+         YYCDEBUG << std::endl;
        }
 #endif
     }
@@ -629,7 +633,7 @@
   if (n_ == final_)
     goto yyacceptlab;
 
-  YYDPRINTF ((stderr, "Shifting error token, "));
+  YYCDEBUG << "Shifting error token, ";
 
   semantic_stack_.push (value);
   location_stack_.push (location);



reply via email to

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