From 25a648d8a6a00bb810844e375d0d15125ed7eaf8 Mon Sep 17 00:00:00 2001 From: "Joel E. Denny" Date: Sun, 7 Nov 2010 09:45:18 -0500 Subject: [PATCH] yysyntax_error: more preparation for readability of next patch. There are no behavioral changes here. * data/glr.c (yyreportSyntaxError): Reorganize. * data/lalr1.cc (yy::parser::yysyntax_error_): Reorganize. * tests/conflicts.at (parse.error=verbose and consistent errors): Reorganize. --- ChangeLog | 9 ++++ data/glr.c | 11 +++-- data/lalr1.cc | 10 ++-- tests/conflicts.at | 119 +++++++++++++++++++++++++-------------------- 4 files changed, 87 insertions(+), 62 deletions(-) diff --git a/ChangeLog b/ChangeLog index 76163f20..019c2384 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,12 @@ +2010-11-07 Joel E. Denny + + yysyntax_error: more preparation for readability of next patch. + There are no behavioral changes here. + * data/glr.c (yyreportSyntaxError): Reorganize. + * data/lalr1.cc (yy::parser::yysyntax_error_): Reorganize. + * tests/conflicts.at (parse.error=verbose and consistent errors): + Reorganize. + 2010-11-07 Joel E. Denny yysyntax_error: prepare for readability of next patches. diff --git a/data/glr.c b/data/glr.c index f6c51c35..93ed5128 100644 --- a/data/glr.c +++ b/data/glr.c @@ -2076,9 +2076,11 @@ yyprocessOneStack (yyGLRStack* yystackp, size_t yyk, /*ARGSUSED*/ static void yyreportSyntaxError (yyGLRStack* yystackp]b4_user_formals[) { - if (yystackp->yyerrState == 0) - { -#if YYERROR_VERBOSE + if (yystackp->yyerrState != 0) + return; +#if ! YYERROR_VERBOSE + yyerror (]b4_lyyerror_args[YY_("syntax error")); +#else int yyn; yyn = yypact[yystackp->yytops.yystates[0]->yylrState]; if (YYPACT_NINF < yyn && yyn <= YYLAST) @@ -2175,11 +2177,10 @@ if (YYPACT_NINF < yyn && yyn <= YYLAST) } } else -#endif /* YYERROR_VERBOSE */ yyerror (]b4_lyyerror_args[YY_("syntax error")); +#endif /* YYERROR_VERBOSE */ yynerrs += 1; } -} /* Recover from a syntax error on *YYSTACKP, assuming that *YYSTACKP->YYTOKENP, yylval, and yylloc are the syntactic category, semantic value, and location diff --git a/data/lalr1.cc b/data/lalr1.cc index d1d88d04..a3b22320 100644 --- a/data/lalr1.cc +++ b/data/lalr1.cc @@ -977,8 +977,8 @@ m4_ifdef([b4_lex_param], [, ]b4_lex_param)));])[ ]b4_parser_class_name[::yysyntax_error_ (]dnl b4_error_verbose_if([state_type yystate, int yytoken], [int, int])[) - { - std::string yyres;]b4_error_verbose_if([[ + {]b4_error_verbose_if([[ + std::string yyres; int yyn = yypact_[yystate]; if (yypact_ninf_ < yyn && yyn <= yylast_) { @@ -1038,9 +1038,9 @@ b4_error_verbose_if([state_type yystate, int yytoken], yyres += *yyp; } else - ]])dnl -[ yyres = YY_("syntax error"); - return yyres; + yyres = YY_("syntax error"); + return yyres;]], [[ + return YY_("syntax error");]])[ } diff --git a/tests/conflicts.at b/tests/conflicts.at index ffcbb777..72f56963 100644 --- a/tests/conflicts.at +++ b/tests/conflicts.at @@ -147,21 +147,6 @@ AT_SETUP([[parse.error=verbose and consistent errors]]) m4_pushdef([AT_CONSISTENT_ERRORS_CHECK], [ -AT_BISON_CHECK([$1[ -o input.c input.y]]) -AT_COMPILE([[input]]) - -m4_pushdef([AT_EXPECTING], [m4_if($3, [ab], [[, expecting 'a' or 'b']], - $3, [a], [[, expecting 'a']], - $3, [b], [[, expecting 'b']])]) - -AT_PARSER_CHECK([[./input]], [[1]], [], -[[syntax error, unexpected ]$2[]AT_EXPECTING[ -]]) - -m4_popdef([AT_EXPECTING]) - -]) - AT_DATA_GRAMMAR([input.y], [[%code { #include @@ -171,13 +156,53 @@ AT_DATA_GRAMMAR([input.y], #define USE(Var) } +]$1[ + %define parse.error verbose -// The point isn't to test IELR here, but state merging happens to -// complicate the example. -%define lr.type ielr +%% + +]$2[ + +%% + +int +yylex (void) +{ + static char const *input = "]$3["; + yylval = 1; + return *input++; +} + +void +yyerror (char const *msg) +{ + fprintf (stderr, "%s\n", msg); +} + +int +main (void) +{ + return yyparse (); +} +]]) +AT_BISON_CHECK([[-o input.c input.y]]) +AT_COMPILE([[input]]) + +m4_pushdef([AT_EXPECTING], [m4_if($5, [ab], [[, expecting 'a' or 'b']], + $5, [a], [[, expecting 'a']], + $5, [b], [[, expecting 'b']])]) -%nonassoc 'a' +AT_PARSER_CHECK([[./input]], [[1]], [[]], +[[syntax error, unexpected ]$4[]AT_EXPECTING[ +]]) + +m4_popdef([AT_EXPECTING]) + +]) + +m4_pushdef([AT_USER_ACTION_GRAMMAR], +[[%nonassoc 'a'; // If yylval=0 here, then we know that the 'a' destructor is being // invoked incorrectly for the 'b' set in the semantic action below. @@ -185,9 +210,7 @@ AT_DATA_GRAMMAR([input.y], %destructor { if (!$$) fprintf (stderr, "Wrong destructor.\n"); -} 'a' - -%% +} 'a'; // The lookahead assigned by the semantic action isn't needed before // either error action is encountered. In a previous version of Bison, @@ -198,13 +221,13 @@ AT_DATA_GRAMMAR([input.y], // except that, due to another bug, the unexpected token is not reported // at all because the error action is the default action in a consistent // state. That bug still needs to be fixed. -start: error-reduce consistent-error 'a' { USE ($3); } ; +start: error-reduce consistent-error 'a' { USE ($][3); } ; error-reduce: 'a' 'a' consistent-reduction consistent-error 'a' - { USE (($1, $2, $5)); } + { USE (($][1, $][2, $][5)); } | 'a' error - { USE ($1); } + { USE ($][1); } ; consistent-reduction: /*empty*/ { @@ -214,48 +237,40 @@ consistent-reduction: /*empty*/ { } ; consistent-error: - 'a' { USE ($1); } + 'a' { USE ($][1); } | /*empty*/ %prec 'a' ; // Provide another context in which all rules are useful so that this // test case looks a little more realistic. start: 'b' consistent-error 'b' ; - -%% - -int -yylex (void) -{ - static char const *input = "aa"; - yylval = 1; - return *input++; -} - -void -yyerror (char const *msg) -{ - fprintf (stderr, "%s\n", msg); -} - -int -main (void) -{ - return yyparse (); -} ]]) +m4_pushdef([AT_USER_ACTION_INPUT], [[aa]]) # See comments in grammar for why this test doesn't succeed. AT_XFAIL_IF([[:]]) -AT_CONSISTENT_ERRORS_CHECK([], [['b']], [[none]]) -AT_CONSISTENT_ERRORS_CHECK([[-Dlr.default-reductions=consistent]], +AT_CONSISTENT_ERRORS_CHECK([[]], + [AT_USER_ACTION_GRAMMAR], + [AT_USER_ACTION_INPUT], + [['b']], [[none]]) +AT_CONSISTENT_ERRORS_CHECK([[%define lr.default-reductions consistent]], + [AT_USER_ACTION_GRAMMAR], + [AT_USER_ACTION_INPUT], [['b']], [[none]]) # Canonical LR doesn't foresee the error for 'a'! -AT_CONSISTENT_ERRORS_CHECK([[-Dlr.default-reductions=accepting]], +AT_CONSISTENT_ERRORS_CHECK([[%define lr.default-reductions accepting]], + [AT_USER_ACTION_GRAMMAR], + [AT_USER_ACTION_INPUT], [[$end]], [[a]]) -AT_CONSISTENT_ERRORS_CHECK([[-Flr.type=canonical-lr]], [[$end]], [[a]]) +AT_CONSISTENT_ERRORS_CHECK([[%define lr.type canonical-lr]], + [AT_USER_ACTION_GRAMMAR], + [AT_USER_ACTION_INPUT], + [[$end]], [[a]]) + +m4_popdef([AT_USER_ACTION_GRAMMAR]) +m4_popdef([AT_USER_ACTION_INPUT]) m4_popdef([AT_CONSISTENT_ERRORS_CHECK]) -- 2.45.2