X-Git-Url: https://git.saurik.com/bison.git/blobdiff_plain/4b3847c3c0e161b85267d077adb256a880c0f3e3..a26424642b1de2c5e237cd69a54f12b2f430321f:/data/lalr1.cc?ds=inline diff --git a/data/lalr1.cc b/data/lalr1.cc index 337af10a..2ec1d0d7 100644 --- a/data/lalr1.cc +++ b/data/lalr1.cc @@ -55,12 +55,7 @@ b4_copyright([Skeleton interface for Bison LALR(1) parsers in C++], ]b4_percent_define_ifdef([[location_type]], [], [[#include "location.hh"]])[ -]b4_null_define[ - -/* Enabling traces. */ -#ifndef YYDEBUG -# define YYDEBUG ]b4_debug_flag[ -#endif +]b4_YYDEBUG_define[ ]b4_namespace_open[ @@ -100,7 +95,7 @@ b4_user_stype /// \returns 0 iff parsing succeeded. virtual int parse (); -#if YYDEBUG +#if ]b4_api_PREFIX[DEBUG /// The current debugging stream. std::ostream& debug_stream () const; /// Set the current debugging stream. @@ -125,7 +120,7 @@ b4_user_stype /// \param tok the lookahead token. virtual std::string yysyntax_error_ (int yystate, int tok); -#if YYDEBUG +#if ]b4_api_PREFIX[DEBUG /// \brief Report a symbol value on the debug stream. /// \param yytype The token type. /// \param yyvaluep Its semantic value. @@ -203,10 +198,10 @@ b4_user_stype /// Convert the symbol name \a n to a form suitable for a diagnostic. static std::string yytnamerr_ (const char *n);])[ -]b4_token_table_if([], [[#if YYDEBUG]])[ +]b4_token_table_if([], [[#if ]b4_api_PREFIX[DEBUG]])[ /// For a symbol, its name in clear. static const char* const yytname_[]; -]b4_token_table_if([[#if YYDEBUG]])[ +]b4_token_table_if([[#if ]b4_api_PREFIX[DEBUG]])[ /// A type to store symbol numbers and -1. typedef ]b4_int_type_for([b4_rhs])[ rhs_number_type; /// A `-1'-separated list of the rules' RHS. @@ -280,16 +275,18 @@ m4_if(b4_prefix, [yy], [], #define yylex b4_prefix[]lex])[ /* First part of user declarations. */ -]b4_user_pre_prologue +]b4_user_pre_prologue[ -b4_defines_if([[ +]b4_defines_if([[ #include "@basename(]b4_spec_defines_file[@)"]])[ /* User implementation prologue. */ -]b4_user_post_prologue -b4_percent_code_get[]dnl +]b4_user_post_prologue[ +]b4_percent_code_get[ -[#ifndef YY_ +]b4_null_define[ + +#ifndef YY_ # if defined YYENABLE_NLS && YYENABLE_NLS # if ENABLE_NLS # include /* FIXME: INFRINGES ON USER NAME SPACE */ @@ -301,31 +298,14 @@ b4_percent_code_get[]dnl # endif #endif -/* YYLLOC_DEFAULT -- Set CURRENT to span from RHS[1] to RHS[N]. - If N is 0, then set CURRENT to the empty location which ends - the previous symbol: RHS[0] (always defined). */ - #define YYRHSLOC(Rhs, K) ((Rhs)[K]) -#ifndef YYLLOC_DEFAULT -# define YYLLOC_DEFAULT(Current, Rhs, N) \ - do \ - if (N) \ - { \ - (Current).begin = YYRHSLOC (Rhs, 1).begin; \ - (Current).end = YYRHSLOC (Rhs, N).end; \ - } \ - else \ - { \ - (Current).begin = (Current).end = YYRHSLOC (Rhs, 0).end; \ - } \ - while (false) -#endif +]b4_yylloc_default_define[ /* Suppress unused-variable warnings by "using" E. */ #define YYUSE(e) ((void) (e)) /* Enable debugging if requested. */ -#if YYDEBUG +#if ]b4_api_PREFIX[DEBUG /* A pseudo ostream that takes yydebug_ into account. */ # define YYCDEBUG if (yydebug_) (*yycdebug_) @@ -352,14 +332,14 @@ do { \ yystack_print_ (); \ } while (false) -#else /* !YYDEBUG */ +#else /* !]b4_api_PREFIX[DEBUG */ # define YYCDEBUG if (false) std::cerr -# define YY_SYMBOL_PRINT(Title, Type, Value, Location) -# define YY_REDUCE_PRINT(Rule) -# define YY_STACK_PRINT() +# define YY_SYMBOL_PRINT(Title, Type, Value, Location) YYUSE(Type) +# define YY_REDUCE_PRINT(Rule) static_cast(0) +# define YY_STACK_PRINT() static_cast(0) -#endif /* !YYDEBUG */ +#endif /* !]b4_api_PREFIX[DEBUG */ #define yyerrok (yyerrstatus_ = 0) #define yyclearin (yychar = yyempty_) @@ -412,7 +392,7 @@ do { \ /// Build a parser object. ]b4_parser_class_name::b4_parser_class_name[ (]b4_parse_param_decl[)]m4_ifset([b4_parse_param], [ :])[ -#if YYDEBUG +#if ]b4_api_PREFIX[DEBUG ]m4_ifset([b4_parse_param], [ ], [ :])[yydebug_ (false), yycdebug_ (&std::cerr)]m4_ifset([b4_parse_param], [,])[ #endif]b4_parse_param_cons[ @@ -423,7 +403,7 @@ do { \ { } -#if YYDEBUG +#if ]b4_api_PREFIX[DEBUG /*--------------------------------. | Print this symbol on YYOUTPUT. | `--------------------------------*/ @@ -484,7 +464,7 @@ do { \ yylocation_stack_.pop (n); } -#if YYDEBUG +#if ]b4_api_PREFIX[DEBUG std::ostream& ]b4_parser_class_name[::debug_stream () const { @@ -553,15 +533,17 @@ do { \ int yyresult; + // FIXME: This shoud be completely indented. It is not yet to + // avoid gratuitous conflicts when merging into the master branch. + try + { YYCDEBUG << "Starting parse" << std::endl; ]m4_ifdef([b4_initial_action], [ -m4_pushdef([b4_at_dollar], [yylloc])dnl -m4_pushdef([b4_dollar_dollar], [yylval])dnl - /* User initialization code. */ - b4_user_initial_action -m4_popdef([b4_dollar_dollar])dnl -m4_popdef([b4_at_dollar])])dnl +b4_dollar_pushdef([yylval], [], [yylloc])dnl +/* User initialization code. */ +b4_user_initial_action +b4_dollar_popdef])[]dnl [ /* Initialize the stacks. The initial state will be pushed in yynewstate, since the latter expects the semantical and the @@ -595,14 +577,13 @@ m4_popdef([b4_at_dollar])])dnl /* Read a lookahead token. */ if (yychar == yyempty_) { - YYCDEBUG << "Reading a token: "; - yychar = ]b4_c_function_call([yylex], [int], - [b4_api_PREFIX[STYPE*], [&yylval]][]dnl + YYCDEBUG << "Reading a token: "; + yychar = ]b4_c_function_call([yylex], [int], + [b4_api_PREFIX[STYPE*], [&yylval]][]dnl b4_locations_if([, [[location*], [&yylloc]]])dnl m4_ifdef([b4_lex_param], [, ]b4_lex_param))[; } - /* Convert token to internal form. */ if (yychar <= yyeof_) { @@ -673,17 +654,21 @@ m4_ifdef([b4_lex_param], [, ]b4_lex_param))[; else yyval = yysemantic_stack_[0]; + // Compute the default @@$. { slice slice (yylocation_stack_, yylen); YYLLOC_DEFAULT (yyloc, slice, yylen); } + + // Perform the reduction. YY_REDUCE_PRINT (yyn); switch (yyn) { - ]b4_user_actions[ - default: - break; + ]b4_user_actions[ + default: + break; } + /* User semantic actions sometimes alter yychar, and that requires that yytoken be updated with the new translation. We take the approach of translating immediately before every use of yytoken. @@ -734,20 +719,19 @@ m4_ifdef([b4_lex_param], [, ]b4_lex_param))[; yyerror_range[1] = yylloc; if (yyerrstatus_ == 3) { - /* If just tried and failed to reuse lookahead token after an - error, discard it. */ - - if (yychar <= yyeof_) - { - /* Return failure if at end of input. */ - if (yychar == yyeof_) - YYABORT; - } - else - { - yydestruct_ ("Error: discarding", yytoken, &yylval, &yylloc); - yychar = yyempty_; - } + /* If just tried and failed to reuse lookahead token after an + error, discard it. */ + if (yychar <= yyeof_) + { + /* Return failure if at end of input. */ + if (yychar == yyeof_) + YYABORT; + } + else + { + yydestruct_ ("Error: discarding", yytoken, &yylval, &yylloc); + yychar = yyempty_; + } } /* Else will try to reuse lookahead token after shifting the error @@ -796,7 +780,7 @@ m4_ifdef([b4_lex_param], [, ]b4_lex_param))[; /* Pop the current state because it cannot handle the error token. */ if (yystate_stack_.height () == 1) - YYABORT; + YYABORT; yyerror_range[1] = yylocation_stack_[0]; yydestruct_ ("Error: popping", @@ -844,16 +828,40 @@ m4_ifdef([b4_lex_param], [, ]b4_lex_param))[; /* Do not reclaim the symbols of the rule which action triggered this YYABORT or YYACCEPT. */ yypop_ (yylen); - while (yystate_stack_.height () != 1) + while (1 < yystate_stack_.height ()) { - yydestruct_ ("Cleanup: popping", - yystos_[yystate_stack_[0]], - &yysemantic_stack_[0], - &yylocation_stack_[0]); - yypop_ (); + yydestruct_ ("Cleanup: popping", + yystos_[yystate_stack_[0]], + &yysemantic_stack_[0], + &yylocation_stack_[0]); + yypop_ (); } return yyresult; + } + catch (...) + { + YYCDEBUG << "Exception caught" << std::endl; + if (yychar != yyempty_) + { + /* Make sure we have latest lookahead translation. See + comments at user semantic actions for why this is + necessary. */ + yytoken = yytranslate_ (yychar); + yydestruct_ ("Cleanup: discarding lookahead", yytoken, &yylval, + &yylloc); + } + + while (1 < yystate_stack_.height ()) + { + yydestruct_ ("Cleanup: popping", + yystos_[yystate_stack_[0]], + &yysemantic_stack_[0], + &yylocation_stack_[0]); + yypop_ (); + } + throw; + } } // Generate an error message. @@ -1012,7 +1020,7 @@ b4_error_verbose_if([int yystate, int yytoken], ]b4_stos[ }; -#if YYDEBUG +#if ]b4_api_PREFIX[DEBUG /* TOKEN_NUMBER_[YYLEX-NUM] -- Internal symbol number corresponding to YYLEX-NUM. */ const ]b4_int_type_for([b4_toknum])[ @@ -1036,7 +1044,7 @@ b4_error_verbose_if([int yystate, int yytoken], ]b4_r2[ }; -]b4_token_table_if([], [[#if YYDEBUG]])[ +]b4_token_table_if([], [[#if ]b4_api_PREFIX[DEBUG]])[ /* YYTNAME[SYMBOL-NUM] -- String name of the symbol SYMBOL-NUM. First, the terminals, then, starting at \a yyntokens_, nonterminals. */ const char* @@ -1045,7 +1053,7 @@ b4_error_verbose_if([int yystate, int yytoken], ]b4_tname[ }; -]b4_token_table_if([[#if YYDEBUG]])[ +]b4_token_table_if([[#if ]b4_api_PREFIX[DEBUG]])[ /* YYRHS -- A `-1'-separated list of the rules' RHS. */ const ]b4_parser_class_name[::rhs_number_type ]b4_parser_class_name[::yyrhs_[] = @@ -1095,7 +1103,7 @@ b4_error_verbose_if([int yystate, int yytoken], &]b4_rhs_value(yynrhs, yyi + 1)[, &]b4_rhs_location(yynrhs, yyi + 1)[); } -#endif // YYDEBUG +#endif // ]b4_api_PREFIX[DEBUG /* YYTRANSLATE(YYLEX) -- Bison symbol number corresponding to YYLEX. */ ]b4_parser_class_name[::token_number_type @@ -1126,5 +1134,5 @@ b4_error_verbose_if([int yystate, int yytoken], const ]b4_parser_class_name[::token_number_type ]b4_parser_class_name[::yyundef_token_ = ]b4_undef_token_number[; ]b4_namespace_close[ -]b4_epilogue +]b4_epilogue[]dnl m4_divert_pop(0)