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


3.4.5 Providing a Structured Semantic Value Type

Instead of %union, you can define and use your own union type YYSTYPE if your grammar contains at least one ‘<type>’ tag. For example, you can put the following into a header file parser.h:

union YYSTYPE {
  double val;
  symrec *tptr;
};

and then your grammar can use the following instead of %union:

%{
#include "parser.h"
%}
%define api.value.type {union YYSTYPE}
%nterm <val> expr
%token <tptr> ID

Actually, you may also provide a struct rather that a union, which may be handy if you want to track information for every symbol (such as preceding comments).

The type you provide may even be structured and include pointers, in which case the type tags you provide may be composite, with ‘.’ and ‘->’ operators.