Test also --verbose', --defines' and `--name-prefix'. Testing

the latter demonstrates a flaw in the handling of non debugging
parsers introduced by myself on 2000-03-16: `#define yydebug 0'
was used in order to simplify:
#if YYDEBUG
if (yydebug)
{
...
}
#endif
into
if (yydebug)
{
...
}
unfortunately this leads to a CPP conflict when
`--name-prefix=foo' is used since it produces `#define yydebug
foodebug'.
* src/bison.s1 [!YYDEBUG]: Do not define yydebug.
(YYDPRINTF): New macro.
Spread its use.
* tests/calc.m4 (AT_CHECK_CALC): Do require a title, build it from
the bison options.
Also test `--verbose', `--defines' and `--name-prefix'.
This commit is contained in:
Akim Demaille
2000-10-02 09:04:40 +00:00
parent 71da9eeacc
commit 5a35a6cb71
4 changed files with 124 additions and 106 deletions

View File

@@ -1,3 +1,36 @@
2000-10-02 Akim Demaille <akim@epita.fr>
Test also `--verbose', `--defines' and `--name-prefix'. Testing
the latter demonstrates a flaw in the handling of non debugging
parsers introduced by myself on 2000-03-16: `#define yydebug 0'
was used in order to simplify:
#if YYDEBUG
if (yydebug)
{
...
}
#endif
into
if (yydebug)
{
...
}
unfortunately this leads to a CPP conflict when
`--name-prefix=foo' is used since it produces `#define yydebug
foodebug'.
* src/bison.s1 [!YYDEBUG]: Do not define yydebug.
(YYDPRINTF): New macro.
Spread its use.
* tests/calc.m4 (AT_CHECK_CALC): Do require a title, build it from
the bison options.
Also test `--verbose', `--defines' and `--name-prefix'.
2000-10-02 Akim Demaille <akim@epita.fr> 2000-10-02 Akim Demaille <akim@epita.fr>
Improve the readability of the produced parsers. Improve the readability of the produced parsers.

View File

@@ -154,28 +154,29 @@ YYLTYPE yylloc;
int yynerrs; int yynerrs;
#endif /* !YYPURE */ #endif /* !YYPURE */
/* Enable debugging if requested. */
#if YYDEBUG #if YYDEBUG
int yydebug; /* nonzero means print parse trace */ # define YYDPRINTF(Args) \
do { \
/* [The following comment makes no sense to me. Could someone if (yydebug) \
clarify it? --akim] Since this is uninitialized, it does not fprintf Args; \
stop multiple parsers from coexisting. */ } while (0)
#else /* Nonzero means print parse trace. [The following comment makes no
/* To avoid crippling this file with `#if YYDEBUG', define `yydebug' sense to me. Could someone clarify it? --akim] Since this is
as `0', so that the `if (yydebug)' be removed as dead code. */ uninitialized, it does not stop multiple parsers from coexisting.
# define yydebug 0 */
#endif int yydebug;
#else /* !YYDEBUG */
/* YYINITDEPTH indicates the initial size of the parser's stacks */ # define YYDPRINTF(Args)
#endif /* !YYDEBUG */
/* YYINITDEPTH -- initial size of the parser's stacks. */
#ifndef YYINITDEPTH #ifndef YYINITDEPTH
# define YYINITDEPTH 200 # define YYINITDEPTH 200
#endif #endif
/* YYMAXDEPTH is the maximum size the stacks can grow to /* YYMAXDEPTH -- maximum size the stacks can grow to (effective only
(effective only if the built-in stack extension method is used). */ if the built-in stack extension method is used). */
#if YYMAXDEPTH == 0 #if YYMAXDEPTH == 0
# undef YYMAXDEPTH # undef YYMAXDEPTH
#endif #endif
@@ -190,7 +191,7 @@ int yydebug; /* nonzero means print parse trace */
of type size_t, but it can handle unsigned int. */ of type size_t, but it can handle unsigned int. */
#if __GNUC__ > 1 /* GNU C and GNU C++ define this. */ #if __GNUC__ > 1 /* GNU C and GNU C++ define this. */
# define __yy_memcpy(To,From,Count) __builtin_memcpy (To,From,Count) # define __yy_memcpy(To, From, Count) __builtin_memcpy (To, From, Count)
#else /* not GNU C or C++ */ #else /* not GNU C or C++ */
# ifndef __cplusplus # ifndef __cplusplus
@@ -324,8 +325,7 @@ yyparse (YYPARSE_PARAM_ARG)
rule. */ rule. */
int yylen; int yylen;
if (yydebug) YYDPRINTF ((stderr, "Starting parse\n"));
fprintf (stderr, "Starting parse\n");
yystate = 0; yystate = 0;
yyerrstatus = 0; yyerrstatus = 0;
@@ -429,15 +429,13 @@ yynewstate:
yylsp = yyls + size - 1; yylsp = yyls + size - 1;
#endif #endif
if (yydebug) YYDPRINTF ((stderr, "Stack size increased to %d\n", yystacksize));
fprintf (stderr, "Stack size increased to %d\n", yystacksize);
if (yyssp >= yyss + yystacksize - 1) if (yyssp >= yyss + yystacksize - 1)
YYABORT; YYABORT;
} }
if (yydebug) YYDPRINTF ((stderr, "Entering state %d\n", yystate));
fprintf (stderr, "Entering state %d\n", yystate);
goto yybackup; goto yybackup;
@@ -464,8 +462,7 @@ yybackup:
if (yychar == YYEMPTY) if (yychar == YYEMPTY)
{ {
if (yydebug) YYDPRINTF ((stderr, "Reading a token: "));
fprintf (stderr, "Reading a token: ");
yychar = YYLEX; yychar = YYLEX;
} }
@@ -476,8 +473,7 @@ yybackup:
yychar1 = 0; yychar1 = 0;
yychar = YYEOF; /* Don't call YYLEX any more */ yychar = YYEOF; /* Don't call YYLEX any more */
if (yydebug) YYDPRINTF ((stderr, "Now at end of input.\n"));
fprintf (stderr, "Now at end of input.\n");
} }
else else
{ {
@@ -526,10 +522,7 @@ yybackup:
YYACCEPT; YYACCEPT;
/* Shift the lookahead token. */ /* Shift the lookahead token. */
#if YYDEBUG YYDPRINTF ((stderr, "Shifting token %d (%s), ", yychar, yytname[yychar1]));
if (yydebug)
fprintf (stderr, "Shifting token %d (%s), ", yychar, yytname[yychar1]);
#endif
/* Discard the token being shifted unless it is eof. */ /* Discard the token being shifted unless it is eof. */
if (yychar != YYEOF) if (yychar != YYEOF)
@@ -612,6 +605,7 @@ $ /* The action file replaces this line marked with this dollarsign. */
yylsp -= yylen; yylsp -= yylen;
#endif #endif
#if YYDEBUG
if (yydebug) if (yydebug)
{ {
short *ssp1 = yyss - 1; short *ssp1 = yyss - 1;
@@ -620,6 +614,7 @@ $ /* The action file replaces this line marked with this dollarsign. */
fprintf (stderr, " %d", *++ssp1); fprintf (stderr, " %d", *++ssp1);
fprintf (stderr, "\n"); fprintf (stderr, "\n");
} }
#endif
*++yyvsp = yyval; *++yyvsp = yyval;
#ifdef YYLSP_NEEDED #ifdef YYLSP_NEEDED
@@ -712,11 +707,8 @@ yyerrlab1:
/* return failure if at end of input */ /* return failure if at end of input */
if (yychar == YYEOF) if (yychar == YYEOF)
YYABORT; YYABORT;
#if YYDEBUG YYDPRINTF ((stderr, "Discarding token %d (%s).\n",
if (yydebug) yychar, yytname[yychar1]));
fprintf (stderr, "Discarding token %d (%s).\n",
yychar, yytname[yychar1]);
#endif
yychar = YYEMPTY; yychar = YYEMPTY;
} }
@@ -757,6 +749,7 @@ yyerrpop:
yylsp--; yylsp--;
#endif #endif
#if YYDEBUG
if (yydebug) if (yydebug)
{ {
short *ssp1 = yyss - 1; short *ssp1 = yyss - 1;
@@ -765,7 +758,7 @@ yyerrpop:
fprintf (stderr, " %d", *++ssp1); fprintf (stderr, " %d", *++ssp1);
fprintf (stderr, "\n"); fprintf (stderr, "\n");
} }
#endif
/*--------------. /*--------------.
| yyerrhandle. | | yyerrhandle. |
@@ -793,8 +786,7 @@ yyerrhandle:
if (yyn == YYFINAL) if (yyn == YYFINAL)
YYACCEPT; YYACCEPT;
if (yydebug) YYDPRINTF ((stderr, "Shifting error token, "));
fprintf (stderr, "Shifting error token, ");
*++yyvsp = yylval; *++yyvsp = yylval;
#ifdef YYLSP_NEEDED #ifdef YYLSP_NEEDED

View File

@@ -154,28 +154,29 @@ YYLTYPE yylloc;
int yynerrs; int yynerrs;
#endif /* !YYPURE */ #endif /* !YYPURE */
/* Enable debugging if requested. */
#if YYDEBUG #if YYDEBUG
int yydebug; /* nonzero means print parse trace */ # define YYDPRINTF(Args) \
do { \
/* [The following comment makes no sense to me. Could someone if (yydebug) \
clarify it? --akim] Since this is uninitialized, it does not fprintf Args; \
stop multiple parsers from coexisting. */ } while (0)
#else /* Nonzero means print parse trace. [The following comment makes no
/* To avoid crippling this file with `#if YYDEBUG', define `yydebug' sense to me. Could someone clarify it? --akim] Since this is
as `0', so that the `if (yydebug)' be removed as dead code. */ uninitialized, it does not stop multiple parsers from coexisting.
# define yydebug 0 */
#endif int yydebug;
#else /* !YYDEBUG */
/* YYINITDEPTH indicates the initial size of the parser's stacks */ # define YYDPRINTF(Args)
#endif /* !YYDEBUG */
/* YYINITDEPTH -- initial size of the parser's stacks. */
#ifndef YYINITDEPTH #ifndef YYINITDEPTH
# define YYINITDEPTH 200 # define YYINITDEPTH 200
#endif #endif
/* YYMAXDEPTH is the maximum size the stacks can grow to /* YYMAXDEPTH -- maximum size the stacks can grow to (effective only
(effective only if the built-in stack extension method is used). */ if the built-in stack extension method is used). */
#if YYMAXDEPTH == 0 #if YYMAXDEPTH == 0
# undef YYMAXDEPTH # undef YYMAXDEPTH
#endif #endif
@@ -190,7 +191,7 @@ int yydebug; /* nonzero means print parse trace */
of type size_t, but it can handle unsigned int. */ of type size_t, but it can handle unsigned int. */
#if __GNUC__ > 1 /* GNU C and GNU C++ define this. */ #if __GNUC__ > 1 /* GNU C and GNU C++ define this. */
# define __yy_memcpy(To,From,Count) __builtin_memcpy (To,From,Count) # define __yy_memcpy(To, From, Count) __builtin_memcpy (To, From, Count)
#else /* not GNU C or C++ */ #else /* not GNU C or C++ */
# ifndef __cplusplus # ifndef __cplusplus
@@ -324,8 +325,7 @@ yyparse (YYPARSE_PARAM_ARG)
rule. */ rule. */
int yylen; int yylen;
if (yydebug) YYDPRINTF ((stderr, "Starting parse\n"));
fprintf (stderr, "Starting parse\n");
yystate = 0; yystate = 0;
yyerrstatus = 0; yyerrstatus = 0;
@@ -429,15 +429,13 @@ yynewstate:
yylsp = yyls + size - 1; yylsp = yyls + size - 1;
#endif #endif
if (yydebug) YYDPRINTF ((stderr, "Stack size increased to %d\n", yystacksize));
fprintf (stderr, "Stack size increased to %d\n", yystacksize);
if (yyssp >= yyss + yystacksize - 1) if (yyssp >= yyss + yystacksize - 1)
YYABORT; YYABORT;
} }
if (yydebug) YYDPRINTF ((stderr, "Entering state %d\n", yystate));
fprintf (stderr, "Entering state %d\n", yystate);
goto yybackup; goto yybackup;
@@ -464,8 +462,7 @@ yybackup:
if (yychar == YYEMPTY) if (yychar == YYEMPTY)
{ {
if (yydebug) YYDPRINTF ((stderr, "Reading a token: "));
fprintf (stderr, "Reading a token: ");
yychar = YYLEX; yychar = YYLEX;
} }
@@ -476,8 +473,7 @@ yybackup:
yychar1 = 0; yychar1 = 0;
yychar = YYEOF; /* Don't call YYLEX any more */ yychar = YYEOF; /* Don't call YYLEX any more */
if (yydebug) YYDPRINTF ((stderr, "Now at end of input.\n"));
fprintf (stderr, "Now at end of input.\n");
} }
else else
{ {
@@ -526,10 +522,7 @@ yybackup:
YYACCEPT; YYACCEPT;
/* Shift the lookahead token. */ /* Shift the lookahead token. */
#if YYDEBUG YYDPRINTF ((stderr, "Shifting token %d (%s), ", yychar, yytname[yychar1]));
if (yydebug)
fprintf (stderr, "Shifting token %d (%s), ", yychar, yytname[yychar1]);
#endif
/* Discard the token being shifted unless it is eof. */ /* Discard the token being shifted unless it is eof. */
if (yychar != YYEOF) if (yychar != YYEOF)
@@ -612,6 +605,7 @@ $ /* The action file replaces this line marked with this dollarsign. */
yylsp -= yylen; yylsp -= yylen;
#endif #endif
#if YYDEBUG
if (yydebug) if (yydebug)
{ {
short *ssp1 = yyss - 1; short *ssp1 = yyss - 1;
@@ -620,6 +614,7 @@ $ /* The action file replaces this line marked with this dollarsign. */
fprintf (stderr, " %d", *++ssp1); fprintf (stderr, " %d", *++ssp1);
fprintf (stderr, "\n"); fprintf (stderr, "\n");
} }
#endif
*++yyvsp = yyval; *++yyvsp = yyval;
#ifdef YYLSP_NEEDED #ifdef YYLSP_NEEDED
@@ -712,11 +707,8 @@ yyerrlab1:
/* return failure if at end of input */ /* return failure if at end of input */
if (yychar == YYEOF) if (yychar == YYEOF)
YYABORT; YYABORT;
#if YYDEBUG YYDPRINTF ((stderr, "Discarding token %d (%s).\n",
if (yydebug) yychar, yytname[yychar1]));
fprintf (stderr, "Discarding token %d (%s).\n",
yychar, yytname[yychar1]);
#endif
yychar = YYEMPTY; yychar = YYEMPTY;
} }
@@ -757,6 +749,7 @@ yyerrpop:
yylsp--; yylsp--;
#endif #endif
#if YYDEBUG
if (yydebug) if (yydebug)
{ {
short *ssp1 = yyss - 1; short *ssp1 = yyss - 1;
@@ -765,7 +758,7 @@ yyerrpop:
fprintf (stderr, " %d", *++ssp1); fprintf (stderr, " %d", *++ssp1);
fprintf (stderr, "\n"); fprintf (stderr, "\n");
} }
#endif
/*--------------. /*--------------.
| yyerrhandle. | | yyerrhandle. |
@@ -793,8 +786,7 @@ yyerrhandle:
if (yyn == YYFINAL) if (yyn == YYFINAL)
YYACCEPT; YYACCEPT;
if (yydebug) YYDPRINTF ((stderr, "Shifting error token, "));
fprintf (stderr, "Shifting error token, ");
*++yyvsp = yylval; *++yyvsp = yylval;
#ifdef YYLSP_NEEDED #ifdef YYLSP_NEEDED

View File

@@ -36,6 +36,7 @@ AT_DEFINE([_AT_DATA_CALC_Y],
static int power (int base, int exponent); static int power (int base, int exponent);
static int read_signed_integer (FILE *stream); static int read_signed_integer (FILE *stream);
static void yyerror (const char *s); static void yyerror (const char *s);
static int yylex (void);
extern void perror (const char *s); extern void perror (const char *s);
%} %}
@@ -105,8 +106,8 @@ read_signed_integer (FILE *stream)
| blanks and tabs, returns 0 for EOF. | | blanks and tabs, returns 0 for EOF. |
`---------------------------------------------------------------*/ `---------------------------------------------------------------*/
int static int
yylex () yylex (void)
{ {
int c; int c;
@@ -176,36 +177,36 @@ AT_DEFINE([_AT_CHECK_CALC],
[AT_CHECK([echo "$1" | calc], 0, [$2], [$3])]) [AT_CHECK([echo "$1" | calc], 0, [$2], [$3])])
# AT_CHECK_CALC(TITLE, [BISON-OPTIONS], [PARSER-EXPECTED-STDERR]) # AT_CHECK_CALC([BISON-OPTIONS], [PARSER-EXPECTED-STDERR])
# --------------------------------------------------------------- # --------------------------------------------------------
# Start a testing chunk named TITLE which compiles `calc' grammar with # Start a testing chunk which compiles `calc' grammar with
# BISON-OPTIONS, and performs several tests over the parser. # BISON-OPTIONS, and performs several tests over the parser.
AT_DEFINE([AT_CHECK_CALC], AT_DEFINE([AT_CHECK_CALC],
[# We use integers to avoid dependencies upon the precision of doubles. [# We use integers to avoid dependencies upon the precision of doubles.
AT_SETUP([$1]) AT_SETUP([Calculator $1])
AT_DATA_CALC_Y AT_DATA_CALC_Y
# Specify the output files to avoid problems on different file systems. # Specify the output files to avoid problems on different file systems.
AT_CHECK([bison calc.y -o calc.c $2], 0, [], []) AT_CHECK([bison calc.y -o calc.c $1], 0, [], [])
AT_CHECK([$CC $CFLAGS calc.c -o calc], 0, [], []) AT_CHECK([$CC $CFLAGS calc.c -o calc], 0, [], [])
# Test the priorities. # Test the priorities.
_AT_CHECK_CALC([1 + 2 * 3], [7], [$3]) _AT_CHECK_CALC([1 + 2 * 3], [7], [$2])
_AT_CHECK_CALC([1 + 2 * -3], [-5], [$3]) _AT_CHECK_CALC([1 + 2 * -3], [-5], [$2])
_AT_CHECK_CALC([-1^2], [-1], [$3]) _AT_CHECK_CALC([-1^2], [-1], [$2])
_AT_CHECK_CALC([(-1)^2], [1], [$3]) _AT_CHECK_CALC([(-1)^2], [1], [$2])
_AT_CHECK_CALC([---1], [-1], [$3]) _AT_CHECK_CALC([---1], [-1], [$2])
_AT_CHECK_CALC([1 - 2 - 3], [-4], [$3]) _AT_CHECK_CALC([1 - 2 - 3], [-4], [$2])
_AT_CHECK_CALC([1 - (2 - 3)], [2], [$3]) _AT_CHECK_CALC([1 - (2 - 3)], [2], [$2])
_AT_CHECK_CALC([2^2^3], [256], [$3]) _AT_CHECK_CALC([2^2^3], [256], [$2])
_AT_CHECK_CALC([(2^2)^3], [64], [$3]) _AT_CHECK_CALC([(2^2)^3], [64], [$2])
AT_CLEANUP(calc calc.c) AT_CLEANUP(calc calc.c calc.h calc.output)
])# AT_CHECK_CALC ])# AT_CHECK_CALC
@@ -214,16 +215,16 @@ AT_CLEANUP(calc calc.c)
# -------------- # # -------------- #
AT_CHECK_CALC([Simple calculator]) AT_CHECK_CALC()
# This one is very suspicious. The test fails, but it might be normal.
AT_CHECK_CALC([--raw])
AT_CHECK_CALC([Simple Yacc compatible calculator], AT_CHECK_CALC([--defines])
[--yacc]) AT_CHECK_CALC([--name-prefix=calc])
AT_CHECK_CALC([--verbose])
AT_CHECK_CALC([--yacc])
AT_CHECK_CALC([--defines --name-prefix=calc --verbose --yacc])
AT_CHECK_CALC([Simple calculator whose tokens are numbered from 3], # When --debug, a lot of data is sent to STDERR, we can't test it.
[--raw]) AT_CHECK_CALC([--debug], ignore)
AT_CHECK_CALC([--debug --defines --name-prefix=calc --verbose --yacc], ignore)
AT_CHECK_CALC([Simple debugging calculator],
[--debug], ignore)
AT_CHECK_CALC([Simple Yacc compatible debugging calculator],
[--debug --yacc], ignore)