]> git.saurik.com Git - bison.git/blobdiff - data/lalr1.cc
Compute at M4 time some of the subtractions.
[bison.git] / data / lalr1.cc
index d2c33a143e5d2f0610ac0854e3a842b2a5f3797a..97ba371bd8795235b5f1a93fc0eeaeb5c0991c56 100644 (file)
 
 m4_include(b4_pkgdatadir/[c++.m4])
 
+# b4_subtract(LHS, RHS)
+# ---------------------
+# Evaluate LHS - RHS if they are integer literals, otherwise expand
+# to (LHS) - (RHS).
+m4_define([b4_subtract],
+[m4_bmatch([$1$2], [^[0123456789]*$],
+           [m4_eval([$1 - $2])],
+           [($1) - ($2)])])
+
 
 # b4_args(ARG1, ...)
 # _b4_args(ARG1, ...)
@@ -107,7 +116,7 @@ m4_define([b4_lhs_location],
 # Return the data corresponding to the symbol #NUM, where the current
 # rule has RULE-LENGTH symbols on RHS.
 m4_define([b4_rhs_data],
-          [yystack_@{($1) - ($2)@}])
+          [yystack_@{b4_subtract($@)@}])
 
 
 # b4_rhs_state(RULE-LENGTH, NUM)
@@ -346,6 +355,21 @@ m4_define([b4_symbol_constructor_definitions],
   {
     return type;
   }
+]b4_lex_symbol_if([[
+  ]b4_parser_class_name[::token_type
+  ]b4_parser_class_name[::symbol_type::token () const
+  {
+    // YYTOKNUM[NUM] -- (External) token number corresponding to the
+    // (internal) symbol number NUM (which must be that of a token).  */
+    static
+    const ]b4_int_type_for([b4_toknum])[
+    yytoken_number_[] =
+    {
+  ]b4_toknum[
+    };
+    return static_cast<token_type> (yytoken_number_[type]);
+  }
+]])[
 
 ]b4_variant_if(
 [  // Implementation of make_symbol for each symbol type.
@@ -411,7 +435,7 @@ m4_map([b4_char_sizeof_], [$@])dnl
 # Define yytranslate_.  Sometimes we want it in the header file,
 # sometimes the cc file suffices.
 m4_define([b4_yytranslate_definition],
-[[  // YYTRANSLATE(YYLEX) -- Bison symbol number corresponding to YYLEX.
+[[  // Symbol number corresponding to token number t.
   ]b4_parser_class_name[::token_number_type
   ]b4_parser_class_name[::yytranslate_ (]b4_lex_symbol_if([token_type],
                                                           [int])[ t)
@@ -727,8 +751,6 @@ m4_ifdef([b4_stype],
 #if YYDEBUG
     /// For each rule, its source line number.
     static const ]b4_int_type_for([b4_rline])[ yyrline_[];
-    /// For each scanner token number, its symbol number.
-    static const ]b4_int_type_for([b4_toknum])[ yytoken_number_[];
     /// Report on the debug stream that the rule \a r is going to be reduced.
     virtual void yy_reduce_print_ (int r);
     /// Print the state stack on the debug stream.
@@ -809,6 +831,9 @@ m4_ifdef([b4_stype],
 
       /// Return the type corresponding to this state.
       inline int type_get_ () const;
+
+      /// Its token.
+      inline token_type token () const;
     };
 
 ]b4_symbol_constructor_declarations[
@@ -877,6 +902,7 @@ m4_ifdef([b4_stype],
 
 ]b4_lex_symbol_if([b4_yytranslate_definition])[
 ]b4_symbol_constructor_specializations[
+]b4_lex_symbol_if([b4_symbol_constructor_definitions])[
 ]b4_namespace_close[
 
 ]b4_percent_define_flag_if([[global_tokens_and_yystype]],
@@ -1026,7 +1052,7 @@ b4_percent_code_get[]dnl
   | Symbol types.  |
   `---------------*/
 
-]b4_symbol_constructor_definitions[
+]b4_lex_symbol_if([], [b4_symbol_constructor_definitions])[
 
   // stack_symbol_type.
   ]b4_parser_class_name[::stack_symbol_type::stack_symbol_type ()
@@ -1562,12 +1588,6 @@ b4_error_verbose_if([int yystate, int yytoken],
      symbol of state STATE-NUM.  */
   ]b4_table_define([stos], [b4_stos])[;
 
-#if YYDEBUG
-  /* TOKEN_NUMBER_[YYLEX-NUM] -- Internal symbol number corresponding
-     to YYLEX-NUM.  */
-  ]b4_table_define([token_number], [b4_toknum])[;
-#endif
-
   /* YYR1[YYN] -- Symbol number of symbol that rule YYN derives.  */
   ]b4_table_define([r1], [b4_r1])[;