+2006-06-11 Joel E. Denny <jdenny@ces.clemson.edu>
+
+ For associating token numbers with token names for "yacc.c", don't use
+ #define statements unless `--yacc' is specified; always use enum
+ yytokentype. Most important discussions start at:
+ <http://lists.gnu.org/archive/html/bison-patches/2005-09/msg00053.html>,
+ <http://lists.gnu.org/archive/html/bison-patches/2005-12/msg00052.html>,
+ and
+ <http://lists.gnu.org/archive/html/bison-patches/2006-06/msg00043.html>.
+ * NEWS (2.3+): Mention.
+ * data/c.m4 (b4_yacc_if): New.
+ (b4_token_enums_defines): Use b4_yacc_if to decide whether to add the
+ token #define's.
+ * doc/bison.texinfo (Bison Options): Describe the effect of `--yacc'
+ on token name definitions.
+ * src/getargs.c (usage): Capitalize `Yacc' in English.
+ * src/output.c (prepare): Define b4_yacc_flag.
+ * tests/regression.at (Early token definitions): Test that tokens names
+ are defined before the pre-prologue not just before the post-prologue.
+ Remove this test case and copy to...
+ (Early token definitions with --yacc): ... this to test #define's.
+ (Early token definitions without --yacc): ... and this to test enums.
+
2006-06-11 Paul Eggert <eggert@cs.ucla.edu>
* NEWS: Reword the post-2.3 change to not be so optimistic about
Changes in version 2.3+:
+* Except for LALR(1) parsers in C with POSIX Yacc emulation enabled (with `-y',
+ `--yacc', or `%yacc'), Bison no longer generates #define statements for
+ associating token numbers with token names. Removing the #define statements
+ helps to sanitize the global namespace during preprocessing, but POSIX Yacc
+ requires them. Bison still generates an enum for token names in all cases.
+
* The option `--report=look-ahead' has been changed to `--report=lookahead'.
The old spelling still works, but is not documented and may be removed
in a future release.
b4_define_flag_if([error_verbose]) # Wheter error are verbose.
b4_define_flag_if([locations]) # Whether locations are tracked.
b4_define_flag_if([pure]) # Whether the interface is pure.
+b4_define_flag_if([yacc]) # Whether POSIX Yacc is emulated.
# b4_token_enums_defines(LIST-OF-PAIRS-TOKEN-NAME-TOKEN-NUMBER)
# -------------------------------------------------------------
-# Output the definition of the tokens (if there are) as enums and #defines.
+# Output the definition of the tokens (if there are any) as enums and, if POSIX
+# Yacc is enabled, as #defines.
m4_define([b4_token_enums_defines],
-[b4_token_enums($@)b4_token_defines($@)
+[b4_token_enums($@)b4_yacc_if([b4_token_defines($@)], [])
])
other minor ways. Most importantly, imitate Yacc's output
file name conventions, so that the parser output file is called
@file{y.tab.c}, and the other outputs are called @file{y.output} and
-@file{y.tab.h}. Thus, the following shell script can substitute
-for Yacc, and the Bison distribution contains such a script for
-compatibility with @acronym{POSIX}:
+@file{y.tab.h}.
+Also, if generating an @acronym{LALR}(1) parser in C, generate @code{#define}
+statements in addition to an @code{enum} to associate token numbers with token
+names.
+Thus, the following shell script can substitute for Yacc, and the Bison
+distribution contains such a script for compatibility with @acronym{POSIX}:
@example
#! /bin/sh
-h, --help display this help and exit\n\
-V, --version output version information and exit\n\
--print-localedir output directory containing locale-dependent data\n\
- -y, --yacc emulate POSIX yacc\n"), stdout);
+ -y, --yacc emulate POSIX Yacc\n"), stdout);
putc ('\n', stdout);
fputs (_("\
MUSCLE_INSERT_BOOL ("locations_flag", locations_flag);
MUSCLE_INSERT_BOOL ("pure_flag", pure_parser);
MUSCLE_INSERT_BOOL ("synclines_flag", !no_lines_flag);
+ MUSCLE_INSERT_BOOL ("yacc_flag", yacc_flag);
/* File names. */
MUSCLE_INSERT_STRING ("prefix", spec_name_prefix ? spec_name_prefix : "yy");
-## ------------------------- ##
-## Early token definitions. ##
-## ------------------------- ##
+## ------------------------------------- ##
+## Early token definitions with --yacc. ##
+## ------------------------------------- ##
-AT_SETUP([Early token definitions])
+AT_SETUP([Early token definitions with --yacc])
# Found in GCJ: they expect the tokens to be defined before the user
# prologue, so that they can use the token definitions in it.
[[%{
void yyerror (const char *s);
int yylex (void);
+#ifndef MY_TOKEN
+# error "MY_TOKEN not defined."
+#endif
%}
%union
{
int val;
};
-%{
-#ifndef MY_TOKEN
-# error "MY_TOKEN not defined."
-#endif
+%token MY_TOKEN
+%%
+exp: MY_TOKEN;
+%%
+]])
+
+AT_CHECK([bison -y -o input.c input.y])
+AT_COMPILE([input.o], [-c input.c])
+
+AT_CLEANUP
+
+
+
+## ---------------------------------------- ##
+## Early token definitions without --yacc. ##
+## ---------------------------------------- ##
+
+
+AT_SETUP([Early token definitions without --yacc])
+
+# Found in GCJ: they expect the tokens to be defined before the user
+# prologue, so that they can use the token definitions in it.
+
+AT_DATA_GRAMMAR([input.y],
+[[%{
+#include <stdio.h>
+void yyerror (const char *s);
+int yylex (void);
+void print_my_token (void);
+void
+print_my_token (void)
+{
+ enum yytokentype my_token = MY_TOKEN;
+ printf ("%d\n", my_token);
+}
%}
+
+%union
+{
+ int val;
+};
%token MY_TOKEN
%%
exp: MY_TOKEN;