我检查了每个函数Function()
是否以输入文件流中的注释开头。这就像:
SKIP : { " " | "\t" | "\n" | "\r" }
/* COMMENTS */
SPECIAL_TOKEN : { <SINGLE_LINE_COMMENT: "--" (~["\n","\r"])* ("\n"|"\r"|"\r\n")?> }
void Function : {
Token firstToken, id;} {
firstToken=<start> id=<id> "(" ")"
.........
<end>
{ if( firstToken.specialToken == null
|| firstToken.specialToken.kind != COMMENT )
System.out.println("Function " +id.image+
" is not preceded by a comment!" ) ;
} }
所以,我想验证这个评论是否包含保留字。
提前谢谢。
答案 0 :(得分:0)
如果您只是想知道注释是否包含给定的单词,那么您也可以使用Java的字符串搜索机制。假设你想知道在函数定义之前的注释中是否有“bandersnatch”这个词。
void Function() : {
Token firstToken, id;}
{
firstToken=<start> id=<id> "(" ")"
.........
<end>
{ if( firstToken.specialToken == null
|| firstToken.specialToken.kind != COMMENT )
System.out.println("Function " +id.image+
" is not preceded by a comment!" ) ;
else {
String comment = firstToken.specialToken.image ;
boolean hasBandersnatch = comment.indexOf("bandersnatch") != -1 ; if( ! hasBandersnatch )
System.out.println("Function " +id.image+
" is preceded by a comment that does not contain 'bandersnatch'!" ) ; }
}
}
如果您希望搜索不区分大小写,请更改comment
的初始化。
String comment = firstToken.specialToken.image.toLower() ;