lalr1.cc 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621
  1. # C++ skeleton for Bison
  2. # Copyright (C) 2002-2015, 2018-2021 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 <https://www.gnu.org/licenses/>.
  15. m4_include(b4_skeletonsdir/[c++.m4])
  16. # api.value.type=variant is valid.
  17. m4_define([b4_value_type_setup_variant])
  18. # Check the value of %define parse.lac, where LAC stands for lookahead
  19. # correction.
  20. b4_percent_define_default([[parse.lac]], [[none]])
  21. b4_percent_define_check_values([[[[parse.lac]], [[full]], [[none]]]])
  22. b4_define_flag_if([lac])
  23. m4_define([b4_lac_flag],
  24. [m4_if(b4_percent_define_get([[parse.lac]]),
  25. [none], [[0]], [[1]])])
  26. # b4_tname_if(TNAME-NEEDED, TNAME-NOT-NEEDED)
  27. # -------------------------------------------
  28. m4_define([b4_tname_if],
  29. [m4_case(b4_percent_define_get([[parse.error]]),
  30. [verbose], [$1],
  31. [b4_token_table_if([$1],
  32. [$2])])])
  33. # b4_integral_parser_table_declare(TABLE-NAME, CONTENT, COMMENT)
  34. # --------------------------------------------------------------
  35. # Declare "parser::yy<TABLE-NAME>_" whose contents is CONTENT.
  36. m4_define([b4_integral_parser_table_declare],
  37. [m4_ifval([$3], [b4_comment([$3], [ ])
  38. ])dnl
  39. static const b4_int_type_for([$2]) yy$1_[[]];dnl
  40. ])
  41. # b4_integral_parser_table_define(TABLE-NAME, CONTENT, COMMENT)
  42. # -------------------------------------------------------------
  43. # Define "parser::yy<TABLE-NAME>_" whose contents is CONTENT.
  44. m4_define([b4_integral_parser_table_define],
  45. [ const b4_int_type_for([$2])
  46. b4_parser_class::yy$1_[[]] =
  47. {
  48. $2
  49. };dnl
  50. ])
  51. # b4_symbol_kind(NUM)
  52. # -------------------
  53. m4_define([b4_symbol_kind],
  54. [symbol_kind::b4_symbol_kind_base($@)])
  55. # b4_symbol_value_template(VAL, SYMBOL-NUM, [TYPE])
  56. # -------------------------------------------------
  57. # Same as b4_symbol_value, but used in a template method. It makes
  58. # a difference when using variants. Note that b4_value_type_setup_union
  59. # overrides b4_symbol_value, so we must override it again.
  60. m4_copy([b4_symbol_value], [b4_symbol_value_template])
  61. m4_append([b4_value_type_setup_union],
  62. [m4_copy_force([b4_symbol_value_union], [b4_symbol_value_template])])
  63. # b4_lhs_value(SYMBOL-NUM, [TYPE])
  64. # --------------------------------
  65. # See README.
  66. m4_define([b4_lhs_value],
  67. [b4_symbol_value([yylhs.value], [$1], [$2])])
  68. # b4_lhs_location()
  69. # -----------------
  70. # Expansion of @$.
  71. m4_define([b4_lhs_location],
  72. [yylhs.location])
  73. # b4_rhs_data(RULE-LENGTH, POS)
  74. # -----------------------------
  75. # See README.
  76. m4_define([b4_rhs_data],
  77. [yystack_@{b4_subtract($@)@}])
  78. # b4_rhs_state(RULE-LENGTH, POS)
  79. # ------------------------------
  80. # The state corresponding to the symbol #POS, where the current
  81. # rule has RULE-LENGTH symbols on RHS.
  82. m4_define([b4_rhs_state],
  83. [b4_rhs_data([$1], [$2]).state])
  84. # b4_rhs_value(RULE-LENGTH, POS, SYMBOL-NUM, [TYPE])
  85. # --------------------------------------------------
  86. # See README.
  87. m4_define([_b4_rhs_value],
  88. [b4_symbol_value([b4_rhs_data([$1], [$2]).value], [$3], [$4])])
  89. m4_define([b4_rhs_value],
  90. [b4_percent_define_ifdef([api.value.automove],
  91. [YY_MOVE (_b4_rhs_value($@))],
  92. [_b4_rhs_value($@)])])
  93. # b4_rhs_location(RULE-LENGTH, POS)
  94. # ---------------------------------
  95. # Expansion of @POS, where the current rule has RULE-LENGTH symbols
  96. # on RHS.
  97. m4_define([b4_rhs_location],
  98. [b4_rhs_data([$1], [$2]).location])
  99. # b4_symbol_action(SYMBOL-NUM, KIND)
  100. # ----------------------------------
  101. # Run the action KIND (destructor or printer) for SYMBOL-NUM.
  102. # Same as in C, but using references instead of pointers.
  103. m4_define([b4_symbol_action],
  104. [b4_symbol_if([$1], [has_$2],
  105. [m4_pushdef([b4_symbol_value], m4_defn([b4_symbol_value_template]))[]dnl
  106. b4_dollar_pushdef([yysym.value],
  107. [$1],
  108. [],
  109. [yysym.location])dnl
  110. _b4_symbol_case([$1])[]dnl
  111. b4_syncline([b4_symbol([$1], [$2_line])], [b4_symbol([$1], [$2_file])])dnl
  112. b4_symbol([$1], [$2])
  113. b4_syncline([@oline@], [@ofile@])dnl
  114. break;
  115. m4_popdef([b4_symbol_value])[]dnl
  116. b4_dollar_popdef[]dnl
  117. ])])
  118. # b4_lex
  119. # ------
  120. # Call yylex.
  121. m4_define([b4_lex],
  122. [b4_token_ctor_if(
  123. [b4_function_call([yylex],
  124. [symbol_type], m4_ifdef([b4_lex_param], b4_lex_param))],
  125. [b4_function_call([yylex], [int],
  126. [b4_api_PREFIX[STYPE*], [&yyla.value]][]dnl
  127. b4_locations_if([, [[location*], [&yyla.location]]])dnl
  128. m4_ifdef([b4_lex_param], [, ]b4_lex_param))])])
  129. m4_pushdef([b4_copyright_years],
  130. [2002-2015, 2018-2021])
  131. m4_define([b4_parser_class],
  132. [b4_percent_define_get([[api.parser.class]])])
  133. b4_bison_locations_if([# Backward compatibility.
  134. m4_define([b4_location_constructors])
  135. m4_include(b4_skeletonsdir/[location.cc])])
  136. m4_include(b4_skeletonsdir/[stack.hh])
  137. b4_variant_if([m4_include(b4_skeletonsdir/[variant.hh])])
  138. # b4_shared_declarations(hh|cc)
  139. # -----------------------------
  140. # Declaration that might either go into the header (if --defines, $1 = hh)
  141. # or in the implementation file.
  142. m4_define([b4_shared_declarations],
  143. [b4_percent_code_get([[requires]])[
  144. ]b4_parse_assert_if([# include <cassert>])[
  145. # include <cstdlib> // std::abort
  146. # include <iostream>
  147. # include <stdexcept>
  148. # include <string>
  149. # include <vector>
  150. ]b4_cxx_portability[
  151. ]m4_ifdef([b4_location_include],
  152. [[# include ]b4_location_include])[
  153. ]b4_variant_if([b4_variant_includes])[
  154. ]b4_attribute_define[
  155. ]b4_cast_define[
  156. ]b4_null_define[
  157. ]b4_YYDEBUG_define[
  158. ]b4_namespace_open[
  159. ]b4_bison_locations_if([m4_ifndef([b4_location_file],
  160. [b4_location_define])])[
  161. /// A Bison parser.
  162. class ]b4_parser_class[
  163. {
  164. public:
  165. ]b4_public_types_declare[
  166. ]b4_symbol_type_define[
  167. /// Build a parser object.
  168. ]b4_parser_class[ (]b4_parse_param_decl[);
  169. virtual ~]b4_parser_class[ ();
  170. #if 201103L <= YY_CPLUSPLUS
  171. /// Non copyable.
  172. ]b4_parser_class[ (const ]b4_parser_class[&) = delete;
  173. /// Non copyable.
  174. ]b4_parser_class[& operator= (const ]b4_parser_class[&) = delete;
  175. #endif
  176. /// Parse. An alias for parse ().
  177. /// \returns 0 iff parsing succeeded.
  178. int operator() ();
  179. /// Parse.
  180. /// \returns 0 iff parsing succeeded.
  181. virtual int parse ();
  182. #if ]b4_api_PREFIX[DEBUG
  183. /// The current debugging stream.
  184. std::ostream& debug_stream () const YY_ATTRIBUTE_PURE;
  185. /// Set the current debugging stream.
  186. void set_debug_stream (std::ostream &);
  187. /// Type for debugging levels.
  188. typedef int debug_level_type;
  189. /// The current debugging level.
  190. debug_level_type debug_level () const YY_ATTRIBUTE_PURE;
  191. /// Set the current debugging level.
  192. void set_debug_level (debug_level_type l);
  193. #endif
  194. /// Report a syntax error.]b4_locations_if([[
  195. /// \param loc where the syntax error is found.]])[
  196. /// \param msg a description of the syntax error.
  197. virtual void error (]b4_locations_if([[const location_type& loc, ]])[const std::string& msg);
  198. /// Report a syntax error.
  199. void error (const syntax_error& err);
  200. ]b4_parse_error_bmatch(
  201. [custom\|detailed],
  202. [[ /// The user-facing name of the symbol whose (internal) number is
  203. /// YYSYMBOL. No bounds checking.
  204. static const char *symbol_name (symbol_kind_type yysymbol);]],
  205. [simple],
  206. [[#if ]b4_api_PREFIX[DEBUG || ]b4_token_table_flag[
  207. /// The user-facing name of the symbol whose (internal) number is
  208. /// YYSYMBOL. No bounds checking.
  209. static const char *symbol_name (symbol_kind_type yysymbol);
  210. #endif // #if ]b4_api_PREFIX[DEBUG || ]b4_token_table_flag[
  211. ]],
  212. [verbose],
  213. [[ /// The user-facing name of the symbol whose (internal) number is
  214. /// YYSYMBOL. No bounds checking.
  215. static std::string symbol_name (symbol_kind_type yysymbol);]])[
  216. ]b4_token_constructor_define[
  217. ]b4_parse_error_bmatch([custom\|detailed\|verbose], [[
  218. class context
  219. {
  220. public:
  221. context (const ]b4_parser_class[& yyparser, const symbol_type& yyla);
  222. const symbol_type& lookahead () const YY_NOEXCEPT { return yyla_; }
  223. symbol_kind_type token () const YY_NOEXCEPT { return yyla_.kind (); }]b4_locations_if([[
  224. const location_type& location () const YY_NOEXCEPT { return yyla_.location; }
  225. ]])[
  226. /// Put in YYARG at most YYARGN of the expected tokens, and return the
  227. /// number of tokens stored in YYARG. If YYARG is null, return the
  228. /// number of expected tokens (guaranteed to be less than YYNTOKENS).
  229. int expected_tokens (symbol_kind_type yyarg[], int yyargn) const;
  230. private:
  231. const ]b4_parser_class[& yyparser_;
  232. const symbol_type& yyla_;
  233. };
  234. ]])[
  235. private:
  236. #if YY_CPLUSPLUS < 201103L
  237. /// Non copyable.
  238. ]b4_parser_class[ (const ]b4_parser_class[&);
  239. /// Non copyable.
  240. ]b4_parser_class[& operator= (const ]b4_parser_class[&);
  241. #endif
  242. ]b4_lac_if([[
  243. /// Check the lookahead yytoken.
  244. /// \returns true iff the token will be eventually shifted.
  245. bool yy_lac_check_ (symbol_kind_type yytoken) const;
  246. /// Establish the initial context if no initial context currently exists.
  247. /// \returns true iff the token will be eventually shifted.
  248. bool yy_lac_establish_ (symbol_kind_type yytoken);
  249. /// Discard any previous initial lookahead context because of event.
  250. /// \param event the event which caused the lookahead to be discarded.
  251. /// Only used for debbuging output.
  252. void yy_lac_discard_ (const char* event);]])[
  253. /// Stored state numbers (used for stacks).
  254. typedef ]b4_int_type(0, m4_eval(b4_states_number - 1))[ state_type;
  255. ]b4_parse_error_bmatch(
  256. [custom], [[
  257. /// Report a syntax error
  258. /// \param yyctx the context in which the error occurred.
  259. void report_syntax_error (const context& yyctx) const;]],
  260. [detailed\|verbose], [[
  261. /// The arguments of the error message.
  262. int yy_syntax_error_arguments_ (const context& yyctx,
  263. symbol_kind_type yyarg[], int yyargn) const;
  264. /// Generate an error message.
  265. /// \param yyctx the context in which the error occurred.
  266. virtual std::string yysyntax_error_ (const context& yyctx) const;]])[
  267. /// Compute post-reduction state.
  268. /// \param yystate the current state
  269. /// \param yysym the nonterminal to push on the stack
  270. static state_type yy_lr_goto_state_ (state_type yystate, int yysym);
  271. /// Whether the given \c yypact_ value indicates a defaulted state.
  272. /// \param yyvalue the value to check
  273. static bool yy_pact_value_is_default_ (int yyvalue);
  274. /// Whether the given \c yytable_ value indicates a syntax error.
  275. /// \param yyvalue the value to check
  276. static bool yy_table_value_is_error_ (int yyvalue);
  277. static const ]b4_int_type(b4_pact_ninf, b4_pact_ninf)[ yypact_ninf_;
  278. static const ]b4_int_type(b4_table_ninf, b4_table_ninf)[ yytable_ninf_;
  279. /// Convert a scanner token kind \a t to a symbol kind.
  280. /// In theory \a t should be a token_kind_type, but character literals
  281. /// are valid, yet not members of the token_type enum.
  282. static symbol_kind_type yytranslate_ (int t);
  283. ]b4_parse_error_bmatch(
  284. [simple],
  285. [[#if ]b4_api_PREFIX[DEBUG || ]b4_token_table_flag[
  286. /// For a symbol, its name in clear.
  287. static const char* const yytname_[];
  288. #endif // #if ]b4_api_PREFIX[DEBUG || ]b4_token_table_flag[
  289. ]],
  290. [verbose],
  291. [[ /// Convert the symbol name \a n to a form suitable for a diagnostic.
  292. static std::string yytnamerr_ (const char *yystr);
  293. /// For a symbol, its name in clear.
  294. static const char* const yytname_[];
  295. ]])[
  296. // Tables.
  297. ]b4_parser_tables_declare[
  298. #if ]b4_api_PREFIX[DEBUG
  299. ]b4_integral_parser_table_declare([rline], [b4_rline],
  300. [[YYRLINE[YYN] -- Source line where rule number YYN was defined.]])[
  301. /// Report on the debug stream that the rule \a r is going to be reduced.
  302. virtual void yy_reduce_print_ (int r) const;
  303. /// Print the state stack on the debug stream.
  304. virtual void yy_stack_print_ () const;
  305. /// Debugging level.
  306. int yydebug_;
  307. /// Debug stream.
  308. std::ostream* yycdebug_;
  309. /// \brief Display a symbol kind, value and location.
  310. /// \param yyo The output stream.
  311. /// \param yysym The symbol.
  312. template <typename Base>
  313. void yy_print_ (std::ostream& yyo, const basic_symbol<Base>& yysym) const;
  314. #endif
  315. /// \brief Reclaim the memory associated to a symbol.
  316. /// \param yymsg Why this token is reclaimed.
  317. /// If null, print nothing.
  318. /// \param yysym The symbol.
  319. template <typename Base>
  320. void yy_destroy_ (const char* yymsg, basic_symbol<Base>& yysym) const;
  321. private:
  322. /// Type access provider for state based symbols.
  323. struct by_state
  324. {
  325. /// Default constructor.
  326. by_state () YY_NOEXCEPT;
  327. /// The symbol kind as needed by the constructor.
  328. typedef state_type kind_type;
  329. /// Constructor.
  330. by_state (kind_type s) YY_NOEXCEPT;
  331. /// Copy constructor.
  332. by_state (const by_state& that) YY_NOEXCEPT;
  333. /// Record that this symbol is empty.
  334. void clear () YY_NOEXCEPT;
  335. /// Steal the symbol kind from \a that.
  336. void move (by_state& that);
  337. /// The symbol kind (corresponding to \a state).
  338. /// \a ]b4_symbol(-2, kind)[ when empty.
  339. symbol_kind_type kind () const YY_NOEXCEPT;
  340. /// The state number used to denote an empty symbol.
  341. /// We use the initial state, as it does not have a value.
  342. enum { empty_state = 0 };
  343. /// The state.
  344. /// \a empty when empty.
  345. state_type state;
  346. };
  347. /// "Internal" symbol: element of the stack.
  348. struct stack_symbol_type : basic_symbol<by_state>
  349. {
  350. /// Superclass.
  351. typedef basic_symbol<by_state> super_type;
  352. /// Construct an empty symbol.
  353. stack_symbol_type ();
  354. /// Move or copy construction.
  355. stack_symbol_type (YY_RVREF (stack_symbol_type) that);
  356. /// Steal the contents from \a sym to build this.
  357. stack_symbol_type (state_type s, YY_MOVE_REF (symbol_type) sym);
  358. #if YY_CPLUSPLUS < 201103L
  359. /// Assignment, needed by push_back by some old implementations.
  360. /// Moves the contents of that.
  361. stack_symbol_type& operator= (stack_symbol_type& that);
  362. /// Assignment, needed by push_back by other implementations.
  363. /// Needed by some other old implementations.
  364. stack_symbol_type& operator= (const stack_symbol_type& that);
  365. #endif
  366. };
  367. ]b4_stack_define[
  368. /// Stack type.
  369. typedef stack<stack_symbol_type> stack_type;
  370. /// The stack.
  371. stack_type yystack_;]b4_lac_if([[
  372. /// The stack for LAC.
  373. /// Logically, the yy_lac_stack's lifetime is confined to the function
  374. /// yy_lac_check_. We just store it as a member of this class to hold
  375. /// on to the memory and to avoid frequent reallocations.
  376. /// Since yy_lac_check_ is const, this member must be mutable.
  377. mutable std::vector<state_type> yylac_stack_;
  378. /// Whether an initial LAC context was established.
  379. bool yy_lac_established_;
  380. ]])[
  381. /// Push a new state on the stack.
  382. /// \param m a debug message to display
  383. /// if null, no trace is output.
  384. /// \param sym the symbol
  385. /// \warning the contents of \a s.value is stolen.
  386. void yypush_ (const char* m, YY_MOVE_REF (stack_symbol_type) sym);
  387. /// Push a new look ahead token on the state on the stack.
  388. /// \param m a debug message to display
  389. /// if null, no trace is output.
  390. /// \param s the state
  391. /// \param sym the symbol (for its value and location).
  392. /// \warning the contents of \a sym.value is stolen.
  393. void yypush_ (const char* m, state_type s, YY_MOVE_REF (symbol_type) sym);
  394. /// Pop \a n symbols from the stack.
  395. void yypop_ (int n = 1);
  396. /// Constants.
  397. enum
  398. {
  399. yylast_ = ]b4_last[, ///< Last index in yytable_.
  400. yynnts_ = ]b4_nterms_number[, ///< Number of nonterminal symbols.
  401. yyfinal_ = ]b4_final_state_number[ ///< Termination state number.
  402. };
  403. ]b4_parse_param_vars[
  404. ]b4_percent_code_get([[yy_bison_internal_hook]])[
  405. };
  406. ]b4_token_ctor_if([b4_yytranslate_define([$1])[
  407. ]b4_public_types_define([$1])])[
  408. ]b4_namespace_close[
  409. ]b4_percent_code_get([[provides]])[
  410. ]])
  411. ## -------------- ##
  412. ## Output files. ##
  413. ## -------------- ##
  414. b4_defines_if(
  415. [b4_output_begin([b4_spec_header_file])
  416. b4_copyright([Skeleton interface for Bison LALR(1) parsers in C++])
  417. [
  418. /**
  419. ** \file ]b4_spec_mapped_header_file[
  420. ** Define the ]b4_namespace_ref[::parser class.
  421. */
  422. // C++ LALR(1) parser skeleton written by Akim Demaille.
  423. ]b4_disclaimer[
  424. ]b4_cpp_guard_open([b4_spec_mapped_header_file])[
  425. ]b4_shared_declarations(hh)[
  426. ]b4_cpp_guard_close([b4_spec_mapped_header_file])[
  427. ]b4_output_end[
  428. ]])
  429. b4_output_begin([b4_parser_file_name])[
  430. ]b4_copyright([Skeleton implementation for Bison LALR(1) parsers in C++])[
  431. ]b4_disclaimer[
  432. ]b4_percent_code_get([[top]])[]dnl
  433. m4_if(b4_prefix, [yy], [],
  434. [
  435. // Take the name prefix into account.
  436. [#]define yylex b4_prefix[]lex])[
  437. ]b4_user_pre_prologue[
  438. ]b4_defines_if([[#include "@basename(]b4_spec_header_file[@)"]],
  439. [b4_shared_declarations([cc])])[
  440. ]b4_user_post_prologue[
  441. ]b4_percent_code_get[
  442. #ifndef YY_
  443. # if defined YYENABLE_NLS && YYENABLE_NLS
  444. # if ENABLE_NLS
  445. # include <libintl.h> // FIXME: INFRINGES ON USER NAME SPACE.
  446. # define YY_(msgid) dgettext ("bison-runtime", msgid)
  447. # endif
  448. # endif
  449. # ifndef YY_
  450. # define YY_(msgid) msgid
  451. # endif
  452. #endif
  453. ]b4_has_translations_if([
  454. #ifndef N_
  455. # define N_(Msgid) Msgid
  456. #endif
  457. ])[
  458. // Whether we are compiled with exception support.
  459. #ifndef YY_EXCEPTIONS
  460. # if defined __GNUC__ && !defined __EXCEPTIONS
  461. # define YY_EXCEPTIONS 0
  462. # else
  463. # define YY_EXCEPTIONS 1
  464. # endif
  465. #endif
  466. ]b4_locations_if([dnl
  467. [#define YYRHSLOC(Rhs, K) ((Rhs)[K].location)
  468. ]b4_yylloc_default_define])[
  469. // Enable debugging if requested.
  470. #if ]b4_api_PREFIX[DEBUG
  471. // A pseudo ostream that takes yydebug_ into account.
  472. # define YYCDEBUG if (yydebug_) (*yycdebug_)
  473. # define YY_SYMBOL_PRINT(Title, Symbol) \
  474. do { \
  475. if (yydebug_) \
  476. { \
  477. *yycdebug_ << Title << ' '; \
  478. yy_print_ (*yycdebug_, Symbol); \
  479. *yycdebug_ << '\n'; \
  480. } \
  481. } while (false)
  482. # define YY_REDUCE_PRINT(Rule) \
  483. do { \
  484. if (yydebug_) \
  485. yy_reduce_print_ (Rule); \
  486. } while (false)
  487. # define YY_STACK_PRINT() \
  488. do { \
  489. if (yydebug_) \
  490. yy_stack_print_ (); \
  491. } while (false)
  492. #else // !]b4_api_PREFIX[DEBUG
  493. # define YYCDEBUG if (false) std::cerr
  494. # define YY_SYMBOL_PRINT(Title, Symbol) YY_USE (Symbol)
  495. # define YY_REDUCE_PRINT(Rule) static_cast<void> (0)
  496. # define YY_STACK_PRINT() static_cast<void> (0)
  497. #endif // !]b4_api_PREFIX[DEBUG
  498. #define yyerrok (yyerrstatus_ = 0)
  499. #define yyclearin (yyla.clear ())
  500. #define YYACCEPT goto yyacceptlab
  501. #define YYABORT goto yyabortlab
  502. #define YYERROR goto yyerrorlab
  503. #define YYRECOVERING() (!!yyerrstatus_)
  504. ]b4_namespace_open[
  505. /// Build a parser object.
  506. ]b4_parser_class::b4_parser_class[ (]b4_parse_param_decl[)
  507. #if ]b4_api_PREFIX[DEBUG
  508. : yydebug_ (false),
  509. yycdebug_ (&std::cerr)]b4_lac_if([,], [m4_ifset([b4_parse_param], [,])])[
  510. #else
  511. ]b4_lac_if([ :], [m4_ifset([b4_parse_param], [ :])])[
  512. #endif]b4_lac_if([[
  513. yy_lac_established_ (false)]m4_ifset([b4_parse_param], [,])])[]b4_parse_param_cons[
  514. {}
  515. ]b4_parser_class::~b4_parser_class[ ()
  516. {}
  517. ]b4_parser_class[::syntax_error::~syntax_error () YY_NOEXCEPT YY_NOTHROW
  518. {}
  519. /*---------------.
  520. | symbol kinds. |
  521. `---------------*/
  522. ]b4_token_ctor_if([], [b4_public_types_define([cc])])[
  523. // by_state.
  524. ]b4_parser_class[::by_state::by_state () YY_NOEXCEPT
  525. : state (empty_state)
  526. {}
  527. ]b4_parser_class[::by_state::by_state (const by_state& that) YY_NOEXCEPT
  528. : state (that.state)
  529. {}
  530. void
  531. ]b4_parser_class[::by_state::clear () YY_NOEXCEPT
  532. {
  533. state = empty_state;
  534. }
  535. void
  536. ]b4_parser_class[::by_state::move (by_state& that)
  537. {
  538. state = that.state;
  539. that.clear ();
  540. }
  541. ]b4_parser_class[::by_state::by_state (state_type s) YY_NOEXCEPT
  542. : state (s)
  543. {}
  544. ]b4_parser_class[::symbol_kind_type
  545. ]b4_parser_class[::by_state::kind () const YY_NOEXCEPT
  546. {
  547. if (state == empty_state)
  548. return ]b4_symbol(-2, kind)[;
  549. else
  550. return YY_CAST (symbol_kind_type, yystos_[+state]);
  551. }
  552. ]b4_parser_class[::stack_symbol_type::stack_symbol_type ()
  553. {}
  554. ]b4_parser_class[::stack_symbol_type::stack_symbol_type (YY_RVREF (stack_symbol_type) that)
  555. : super_type (YY_MOVE (that.state)]b4_variant_if([], [, YY_MOVE (that.value)])b4_locations_if([, YY_MOVE (that.location)])[)
  556. {]b4_variant_if([
  557. b4_symbol_variant([that.kind ()],
  558. [value], [YY_MOVE_OR_COPY], [YY_MOVE (that.value)])])[
  559. #if 201103L <= YY_CPLUSPLUS
  560. // that is emptied.
  561. that.state = empty_state;
  562. #endif
  563. }
  564. ]b4_parser_class[::stack_symbol_type::stack_symbol_type (state_type s, YY_MOVE_REF (symbol_type) that)
  565. : super_type (s]b4_variant_if([], [, YY_MOVE (that.value)])[]b4_locations_if([, YY_MOVE (that.location)])[)
  566. {]b4_variant_if([
  567. b4_symbol_variant([that.kind ()],
  568. [value], [move], [YY_MOVE (that.value)])])[
  569. // that is emptied.
  570. that.kind_ = ]b4_symbol(-2, kind)[;
  571. }
  572. #if YY_CPLUSPLUS < 201103L
  573. ]b4_parser_class[::stack_symbol_type&
  574. ]b4_parser_class[::stack_symbol_type::operator= (const stack_symbol_type& that)
  575. {
  576. state = that.state;
  577. ]b4_variant_if([b4_symbol_variant([that.kind ()],
  578. [value], [copy], [that.value])],
  579. [[value = that.value;]])[]b4_locations_if([
  580. location = that.location;])[
  581. return *this;
  582. }
  583. ]b4_parser_class[::stack_symbol_type&
  584. ]b4_parser_class[::stack_symbol_type::operator= (stack_symbol_type& that)
  585. {
  586. state = that.state;
  587. ]b4_variant_if([b4_symbol_variant([that.kind ()],
  588. [value], [move], [that.value])],
  589. [[value = that.value;]])[]b4_locations_if([
  590. location = that.location;])[
  591. // that is emptied.
  592. that.state = empty_state;
  593. return *this;
  594. }
  595. #endif
  596. template <typename Base>
  597. void
  598. ]b4_parser_class[::yy_destroy_ (const char* yymsg, basic_symbol<Base>& yysym) const
  599. {
  600. if (yymsg)
  601. YY_SYMBOL_PRINT (yymsg, yysym);]b4_variant_if([], [
  602. // User destructor.
  603. b4_symbol_actions([destructor], [yysym.kind ()])])[
  604. }
  605. #if ]b4_api_PREFIX[DEBUG
  606. template <typename Base>
  607. void
  608. ]b4_parser_class[::yy_print_ (std::ostream& yyo, const basic_symbol<Base>& yysym) const
  609. {
  610. std::ostream& yyoutput = yyo;
  611. YY_USE (yyoutput);
  612. if (yysym.empty ())
  613. yyo << "empty symbol";
  614. else
  615. {
  616. symbol_kind_type yykind = yysym.kind ();
  617. yyo << (yykind < YYNTOKENS ? "token" : "nterm")
  618. << ' ' << yysym.name () << " ("]b4_locations_if([
  619. << yysym.location << ": "])[;
  620. ]b4_symbol_actions([printer])[
  621. yyo << ')';
  622. }
  623. }
  624. #endif
  625. void
  626. ]b4_parser_class[::yypush_ (const char* m, YY_MOVE_REF (stack_symbol_type) sym)
  627. {
  628. if (m)
  629. YY_SYMBOL_PRINT (m, sym);
  630. yystack_.push (YY_MOVE (sym));
  631. }
  632. void
  633. ]b4_parser_class[::yypush_ (const char* m, state_type s, YY_MOVE_REF (symbol_type) sym)
  634. {
  635. #if 201103L <= YY_CPLUSPLUS
  636. yypush_ (m, stack_symbol_type (s, std::move (sym)));
  637. #else
  638. stack_symbol_type ss (s, sym);
  639. yypush_ (m, ss);
  640. #endif
  641. }
  642. void
  643. ]b4_parser_class[::yypop_ (int n)
  644. {
  645. yystack_.pop (n);
  646. }
  647. #if ]b4_api_PREFIX[DEBUG
  648. std::ostream&
  649. ]b4_parser_class[::debug_stream () const
  650. {
  651. return *yycdebug_;
  652. }
  653. void
  654. ]b4_parser_class[::set_debug_stream (std::ostream& o)
  655. {
  656. yycdebug_ = &o;
  657. }
  658. ]b4_parser_class[::debug_level_type
  659. ]b4_parser_class[::debug_level () const
  660. {
  661. return yydebug_;
  662. }
  663. void
  664. ]b4_parser_class[::set_debug_level (debug_level_type l)
  665. {
  666. yydebug_ = l;
  667. }
  668. #endif // ]b4_api_PREFIX[DEBUG
  669. ]b4_parser_class[::state_type
  670. ]b4_parser_class[::yy_lr_goto_state_ (state_type yystate, int yysym)
  671. {
  672. int yyr = yypgoto_[yysym - YYNTOKENS] + yystate;
  673. if (0 <= yyr && yyr <= yylast_ && yycheck_[yyr] == yystate)
  674. return yytable_[yyr];
  675. else
  676. return yydefgoto_[yysym - YYNTOKENS];
  677. }
  678. bool
  679. ]b4_parser_class[::yy_pact_value_is_default_ (int yyvalue)
  680. {
  681. return yyvalue == yypact_ninf_;
  682. }
  683. bool
  684. ]b4_parser_class[::yy_table_value_is_error_ (int yyvalue)
  685. {
  686. return yyvalue == yytable_ninf_;
  687. }
  688. int
  689. ]b4_parser_class[::operator() ()
  690. {
  691. return parse ();
  692. }
  693. int
  694. ]b4_parser_class[::parse ()
  695. {
  696. int yyn;
  697. /// Length of the RHS of the rule being reduced.
  698. int yylen = 0;
  699. // Error handling.
  700. int yynerrs_ = 0;
  701. int yyerrstatus_ = 0;
  702. /// The lookahead symbol.
  703. symbol_type yyla;]b4_locations_if([[
  704. /// The locations where the error started and ended.
  705. stack_symbol_type yyerror_range[3];]])[
  706. /// The return value of parse ().
  707. int yyresult;]b4_lac_if([[
  708. /// Discard the LAC context in case there still is one left from a
  709. /// previous invocation.
  710. yy_lac_discard_ ("init");]])[
  711. #if YY_EXCEPTIONS
  712. try
  713. #endif // YY_EXCEPTIONS
  714. {
  715. YYCDEBUG << "Starting parse\n";
  716. ]m4_ifdef([b4_initial_action], [
  717. b4_dollar_pushdef([yyla.value], [], [], [yyla.location])dnl
  718. b4_user_initial_action
  719. b4_dollar_popdef])[]dnl
  720. [ /* Initialize the stack. The initial state will be set in
  721. yynewstate, since the latter expects the semantical and the
  722. location values to have been already stored, initialize these
  723. stacks with a primary value. */
  724. yystack_.clear ();
  725. yypush_ (YY_NULLPTR, 0, YY_MOVE (yyla));
  726. /*-----------------------------------------------.
  727. | yynewstate -- push a new symbol on the stack. |
  728. `-----------------------------------------------*/
  729. yynewstate:
  730. YYCDEBUG << "Entering state " << int (yystack_[0].state) << '\n';
  731. YY_STACK_PRINT ();
  732. // Accept?
  733. if (yystack_[0].state == yyfinal_)
  734. YYACCEPT;
  735. goto yybackup;
  736. /*-----------.
  737. | yybackup. |
  738. `-----------*/
  739. yybackup:
  740. // Try to take a decision without lookahead.
  741. yyn = yypact_[+yystack_[0].state];
  742. if (yy_pact_value_is_default_ (yyn))
  743. goto yydefault;
  744. // Read a lookahead token.
  745. if (yyla.empty ())
  746. {
  747. YYCDEBUG << "Reading a token\n";
  748. #if YY_EXCEPTIONS
  749. try
  750. #endif // YY_EXCEPTIONS
  751. {]b4_token_ctor_if([[
  752. symbol_type yylookahead (]b4_lex[);
  753. yyla.move (yylookahead);]], [[
  754. yyla.kind_ = yytranslate_ (]b4_lex[);]])[
  755. }
  756. #if YY_EXCEPTIONS
  757. catch (const syntax_error& yyexc)
  758. {
  759. YYCDEBUG << "Caught exception: " << yyexc.what() << '\n';
  760. error (yyexc);
  761. goto yyerrlab1;
  762. }
  763. #endif // YY_EXCEPTIONS
  764. }
  765. YY_SYMBOL_PRINT ("Next token is", yyla);
  766. if (yyla.kind () == ]b4_symbol(1, kind)[)
  767. {
  768. // The scanner already issued an error message, process directly
  769. // to error recovery. But do not keep the error token as
  770. // lookahead, it is too special and may lead us to an endless
  771. // loop in error recovery. */
  772. yyla.kind_ = ]b4_symbol(2, kind)[;
  773. goto yyerrlab1;
  774. }
  775. /* If the proper action on seeing token YYLA.TYPE is to reduce or
  776. to detect an error, take that action. */
  777. yyn += yyla.kind ();
  778. if (yyn < 0 || yylast_ < yyn || yycheck_[yyn] != yyla.kind ())
  779. {]b4_lac_if([[
  780. if (!yy_lac_establish_ (yyla.kind ()))
  781. goto yyerrlab;]])[
  782. goto yydefault;
  783. }
  784. // Reduce or error.
  785. yyn = yytable_[yyn];
  786. if (yyn <= 0)
  787. {
  788. if (yy_table_value_is_error_ (yyn))
  789. goto yyerrlab;]b4_lac_if([[
  790. if (!yy_lac_establish_ (yyla.kind ()))
  791. goto yyerrlab;
  792. ]])[
  793. yyn = -yyn;
  794. goto yyreduce;
  795. }
  796. // Count tokens shifted since error; after three, turn off error status.
  797. if (yyerrstatus_)
  798. --yyerrstatus_;
  799. // Shift the lookahead token.
  800. yypush_ ("Shifting", state_type (yyn), YY_MOVE (yyla));]b4_lac_if([[
  801. yy_lac_discard_ ("shift");]])[
  802. goto yynewstate;
  803. /*-----------------------------------------------------------.
  804. | yydefault -- do the default action for the current state. |
  805. `-----------------------------------------------------------*/
  806. yydefault:
  807. yyn = yydefact_[+yystack_[0].state];
  808. if (yyn == 0)
  809. goto yyerrlab;
  810. goto yyreduce;
  811. /*-----------------------------.
  812. | yyreduce -- do a reduction. |
  813. `-----------------------------*/
  814. yyreduce:
  815. yylen = yyr2_[yyn];
  816. {
  817. stack_symbol_type yylhs;
  818. yylhs.state = yy_lr_goto_state_ (yystack_[yylen].state, yyr1_[yyn]);]b4_variant_if([
  819. /* Variants are always initialized to an empty instance of the
  820. correct type. The default '$$ = $1' action is NOT applied
  821. when using variants. */
  822. b4_symbol_variant([[yyr1_@{yyn@}]], [yylhs.value], [emplace])], [
  823. /* If YYLEN is nonzero, implement the default value of the
  824. action: '$$ = $1'. Otherwise, use the top of the stack.
  825. Otherwise, the following line sets YYLHS.VALUE to garbage.
  826. This behavior is undocumented and Bison users should not rely
  827. upon it. */
  828. if (yylen)
  829. yylhs.value = yystack_@{yylen - 1@}.value;
  830. else
  831. yylhs.value = yystack_@{0@}.value;])[
  832. ]b4_locations_if([dnl
  833. [
  834. // Default location.
  835. {
  836. stack_type::slice range (yystack_, yylen);
  837. YYLLOC_DEFAULT (yylhs.location, range, yylen);
  838. yyerror_range[1].location = yylhs.location;
  839. }]])[
  840. // Perform the reduction.
  841. YY_REDUCE_PRINT (yyn);
  842. #if YY_EXCEPTIONS
  843. try
  844. #endif // YY_EXCEPTIONS
  845. {
  846. switch (yyn)
  847. {
  848. ]b4_user_actions[
  849. default:
  850. break;
  851. }
  852. }
  853. #if YY_EXCEPTIONS
  854. catch (const syntax_error& yyexc)
  855. {
  856. YYCDEBUG << "Caught exception: " << yyexc.what() << '\n';
  857. error (yyexc);
  858. YYERROR;
  859. }
  860. #endif // YY_EXCEPTIONS
  861. YY_SYMBOL_PRINT ("-> $$ =", yylhs);
  862. yypop_ (yylen);
  863. yylen = 0;
  864. // Shift the result of the reduction.
  865. yypush_ (YY_NULLPTR, YY_MOVE (yylhs));
  866. }
  867. goto yynewstate;
  868. /*--------------------------------------.
  869. | yyerrlab -- here on detecting error. |
  870. `--------------------------------------*/
  871. yyerrlab:
  872. // If not already recovering from an error, report this error.
  873. if (!yyerrstatus_)
  874. {
  875. ++yynerrs_;]b4_parse_error_case(
  876. [simple], [[
  877. std::string msg = YY_("syntax error");
  878. error (]b4_join(b4_locations_if([yyla.location]), [[YY_MOVE (msg)]])[);]],
  879. [custom], [[
  880. context yyctx (*this, yyla);
  881. report_syntax_error (yyctx);]],
  882. [[
  883. context yyctx (*this, yyla);
  884. std::string msg = yysyntax_error_ (yyctx);
  885. error (]b4_join(b4_locations_if([yyla.location]), [[YY_MOVE (msg)]])[);]])[
  886. }
  887. ]b4_locations_if([[
  888. yyerror_range[1].location = yyla.location;]])[
  889. if (yyerrstatus_ == 3)
  890. {
  891. /* If just tried and failed to reuse lookahead token after an
  892. error, discard it. */
  893. // Return failure if at end of input.
  894. if (yyla.kind () == ]b4_symbol(0, kind)[)
  895. YYABORT;
  896. else if (!yyla.empty ())
  897. {
  898. yy_destroy_ ("Error: discarding", yyla);
  899. yyla.clear ();
  900. }
  901. }
  902. // Else will try to reuse lookahead token after shifting the error token.
  903. goto yyerrlab1;
  904. /*---------------------------------------------------.
  905. | yyerrorlab -- error raised explicitly by YYERROR. |
  906. `---------------------------------------------------*/
  907. yyerrorlab:
  908. /* Pacify compilers when the user code never invokes YYERROR and
  909. the label yyerrorlab therefore never appears in user code. */
  910. if (false)
  911. YYERROR;
  912. /* Do not reclaim the symbols of the rule whose action triggered
  913. this YYERROR. */
  914. yypop_ (yylen);
  915. yylen = 0;
  916. YY_STACK_PRINT ();
  917. goto yyerrlab1;
  918. /*-------------------------------------------------------------.
  919. | yyerrlab1 -- common code for both syntax error and YYERROR. |
  920. `-------------------------------------------------------------*/
  921. yyerrlab1:
  922. yyerrstatus_ = 3; // Each real token shifted decrements this.
  923. // Pop stack until we find a state that shifts the error token.
  924. for (;;)
  925. {
  926. yyn = yypact_[+yystack_[0].state];
  927. if (!yy_pact_value_is_default_ (yyn))
  928. {
  929. yyn += ]b4_symbol(1, kind)[;
  930. if (0 <= yyn && yyn <= yylast_
  931. && yycheck_[yyn] == ]b4_symbol(1, kind)[)
  932. {
  933. yyn = yytable_[yyn];
  934. if (0 < yyn)
  935. break;
  936. }
  937. }
  938. // Pop the current state because it cannot handle the error token.
  939. if (yystack_.size () == 1)
  940. YYABORT;
  941. ]b4_locations_if([[
  942. yyerror_range[1].location = yystack_[0].location;]])[
  943. yy_destroy_ ("Error: popping", yystack_[0]);
  944. yypop_ ();
  945. YY_STACK_PRINT ();
  946. }
  947. {
  948. stack_symbol_type error_token;
  949. ]b4_locations_if([[
  950. yyerror_range[2].location = yyla.location;
  951. YYLLOC_DEFAULT (error_token.location, yyerror_range, 2);]])[
  952. // Shift the error token.]b4_lac_if([[
  953. yy_lac_discard_ ("error recovery");]])[
  954. error_token.state = state_type (yyn);
  955. yypush_ ("Shifting", YY_MOVE (error_token));
  956. }
  957. goto yynewstate;
  958. /*-------------------------------------.
  959. | yyacceptlab -- YYACCEPT comes here. |
  960. `-------------------------------------*/
  961. yyacceptlab:
  962. yyresult = 0;
  963. goto yyreturn;
  964. /*-----------------------------------.
  965. | yyabortlab -- YYABORT comes here. |
  966. `-----------------------------------*/
  967. yyabortlab:
  968. yyresult = 1;
  969. goto yyreturn;
  970. /*-----------------------------------------------------.
  971. | yyreturn -- parsing is finished, return the result. |
  972. `-----------------------------------------------------*/
  973. yyreturn:
  974. if (!yyla.empty ())
  975. yy_destroy_ ("Cleanup: discarding lookahead", yyla);
  976. /* Do not reclaim the symbols of the rule whose action triggered
  977. this YYABORT or YYACCEPT. */
  978. yypop_ (yylen);
  979. YY_STACK_PRINT ();
  980. while (1 < yystack_.size ())
  981. {
  982. yy_destroy_ ("Cleanup: popping", yystack_[0]);
  983. yypop_ ();
  984. }
  985. return yyresult;
  986. }
  987. #if YY_EXCEPTIONS
  988. catch (...)
  989. {
  990. YYCDEBUG << "Exception caught: cleaning lookahead and stack\n";
  991. // Do not try to display the values of the reclaimed symbols,
  992. // as their printers might throw an exception.
  993. if (!yyla.empty ())
  994. yy_destroy_ (YY_NULLPTR, yyla);
  995. while (1 < yystack_.size ())
  996. {
  997. yy_destroy_ (YY_NULLPTR, yystack_[0]);
  998. yypop_ ();
  999. }
  1000. throw;
  1001. }
  1002. #endif // YY_EXCEPTIONS
  1003. }
  1004. void
  1005. ]b4_parser_class[::error (const syntax_error& yyexc)
  1006. {
  1007. error (]b4_join(b4_locations_if([yyexc.location]),
  1008. [[yyexc.what ()]])[);
  1009. }
  1010. ]b4_parse_error_bmatch([custom\|detailed],
  1011. [[ const char *
  1012. ]b4_parser_class[::symbol_name (symbol_kind_type yysymbol)
  1013. {
  1014. static const char *const yy_sname[] =
  1015. {
  1016. ]b4_symbol_names[
  1017. };]b4_has_translations_if([[
  1018. /* YYTRANSLATABLE[SYMBOL-NUM] -- Whether YY_SNAME[SYMBOL-NUM] is
  1019. internationalizable. */
  1020. static ]b4_int_type_for([b4_translatable])[ yytranslatable[] =
  1021. {
  1022. ]b4_translatable[
  1023. };
  1024. return (yysymbol < YYNTOKENS && yytranslatable[yysymbol]
  1025. ? _(yy_sname[yysymbol])
  1026. : yy_sname[yysymbol]);]], [[
  1027. return yy_sname[yysymbol];]])[
  1028. }
  1029. ]],
  1030. [simple],
  1031. [[#if ]b4_api_PREFIX[DEBUG || ]b4_token_table_flag[
  1032. const char *
  1033. ]b4_parser_class[::symbol_name (symbol_kind_type yysymbol)
  1034. {
  1035. return yytname_[yysymbol];
  1036. }
  1037. #endif // #if ]b4_api_PREFIX[DEBUG || ]b4_token_table_flag[
  1038. ]],
  1039. [verbose],
  1040. [[ /* Return YYSTR after stripping away unnecessary quotes and
  1041. backslashes, so that it's suitable for yyerror. The heuristic is
  1042. that double-quoting is unnecessary unless the string contains an
  1043. apostrophe, a comma, or backslash (other than backslash-backslash).
  1044. YYSTR is taken from yytname. */
  1045. std::string
  1046. ]b4_parser_class[::yytnamerr_ (const char *yystr)
  1047. {
  1048. if (*yystr == '"')
  1049. {
  1050. std::string yyr;
  1051. char const *yyp = yystr;
  1052. for (;;)
  1053. switch (*++yyp)
  1054. {
  1055. case '\'':
  1056. case ',':
  1057. goto do_not_strip_quotes;
  1058. case '\\':
  1059. if (*++yyp != '\\')
  1060. goto do_not_strip_quotes;
  1061. else
  1062. goto append;
  1063. append:
  1064. default:
  1065. yyr += *yyp;
  1066. break;
  1067. case '"':
  1068. return yyr;
  1069. }
  1070. do_not_strip_quotes: ;
  1071. }
  1072. return yystr;
  1073. }
  1074. std::string
  1075. ]b4_parser_class[::symbol_name (symbol_kind_type yysymbol)
  1076. {
  1077. return yytnamerr_ (yytname_[yysymbol]);
  1078. }
  1079. ]])[
  1080. ]b4_parse_error_bmatch([custom\|detailed\|verbose], [[
  1081. // ]b4_parser_class[::context.
  1082. ]b4_parser_class[::context::context (const ]b4_parser_class[& yyparser, const symbol_type& yyla)
  1083. : yyparser_ (yyparser)
  1084. , yyla_ (yyla)
  1085. {}
  1086. int
  1087. ]b4_parser_class[::context::expected_tokens (symbol_kind_type yyarg[], int yyargn) const
  1088. {
  1089. // Actual number of expected tokens
  1090. int yycount = 0;
  1091. ]b4_lac_if([[
  1092. #if ]b4_api_PREFIX[DEBUG
  1093. // Execute LAC once. We don't care if it is successful, we
  1094. // only do it for the sake of debugging output.
  1095. if (!yyparser_.yy_lac_established_)
  1096. yyparser_.yy_lac_check_ (yyla_.kind ());
  1097. #endif
  1098. for (int yyx = 0; yyx < YYNTOKENS; ++yyx)
  1099. {
  1100. symbol_kind_type yysym = YY_CAST (symbol_kind_type, yyx);
  1101. if (yysym != ]b4_symbol(1, kind)[
  1102. && yysym != ]b4_symbol(2, kind)[
  1103. && yyparser_.yy_lac_check_ (yysym))
  1104. {
  1105. if (!yyarg)
  1106. ++yycount;
  1107. else if (yycount == yyargn)
  1108. return 0;
  1109. else
  1110. yyarg[yycount++] = yysym;
  1111. }
  1112. }]], [[
  1113. int yyn = yypact_[+yyparser_.yystack_[0].state];
  1114. if (!yy_pact_value_is_default_ (yyn))
  1115. {
  1116. /* Start YYX at -YYN if negative to avoid negative indexes in
  1117. YYCHECK. In other words, skip the first -YYN actions for
  1118. this state because they are default actions. */
  1119. int yyxbegin = yyn < 0 ? -yyn : 0;
  1120. // Stay within bounds of both yycheck and yytname.
  1121. int yychecklim = yylast_ - yyn + 1;
  1122. int yyxend = yychecklim < YYNTOKENS ? yychecklim : YYNTOKENS;
  1123. for (int yyx = yyxbegin; yyx < yyxend; ++yyx)
  1124. if (yycheck_[yyx + yyn] == yyx && yyx != ]b4_symbol(1, kind)[
  1125. && !yy_table_value_is_error_ (yytable_[yyx + yyn]))
  1126. {
  1127. if (!yyarg)
  1128. ++yycount;
  1129. else if (yycount == yyargn)
  1130. return 0;
  1131. else
  1132. yyarg[yycount++] = YY_CAST (symbol_kind_type, yyx);
  1133. }
  1134. }
  1135. ]])[
  1136. if (yyarg && yycount == 0 && 0 < yyargn)
  1137. yyarg[0] = ]b4_symbol(-2, kind)[;
  1138. return yycount;
  1139. }
  1140. ]])b4_lac_if([[
  1141. bool
  1142. ]b4_parser_class[::yy_lac_check_ (symbol_kind_type yytoken) const
  1143. {
  1144. // Logically, the yylac_stack's lifetime is confined to this function.
  1145. // Clear it, to get rid of potential left-overs from previous call.
  1146. yylac_stack_.clear ();
  1147. // Reduce until we encounter a shift and thereby accept the token.
  1148. #if ]b4_api_PREFIX[DEBUG
  1149. YYCDEBUG << "LAC: checking lookahead " << symbol_name (yytoken) << ':';
  1150. #endif
  1151. std::ptrdiff_t lac_top = 0;
  1152. while (true)
  1153. {
  1154. state_type top_state = (yylac_stack_.empty ()
  1155. ? yystack_[lac_top].state
  1156. : yylac_stack_.back ());
  1157. int yyrule = yypact_[+top_state];
  1158. if (yy_pact_value_is_default_ (yyrule)
  1159. || (yyrule += yytoken) < 0 || yylast_ < yyrule
  1160. || yycheck_[yyrule] != yytoken)
  1161. {
  1162. // Use the default action.
  1163. yyrule = yydefact_[+top_state];
  1164. if (yyrule == 0)
  1165. {
  1166. YYCDEBUG << " Err\n";
  1167. return false;
  1168. }
  1169. }
  1170. else
  1171. {
  1172. // Use the action from yytable.
  1173. yyrule = yytable_[yyrule];
  1174. if (yy_table_value_is_error_ (yyrule))
  1175. {
  1176. YYCDEBUG << " Err\n";
  1177. return false;
  1178. }
  1179. if (0 < yyrule)
  1180. {
  1181. YYCDEBUG << " S" << yyrule << '\n';
  1182. return true;
  1183. }
  1184. yyrule = -yyrule;
  1185. }
  1186. // By now we know we have to simulate a reduce.
  1187. YYCDEBUG << " R" << yyrule - 1;
  1188. // Pop the corresponding number of values from the stack.
  1189. {
  1190. std::ptrdiff_t yylen = yyr2_[yyrule];
  1191. // First pop from the LAC stack as many tokens as possible.
  1192. std::ptrdiff_t lac_size = std::ptrdiff_t (yylac_stack_.size ());
  1193. if (yylen < lac_size)
  1194. {
  1195. yylac_stack_.resize (std::size_t (lac_size - yylen));
  1196. yylen = 0;
  1197. }
  1198. else if (lac_size)
  1199. {
  1200. yylac_stack_.clear ();
  1201. yylen -= lac_size;
  1202. }
  1203. // Only afterwards look at the main stack.
  1204. // We simulate popping elements by incrementing lac_top.
  1205. lac_top += yylen;
  1206. }
  1207. // Keep top_state in sync with the updated stack.
  1208. top_state = (yylac_stack_.empty ()
  1209. ? yystack_[lac_top].state
  1210. : yylac_stack_.back ());
  1211. // Push the resulting state of the reduction.
  1212. state_type state = yy_lr_goto_state_ (top_state, yyr1_[yyrule]);
  1213. YYCDEBUG << " G" << int (state);
  1214. yylac_stack_.push_back (state);
  1215. }
  1216. }
  1217. // Establish the initial context if no initial context currently exists.
  1218. bool
  1219. ]b4_parser_class[::yy_lac_establish_ (symbol_kind_type yytoken)
  1220. {
  1221. /* Establish the initial context for the current lookahead if no initial
  1222. context is currently established.
  1223. We define a context as a snapshot of the parser stacks. We define
  1224. the initial context for a lookahead as the context in which the
  1225. parser initially examines that lookahead in order to select a
  1226. syntactic action. Thus, if the lookahead eventually proves
  1227. syntactically unacceptable (possibly in a later context reached via a
  1228. series of reductions), the initial context can be used to determine
  1229. the exact set of tokens that would be syntactically acceptable in the
  1230. lookahead's place. Moreover, it is the context after which any
  1231. further semantic actions would be erroneous because they would be
  1232. determined by a syntactically unacceptable token.
  1233. yy_lac_establish_ should be invoked when a reduction is about to be
  1234. performed in an inconsistent state (which, for the purposes of LAC,
  1235. includes consistent states that don't know they're consistent because
  1236. their default reductions have been disabled).
  1237. For parse.lac=full, the implementation of yy_lac_establish_ is as
  1238. follows. If no initial context is currently established for the
  1239. current lookahead, then check if that lookahead can eventually be
  1240. shifted if syntactic actions continue from the current context. */
  1241. if (!yy_lac_established_)
  1242. {
  1243. #if ]b4_api_PREFIX[DEBUG
  1244. YYCDEBUG << "LAC: initial context established for "
  1245. << symbol_name (yytoken) << '\n';
  1246. #endif
  1247. yy_lac_established_ = true;
  1248. return yy_lac_check_ (yytoken);
  1249. }
  1250. return true;
  1251. }
  1252. // Discard any previous initial lookahead context.
  1253. void
  1254. ]b4_parser_class[::yy_lac_discard_ (const char* evt)
  1255. {
  1256. /* Discard any previous initial lookahead context because of Event,
  1257. which may be a lookahead change or an invalidation of the currently
  1258. established initial context for the current lookahead.
  1259. The most common example of a lookahead change is a shift. An example
  1260. of both cases is syntax error recovery. That is, a syntax error
  1261. occurs when the lookahead is syntactically erroneous for the
  1262. currently established initial context, so error recovery manipulates
  1263. the parser stacks to try to find a new initial context in which the
  1264. current lookahead is syntactically acceptable. If it fails to find
  1265. such a context, it discards the lookahead. */
  1266. if (yy_lac_established_)
  1267. {
  1268. YYCDEBUG << "LAC: initial context discarded due to "
  1269. << evt << '\n';
  1270. yy_lac_established_ = false;
  1271. }
  1272. }]])b4_parse_error_bmatch([detailed\|verbose], [[
  1273. int
  1274. ]b4_parser_class[::yy_syntax_error_arguments_ (const context& yyctx,
  1275. symbol_kind_type yyarg[], int yyargn) const
  1276. {
  1277. /* There are many possibilities here to consider:
  1278. - If this state is a consistent state with a default action, then
  1279. the only way this function was invoked is if the default action
  1280. is an error action. In that case, don't check for expected
  1281. tokens because there are none.
  1282. - The only way there can be no lookahead present (in yyla) is
  1283. if this state is a consistent state with a default action.
  1284. Thus, detecting the absence of a lookahead is sufficient to
  1285. determine that there is no unexpected or expected token to
  1286. report. In that case, just report a simple "syntax error".
  1287. - Don't assume there isn't a lookahead just because this state is
  1288. a consistent state with a default action. There might have
  1289. been a previous inconsistent state, consistent state with a
  1290. non-default action, or user semantic action that manipulated
  1291. yyla. (However, yyla is currently not documented for users.)]b4_lac_if([[
  1292. In the first two cases, it might appear that the current syntax
  1293. error should have been detected in the previous state when
  1294. yy_lac_check was invoked. However, at that time, there might
  1295. have been a different syntax error that discarded a different
  1296. initial context during error recovery, leaving behind the
  1297. current lookahead.]], [[
  1298. - Of course, the expected token list depends on states to have
  1299. correct lookahead information, and it depends on the parser not
  1300. to perform extra reductions after fetching a lookahead from the
  1301. scanner and before detecting a syntax error. Thus, state merging
  1302. (from LALR or IELR) and default reductions corrupt the expected
  1303. token list. However, the list is correct for canonical LR with
  1304. one exception: it will still contain any token that will not be
  1305. accepted due to an error action in a later state.]])[
  1306. */
  1307. if (!yyctx.lookahead ().empty ())
  1308. {
  1309. if (yyarg)
  1310. yyarg[0] = yyctx.token ();
  1311. int yyn = yyctx.expected_tokens (yyarg ? yyarg + 1 : yyarg, yyargn - 1);
  1312. return yyn + 1;
  1313. }
  1314. return 0;
  1315. }
  1316. // Generate an error message.
  1317. std::string
  1318. ]b4_parser_class[::yysyntax_error_ (const context& yyctx) const
  1319. {
  1320. // Its maximum.
  1321. enum { YYARGS_MAX = 5 };
  1322. // Arguments of yyformat.
  1323. symbol_kind_type yyarg[YYARGS_MAX];
  1324. int yycount = yy_syntax_error_arguments_ (yyctx, yyarg, YYARGS_MAX);
  1325. char const* yyformat = YY_NULLPTR;
  1326. switch (yycount)
  1327. {
  1328. #define YYCASE_(N, S) \
  1329. case N: \
  1330. yyformat = S; \
  1331. break
  1332. default: // Avoid compiler warnings.
  1333. YYCASE_ (0, YY_("syntax error"));
  1334. YYCASE_ (1, YY_("syntax error, unexpected %s"));
  1335. YYCASE_ (2, YY_("syntax error, unexpected %s, expecting %s"));
  1336. YYCASE_ (3, YY_("syntax error, unexpected %s, expecting %s or %s"));
  1337. YYCASE_ (4, YY_("syntax error, unexpected %s, expecting %s or %s or %s"));
  1338. YYCASE_ (5, YY_("syntax error, unexpected %s, expecting %s or %s or %s or %s"));
  1339. #undef YYCASE_
  1340. }
  1341. std::string yyres;
  1342. // Argument number.
  1343. std::ptrdiff_t yyi = 0;
  1344. for (char const* yyp = yyformat; *yyp; ++yyp)
  1345. if (yyp[0] == '%' && yyp[1] == 's' && yyi < yycount)
  1346. {
  1347. yyres += symbol_name (yyarg[yyi++]);
  1348. ++yyp;
  1349. }
  1350. else
  1351. yyres += *yyp;
  1352. return yyres;
  1353. }]])[
  1354. const ]b4_int_type(b4_pact_ninf, b4_pact_ninf) b4_parser_class::yypact_ninf_ = b4_pact_ninf[;
  1355. const ]b4_int_type(b4_table_ninf, b4_table_ninf) b4_parser_class::yytable_ninf_ = b4_table_ninf[;
  1356. ]b4_parser_tables_define[
  1357. ]b4_parse_error_bmatch([simple\|verbose],
  1358. [[#if ]b4_api_PREFIX[DEBUG]b4_tname_if([[ || 1]])[
  1359. // YYTNAME[SYMBOL-NUM] -- String name of the symbol SYMBOL-NUM.
  1360. // First, the terminals, then, starting at \a YYNTOKENS, nonterminals.
  1361. const char*
  1362. const ]b4_parser_class[::yytname_[] =
  1363. {
  1364. ]b4_tname[
  1365. };
  1366. #endif
  1367. ]])[
  1368. #if ]b4_api_PREFIX[DEBUG][
  1369. ]b4_integral_parser_table_define([rline], [b4_rline])[
  1370. void
  1371. ]b4_parser_class[::yy_stack_print_ () const
  1372. {
  1373. *yycdebug_ << "Stack now";
  1374. for (stack_type::const_iterator
  1375. i = yystack_.begin (),
  1376. i_end = yystack_.end ();
  1377. i != i_end; ++i)
  1378. *yycdebug_ << ' ' << int (i->state);
  1379. *yycdebug_ << '\n';
  1380. }
  1381. void
  1382. ]b4_parser_class[::yy_reduce_print_ (int yyrule) const
  1383. {
  1384. int yylno = yyrline_[yyrule];
  1385. int yynrhs = yyr2_[yyrule];
  1386. // Print the symbols being reduced, and their result.
  1387. *yycdebug_ << "Reducing stack by rule " << yyrule - 1
  1388. << " (line " << yylno << "):\n";
  1389. // The symbols being reduced.
  1390. for (int yyi = 0; yyi < yynrhs; yyi++)
  1391. YY_SYMBOL_PRINT (" $" << yyi + 1 << " =",
  1392. ]b4_rhs_data(yynrhs, yyi + 1)[);
  1393. }
  1394. #endif // ]b4_api_PREFIX[DEBUG
  1395. ]b4_token_ctor_if([], [b4_yytranslate_define([cc])])[
  1396. ]b4_namespace_close[
  1397. ]b4_epilogue[]dnl
  1398. b4_output_end
  1399. m4_popdef([b4_copyright_years])dnl