Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ANTLR4 Parser, Visitor not created

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.

like image 333
Pacu Avatar asked Jan 19 '14 04:01

Pacu


2 Answers

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.

like image 135
A.Thabet Avatar answered Sep 27 '22 23:09

A.Thabet


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>
like image 20
aled Avatar answered Sep 27 '22 21:09

aled