[antlr-interest] nested comments

Elden Crom eldencrom at comcast.net
Mon Jul 4 09:45:39 PDT 2005



This isn’t the prettiest thing but it seems to work…..
(Nesting is only applied to ML comments ie /* comment /* nested comment 
*/ still a comment */)

{...
int ML_depth=0;
...
}
// nested multiple-line comments
ML_COMMENT
: "/*" {ML_depth=1;}
( /* '\r' '\n' can be matched in one alternative or by matching
'\r' in one iteration and '\n' in another. I am trying to
handle any flavor of newline that comes in, but the language
that allows both "\r\n" and "\r" and "\n" to all be valid
newline is ambiguous. Consequently, the resulting grammar
must be ambiguous. I'm shutting this warning off.
*/
options {
generateAmbigWarnings=false;
}
:
{ LA(2)!='/' }? '*'
| { LA(2)=='/' && ML_depth!=1 }? '*' {ML_depth--;} //remove one nesting 
level
| "/*" {ML_depth++;}
| '\r' '\n' {newline();}
| '\r' {newline();}
| '\n' {newline();}
| ~('*'|'\n'|'\r')
)*
//{ ML_depth==1 && p("exiting ML_depth="+ML_depth);}? "*/"
"*/"
{$setType(Token.SKIP);}
;




More information about the antlr-interest mailing list