[antlr-interest] Reg Multi-line comments

Michael micha-1 at fantasymail.de
Thu Jul 16 04:35:00 PDT 2009


Am Thursday 16 July 2009 12:14:55 schrieb Gokulakannan Somasundaram:
> Hi,
>    I am trying to filter out multi-line comments, for which i am using the
> following Token definition (provided in antlr.org)
> ML_COMMENT
>
>     :    '/*' ( options { greedy = false; } : .* ) '*/' { skip(); };
>

I have done it like this:
in the lexer I added a member openComment:

@member {
	boolean openComment = false;

	//override nextToken:
	@Override
	public Token nextToken() {
		Token t = super.nextToken();
		if (t == Token.EOF_TOKEN && openComment) {
			System.err.println("gotcha!");
		}
		return t;
	}

}


change your comment rule :

ML_COMMENT
@init { openComment = true: }
    :    '/*' ( options { greedy = false; } : .* ) '*/' { openComment = false; 
skip(); };




don't know if this is nice, but it works :-)
 Michael



More information about the antlr-interest mailing list