X-Git-Url: https://git.saurik.com/bison.git/blobdiff_plain/25a648d8a6a00bb810844e375d0d15125ed7eaf8..219139f52178b3e020be1952c59b1ba76009c386:/data/lalr1.cc diff --git a/data/lalr1.cc b/data/lalr1.cc index a3b22320..8afabc21 100644 --- a/data/lalr1.cc +++ b/data/lalr1.cc @@ -1,7 +1,6 @@ # C++ skeleton for Bison -# Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, -# 2010 Free Software Foundation, Inc. +# Copyright (C) 2002-2012 Free Software Foundation, Inc. # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by @@ -116,7 +115,7 @@ m4_popdef([b4_dollar_dollar])dnl m4_pushdef([b4_copyright_years], - [2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010]) + [2002-2012]) m4_define([b4_parser_class_name], [b4_percent_define_get([[parser_class_name]])]) @@ -337,7 +336,7 @@ b4_namespace_close])[ yyfinal_ = ]b4_final_state_number[, //< Termination state number. yyterror_ = 1, yyerrcode_ = 256, - yyntokens_ = ]b4_tokens_number[, //< Number of tokens. + yyntokens_ = ]b4_tokens_number[ //< Number of tokens. }; ]b4_parse_param_vars[ @@ -427,14 +426,14 @@ b4_percent_code_get[]dnl } \ } while (false) -# define YY_REDUCE_PRINT(Rule) \ - do { \ +# define YY_REDUCE_PRINT(Rule) \ + do { \ if (yydebug_) \ - yy_reduce_print_ (Rule); \ + yy_reduce_print_ (Rule); \ } while (false) -# define YY_STACK_PRINT() \ - do { \ +# define YY_STACK_PRINT() \ + do { \ if (yydebug_) \ yystack_print_ (); \ } while (false) @@ -576,7 +575,7 @@ b4_percent_code_get[]dnl { ]b4_symbol_foreach([b4_symbol_printer])dnl [ default: - break; + break; } yyo << ')'; } @@ -740,7 +739,7 @@ m4_popdef([b4_at_dollar])])dnl [ yyla = b4_c_function_call([yylex], [symbol_type], m4_ifdef([b4_lex_param], b4_lex_param));], [ yyla.type = yytranslate_ (b4_c_function_call([yylex], [int], - [[YYSTYPE*], [&yyla.value]][]dnl + [[YYSTYPE*], [&yyla.value]][]dnl b4_locations_if([, [[location*], [&yyla.location]]])dnl m4_ifdef([b4_lex_param], [, ]b4_lex_param)));])[ yyempty = false; @@ -757,10 +756,10 @@ m4_ifdef([b4_lex_param], [, ]b4_lex_param)));])[ yyn = yytable_[yyn]; if (yyn <= 0) { - if (yy_table_value_is_error_ (yyn)) - goto yyerrlab; - yyn = -yyn; - goto yyreduce; + if (yy_table_value_is_error_ (yyn)) + goto yyerrlab; + yyn = -yyn; + goto yyreduce; } /* Discard the token being shifted. */ @@ -819,7 +818,7 @@ m4_ifdef([b4_lex_param], [, ]b4_lex_param)));])[ switch (yyn) { ]b4_user_actions[ - default: + default: break; } } @@ -860,16 +859,17 @@ m4_ifdef([b4_lex_param], [, ]b4_lex_param)));])[ /* If not already recovering from an error, report this error. */ if (!yyerrstatus_) { - ++yynerrs_; - error (]b4_args(b4_locations_if([yyla.location]), - [[yysyntax_error_ (yystack_[0].state, yyla.type)]])[); + ++yynerrs_; + error (]b4_args(b4_locations_if([yyla.location]), + [[yysyntax_error_ (yystack_[0].state, + yyempty ? yyempty_ : yyla.type)]])[); } ]b4_locations_if([[ yyerror_range[1].location = yyla.location;]])[ if (yyerrstatus_ == 3) { - /* If just tried and failed to reuse lookahead token after an + /* If just tried and failed to reuse lookahead token after an error, discard it. */ /* Return failure if at end of input. */ @@ -879,7 +879,7 @@ m4_ifdef([b4_lex_param], [, ]b4_lex_param)));])[ { yy_destroy_ ("Error: discarding", yyla); yyempty = true; - } + } } /* Else will try to reuse lookahead token after shifting the error @@ -910,7 +910,7 @@ m4_ifdef([b4_lex_param], [, ]b4_lex_param)));])[ | yyerrlab1 -- common code for both syntax error and YYERROR. | `-------------------------------------------------------------*/ yyerrlab1: - yyerrstatus_ = 3; /* Each real token shifted decrements this. */ + yyerrstatus_ = 3; /* Each real token shifted decrements this. */ { stack_symbol_type error_token; for (;;) @@ -965,8 +965,8 @@ m4_ifdef([b4_lex_param], [, ]b4_lex_param)));])[ yypop_ (yylen); while (yystack_.size () != 1) { - yy_destroy_ ("Cleanup: popping", yystack_[0]); - yypop_ (); + yy_destroy_ ("Cleanup: popping", yystack_[0]); + yypop_ (); } return yyresult; @@ -979,26 +979,52 @@ b4_error_verbose_if([state_type yystate, int yytoken], [int, int])[) {]b4_error_verbose_if([[ std::string yyres; - int yyn = yypact_[yystate]; - if (yypact_ninf_ < yyn && yyn <= yylast_) + // Number of reported tokens (one for the "unexpected", one per + // "expected"). + size_t yycount = 0; + // Its maximum. + enum { YYERROR_VERBOSE_ARGS_MAXIMUM = 5 }; + // Arguments of yyformat. + char const *yyarg[YYERROR_VERBOSE_ARGS_MAXIMUM]; + + /* There are many possibilities here to consider: + - If this state is a consistent state with a default action, then + the only way this function was invoked is if the default action + is an error action. In that case, don't check for expected + tokens because there are none. + - The only way there can be no lookahead present (in yytoken) is + if this state is a consistent state with a default action. + Thus, detecting the absence of a lookahead is sufficient to + determine that there is no unexpected or expected token to + report. In that case, just report a simple "syntax error". + - Don't assume there isn't a lookahead just because this state is + a consistent state with a default action. There might have + been a previous inconsistent state, consistent state with a + non-default action, or user semantic action that manipulated + yyla. (However, yyla is currently not documented for users.) + - Of course, the expected token list depends on states to have + correct lookahead information, and it depends on the parser not + to perform extra reductions after fetching a lookahead from the + scanner and before detecting a syntax error. Thus, state + merging (from LALR or IELR) and default reductions corrupt the + expected token list. However, the list is correct for + canonical LR with one exception: it will still contain any + token that will not be accepted due to an error action in a + later state. + */ + if (yytoken != yyempty_) { + yyarg[yycount++] = yytname_[yytoken]; + int yyn = yypact_[yystate]; + if (!yy_pact_value_is_default_ (yyn)) + { /* Start YYX at -YYN if negative to avoid negative indexes in YYCHECK. In other words, skip the first -YYN actions for this state because they are default actions. */ int yyxbegin = yyn < 0 ? -yyn : 0; - /* Stay within bounds of both yycheck and yytname. */ int yychecklim = yylast_ - yyn + 1; int yyxend = yychecklim < yyntokens_ ? yychecklim : yyntokens_; - - // Number of reported tokens (one for the "unexpected", one per - // "expected"). - size_t yycount = 0; - // Its maximum. - enum { YYERROR_VERBOSE_ARGS_MAXIMUM = 5 }; - // Arguments of yyformat. - char const *yyarg[YYERROR_VERBOSE_ARGS_MAXIMUM]; - yyarg[yycount++] = yytname_[yytoken]; for (int yyx = yyxbegin; yyx < yyxend; ++yyx) if (yycheck_[yyx + yyn] == yyx && yyx != yyterror_ && !yy_table_value_is_error_ (yytable_[yyx + yyn])) @@ -1011,6 +1037,8 @@ b4_error_verbose_if([state_type yystate, int yytoken], else yyarg[yycount++] = yytname_[yyx]; } + } + } char const* yyformat = 0; switch (yycount) @@ -1019,6 +1047,7 @@ b4_error_verbose_if([state_type yystate, int yytoken], case N: \ yyformat = S; \ break + YYCASE_(0, YY_("syntax error")); YYCASE_(1, YY_("syntax error, unexpected %s")); YYCASE_(2, YY_("syntax error, unexpected %s, expecting %s")); YYCASE_(3, YY_("syntax error, unexpected %s, expecting %s or %s")); @@ -1026,6 +1055,7 @@ b4_error_verbose_if([state_type yystate, int yytoken], YYCASE_(5, YY_("syntax error, unexpected %s, expecting %s or %s or %s or %s")); #undef YYCASE_ } + // Argument number. size_t yyi = 0; for (char const* yyp = yyformat; *yyp; ++yyp) @@ -1036,9 +1066,6 @@ b4_error_verbose_if([state_type yystate, int yytoken], } else yyres += *yyp; - } - else - yyres = YY_("syntax error"); return yyres;]], [[ return YY_("syntax error");]])[ } @@ -1071,7 +1098,7 @@ b4_error_verbose_if([state_type yystate, int yytoken], for (stack_type::const_iterator i = yystack_.begin (), i_end = yystack_.end (); - i != i_end; ++i) + i != i_end; ++i) *yycdebug_ << ' ' << i->state; *yycdebug_ << std::endl; } @@ -1084,7 +1111,7 @@ b4_error_verbose_if([state_type yystate, int yytoken], int yynrhs = yyr2_[yyrule]; /* Print the symbols being reduced, and their result. */ *yycdebug_ << "Reducing stack by rule " << yyrule - 1 - << " (line " << yylno << "):" << std::endl; + << " (line " << yylno << "):" << std::endl; /* The symbols being reduced. */ for (int yyi = 0; yyi < yynrhs; yyi++) YY_SYMBOL_PRINT (" $" << yyi + 1 << " =",