bison-patches
[Top][All Lists]
Advanced

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

Re: Lookahead correction for the C++ skeleton lalr1.cc


From: Akim Demaille
Subject: Re: Lookahead correction for the C++ skeleton lalr1.cc
Date: Sun, 14 Jul 2019 13:55:27 +0200

There's an issue I had not paid attention to: you made a lac_stack_
a fully blown stack of symbols (i.e., type, value and location).  I
believe you only need a stack a states, the rest is useless.  And
GCC 4.6 chokes on it :)

But stack.hh was really designed for symbols, not simple values such
as state numbers.  Which is going to be painful to have work properly
in C++98.  I tried to have it work properly, but C++98 is really
painful (I had forgotten default template parameters were not allowed
for function templates, only class template).

You may play with the appended protopatch if you feel like it, but
otherwise I would just go for a plain std::vector for lac_stack_.
If you do so, please also remove stack::empty and other now-useless
membres.


diff --git a/data/skeletons/lalr1.cc b/data/skeletons/lalr1.cc
index 60c2ba71..5f269666 100644
--- a/data/skeletons/lalr1.cc
+++ b/data/skeletons/lalr1.cc
@@ -369,7 +369,7 @@ m4_define([b4_shared_declarations],
     /// yy_lac_check_. We just store it as a member of this class to hold
     /// on to the memory and to avoid frequent reallocations.
     /// Since yy_lac_check_ is const, this member must be mutable.
-    mutable stack<by_state> yylac_stack_;
+    mutable stack<state_type> yylac_stack_;
     /// Was an initial LAC context established?
     bool yy_lac_established_;
 ]])[
@@ -1142,9 +1142,8 @@ b4_dollar_popdef])[]dnl
     size_t lac_top = 0;
     while (true)
       {
-        state_type top_state = (yylac_stack_.empty ()
-                                ? yystack_[lac_top].state
-                                : yylac_stack_[0].state);
+        state_type top_state =
+          yylac_stack_.empty () ? yystack_[lac_top].state : yylac_stack_[0];
         int yyrule = yypact_[top_state];
         if (yy_pact_value_is_default_ (yyrule)
             || (yyrule += yytoken) < 0 || yylast_ < yyrule
@@ -1196,13 +1195,12 @@ b4_dollar_popdef])[]dnl
           lac_top += yylen;
         }
         // Keep top_state in sync with the updated stack.
-        top_state = (yylac_stack_.empty ()
-                     ? yystack_[lac_top].state
-                     : yylac_stack_[0].state);
+        top_state =
+          (yylac_stack_.empty () ? yystack_[lac_top].state : yylac_stack_[0]);
         // Push the resulting state of the reduction.
         state_type state = yy_lr_goto_state_ (top_state, yyr1_[yyrule]);
         YYCDEBUG << " G" << state;
-        yylac_stack_.push (by_state (state));
+        yylac_stack_.push (state_type (state));
       }
   }
 
diff --git a/data/skeletons/stack.hh b/data/skeletons/stack.hh
index a481fb26..5355559f 100644
--- a/data/skeletons/stack.hh
+++ b/data/skeletons/stack.hh
@@ -26,7 +26,19 @@ b4_defines_if([b4_required_version_if([302], [],
 # b4_stack_define
 # ---------------
 m4_define([b4_stack_define],
-[[    /// A stack with random access from its top.
+[[    template <class T, class U>
+      struct is_same { enum {value = false}; };
+
+    template <class T>
+      struct is_same<T, T> { enum{value = true}; };
+
+    template <bool B, typename T = void>
+    struct enable_if {};
+
+    template <typename T>
+    struct enable_if <true, T> { typedef T type; };
+
+    /// A stack with random access from its top.
     template <typename T, typename S = std::vector<T> >
     class stack
     {
@@ -76,10 +88,18 @@ m4_define([b4_stack_define],
         return operator[] (size_type (i));
       }
 
+      template <typename U = T>
+      typename enable_if<is_same<U, int>::value>::type
+      push (int t)
+      {
+        seq_.push_back (t);
+      }
+
       /// Steal the contents of \a t.
       ///
       /// Close to move-semantics.
-      void
+      template <typename U = T>
+      typename enable_if<!is_same<U, int>::value>::type
       push (YY_MOVE_REF (T) t)
       {
         seq_.push_back (T ());




reply via email to

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