【问题标题】:Are Antlr v4 grammars jar-version dependent?Antlr v4 语法是否依赖于 jar 版本?
【发布时间】:2014-12-22 21:11:53
【问题描述】:

我在 Eclipse 中使用 antlr-4.4-complete.jar 在 v4 语法中嵌入动作时遇到问题。

当使用 4.4 jar 从 ant 生成识别器时,生成器会发出消息“unknown attribute 'text' for rule ...”

动作可以像{System.out.println($rule.text);}一样简单

如果我使用 antlr-4.0-complete.jar,我的 ant 构建工作正常。

当我发现这条评论最近发布到 Antlr 4 book errata:

#77534: When I run antlr4 on Simple.g4 I get the error message:
error(65): Simple.g4:18:52: unknown attribute text for rule stat in $stat.text 
error(65): Simple.g4:20:54: unknown attribute text for rule stat in $stat.text 

寻找一种方法来验证问题并改进工作流程,我想我会尝试使用 maven。

如果antlr4-maven-plugin版本设置为4.3,下面的片段会报告“unknown attribute 'text'”消息如果antlr4-maven-plugin版本设置为4.2,它将成功构建代码。

另一个问题:

.tokens 不是在目标包目录中生成的,而是在 generate-sources/antlr4 目录中创建的,但是插件文档指出(在 using library 目录下):

任何语法的 .tokens 文件都在与 .java 文件相同的输出目录结构中生成。

插件将...在输出目录 target/generated-sources/antlr4/org/foo/bar 中生成 .java 和 .tokens 文件。

我是否缺少配置参数?

<plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.0</version> <configuration> <source>6</source> <target>6</target> <showWarnings>true</showWarnings> <showDeprecation>true</showDeprecation> <compilerArguments> <Xlint /> </compilerArguments> </configuration> </plugin> <plugin> <groupId>org.antlr</groupId> <artifactId>antlr4-maven-plugin</artifactId> <version>4.2</version> <configuration> <sourceDirectory>src/antlr4</sourceDirectory> </configuration> <executions> <execution> <goals> <goal>antlr4</goal> </goals> </execution> </executions> </plugin> </plugins> <pluginManagement> <plugins> <plugin> <groupId>org.antlr</groupId> <artifactId>antlr4-maven-plugin</artifactId> <version>4.3</version> </plugin> <!--This plugin's configuration is used to store Eclipse m2e settings only. It has no influence on the Maven build itself. --> <plugin> <groupId>org.eclipse.m2e</groupId> <artifactId>lifecycle-mapping</artifactId> <version>1.0.0</version> <configuration> <lifecycleMappingMetadata> <pluginExecutions> <pluginExecution> <pluginExecutionFilter> <groupId>org.antlr</groupId> <artifactId>antlr4-maven-plugin</artifactId> <versionRange>[4.0,)</versionRange> <goals> <goal>antlr4</goal> </goals> </pluginExecutionFilter> <action> <execute> <runOnIncremental>true</runOnIncremental> </execute> </action> </pluginExecution> <pluginExecution> <pluginExecutionFilter> <groupId>org.codehaus.mojo</groupId> <artifactId> build-helper-maven-plugin </artifactId> <versionRange>[1.7,)</versionRange> <goals> <goal>add-source</goal> </goals> </pluginExecutionFilter> <action> <ignore></ignore> </action> </pluginExecution> </pluginExecutions> </lifecycleMappingMetadata> </configuration> </plugin> </plugins>

【问题讨论】:

    标签: eclipse maven-plugin antlr4


    【解决方案1】:

    来自 Antlr 4.3 发行说明,在重大更改下

    在嵌入的操作或语义谓词中,对封闭规则的引用不能再写为 $ruleName。相反,使用特殊符号 $ctx 来引用封闭规则。在此更改之前已从语法生成的解析器不受影响,但在使用 ANTLR 4.3 生成代码之前需要更新语法。此更改是纠正 #571 的一部分。

    从 antlr 4.3 开始,关于以 $rule.text 形式访问文本属性的操作需要更改为 $ctx.getText();

    我想我必须更好地掌握我的工具。

    我在最近下载的 Antlr 4 示例中没有注意到这种变化。

    【讨论】:

    • 有趣的是,$ctx.text 不起作用。尝试编译生成的 java 源代码失败。
    猜你喜欢
    • 1970-01-01
    • 2019-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-06
    • 1970-01-01
    • 2012-08-16
    • 1970-01-01
    相关资源
    最近更新 更多