[antlr-interest] How to combine tokens around comments without using AST

Jamal Haider syedjamalhaider at gmail.com
Wed Apr 25 03:34:29 PDT 2012


I am a newbie to ANTLR and using it to develop a parser for an ambiguous
language. What I want to do is to some how combine the tokens around the
"comments" into one token without using AST.

I am using this simple grammar to illustrate the problem

grammar test;

query
    :     expression+
    ;

expression
    :   alpha
    ;


alpha
    : ID
    ;


ID  :   ('a'..'z'|'A'..'Z'|'_')*
    ;


COMMENT
    :   '{' ( options {greedy=false;} : . )* '}' {$channel=HIDDEN;}
    ;

Now if we execute it with a simple text "Test{Comments}er" two separate
tokens are generated i.e. "Test" and "er". while I want to create a single
token out of it. Any help will be much appreciated.

Thanks in advance

Jim


More information about the antlr-interest mailing list