[antlr-interest] FW: MACROs in ANTLR

Jim Idle jimi at temporal-wave.com
Thu Oct 25 16:58:22 PDT 2007


Hi Dan,

While I generally don't condone the use of Macros for holding code, there
are always situations where such things are valid of course, and you should
make your own decision as to whether this is such a case ;-)

There are a number of sections available in the C target grammars, some of
which are the same as the Java target, but through necessity, some are C
target specific. In particular, you would want to either define this macro
in the generated .h file, or and I recommend this, just add an #include into
this .h file that includes your own project specific definitions. Here of
course you could also have prototypes for functions, which would then be
naturally available in your actions. Rather than MACROS, you can then make
function calls, which will result in smaller object code for not much
overhead really, but up to you.

The sections you are probably interested in are:

@lexer::includes
{
#include	<mydefs.h>	// Pre declare typedefs, add my MACRO
defs...
}

And @parser::includes

These two insert whatever you place in the {} BEFORE the ANTLR.h includes
etc.

@lexer::postinclude
{
#include	<myctl.h>	// Control structure etc
}

And @parser:postinclude.

These place their contents in the lexer/Parser AFTER the include of the
generated .h file, so you may override anything you don't like ;-)

@lexer::context
{
	pMVB_PARSE_SESSION		ps;				//
Compiler session context 
	ANTLR3_BOOLEAN			haveHeader;		// Set to
TRUE once we have seen a PROGRAM or SUBROUTINE
}

And @parser. The declarations palced here will be added to the thread
friendly context structure for the parser/lexer, so that you may add your
own instance based elements to them.

@lexer::apifuncs
{
   lexCtx->haveHeader = ANTLR3_FALSE;
}

(And @parser, which uses ctx->). This code is placed in the init() method
for your parser or lexer, so here you initialize anything in the context
section.

@lexer::members (and @parser).

Here is where you can add small functions that you only need in the grammar
(however, generally, place these in your own source .c files and include
their declarations in a matching .h file.



Hope this helps you!!!

Jim


> -----Original Message-----
> From: antlr-interest-bounces at antlr.org [mailto:antlr-interest-
> bounces at antlr.org] On Behalf Of Dan Hazon
> Sent: Thursday, October 25, 2007 3:59 PM
> To: antlr-interest at antlr.org
> Subject: [antlr-interest] FW: MACROs in ANTLR
> 
> I pretty new ANTLR user. I have read the definitive reference and using
> the C target language.
> I have a section of code that repeats itself many times in different
> rules with slight modification on the name of one variable. Instead of
> manually instantiating the code and blowing up the size of the .g file
> I'm interested to declare a macro that will reference some ANTLR
> variables and let the code generator instantiate this in the same
> manner
> C compiler does.
> I think I can do it in C by "learning" the generated C code and
> defining
> a C macro that would instantiate the expected code. However this is
> almost as wrong as modifying the machine generated code manually.
> Does ANTLR provide a way to define such macros?
> 
> Here is a simple example to demonstrate my need:
> 
> Section of the grammar:
> exampleCFG
>    :  ('exmpl1' { $eiddef = NULL; $eidref = NULL; }
>       (eiddef=IDENTIFIER)? ('=' eidref=IDENTIFIER)? '{'
>          { MY_DESIRED_MACRO(globalStructPtr, exmpl1Cfg) }
>       cagCFG '}')?
> 
>       ('exmpl2' {$eiddef = NULL; $eidref = NULL; }
>       (eiddef=IDENTIFIER)? ('=' eidref=IDENTIFIER)? '{'
>          { MY_DESIRED_MACRO(globalStructPtr, exmpl2Cfg) }
> 
>       ...
>    ;
> 
> 
> definition of MY_DESIRED_MACRO:
> 
> #define MY_DESIRED_MACRO(pParent, name)\
> if ($eiddef != NULL) {\
>    pParent->DefHash_##name = index;\
>    hashTbl->put(hashTbl, $eiddef.text->chars, index++, NULL);\
> }\
> if ($eidref != NULL) {\
>    pParent->RefHash_##name = hashTbl->get(hashTbl,
> (void*)$eidref.text->chars);\
> }
> 
> 
> 
> the macro replaces every ##name with the actual string given in the
> name
> place.
> 
> I hope I make some sense.
> Appreciate your help,
> Dan




More information about the antlr-interest mailing list