[antlr-interest] Calling a function on token match

Burton Samograd burton.samograd at markit.com
Wed Jul 11 11:01:58 PDT 2012


Hello,

I am trying to call some code every time a token matches ('\n' in this case).  My naive attempt is:

fragment
NEWLINE
    : '\n' { std::cerr << "NEWLINE\n"; }
    ;

Giving:

error(100): SDL.g:0:0: syntax error: buildnfa: <AST>:913:12: expecting EOA, found ' std::cerr << "NEWLINE\n";

I found this related post:

http://www.antlr.org/pipermail/antlr-interest/2009-February/032921.html

But I'm not fluent enough in antlr to understand exactly what the solution entails.  In our grammar we have a number of places to handle whitespace like ( ' ' | '\t' | '\r' | '\n' ) which I am hoping that with the above NEWLINE rule can then be replaced with ( ' ' | '\t' | '\r' | NEWLINE ).  I want to update an external line count variable using a function in the code to be called on token match.

Is what I'm trying to do possible?  If so, could someone show me an example of how to call code on a token match.

Thank you.

--
Burton Samograd

________________________________
This e-mail, including accompanying communications and attachments, is strictly confidential and only for the intended recipient. Any retention, use or disclosure not expressly authorised by Markit is prohibited. This email is subject to all waivers and other terms at the following link: http://www.markit.com/en/about/legal/email-disclaimer.page

Please visit http://www.markit.com/en/about/contact/contact-us.page? for contact information on our offices worldwide.


More information about the antlr-interest mailing list