[antlr-interest] @declarations added to C target output ANTLR3

Jim Idle jimi at temporal-wave.com
Thu Mar 22 15:58:42 PDT 2007


I have added (and pushed to perforce) a new element for rules in the C
target output. You will either need to get hits template and get the
current antlr.jar to use it, or get the latest source code and rebuild
the anltr.jar, or wait until Ter next pushes a build snapshot of the
day. It was trivial to add and I do not expect it to cause any problems
unless you place the worng code in there ;-)

 

This is:

 

@declarations { c declarator.. }

 

This is used in conjunction with @init to avoid issues where C compilers
will not let you declare new variables once you have some code that will
execute.

 

For C targeted output then, you must place ONLY declarations in the
@declarations section and place ONLY code to execute before the rule
starts in @init (such as initialization of values etc).

 

An example, of no particular use in and of itself:

 

grammar gtest;

 

options

{

    language=C;

}

 

tester 

@declarations

{

    char * fred;

}

@init

{

    fred = NULL;

}

: LEXEME+;

 

 

LEXEME

@declarations

{

      int i;

}

@init

{

      i = 0;

}

: 'A'..'Z'+ { printf("did it\n"); } ;

 

Jim

PS: Bug accessing global scopes to be addressed shortly.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20070322/d1aaaa98/attachment-0001.html 


More information about the antlr-interest mailing list