[antlr-interest] lexing nested comments

Sam Harwell sharwell at pixelminegames.com
Wed Dec 3 06:27:29 PST 2008


MULTI_COMMENT
	:	'/*'
		{
			$channel=HIDDEN;
		}
		(	~('/'|'*')
			|	('/' ~'*') => '/'
			|	('*' ~'/') => '*'
			|	MULTI_COMMENT
		)*
		'*/'
	;


-----Original Message-----
From: antlr-interest-bounces at antlr.org
[mailto:antlr-interest-bounces at antlr.org] On Behalf Of micha
Sent: Wednesday, December 03, 2008 4:18 AM
To: antlr-interest at antlr.org
Subject: [antlr-interest] lexing nested comments

Hi,

I don't get the lexer rules correct to lex my nested comments.
As a workaround I use the following, see below. What are the right rules
for 
this ?

thanks
 Michael


------------------------------------------

one lexer rule:

MULTI_COMMENT: '/*'  { nestedComment(0); skip(); }


and in the @members section (my STRINGs don't allow escapes):

	private final void nestedComment(int nest) throws
RecognitionException {
	
		System.err.println("reading Comment nesting level " +
nest);
		while (true) {
			int la = input.LA(1);
			if (la == '/') {
				input.consume();
				la = input.LA(1);
				if (la == '*') {input.consume();
nestedComment(nest+1);}
			}
			else if (la == '*') {
				input.consume();
				la = input.LA(1);
				if (la == '/') {input.consume();
return;}
			}
			else if (la == '"') {
				mSTRING();
			} else
				input.consume(); 
			
		}
	
	}
	

List: http://www.antlr.org/mailman/listinfo/antlr-interest
Unsubscribe:
http://www.antlr.org/mailman/options/antlr-interest/your-email-addr
ess



More information about the antlr-interest mailing list