Antlr3 tree grammar optional argument

74 views Asked by At

Here is my tree grammar from antlr3:

tree grammar TExpr1;

options {
    tokenVocab=Expr;

    ASTLabelType=CommonTree;
    superClass=MyTreeParser;
}

@header {
package tb.antlr.interpreter;
import java.util.HashMap;
}
@members {
HashMap<String, Integer> memory = new HashMap<String, Integer>();
}

prog    : (e=expr {drukuj ($e.text + " = " + $e.out.toString());})* ;

expr returns [Integer out]
          : ^(PLUS  e1=expr e2=expr) {$out = $e1.out + $e2.out;}
        | ^(MINUS e1=expr e2=expr) {$out = $e1.out - $e2.out;}
        | ^(T e1=expr e2=expr e3=expr {$out = $e1.out * 3600 + $e2.out * 60 + $e3.out;}
        | INT                      {$out = getInt($INT.text);}
        ;

The input of T are two or three numbers. I'd like first argument (e1) to be optional. How can I do so? I've tried to overload this line for two and three arguments but it seems like it doesn't work in antlr. Ah and here is some code from grammar file:

time 
  : INT (CL INT (CL INT)? )? ->^(T INT*)
  ;

T :
  ;
0

There are 0 answers