Hongxu Chen
Hongxu Chen

Reputation: 5360

antlr3ide generates parsers and lexers without package info?

antlr3ide seems to generate parser and lexer files without the package info where the java files are located (such as package tour.trees;, here the relative path folder tour/trees contains the corresponding files ExprParser.java and ExprLexer.java).

The official forum seems a bit inactive and the documentation gives me not much help:(

Below is a sample grammar file Expr.g:

grammar Expr;

options {
  language = Java;
}


prog : stat+;

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

expr: multiExpr (('+'|'-') multiExpr)*
    ;

multiExpr : atom('*' atom)*
    ;

atom : INT
     | ID
     | '(' expr ')'
     ;

ID : ('a'..'z'|'A'..'Z')+ ;
INT : '0'..'9'+;
NEWLINE : '\r'?'\n';
WS : (' '|'\t'|'\n'|'\r')+{skip();};

Upvotes: 1

Views: 81

Answers (1)

Bart Kiers
Bart Kiers

Reputation: 170278

The package declaration is not something that antlrv3ide generates. This is done by ANTLR. To let ANTLR generate source files in the package tour.trees, add @header blocks containing the package declarations in your grammar file like this:

grammar Expr;

options {
  language = Java;
}

// placed _after_ the `options`-block!    
@parser::header { package tour.trees; }
@lexer::header { package tour.trees; }

prog : stat+;

...

Upvotes: 1

Related Questions