luo*_*ivo 2 java parsing comments javacc parser-generator
我知道如何使用SKIP声明跳过这些注释,但我需要做的就是获取C源并输出相同的源而不带注释.
所以我声明了一个令牌<GENERIC_TEXT:(〜[])+>,它被复制到输出中,并且不会跳过注释.我怀疑这个令牌为自己获取所有输入.
有谁可以帮助我吗?
谢谢
不要使用(~[])+:它会吞噬你所有的输入.这可能就是为什么你没有看到令牌被跳过的原因.
在默认词法分析器模式下,遇到"/*"(多行注释的开头)时更改为其他状态.在这个不同的统计中,要么匹配"*/"(并且回到默认的词法分析器状态),要么匹配任何字符~[](不是(~[])+!).
快速演示:
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>
}
Run Code Online (Sandbox Code Playgroud)
鉴于测试文件:
/*
* comments
*/
class Test {
// more comments
int foo() {
return 42;
}
}
Run Code Online (Sandbox Code Playgroud)
像这样运行演示(假设您在同一目录中有文件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;
}
}
Run Code Online (Sandbox Code Playgroud)
(暂无评论)
请注意,您仍然需要考虑可能如下所示的字符串文字:
"the following: /*, is not the start of a comment"
Run Code Online (Sandbox Code Playgroud)
和字面文字:
'"' // not the start of a string literal!
Run Code Online (Sandbox Code Playgroud)