1*16467b97STreehugger Robot/// \page atsections Using Sections Within Grammar Files 2*16467b97STreehugger Robot/// 3*16467b97STreehugger Robot/// \section intro Introduction 4*16467b97STreehugger Robot/// 5*16467b97STreehugger Robot/// A C targeted grammar can make use of special annotations within a grammar 6*16467b97STreehugger Robot/// file, which are prefixed with the <b>\@</b> character. These sections cause the 7*16467b97STreehugger Robot/// the placement of their contents within the generated code at defined points 8*16467b97STreehugger Robot/// such as within the generated C header file. 9*16467b97STreehugger Robot/// 10*16467b97STreehugger Robot/// The general form of these annotations is: 11*16467b97STreehugger Robot/// 12*16467b97STreehugger Robot/// \code 13*16467b97STreehugger Robot/// section 14*16467b97STreehugger Robot/// : '@' (( 'parser' | 'lexer' ) '::')? SECTIONNAME '{' yourcode '}' 15*16467b97STreehugger Robot/// ; 16*16467b97STreehugger Robot/// \endcode 17*16467b97STreehugger Robot/// 18*16467b97STreehugger Robot/// If the 'parser' or lexer keywords are left out of the specification, then the 19*16467b97STreehugger Robot/// ANTLR tool assumes a lexer target for a lexer grammar, a parser target for a parser 20*16467b97STreehugger Robot/// or tree parser grammar, and a parser target for a combined lexer/parser grammar. You 21*16467b97STreehugger Robot/// are advised as a matter of course to include the parser or lexer target keyword. 22*16467b97STreehugger Robot/// 23*16467b97STreehugger Robot/// Documentation regarding the \@sections available for a grammar targeted at C now 24*16467b97STreehugger Robot/// follows. 25*16467b97STreehugger Robot/// 26*16467b97STreehugger Robot/// \subsection psrinit Sections \@init and \@declarations 27*16467b97STreehugger Robot/// 28*16467b97STreehugger Robot/// Java targeted grammars allow the special section <code>\@init</code> to be placed after the declaration 29*16467b97STreehugger Robot/// of a rule (lexer, parser and tree parser rules). This allows you to both declare and initialize 30*16467b97STreehugger Robot/// variables that are local to the code generated for that rule. You can then reference them within 31*16467b97STreehugger Robot/// your rule action code. 32*16467b97STreehugger Robot/// 33*16467b97STreehugger Robot/// With the C target, the generated code is subject to the restrictions of C semantics and this 34*16467b97STreehugger Robot/// means that you must declare any local variables, then assign to them afterwards. As well as the 35*16467b97STreehugger Robot/// <code>\@init</code> section, which C programmers should use to initialize their local variables, the C 36*16467b97STreehugger Robot/// target provides the <code>\@declarations</code> section, which is also a rule based section. This section 37*16467b97STreehugger Robot/// is where the C programmer should declare the local variables, thus separating their declaration 38*16467b97STreehugger Robot/// from their initialization. Here is an example: 39*16467b97STreehugger Robot/// 40*16467b97STreehugger Robot/// \code 41*16467b97STreehugger Robot/// translation_unit 42*16467b97STreehugger Robot/// @declarations 43*16467b97STreehugger Robot/// { 44*16467b97STreehugger Robot/// pANTLR3_BOOLEAN hasUsing; 45*16467b97STreehugger Robot/// } 46*16467b97STreehugger Robot/// @init 47*16467b97STreehugger Robot/// { 48*16467b97STreehugger Robot/// 49*16467b97STreehugger Robot/// // Assume no Using directives 50*16467b97STreehugger Robot/// // 51*16467b97STreehugger Robot/// hasUsing = ANTLR3_FALSE; 52*16467b97STreehugger Robot/// 53*16467b97STreehugger Robot/// } 54*16467b97STreehugger Robot/// : rulea ruleb ... 55*16467b97STreehugger Robot/// 56*16467b97STreehugger Robot/// \endcode 57*16467b97STreehugger Robot/// 58*16467b97STreehugger Robot/// Using the <code>\@declarations</code> and <code>\@init</code> sections guarantees that your generated code will 59*16467b97STreehugger Robot/// compile correctly on any standard C compiler (assuming, of course, that you type in valid C code.) 60*16467b97STreehugger Robot/// 61*16467b97STreehugger Robot/// \subsection psrheader \@header section. 62*16467b97STreehugger Robot/// 63*16467b97STreehugger Robot/// The <code>\@parser::header</code> or <code>\@lexer::header</code> annotations cause the code they encapsulate 64*16467b97STreehugger Robot/// to be placed at the start of each generated file, regardless of whether it is a .c or .h file. This can 65*16467b97STreehugger Robot/// be useful for inserting copyright information and so on in all your generated files. 66*16467b97STreehugger Robot/// 67*16467b97STreehugger Robot/// \bNOTE: Be careful not to confuse this concept with placing code in the generated .h header file. The name choice is 68*16467b97STreehugger Robot/// unfortunate, but was already used in the Java target to allow the placement of \c imports statements 69*16467b97STreehugger Robot/// in generated java classes. We have therefore kept the intent of this section the same. 70*16467b97STreehugger Robot/// 71*16467b97STreehugger Robot/// Here is an example: 72*16467b97STreehugger Robot//// 73*16467b97STreehugger Robot/// \code 74*16467b97STreehugger Robot/// @lexer::header 75*16467b97STreehugger Robot/// { 76*16467b97STreehugger Robot/// // Copyright (c) Jim Idle 2007 - All your grammar are belong to us. 77*16467b97STreehugger Robot/// } 78*16467b97STreehugger Robot/// 79*16467b97STreehugger Robot/// @parser::header 80*16467b97STreehugger Robot/// { 81*16467b97STreehugger Robot/// // Copyright (c) Jim Idle 2007 - All your grammar are belong to us. 82*16467b97STreehugger Robot/// } 83*16467b97STreehugger Robot/// \endcode 84*16467b97STreehugger Robot/// 85*16467b97STreehugger Robot/// 86*16467b97STreehugger Robot/// \subsection hdrinclude \@includes section 87*16467b97STreehugger Robot/// 88*16467b97STreehugger Robot/// The <code>\@parser::includes</code> or <code>\@lexer::includes</code> annotations cause 89*16467b97STreehugger Robot/// the code they encapsulate to be placed in the generated .h file, \b after the standard 90*16467b97STreehugger Robot/// includes required by the ANTLR generated code. 91*16467b97STreehugger Robot/// 92*16467b97STreehugger Robot/// Here you could for instance place a <code>\#include</code> 93*16467b97STreehugger Robot/// statement to cause your grammar code to include some standard definitions. Because you 94*16467b97STreehugger Robot/// may use multiple parsers and lexers in your solution, you should probably not place 95*16467b97STreehugger Robot/// <code>#define</code> statements here, but in the <code>\@postinclude</code> section. Then you 96*16467b97STreehugger Robot/// may create different <code>\#defines</code> for different recognizers. 97*16467b97STreehugger Robot/// 98*16467b97STreehugger Robot/// Here is an example: 99*16467b97STreehugger Robot//// 100*16467b97STreehugger Robot/// \code 101*16467b97STreehugger Robot/// @lexer::includes 102*16467b97STreehugger Robot/// { 103*16467b97STreehugger Robot/// #include "myprojectcommondefs.h" 104*16467b97STreehugger Robot/// } 105*16467b97STreehugger Robot/// 106*16467b97STreehugger Robot/// @parser::includes 107*16467b97STreehugger Robot/// { 108*16467b97STreehugger Robot/// #include "myprojectcommondefs.h" 109*16467b97STreehugger Robot/// } 110*16467b97STreehugger Robot/// \endcode 111*16467b97STreehugger Robot/// 112*16467b97STreehugger Robot/// 113*16467b97STreehugger Robot/// \subsection hdrpreinclude \@preincludes section 114*16467b97STreehugger Robot/// 115*16467b97STreehugger Robot/// The <code>\@parser::preincludes</code> or <code>\@lexer::preincludes</code> annotations cause 116*16467b97STreehugger Robot/// the code they encapsulate to be placed in the generated .h file, \b before the standard 117*16467b97STreehugger Robot/// includes required by the ANTLR generated code. 118*16467b97STreehugger Robot/// 119*16467b97STreehugger Robot/// You should use this section when you wish to place #defines and other definitions 120*16467b97STreehugger Robot/// in the code before the standard ANTLR runtime includes defined them. This allows you 121*16467b97STreehugger Robot/// to override any predefined symbols and options that the includes otherwise take 122*16467b97STreehugger Robot/// defaults for. For instance, if you have built a version of the runtime with a 123*16467b97STreehugger Robot/// special version of malloc, you can <code>\#define</code> #ANTLR3_MALLOC to match the definition 124*16467b97STreehugger Robot/// you used for the ANTLR runtime library. 125*16467b97STreehugger Robot/// 126*16467b97STreehugger Robot/// \subsection hdrpostinclude \@postinclude section 127*16467b97STreehugger Robot/// 128*16467b97STreehugger Robot/// The <code>\@parser::postinclude</code> or <code>\@lexer::postinclude</code> annotations cause 129*16467b97STreehugger Robot/// the code they encapsulate to be placed in the generated <b>.C</b> file, after the generated include 130*16467b97STreehugger Robot/// file (which includes the standard ANTLR3C library includes. 131*16467b97STreehugger Robot/// 132*16467b97STreehugger Robot/// Code you place here then will be subject to any macros defined by your own includes, by the 133*16467b97STreehugger Robot/// generated include and by the standard ANTLR3 includes. This is a good place to <code>\#undef</code> 134*16467b97STreehugger Robot/// anything that you don;t like the default values of, but cannot override before the includes 135*16467b97STreehugger Robot/// define them. 136*16467b97STreehugger Robot/// 137*16467b97STreehugger Robot/// This is also a good place to <code>#define</code> any macros you may wish to use in the generated 138*16467b97STreehugger Robot/// .c file. As you can include multiple parsers in your projects, you will need to include the 139*16467b97STreehugger Robot/// generated .h file of each of them, possibly globally, but almost certainly in a context where you 140*16467b97STreehugger Robot/// are including more than one .h file simultaneously. Hence if you commonly use the same macro 141*16467b97STreehugger Robot/// names for accessing structures and so on, and they change from grammar to grammar, you should 142*16467b97STreehugger Robot/// define them here to avoid creating conflicting definitions in the header files. 143*16467b97STreehugger Robot///