[ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
3.4.2 More Than One Value Type
In most programs, you will need different data types for different kinds
of tokens and groupings. For example, a numeric constant may need type
int
or long int
, while a string constant needs type
char *
, and an identifier might need a pointer to an entry in the
symbol table.
To use more than one data type for semantic values in one parser, Bison requires you to do two things:
-
Specify the entire collection of possible data types. There are several
options:
- let Bison compute the union type from the tags you assign to symbols;
-
use the
%union
Bison declaration (see section The Union Declaration); -
define the
%define
variableapi.value.type
to be a union type whose members are the type tags (see section Providing a Structured Semantic Value Type); -
use a
typedef
or a#define
to defineYYSTYPE
to be a union type whose member names are the type tags.
-
Choose one of those types for each symbol (terminal or nonterminal) for
which semantic values are used. This is done for tokens with the
%token
Bison declaration (see section Token Type Names) and for groupings with the%type
Bison declaration (see section Nonterminal Symbols).
[ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
This document was generated on August 25, 2013 using texi2html 5.0.