bison-patches
[Top][All Lists]
Advanced

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

[PATCH 10/11] glr2.cc: introduce the yytranslate_ member function


From: Akim Demaille
Subject: [PATCH 10/11] glr2.cc: introduce the yytranslate_ member function
Date: Sun, 10 Jan 2021 17:59:09 +0100

* data/skeletons/c++.m4 (b4_yytranslate_define): Use static_cast
rather than the YY_CAST macro.
Avoids the need to define YY_CAST in the header.
* data/skeletons/glr2.cc: Fix calls to b4_shared_declarations: pass
the type of file we are in.
Don't define YYTRANSLATE.
(parser::yytranslate_): New, as in lalr1.cc.
Adjust to use it.

* tests/glr-regression.at: Adjust.
---
 data/skeletons/c++.m4   |  2 +-
 data/skeletons/glr2.cc  | 32 ++++++++++----------------------
 tests/glr-regression.at | 27 +++++++++++++++++++--------
 3 files changed, 30 insertions(+), 31 deletions(-)

diff --git a/data/skeletons/c++.m4 b/data/skeletons/c++.m4
index 2ae34baf..b76aebe6 100644
--- a/data/skeletons/c++.m4
+++ b/data/skeletons/c++.m4
@@ -602,7 +602,7 @@ m4_define([b4_yytranslate_define],
     if (t <= 0)
       return symbol_kind::]b4_symbol_prefix[YYEOF;
     else if (t <= code_max)
-      return YY_CAST (symbol_kind_type, translate_table[t]);
+      return static_cast <symbol_kind_type> (translate_table[t]);
     else
       return symbol_kind::]b4_symbol_prefix[YYUNDEF;]])[
   }
diff --git a/data/skeletons/glr2.cc b/data/skeletons/glr2.cc
index 9d80ec8c..bdbdfb06 100644
--- a/data/skeletons/glr2.cc
+++ b/data/skeletons/glr2.cc
@@ -354,6 +354,11 @@ const std::ptrdiff_t strong_index_alias<T>::INVALID_INDEX =
 #endif
 
   public: // FIXME: Private
+    /// Convert a scanner token kind \a t to a symbol kind.
+    /// In theory \a t should be a token_kind_type, but character literals
+    /// are valid, yet not members of the token_type enum.
+    static symbol_kind_type yytranslate_ (int t);
+
 ]b4_parse_error_bmatch(
 [simple],
 [[#if ]b4_api_PREFIX[DEBUG || ]b4_token_table_flag[
@@ -404,7 +409,7 @@ const std::ptrdiff_t strong_index_alias<T>::INVALID_INDEX =
 
 ]b4_disclaimer[
 ]b4_cpp_guard_open([b4_spec_mapped_header_file])[
-]b4_shared_declarations[
+]b4_shared_declarations([hh])[
 ]b4_cpp_guard_close([b4_spec_mapped_header_file])[
 ]b4_output_end])[
 
@@ -434,7 +439,7 @@ const std::ptrdiff_t strong_index_alias<T>::INVALID_INDEX =
 ]b4_null_define[
 
 ]b4_header_if([[#include "@basename(]b4_spec_header_file[@)"]],
-               [b4_shared_declarations])[
+              [b4_shared_declarations([cc])])[
 
 typedef ]b4_namespace_ref[::]b4_parser_class[::symbol_kind_type 
yysymbol_kind_t;
 
@@ -529,22 +534,6 @@ static 
]b4_namespace_ref[::]b4_parser_class[::location_type yyloc_default][]b4_y
    accessed by $0, $-1, etc., in any rule.  */
 #define YYMAXLEFT ]b4_max_left_semantic_context[
 
-/* YYTRANSLATE(TOKEN-NUM) -- Symbol number corresponding to TOKEN-NUM
-   as returned by yylex, with out-of-bounds checking.  */
-]b4_api_token_raw_if(dnl
-[[#define YYTRANSLATE(YYX) static_cast<yysymbol_kind_t>(YYX)]],
-[[#define YYTRANSLATE(YYX)                                \
-  (0 <= (YYX) && (YYX) <= ]b4_code_max[                     \
-   ? static_cast<yysymbol_kind_t>(yytranslate[YYX])        \
-   : ]b4_namespace_ref[::]b4_parser_class[::]b4_symbol(empty, kind)[)
-
-/* YYTRANSLATE[TOKEN-NUM] -- Symbol number corresponding to TOKEN-NUM
-   as returned by yylex.  */
-static const ]b4_int_type_for([b4_translate])[ yytranslate[] =
-{
-  ]b4_translate[
-};]])[
-
 #if ]b4_api_PREFIX[DEBUG
 /* YYRLINE[YYN] -- source line where rule number YYN was defined.  */
 static const ]b4_int_type_for([b4_rline])[ yyrline[] =
@@ -2936,9 +2925,7 @@ yygetToken (]b4_namespace_ref[::]b4_parser_class[& 
yyparser, glr_stack& yystack]
         }
 #endif // YY_EXCEPTIONS
       yystack.yytoken
-        = (yychar <= 
]b4_namespace_ref[::]b4_parser_class[::token::]b4_symbol(eof, id)[)
-          ? ]b4_namespace_ref[::]b4_parser_class[::]b4_symbol(eof, kind)[
-          : YYTRANSLATE (yychar);
+        = ]b4_namespace_ref[::]b4_parser_class[::yytranslate_ (yychar);
     }
   if (yystack.yytoken == 
]b4_namespace_ref[::]b4_parser_class[::]b4_symbol(eof, kind)[)
     YYCDEBUG << "Now at end of input.\n";
@@ -3433,8 +3420,9 @@ b4_dollar_popdef])[]dnl
     // Actually, it is yydebug which is really used.
     yydebug = l;
   }
+#endif // ]b4_api_PREFIX[DEBUG
 
-#endif
+]b4_token_ctor_if([], [b4_yytranslate_define([cc])])[
 ]m4_popdef([b4_parse_param])dnl
 b4_namespace_close[]dnl
 b4_epilogue[]dnl
diff --git a/tests/glr-regression.at b/tests/glr-regression.at
index 7905a581..e31ebf4f 100644
--- a/tests/glr-regression.at
+++ b/tests/glr-regression.at
@@ -69,11 +69,22 @@ print_lookahead (int yychr, ]AT_YYSTYPE[ *yylvalp, 
]AT_YYLTYPE[ *yyllocp,
 ]])[
 {
 ]AT_GLR2_CC_IF([[
+  // We use -2 and 0 to avoid this warning:
+  //
+  // glr-regr13.y:114:53: error: enumeral and non-enumeral type in conditional 
expression [-Werror=extra]
+  //   114 |     : yytoken == yy::parser::symbol_kind::S_YYEOF   ? 
yy::parser::token::YYEOF
+  //       |       
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
+  //   115 |     : yytoken == yy::parser::yytranslate_ ('a')     ? 'a'
+  //       |     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+  //   116 |     : yytoken == yy::parser::yytranslate_ ('b')     ? 'b'
+  //       |     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+  //   117 |     : '?';
+  //       |     ~~~~~
   int yychr
-    = yytoken == YYTRANSLATE (]AT_TOKEN([YYEMPTY])[) ? -2
-    : yytoken == YYTRANSLATE (]AT_TOKEN([YYEOF])[)   ? 0
-    : yytoken == YYTRANSLATE ('a')                   ? 'a'
-    : yytoken == YYTRANSLATE ('b')                   ? 'b'
+    = yytoken == yy::parser::symbol_kind::S_YYEMPTY ? -2
+    : yytoken == yy::parser::symbol_kind::S_YYEOF   ? 0
+    : yytoken == yy::parser::yytranslate_ ('a')     ? 'a'
+    : yytoken == yy::parser::yytranslate_ ('b')     ? 'b'
     : '?';
 ]])[
   printf ("%s:\n  yychar=", reduction);
@@ -1383,7 +1394,7 @@ nondefstate:
 change_lookahead:
   %empty
     {
-      ]AT_GLR2_CC_IF([[yytoken = YYTRANSLATE ('a')]], [[yychar = 'a']])[;
+      ]AT_GLR2_CC_IF([[yytoken = yy::parser::yytranslate_ ('a')]], [[yychar = 
'a']])[;
     }
 ;
 
@@ -1564,7 +1575,7 @@ alt1:
     {
       USE ($][1);
       if (]AT_GLR2_CC_IF(
-        [[yytoken != YYTRANSLATE('d') && yytoken != symbol_kind::S_YYEOF]],
+        [[yytoken != yy::parser::yytranslate_ ('d') && yytoken != 
symbol_kind::S_YYEOF]],
         [[yychar != 'd' && yychar != ]AT_GLR2_CC_IF([token::])[YYEOF]])[)
         PRINT_LOOKAHEAD ("Incorrect lookahead during stack explosion.");
     }
@@ -1575,7 +1586,7 @@ alt2:
     {
       USE ($][1);
       if (]AT_GLR2_CC_IF(
-        [[yytoken != YYTRANSLATE('d') && yytoken != symbol_kind::S_YYEOF]],
+        [[yytoken != yy::parser::yytranslate_ ('d') && yytoken != 
symbol_kind::S_YYEOF]],
         [[yychar != 'd' && yychar != ]AT_GLR2_CC_IF([token::])[YYEOF]])[)
         PRINT_LOOKAHEAD ("Incorrect lookahead during stack explosion.");
     }
@@ -1586,7 +1597,7 @@ alt3:
     {
       USE ($][1);
         if (]AT_GLR2_CC_IF(
-          [[yytoken != YYTRANSLATE('d') && yytoken != symbol_kind::S_YYEOF]],
+          [[yytoken != yy::parser::yytranslate_ ('d') && yytoken != 
symbol_kind::S_YYEOF]],
           [[yychar != 'd' && yychar != ]AT_GLR2_CC_IF([token::])[YYEOF]])[)
         PRINT_LOOKAHEAD ("Incorrect lookahead during stack explosion.");
     }
-- 
2.30.0




reply via email to

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