[antlr-interest] Antlr v3: Custom actions and custom tokens

Chris Wood chriswood at iname.com
Fri Apr 13 04:41:21 PDT 2007


Terence,

>> Are there any docs or posts explaining how to implement and then
>> use custom actions (to modify a returned token) in ANTLR v3? I
>> assume that a custom token class will be required and that this
>> must somehow be fitted into the grammar in question. I have
>> searched for advice across the ANTLR site without much success so far!
>>
>> Thanks a lot in advance for any pointers.
>
> I added a FAQ entry:
>
> http://www.antlr.org/wiki/pages/viewpage.action?pageId=1844

Thanks a lot for adding that - that works great for creating a custom token 
class, though I used a full signature for the emit method, i.e. emit(int 
tokenType, int line, int charPosition, int channel, int start, int stop) 
rather than emit().


Follow-up question:

I remain confused about how to use actions with the new custom token type.

Specifically, what I am trying to do is alter the example Python grammar 
(now outputting AST) so that multiple colno and lineno pairs can be carried 
in a single new token.

In the grammar I have:

atom: ...
    | LBRACK (listmaker)? RBRACK -> ^(List (listmaker)?)
      ...

and would like List to be annotated with the colno and lineno data from 
both LBRACK and RBRACK.

I thought that to do this I could create a custom token type (as solved 
above) with additional fields and then use an action to populate those 
fields from the attributes of LBRACK and RBRACK. But I am unsure how to use 
any custom actions. Where and how should they be added?

Or am I going about this completely the wrong way? Thanks again for any 
advice.

Regards,

Chris Wood






More information about the antlr-interest mailing list