JavaCC生成死代码
我第一次使用JavaCC,我注意到它产生了很多死代码。有许多行看起来像(请原谅间距,它是自动的): 是否可以避免生成这种死代码?它会导致数十个编译器警告,希望可以避免 谢谢 下面是一个来自.jj文件的完整示例:JavaCC生成死代码,java,compiler-construction,javacc,Java,Compiler Construction,Javacc,我第一次使用JavaCC,我注意到它产生了很多死代码。有许多行看起来像(请原谅间距,它是自动的): 是否可以避免生成这种死代码?它会导致数十个编译器警告,希望可以避免 谢谢 下面是一个来自.jj文件的完整示例: Statement UseStatement(): { String dbName; } { <USE> dbName=DbObjectName() { return new UseStatement(dbName); } }
Statement UseStatement():
{
String dbName;
}
{
<USE> dbName=DbObjectName()
{
return new UseStatement(dbName);
}
}
另外,JavaCC正在生成一个ParserTokenManager文件,该文件抛出一个TokenMgrError,但代码没有编译。它声明了一个
受保护的int-curChar
,在这里它应该声明一个char
。用谷歌搜索这个短语可以显示许多正确声明为char
-这只是很多人手工编辑结果吗?我找到了源代码,为了防止死代码,你必须触发选项。isLegacyExceptionHandling
// Add if statement to prevent subsequent code generated
// from being dead code.
// NB: eclipse now detects 'if (true)' as dead code, so use the more complicated
// 'if ("" != null)'
if (inAction && (Options.isLegacyExceptionHandling()) ) {
t.image = "{if (\"\" != null) return";
jumpPatched = true;
}
然后触发:
if (p.isJumpPatched() && !voidReturn) {
if (isJavaDialect) {
// TODO :: I don't think we need to throw an Error/Exception to mark that a return statement is missing as the compiler will flag this error automatically
if (Options.isLegacyExceptionHandling()) {
codeGenerator.genCodeLine(" throw new "+(Options.isLegacyExceptionHandling() ? "Error" : "RuntimeException")+"(\"Missing return statement in function\");");
}
} else {
codeGenerator.genCodeLine(" throw \"Missing return statement in function\";");
}
}
遗留异常处理是一个派生选项,仅当JAVA\u TEMPLATE\u TYPE=modern
时,该选项才为false。正确设置的唯一方法是将其包含在.jj文件的选项
块中,如下所示:
options {
JAVA_TEMPLATE_TYPE="modern";
}
理论上,它也可以通过命令行选项进行设置,但在撰写本文时,实际上不可能在解析命令行参数之前设置派生选项()是否有办法关闭“遗留异常处理”?是-请参阅我的编辑。这有点疯狂,但我最终还是明白了。你使用的是什么版本的JavaCC?您是否尝试了
LEGACY\u EXCEPTION\u HANDLING=false
选项?要回答有关curChar
类型的问题,不,这不是很多手工编辑。版本5生成的类型为char
。遗留异常处理实际上不是“用户可设置的”,但可以更改它-请参阅下面的编辑。我使用的Maven插件使用的版本是6.1.3,但我知道如何回到原来的codehaus插件,并将其JavaCC版本强制为7.x。谢谢
if (p.isJumpPatched() && !voidReturn) {
if (isJavaDialect) {
// TODO :: I don't think we need to throw an Error/Exception to mark that a return statement is missing as the compiler will flag this error automatically
if (Options.isLegacyExceptionHandling()) {
codeGenerator.genCodeLine(" throw new "+(Options.isLegacyExceptionHandling() ? "Error" : "RuntimeException")+"(\"Missing return statement in function\");");
}
} else {
codeGenerator.genCodeLine(" throw \"Missing return statement in function\";");
}
}
options {
JAVA_TEMPLATE_TYPE="modern";
}