我的build.xml如下所示:
<property name="lib" value="${basedir}/lib" />
<property name="src" value="${basedir}/src" />
<property name="package" value="com/package/antlr" />
<path id="compile.classpath">
<fileset dir="${lib}">
<include name="antlr-3.5-complete.jar" />
</fileset>
</path>
<!-- Check if ANTLR3 can be found in the classpath -->
<target name="antlr_classpath">
<whichresource property="antlr.in.classpath" class="org.antlr.Tool">
<classpath>
<path refid="compile.classpath" />
</classpath>
</whichresource>
<fail message="ANTLR3 not found in CLASSPATH">
<condition>
<not>
<isset property="antlr.in.classpath" />
</not>
</condition>
</fail>
<echo>ANTLR3 found via CLASSPATH</echo>
</target>
<!-- Antlr3 is called here -->
<target name="antlr" depends="antlr_classpath">
<java classname="org.antlr.Tool" fork="true" failonerror="true">
<classpath>
<path refid="compile.classpath" />
</classpath>
<arg value="-verbose" />
<arg value="-make"/>
<arg value="-o" />
<arg path="${src}/${package}" />
<arg path="${src}/${package}/BoolExpr.g" />
</java>
</target>
在/src/com/package/antlr文件夹中生成BoolExprLexer.java和BoolExprParser.java,这是我想要的,但是生成的文件属于默认包。如何使它们成为com.package.antlr的一部分?
ANTLR 3
对于词法分析器语法或解析器语法,在语法文件中添加以下内容:
@header {
package com.package.antlr;
}
对于组合语法(词法分析器和解析器在同一语法文件中),将以下内容添加到语法文件中:
@lexer::header {
package com.package.antlr;
}
@parser::header {
package com.package.antlr;
}
ANTLR 4
当您从语法生成词法分析器/解析器时,将以下参数传递给ANTLR工具:
-package com.package.antlr
这可以在Ant构建期间通过向构建脚本添加以下两行来完成:
<arg value="-package"/>
<arg value="com.package.antlr" />