Next: Multiple Types, Up: Semantics [Contents][Index]
In a simple program it may be sufficient to use the same data type for the semantic values of all language constructs. This was true in the RPN and infix calculator examples (see Reverse Polish Notation Calculator).
Bison normally uses the type int
for semantic values if your
program uses the same data type for all language constructs. To
specify some other type, define the %define
variable
api.value.type
like this:
%define api.value.type {double}
or
%define api.value.type {struct semantic_type}
The value of api.value.type
should be a type name that does not
contain parentheses or square brackets.
Alternatively, instead of relying of Bison’s %define
support, you may
rely on the C/C++ preprocessor and define YYSTYPE
as a macro, like
this:
#define YYSTYPE double
This macro definition must go in the prologue of the grammar file
(see Outline of a Bison Grammar). If compatibility
with POSIX Yacc matters to you, use this. Note however that Bison cannot
know YYSTYPE
’s value, not even whether it is defined, so there are
services it cannot provide. Besides this works only for languages that have
a preprocessor.