[antlr-interest] How to handle first non whitespace commentcharacters?

Jim Idle jimi at temporal-wave.com
Wed Jun 13 08:53:53 PDT 2007


You might find this helpful (C output but Java is very similar of course, just getCharPositionInLine() I think rather than the upper case MACRO and put declaration and initialization in the @init rule.):

OPMULT
@declarations
{
	ANTLR3_UINT32	lno;
}
@init 
{ 
	lno = GETCHARPOSITIONINLINE(); 
}
	: '*'
	    (	  {lno==0}?=> (~('\r' | '\n'))* EOL_SEQUENCE+
							{	$channel=2; $type = COMMENT;	}
	      | '='		    {	$type = OPMULT_ASSIGN;		}
		|
	    )
	;

Jim


> -----Original Message-----
> From: antlr-interest-bounces at antlr.org [mailto:antlr-interest-
> bounces at antlr.org] On Behalf Of Jim Loverde
> Sent: Wednesday, June 13, 2007 1:20 AM
> To: antlr-interest at antlr.org
> Subject: [antlr-interest] How to handle first non whitespace
> commentcharacters?
> 
> Hello,
> 
> I was hoping someone could help me out, as I've been struggling with
> this for a bit.  Basically, how do you handle something like a COBOL
> asterisk comment character, where it is only a valid line comment
> indicator if it is the first non whitespace character on a line (but if
> it's part of an expression such as 5 * 7 it shouldn't be treated as a
> line comment character).
> 
> For example:
> 
> * This is a line comment
> IF 5 * 5 = 20 THEN
>     * this is also a line comment
>     NOTHING
> END-IF.
> 
> Ideally I'd like to have the line comments channeled to the hidden
> channel similar to the "normal" line comment such as:
> 
> LINE_COMMENT
>     :    '!' ~('\n'|'\r')*  '\r'? '\n' {$channel=HIDDEN;}
> 
> Also, while on the topic, does anyone have a COBOL grammar for ANTLR
> (or even a partial grammar) that they can share?
> 
> Thanks in advance for any help.
> 



More information about the antlr-interest mailing list