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.
This commit is contained in:
Joel E. Denny
2006-06-11 18:27:44 +00:00
parent 9e6e7ed2b2
commit b931235eb9
7 changed files with 88 additions and 14 deletions

View File

@@ -1,3 +1,26 @@
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

6
NEWS
View File

@@ -3,6 +3,12 @@ Bison News
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.

View File

@@ -207,6 +207,7 @@ b4_define_flag_if([defines]) # Whether headers are requested.
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.
@@ -260,9 +261,10 @@ m4_map_sep([ b4_token_enum], [,
# 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($@)], [])
])

View File

@@ -6830,9 +6830,12 @@ different diagnostics to be generated, and may change behavior in
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

View File

@@ -217,7 +217,7 @@ Operation modes:\n\
-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 (_("\

View File

@@ -590,6 +590,7 @@ prepare (void)
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");

View File

@@ -49,12 +49,12 @@ AT_CLEANUP
## ------------------------- ##
## 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.
@@ -63,17 +63,56 @@ AT_DATA_GRAMMAR([input.y],
[[%{
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;