Next: , Previous: Java Bison Interface, Up: Java Parsers


10.2.2 Java Semantic Values

There is no %union directive in Java parsers. Instead, the semantic values' types (class names) should be specified in the %type or %token directive:

     %type <Expression> expr assignment_expr term factor
     %type <Integer> number

By default, the semantic stack is declared to have Object members, which means that the class types you specify can be of any class. To improve the type safety of the parser, you can declare the common superclass of all the semantic values using the ‘%define api.value.type’ directive. For example, after the following declaration:

     %define api.value.type {ASTNode}

any %type or %token specifying a semantic type which is not a subclass of ASTNode, will cause a compile-time error.

Types used in the directives may be qualified with a package name. Primitive data types are accepted for Java version 1.5 or later. Note that in this case the autoboxing feature of Java 1.5 will be used. Generic types may not be used; this is due to a limitation in the implementation of Bison, and may change in future releases.

Java parsers do not support %destructor, since the language adopts garbage collection. The parser will try to hold references to semantic values for as little time as needed.

Java parsers do not support %printer, as toString() can be used to print the semantic values. This however may change (in a backwards-compatible way) in future versions of Bison.