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

Johannes Luber jaluber at gmx.de
Wed Jun 13 02:39:00 PDT 2007


Jim Loverde wrote:
> 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?

I have a similar problem with C#'s preprocessor directives. Only at the
beginning of the line or only whitespace may be in front of "#". My
solution is to use a boolean variable, which is reset at the NEW_LINE token:

NEW_LINE
   :    { AllowPPDirective(); } '\u000D' // Carriage return character
   |    { AllowPPDirective(); } '\u000A' // Line feed character
   |    { AllowPPDirective(); } '\u000D\u000A'  // Carriage return
character followed by line feed character
   |    { AllowPPDirective(); } '\u0085' // Next line character
   |    { AllowPPDirective(); } '\u2028' // Line separator character
   |    { AllowPPDirective(); } '\u2029' // Paragraph separator character
   ;

Then at every non-fragment rule I put:

SINGLE_LINE_COMMENT
   :    { DisallowPPDirective(); } '//' INPUT_CHARACTER*
   ;

And I do the testing like:

PP_DEFINE
   :    { isPPDirectiveAllowed }? '#' WHITESPACE? 'define'
   ;

Unfortunately I couldn't get my parser running due to unrelated
problems, so I can't vouch that this solution will actually work. On the
other hand, there isn't anything what would make me doubt my solution's
capabilities.

Best regards,
Johannes Luber


More information about the antlr-interest mailing list