我遇到了在Eclipse中使用antlr-4.4-complete.jar在v4语法中嵌入动作的问题。
当使用4.4 jar从ant生成识别器时,生成器会发出一条消息,“规则的未知属性'文本'......”
操作可以像{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,下面的片段会报告“未知属性'文本'”消息。如果antlr4-maven-plugin版本设置为4.2,它将成功构建代码。
另一个问题:
.tokens不是在目标包目录中生成的,而是在generated-sources / antlr4目录中创建的,但插件文档说明(在使用库目录下):
任何语法的.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>
<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>
答案 0 :(得分:2)
从Antlr 4.3发行说明中,在重大变化下:
在嵌入式操作或语义谓词中,对封闭规则的引用不能再写为$ ruleName。而是使用特殊符号$ ctx来引用封闭规则。在此更改之前已经从语法生成的解析器不受影响,但在使用ANTLR 4.3生成代码之前,需要更新语法。作为纠正#571的一部分,需要进行此更改。
从antlr 4.3开始,以$ rule.text形式访问文本属性的操作需要更改为$ ctx.getText();
我想我必须更好地掌握我的工具。
我最近下载的Antlr 4示例中没有注意到这种变化。