[antlr-interest] Question with greedy

Gordon Tyler Gordon.Tyler at quest.com
Thu Sep 24 06:20:38 PDT 2009


To exclude the /* and */ would something like this work?

COMMENT
 	:  '/*'! ( options {greedy=false;} : . )* '*/'!
 	;

-----Original Message-----
From: antlr-interest-bounces at antlr.org [mailto:antlr-interest-bounces at antlr.org] On Behalf Of Andreas Volz
Sent: September 23, 2009 5:36 PM
To: antlr-interest at antlr.org
Subject: [antlr-interest] Question with greedy

Hello,

I wrote this grammar:

startrule
	: (property comment property)*
	;
comment
	: COMMENT { printf("Comment: \%s\n", $COMMENT.text->chars); }
	;
	
COMMENT
 	:  '/*' ( options {greedy=false;} : . )* '*/'
 	;
	
property
	: TOKEN { printf("Property: \%s\n", $TOKEN.text->chars);}

TOKEN
	: (ALPHA | DIGIT)+

fragment DIGIT  	
	: '0'..'9'
	;
	
fragment ALPHA
	: 'a'..'z' | 'A'..'Z' |'@'|'.'| ' ' 
	;

The input is:

This is a test /* with a comment */ in the middle
This is a test /* with a comment */ in the middle
This is a test /* with a comment */ in the middle

The result looks good, but some errors are print out:

test.txt(1) : lexer error 3 :
	 at offset 49, near char(0XA) :
	
This is a test /* w
test.txt(2) : lexer error 3 :
	 at offset 50, near char(0XA) :
	
This is a test /* w
test.txt(3) : lexer error 3 :
	 at offset 50, near char(0XA) :
	

Property: This is a test 
Comment: /* with a comment */
Property:  in the middle
Property: This is a test 
Comment: /* with a comment */
Property:  in the middle
Property: This is a test 
Comment: /* with a comment */
Property:  in the middle

BTW: The line ending in this file is 0x0A.

Could anyone explain this error and how to prevent it?

Second question: How do I not include the '/*' and '*/' tags in the
comment match?

regards
	Andreas

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


More information about the antlr-interest mailing list