[antlr-interest] Nested Multi-Line Commentary

John B. Brodie jbb at acm.org
Thu Aug 14 18:16:12 PDT 2008


Greetings!

I'd like to permit nested multi line commentary in my grammar, e.g:

/*
foo
/*
bar
*/
baz
*/
<EOF here>

should be legal, because each opening /* is matched by a closing */

and

/*
foo
/*
bar
*/
<EOF here>

is illegal, because a closing */ is missing.

So, well, I tried these two Lexer rules:

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

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

and I get this error message from ANTLR v3.1:

ANTLR Parser Generator  Version 3.1 (August 12, 2008)  1989-2008
error(206): Lambda.g:1:8: Alternative 15: after matching input such as '/''*''/''*''/''*''/''*''/'<EOT> decision cannot predict what comes next due to recursion overflow to FRAG_ML_COMMENT from FRAG_ML_COMMENT

can you please help me correct this?

I kinda thought that, while infinite recursion is possible, the
syntactic predicate would help resolve whether or not to recurse.

It is acceptable that when any closing */ are missing the Lexer would
not detect that error(s) until the EOF is encountered. e.g. when a */
is missing, the rest of the input file is treated as comments and
an error should be reported at the end of file.

Thankx
   -jbb


More information about the antlr-interest mailing list