summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorAkim Demaille <akim.demaille@gmail.com>2020-03-21 08:04:01 +0100
committerAkim Demaille <akim.demaille@gmail.com>2020-03-28 15:13:27 +0100
commit1045c8d0efe8d8549c67842b08ab6b3d3685838d (patch)
tree695dd3c5f113df97699c48001a56d2ecc0c81ed5 /examples
parentef8965b5f55995cfeede5f3037aff7704e19fe93 (diff)
downloadbison-1045c8d0efe8d8549c67842b08ab6b3d3685838d.tar.gz
examples: don't use yysyntax_error_arguments
Suggested by Adrian Vogelsgesang. https://lists.gnu.org/archive/html/bison-patches/2020-02/msg00069.html * data/skeletons/lalr1.java (Context.EMPTY, Context.getToken): New. (Context.yyntokens): Rename as... (Context.NTOKENS): this. Because (i) all the Java coding styles recommend upper case for constants, and (ii) the Java Skeleton exposes Lexer.EOF, not Lexer.YYEOF. * data/skeletons/yacc.c (yyparse_context_token): New. * examples/c/bistromathic/parse.y (yyreport_syntax_error): Don't use yysyntax_error_arguments. * examples/java/calc/Calc.y (yyreportSyntaxError): Likewise.
Diffstat (limited to 'examples')
-rw-r--r--examples/c/bistromathic/parse.y29
-rw-r--r--examples/java/calc/Calc.y21
2 files changed, 29 insertions, 21 deletions
diff --git a/examples/c/bistromathic/parse.y b/examples/c/bistromathic/parse.y
index 190b1cd0..819313c9 100644
--- a/examples/c/bistromathic/parse.y
+++ b/examples/c/bistromathic/parse.y
@@ -284,23 +284,26 @@ yylex (const char **line, YYSTYPE *yylval, YYLTYPE *yylloc)
int
yyreport_syntax_error (const yyparse_context_t *ctx)
{
- enum { ARGMAX = 10 };
int res = 0;
- int arg[ARGMAX];
- int n = yysyntax_error_arguments (ctx, arg, ARGMAX);
- if (n < 0)
- // Forward errors to yyparse.
- res = n;
YY_LOCATION_PRINT (stderr, *yyparse_context_location (ctx));
fprintf (stderr, ": syntax error");
- if (n >= 0)
- {
- for (int i = 1; i < n; ++i)
+ {
+ enum { TOKENMAX = 10 };
+ int expected[TOKENMAX];
+ int n = yyexpected_tokens (ctx, expected, TOKENMAX);
+ if (n < 0)
+ // Forward errors to yyparse.
+ res = n;
+ else
+ for (int i = 0; i < n; ++i)
fprintf (stderr, "%s %s",
- i == 1 ? ": expected" : " or", yysymbol_name (arg[i]));
- if (n)
- fprintf (stderr, " before %s", yysymbol_name (arg[0]));
- }
+ i == 0 ? ": expected" : " or", yysymbol_name (expected[i]));
+ }
+ {
+ int lookahead = yyparse_context_token (ctx);
+ if (lookahead != YYEMPTY)
+ fprintf (stderr, " before %s", yysymbol_name (lookahead));
+ }
fprintf (stderr, "\n");
return res;
}
diff --git a/examples/java/calc/Calc.y b/examples/java/calc/Calc.y
index f634937d..4bb01c72 100644
--- a/examples/java/calc/Calc.y
+++ b/examples/java/calc/Calc.y
@@ -108,15 +108,20 @@ class CalcLexer implements Calc.Lexer {
public void yyreportSyntaxError (Calc.Context ctx)
{
- final int ARGMAX = 10;
- int[] arg = new int[ARGMAX];
- int n = ctx.yysyntaxErrorArguments (arg, ARGMAX);
System.err.print (ctx.getLocation () + ": syntax error");
- for (int i = 1; i < n; ++i)
- System.err.print ((i == 1 ? ": expected " : " or ")
- + ctx.yysymbolName (arg[i]));
- if (n != 0)
- System.err.print (" before " + ctx.yysymbolName (arg[0]));
+ {
+ final int TOKENMAX = 10;
+ int[] arg = new int[TOKENMAX];
+ int n = ctx.yyexpectedTokens (arg, TOKENMAX);
+ for (int i = 0; i < n; ++i)
+ System.err.print ((i == 0 ? ": expected " : " or ")
+ + ctx.yysymbolName (arg[i]));
+ }
+ {
+ int lookahead = ctx.getToken ();
+ if (lookahead != ctx.EMPTY)
+ System.err.print (" before " + ctx.yysymbolName (lookahead));
+ }
System.err.println ("");
}