ANTLR4 Parser, Visitor not created
Asked Answered
S

5

8

I'm new to ANTLR and trying to write grammar in ANTLR4 without any prior brush with the previous version. I'm following the book 'The Definitive ANTLR 4 Reference'. I use Eclipse and installed ANTLR4 IDE as given in here. I wrote the following grammar in Expr.g4:

grammar Expr;

import Common;

options{
language = Java;
}
prog: stat+;

stat: expr NEWLINE
    | ID '=' expr NEWLINE
    | NEWLINE;

expr: expr ('/'|'*') expr
    | expr ('+'|'-') expr
    | INT
    | ID
    | '('expr')';

The Common.g4 contains the following:

lexer grammar Common;

ID: [A-Za-z]+;
INT: [0-9]+;
NEWLINE: '\r'?'\n';
WS: [\t]+ -> skip;

The lexer.java was created but not parser.java and visitor.java and other base file. Please help me fix the problem. Thanks in advance.

Shipper answered 19/1, 2014 at 4:18 Comment(3)
Not enough information. Are you sure the parser and visitor are supposed to be generated? Did you look in the correct directories? Perhaps you should review the documentation and/or issue tracker once more.Norahnorbert
@Norahnorbert Yes, I have used other grammar and checked. When I saved the file all the files were created automatically. But not in the above case.Shipper
I have same problem, have you found solution?Wendelin
D
8

In fact I had the same problem once, I used to integrate two G4 files within the same project the first one generated Visitor but the second didn't.

Then I realized that each G4 file has its own configuration for code generation that you can change by:

  1. Right click on the G4 file then Run As
  2. Choose External tool configuration
  3. Change the no-visitor to visitor, you can do the same for listener.

Now the Visitor file is generated.

Dipnoan answered 29/6, 2016 at 9:56 Comment(1)
This did the trick for me! I was getting no visitor files. I am using just one G4 file, but knowing that each G4 file has its own tool configuration was the key...Ahem
R
6

For Maven it was not clear from the documentation how to set the visitor property. You have to do it in the element.

<plugin>
    <groupId>org.antlr</groupId>
    <artifactId>antlr4-maven-plugin</artifactId>
    <version>${antlr.version}</version>
    <configuration>
        <visitor>true</visitor>
    </configuration>
    ...
</plugin>
Rabassa answered 17/10, 2019 at 16:24 Comment(0)
A
2

You should generate your grammars during the build process. Starting with ANTLR 4.2 (which is currently available through the Sonatype snapshot Maven repository as 4.2-SNAPSHOT), the Maven plugin supports Eclipse m2e. The documentation for the Maven plugin is available here:

http://www.antlr.org/api/maven-plugin/master/index.html

I would not trust any build which uses ANTLR grammars and does not automate the code generation steps into the build itself, and highly recommend you avoid using manual code generation steps or code generation as part of an IDE extension ever.

Alysonalysoun answered 22/1, 2014 at 14:5 Comment(0)
G
2

Check your JRE version (command line [windows]: java -version).
I was having the same problem with this example. because the JRE 8.

So, if you have JRE8, Posible solutions are:

  • adding -version:1.7 in eclipse.ini ; or
  • in the "Run As/External Tools Configurations...", adding -version:1.7 in Arguments; or
  • selecting JRE 1.7 in Windows/Java/Installed JREs

To generate XXXVisitor, in "Run As/External Tools Configurations..." change -no-visitor argument by -visitor.

  • Works for -version:1.6, too.
Graminivorous answered 23/1, 2014 at 23:38 Comment(1)
When you are using command line to generate the java class use -visitor as arg as mentioned above to generate the visitor class.Sheelagh
W
1

Preference -> ANTLR4 -> Tool -> Options -> Generate parse tree visitor

Waziristan answered 24/4, 2015 at 2:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.