mirror of
https://git.savannah.gnu.org/git/bison.git
synced 2026-03-09 12:23:04 +00:00
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.
This commit is contained in:
1
THANKS
1
THANKS
@@ -4,6 +4,7 @@ it is today without the invaluable help of these people:
|
|||||||
Aaro Koskinen aaro.koskinen@iki.fi
|
Aaro Koskinen aaro.koskinen@iki.fi
|
||||||
Аскар Сафин safinaskar@mail.ru
|
Аскар Сафин safinaskar@mail.ru
|
||||||
Adam Sampson ats@offog.org
|
Adam Sampson ats@offog.org
|
||||||
|
Adrian Vogelsgesang avogelsgesang@tableau.com
|
||||||
Ahcheong Lee dkcjd2000@gmail.com
|
Ahcheong Lee dkcjd2000@gmail.com
|
||||||
Airy Andre Airy.Andre@edf.fr
|
Airy Andre Airy.Andre@edf.fr
|
||||||
Akim Demaille akim@gnu.org
|
Akim Demaille akim@gnu.org
|
||||||
|
|||||||
@@ -867,19 +867,37 @@ b4_dollar_popdef[]dnl
|
|||||||
yytoken = token;]b4_locations_if([[
|
yytoken = token;]b4_locations_if([[
|
||||||
yylocation = loc;]])[
|
yylocation = loc;]])[
|
||||||
}
|
}
|
||||||
|
|
||||||
private YYStack yystack;
|
private YYStack yystack;
|
||||||
|
|
||||||
|
public int getToken ()
|
||||||
|
{
|
||||||
|
return yytoken;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Value returned by getToken when there is no token.
|
||||||
|
*/
|
||||||
|
public static final int EMPTY = ]b4_parser_class[.yyempty_;
|
||||||
|
|
||||||
private int yytoken;]b4_locations_if([[
|
private int yytoken;]b4_locations_if([[
|
||||||
public ]b4_location_type[ getLocation ()
|
public ]b4_location_type[ getLocation ()
|
||||||
{
|
{
|
||||||
return yylocation;
|
return yylocation;
|
||||||
}
|
}
|
||||||
|
|
||||||
private ]b4_location_type[ yylocation;]])[
|
private ]b4_location_type[ yylocation;]])[
|
||||||
static final int yyntokens = ]b4_parser_class[.yyntokens_;
|
static final int NTOKENS = ]b4_parser_class[.yyntokens_;
|
||||||
|
|
||||||
/* Put in YYARG at most YYARGN of the expected tokens given the
|
/* Put in YYARG at most YYARGN of the expected tokens given the
|
||||||
current YYCTX, and return the number of tokens stored in YYARG. If
|
current YYCTX, and return the number of tokens stored in YYARG. If
|
||||||
YYARG is null, return the number of expected tokens (guaranteed to
|
YYARG is null, return the number of expected tokens (guaranteed to
|
||||||
be less than YYNTOKENS). */
|
be less than YYNTOKENS). */
|
||||||
|
int yyexpectedTokens (int yyarg[], int yyargn)
|
||||||
|
{
|
||||||
|
return yyexpectedTokens (yyarg, 0, yyargn);
|
||||||
|
}
|
||||||
|
|
||||||
int yyexpectedTokens (int yyarg[], int yyoffset, int yyargn)
|
int yyexpectedTokens (int yyarg[], int yyoffset, int yyargn)
|
||||||
{
|
{
|
||||||
int yycount = yyoffset;
|
int yycount = yyoffset;
|
||||||
@@ -893,7 +911,7 @@ b4_dollar_popdef[]dnl
|
|||||||
int yyxbegin = yyn < 0 ? -yyn : 0;
|
int yyxbegin = yyn < 0 ? -yyn : 0;
|
||||||
/* Stay within bounds of both yycheck and yytname. */
|
/* Stay within bounds of both yycheck and yytname. */
|
||||||
int yychecklim = yylast_ - yyn + 1;
|
int yychecklim = yylast_ - yyn + 1;
|
||||||
int yyxend = yychecklim < yyntokens ? yychecklim : yyntokens;
|
int yyxend = yychecklim < NTOKENS ? yychecklim : NTOKENS;
|
||||||
for (int x = yyxbegin; x < yyxend; ++x)
|
for (int x = yyxbegin; x < yyxend; ++x)
|
||||||
if (yycheck_[x + yyn] == x && x != yy_error_token_
|
if (yycheck_[x + yyn] == x && x != yy_error_token_
|
||||||
&& !yyTableValueIsError (yytable_[x + yyn]))
|
&& !yyTableValueIsError (yytable_[x + yyn]))
|
||||||
|
|||||||
@@ -1198,6 +1198,10 @@ yyexpected_tokens (const yyparse_context_t *yyctx,
|
|||||||
return yypstate_expected_tokens (yyctx->yyps, yyarg, yyargn);
|
return yypstate_expected_tokens (yyctx->yyps, yyarg, yyargn);
|
||||||
}]])[
|
}]])[
|
||||||
|
|
||||||
|
static int
|
||||||
|
yysyntax_error_arguments (const yyparse_context_t *yyctx,
|
||||||
|
int yyarg[], int yyargn) YY_ATTRIBUTE_UNUSED;
|
||||||
|
|
||||||
static int
|
static int
|
||||||
yysyntax_error_arguments (const yyparse_context_t *yyctx,
|
yysyntax_error_arguments (const yyparse_context_t *yyctx,
|
||||||
int yyarg[], int yyargn)
|
int yyarg[], int yyargn)
|
||||||
@@ -1251,7 +1255,17 @@ yysyntax_error_arguments (const yyparse_context_t *yyctx,
|
|||||||
|
|
||||||
]b4_parse_error_case(
|
]b4_parse_error_case(
|
||||||
[custom],
|
[custom],
|
||||||
[b4_locations_if([[/* The location of this context. */
|
[[/* The token type of the lookahead of this context. */
|
||||||
|
static int
|
||||||
|
yyparse_context_token (const yyparse_context_t *yyctx) YY_ATTRIBUTE_UNUSED;
|
||||||
|
|
||||||
|
static int
|
||||||
|
yyparse_context_token (const yyparse_context_t *yyctx)
|
||||||
|
{
|
||||||
|
return yyctx->yytoken;
|
||||||
|
}
|
||||||
|
|
||||||
|
]b4_locations_if([[/* The location of the lookahead of this context. */
|
||||||
static YYLTYPE *
|
static YYLTYPE *
|
||||||
yyparse_context_location (const yyparse_context_t *yyctx) YY_ATTRIBUTE_UNUSED;
|
yyparse_context_location (const yyparse_context_t *yyctx) YY_ATTRIBUTE_UNUSED;
|
||||||
|
|
||||||
|
|||||||
@@ -284,23 +284,26 @@ yylex (const char **line, YYSTYPE *yylval, YYLTYPE *yylloc)
|
|||||||
int
|
int
|
||||||
yyreport_syntax_error (const yyparse_context_t *ctx)
|
yyreport_syntax_error (const yyparse_context_t *ctx)
|
||||||
{
|
{
|
||||||
enum { ARGMAX = 10 };
|
|
||||||
int res = 0;
|
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));
|
YY_LOCATION_PRINT (stderr, *yyparse_context_location (ctx));
|
||||||
fprintf (stderr, ": syntax error");
|
fprintf (stderr, ": syntax error");
|
||||||
if (n >= 0)
|
{
|
||||||
{
|
enum { TOKENMAX = 10 };
|
||||||
for (int i = 1; i < n; ++i)
|
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",
|
fprintf (stderr, "%s %s",
|
||||||
i == 1 ? ": expected" : " or", yysymbol_name (arg[i]));
|
i == 0 ? ": expected" : " or", yysymbol_name (expected[i]));
|
||||||
if (n)
|
}
|
||||||
fprintf (stderr, " before %s", yysymbol_name (arg[0]));
|
{
|
||||||
}
|
int lookahead = yyparse_context_token (ctx);
|
||||||
|
if (lookahead != YYEMPTY)
|
||||||
|
fprintf (stderr, " before %s", yysymbol_name (lookahead));
|
||||||
|
}
|
||||||
fprintf (stderr, "\n");
|
fprintf (stderr, "\n");
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -108,15 +108,20 @@ class CalcLexer implements Calc.Lexer {
|
|||||||
|
|
||||||
public void yyreportSyntaxError (Calc.Context ctx)
|
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");
|
System.err.print (ctx.getLocation () + ": syntax error");
|
||||||
for (int i = 1; i < n; ++i)
|
{
|
||||||
System.err.print ((i == 1 ? ": expected " : " or ")
|
final int TOKENMAX = 10;
|
||||||
+ ctx.yysymbolName (arg[i]));
|
int[] arg = new int[TOKENMAX];
|
||||||
if (n != 0)
|
int n = ctx.yyexpectedTokens (arg, TOKENMAX);
|
||||||
System.err.print (" before " + ctx.yysymbolName (arg[0]));
|
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 ("");
|
System.err.println ("");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -964,8 +964,8 @@ m4_define([AT_YYERROR_DEFINE(java)],
|
|||||||
]AT_ERROR_CUSTOM_IF([[
|
]AT_ERROR_CUSTOM_IF([[
|
||||||
public void yyreportSyntaxError (Calc.Context ctx)
|
public void yyreportSyntaxError (Calc.Context ctx)
|
||||||
{
|
{
|
||||||
int[] arg = new int[ctx.yyntokens];
|
int[] arg = new int[ctx.NTOKENS];
|
||||||
int n = ctx.yysyntaxErrorArguments (arg, ctx.yyntokens);
|
int n = ctx.yysyntaxErrorArguments (arg, ctx.NTOKENS);
|
||||||
System.err.print (]AT_LOCATION_IF([[ctx.getLocation () + ": "]]
|
System.err.print (]AT_LOCATION_IF([[ctx.getLocation () + ": "]]
|
||||||
+ )["syntax error on token @<:@" + ctx.yysymbolName (arg[0]) + "@:>@");
|
+ )["syntax error on token @<:@" + ctx.yysymbolName (arg[0]) + "@:>@");
|
||||||
if (1 < n)
|
if (1 < n)
|
||||||
|
|||||||
Reference in New Issue
Block a user