From: Akim Demaille Date: Sun, 12 Mar 2006 14:45:27 +0000 (+0000) Subject: Apply --prefix to C++ skeletons too: they change the namespace. X-Git-Tag: v2.3b~429 X-Git-Url: https://git.saurik.com/bison.git/commitdiff_plain/aa08666d72d132462e8d53d2d23c95a3bcfa403d?hp=455671735e6f4e9712c2f5ac1bc153b07b33e67b Apply --prefix to C++ skeletons too: they change the namespace. The test suite already exercize these cases. * data/c++.m4 (b4_namespace): New. * data/lalr1.cc, data/glr.cc, data/location.cc: Use it instead of `yy'. * data/lalr1.cc (yytnameerr_): Move its definition into the namespace. * data/yacc.c, data/glr.c: Remove a useless `[]'. * doc/bison.texinfo: Document it. (Option Cross Key): Use @multitable in all formats. It looks nicer, even in TeX outputs. (Rules): Use the same code whatever the output type is. * tests/local.at (_AT_BISON_OPTION_PUSHDEFS) (_AT_BISON_OPTION_POPDEFS): Support AT_NAME_PREFIX. * tests/calc.at: Use it, instead of hard coding `yy'. --- diff --git a/ChangeLog b/ChangeLog index f75afd18..e81dcc52 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,19 @@ +2006-03-12 Akim Demaille + + Apply --prefix to C++ skeletons too: they change the namespace. + The test suite already exercize these cases. + * data/c++.m4 (b4_namespace): New. + * data/lalr1.cc, data/glr.cc, data/location.cc: Use it instead of `yy'. + * data/lalr1.cc (yytnameerr_): Move its definition into the namespace. + * data/yacc.c, data/glr.c: Remove a useless `[]'. + * doc/bison.texinfo: Document it. + (Option Cross Key): Use @multitable in all formats. It looks + nicer, even in TeX outputs. + (Rules): Use the same code whatever the output type is. + * tests/local.at (_AT_BISON_OPTION_PUSHDEFS) + (_AT_BISON_OPTION_POPDEFS): Support AT_NAME_PREFIX. + * tests/calc.at: Use it, instead of hard coding `yy'. + 2006-03-10 Akim Demaille * TODO: Remove dead items. diff --git a/NEWS b/NEWS index 702f45b7..4de67f10 100644 --- a/NEWS +++ b/NEWS @@ -3,7 +3,9 @@ Bison News Changes in version 2.1b: -* No user-visible changes. +* %name-prefix changes the namespace name in C++ outputs. + +* The C++ parsers export their token_type. Changes in version 2.1a, 2006-02-13: diff --git a/data/c++.m4 b/data/c++.m4 index a2834f02..57aee0ce 100644 --- a/data/c++.m4 +++ b/data/c++.m4 @@ -29,7 +29,7 @@ m4_include(b4_pkgdatadir/[c.m4]) m4_define_default([b4_parser_class_name], [parser]) m4_define_default([b4_location_type], [location]) m4_define_default([b4_filename_type], [std::string]) - +m4_define_default([b4_namespace], m4_defn([b4_prefix])) # b4_token_enums(LIST-OF-PAIRS-TOKEN-NAME-TOKEN-NUMBER) diff --git a/data/glr.c b/data/glr.c index e67573b6..9902b29f 100644 --- a/data/glr.c +++ b/data/glr.c @@ -150,7 +150,7 @@ b4_copyright([Skeleton parser for GLR parsing with Bison], ]b4_identification -m4_if(b4_prefix[], [yy], [], +m4_if(b4_prefix, [yy], [], [/* Substitute the variable and function names. */ #define yyparse b4_prefix[]parse #define yylex b4_prefix[]lex diff --git a/data/glr.cc b/data/glr.cc index 521b36b6..adb5b34e 100644 --- a/data/glr.cc +++ b/data/glr.cc @@ -73,9 +73,9 @@ m4_define([b4_yy_symbol_print_generate], [static void], [[FILE *], []], [[int yytype], [yytype]], - [[const yy::b4_parser_class_name::semantic_type *yyvaluep], + [[const b4_namespace::b4_parser_class_name::semantic_type *yyvaluep], [yyvaluep]], - [[const yy::b4_parser_class_name::location_type *yylocationp], + [[const b4_namespace::b4_parser_class_name::location_type *yylocationp], [yylocationp]], b4_parse_param)[ { @@ -92,7 +92,7 @@ b4_syncline([@oline@], [@ofile@]) b4_c_ansi_function_decl([yyerror], [static void], - [[yy::b4_parser_class_name::location_type *yylocationp], [yylocationp]], + [[b4_namespace::b4_parser_class_name::location_type *yylocationp], [yylocationp]], b4_parse_param, [[const char* msg], [msg]])]) @@ -107,7 +107,7 @@ b4_syncline([@oline@], [@ofile@])[ ]b4_c_ansi_function_def([yyerror], [static void], - [[yy::b4_parser_class_name::location_type *yylocationp], [yylocationp]], + [[b4_namespace::b4_parser_class_name::location_type *yylocationp], [yylocationp]], b4_parse_param, [[const char* msg], [msg]])[ { @@ -116,7 +116,7 @@ b4_syncline([@oline@], [@ofile@])[ } -namespace yy +namespace ]b4_namespace[ { ]dnl In this section, the parse param are the original parse_params. m4_pushdef([b4_parse_param], m4_defn([b4_parse_param_orig]))dnl @@ -195,7 +195,7 @@ m4_pushdef([b4_parse_param], m4_defn([b4_parse_param_orig]))dnl #endif /* ! YYDEBUG */ ]m4_popdef([b4_parse_param])dnl -[} // namespace yy +[} // namespace ]b4_namespace[ ]]) @@ -203,10 +203,10 @@ m4_pushdef([b4_parse_param], m4_defn([b4_parse_param_orig]))dnl # Let glr.c believe that the user arguments include the parser itself. m4_ifset([b4_parse_param], [m4_pushdef([b4_parse_param], - m4_dquote([[[yy::b4_parser_class_name& yyparser], [[yyparser]]],] + m4_dquote([[[b4_namespace::b4_parser_class_name& yyparser], [[yyparser]]],] m4_defn([b4_parse_param])))], [m4_pushdef([b4_parse_param], - [[[[yy::b4_parser_class_name& yyparser], [[yyparser]]]]]) + [[[[b4_namespace::b4_parser_class_name& yyparser], [[yyparser]]]]]) ]) m4_include(b4_pkgdatadir/[glr.c]) m4_popdef([b4_parse_param]) @@ -227,7 +227,7 @@ b4_copyright([C++ Skeleton parser for GLALR(1) parsing with Bison], /* Using locations. */ #define YYLSP_NEEDED ]b4_locations_flag[ -namespace yy +namespace ]b4_namespace[ { class position; class location; @@ -287,7 +287,7 @@ b4_syncline([@oline@], [@ofile@])[ while (/*CONSTCOND*/ 0) #endif -namespace yy +namespace ]b4_namespace[ { /// A Bison parser. class ]b4_parser_class_name[ @@ -384,10 +384,10 @@ m4_ifset([b4_global_tokens_and_yystype], [b4_token_defines(b4_tokens)]) [ #ifndef YYSTYPE -# define YYSTYPE yy::]b4_parser_class_name[::semantic_type +# define YYSTYPE ]b4_namespace[::]b4_parser_class_name[::semantic_type #endif #ifndef YYLTYPE -# define YYLTYPE yy::]b4_parser_class_name[::location_type +# define YYLTYPE ]b4_namespace[::]b4_parser_class_name[::location_type #endif } diff --git a/data/lalr1.cc b/data/lalr1.cc index 8f0ebe8f..e4c1e6db 100644 --- a/data/lalr1.cc +++ b/data/lalr1.cc @@ -42,7 +42,7 @@ dnl FIXME: This is wrong, we want computed header guards. #include #include "stack.hh" -namespace yy +namespace ]b4_namespace[ { class position; class location; @@ -94,7 +94,7 @@ do { \ } while (false) #endif -namespace yy +namespace ]b4_namespace[ { /// A Bison parser. @@ -291,7 +291,7 @@ b4_error_verbose_if([, int tok])[); #ifndef YYSTYPE /* Redirection for backward compatibility. */ -# define YYSTYPE yy::b4_parser_class_name::semantic_type +# define YYSTYPE b4_namespace::b4_parser_class_name::semantic_type #endif ])[ #endif /* ! defined PARSER_HEADER_H */] @@ -299,7 +299,7 @@ b4_error_verbose_if([, int tok])[); @output @output_parser_name@ b4_copyright([C++ Skeleton parser for LALR(1) parsing with Bison], [2002, 2003, 2004, 2005, 2006]) -m4_if(b4_prefix[], [yy], [], +m4_if(b4_prefix, [yy], [], [ // Take the name prefix into account. #define yylex b4_prefix[]lex]) @@ -370,49 +370,49 @@ do { \ #define YYABORT goto yyabortlab #define YYERROR goto yyerrorlab -#if YYERROR_VERBOSE - -/* Return YYSTR after stripping away unnecessary quotes and - backslashes, so that it's suitable for yyerror. The heuristic is - that double-quoting is unnecessary unless the string contains an - apostrophe, a comma, or backslash (other than backslash-backslash). - YYSTR is taken from yytname. */ -std::string -yy::]b4_parser_class_name[::yytnamerr_ (const char *yystr) +namespace ]b4_namespace[ { - if (*yystr == '"') - { - std::string yyr = ""; - char const *yyp = yystr; - - for (;;) - switch (*++yyp) - { - case '\'': - case ',': - goto do_not_strip_quotes; - - case '\\': - if (*++yyp != '\\') - goto do_not_strip_quotes; - /* Fall through. */ - default: - yyr += *yyp; - break; - - case '"': - return yyr; - } - do_not_strip_quotes: ; - } +#if YYERROR_VERBOSE - return yystr; -} + /* Return YYSTR after stripping away unnecessary quotes and + backslashes, so that it's suitable for yyerror. The heuristic is + that double-quoting is unnecessary unless the string contains an + apostrophe, a comma, or backslash (other than backslash-backslash). + YYSTR is taken from yytname. */ + std::string + ]b4_parser_class_name[::yytnamerr_ (const char *yystr) + { + if (*yystr == '"') + { + std::string yyr = ""; + char const *yyp = yystr; + + for (;;) + switch (*++yyp) + { + case '\'': + case ',': + goto do_not_strip_quotes; + + case '\\': + if (*++yyp != '\\') + goto do_not_strip_quotes; + /* Fall through. */ + default: + yyr += *yyp; + break; + + case '"': + return yyr; + } + do_not_strip_quotes: ; + } + + return yystr; + } #endif -namespace yy -{ /// Build a parser object. ]b4_parser_class_name::b4_parser_class_name[ (]b4_parse_param_decl[) : yydebug_ (false), @@ -1047,7 +1047,7 @@ b4_error_verbose_if([, int tok])[) const unsigned int ]b4_parser_class_name[::yyuser_token_number_max_ = ]b4_user_token_number_max[; const ]b4_parser_class_name[::token_number_type ]b4_parser_class_name[::yyundef_token_ = ]b4_undef_token_number[; -} // namespace yy +} // namespace ]b4_namespace[ ]b4_epilogue dnl @@ -1060,7 +1060,7 @@ b4_copyright([stack handling for Bison C++ parsers], #include -namespace yy +namespace ]b4_namespace[ { template > class stack diff --git a/data/location.cc b/data/location.cc index 6a5b83ea..a3dacac0 100644 --- a/data/location.cc +++ b/data/location.cc @@ -28,7 +28,7 @@ b4_copyright([Position class for Bison C++ parsers], /** ** \file position.hh - ** Define the yy::position class. + ** Define the ]b4_namespace[::position class. */ #ifndef BISON_POSITION_HH @@ -37,7 +37,7 @@ b4_copyright([Position class for Bison C++ parsers], # include # include -namespace yy +namespace ]b4_namespace[ { /// Abstract a position. class position @@ -140,7 +140,7 @@ b4_copyright([Location class for Bison C++ parsers], /** ** \file location.hh - ** Define the yy::location class. + ** Define the ]b4_namespace[::location class. */ #ifndef BISON_LOCATION_HH @@ -150,7 +150,7 @@ b4_copyright([Location class for Bison C++ parsers], # include # include "position.hh" -namespace yy +namespace ]b4_namespace[ { /// Abstract a location. diff --git a/data/yacc.c b/data/yacc.c index 6f847177..d62b8b60 100644 --- a/data/yacc.c +++ b/data/yacc.c @@ -154,7 +154,7 @@ b4_copyright([Skeleton parser for Yacc-like parsing with Bison], USER NAME SPACE" below. */ ]b4_identification -m4_if(b4_prefix[], [yy], [], +m4_if(b4_prefix, [yy], [], [/* Substitute the variable and function names. */ #define yyparse b4_prefix[]parse #define yylex b4_prefix[]lex diff --git a/doc/bison.texinfo b/doc/bison.texinfo index cd5ec423..59d0a76e 100644 --- a/doc/bison.texinfo +++ b/doc/bison.texinfo @@ -515,8 +515,8 @@ int /* @r{keyword `int'} */ square (int x) /* @r{identifier, open-paren, keyword `int',} @r{identifier, close-paren} */ @{ /* @r{open-brace} */ - return x * x; /* @r{keyword `return', identifier, asterisk, - identifier, semicolon} */ + return x * x; /* @r{keyword `return', identifier, asterisk,} + @r{identifier, semicolon} */ @} /* @r{close-brace} */ @end example @end ifinfo @@ -1368,11 +1368,8 @@ under BSD Unix 4.3; each produces a usable, though limited, interactive desk-top calculator. These examples are simple, but Bison grammars for real programming -languages are written the same way. -@ifinfo -You can copy these examples out of the Info file and into a source file -to try them. -@end ifinfo +languages are written the same way. You can copy these examples into a +source file to try them. @menu * RPN Calc:: Reverse polish notation calculator; @@ -2936,15 +2933,6 @@ Usually there is only one action and it follows the components. Multiple rules for the same @var{result} can be written separately or can be joined with the vertical-bar character @samp{|} as follows: -@ifinfo -@example -@var{result}: @var{rule1-components}@dots{} - | @var{rule2-components}@dots{} - @dots{} - ; -@end example -@end ifinfo -@iftex @example @group @var{result}: @var{rule1-components}@dots{} @@ -2953,7 +2941,6 @@ be joined with the vertical-bar character @samp{|} as follows: ; @end group @end example -@end iftex @noindent They are still considered distinct rules even when joined in this way. @@ -4244,12 +4231,14 @@ accurate syntax error messages. @deffn {Directive} %name-prefix="@var{prefix}" Rename the external symbols used in the parser so that they start with @var{prefix} instead of @samp{yy}. The precise list of symbols renamed +in C parsers is @code{yyparse}, @code{yylex}, @code{yyerror}, @code{yynerrs}, @code{yylval}, @code{yylloc}, @code{yychar}, @code{yydebug}, and possible @code{yylloc}. For example, if you use @samp{%name-prefix="c_"}, the names become @code{c_parse}, @code{c_lex}, -and so on. @xref{Multiple Parsers, ,Multiple Parsers in the Same -Program}. +and so on. In C++ parsers, it is only the surrounding namespace which is +named @var{prefix} instead of @samp{yy}. +@xref{Multiple Parsers, ,Multiple Parsers in the Same Program}. @end deffn @ifset defaultprec @@ -6920,7 +6909,7 @@ Same as above, but save in the file @var{defines-file}. @item -b @var{file-prefix} @itemx --file-prefix=@var{prefix} -Pretend that @code{%verbose} was specified, i.e, specify prefix to use +Pretend that @code{%file-prefix} was specified, i.e, specify prefix to use for all Bison output file names. @xref{Decl Summary}. @item -r @var{things} @@ -6970,48 +6959,27 @@ the output graph file. @node Option Cross Key @section Option Cross Key +@c FIXME: How about putting the directives too? Here is a list of options, alphabetized by long option, to help you find the corresponding short option. -@tex -\def\leaderfill{\leaders\hbox to 1em{\hss.\hss}\hfill} - -{\tt -\line{ --debug \leaderfill -t} -\line{ --defines \leaderfill -d} -\line{ --file-prefix \leaderfill -b} -\line{ --graph \leaderfill -g} -\line{ --help \leaderfill -h} -\line{ --name-prefix \leaderfill -p} -\line{ --no-lines \leaderfill -l} -\line{ --no-parser \leaderfill -n} -\line{ --output \leaderfill -o} -\line{ --print-localedir} -\line{ --token-table \leaderfill -k} -\line{ --verbose \leaderfill -v} -\line{ --version \leaderfill -V} -\line{ --yacc \leaderfill -y} -} -@end tex - -@ifinfo -@example ---debug -t ---defines=@var{defines-file} -d ---file-prefix=@var{prefix} -b @var{file-prefix} ---graph=@var{graph-file} -d ---help -h ---name-prefix=@var{prefix} -p @var{name-prefix} ---no-lines -l ---no-parser -n ---output=@var{outfile} -o @var{outfile} ---print-localedir ---token-table -k ---verbose -v ---version -V ---yacc -y -@end example -@end ifinfo +@multitable {@option{--defines=@var{defines-file}}} {@option{-b @var{file-prefix}XXX}} +@headitem Long Option @tab Short Option +@item @option{--debug} @tab @option{-t} +@item @option{--defines=@var{defines-file}} @tab @option{-d} +@item @option{--file-prefix=@var{prefix}} @tab @option{-b @var{file-prefix}} +@item @option{--graph=@var{graph-file}} @tab @option{-d} +@item @option{--help} @tab @option{-h} +@item @option{--name-prefix=@var{prefix}} @tab @option{-p @var{name-prefix}} +@item @option{--no-lines} @tab @option{-l} +@item @option{--no-parser} @tab @option{-n} +@item @option{--output=@var{outfile}} @tab @option{-o @var{outfile}} +@item @option{--print-localedir} @tab +@item @option{--token-table} @tab @option{-k} +@item @option{--verbose} @tab @option{-v} +@item @option{--version} @tab @option{-V} +@item @option{--yacc} @tab @option{-y} +@end multitable @node Yacc Library @section Yacc Library @@ -7066,11 +7034,14 @@ int yyparse (void); @c - Always pure @c - initial action -The C++ parser @acronym{LALR}(1) skeleton is named @file{lalr1.cc}. To select -it, you may either pass the option @option{--skeleton=lalr1.cc} to -Bison, or include the directive @samp{%skeleton "lalr1.cc"} in the +The C++ parser @acronym{LALR}(1) skeleton is named @file{lalr1.cc}. To +select it, you may either pass the option @option{--skeleton=lalr1.cc} +to Bison, or include the directive @samp{%skeleton "lalr1.cc"} in the grammar preamble. When run, @command{bison} will create several -files: +entities in the @samp{yy} namespace. Use the @samp{%name-prefix} +directive to change the namespace name, see @ref{Decl Summary}. The +various classes are generated in the following files: + @table @file @item position.hh @itemx location.hh diff --git a/tests/calc.at b/tests/calc.at index 909b4a6c..dedf568e 100644 --- a/tests/calc.at +++ b/tests/calc.at @@ -73,7 +73,7 @@ static int global_count = 0; static int power (int base, int exponent); ]AT_SKEL_CC_IF( [#ifndef YYLTYPE -# define YYLTYPE yy::location +[#] define YYLTYPE AT_NAME_PREFIX::location #endif #define first_line begin.line #define first_column begin.column @@ -152,7 +152,7 @@ static FILE *input; ]AT_SKEL_CC_IF( [/* A C++ error reporting function. */ void -yy::parser::error (const location& l, const std::string& m) +AT_NAME_PREFIX::parser::error (const location& l, const std::string& m) { (void) l; std::cerr << AT_LOCATION_IF([l << ": " << ])m << std::endl; @@ -161,7 +161,7 @@ yy::parser::error (const location& l, const std::string& m) int yyparse (AT_PARAM_IF([semantic_value *result, int *count])) { - yy::parser parser[]AT_PARAM_IF([ (result, count)]); + AT_NAME_PREFIX::parser parser[]AT_PARAM_IF([ (result, count)]); parser.set_debug_level (!!YYDEBUG); return parser.parse (); } diff --git a/tests/local.at b/tests/local.at index daace07c..3f6de195 100644 --- a/tests/local.at +++ b/tests/local.at @@ -63,7 +63,8 @@ m4_pushdef([AT_PURE_AND_LOC_IF], [$1], [$2])]) m4_pushdef([AT_GLR_OR_PARAM_IF], [m4_bmatch([$3], [%glr-parser\|%parse-param], [$1], [$2])]) - +m4_pushdef([AT_NAME_PREFIX], +[m4_bregexp([$3], [name-prefix="\([^"]*\)"], [\1])]) # yyerror receives the location if %location & %pure & (%glr or %parse-param). m4_pushdef([AT_YYERROR_ARG_LOC_IF], [AT_GLR_OR_PARAM_IF([AT_PURE_AND_LOC_IF([$1], [$2])], @@ -80,7 +81,7 @@ m4_pushdef([AT_YYERROR_SEES_LOC_IF], # are using the C++ parsers. m4_pushdef([AT_PURE_LEX_IF], [AT_PURE_IF([$1], - [AT_LALR1_CC_IF([$1], [$2])])]) + [AT_SKEL_CC_IF([$1], [$2])])]) AT_PURE_LEX_IF( [m4_pushdef([AT_LOC], [(*llocp)]) @@ -120,6 +121,7 @@ m4_popdef([AT_LOC]) m4_popdef([AT_PURE_LEX_IF]) m4_popdef([AT_YYERROR_SEES_LOC_IF]) m4_popdef([AT_YYERROR_ARG_LOC_IF]) +m4_popdef([AT_NAME_PREFIX]) m4_popdef([AT_GLR_OR_PARAM_IF]) m4_popdef([AT_PURE_AND_LOC_IF]) m4_popdef([AT_LOCATION_IF])