1

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();};
Hongxu Chen
  • 5,240
  • 2
  • 45
  • 85

1 Answers1

1

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

...
Bart Kiers
  • 166,582
  • 36
  • 299
  • 288
  • It works!Thanks so much!However I'm wondering why antlr3ide do not add the package info where the generated source code lies?And should I need to add a null `options` block so that I can add `@parser::header` or `@lexer::header` if I use the default option for `Expr.g` file? – Hongxu Chen Sep 29 '12 at 07:49
  • @HongxuChen I don't use `antlr3ide`, but I wouldn't know why it should override functionality that is already handled by ANTLR. And you can simply remove the `options`-block (`language=Java` is the default, so you don't need it), you don't need an empty one (if memory serves me, an empty `options` block is even illegal...). In case you add an `options` block, it should just be placed before the `@header` blocks. You're welcome, of course. – Bart Kiers Sep 29 '12 at 07:52