From 3b8378825ea8c3d2cae8c615a9ebf1f679730895 Mon Sep 17 00:00:00 2001 From: "Joel E. Denny" Date: Sun, 7 Nov 2010 16:10:09 -0500 Subject: [PATCH] yysyntax_error: adjust prior fixes for branch-2.5's lalr1.cc. On master, there is no yychar in lalr1.cc, but there is on branch-2.5, and the prior cherry-pick of "Fix handling of yychar manipulation in user semantic actions" wasn't adjusted for that difference. * data/lalr1.cc (yy::parser::parse): Translate yychar before every use of yytoken, and add comments explaining this approach. * tests/conflicts.at (%error-verbose and consistent errors): Extend to test yychar manipulation with lalr1.cc. --- ChangeLog | 12 ++++++++++++ data/lalr1.cc | 23 ++++++++++++++++++++++- tests/conflicts.at | 9 +++++++-- 3 files changed, 41 insertions(+), 3 deletions(-) diff --git a/ChangeLog b/ChangeLog index 0e711e5e..1d80e92d 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,15 @@ +2010-11-07 Joel E. Denny + + yysyntax_error: adjust prior fixes for branch-2.5's lalr1.cc. + On master, there is no yychar in lalr1.cc, but there is on + branch-2.5, and the prior cherry-pick of "Fix handling of yychar + manipulation in user semantic actions" wasn't adjusted for that + difference. + * data/lalr1.cc (yy::parser::parse): Translate yychar before + every use of yytoken, and add comments explaining this approach. + * tests/conflicts.at (%error-verbose and consistent errors): + Extend to test yychar manipulation with lalr1.cc. + 2010-11-07 Joel E. Denny yysyntax_error: fix for consistent error with lookahead. diff --git a/data/lalr1.cc b/data/lalr1.cc index 33ab08ee..8c91fb68 100644 --- a/data/lalr1.cc +++ b/data/lalr1.cc @@ -692,6 +692,17 @@ m4_ifdef([b4_lex_param], [, ]b4_lex_param))[; 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. + One alternative is translating here after every semantic action, + but that translation would be missed if the semantic action + invokes YYABORT, YYACCEPT, or YYERROR immediately after altering + yychar. In the case of YYABORT or YYACCEPT, an incorrect + destructor might then be invoked immediately. In the case of + YYERROR, subsequent parser actions might lead to an incorrect + destructor call or verbose syntax error message before the + lookahead is translated. */ YY_SYMBOL_PRINT ("-> $$ =", yyr1_[yyn], &yyval, &yyloc); yypop_ (yylen); @@ -715,6 +726,10 @@ m4_ifdef([b4_lex_param], [, ]b4_lex_param))[; | yyerrlab -- here on detecting error | `------------------------------------*/ yyerrlab: + /* Make sure we have latest lookahead translation. See comments at + user semantic actions for why this is necessary. */ + yytoken = yytranslate_ (yychar); + /* If not already recovering from an error, report this error. */ if (!yyerrstatus_) { @@ -826,7 +841,13 @@ m4_ifdef([b4_lex_param], [, ]b4_lex_param))[; yyreturn: if (yychar != yyempty_) - yydestruct_ ("Cleanup: discarding lookahead", yytoken, &yylval, &yylloc); + { + /* 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); + } /* Do not reclaim the symbols of the rule which action triggered this YYABORT or YYACCEPT. */ diff --git a/tests/conflicts.at b/tests/conflicts.at index 1d4eb04e..f455d1f5 100644 --- a/tests/conflicts.at +++ b/tests/conflicts.at @@ -161,6 +161,7 @@ AT_SKEL_JAVA_IF([AT_DATA], [AT_DATA_GRAMMAR])([input.y], }]], [[ %code {]AT_SKEL_CC_IF([[ + #include #include ]], [[ #include #include @@ -376,7 +377,7 @@ error-reduce: ; consistent-reduction: /*empty*/ { - assert (yychar == YYEMPTY); + assert (yychar == ]AT_SKEL_CC_IF([[yyempty_]], [[YYEMPTY]])[); yylval = 0; yychar = 'b'; } ; @@ -400,7 +401,11 @@ AT_CONSISTENT_ERRORS_CHECK([[%glr-parser]], [AT_USER_ACTION_GRAMMAR], [AT_USER_ACTION_INPUT], [['b']], [[none]]) -# No C++ or Java test because yychar cannot be manipulated by users. +AT_CONSISTENT_ERRORS_CHECK([[%language "c++"]], + [AT_USER_ACTION_GRAMMAR], + [AT_USER_ACTION_INPUT], + [['b']], [[none]]) +# No Java test because yychar cannot be manipulated by users. AT_CONSISTENT_ERRORS_CHECK([[%define lr.default-reductions consistent]], [AT_USER_ACTION_GRAMMAR], -- 2.47.2