From: Joel E. Denny Date: Sun, 11 Jun 2006 18:27:44 +0000 (+0000) Subject: For associating token numbers with token names for "yacc.c", don't use X-Git-Tag: v2.3b~368 X-Git-Url: https://git.saurik.com/bison.git/commitdiff_plain/b931235eb9e55277e0147443f916c0f73ad697c4 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: , , and . * 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. --- diff --git a/ChangeLog b/ChangeLog index 68088e4d..bd43c25a 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,26 @@ +2006-06-11 Joel E. Denny + + 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: + , + , + and + . + * 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 * NEWS: Reword the post-2.3 change to not be so optimistic about diff --git a/NEWS b/NEWS index 2e757516..a1bf9ae1 100644 --- a/NEWS +++ b/NEWS @@ -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. diff --git a/data/c.m4 b/data/c.m4 index f10e0901..46bacfcc 100644 --- a/data/c.m4 +++ b/data/c.m4 @@ -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($@)], []) ]) diff --git a/doc/bison.texinfo b/doc/bison.texinfo index 84a7dee8..f433b712 100644 --- a/doc/bison.texinfo +++ b/doc/bison.texinfo @@ -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 diff --git a/src/getargs.c b/src/getargs.c index ec861eb0..0a7f3727 100644 --- a/src/getargs.c +++ b/src/getargs.c @@ -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 (_("\ diff --git a/src/output.c b/src/output.c index 4e7b5bf3..0d648770 100644 --- a/src/output.c +++ b/src/output.c @@ -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"); diff --git a/tests/regression.at b/tests/regression.at index 04beccc3..bbd00761 100644 --- a/tests/regression.at +++ b/tests/regression.at @@ -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 +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;