]> git.saurik.com Git - bison.git/blobdiff - data/lalr1.cc
2004-11-23 Akim Demaille <akim@epita.fr>
[bison.git] / data / lalr1.cc
index 00090e7a02898b383e0b8022608814709e9c72f6..817b3833b333d35868508cb33b2624ad8ce4c640 100644 (file)
@@ -164,16 +164,16 @@ b4_syncline([@oline@], [@ofile@])[
 # define YYERROR_VERBOSE ]b4_error_verbose[
 #endif
 
-#ifdef YYSTYPE
-typedef YYSTYPE yystype;
-#else
+#if ! defined (YYSTYPE) && ! defined (YYSTYPE_IS_DECLARED)
 ]m4_ifdef([b4_stype],
 [b4_syncline([b4_stype_line], [b4_filename])
-typedef union b4_stype yystype;
+union YYSTYPE b4_stype;
 /* Line __line__ of lalr1.cc.  */
 b4_syncline([@oline@], [@ofile@])],
-[typedef int yystype;])[
-# define YYSTYPE yystype
+[typedef int YYSTYPE;])[
+# define yystype YYSTYPE /* obsolescent; will be withdrawn */
+# define YYSTYPE_IS_DECLARED 1
+# define YYSTYPE_IS_TRIVIAL 1
 #endif
 
 /* Copy the second part of user declarations.  */
@@ -181,13 +181,22 @@ b4_syncline([@oline@], [@ofile@])],
 
 ]/* Line __line__ of lalr1.cc.  */
 b4_syncline([@oline@], [@ofile@])[
-/* YYLLOC_DEFAULT -- Set CURRENT to span from RHS[1] to RHS[N].  */
+/* YYLLOC_DEFAULT -- Set CURRENT to span from RHS[1] to RHS[N].
+   If N is 0, then set CURRENT to the empty location which ends
+   the previous symbol: RHS[0] (always defined).  */
 
 #ifndef YYLLOC_DEFAULT
-# define YYLLOC_DEFAULT(Current, Rhs, N)       \
-do {                                           \
-   ((Current).begin = (Rhs)[1].begin);         \
-   ((Current).end   = (Rhs)[N].end);           \
+# define YYLLOC_DEFAULT(Current, Rhs, N)               \
+do {                                                   \
+  if (N)                                               \
+    {                                                  \
+      (Current).begin = (Rhs)[1].begin;                        \
+      (Current).end   = (Rhs)[N].end;                  \
+    }                                                  \
+  else                                                 \
+    {                                                  \
+      (Current).begin = (Current).end = (Rhs)[0].end;  \
+    }                                                  \
 } while (0)
 #endif
 
@@ -195,18 +204,18 @@ namespace yy
 {
   class ]b4_parser_class_name[;
 
-  template < typename P >
+  template <typename P>
   struct Traits
   {
   };
 
-  template < >
-  struct Traits< ]b4_parser_class_name[ >
+  template <>
+  struct Traits<]b4_parser_class_name[>
   {
     typedef ]b4_int_type_for([b4_translate])[ TokenNumberType;
     typedef ]b4_int_type_for([b4_rhs])[       RhsNumberType;
-    typedef int      StateType;
-    typedef yystype  SemanticType;
+    typedef int StateType;
+    typedef YYSTYPE SemanticType;
     typedef ]b4_location_type[ LocationType;
   };
 }
@@ -217,38 +226,53 @@ namespace yy
   {
   public:
 
-    typedef Traits< ]b4_parser_class_name[ >::TokenNumberType TokenNumberType;
-    typedef Traits< ]b4_parser_class_name[ >::RhsNumberType   RhsNumberType;
-    typedef Traits< ]b4_parser_class_name[ >::StateType       StateType;
-    typedef Traits< ]b4_parser_class_name[ >::SemanticType    SemanticType;
-    typedef Traits< ]b4_parser_class_name[ >::LocationType    LocationType;
+    typedef Traits<]b4_parser_class_name[>::TokenNumberType TokenNumberType;
+    typedef Traits<]b4_parser_class_name[>::RhsNumberType   RhsNumberType;
+    typedef Traits<]b4_parser_class_name[>::StateType       StateType;
+    typedef Traits<]b4_parser_class_name[>::SemanticType    SemanticType;
+    typedef Traits<]b4_parser_class_name[>::LocationType    LocationType;
 
-    typedef Stack< StateType >    StateStack;
-    typedef Stack< SemanticType > SemanticStack;
-    typedef Stack< LocationType > LocationStack;
+    typedef Stack<StateType>    StateStack;
+    typedef Stack<SemanticType> SemanticStack;
+    typedef Stack<LocationType> LocationStack;
 
     ]b4_parser_class_name[ (bool debug][]b4_param[]b4_parse_param_decl[) :
       ]b4_constructor[][debug_ (debug),
-      cdebug_ (std::cerr)]b4_parse_param_cons[
+      yycdebug_ (&std::cerr)]b4_parse_param_cons[
     {
     }
 
+    ]b4_parser_class_name[ (bool debug,
+           LocationType][]b4_param[]b4_parse_param_decl[) :
+      ]b4_constructor[][debug_ (debug),
+      yycdebug_ (&std::cerr)]b4_parse_param_cons[
+    {
+      *yycdebug_ << __FILE__ << ':' << __LINE__
+                << ": this constructor is provided by backward compatibility"
+                << ", but will be removed in the near future."
+                << std::endl;
+    }
+
     virtual ~]b4_parser_class_name[ ()
     {
     }
 
     virtual int parse ();
 
+    /// Return the current debugging stream.
+    std::ostream& debug_stream () const;
+    /// Set the current debugging stream.
+    void set_debug_stream (std::ostream &);
+
   private:
 
     virtual void lex_ ();
     virtual void error_ ();
-    virtual void print_ ();
     virtual void report_syntax_error_ ();
 #if YYDEBUG
     virtual void symprint_ (int yytype,
-                           const SemanticType *yyvaluep,
-                           const LocationType *yylocationp);
+                           const SemanticTypeyyvaluep,
+                           const LocationTypeyylocationp);
 #endif /* ! YYDEBUG */
 
 
@@ -286,9 +310,9 @@ namespace yy
 
     /* Even more tables.  */
     inline TokenNumberType translate_ (int token);
-    inline void destruct_ (const char *yymsg,
+    inline void destruct_ (const charyymsg,
                            int yytype,
-                           SemanticType *yyvaluep, LocationType *yylocationp);
+                           SemanticType* yyvaluep, LocationType* yylocationp);
 
     /// Pop \a n symbols the three stacks.
     inline void pop (unsigned int n = 1);
@@ -317,7 +341,7 @@ namespace yy
 
     /* Debugging.  */
     int debug_;
-    std::ostream &cdebug_;
+    std::ostream* yycdebug_;
 
     /* Look-ahead and look-ahead in internal form.  */
     int looka_;
@@ -349,20 +373,21 @@ b4_copyright([C++ Skeleton parser for LALR(1) parsing with Bison],
 
 m4_if(b4_defines_flag, 0, [], [#include @output_header_name@])[
 
+/* A pseudo ostream that takes debug_ into account. */
+# define YYCDEBUG                                                      \
+  for (bool yydebugcond_ = debug_; yydebugcond_; yydebugcond_ = false) \
+    (*yycdebug_)
+
 /* Enable debugging if requested.  */
 #if YYDEBUG
 
-# define YYCDEBUG                              \
-  if (debug_)                                  \
-    cdebug_
-
 # define YY_SYMBOL_PRINT(Title, Type, Value, Location) \
 do {                                                   \
   if (debug_)                                          \
     {                                                  \
-      cdebug_ << (Title) << ' ';                       \
+      *yycdebug_ << (Title) << ' ';                    \
       symprint_ ((Type), (Value), (Location));         \
-      cdebug_ << std::endl;                            \
+      *yycdebug_ << std::endl;                         \
     }                                                  \
 } while (0)
 
@@ -380,7 +405,6 @@ do {                                        \
 
 #else /* !YYDEBUG */
 
-# define YYCDEBUG    if (0) cdebug_
 # define YY_SYMBOL_PRINT(Title, Type, Value, Location)
 # define YY_REDUCE_PRINT(Rule)
 # define YY_STACK_PRINT()
@@ -398,28 +422,31 @@ do {                                      \
 
 void
 yy::]b4_parser_class_name[::symprint_ (int yytype,
-                         const SemanticType *yyvaluep, const LocationType *yylocationp)
+                         const SemanticType* yyvaluep, const LocationType* yylocationp)
 {
   /* Pacify ``unused variable'' warnings.  */
   (void) yyvaluep;
   (void) yylocationp;
+  /* Backward compatibility, but should be removed eventually. */
+  std::ostream& cdebug_ = *yycdebug_;
+  (void) cdebug_;
 
-  cdebug_ << (yytype < ntokens_ ? "token" : "nterm")
-         << ' ' << name_[yytype] << " ("
-          << *yylocationp << ": ";
+  *yycdebug_ << (yytype < ntokens_ ? "token" : "nterm")
+            << ' ' << name_[yytype] << " ("
+             << *yylocationp << ": ";
   switch (yytype)
     {
 ]m4_map([b4_symbol_actions], m4_defn([b4_symbol_printers]))dnl
 [      default:
         break;
     }
-  cdebug_ << ')';
+  *yycdebug_ << ')';
 }
 #endif /* ! YYDEBUG */
 
 void
-yy::]b4_parser_class_name[::destruct_ (const char *yymsg,
-                         int yytype, SemanticType *yyvaluep, LocationType *yylocationp)
+yy::]b4_parser_class_name[::destruct_ (const charyymsg,
+                         int yytype, SemanticType* yyvaluep, LocationType* yylocationp)
 {
   /* Pacify ``unused variable'' warnings.  */
   (void) yyvaluep;
@@ -443,6 +470,19 @@ yy::]b4_parser_class_name[::pop (unsigned int n)
   location_stack_.pop (n);
 }
 
+std::ostream&
+yy::]b4_parser_class_name[::debug_stream () const
+{
+  return *yycdebug_;
+}
+
+void
+yy::]b4_parser_class_name[::set_debug_stream (std::ostream& o)
+{
+  yycdebug_ = &o;
+}
+
+
 int
 yy::]b4_parser_class_name[::parse ()
 {
@@ -572,21 +612,14 @@ yyreduce:
      This behavior is undocumented and Bison
      users should not rely upon it.  */
   if (len_)
-    {
-      yyval = semantic_stack_[len_ - 1];
-      yyloc = location_stack_[len_ - 1];
-    }
+    yyval = semantic_stack_[len_ - 1];
   else
-    {
-      yyval = semantic_stack_[0];
-      yyloc = location_stack_[0];
-    }
+    yyval = semantic_stack_[0];
 
-  if (len_)
-    {
-      Slice< LocationType, LocationStack > slice (location_stack_, len_);
-      YYLLOC_DEFAULT (yyloc, slice, len_);
-    }
+  {
+    Slice<LocationType, LocationStack> slice (location_stack_, len_);
+    YYLLOC_DEFAULT (yyloc, slice, len_);
+  }
   YY_REDUCE_PRINT (n_);
   switch (n_)
     {
@@ -902,11 +935,11 @@ yy::]b4_parser_class_name[::rline_[] =
 void
 yy::]b4_parser_class_name[::stack_print_ ()
 {
-  cdebug_ << "Stack now";
+  *yycdebug_ << "Stack now";
   for (StateStack::const_iterator i = state_stack_.begin ();
        i != state_stack_.end (); ++i)
-    cdebug_ << ' ' << *i;
-  cdebug_ << std::endl;
+    *yycdebug_ << ' ' << *i;
+  *yycdebug_ << std::endl;
 }
 
 /** Report that the YYRULE is going to be reduced.  */
@@ -916,11 +949,12 @@ yy::]b4_parser_class_name[::reduce_print_ (int yyrule)
 {
   unsigned int yylno = rline_[yyrule];
   /* Print the symbols being reduced, and their result.  */
-  cdebug_ << "Reducing stack by rule " << n_ - 1 << " (line " << yylno << "), ";
+    *yycdebug_ << "Reducing stack by rule " << n_ - 1
+               << " (line " << yylno << "), ";
   for (]b4_int_type_for([b4_prhs])[ i = prhs_[n_];
        0 <= rhs_[i]; ++i)
-    cdebug_ << name_[rhs_[i]] << ' ';
-  cdebug_ << "-> " << name_[r1_[n_]] << std::endl;
+    *yycdebug_ << name_[rhs_[i]] << ' ';
+  *yycdebug_ << "-> " << name_[r1_[n_]] << std::endl;
 }
 #endif // YYDEBUG
 
@@ -964,7 +998,7 @@ b4_copyright([Stack handling for Bison C++ parsers], [2002, 2003, 2004])[
 
 namespace yy
 {
-  template < class T, class S = std::deque< T > >
+  template <class T, class S = std::deque<T> >
   class Stack
   {
   public:
@@ -1025,7 +1059,7 @@ namespace yy
     S seq_;
   };
 
-  template < class T, class S = Stack< T > >
+  template <class T, class S = Stack<T> >
   class Slice
   {
   public:
@@ -1245,7 +1279,7 @@ namespace yy
   }
 
   /** \brief Add and assign a Location */
-  inline Location &operator+= (Location& res, unsigned int width)
+  inline Locationoperator+= (Location& res, unsigned int width)
   {
     res.columns (width);
     return res;