bison-patches
[Top][All Lists]
Advanced

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

[PATCH 2/6] c++: clean up the handling of empty symbols


From: Akim Demaille
Subject: [PATCH 2/6] c++: clean up the handling of empty symbols
Date: Thu, 8 Jan 2015 14:52: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   | 28 ++++++++++++++++++++--------
 data/lalr1.cc | 19 +++++++++----------
 2 files changed, 29 insertions(+), 18 deletions(-)

diff --git a/data/c++.m4 b/data/c++.m4
index 481ea86..f9ea7e9 100644
--- a/data/c++.m4
+++ b/data/c++.m4
@@ -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.
@@ -341,6 +345,14 @@ m4_define([b4_public_types_define],
 
   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
   ]b4_parser_class_name[::basic_symbol<Base>::move (basic_symbol& s)
   {
@@ -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
diff --git a/data/lalr1.cc b/data/lalr1.cc
index 2c38cdf..1c3481f 100644
--- a/data/lalr1.cc
+++ b/data/lalr1.cc
@@ -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];
-- 
2.2.1




reply via email to

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