[antlr-interest] lexing nested comments

micha micha-1 at fantasymail.de
Wed Dec 3 02:17:30 PST 2008


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(); 
			
		}
	
	}
	


More information about the antlr-interest mailing list