我知道如何使用SKIP声明跳过这些注释,但我需要做的就是获取C源并输出相同的源而不注释。
所以我声明了一个令牌< GENERIC_TEXT:(〜[])+>,它被复制到输出中,并且不会跳过注释。我怀疑这个令牌会为自己获取所有输入。
有人可以帮助我吗?
谢谢
答案 0 :(得分:5)
不要使用(~[])+
:它会吞噬你的所有输入。这可能就是为什么你没有看到令牌被跳过的原因。
在默认词法分析器模式下,遇到"/*"
(多行注释的开头)时,请更改为其他状态。在这个不同的统计数据中,要么匹配"*/"
(并且回到默认的词法分析状态),要么匹配任何字符~[]
(不是(~[])+
!)。
快速演示:
PARSER_BEGIN(CommentStripParser)
public class CommentStripParser {
public static void main(String[] args) throws Exception {
java.io.FileInputStream file = new java.io.FileInputStream(new java.io.File(args[0]));
CommentStripParser parser = new CommentStripParser(file);
parser.parse();
}
}
PARSER_END(CommentStripParser)
TOKEN :
{
< OTHER : ~[] >
}
SKIP :
{
< "//" (~["\r", "\n"])* >
| < "/*" > : ML_COMMENT_STATE
}
<ML_COMMENT_STATE> SKIP :
{
< "*/" > : DEFAULT
| < ~[] >
}
void parse() :
{
Token t;
}
{
( t=<OTHER> {System.out.print(t.image);} )* <EOF>
}
鉴于测试文件:
/*
* comments
*/
class Test {
// more comments
int foo() {
return 42;
}
}
像这样运行演示(假设你有文件 CommentStripParser.jj , Test.java 和JAR javacc.jar 同一目录):
java -cp javacc.jar javacc CommentStripParser.jj javac -cp . *.java java -cp . CommentStripParser Test.java
以下内容将打印到您的控制台:
class Test {
int foo() {
return 42;
}
}
(暂无评论)
请注意,您仍然需要考虑可能如下所示的字符串文字:
"the following: /*, is not the start of a comment"
和char literals:
'"' // not the start of a string literal!