[antlr-interest] File comments aka. unclosed comments

Austin Hastings Austin_Hastings at Yahoo.com
Tue Oct 30 09:28:45 PDT 2007


Ramon,

If you check the recent discussions on this list, you'll find that once 
the lexer commits to a path
(closed or unclosed comment, in this case) it will not backtrack and try 
the other - instead, it will
throw an exception if it cannot match.

I think you'd do well to go with your standard "closed comment" and either:

1. Match EOF as a valid comment-end-delimiter, in addition to '*/'; or

2. Match only '*/' at the end, but modify the code (either manually, or 
using the catch mechanism built in to ANTLR to catch the exception 
thrown when EOF fails to match a closed comment.

=Austin


Ramon Verbruggen wrote:
> I have spent quite some time trying to figure this out, and searched the
> internet extensively (including the antlr mailing list archives) but
> could not find anything related, so as a last resort I am posting this
> here.
>
> I am trying to replace our current parser (ancient YACC++) with an
> ANTLR generated parser and really want to maintain complete backwards
> compatibility.
>
> For reasons that are lost in time, our language (Quill) supports
> unclosed comments (or comments until the end of file), so apart from the
> normal (albeit nested) block comment '/* ... */', just inserting '/*' in
> a code body effectively comments out the rest of the code in the file.
>
> I currently have something like this:
>
> Q_Comment_nested
> 	:	'/*' ( options {greedy=false;} : Q_Comment_nested | .)*
> '*/' {$channel=HIDDEN;}
> 	;
>
> Q_Unclosed_comment
> 	:	'/*' ( options {greedy=false;} : ~'*/')* EOF
> {$channel=HIDDEN;}
> 	;
>
> The above (obviously) reports an unreachable alternative and a
> recursive overflow because the unclosed comment matches anything that
> the nested comment matches as well.
>
> I have tried many different solutions like:
>
> Q_Comment_nested
> 	:	'/*' ( options {greedy=false;} : Q_Comment_nested | .)*
> ( '*/' | EOF) {$channel=HIDDEN;}
> 	;
>
> or:
>
> Q_Comment_nested
> 	:	'/*' ( options {greedy=false;} : (~'*/'))* '*/'?
> {$channel=HIDDEN;}
> 	;
>
> Whatever I tried, I just cannot seem to get it right!
>
> I am open to suggestions ( I know, 'comments are ugly', 'use source
> control or ifdef instead of comments' etc., but this should be possible,
> right?!)
>
> Ramon Verbruggen
>
> Join Quintessence 07 - the annual global Quintiq conference on November
> 20th! www.quintiq.com/quintessence
>
>
> This message contains information that may be privileged or confidential
> and is the property of Quintiq. It is only intended for the person to
> whom it is addressed. If you are not the intended recipient, you are not
> authorized to read, print, retain, copy, disseminate, distribute or use
> this message or any part thereof. If you have received this message in
> error, please notify the sender immediately and delete all copies of
> this message. Please note that e-mails are susceptible to change,
> therefore they are not binding.
>
>
>   



More information about the antlr-interest mailing list