Next: , Up: Defining Language Semantics   [Contents][Index]


3.4.1 Data Types of Semantic Values

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_value_type}

The value of api.value.type should be a type name that does not contain parentheses or square brackets.

Alternatively in C, instead of relying of Bison’s %define support, you may rely on the C preprocessor and define YYSTYPE as a macro:

#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 C.