]> git.saurik.com Git - bison.git/commitdiff
c++: clean up the handling of empty symbols
authorAkim Demaille <akim@lrde.epita.fr>
Thu, 8 Jan 2015 09:04:53 +0000 (10:04 +0100)
committerAkim Demaille <akim@lrde.epita.fr>
Thu, 8 Jan 2015 13:45:42 +0000 (14:45 +0100)
* data/c++.m4, data/lalr1.cc (yyempty_): Remove, replaced by...
(empty_symbol, by_state::empty_state): these.
(basic_symbol::empty): New.

data/c++.m4
data/lalr1.cc

index 481ea8653c85e563db0eccf9f456619116518600..f9ea7e9d69678b41b4ae3b1e44851dc142d88002 100644 (file)
@@ -174,9 +174,12 @@ m4_define([b4_public_types_declare],
     /// (External) token type, as returned by yylex.
     typedef token::yytokentype token_type;
 
-    /// Internal symbol number.
+    /// Symbol type: an internal symbol number.
     typedef int symbol_number_type;
 
+    /// The symbol type number to denote an empty symbol.
+    enum { empty_symbol = -2 };
+
     /// Internal symbol number for tokens (subsumed by symbol_number_type).
     typedef ]b4_int_type_for([b4_translate])[ token_number_type;
 
@@ -212,6 +215,9 @@ m4_define([b4_public_types_declare],
       /// Destroy the symbol.
       ~basic_symbol ();
 
+      /// Whether empty.
+      bool empty () const;
+
       /// Destructive move, \a s is emptied into this.
       void move (basic_symbol& s);
 
@@ -251,12 +257,10 @@ m4_define([b4_public_types_declare],
       /// The token.
       token_type token () const;
 
-      /// The type number used to denote an empty symbol.
-      enum { empty = 0 };
-
       /// The symbol type.
-      /// \a empty when empty.
-      token_number_type type;
+      /// \a empty_symbol when empty.
+      /// An int, not token_number_type, to be able to store empty_symbol.
+      int type;
     };
 
     /// "External" symbols: returned by the scanner.
@@ -339,6 +343,14 @@ m4_define([b4_public_types_define],
   ]b4_symbol_variant([[yytype]], [[value]], [[template destroy]])])[
   }
 
+  template <typename Base>
+  inline
+  bool
+  ]b4_parser_class_name[::basic_symbol<Base>::empty () const
+  {
+    return Base::type_get () == empty_symbol;
+  }
+
   template <typename Base>
   inline
   void
@@ -354,7 +366,7 @@ m4_define([b4_public_types_define],
   // by_type.
   inline
   ]b4_parser_class_name[::by_type::by_type ()
-     : type (empty)
+    : type (empty_symbol)
   {}
 
   inline
@@ -372,7 +384,7 @@ m4_define([b4_public_types_define],
   ]b4_parser_class_name[::by_type::move (by_type& that)
   {
     type = that.type;
-    that.type = empty;
+    that.type = empty_symbol;
   }
 
   inline
index 2c38cdf3adf6340132a90bed2e8721fe5bf5fb50..1c3481ff43e6681b3de2b42a23363fd2f39a744a 100644 (file)
@@ -214,7 +214,7 @@ b4_location_define])])[
 
     /// Generate an error message.
     /// \param yystate   the state where the error occurred.
-    /// \param yytoken   the lookahead token type, or yyempty_.
+    /// \param yytoken   the lookahead token type, or empty_symbol.
     virtual std::string yysyntax_error_ (state_type yystate,
                                          symbol_number_type yytoken) const;
 
@@ -292,11 +292,11 @@ b4_location_define])])[
       void move (by_state& that);
 
       /// The (internal) type number (corresponding to \a state).
-      /// \a empty when empty.
+      /// \a empty_symbol when empty.
       symbol_number_type type_get () const;
 
       /// The state number used to denote an empty symbol.
-      enum { empty = 0 };
+      enum { empty_state = -1 };
 
       /// The state.
       /// \a empty when empty.
@@ -346,7 +346,6 @@ b4_location_define])])[
       yyeof_ = 0,
       yylast_ = ]b4_last[,     ///< Last index in yytable_.
       yynnts_ = ]b4_nterms_number[,  ///< Number of nonterminal symbols.
-      yyempty_ = -2,
       yyfinal_ = ]b4_final_state_number[, ///< Termination state number.
       yyterror_ = 1,
       yyerrcode_ = 256,
@@ -535,7 +534,7 @@ m4_if(b4_prefix, [yy], [],
   // by_state.
   inline
   ]b4_parser_class_name[::by_state::by_state ()
-    : state (empty)
+    : state (empty_state)
   {}
 
   inline
@@ -548,7 +547,7 @@ m4_if(b4_prefix, [yy], [],
   ]b4_parser_class_name[::by_state::move (by_state& that)
   {
     state = that.state;
-    that.state = empty;
+    that.state = empty_state;
   }
 
   inline
@@ -560,7 +559,7 @@ m4_if(b4_prefix, [yy], [],
   ]b4_parser_class_name[::symbol_number_type
   ]b4_parser_class_name[::by_state::type_get () const
   {
-    return state == empty ? 0 : yystos_[state];
+    return state == empty_state ? empty_symbol : yystos_[state];
   }
 
   inline
@@ -576,7 +575,7 @@ m4_if(b4_prefix, [yy], [],
                                       [value], [move], [that.value])],
                    [[value = that.value;]])[
     // that is emptied.
-    that.type = empty;
+    that.type = empty_symbol;
   }
 
   inline
@@ -876,7 +875,7 @@ b4_dollar_popdef])[]dnl
         ++yynerrs_;
         error (]b4_join(b4_locations_if([yyla.location]),
                         [[yysyntax_error_ (yystack_[0].state,
-                                           yyempty ? yyempty_ : yyla.type_get ())]])[);
+                                           yyempty ? empty_symbol : yyla.type_get ())]])[);
       }
 
 ]b4_locations_if([[
@@ -1046,7 +1045,7 @@ b4_error_verbose_if([state_type yystate, symbol_number_type yytoken],
          token that will not be accepted due to an error action in a
          later state.
     */
-    if (yytoken != yyempty_)
+    if (yytoken != empty_symbol)
       {
         yyarg[yycount++] = yytname_[yytoken];
         int yyn = yypact_[yystate];