summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorJoel E. Denny <jdenny@clemson.edu>2009-12-20 02:22:53 -0500
committerJoel E. Denny <jdenny@clemson.edu>2009-12-22 14:50:45 -0500
commitabcc7c03ccf95f3c19445330b9b6d55365c85ccc (patch)
treea0f3a0b34d961fe0444e021e692bdef9654bf502 /tests
parent62efdd2a85847c8b7559d312e7cada6ca03a2779 (diff)
downloadbison-abcc7c03ccf95f3c19445330b9b6d55365c85ccc.tar.gz
Fix handling of yychar manipulation in user semantic actions.
The problem was that yacc.c didn't always update the yychar translation afterwards. However, other skeletons appear to be fine. glr.c appears to already translate yychar before every use. lalr1.cc does not define yychar and does not document its replacement, yyla, for users. It does provide yyclearin, but that does not manipulate yyla and thus requires no translation update. In lalr1.java, yychar is out of scope during semantic actions. * NEWS (2.5): Document. * data/yacc.c (YYBACKUP): Don't bother translating yychar into yytoken here. (yyparse, yypush_parse): Instead, translate before every use of yytoken, and add comments explaining this approach. * tests/actions.at (Destroying lookahead assigned by semantic action): New test group checking that translation happens before lookahead destructor calls at parser return. Previously, incorrect destructors were called. * tests/conflicts.at (%error-verbose and consistent errors): New test group checking that translation happens at syntax error detection before the associated verbose error message and the associated lookahead destructor calls. While the destructor call is fixed by this patch, the verbose error message is currently incorrect due to another bug (see comments in test group), so this is an expected failure for now. (cherry picked from commit df222dfaf4da6becae8665d344669eefc705d7f7) Conflicts: src/parse-gram.c src/parse-gram.h
Diffstat (limited to 'tests')
-rw-r--r--tests/actions.at71
-rw-r--r--tests/conflicts.at124
2 files changed, 195 insertions, 0 deletions
diff --git a/tests/actions.at b/tests/actions.at
index dd57b080..4833a4fb 100644
--- a/tests/actions.at
+++ b/tests/actions.at
@@ -1408,3 +1408,74 @@ AT_MATCHES_CHECK([input.c], [[// TEST:Y:1 [;{}]*\n;\}$]], [[12]])
AT_MATCHES_CHECK([input.c], [[#define TEST_MACRO_N \\\n\[\]"broken\\" \$ \@ \$\$ \@\$ \[\];\\\nstring;"\}]], [[2]])
AT_CLEANUP
+
+
+## -------------------------------------------------- ##
+## Destroying lookahead assigned by semantic action. ##
+## -------------------------------------------------- ##
+
+AT_SETUP([[Destroying lookahead assigned by semantic action]])
+
+AT_DATA_GRAMMAR([input.y],
+[[
+%code {
+ #include <assert.h>
+ #include <stdio.h>
+ static void yyerror (char const *);
+ static int yylex (void);
+ #define USE(Var)
+}
+
+%destructor { fprintf (stderr, "'a' destructor\n"); } 'a'
+%destructor { fprintf (stderr, "'b' destructor\n"); } 'b'
+
+%%
+
+// In a previous version of Bison, yychar assigned by the semantic
+// action below was not translated into yytoken before the lookahead was
+// discarded and thus before its destructor (selected according to
+// yytoken) was called in order to return from yyparse. This would
+// happen even if YYACCEPT was performed in a later semantic action as
+// long as only consistent states with default reductions were visited
+// in between. However, we leave YYACCEPT in the same semantic action
+// for this test in order to show that skeletons cannot simply translate
+// immediately after every semantic action because a semantic action
+// that has set yychar might not always return normally. Instead,
+// skeletons must translate before every use of yytoken.
+start: 'a' accept { USE($1); } ;
+accept: /*empty*/ {
+ assert (yychar == YYEMPTY);
+ yychar = 'b';
+ YYACCEPT;
+} ;
+
+%%
+
+static void
+yyerror (char const *msg)
+{
+ fprintf (stderr, "%s\n", msg);
+}
+
+static int
+yylex (void)
+{
+ static char const *input = "a";
+ return *input++;
+}
+
+int
+main (void)
+{
+ return yyparse ();
+}
+]])
+
+AT_BISON_CHECK([[-o input.c input.y]])
+AT_COMPILE([[input]])
+AT_PARSER_CHECK([[./input]], [[0]], [],
+[['b' destructor
+'a' destructor
+]])
+
+AT_CLEANUP
diff --git a/tests/conflicts.at b/tests/conflicts.at
index 6fefea05..18877296 100644
--- a/tests/conflicts.at
+++ b/tests/conflicts.at
@@ -139,6 +139,130 @@ AT_CLEANUP
+## -------------------------------------- ##
+## %error-verbose and consistent errors. ##
+## -------------------------------------- ##
+
+AT_SETUP([[%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 <assert.h>
+ #include <stdio.h>
+ int yylex (void);
+ void yyerror (char const *);
+ #define USE(Var)
+}
+
+%error-verbose
+
+// The point isn't to test IELR here, but state merging happens to
+// complicate the example.
+%define lr.type ielr
+
+%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.
+// All 'a' tokens are returned by yylex, which sets yylval=1.
+%destructor {
+ if (!$$)
+ fprintf (stderr, "Wrong destructor.\n");
+} 'a'
+
+%%
+
+// The lookahead assigned by the semantic action isn't needed before
+// either error action is encountered. In a previous version of Bison,
+// this was a problem as it meant yychar was not translated into yytoken
+// before either error action. The second error action thus invoked a
+// destructor that it selected according to the incorrect yytoken. The
+// first error action would have reported an incorrect unexpected token
+// 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); } ;
+
+error-reduce:
+ 'a' 'a' consistent-reduction consistent-error 'a'
+ { USE (($1, $2, $5)); }
+| 'a' error
+ { USE ($1); }
+;
+
+consistent-reduction: /*empty*/ {
+ assert (yychar == YYEMPTY);
+ yylval = 0;
+ yychar = 'b';
+} ;
+
+consistent-error:
+ '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 ();
+}
+]])
+
+# 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]],
+ [['b']], [[none]])
+
+# Canonical LR doesn't foresee the error for 'a'!
+AT_CONSISTENT_ERRORS_CHECK([[-Dlr.default-reductions=accepting]],
+ [[$end]], [[a]])
+AT_CONSISTENT_ERRORS_CHECK([[-Flr.type=canonical-lr]], [[$end]], [[a]])
+
+m4_popdef([AT_CONSISTENT_ERRORS_CHECK])
+
+AT_CLEANUP
+
+
+
## ------------------------- ##
## Unresolved SR Conflicts. ##
## ------------------------- ##