[ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
3.7.14 %code Summary
The %code
directive inserts code verbatim into the output
parser source at any of a predefined set of locations. It thus serves
as a flexible and user-friendly alternative to the traditional Yacc
prologue, %{code%}
. This section summarizes the
functionality of %code
for the various target languages
supported by Bison. For a detailed discussion of how to use
%code
in place of %{code%}
for C/C++ and why it
is advantageous to do so, see section Prologue Alternatives.
- Directive: %code {code}
This is the unqualified form of the
%code
directive. It inserts code verbatim at a language-dependent default location in the parser implementation.For C/C++, the default location is the parser implementation file after the usual contents of the parser header file. Thus, the unqualified form replaces
%{code%}
for most purposes.For Java, the default location is inside the parser class.
- Directive: %code qualifier {code}
This is the qualified form of the
%code
directive. qualifier identifies the purpose of code and thus the location(s) where Bison should insert it. That is, if you need to specify location-sensitive code that does not belong at the default location selected by the unqualified%code
form, use this form instead.
For any particular qualifier or for the unqualified form, if there are
multiple occurrences of the %code
directive, Bison concatenates
the specified code in the order in which it appears in the grammar
file.
Not all qualifiers are accepted for all target languages. Unaccepted qualifiers produce an error. Some of the accepted qualifiers are:
requires
-
- Language(s): C, C++
- Purpose: This is the best place to write dependency code required for
YYSTYPE
andYYLTYPE
. In other words, it’s the best place to define types referenced in%union
directives. If you use#define
to override Bison’s defaultYYSTYPE
andYYLTYPE
definitions, then it is also the best place. However you should rather%define
api.value.type
andapi.location.type
. - Location(s): The parser header file and the parser implementation file
before the Bison-generated
YYSTYPE
andYYLTYPE
definitions.
provides
-
- Language(s): C, C++
- Purpose: This is the best place to write additional definitions and declarations that should be provided to other modules.
- Location(s): The parser header file and the parser implementation
file after the Bison-generated
YYSTYPE
,YYLTYPE
, and token definitions.
top
-
- Language(s): C, C++
- Purpose: The unqualified
%code
or%code requires
should usually be more appropriate than%code top
. However, occasionally it is necessary to insert code much nearer the top of the parser implementation file. For example:%code top { #define _GNU_SOURCE #include <stdio.h> }
- Location(s): Near the top of the parser implementation file.
imports
-
- Language(s): Java
- Purpose: This is the best place to write Java import directives.
- Location(s): The parser Java file after any Java package directive and before any class definitions.
Though we say the insertion locations are language-dependent, they are technically skeleton-dependent. Writers of non-standard skeletons however should choose their locations consistently with the behavior of the standard Bison skeletons.
[ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
This document was generated on December 1, 2013 using texi2html 5.0.