glr.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. # C++ GLR skeleton for Bison
  2. # Copyright (C) 2002-2013 Free Software Foundation, Inc.
  3. # This program is free software: you can redistribute it and/or modify
  4. # it under the terms of the GNU General Public License as published by
  5. # the Free Software Foundation, either version 3 of the License, or
  6. # (at your option) any later version.
  7. #
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. # This skeleton produces a C++ class that encapsulates a C glr parser.
  16. # This is in order to reduce the maintenance burden. The glr.c
  17. # skeleton is clean and pure enough so that there are no real
  18. # problems. The C++ interface is the same as that of lalr1.cc. In
  19. # fact, glr.c can replace yacc.c without the user noticing any
  20. # difference, and similarly for glr.cc replacing lalr1.cc.
  21. #
  22. # The passing of parse-params
  23. #
  24. # The additional arguments are stored as members of the parser
  25. # object, yyparser. The C routines need to carry yyparser
  26. # throughout the C parser; that's easy: make yyparser an
  27. # additional parse-param. But because the C++ skeleton needs to
  28. # know the "real" original parse-param, we save them
  29. # (b4_parse_param_orig). Note that b4_parse_param is overquoted
  30. # (and c.m4 strips one level of quotes). This is a PITA, and
  31. # explains why there are so many levels of quotes.
  32. #
  33. # The locations
  34. #
  35. # We use location.cc just like lalr1.cc, but because glr.c stores
  36. # the locations in a union, the position and location classes
  37. # must not have a constructor. Therefore, contrary to lalr1.cc, we
  38. # must not define "b4_location_constructors". As a consequence the
  39. # user must initialize the first positions (in particular the
  40. # filename member).
  41. # We require a pure interface.
  42. m4_define([b4_pure_flag], [1])
  43. m4_include(b4_pkgdatadir/[c++.m4])
  44. b4_bison_locations_if([m4_include(b4_pkgdatadir/[location.cc])])
  45. m4_define([b4_parser_class_name],
  46. [b4_percent_define_get([[parser_class_name]])])
  47. # Save the parse parameters.
  48. m4_define([b4_parse_param_orig], m4_defn([b4_parse_param]))
  49. # b4_parse_param_wrap
  50. # -------------------
  51. # New ones.
  52. m4_ifset([b4_parse_param],
  53. [m4_define([b4_parse_param_wrap],
  54. [[b4_namespace_ref::b4_parser_class_name[& yyparser], [[yyparser]]],]
  55. m4_defn([b4_parse_param]))],
  56. [m4_define([b4_parse_param_wrap],
  57. [[b4_namespace_ref::b4_parser_class_name[& yyparser], [[yyparser]]]])
  58. ])
  59. # b4_yy_symbol_print_define
  60. # -------------------------
  61. # Bypass the default implementation to generate the "yy_symbol_print"
  62. # and "yy_symbol_value_print" functions.
  63. m4_define([b4_yy_symbol_print_define],
  64. [[
  65. /*--------------------.
  66. | Print this symbol. |
  67. `--------------------*/
  68. ]b4_function_define([yy_symbol_print],
  69. [static void],
  70. [[FILE *], []],
  71. [[int yytype], [yytype]],
  72. [[const ]b4_namespace_ref::b4_parser_class_name[::semantic_type *yyvaluep],
  73. [yyvaluep]][]dnl
  74. b4_locations_if([,
  75. [[const ]b4_namespace_ref::b4_parser_class_name[::location_type *yylocationp],
  76. [yylocationp]]]),
  77. b4_parse_param)[
  78. {
  79. ]b4_parse_param_use[]dnl
  80. [ yyparser.yy_symbol_print_ (yytype, yyvaluep]b4_locations_if([, yylocationp])[);
  81. }
  82. ]])[
  83. # Hijack the initial action to initialize the locations.
  84. ]b4_bison_locations_if([m4_define([b4_initial_action],
  85. [yylloc.initialize ();]m4_ifdef([b4_initial_action], [
  86. m4_defn([b4_initial_action])]))])[
  87. # Hijack the post prologue to insert early definition of YYLLOC_DEFAULT
  88. # and declaration of yyerror.
  89. ]m4_append([b4_post_prologue],
  90. [b4_syncline([@oline@], [@ofile@])[
  91. ]b4_yylloc_default_define[
  92. #define YYRHSLOC(Rhs, K) ((Rhs)[K].yystate.yyloc)
  93. ]b4_function_declare([yyerror],
  94. [static void],b4_locations_if([
  95. [[const ]b4_namespace_ref::b4_parser_class_name[::location_type *yylocationp],
  96. [yylocationp]],])
  97. b4_parse_param,
  98. [[const char* msg], [msg]])])
  99. #undef yynerrs
  100. #undef yychar
  101. #undef yylval]b4_locations_if([
  102. #undef yylloc])
  103. m4_if(b4_prefix, [yy], [],
  104. [[/* Substitute the variable and function names. */
  105. #define yyparse ]b4_prefix[parse
  106. #define yylex ]b4_prefix[lex
  107. #define yyerror ]b4_prefix[error
  108. #define yydebug ]b4_prefix[debug
  109. ]]b4_pure_if([], [[
  110. #define yylval ]b4_prefix[lval
  111. #define yychar ]b4_prefix[char
  112. #define yynerrs ]b4_prefix[nerrs]b4_locations_if([[
  113. #define yylloc ]b4_prefix[lloc]])]))
  114. # Hijack the epilogue to define implementations (yyerror, parser member
  115. # functions etc.).
  116. m4_append([b4_epilogue],
  117. [b4_syncline([@oline@], [@ofile@])[
  118. /*------------------.
  119. | Report an error. |
  120. `------------------*/
  121. ]b4_function_define([yyerror],
  122. [static void],b4_locations_if([
  123. [[const ]b4_namespace_ref::b4_parser_class_name[::location_type *yylocationp],
  124. [yylocationp]],])
  125. b4_parse_param,
  126. [[const char* msg], [msg]])[
  127. {
  128. ]b4_parse_param_use[]dnl
  129. [ yyparser.error (]b4_locations_if([[*yylocationp, ]])[msg);
  130. }
  131. ]b4_namespace_open[
  132. ]dnl In this section, the parse params are the original parse_params.
  133. m4_pushdef([b4_parse_param], m4_defn([b4_parse_param_orig]))dnl
  134. [ /// Build a parser object.
  135. ]b4_parser_class_name::b4_parser_class_name[ (]b4_parse_param_decl[)]m4_ifset([b4_parse_param], [
  136. :])[
  137. #if ]b4_api_PREFIX[DEBUG
  138. ]m4_ifset([b4_parse_param], [ ], [ :])[yycdebug_ (&std::cerr)]m4_ifset([b4_parse_param], [,])[
  139. #endif]b4_parse_param_cons[
  140. {
  141. }
  142. ]b4_parser_class_name::~b4_parser_class_name[ ()
  143. {
  144. }
  145. int
  146. ]b4_parser_class_name[::parse ()
  147. {
  148. return ::yyparse (*this]b4_user_args[);
  149. }
  150. #if ]b4_api_PREFIX[DEBUG
  151. /*--------------------.
  152. | Print this symbol. |
  153. `--------------------*/
  154. inline void
  155. ]b4_parser_class_name[::yy_symbol_value_print_ (int yytype,
  156. const semantic_type* yyvaluep]b4_locations_if([[,
  157. const location_type* yylocationp]])[)
  158. {]b4_locations_if([[
  159. YYUSE (yylocationp);]])[
  160. YYUSE (yyvaluep);
  161. std::ostream& yyoutput = debug_stream ();
  162. std::ostream& yyo = yyoutput;
  163. YYUSE (yyo);
  164. ]b4_symbol_actions([printer])[
  165. }
  166. void
  167. ]b4_parser_class_name[::yy_symbol_print_ (int yytype,
  168. const semantic_type* yyvaluep]b4_locations_if([[,
  169. const location_type* yylocationp]])[)
  170. {
  171. *yycdebug_ << (yytype < YYNTOKENS ? "token" : "nterm")
  172. << ' ' << yytname[yytype] << " ("]b4_locations_if([[
  173. << *yylocationp << ": "]])[;
  174. yy_symbol_value_print_ (yytype, yyvaluep]b4_locations_if([[, yylocationp]])[);
  175. *yycdebug_ << ')';
  176. }
  177. std::ostream&
  178. ]b4_parser_class_name[::debug_stream () const
  179. {
  180. return *yycdebug_;
  181. }
  182. void
  183. ]b4_parser_class_name[::set_debug_stream (std::ostream& o)
  184. {
  185. yycdebug_ = &o;
  186. }
  187. ]b4_parser_class_name[::debug_level_type
  188. ]b4_parser_class_name[::debug_level () const
  189. {
  190. return yydebug;
  191. }
  192. void
  193. ]b4_parser_class_name[::set_debug_level (debug_level_type l)
  194. {
  195. // Actually, it is yydebug which is really used.
  196. yydebug = l;
  197. }
  198. #endif
  199. ]m4_popdef([b4_parse_param])dnl
  200. b4_namespace_close
  201. ])
  202. # b4_shared_declarations
  203. # ----------------------
  204. # Declaration that might either go into the header (if --defines)
  205. # or open coded in the parser body.
  206. m4_define([b4_shared_declarations],
  207. [m4_pushdef([b4_parse_param], m4_defn([b4_parse_param_orig]))dnl
  208. b4_percent_code_get([[requires]])[
  209. #include <stdexcept>
  210. #include <string>
  211. #include <iostream>]b4_defines_if([
  212. b4_bison_locations_if([[#include "location.hh"]])])[
  213. ]b4_YYDEBUG_define[
  214. ]b4_namespace_open[
  215. ]b4_defines_if([],
  216. [b4_bison_locations_if([b4_position_define
  217. b4_location_define])])[
  218. /// A Bison parser.
  219. class ]b4_parser_class_name[
  220. {
  221. public:
  222. ]b4_public_types_declare[
  223. /// Build a parser object.
  224. ]b4_parser_class_name[ (]b4_parse_param_decl[);
  225. virtual ~]b4_parser_class_name[ ();
  226. /// Parse.
  227. /// \returns 0 iff parsing succeeded.
  228. virtual int parse ();
  229. /// The current debugging stream.
  230. std::ostream& debug_stream () const;
  231. /// Set the current debugging stream.
  232. void set_debug_stream (std::ostream &);
  233. /// Type for debugging levels.
  234. typedef int debug_level_type;
  235. /// The current debugging level.
  236. debug_level_type debug_level () const;
  237. /// Set the current debugging level.
  238. void set_debug_level (debug_level_type l);
  239. public:
  240. /// Report a syntax error.]b4_locations_if([[
  241. /// \param loc where the syntax error is found.]])[
  242. /// \param msg a description of the syntax error.
  243. virtual void error (]b4_locations_if([[const location_type& loc, ]])[const std::string& msg);
  244. # if ]b4_api_PREFIX[DEBUG
  245. public:
  246. /// \brief Report a symbol value on the debug stream.
  247. /// \param yytype The token type.
  248. /// \param yyvaluep Its semantic value.]b4_locations_if([[
  249. /// \param yylocationp Its location.]])[
  250. virtual void yy_symbol_value_print_ (int yytype,
  251. const semantic_type* yyvaluep]b4_locations_if([[,
  252. const location_type* yylocationp]])[);
  253. /// \brief Report a symbol on the debug stream.
  254. /// \param yytype The token type.
  255. /// \param yyvaluep Its semantic value.]b4_locations_if([[
  256. /// \param yylocationp Its location.]])[
  257. virtual void yy_symbol_print_ (int yytype,
  258. const semantic_type* yyvaluep]b4_locations_if([[,
  259. const location_type* yylocationp]])[);
  260. private:
  261. // Debugging.
  262. std::ostream* yycdebug_;
  263. #endif
  264. ]b4_parse_param_vars[
  265. };
  266. ]dnl Redirections for glr.c.
  267. b4_percent_define_flag_if([[global_tokens_and_yystype]],
  268. [b4_token_defines])
  269. [
  270. #ifndef ]b4_api_PREFIX[STYPE
  271. # define ]b4_api_PREFIX[STYPE ]b4_namespace_ref[::]b4_parser_class_name[::semantic_type
  272. #endif
  273. #ifndef ]b4_api_PREFIX[LTYPE
  274. # define ]b4_api_PREFIX[LTYPE ]b4_namespace_ref[::]b4_parser_class_name[::location_type
  275. #endif
  276. ]b4_namespace_close[
  277. ]b4_percent_code_get([[provides]])[
  278. ]m4_popdef([b4_parse_param])dnl
  279. ])
  280. b4_defines_if(
  281. [b4_output_begin([b4_spec_defines_file])
  282. b4_copyright([Skeleton interface for Bison GLR parsers in C++],
  283. [2002-2013])[
  284. // C++ GLR parser skeleton written by Akim Demaille.
  285. ]b4_cpp_guard_open([b4_spec_defines_file])[
  286. ]b4_shared_declarations[
  287. ]b4_cpp_guard_close([b4_spec_defines_file])[
  288. ]b4_output_end()])
  289. # Let glr.c (and b4_shared_declarations) believe that the user
  290. # arguments include the parser itself.
  291. m4_pushdef([b4_parse_param], m4_defn([b4_parse_param_wrap]))
  292. m4_include(b4_pkgdatadir/[glr.c])
  293. m4_popdef([b4_parse_param])