Skip to content

Commit 5d9ccd5

Browse files
committed
yacc: declare yyerror/yylex only when POSIXLY_CORRECT
The recent changes to comply with POSIX are breaking Automake's test suite. Reported by Kiyoshi Kanazawa. <https://lists.gnu.org/r/bug-bison/2021-09/msg00005.html> To limit the impact of POSIX changes, bind them to $POSIXLY_CORRECT. Suggested by Karl Berry. <https://lists.gnu.org/r/bug-bison/2021-09/msg00009.html> The existing `maintainer-check-posix` Make target checks these changes. * src/getargs.h, src/getargs.c (set_yacc): New. Use it. * data/skeletons/bison.m4 (b4_posix_if): New. * data/skeletons/yacc.c (b4_declare_yyerror_and_yylex): Use it. * doc/bison.texi, tests/local.at: Adjust.
1 parent ad9b8a0 commit 5d9ccd5

File tree

11 files changed

+53
-16
lines changed

11 files changed

+53
-16
lines changed

NEWS

+9-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@ GNU Bison NEWS
22

33
* Noteworthy changes in release ?.? (????-??-??) [?]
44

5+
The generation of prototypes for yylex and yyerror in Yacc mode is
6+
breaking existing grammar files. To avoid breaking too many grammars, the
7+
prototypes are now generated when `-y/--yacc` is used *and* the
8+
`POSIXLY_CORRECT` environment variable is defined.
9+
10+
Avoid using `-y`/`--yacc` simply to comply with Yacc's file name
11+
conventions, rather, use `-o y.tab.c`. Autoconf's AC_PROG_YACC macro uses
12+
`-y`. Avoid it if possible, for instance by using gnulib's gl_PROG_BISON.
513

614
* Noteworthy changes in release 3.8 (2021-09-07) [stable]
715

@@ -4706,7 +4714,7 @@ LocalWords: autocompletion bistromathic submessages Cayuela lexcalc hoc
47064714
LocalWords: yytoken YYUNDEF YYerror basename Automake's UTF ifdef ffile
47074715
LocalWords: gotos readline Imbimbo Wcounterexamples Wcex Nonunifying rcex
47084716
LocalWords: Vais xsltproc YYNOMEM YYLOCATION signedness YYBISON MITRE's
4709-
LocalWords: libreadline YYMALLOC YYFREE MSVC redefinitions
4717+
LocalWords: libreadline YYMALLOC YYFREE MSVC redefinitions POSIXLY
47104718

47114719
Local Variables:
47124720
ispell-dictionary: "american"

data/skeletons/bison.m4

+1
Original file line numberDiff line numberDiff line change
@@ -1110,6 +1110,7 @@ b4_percent_define_if_define([token_ctor], [api.token.constructor])
11101110
b4_percent_define_if_define([locations]) # Whether locations are tracked.
11111111
b4_percent_define_if_define([parse.assert])
11121112
b4_percent_define_if_define([parse.trace])
1113+
b4_percent_define_if_define([posix])
11131114

11141115

11151116
# b4_bison_locations_if([IF-TRUE])

data/skeletons/yacc.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ m4_define([b4_declare_yyparse],
367367
# Comply with POSIX Yacc.
368368
# <https://austingroupbugs.net/view.php?id=1388#c5220>
369369
m4_define([b4_declare_yyerror_and_yylex],
370-
[b4_yacc_if([[#if !defined ]b4_prefix[error && !defined ]b4_api_PREFIX[ERROR_IS_DECLARED
370+
[b4_posix_if([[#if !defined ]b4_prefix[error && !defined ]b4_api_PREFIX[ERROR_IS_DECLARED
371371
]b4_function_declare([b4_prefix[error]], void, b4_yyerror_formals)[
372372
#endif
373373
#if !defined ]b4_prefix[lex && !defined ]b4_api_PREFIX[LEX_IS_DECLARED

doc/bison.texi

+4-1
Original file line numberDiff line numberDiff line change
@@ -11877,7 +11877,10 @@ Generate different diagnostics (it implies @option{-Wyacc}).
1187711877
Generate @code{#define} statements in addition to an @code{enum} to
1187811878
associate token codes with token kind names.
1187911879
@item
11880-
Generate prototypes for @code{yyerror} and @code{yylex} (since Bison 3.8):
11880+
If the @code{POSIXLY_CORRECT} environment variable is defined, generate
11881+
prototypes for @code{yyerror} and @code{yylex}@footnote{See
11882+
@url{https://austingroupbugs.net/view.php?id=1388#c5220}.} (since Bison
11883+
3.8):
1188111884
@example
1188211885
int yylex (void);
1188311886
void yyerror (const char *);

src/getargs.c

+14-3
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,18 @@ struct bison_language const *language = &valid_languages[0];
7171

7272
typedef int* (xargmatch_fn) (const char *context, const char *arg);
7373

74+
void
75+
set_yacc (location loc)
76+
{
77+
yacc_loc = loc;
78+
if (getenv ("POSIXLY_CORRECT"))
79+
muscle_percent_define_insert ("posix",
80+
loc,
81+
muscle_keyword, "",
82+
MUSCLE_PERCENT_DEFINE_D);
83+
}
84+
85+
7486
/** Decode an option's key.
7587
*
7688
* \param opt option being decoded.
@@ -631,8 +643,7 @@ static struct option const long_options[] =
631643
};
632644

633645
/* Build a location for the current command line argument. */
634-
static
635-
location
646+
static location
636647
command_line_location (void)
637648
{
638649
location res;
@@ -835,7 +846,7 @@ getargs (int argc, char *argv[])
835846

836847
case 'y':
837848
warning_argmatch ("yacc", 0, 0);
838-
yacc_loc = loc;
849+
set_yacc (loc);
839850
break;
840851

841852
case COLOR_OPTION:

src/getargs.h

+1
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ void getargs (int argc, char *argv[]);
140140
/* Used by parse-gram.y. */
141141
void language_argmatch (char const *arg, int prio, location loc);
142142
void skeleton_arg (const char *arg, int prio, location loc);
143+
void set_yacc (location loc);
143144

144145
/** In the string \c s, replace all characters \c from by \c to. */
145146
void tr (char *s, char from, char to);

src/parse-gram.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* A Bison parser, made by GNU Bison 3.7.5.297-b46a. */
1+
/* A Bison parser, made by GNU Bison 3.8. */
22

33
/* Bison implementation for Yacc-like parsers in C
44
@@ -46,10 +46,10 @@
4646
USER NAME SPACE" below. */
4747

4848
/* Identify Bison output, and Bison version. */
49-
#define YYBISON 30705
49+
#define YYBISON 30800
5050

5151
/* Bison version string. */
52-
#define YYBISON_VERSION "3.7.5.297-b46a"
52+
#define YYBISON_VERSION "3.8"
5353

5454
/* Skeleton name. */
5555
#define YYSKELETON_NAME "yacc.c"
@@ -3129,7 +3129,7 @@ handle_yacc (location const *loc)
31293129
const char *directive = "%yacc";
31303130
bison_directive (loc, directive);
31313131
if (location_empty (yacc_loc))
3132-
yacc_loc = *loc;
3132+
set_yacc (*loc);
31333133
else
31343134
duplicate_directive (directive, yacc_loc, *loc);
31353135
}

src/parse-gram.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* A Bison parser, made by GNU Bison 3.7.5.297-b46a. */
1+
/* A Bison parser, made by GNU Bison 3.8. */
22

33
/* Bison interface for Yacc-like parsers in C
44
@@ -215,6 +215,7 @@ struct GRAM_LTYPE
215215

216216

217217

218+
218219
int gram_parse (void);
219220

220221
/* "%code provides" blocks. */

src/parse-gram.y

+1-1
Original file line numberDiff line numberDiff line change
@@ -1112,7 +1112,7 @@ handle_yacc (location const *loc)
11121112
const char *directive = "%yacc";
11131113
bison_directive (loc, directive);
11141114
if (location_empty (yacc_loc))
1115-
yacc_loc = *loc;
1115+
set_yacc (*loc);
11161116
else
11171117
duplicate_directive (directive, yacc_loc, *loc);
11181118
}

tests/calc.at

+13-1
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ read_integer (]AT_YYLEX_FORMALS[)
268268

269269

270270
m4_define([_AT_DATA_CALC_Y(c)],
271-
[AT_DATA_GRAMMAR([calc.y],
271+
[AT_DATA_GRAMMAR([calc.y.tmp],
272272
[[/* Infix notation calculator--calc */
273273
]$4[
274274
%code requires
@@ -493,6 +493,18 @@ location_print (FILE *o, Span s)
493493
[AT_CALC_YYLEX
494494
AT_CALC_MAIN])])
495495

496+
# Remove the generated prototypes.
497+
AT_CHECK(
498+
[AT_YACC_IF([[
499+
if "$POSIXLY_CORRECT_IS_EXPORTED"; then
500+
sed -e '/\/\* !POSIX \*\//d' calc.y.tmp >calc.y
501+
else
502+
mv calc.y.tmp calc.y
503+
fi
504+
]],
505+
[[mv calc.y.tmp calc.y]])
506+
])
507+
496508
AT_HEADER_IF([AT_DATA_SOURCE([[calc-lex.]AT_LANG_EXT],
497509
[[#include "calc.]AT_LANG_HDR["
498510

tests/local.at

+3-3
Original file line numberDiff line numberDiff line change
@@ -687,8 +687,8 @@ m4_define([AT_YYERROR_DECLARE_EXTERN(c)],
687687
m4_define([AT_YYERROR_DECLARE(c)],
688688
[[#include <stdio.h>
689689
]AT_LOCATION_PRINT_DECLARE[
690-
]AT_YACC_IF([], [[static ]AT_YYERROR_DECLARE_EXTERN])])
691-
690+
/* !POSIX */ static ]AT_YYERROR_DECLARE_EXTERN[]dnl
691+
])
692692

693693
# "%define parse.error custom" uses a different format, easy to check.
694694
# The "verbose" one can be computed from it (see _AT_CHECK_CALC_ERROR).
@@ -733,7 +733,7 @@ yyreport_syntax_error (const yypcontext_t *ctx]AT_PARAM_IF([, AT_PARSE_PARAMS])[
733733
]])[
734734

735735
/* A C error reporting function. */
736-
]AT_YACC_IF([], [static])[
736+
/* !POSIX */ static
737737
]AT_YYERROR_PROTOTYPE[
738738
{]m4_bpatsubst(m4_defn([AT_PARSE_PARAMS]),
739739
[[^,]+[^A-Za-z_0-9]\([A-Za-z_][A-Za-z_0-9]*\),* *], [

0 commit comments

Comments
 (0)