summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorAkim Demaille <akim@lrde.epita.fr>2020-04-29 07:58:51 +0200
committerAkim Demaille <akim.demaille@gmail.com>2020-05-06 08:11:52 +0200
commitd9a9b054ae1821e0f01fa876180961f5b2fa05bd (patch)
tree68c0eae1eb3ecf552d40c60b1f2b908f87c9b230 /examples
parentcb9f4cb5439adc33f877f134e87b9b2233416c17 (diff)
downloadbison-d9a9b054ae1821e0f01fa876180961f5b2fa05bd.tar.gz
all: fix the interface of yyexpected_tokens
The user gives yyexpected_tokens a limit: the max number of tokens she wants to hear about. That's because an error message that reports a bazillion of possible tokens is useless. In that case yyexpected_tokens returned 0, so the user would not know if there are too many expected tokens or none (yes, that's possible). There are several ways to tell the user in which situation she's in: - return some E2MANY, a negative value. Then it makes the pattern int argsize = yypcontext_expected_tokens (ctx, arg, ARGS_MAX); if (argsize < 0) return argsize; no longer valid, as for E2MANY (i) the user must generate the error message anyway, and (ii) she should not return E2MANY - return ARGS_MAX + 1. Then it makes it dangerous for the user, as she has to iterate update `min (ARGS_MAX, argsize)`. Returning 0 is definitely simpler and safer for the user, as it tells her "this is not an error, just generate your message without a list of expecting tokens". So let's still return 0, but set arg[0] to the empty token when the list is really empty. * data/skeletons/glr.c, data/skeletons/lalr1.cc, data/skeletons/lalr1.java * data/skeletons/yacc.c (yyexpected_tokens): Put the empty symbol first if there are no possible tokens at all. * examples/c/bistromathic/parse.y: Demonstrate how to use that.
Diffstat (limited to 'examples')
-rwxr-xr-xexamples/c/bistromathic/bistromathic.test2
-rw-r--r--examples/c/bistromathic/parse.y9
2 files changed, 7 insertions, 4 deletions
diff --git a/examples/c/bistromathic/bistromathic.test b/examples/c/bistromathic/bistromathic.test
index 3bb83b77..495146bd 100755
--- a/examples/c/bistromathic/bistromathic.test
+++ b/examples/c/bistromathic/bistromathic.test
@@ -96,7 +96,7 @@ cat >input <<EOF
EOF
run 0 '> *
> ''
-err: 1.1: syntax error: expected end of file or - or ( or exit or number or function or variable before *'
+err: 1.1: syntax error: expected end of file or - or ( or exit or number or function etc., before *'
cat >input <<EOF
1 + 2 * * 3
diff --git a/examples/c/bistromathic/parse.y b/examples/c/bistromathic/parse.y
index 38fedb46..c2ddb124 100644
--- a/examples/c/bistromathic/parse.y
+++ b/examples/c/bistromathic/parse.y
@@ -333,7 +333,7 @@ error_format_string (int argc)
case 5: return _("%@: syntax error: expected %0e or %1e or %2e or %3e before %u");
case 6: return _("%@: syntax error: expected %0e or %1e or %2e or %3e or %4e before %u");
case 7: return _("%@: syntax error: expected %0e or %1e or %2e or %3e or %4e or %5e before %u");
- case 8: return _("%@: syntax error: expected %0e or %1e or %2e or %3e or %4e or %5e or %6e before %u");
+ case 8: return _("%@: syntax error: expected %0e or %1e or %2e or %3e or %4e or %5e etc., before %u");
}
}
@@ -341,12 +341,15 @@ error_format_string (int argc)
int
yyreport_syntax_error (const yypcontext_t *ctx)
{
- enum { ARGS_MAX = 7 };
+ enum { ARGS_MAX = 6 };
yysymbol_kind_t arg[ARGS_MAX];
int argsize = yypcontext_expected_tokens (ctx, arg, ARGS_MAX);
if (argsize < 0)
return argsize;
- const char *format = error_format_string (1 + argsize);
+ const int too_many_expected_tokens = argsize == 0 && arg[0] != YYSYMBOL_YYEMPTY;
+ if (too_many_expected_tokens)
+ argsize = ARGS_MAX;
+ const char *format = error_format_string (1 + argsize + too_many_expected_tokens);
while (*format)
// %@: location.