[antlr-interest] Re: C++ Comments (Digest 11/49)

David Wigg wiggjd at lsbu.ac.uk
Fri Oct 28 05:07:33 PDT 2005


Henry Butowsky asked about parsing C++ style comments.

Message: 3
Date: Thu, 27 Oct 2005 14:29:14 +0100
From: Henry Butowsky <henryb at ntlworld.com>
Subject: [antlr-interest] C++ Comments
To: antlr-interest at antlr.org
Message-ID: <4360D62A.7020008 at ntlworld.com>
Content-Type: text/plain; charset=us-ascii; format=flowed

Hi Guys I'm using the following rule in my Lexer to skip C++ 
style comments

CPP_COMMENT: ("//" (~'\n')* '\n')
	{ newline(); $setType(antlr::Token::SKIP); };

Upon executing this rule the lexer is "aborted" ( in
CalcLexer::nextToken() )

when I use the string ("//" (~'\n')*) it deletes all text to the 
end-of file ? There is some problem with the '\n' -- Any ideas ?
Many thx Henry


FYI I use the following in our C++ parser available from the 
antlr website,


CPPComment
    :	
        "//" (~('\n' | '\r'))* EndOfLine
        {_ttype = ANTLR_USE_NAMESPACE(antlr)Token::SKIP;
         newline();}
    ;

protected
EndOfLine
    :	
        (options{generateAmbigWarnings = false;}:
         "\r\n"  // MS
        |'\r'    // Mac
        |'\n'    // Unix
        )
    ;

David.



More information about the antlr-interest mailing list