TomEE
TomEE

Reputation: 598

Are Antlr v4 grammars jar-version dependent?

I am running into problems embedding actions in a v4 grammar using antlr-4.4-complete.jar in Eclipse.

When generating the recognizer from ant using the 4.4 jar the generator squawks with the message, "unknown attribute 'text' for rule …"

The action can be as simple as {System.out.println($rule.text);}

My ant build works fine if I use antlr-4.0-complete.jar.

My suspicion was aroused when I found this comment recently posted to the 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 

Looking for a way to verify the problem and improve workflow I thought I'd try using maven.

The fragment below reports the "unknown attribute 'text'" message if the antlr4-maven-plugin version is set to 4.3 It will successfully build code if the antlr4-maven-plugin version is set to 4.2.

One other question:

The .tokens are not generated in the target package directory but are created in the generated-sources/antlr4 directory, but the plugin documentation states that (under using library directories):

The .tokens files for any grammars are generated within the same output directory structure as the .java files.

and

The plugin will ... produce .java and .tokens files in the output directory target/generated-sources/antlr4/org/foo/bar.

Am I missing a configuration parameter?

<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>

Upvotes: 0

Views: 456

Answers (1)

TomEE
TomEE

Reputation: 598

From the Antlr 4.3 release notes, under Breaking Changes:

Within an embedded action or semantic predicate, references to the enclosing rule can no longer be written as $ruleName. Instead, use the special symbol $ctx to refer to the enclosing rule. Parsers already generated from grammars prior to this change are not affected, but the grammar will need to be updated prior to generating code with ANTLR 4.3. This change was required as part of correcting #571.

From antlr 4.3 on actions accessing text attributes in the form of $rule.text need to be changed to $ctx.getText();

I guess I have to do a better job of staying on top of my tools.

I did not notice this change in the Antlr 4 examples I recently downloaded.

Upvotes: 3

Related Questions