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-12 Akim Demaille <akim@lrde.epita.fr>
+
+ 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 <akim@lrde.epita.fr>
* TODO: Remove dead items.
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:
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)
]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
[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)[
{
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]])])
]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]])[
{
}
-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
#endif /* ! YYDEBUG */
]m4_popdef([b4_parse_param])dnl
-[} // namespace yy
+[} // namespace ]b4_namespace[
]])
# 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])
/* Using locations. */
#define YYLSP_NEEDED ]b4_locations_flag[
-namespace yy
+namespace ]b4_namespace[
{
class position;
class location;
while (/*CONSTCOND*/ 0)
#endif
-namespace yy
+namespace ]b4_namespace[
{
/// A Bison parser.
class ]b4_parser_class_name[
[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
}
#include <iostream>
#include "stack.hh"
-namespace yy
+namespace ]b4_namespace[
{
class position;
class location;
} while (false)
#endif
-namespace yy
+namespace ]b4_namespace[
{
/// A Bison parser.
#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 */]
@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])
#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),
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
#include <deque>
-namespace yy
+namespace ]b4_namespace[
{
template <class T, class S = std::deque<T> >
class stack
/**
** \file position.hh
- ** Define the yy::position class.
+ ** Define the ]b4_namespace[::position class.
*/
#ifndef BISON_POSITION_HH
# include <iostream>
# include <string>
-namespace yy
+namespace ]b4_namespace[
{
/// Abstract a position.
class position
/**
** \file location.hh
- ** Define the yy::location class.
+ ** Define the ]b4_namespace[::location class.
*/
#ifndef BISON_LOCATION_HH
# include <string>
# include "position.hh"
-namespace yy
+namespace ]b4_namespace[
{
/// Abstract a location.
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
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
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;
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{}
;
@end group
@end example
-@end iftex
@noindent
They are still considered distinct rules even when joined in this way.
@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
@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}
@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
@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
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
]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;
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 ();
}
[$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])],
# 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)])
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])