From d1ff7a7cc62900b658ee4bb8cb2a2c6a1b0a2bea Mon Sep 17 00:00:00 2001 From: Akim Demaille Date: Wed, 16 Nov 2005 14:20:48 +0000 Subject: [PATCH] Generalize the display of semantic values and locations in traces. * data/glr.c (yy_reduce_print): Fix indices (again). * data/c++.m4 (b4_rhs_value, b4_rhs_location): Don't expect literal integers. * data/lalr1.cc (yyreduce_print): Rename as... (yy_reduce_print): this. Display values and locations. * data/yacc.c (yy_reduce_print): Likewise. (YY_REDUCE_PRINT): Adjust to pass the required arguments. (yysymprint): Move higher to be visible from yy_reduce_print). (yyparse): Adjust. * tests/calc.at: Adjust the expected length of the traces. --- ChangeLog | 15 +++++++++++++ data/c++.m4 | 4 ++-- data/glr.c | 4 ++-- data/lalr1.cc | 25 ++++++++++++---------- data/yacc.c | 58 ++++++++++++++++++++++++++++++--------------------- tests/calc.at | 16 +++++++------- 6 files changed, 75 insertions(+), 47 deletions(-) diff --git a/ChangeLog b/ChangeLog index 9827aaac..b0fd56f8 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,18 @@ +2005-11-16 Akim Demaille + + Generalize the display of semantic values and locations in traces. + * data/glr.c (yy_reduce_print): Fix indices (again). + * data/c++.m4 (b4_rhs_value, b4_rhs_location): Don't expect + literal integers. + * data/lalr1.cc (yyreduce_print): Rename as... + (yy_reduce_print): this. + Display values and locations. + * data/yacc.c (yy_reduce_print): Likewise. + (YY_REDUCE_PRINT): Adjust to pass the required arguments. + (yysymprint): Move higher to be visible from yy_reduce_print). + (yyparse): Adjust. + * tests/calc.at: Adjust the expected length of the traces. + 2005-11-14 Akim Demaille * data/glr.c (yy_reduce_print): The loop was quite wrong: type are diff --git a/data/c++.m4 b/data/c++.m4 index f3926f7b..cf5e83d6 100644 --- a/data/c++.m4 +++ b/data/c++.m4 @@ -61,7 +61,7 @@ m4_define([b4_lhs_value], # Expansion of $NUM, where the current rule has RULE-LENGTH # symbols on RHS. m4_define([b4_rhs_value], -[(yysemantic_stack_@{m4_eval([$1 - $2])@}m4_ifval([$3], [.$3]))]) +[(yysemantic_stack_@{($1) - ($2)@}m4_ifval([$3], [.$3]))]) # b4_lhs_location() # ----------------- @@ -75,7 +75,7 @@ m4_define([b4_lhs_location], # Expansion of @NUM, where the current rule has RULE-LENGTH symbols # on RHS. m4_define([b4_rhs_location], -[(yylocation_stack_@{m4_eval([$1 - $2])@})]) +[(yylocation_stack_@{($1) - ($2)@})]) # b4_parse_param_decl diff --git a/data/glr.c b/data/glr.c index d770b439..4a286ad3 100644 --- a/data/glr.c +++ b/data/glr.c @@ -1387,8 +1387,8 @@ yy_reduce_print (yyGLRStack* yystack, size_t yyk, yyRuleNum yyrule, { fprintf (stderr, " $%d = ", yyi + 1); yysymprint (stderr, yyrhs[yyprhs[yyrule] + yyi], - &]b4_rhs_value(yynrhs, yyi - 1)[ - ]b4_location_if([, &]b4_rhs_location(yynrhs, yyi - 1))[]dnl + &]b4_rhs_value(yynrhs, yyi + 1)[ + ]b4_location_if([, &]b4_rhs_location(yynrhs, yyi + 1))[]dnl b4_user_args[); fprintf (stderr, "\n"); } diff --git a/data/lalr1.cc b/data/lalr1.cc index 1073e8d2..bcc6080f 100644 --- a/data/lalr1.cc +++ b/data/lalr1.cc @@ -232,7 +232,7 @@ b4_error_verbose_if([, int tok])[); /// For each scanner token number, its symbol number. static const ]b4_int_type_for([b4_toknum])[ yytoken_number_[]; /// Report on the debug stream that the rule \a r is going to be reduced. - virtual void yyreduce_print_ (int r); + virtual void yy_reduce_print_ (int r); /// Print the state stack on the debug stream. virtual void yystack_print_ (); #endif @@ -328,7 +328,7 @@ b4_syncline([@oline@], [@ofile@])[ do { \ if (yydebug_) \ { \ - *yycdebug_ << (Title) << ' '; \ + *yycdebug_ << Title << ' '; \ yysymprint_ ((Type), (Value), (Location)); \ *yycdebug_ << std::endl; \ } \ @@ -337,7 +337,7 @@ do { \ # define YY_REDUCE_PRINT(Rule) \ do { \ if (yydebug_) \ - yyreduce_print_ (Rule); \ + yy_reduce_print_ (Rule); \ } while (false) # define YY_STACK_PRINT() \ @@ -655,9 +655,9 @@ m4_ifdef([b4_lex_param], [, ]b4_lex_param))[; ]b4_actions[ default: break; } - - ]/* Line __line__ of lalr1.cc. */ - b4_syncline([@oline@], [@ofile@])[ + YY_SYMBOL_PRINT ("-> $$ =", yyr1_[yyn], &yyval, &yyloc); + /* Line __line__ of lalr1.cc. */ +]b4_syncline([@oline@], [@ofile@])[ yypop_ (yylen); @@ -981,16 +981,19 @@ b4_error_verbose_if([, int tok])[) // Report on the debug stream that the rule \a yyrule is going to be reduced. void - ]b4_parser_class_name[::yyreduce_print_ (int yyrule) + ]b4_parser_class_name[::yy_reduce_print_ (int yyrule) { unsigned int yylno = yyrline_[yyrule]; + int yynrhs = yyr2_[yyrule]; /* Print the symbols being reduced, and their result. */ *yycdebug_ << "Reducing stack by rule " << yyrule - 1 << " (line " << yylno << "), "; - for (]b4_int_type_for([b4_prhs])[ i = yyprhs_[yyrule]; - 0 <= yyrhs_[i]; ++i) - *yycdebug_ << yytname_[yyrhs_[i]] << ' '; - *yycdebug_ << "-> " << yytname_[yyr1_[yyrule]] << std::endl; + /* The symbols being reduced. */ + for (int yyi = 0; yyi < yynrhs; yyi++) + YY_SYMBOL_PRINT (" $" << yyi + 1 << " =", + yyrhs_[yyprhs_[yyrule] + yyi], + &]b4_rhs_value(yynrhs, yyi + 1)[, + &]b4_rhs_location(yynrhs, yyi + 1)[); } #endif // YYDEBUG diff --git a/data/yacc.c b/data/yacc.c index 7af81941..9bc021fa 100644 --- a/data/yacc.c +++ b/data/yacc.c @@ -102,7 +102,7 @@ m4_define([b4_lhs_value], # Expansion of $NUM, where the current rule has RULE-LENGTH # symbols on RHS. m4_define([b4_rhs_value], -[(yyvsp@{m4_eval([$2 - $1])@}m4_ifval([$3], [.$3]))]) +[(yyvsp@{($2) - ($1)@}m4_ifval([$3], [.$3]))]) @@ -122,7 +122,7 @@ m4_define([b4_lhs_location], # Expansion of @NUM, where the current rule has RULE-LENGTH symbols # on RHS. m4_define([b4_rhs_location], -[(yylsp@{m4_eval([$2 - $1])@})]) +[(yylsp@{($2) - ($1)@})]) @@ -639,6 +639,8 @@ do { \ } \ } while (/*CONSTCOND*/ 0) +]b4_yysymprint_generate([b4_c_function_def])[ + /*------------------------------------------------------------------. | yy_stack_print -- Print the state stack from its BOTTOM up to its | | TOP (included). | @@ -666,22 +668,32 @@ do { \ `------------------------------------------------*/ ]b4_c_function_def([yy_reduce_print], [static void], - [[int yyrule], [yyrule]])[ + [[YYSTYPE *yyvsp], [yyvsvp]], + b4_location_if([[[YYLTYPE *yylsp], [yyvlvp]],]) + [[int yyrule], [yyrule]] + b4_parse_param)[ { + int yynrhs = yyr2[yyrule]; int yyi; unsigned long int yylno = yyrline[yyrule]; - YYFPRINTF (stderr, "Reducing stack by rule %d (line %lu), ", + YYFPRINTF (stderr, "Reducing stack by rule %d (line %lu):\n", yyrule - 1, yylno); - /* Print the symbols being reduced, and their result. */ - for (yyi = yyprhs[yyrule]; 0 <= yyrhs[yyi]; yyi++) - YYFPRINTF (stderr, "%s ", yytname[yyrhs[yyi]]); - YYFPRINTF (stderr, "-> %s\n", yytname[yyr1[yyrule]]); + /* The symbols being reduced. */ + for (yyi = 0; yyi < yynrhs; yyi++) + { + fprintf (stderr, " $%d = ", yyi + 1); + yysymprint (stderr, yyrhs[yyprhs[yyrule] + yyi], + &]b4_rhs_value(yynrhs, yyi + 1)[ + ]b4_location_if([, &]b4_rhs_location(yynrhs, yyi + 1))[]dnl + b4_user_args[); + fprintf (stderr, "\n"); + } } -# define YY_REDUCE_PRINT(Rule) \ -do { \ - if (yydebug) \ - yy_reduce_print (Rule); \ +# define YY_REDUCE_PRINT(Args) \ +do { \ + if (yydebug) \ + yy_reduce_print (yyvsp, ]b4_location_if([yylsp, ])[Args); \ } while (/*CONSTCOND*/ 0) /* Nonzero means print parse trace. It is left uninitialized so that @@ -691,7 +703,7 @@ int yydebug; # define YYDPRINTF(Args) # define YY_SYMBOL_PRINT(Title, Type, Value, Location) # define YY_STACK_PRINT(Bottom, Top) -# define YY_REDUCE_PRINT(Rule) +# define YY_REDUCE_PRINT(Args) #endif /* !YYDEBUG */ @@ -922,10 +934,7 @@ yysyntax_error (char *yyresult, int yystate, int yychar) #endif /* YYERROR_VERBOSE */ -#if YYDEBUG -]b4_yysymprint_generate([b4_c_function_def])[ -#endif /* ! YYDEBUG */ -]b4_yydestruct_generate([b4_c_function_def]) +]b4_yydestruct_generate([b4_c_function_def])[ /* Prevent warnings from -Wmissing-prototypes. */ @@ -937,11 +946,11 @@ int yyparse (void *YYPARSE_PARAM); int yyparse (); # endif #else /* ! YYPARSE_PARAM */ -b4_c_function_decl([yyparse], [int], b4_parse_param) +]b4_c_function_decl([yyparse], [int], b4_parse_param)[ #endif /* ! YYPARSE_PARAM */ -m4_divert_push([KILL])# ======================== M4 code. +]m4_divert_push([KILL])# ======================== M4 code. # b4_declare_parser_variables # --------------------------- # Declare the variables that are global, or local to YYPARSE if @@ -1252,17 +1261,18 @@ yyreduce: ]b4_location_if( [[ /* Default location. */ YYLLOC_DEFAULT (yyloc, yylsp - yylen, yylen);]])[ - YY_REDUCE_PRINT (yyn); + YY_REDUCE_PRINT ((yyn]b4_user_args[)); switch (yyn) - ]{ - b4_actions + { + ]b4_actions[ default: break; } + YY_SYMBOL_PRINT ("-> $$ =", yyr1[yyn], &yyval, &yyloc); /* Line __line__ of yacc.c. */ -b4_syncline([@oline@], [@ofile@]) +]b4_syncline([@oline@], [@ofile@])[ -[ yyvsp -= yylen; + yyvsp -= yylen; yyssp -= yylen; ]b4_location_if([ yylsp -= yylen;])[ diff --git a/tests/calc.at b/tests/calc.at index ca7cc95d..217b8828 100644 --- a/tests/calc.at +++ b/tests/calc.at @@ -465,21 +465,21 @@ _AT_CHECK_CALC([$1], 2^2^3 = 256 (2^2)^3 = 64], - [571]) + [842]) # Some syntax errors. -_AT_CHECK_CALC_ERROR([$1], [1], [0 0], [13], +_AT_CHECK_CALC_ERROR([$1], [1], [0 0], [15], [1.2: syntax error, unexpected number]) -_AT_CHECK_CALC_ERROR([$1], [1], [1//2], [18], +_AT_CHECK_CALC_ERROR([$1], [1], [1//2], [20], [1.2: syntax error, unexpected '/', expecting number or '-' or '(' or '!']) _AT_CHECK_CALC_ERROR([$1], [1], [error], [5], [1.0: syntax error, unexpected $undefined]) -_AT_CHECK_CALC_ERROR([$1], [1], [1 = 2 = 3], [26], +_AT_CHECK_CALC_ERROR([$1], [1], [1 = 2 = 3], [30], [1.6: syntax error, unexpected '=']) _AT_CHECK_CALC_ERROR([$1], [1], [ +1], - [16], + [20], [2.0: syntax error, unexpected '+']) # Exercise error messages with EOF: work on an empty file. _AT_CHECK_CALC_ERROR([$1], [1], [/dev/null], [4], @@ -503,7 +503,7 @@ _AT_CHECK_CALC_ERROR([$1], [1], [/dev/null], [4], # _AT_CHECK_CALC_ERROR([$1], [0], [() + (1 + 1 + 1 +) + (* * *) + (1 * 2 * *) = 1], - [189], + [250], [1.1: syntax error, unexpected ')', expecting number or '-' or '(' or '!' 1.17: syntax error, unexpected ')', expecting number or '-' or '(' or '!' 1.22: syntax error, unexpected '*', expecting number or '-' or '(' or '!' @@ -512,10 +512,10 @@ calc: error: 4444 != 1]) # The same, but this time exercising explicitly triggered syntax errors. # POSIX says the look-ahead causing the error should not be discarded. -_AT_CHECK_CALC_ERROR([$1], [0], [(!) + (0 0) = 1], [76], +_AT_CHECK_CALC_ERROR([$1], [0], [(!) + (0 0) = 1], [102], [1.9: syntax error, unexpected number calc: error: 2222 != 1]) -_AT_CHECK_CALC_ERROR([$1], [0], [(- *) + (0 0) = 1], [86], +_AT_CHECK_CALC_ERROR([$1], [0], [(- *) + (0 0) = 1], [113], [1.3: syntax error, unexpected '*', expecting number or '-' or '(' or '!' 1.11: syntax error, unexpected number calc: error: 2222 != 1]) -- 2.45.2