lalr1.cc 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161
  1. # C++ 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. m4_include(b4_pkgdatadir/[c++.m4])
  16. # api.value.type=variant is valid.
  17. m4_define([b4_value_type_setup_variant])
  18. # b4_integral_parser_table_declare(TABLE-NAME, CONTENT, COMMENT)
  19. # --------------------------------------------------------------
  20. # Declare "parser::yy<TABLE-NAME>_" whose contents is CONTENT.
  21. m4_define([b4_integral_parser_table_declare],
  22. [m4_ifval([$3], [b4_comment([$3], [ ])
  23. ])dnl
  24. static const b4_int_type_for([$2]) yy$1_[[]];dnl
  25. ])
  26. # b4_integral_parser_table_define(TABLE-NAME, CONTENT, COMMENT)
  27. # -------------------------------------------------------------
  28. # Define "parser::yy<TABLE-NAME>_" whose contents is CONTENT.
  29. m4_define([b4_integral_parser_table_define],
  30. [ const b4_int_type_for([$2])
  31. b4_parser_class_name::yy$1_[[]] =
  32. {
  33. $2
  34. };dnl
  35. ])
  36. # b4_symbol_value_template(VAL, [TYPE])
  37. # -------------------------------------
  38. # Same as b4_symbol_value, but used in a template method. It makes
  39. # a difference when using variants. Note that b4_value_type_setup_union
  40. # overrides b4_symbol_value, so we must override it again.
  41. m4_copy([b4_symbol_value], [b4_symbol_value_template])
  42. m4_append([b4_value_type_setup_union],
  43. [m4_copy_force([b4_symbol_value_union], [b4_symbol_value_template])])
  44. # b4_lhs_value([TYPE])
  45. # --------------------
  46. # Expansion of $<TYPE>$.
  47. m4_define([b4_lhs_value],
  48. [b4_symbol_value([yylhs.value], [$1])])
  49. # b4_lhs_location()
  50. # -----------------
  51. # Expansion of @$.
  52. m4_define([b4_lhs_location],
  53. [yylhs.location])
  54. # b4_rhs_data(RULE-LENGTH, NUM)
  55. # -----------------------------
  56. # Return the data corresponding to the symbol #NUM, where the current
  57. # rule has RULE-LENGTH symbols on RHS.
  58. m4_define([b4_rhs_data],
  59. [yystack_@{b4_subtract($@)@}])
  60. # b4_rhs_state(RULE-LENGTH, NUM)
  61. # ------------------------------
  62. # The state corresponding to the symbol #NUM, where the current
  63. # rule has RULE-LENGTH symbols on RHS.
  64. m4_define([b4_rhs_state],
  65. [b4_rhs_data([$1], [$2]).state])
  66. # b4_rhs_value(RULE-LENGTH, NUM, [TYPE])
  67. # --------------------------------------
  68. # Expansion of $<TYPE>NUM, where the current rule has RULE-LENGTH
  69. # symbols on RHS.
  70. m4_define([b4_rhs_value],
  71. [b4_symbol_value([b4_rhs_data([$1], [$2]).value], [$3])])
  72. # b4_rhs_location(RULE-LENGTH, NUM)
  73. # ---------------------------------
  74. # Expansion of @NUM, where the current rule has RULE-LENGTH symbols
  75. # on RHS.
  76. m4_define([b4_rhs_location],
  77. [b4_rhs_data([$1], [$2]).location])
  78. # b4_symbol_action(SYMBOL-NUM, KIND)
  79. # ----------------------------------
  80. # Run the action KIND (destructor or printer) for SYMBOL-NUM.
  81. # Same as in C, but using references instead of pointers.
  82. m4_define([b4_symbol_action],
  83. [b4_symbol_if([$1], [has_$2],
  84. [m4_pushdef([b4_symbol_value], m4_defn([b4_symbol_value_template]))[]dnl
  85. b4_dollar_pushdef([yysym.value],
  86. b4_symbol_if([$1], [has_type],
  87. [m4_dquote(b4_symbol([$1], [type]))]),
  88. [yysym.location])dnl
  89. b4_symbol_case_([$1])
  90. b4_syncline([b4_symbol([$1], [$2_line])], ["b4_symbol([$1], [$2_file])"])
  91. b4_symbol([$1], [$2])
  92. b4_syncline([@oline@], [@ofile@])
  93. break;
  94. m4_popdef([b4_symbol_value])[]dnl
  95. b4_dollar_popdef[]dnl
  96. ])])
  97. # b4_lex
  98. # ------
  99. # Call yylex.
  100. m4_define([b4_lex],
  101. [b4_token_ctor_if(
  102. [b4_function_call([yylex],
  103. [symbol_type], m4_ifdef([b4_lex_param], b4_lex_param))],
  104. [b4_function_call([yylex], [int],
  105. [b4_api_PREFIX[STYPE*], [&yyla.value]][]dnl
  106. b4_locations_if([, [[location*], [&yyla.location]]])dnl
  107. m4_ifdef([b4_lex_param], [, ]b4_lex_param))])])
  108. m4_pushdef([b4_copyright_years],
  109. [2002-2013])
  110. m4_define([b4_parser_class_name],
  111. [b4_percent_define_get([[parser_class_name]])])
  112. b4_bison_locations_if([# Backward compatibility.
  113. m4_define([b4_location_constructors])
  114. m4_include(b4_pkgdatadir/[location.cc])])
  115. m4_include(b4_pkgdatadir/[stack.hh])
  116. b4_variant_if([m4_include(b4_pkgdatadir/[variant.hh])])
  117. # b4_shared_declarations
  118. # ----------------------
  119. # Declaration that might either go into the header (if --defines)
  120. # or open coded in the parser body.
  121. m4_define([b4_shared_declarations],
  122. [b4_percent_code_get([[requires]])[
  123. ]b4_parse_assert_if([# include <cassert>])[
  124. # include <vector>
  125. # include <iostream>
  126. # include <stdexcept>
  127. # include <string>]b4_defines_if([[
  128. # include "stack.hh"
  129. ]b4_bison_locations_if([[# include "location.hh"]])])[
  130. ]b4_variant_if([b4_variant_includes])[
  131. ]b4_YYDEBUG_define[
  132. ]b4_namespace_open[
  133. ]b4_defines_if([],
  134. [b4_stack_define
  135. b4_bison_locations_if([b4_position_define
  136. b4_location_define])])[
  137. ]b4_variant_if([b4_variant_define])[
  138. /// A Bison parser.
  139. class ]b4_parser_class_name[
  140. {
  141. public:
  142. ]b4_public_types_declare[
  143. /// Build a parser object.
  144. ]b4_parser_class_name[ (]b4_parse_param_decl[);
  145. virtual ~]b4_parser_class_name[ ();
  146. /// Parse.
  147. /// \returns 0 iff parsing succeeded.
  148. virtual int parse ();
  149. #if ]b4_api_PREFIX[DEBUG
  150. /// The current debugging stream.
  151. std::ostream& debug_stream () const;
  152. /// Set the current debugging stream.
  153. void set_debug_stream (std::ostream &);
  154. /// Type for debugging levels.
  155. typedef int debug_level_type;
  156. /// The current debugging level.
  157. debug_level_type debug_level () const;
  158. /// Set the current debugging level.
  159. void set_debug_level (debug_level_type l);
  160. #endif
  161. /// Report a syntax error.]b4_locations_if([[
  162. /// \param loc where the syntax error is found.]])[
  163. /// \param msg a description of the syntax error.
  164. virtual void error (]b4_locations_if([[const location_type& loc, ]])[const std::string& msg);
  165. /// Report a syntax error.
  166. void error (const syntax_error& err);
  167. private:
  168. /// This class is not copyable.
  169. ]b4_parser_class_name[ (const ]b4_parser_class_name[&);
  170. ]b4_parser_class_name[& operator= (const ]b4_parser_class_name[&);
  171. /// State numbers.
  172. typedef int state_type;
  173. /// Generate an error message.
  174. /// \param yystate the state where the error occurred.
  175. /// \param yytoken the lookahead token type, or yyempty_.
  176. virtual std::string yysyntax_error_ (state_type yystate,
  177. symbol_number_type yytoken) const;
  178. /// Compute post-reduction state.
  179. /// \param yystate the current state
  180. /// \param yylhs the nonterminal to push on the stack
  181. state_type yy_lr_goto_state_ (state_type yystate, int yylhs);
  182. /// Whether the given \c yypact_ value indicates a defaulted state.
  183. /// \param yyvalue the value to check
  184. static bool yy_pact_value_is_default_ (int yyvalue);
  185. /// Whether the given \c yytable_ value indicates a syntax error.
  186. /// \param yyvalue the value to check
  187. static bool yy_table_value_is_error_ (int yyvalue);
  188. static const ]b4_int_type(b4_pact_ninf, b4_pact_ninf)[ yypact_ninf_;
  189. static const ]b4_int_type(b4_table_ninf, b4_table_ninf)[ yytable_ninf_;
  190. /// Convert a scanner token number \a t to a symbol number.
  191. static token_number_type yytranslate_ (]b4_token_ctor_if([token_type], [int])[ t);
  192. // Tables.
  193. ]b4_parser_tables_declare[]b4_error_verbose_if([
  194. /// Convert the symbol name \a n to a form suitable for a diagnostic.
  195. static std::string yytnamerr_ (const char *n);])[
  196. ]b4_token_table_if([], [[#if ]b4_api_PREFIX[DEBUG]])[
  197. /// For a symbol, its name in clear.
  198. static const char* const yytname_[];
  199. ]b4_token_table_if([[#if ]b4_api_PREFIX[DEBUG]])[
  200. ]b4_integral_parser_table_declare([rline], [b4_rline],
  201. [[YYRLINE[YYN] -- Source line where rule number YYN was defined.]])[
  202. /// Report on the debug stream that the rule \a r is going to be reduced.
  203. virtual void yy_reduce_print_ (int r);
  204. /// Print the state stack on the debug stream.
  205. virtual void yystack_print_ ();
  206. // Debugging.
  207. int yydebug_;
  208. std::ostream* yycdebug_;
  209. /// \brief Display a symbol type, value and location.
  210. /// \param yyo The output stream.
  211. /// \param yysym The symbol.
  212. template <typename Base>
  213. void yy_print_ (std::ostream& yyo, const basic_symbol<Base>& yysym) const;
  214. #endif
  215. /// \brief Reclaim the memory associated to a symbol.
  216. /// \param yymsg Why this token is reclaimed.
  217. /// If null, print nothing.
  218. /// \param s The symbol.
  219. template <typename Base>
  220. void yy_destroy_ (const char* yymsg, basic_symbol<Base>& yysym) const;
  221. private:
  222. /// Type access provider for state based symbols.
  223. struct by_state
  224. {
  225. /// Default constructor.
  226. by_state ();
  227. /// The symbol type as needed by the constructor.
  228. typedef state_type kind_type;
  229. /// Constructor.
  230. by_state (kind_type s);
  231. /// Copy constructor.
  232. by_state (const by_state& other);
  233. /// Steal the symbol type from \a that.
  234. void move (by_state& that);
  235. /// The (internal) type number (corresponding to \a state).
  236. /// "empty" when empty.
  237. symbol_number_type type_get () const;
  238. enum { empty = 0 };
  239. /// The state.
  240. state_type state;
  241. };
  242. /// "Internal" symbol: element of the stack.
  243. struct stack_symbol_type : basic_symbol<by_state>
  244. {
  245. /// Superclass.
  246. typedef basic_symbol<by_state> super_type;
  247. /// Construct an empty symbol.
  248. stack_symbol_type ();
  249. /// Steal the contents from \a sym to build this.
  250. stack_symbol_type (state_type s, symbol_type& sym);
  251. /// Assignment, needed by push_back.
  252. stack_symbol_type& operator= (const stack_symbol_type& that);
  253. };
  254. /// Stack type.
  255. typedef stack<stack_symbol_type> stack_type;
  256. /// The stack.
  257. stack_type yystack_;
  258. /// Push a new state on the stack.
  259. /// \param m a debug message to display
  260. /// if null, no trace is output.
  261. /// \param s the symbol
  262. /// \warning the contents of \a s.value is stolen.
  263. void yypush_ (const char* m, stack_symbol_type& s);
  264. /// Push a new look ahead token on the state on the stack.
  265. /// \param m a debug message to display
  266. /// if null, no trace is output.
  267. /// \param s the state
  268. /// \param sym the symbol (for its value and location).
  269. /// \warning the contents of \a s.value is stolen.
  270. void yypush_ (const char* m, state_type s, symbol_type& sym);
  271. /// Pop \a n symbols the three stacks.
  272. void yypop_ (unsigned int n = 1);
  273. // Constants.
  274. enum
  275. {
  276. yyeof_ = 0,
  277. yylast_ = ]b4_last[, //< Last index in yytable_.
  278. yynnts_ = ]b4_nterms_number[, //< Number of nonterminal symbols.
  279. yyempty_ = -2,
  280. yyfinal_ = ]b4_final_state_number[, //< Termination state number.
  281. yyterror_ = 1,
  282. yyerrcode_ = 256,
  283. yyntokens_ = ]b4_tokens_number[ //< Number of tokens.
  284. };
  285. ]b4_parse_param_vars[
  286. };
  287. ]b4_token_ctor_if([b4_yytranslate_define
  288. b4_public_types_define])[
  289. ]b4_namespace_close[
  290. ]b4_percent_define_flag_if([[global_tokens_and_yystype]],
  291. [b4_token_defines
  292. #ifndef ]b4_api_PREFIX[STYPE
  293. // Redirection for backward compatibility.
  294. # define ]b4_api_PREFIX[STYPE b4_namespace_ref::b4_parser_class_name::semantic_type
  295. #endif
  296. ])[
  297. ]b4_percent_code_get([[provides]])[
  298. ]])
  299. b4_defines_if(
  300. [b4_output_begin([b4_spec_defines_file])
  301. b4_copyright([Skeleton interface for Bison LALR(1) parsers in C++])
  302. [
  303. /**
  304. ** \file ]b4_spec_defines_file[
  305. ** Define the ]b4_namespace_ref[::parser class.
  306. */
  307. // C++ LALR(1) parser skeleton written by Akim Demaille.
  308. ]b4_cpp_guard_open([b4_spec_defines_file])[
  309. ]b4_shared_declarations[
  310. ]b4_cpp_guard_close([b4_spec_defines_file])
  311. b4_output_end()
  312. ])
  313. b4_output_begin([b4_parser_file_name])
  314. b4_copyright([Skeleton implementation for Bison LALR(1) parsers in C++])
  315. b4_percent_code_get([[top]])[]dnl
  316. m4_if(b4_prefix, [yy], [],
  317. [
  318. // Take the name prefix into account.
  319. #define yylex b4_prefix[]lex])[
  320. // First part of user declarations.
  321. ]b4_user_pre_prologue[
  322. ]b4_null_define[
  323. ]b4_defines_if([[#include "@basename(]b4_spec_defines_file[@)"]],
  324. [b4_shared_declarations])[
  325. // User implementation prologue.
  326. ]b4_user_post_prologue[
  327. ]b4_percent_code_get[
  328. #ifndef YY_
  329. # if defined YYENABLE_NLS && YYENABLE_NLS
  330. # if ENABLE_NLS
  331. # include <libintl.h> // FIXME: INFRINGES ON USER NAME SPACE.
  332. # define YY_(msgid) dgettext ("bison-runtime", msgid)
  333. # endif
  334. # endif
  335. # ifndef YY_
  336. # define YY_(msgid) msgid
  337. # endif
  338. #endif
  339. ]b4_locations_if([dnl
  340. [#define YYRHSLOC(Rhs, K) ((Rhs)[K].location)
  341. ]b4_yylloc_default_define])[
  342. // Suppress unused-variable warnings by "using" E.
  343. #define YYUSE(E) ((void) (E))
  344. // Enable debugging if requested.
  345. #if ]b4_api_PREFIX[DEBUG
  346. // A pseudo ostream that takes yydebug_ into account.
  347. # define YYCDEBUG if (yydebug_) (*yycdebug_)
  348. # define YY_SYMBOL_PRINT(Title, Symbol) \
  349. do { \
  350. if (yydebug_) \
  351. { \
  352. *yycdebug_ << Title << ' '; \
  353. yy_print_ (*yycdebug_, Symbol); \
  354. *yycdebug_ << std::endl; \
  355. } \
  356. } while (false)
  357. # define YY_REDUCE_PRINT(Rule) \
  358. do { \
  359. if (yydebug_) \
  360. yy_reduce_print_ (Rule); \
  361. } while (false)
  362. # define YY_STACK_PRINT() \
  363. do { \
  364. if (yydebug_) \
  365. yystack_print_ (); \
  366. } while (false)
  367. #else // !]b4_api_PREFIX[DEBUG
  368. # define YYCDEBUG if (false) std::cerr
  369. # define YY_SYMBOL_PRINT(Title, Symbol) YYUSE(Symbol)
  370. # define YY_REDUCE_PRINT(Rule) static_cast<void>(0)
  371. # define YY_STACK_PRINT() static_cast<void>(0)
  372. #endif // !]b4_api_PREFIX[DEBUG
  373. #define yyerrok (yyerrstatus_ = 0)
  374. #define yyclearin (yyempty = true)
  375. #define YYACCEPT goto yyacceptlab
  376. #define YYABORT goto yyabortlab
  377. #define YYERROR goto yyerrorlab
  378. #define YYRECOVERING() (!!yyerrstatus_)
  379. ]b4_namespace_open[]b4_error_verbose_if([[
  380. /* Return YYSTR after stripping away unnecessary quotes and
  381. backslashes, so that it's suitable for yyerror. The heuristic is
  382. that double-quoting is unnecessary unless the string contains an
  383. apostrophe, a comma, or backslash (other than backslash-backslash).
  384. YYSTR is taken from yytname. */
  385. std::string
  386. ]b4_parser_class_name[::yytnamerr_ (const char *yystr)
  387. {
  388. if (*yystr == '"')
  389. {
  390. std::string yyr = "";
  391. char const *yyp = yystr;
  392. for (;;)
  393. switch (*++yyp)
  394. {
  395. case '\'':
  396. case ',':
  397. goto do_not_strip_quotes;
  398. case '\\':
  399. if (*++yyp != '\\')
  400. goto do_not_strip_quotes;
  401. // Fall through.
  402. default:
  403. yyr += *yyp;
  404. break;
  405. case '"':
  406. return yyr;
  407. }
  408. do_not_strip_quotes: ;
  409. }
  410. return yystr;
  411. }
  412. ]])[
  413. /// Build a parser object.
  414. ]b4_parser_class_name::b4_parser_class_name[ (]b4_parse_param_decl[)]m4_ifset([b4_parse_param], [
  415. :])[
  416. #if ]b4_api_PREFIX[DEBUG
  417. ]m4_ifset([b4_parse_param], [ ], [ :])[yydebug_ (false),
  418. yycdebug_ (&std::cerr)]m4_ifset([b4_parse_param], [,])[
  419. #endif]b4_parse_param_cons[
  420. {}
  421. ]b4_parser_class_name::~b4_parser_class_name[ ()
  422. {}
  423. /*---------------.
  424. | Symbol types. |
  425. `---------------*/
  426. ]b4_token_ctor_if([], [b4_public_types_define])[
  427. // by_state.
  428. inline
  429. ]b4_parser_class_name[::by_state::by_state ()
  430. : state (empty)
  431. {}
  432. inline
  433. ]b4_parser_class_name[::by_state::by_state (const by_state& other)
  434. : state (other.state)
  435. {}
  436. inline
  437. void
  438. ]b4_parser_class_name[::by_state::move (by_state& that)
  439. {
  440. state = that.state;
  441. that.state = empty;
  442. }
  443. inline
  444. ]b4_parser_class_name[::by_state::by_state (state_type s)
  445. : state (s)
  446. {}
  447. inline
  448. ]b4_parser_class_name[::symbol_number_type
  449. ]b4_parser_class_name[::by_state::type_get () const
  450. {
  451. return state == empty ? 0 : yystos_[state];
  452. }
  453. inline
  454. ]b4_parser_class_name[::stack_symbol_type::stack_symbol_type ()
  455. {}
  456. inline
  457. ]b4_parser_class_name[::stack_symbol_type::stack_symbol_type (state_type s, symbol_type& that)
  458. : super_type (s]b4_locations_if([, that.location])[)
  459. {
  460. ]b4_variant_if([b4_symbol_variant([that.type_get ()],
  461. [value], [move], [that.value])],
  462. [[value = that.value;]])[
  463. // that is emptied.
  464. that.type = empty;
  465. }
  466. inline
  467. ]b4_parser_class_name[::stack_symbol_type&
  468. ]b4_parser_class_name[::stack_symbol_type::operator= (const stack_symbol_type& that)
  469. {
  470. state = that.state;
  471. ]b4_variant_if([b4_symbol_variant([that.type_get ()],
  472. [value], [copy], [that.value])],
  473. [[value = that.value;]])[]b4_locations_if([
  474. location = that.location;])[
  475. return *this;
  476. }
  477. template <typename Base>
  478. inline
  479. void
  480. ]b4_parser_class_name[::yy_destroy_ (const char* yymsg, basic_symbol<Base>& yysym) const
  481. {
  482. if (yymsg)
  483. YY_SYMBOL_PRINT (yymsg, yysym);]b4_variant_if([], [
  484. // User destructor.
  485. b4_symbol_actions([destructor], [yysym.type_get ()])])[
  486. }
  487. #if ]b4_api_PREFIX[DEBUG
  488. template <typename Base>
  489. void
  490. ]b4_parser_class_name[::yy_print_ (std::ostream& yyo,
  491. const basic_symbol<Base>& yysym) const
  492. {
  493. std::ostream& yyoutput = yyo;
  494. YYUSE (yyoutput);
  495. symbol_number_type yytype = yysym.type_get ();
  496. yyo << (yytype < yyntokens_ ? "token" : "nterm")
  497. << ' ' << yytname_[yytype] << " ("]b4_locations_if([
  498. << yysym.location << ": "])[;
  499. ]b4_symbol_actions([printer])[
  500. yyo << ')';
  501. }
  502. #endif
  503. inline
  504. void
  505. ]b4_parser_class_name[::yypush_ (const char* m, state_type s, symbol_type& sym)
  506. {
  507. stack_symbol_type t (s, sym);
  508. yypush_ (m, t);
  509. }
  510. inline
  511. void
  512. ]b4_parser_class_name[::yypush_ (const char* m, stack_symbol_type& s)
  513. {
  514. if (m)
  515. YY_SYMBOL_PRINT (m, s);
  516. yystack_.push (s);
  517. }
  518. inline
  519. void
  520. ]b4_parser_class_name[::yypop_ (unsigned int n)
  521. {
  522. yystack_.pop (n);
  523. }
  524. #if ]b4_api_PREFIX[DEBUG
  525. std::ostream&
  526. ]b4_parser_class_name[::debug_stream () const
  527. {
  528. return *yycdebug_;
  529. }
  530. void
  531. ]b4_parser_class_name[::set_debug_stream (std::ostream& o)
  532. {
  533. yycdebug_ = &o;
  534. }
  535. ]b4_parser_class_name[::debug_level_type
  536. ]b4_parser_class_name[::debug_level () const
  537. {
  538. return yydebug_;
  539. }
  540. void
  541. ]b4_parser_class_name[::set_debug_level (debug_level_type l)
  542. {
  543. yydebug_ = l;
  544. }
  545. #endif // ]b4_api_PREFIX[DEBUG
  546. inline ]b4_parser_class_name[::state_type
  547. ]b4_parser_class_name[::yy_lr_goto_state_ (state_type yystate, int yylhs)
  548. {
  549. int yyr = yypgoto_[yylhs - yyntokens_] + yystate;
  550. if (0 <= yyr && yyr <= yylast_ && yycheck_[yyr] == yystate)
  551. return yytable_[yyr];
  552. else
  553. return yydefgoto_[yylhs - yyntokens_];
  554. }
  555. inline bool
  556. ]b4_parser_class_name[::yy_pact_value_is_default_ (int yyvalue)
  557. {
  558. return yyvalue == yypact_ninf_;
  559. }
  560. inline bool
  561. ]b4_parser_class_name[::yy_table_value_is_error_ (int yyvalue)
  562. {
  563. return yyvalue == yytable_ninf_;
  564. }
  565. int
  566. ]b4_parser_class_name[::parse ()
  567. {
  568. /// Whether yyla contains a lookahead.
  569. bool yyempty = true;
  570. // State.
  571. int yyn;
  572. int yylen = 0;
  573. // Error handling.
  574. int yynerrs_ = 0;
  575. int yyerrstatus_ = 0;
  576. /// The lookahead symbol.
  577. symbol_type yyla;]b4_locations_if([[
  578. /// The locations where the error started and ended.
  579. stack_symbol_type yyerror_range[3];]])[
  580. /// $$ and @@$.
  581. stack_symbol_type yylhs;
  582. /// The return value of parse ().
  583. int yyresult;
  584. // FIXME: This shoud be completely indented. It is not yet to
  585. // avoid gratuitous conflicts when merging into the master branch.
  586. try
  587. {
  588. YYCDEBUG << "Starting parse" << std::endl;
  589. ]m4_ifdef([b4_initial_action], [
  590. b4_dollar_pushdef([yyla.value], [], [yyla.location])dnl
  591. // User initialization code.
  592. b4_user_initial_action
  593. b4_dollar_popdef])[]dnl
  594. [ /* Initialize the stack. The initial state will be set in
  595. yynewstate, since the latter expects the semantical and the
  596. location values to have been already stored, initialize these
  597. stacks with a primary value. */
  598. yystack_.clear ();
  599. yypush_ (YY_NULL, 0, yyla);
  600. // A new symbol was pushed on the stack.
  601. yynewstate:
  602. YYCDEBUG << "Entering state " << yystack_[0].state << std::endl;
  603. // Accept?
  604. if (yystack_[0].state == yyfinal_)
  605. goto yyacceptlab;
  606. goto yybackup;
  607. // Backup.
  608. yybackup:
  609. // Try to take a decision without lookahead.
  610. yyn = yypact_[yystack_[0].state];
  611. if (yy_pact_value_is_default_ (yyn))
  612. goto yydefault;
  613. // Read a lookahead token.
  614. if (yyempty)
  615. {
  616. YYCDEBUG << "Reading a token: ";
  617. try
  618. {]b4_token_ctor_if([[
  619. symbol_type yylookahead (]b4_lex[);
  620. yyla.move (yylookahead);]], [[
  621. yyla.type = yytranslate_ (]b4_lex[);]])[
  622. }
  623. catch (const syntax_error& yyexc)
  624. {
  625. error (yyexc);
  626. goto yyerrlab1;
  627. }
  628. yyempty = false;
  629. }
  630. YY_SYMBOL_PRINT ("Next token is", yyla);
  631. /* If the proper action on seeing token YYLA.TYPE is to reduce or
  632. to detect an error, take that action. */
  633. yyn += yyla.type_get ();
  634. if (yyn < 0 || yylast_ < yyn || yycheck_[yyn] != yyla.type_get ())
  635. goto yydefault;
  636. // Reduce or error.
  637. yyn = yytable_[yyn];
  638. if (yyn <= 0)
  639. {
  640. if (yy_table_value_is_error_ (yyn))
  641. goto yyerrlab;
  642. yyn = -yyn;
  643. goto yyreduce;
  644. }
  645. // Discard the token being shifted.
  646. yyempty = true;
  647. // Count tokens shifted since error; after three, turn off error status.
  648. if (yyerrstatus_)
  649. --yyerrstatus_;
  650. // Shift the lookahead token.
  651. yypush_ ("Shifting", yyn, yyla);
  652. goto yynewstate;
  653. /*-----------------------------------------------------------.
  654. | yydefault -- do the default action for the current state. |
  655. `-----------------------------------------------------------*/
  656. yydefault:
  657. yyn = yydefact_[yystack_[0].state];
  658. if (yyn == 0)
  659. goto yyerrlab;
  660. goto yyreduce;
  661. /*-----------------------------.
  662. | yyreduce -- Do a reduction. |
  663. `-----------------------------*/
  664. yyreduce:
  665. yylen = yyr2_[yyn];
  666. yylhs.state = yy_lr_goto_state_(yystack_[yylen].state, yyr1_[yyn]);]b4_variant_if([
  667. /* Variants are always initialized to an empty instance of the
  668. correct type. The default $$=$1 action is NOT applied when using
  669. variants. */
  670. b4_symbol_variant([[yyr1_@{yyn@}]], [yylhs.value], [build])],[
  671. /* If YYLEN is nonzero, implement the default value of the action:
  672. '$$ = $1'. Otherwise, use the top of the stack.
  673. Otherwise, the following line sets YYLHS.VALUE to garbage.
  674. This behavior is undocumented and Bison
  675. users should not rely upon it. */
  676. if (yylen)
  677. yylhs.value = yystack_@{yylen - 1@}.value;
  678. else
  679. yylhs.value = yystack_@{0@}.value;])[
  680. ]b4_locations_if([dnl
  681. [
  682. // Compute the default @@$.
  683. {
  684. slice<stack_symbol_type, stack_type> slice (yystack_, yylen);
  685. YYLLOC_DEFAULT (yylhs.location, slice, yylen);
  686. }]])[
  687. // Perform the reduction.
  688. YY_REDUCE_PRINT (yyn);
  689. try
  690. {
  691. switch (yyn)
  692. {
  693. ]b4_user_actions[
  694. default:
  695. break;
  696. }
  697. }
  698. catch (const syntax_error& yyexc)
  699. {
  700. error (yyexc);
  701. YYERROR;
  702. }
  703. YY_SYMBOL_PRINT ("-> $$ =", yylhs);
  704. yypop_ (yylen);
  705. yylen = 0;
  706. YY_STACK_PRINT ();
  707. // Shift the result of the reduction.
  708. yypush_ (YY_NULL, yylhs);
  709. goto yynewstate;
  710. /*--------------------------------------.
  711. | yyerrlab -- here on detecting error. |
  712. `--------------------------------------*/
  713. yyerrlab:
  714. // If not already recovering from an error, report this error.
  715. if (!yyerrstatus_)
  716. {
  717. ++yynerrs_;
  718. error (]b4_join(b4_locations_if([yyla.location]),
  719. [[yysyntax_error_ (yystack_[0].state,
  720. yyempty ? yyempty_ : yyla.type_get ())]])[);
  721. }
  722. ]b4_locations_if([[
  723. yyerror_range[1].location = yyla.location;]])[
  724. if (yyerrstatus_ == 3)
  725. {
  726. /* If just tried and failed to reuse lookahead token after an
  727. error, discard it. */
  728. // Return failure if at end of input.
  729. if (yyla.type_get () == yyeof_)
  730. YYABORT;
  731. else if (!yyempty)
  732. {
  733. yy_destroy_ ("Error: discarding", yyla);
  734. yyempty = true;
  735. }
  736. }
  737. // Else will try to reuse lookahead token after shifting the error token.
  738. goto yyerrlab1;
  739. /*---------------------------------------------------.
  740. | yyerrorlab -- error raised explicitly by YYERROR. |
  741. `---------------------------------------------------*/
  742. yyerrorlab:
  743. /* Pacify compilers like GCC when the user code never invokes
  744. YYERROR and the label yyerrorlab therefore never appears in user
  745. code. */
  746. if (false)
  747. goto yyerrorlab;]b4_locations_if([[
  748. yyerror_range[1].location = yystack_[yylen - 1].location;]])b4_variant_if([[
  749. /* $$ was initialized before running the user action. */
  750. YY_SYMBOL_PRINT ("Error: discarding", yylhs);
  751. yylhs.~stack_symbol_type();]])[
  752. /* Do not reclaim the symbols of the rule whose action triggered
  753. this YYERROR. */
  754. yypop_ (yylen);
  755. yylen = 0;
  756. goto yyerrlab1;
  757. /*-------------------------------------------------------------.
  758. | yyerrlab1 -- common code for both syntax error and YYERROR. |
  759. `-------------------------------------------------------------*/
  760. yyerrlab1:
  761. yyerrstatus_ = 3; // Each real token shifted decrements this.
  762. {
  763. stack_symbol_type error_token;
  764. for (;;)
  765. {
  766. yyn = yypact_[yystack_[0].state];
  767. if (!yy_pact_value_is_default_ (yyn))
  768. {
  769. yyn += yyterror_;
  770. if (0 <= yyn && yyn <= yylast_ && yycheck_[yyn] == yyterror_)
  771. {
  772. yyn = yytable_[yyn];
  773. if (0 < yyn)
  774. break;
  775. }
  776. }
  777. // Pop the current state because it cannot handle the error token.
  778. if (yystack_.size () == 1)
  779. YYABORT;
  780. ]b4_locations_if([[
  781. yyerror_range[1].location = yystack_[0].location;]])[
  782. yy_destroy_ ("Error: popping", yystack_[0]);
  783. yypop_ ();
  784. YY_STACK_PRINT ();
  785. }
  786. ]b4_locations_if([[
  787. yyerror_range[2].location = yyla.location;
  788. YYLLOC_DEFAULT (error_token.location, yyerror_range, 2);]])[
  789. // Shift the error token.
  790. error_token.state = yyn;
  791. yypush_ ("Shifting", error_token);
  792. }
  793. goto yynewstate;
  794. // Accept.
  795. yyacceptlab:
  796. yyresult = 0;
  797. goto yyreturn;
  798. // Abort.
  799. yyabortlab:
  800. yyresult = 1;
  801. goto yyreturn;
  802. yyreturn:
  803. if (!yyempty)
  804. yy_destroy_ ("Cleanup: discarding lookahead", yyla);
  805. /* Do not reclaim the symbols of the rule whose action triggered
  806. this YYABORT or YYACCEPT. */
  807. yypop_ (yylen);
  808. while (1 < yystack_.size ())
  809. {
  810. yy_destroy_ ("Cleanup: popping", yystack_[0]);
  811. yypop_ ();
  812. }
  813. return yyresult;
  814. }
  815. catch (...)
  816. {
  817. YYCDEBUG << "Exception caught: cleaning lookahead and stack"
  818. << std::endl;
  819. // Do not try to display the values of the reclaimed symbols,
  820. // as their printer might throw an exception.
  821. if (!yyempty)
  822. yy_destroy_ (YY_NULL, yyla);
  823. while (1 < yystack_.size ())
  824. {
  825. yy_destroy_ (YY_NULL, yystack_[0]);
  826. yypop_ ();
  827. }
  828. throw;
  829. }
  830. }
  831. void
  832. ]b4_parser_class_name[::error (const syntax_error& yyexc)
  833. {
  834. error (]b4_join(b4_locations_if([yyexc.location]),
  835. [[yyexc.what()]])[);
  836. }
  837. // Generate an error message.
  838. std::string
  839. ]b4_parser_class_name[::yysyntax_error_ (]dnl
  840. b4_error_verbose_if([state_type yystate, symbol_number_type yytoken],
  841. [state_type, symbol_number_type])[) const
  842. {]b4_error_verbose_if([[
  843. std::string yyres;
  844. // Number of reported tokens (one for the "unexpected", one per
  845. // "expected").
  846. size_t yycount = 0;
  847. // Its maximum.
  848. enum { YYERROR_VERBOSE_ARGS_MAXIMUM = 5 };
  849. // Arguments of yyformat.
  850. char const *yyarg[YYERROR_VERBOSE_ARGS_MAXIMUM];
  851. /* There are many possibilities here to consider:
  852. - If this state is a consistent state with a default action, then
  853. the only way this function was invoked is if the default action
  854. is an error action. In that case, don't check for expected
  855. tokens because there are none.
  856. - The only way there can be no lookahead present (in yytoken) is
  857. if this state is a consistent state with a default action.
  858. Thus, detecting the absence of a lookahead is sufficient to
  859. determine that there is no unexpected or expected token to
  860. report. In that case, just report a simple "syntax error".
  861. - Don't assume there isn't a lookahead just because this state is
  862. a consistent state with a default action. There might have
  863. been a previous inconsistent state, consistent state with a
  864. non-default action, or user semantic action that manipulated
  865. yyla. (However, yyla is currently not documented for users.)
  866. - Of course, the expected token list depends on states to have
  867. correct lookahead information, and it depends on the parser not
  868. to perform extra reductions after fetching a lookahead from the
  869. scanner and before detecting a syntax error. Thus, state
  870. merging (from LALR or IELR) and default reductions corrupt the
  871. expected token list. However, the list is correct for
  872. canonical LR with one exception: it will still contain any
  873. token that will not be accepted due to an error action in a
  874. later state.
  875. */
  876. if (yytoken != yyempty_)
  877. {
  878. yyarg[yycount++] = yytname_[yytoken];
  879. int yyn = yypact_[yystate];
  880. if (!yy_pact_value_is_default_ (yyn))
  881. {
  882. /* Start YYX at -YYN if negative to avoid negative indexes in
  883. YYCHECK. In other words, skip the first -YYN actions for
  884. this state because they are default actions. */
  885. int yyxbegin = yyn < 0 ? -yyn : 0;
  886. // Stay within bounds of both yycheck and yytname.
  887. int yychecklim = yylast_ - yyn + 1;
  888. int yyxend = yychecklim < yyntokens_ ? yychecklim : yyntokens_;
  889. for (int yyx = yyxbegin; yyx < yyxend; ++yyx)
  890. if (yycheck_[yyx + yyn] == yyx && yyx != yyterror_
  891. && !yy_table_value_is_error_ (yytable_[yyx + yyn]))
  892. {
  893. if (yycount == YYERROR_VERBOSE_ARGS_MAXIMUM)
  894. {
  895. yycount = 1;
  896. break;
  897. }
  898. else
  899. yyarg[yycount++] = yytname_[yyx];
  900. }
  901. }
  902. }
  903. char const* yyformat = YY_NULL;
  904. switch (yycount)
  905. {
  906. #define YYCASE_(N, S) \
  907. case N: \
  908. yyformat = S; \
  909. break
  910. YYCASE_(0, YY_("syntax error"));
  911. YYCASE_(1, YY_("syntax error, unexpected %s"));
  912. YYCASE_(2, YY_("syntax error, unexpected %s, expecting %s"));
  913. YYCASE_(3, YY_("syntax error, unexpected %s, expecting %s or %s"));
  914. YYCASE_(4, YY_("syntax error, unexpected %s, expecting %s or %s or %s"));
  915. YYCASE_(5, YY_("syntax error, unexpected %s, expecting %s or %s or %s or %s"));
  916. #undef YYCASE_
  917. }
  918. // Argument number.
  919. size_t yyi = 0;
  920. for (char const* yyp = yyformat; *yyp; ++yyp)
  921. if (yyp[0] == '%' && yyp[1] == 's' && yyi < yycount)
  922. {
  923. yyres += yytnamerr_ (yyarg[yyi++]);
  924. ++yyp;
  925. }
  926. else
  927. yyres += *yyp;
  928. return yyres;]], [[
  929. return YY_("syntax error");]])[
  930. }
  931. const ]b4_int_type(b4_pact_ninf, b4_pact_ninf) b4_parser_class_name::yypact_ninf_ = b4_pact_ninf[;
  932. const ]b4_int_type(b4_table_ninf, b4_table_ninf) b4_parser_class_name::yytable_ninf_ = b4_table_ninf[;
  933. ]b4_parser_tables_define[
  934. ]b4_token_table_if([], [[#if ]b4_api_PREFIX[DEBUG]])[
  935. // YYTNAME[SYMBOL-NUM] -- String name of the symbol SYMBOL-NUM.
  936. // First, the terminals, then, starting at \a yyntokens_, nonterminals.
  937. const char*
  938. const ]b4_parser_class_name[::yytname_[] =
  939. {
  940. ]b4_tname[
  941. };
  942. ]b4_token_table_if([[#if ]b4_api_PREFIX[DEBUG]])[
  943. ]b4_integral_parser_table_define([rline], [b4_rline])[
  944. // Print the state stack on the debug stream.
  945. void
  946. ]b4_parser_class_name[::yystack_print_ ()
  947. {
  948. *yycdebug_ << "Stack now";
  949. for (stack_type::const_iterator
  950. i = yystack_.begin (),
  951. i_end = yystack_.end ();
  952. i != i_end; ++i)
  953. *yycdebug_ << ' ' << i->state;
  954. *yycdebug_ << std::endl;
  955. }
  956. // Report on the debug stream that the rule \a yyrule is going to be reduced.
  957. void
  958. ]b4_parser_class_name[::yy_reduce_print_ (int yyrule)
  959. {
  960. unsigned int yylno = yyrline_[yyrule];
  961. int yynrhs = yyr2_[yyrule];
  962. // Print the symbols being reduced, and their result.
  963. *yycdebug_ << "Reducing stack by rule " << yyrule - 1
  964. << " (line " << yylno << "):" << std::endl;
  965. // The symbols being reduced.
  966. for (int yyi = 0; yyi < yynrhs; yyi++)
  967. YY_SYMBOL_PRINT (" $" << yyi + 1 << " =",
  968. ]b4_rhs_data(yynrhs, yyi + 1)[);
  969. }
  970. #endif // ]b4_api_PREFIX[DEBUG
  971. ]b4_token_ctor_if([], [b4_yytranslate_define])[
  972. ]b4_namespace_close[
  973. ]b4_epilogue[]dnl
  974. b4_output_end()
  975. m4_popdef([b4_copyright_years])dnl