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.
Preference -> ANTLR4 -> Tool -> Options -> Generate parse tree visitor
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:
Run As
External tool configuration
Now the
Visitor
file is generated.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.
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:
To generate XXXVisitor, in "Run As/External Tools Configurations..." change -no-visitor argument by -visitor.