Test also `--verbose', `--defines' and `--name-prefix'. Testing
authorAkim Demaille <akim@epita.fr>
Mon, 2 Oct 2000 09:04:40 +0000 (09:04 +0000)
committerAkim Demaille <akim@epita.fr>
Mon, 2 Oct 2000 09:04:40 +0000 (09:04 +0000)
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'.

ChangeLog
src/bison.s1
src/bison.simple
tests/calc.m4

index cda73b7..7d28495 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,38 @@
 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>
+
        Improve the readability of the produced parsers.
 
        * src/bison.s1: Formatting changes.
index a0c6b8a..bc65624 100644 (file)
@@ -154,28 +154,29 @@ YYLTYPE yylloc;
 int yynerrs;
 #endif  /* !YYPURE */
 
-
+/* Enable debugging if requested.  */
 #if YYDEBUG
-int yydebug;                   /*  nonzero means print parse trace     */
-
-  /* [The following comment makes no sense to me.  Could someone
-     clarify it?  --akim] Since this is uninitialized, it does not
-     stop multiple parsers from coexisting.  */
-#else
-  /* To avoid crippling this file with `#if YYDEBUG', define `yydebug'
-     as `0', so that the `if (yydebug)' be removed as dead code.  */
-# define yydebug 0
-#endif
-
-/*  YYINITDEPTH indicates the initial size of the parser's stacks      */
-
+# define YYDPRINTF(Args)                       \
+do {                                           \
+  if (yydebug)                                 \
+    fprintf Args;                              \
+} while (0)
+/* Nonzero means print parse trace. [The following comment makes no
+   sense to me.  Could someone clarify it?  --akim] Since this is
+   uninitialized, it does not stop multiple parsers from coexisting.
+   */
+int yydebug;
+#else /* !YYDEBUG */
+# define YYDPRINTF(Args)
+#endif /* !YYDEBUG */
+
+/* YYINITDEPTH -- initial size of the parser's stacks.  */
 #ifndef        YYINITDEPTH
 # define YYINITDEPTH 200
 #endif
 
-/*  YYMAXDEPTH is the maximum size the stacks can grow to
-    (effective only if the built-in stack extension method is used).  */
-
+/* YYMAXDEPTH -- maximum size the stacks can grow to (effective only
+   if the built-in stack extension method is used).  */
 #if YYMAXDEPTH == 0
 # undef YYMAXDEPTH
 #endif
@@ -190,7 +191,7 @@ int yydebug;                        /*  nonzero means print parse trace     */
    of type size_t, but it can handle unsigned int.  */
 
 #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++ */
 # ifndef __cplusplus
 
@@ -324,8 +325,7 @@ yyparse (YYPARSE_PARAM_ARG)
      rule. */
   int yylen;
 
-  if (yydebug)
-    fprintf (stderr, "Starting parse\n");
+  YYDPRINTF ((stderr, "Starting parse\n"));
 
   yystate = 0;
   yyerrstatus = 0;
@@ -429,15 +429,13 @@ yynewstate:
       yylsp = yyls + size - 1;
 #endif
 
-      if (yydebug)
-       fprintf (stderr, "Stack size increased to %d\n", yystacksize);
+      YYDPRINTF ((stderr, "Stack size increased to %d\n", yystacksize));
 
       if (yyssp >= yyss + yystacksize - 1)
        YYABORT;
     }
 
-  if (yydebug)
-    fprintf (stderr, "Entering state %d\n", yystate);
+  YYDPRINTF ((stderr, "Entering state %d\n", yystate));
 
   goto yybackup;
 
@@ -464,8 +462,7 @@ yybackup:
 
   if (yychar == YYEMPTY)
     {
-      if (yydebug)
-       fprintf (stderr, "Reading a token: ");
+      YYDPRINTF ((stderr, "Reading a token: "));
       yychar = YYLEX;
     }
 
@@ -476,8 +473,7 @@ yybackup:
       yychar1 = 0;
       yychar = YYEOF;          /* Don't call YYLEX any more */
 
-      if (yydebug)
-       fprintf (stderr, "Now at end of input.\n");
+      YYDPRINTF ((stderr, "Now at end of input.\n"));
     }
   else
     {
@@ -526,10 +522,7 @@ yybackup:
     YYACCEPT;
 
   /* Shift the lookahead token.  */
-#if YYDEBUG
-  if (yydebug)
-    fprintf (stderr, "Shifting token %d (%s), ", yychar, yytname[yychar1]);
-#endif
+  YYDPRINTF ((stderr, "Shifting token %d (%s), ", yychar, yytname[yychar1]));
 
   /* Discard the token being shifted unless it is eof.  */
   if (yychar != YYEOF)
@@ -612,6 +605,7 @@ $ /* The action file replaces this line marked with this dollarsign. */
   yylsp -= yylen;
 #endif
 
+#if YYDEBUG
   if (yydebug)
     {
       short *ssp1 = yyss - 1;
@@ -620,6 +614,7 @@ $ /* The action file replaces this line marked with this dollarsign. */
        fprintf (stderr, " %d", *++ssp1);
       fprintf (stderr, "\n");
     }
+#endif
 
   *++yyvsp = yyval;
 #ifdef YYLSP_NEEDED
@@ -712,11 +707,8 @@ yyerrlab1:
       /* return failure if at end of input */
       if (yychar == YYEOF)
        YYABORT;
-#if YYDEBUG
-      if (yydebug)
-       fprintf (stderr, "Discarding token %d (%s).\n",
-                yychar, yytname[yychar1]);
-#endif
+      YYDPRINTF ((stderr, "Discarding token %d (%s).\n",
+                 yychar, yytname[yychar1]));
       yychar = YYEMPTY;
     }
 
@@ -757,6 +749,7 @@ yyerrpop:
   yylsp--;
 #endif
 
+#if YYDEBUG
   if (yydebug)
     {
       short *ssp1 = yyss - 1;
@@ -765,7 +758,7 @@ yyerrpop:
        fprintf (stderr, " %d", *++ssp1);
       fprintf (stderr, "\n");
     }
-
+#endif
 
 /*--------------.
 | yyerrhandle.  |
@@ -793,8 +786,7 @@ yyerrhandle:
   if (yyn == YYFINAL)
     YYACCEPT;
 
-  if (yydebug)
-    fprintf (stderr, "Shifting error token, ");
+  YYDPRINTF ((stderr, "Shifting error token, "));
 
   *++yyvsp = yylval;
 #ifdef YYLSP_NEEDED
index a0c6b8a..bc65624 100644 (file)
@@ -154,28 +154,29 @@ YYLTYPE yylloc;
 int yynerrs;
 #endif  /* !YYPURE */
 
-
+/* Enable debugging if requested.  */
 #if YYDEBUG
-int yydebug;                   /*  nonzero means print parse trace     */
-
-  /* [The following comment makes no sense to me.  Could someone
-     clarify it?  --akim] Since this is uninitialized, it does not
-     stop multiple parsers from coexisting.  */
-#else
-  /* To avoid crippling this file with `#if YYDEBUG', define `yydebug'
-     as `0', so that the `if (yydebug)' be removed as dead code.  */
-# define yydebug 0
-#endif
-
-/*  YYINITDEPTH indicates the initial size of the parser's stacks      */
-
+# define YYDPRINTF(Args)                       \
+do {                                           \
+  if (yydebug)                                 \
+    fprintf Args;                              \
+} while (0)
+/* Nonzero means print parse trace. [The following comment makes no
+   sense to me.  Could someone clarify it?  --akim] Since this is
+   uninitialized, it does not stop multiple parsers from coexisting.
+   */
+int yydebug;
+#else /* !YYDEBUG */
+# define YYDPRINTF(Args)
+#endif /* !YYDEBUG */
+
+/* YYINITDEPTH -- initial size of the parser's stacks.  */
 #ifndef        YYINITDEPTH
 # define YYINITDEPTH 200
 #endif
 
-/*  YYMAXDEPTH is the maximum size the stacks can grow to
-    (effective only if the built-in stack extension method is used).  */
-
+/* YYMAXDEPTH -- maximum size the stacks can grow to (effective only
+   if the built-in stack extension method is used).  */
 #if YYMAXDEPTH == 0
 # undef YYMAXDEPTH
 #endif
@@ -190,7 +191,7 @@ int yydebug;                        /*  nonzero means print parse trace     */
    of type size_t, but it can handle unsigned int.  */
 
 #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++ */
 # ifndef __cplusplus
 
@@ -324,8 +325,7 @@ yyparse (YYPARSE_PARAM_ARG)
      rule. */
   int yylen;
 
-  if (yydebug)
-    fprintf (stderr, "Starting parse\n");
+  YYDPRINTF ((stderr, "Starting parse\n"));
 
   yystate = 0;
   yyerrstatus = 0;
@@ -429,15 +429,13 @@ yynewstate:
       yylsp = yyls + size - 1;
 #endif
 
-      if (yydebug)
-       fprintf (stderr, "Stack size increased to %d\n", yystacksize);
+      YYDPRINTF ((stderr, "Stack size increased to %d\n", yystacksize));
 
       if (yyssp >= yyss + yystacksize - 1)
        YYABORT;
     }
 
-  if (yydebug)
-    fprintf (stderr, "Entering state %d\n", yystate);
+  YYDPRINTF ((stderr, "Entering state %d\n", yystate));
 
   goto yybackup;
 
@@ -464,8 +462,7 @@ yybackup:
 
   if (yychar == YYEMPTY)
     {
-      if (yydebug)
-       fprintf (stderr, "Reading a token: ");
+      YYDPRINTF ((stderr, "Reading a token: "));
       yychar = YYLEX;
     }
 
@@ -476,8 +473,7 @@ yybackup:
       yychar1 = 0;
       yychar = YYEOF;          /* Don't call YYLEX any more */
 
-      if (yydebug)
-       fprintf (stderr, "Now at end of input.\n");
+      YYDPRINTF ((stderr, "Now at end of input.\n"));
     }
   else
     {
@@ -526,10 +522,7 @@ yybackup:
     YYACCEPT;
 
   /* Shift the lookahead token.  */
-#if YYDEBUG
-  if (yydebug)
-    fprintf (stderr, "Shifting token %d (%s), ", yychar, yytname[yychar1]);
-#endif
+  YYDPRINTF ((stderr, "Shifting token %d (%s), ", yychar, yytname[yychar1]));
 
   /* Discard the token being shifted unless it is eof.  */
   if (yychar != YYEOF)
@@ -612,6 +605,7 @@ $ /* The action file replaces this line marked with this dollarsign. */
   yylsp -= yylen;
 #endif
 
+#if YYDEBUG
   if (yydebug)
     {
       short *ssp1 = yyss - 1;
@@ -620,6 +614,7 @@ $ /* The action file replaces this line marked with this dollarsign. */
        fprintf (stderr, " %d", *++ssp1);
       fprintf (stderr, "\n");
     }
+#endif
 
   *++yyvsp = yyval;
 #ifdef YYLSP_NEEDED
@@ -712,11 +707,8 @@ yyerrlab1:
       /* return failure if at end of input */
       if (yychar == YYEOF)
        YYABORT;
-#if YYDEBUG
-      if (yydebug)
-       fprintf (stderr, "Discarding token %d (%s).\n",
-                yychar, yytname[yychar1]);
-#endif
+      YYDPRINTF ((stderr, "Discarding token %d (%s).\n",
+                 yychar, yytname[yychar1]));
       yychar = YYEMPTY;
     }
 
@@ -757,6 +749,7 @@ yyerrpop:
   yylsp--;
 #endif
 
+#if YYDEBUG
   if (yydebug)
     {
       short *ssp1 = yyss - 1;
@@ -765,7 +758,7 @@ yyerrpop:
        fprintf (stderr, " %d", *++ssp1);
       fprintf (stderr, "\n");
     }
-
+#endif
 
 /*--------------.
 | yyerrhandle.  |
@@ -793,8 +786,7 @@ yyerrhandle:
   if (yyn == YYFINAL)
     YYACCEPT;
 
-  if (yydebug)
-    fprintf (stderr, "Shifting error token, ");
+  YYDPRINTF ((stderr, "Shifting error token, "));
 
   *++yyvsp = yylval;
 #ifdef YYLSP_NEEDED
index c365924..de0f33f 100644 (file)
@@ -36,6 +36,7 @@ AT_DEFINE([_AT_DATA_CALC_Y],
 static int power (int base, int exponent);
 static int read_signed_integer (FILE *stream);
 static void yyerror (const char *s);
+static int yylex (void);
 extern void perror (const char *s);
 %}
 
@@ -105,8 +106,8 @@ read_signed_integer (FILE *stream)
 | blanks and tabs, returns 0 for EOF.                            |
 `---------------------------------------------------------------*/
 
-int
-yylex ()
+static int
+yylex (void)
 {
   int c;
 
@@ -176,36 +177,36 @@ AT_DEFINE([_AT_CHECK_CALC],
 [AT_CHECK([echo "$1" | calc], 0, [$2], [$3])])
 
 
-# AT_CHECK_CALC(TITLE, [BISON-OPTIONS], [PARSER-EXPECTED-STDERR])
-# ---------------------------------------------------------------
-# Start a testing chunk named TITLE which compiles `calc' grammar with
+# AT_CHECK_CALC([BISON-OPTIONS], [PARSER-EXPECTED-STDERR])
+# --------------------------------------------------------
+# Start a testing chunk which compiles `calc' grammar with
 # BISON-OPTIONS, and performs several tests over the parser.
 AT_DEFINE([AT_CHECK_CALC],
 [# We use integers to avoid dependencies upon the precision of doubles.
-AT_SETUP([$1])
+AT_SETUP([Calculator $1])
 
 AT_DATA_CALC_Y
 
 # 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, [], [])
 
 # Test the priorities.
-_AT_CHECK_CALC([1 + 2 * 3],   [7], [$3])
-_AT_CHECK_CALC([1 + 2 * -3], [-5], [$3])
+_AT_CHECK_CALC([1 + 2 * 3],   [7], [$2])
+_AT_CHECK_CALC([1 + 2 * -3], [-5], [$2])
 
-_AT_CHECK_CALC([-1^2],  [-1], [$3])
-_AT_CHECK_CALC([(-1)^2], [1], [$3])
+_AT_CHECK_CALC([-1^2],  [-1], [$2])
+_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)], [2], [$3])
+_AT_CHECK_CALC([1 - 2 - 3],  [-4], [$2])
+_AT_CHECK_CALC([1 - (2 - 3)], [2], [$2])
 
-_AT_CHECK_CALC([2^2^3],  [256], [$3])
-_AT_CHECK_CALC([(2^2)^3], [64], [$3])
+_AT_CHECK_CALC([2^2^3],  [256], [$2])
+_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
 
 
@@ -214,16 +215,16 @@ AT_CLEANUP(calc calc.c)
 # -------------- #
 
 
-AT_CHECK_CALC([Simple calculator])
-
-AT_CHECK_CALC([Simple Yacc compatible calculator],
-              [--yacc])
-
-AT_CHECK_CALC([Simple calculator whose tokens are numbered from 3],
-              [--raw])
+AT_CHECK_CALC()
+# This one is very suspicious.  The test fails, but it might be normal.
+AT_CHECK_CALC([--raw])
 
-AT_CHECK_CALC([Simple debugging calculator],
-              [--debug], ignore)
+AT_CHECK_CALC([--defines])
+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 Yacc compatible debugging calculator],
-              [--debug --yacc], ignore)
+# When --debug, a lot of data is sent to STDERR, we can't test it.
+AT_CHECK_CALC([--debug], ignore)
+AT_CHECK_CALC([--debug --defines --name-prefix=calc --verbose --yacc], ignore)