[antlr-interest] 2 C-target questions

Gavin Lambert antlr at mirality.co.nz
Wed Oct 3 03:01:38 PDT 2007


At 22:22 3/10/2007, Corno Schraverus wrote:
>If I have labeled a token (like this: myToken=IDENT ), how can I 
>then get the text of that token? (similar like myToken->getText() 
>for C++ in ANTLR2).

Just use $myToken.text.  The codegen should translate that into 
the appropriate bit of code no matter what target language you're 
using.

>The following construct used to work in antlr2, in antlr3 it 
>doesn't. The variable x is placed in a nested block in the 
>generated code and is therefore not accessible outside that block.
>Is this an error on my side, a bug in antlr or a bug in the 
>c-stringtemplate?
[...]
>test
>:
>       {
>             char x;
>       }
>       (
>             'a' { x = 'a'; }

It's more of a quirk :)  You need to use something like this (I 
forget the exact syntax, but it's similar):

test:
   @decl {
     char x;
   }
   ( ...

Using @decl tells ANTLR that you're declaring variables rather 
than trying to insert arbitrary blocks of code.  Similarly you can 
use @init to insert some code specifically before rule processing 
begins (but too late to declare variables).



More information about the antlr-interest mailing list