[antlr-interest] Nested Multi-Line Commentary

John B. Brodie jbb at acm.org
Fri Aug 15 15:09:57 PDT 2008


For the record, my problem is indeed caused by an interaction between
two lexer rules.

I wish that the error message was more indicative of that situation...

Here is a grammar that displays the issue:

//-------------------------
grammar Comment;

foo : VAR* EOF ;

// variable name
VAR : ( 'a'..'z' | SYMBOL ) PRINTABLE* ;
//VAR : ( 'a'..'z' PRINTABLE* ) | SYMBOL ;

fragment PRINTABLE : 'a'..'z' | 'A'..'Z' | '0'..'9' | SYMBOL ;

fragment SYMBOL
    : '+' | '-' | '*' | '/' | '=' | ':' | '<' | '>' | '_' | '\''
    | '!' | '?' | '|' | '&' | '^' | '%' | '$' | '#' | '@' | '~' | '`'
    ;

// Whitespace -- ignored
WS
   :  ( ' ' | '\t' |    '\f'
      | ( '\r' | '\n' ) // handle newlines
      )+
      { $channel=HIDDEN; }
   ;

// single-line comments
SL_COMMENT
    : '//'
        ( options { greedy=false; } : . )*
        ( ( '\r' '\n'? ) | '\n' )
        { $channel=HIDDEN; }
    ;

// nestable multiple-line comments
ML_COMMENT : FRAG_ML_COMMENT { $channel=HIDDEN; } ;

fragment FRAG_ML_COMMENT :
        '/*'
        ( options { greedy=false; } : (('/*')=>FRAG_ML_COMMENT) | . )*
        '*/'
   ;
//-------------------------

note that changing the VAR rule to the one commented above caused the
error to go away.

Thankx
   -jbb


More information about the antlr-interest mailing list