title: ANTLR parsing and code generation with "-" operator and "-" number sign
body: <p>This is my grammar:</p>
  
  <pre><code>grammar FOOL;
  
  @header {
      import java.util.ArrayList;
  }
  
  @lexer::members {
     public ArrayList&lt;String&gt; lexicalErrors = new ArrayList&lt;&gt;();
  }
  
  /*------------------------------------------------------------------
   * PARSER RULES
   *------------------------------------------------------------------*/
  
  prog   : exp SEMIC                      #singleExp
         | let exp SEMIC                  #letInExp
         | (classdec)+ SEMIC (let)? exp SEMIC #classExp
         ;
  
  classdec  : CLASS ID ( EXTENDS ID )? (LPAR (vardec ( COMMA vardec)*)? RPAR)?  (CLPAR ((fun SEMIC)+)? CRPAR)?;
  
  let       : LET (dec SEMIC)+ IN ;
  
  vardec  : type ID ;
  
  varasm     : vardec ASM exp ;
  
  fun    : type ID LPAR ( vardec ( COMMA vardec)* )? RPAR (let)? exp ;
  
  dec   : varasm           #varAssignment
        | fun              #funDeclaration
        ;
  
  
  type   : INT
          | BOOL
          | ID
        ;
  
  exp    :  MINUS? left=term (operator=(PLUS | MINUS) right=exp)?
        ;
  
  term   : left=factor (operator=(TIMES | DIV) right=term)?
        ;
  
  factor : left=value (operator=(EQ | LESSEQ | GREATEREQ | GREATER | LESS | AND | OR ) right=value)?
        ;
  
  value  :  INTEGER                                     #intVal
        | (NOT)? ( TRUE | FALSE )                       #boolVal
        | LPAR exp RPAR                                 #baseExp
        | IF cond=exp THEN CLPAR thenBranch=exp CRPAR (ELSE CLPAR elseBranch=exp CRPAR)?  #ifExp
        | ID                                             #varExp
        | THIS                                              #thisExp
        | funcall        #funExp
        | (ID | THIS) DOT funcall   #methodExp
        | NEW ID ( LPAR (exp (COMMA exp)* )? RPAR)?             #newExp
        | PRINT ( exp )                                  #print
        ;
  
  funcall
      : ID ( LPAR (exp (COMMA exp)* )? RPAR )
      ;
  
  
  /*------------------------------------------------------------------
   * LEXER RULES
   *------------------------------------------------------------------*/
  SEMIC  : ';' ;
  COLON  : ':' ;
  COMMA  : ',' ;
  EQ     : '==' ;
  ASM    : '=' ;
  PLUS   : '+' ;
  MINUS  : '-' ;
  TIMES  : '*' ;
  DIV    : '/' ;
  TRUE   : 'true' ;
  FALSE  : 'false' ;
  LPAR   : '(' ;
  RPAR   : ')' ;
  CLPAR  : '{' ;
  CRPAR  : '}' ;
  IF        : 'if' ;
  THEN   : 'then' ;
  ELSE   : 'else' ;
  PRINT : 'print' ;
  LET    : 'let' ;
  IN     : 'in' ;
  VAR    : 'var' ;
  FUN    : 'fun' ;
  INT    : 'int' ;
  BOOL   : 'bool' ;
  CLASS   : 'class' ;
  EXTENDS   : 'extends' ;
  THIS   : 'this' ;
  NEW    : 'new' ;
  DOT    : '.' ;
  LESSEQ    : ('&lt;=' | '=&lt;') ;
  GREATEREQ    : ('&gt;=' | '=&gt;') ;
  GREATER: '&gt;' ;
  LESS   : '&lt;' ;
  AND    : '&amp;&amp;' ;
  OR     : '||' ;
  NOT    : '!' ;
  
  
  //Numbers
  fragment DIGIT : '0'..'9';
  INTEGER       : DIGIT+;
  
  //IDs
  fragment CHAR  : 'a'..'z' |'A'..'Z' ;
  ID              : CHAR (CHAR | DIGIT)* ;
  
  //ESCAPED SEQUENCES
  WS              : (' '|'\t'|'\n'|'\r')-&gt; skip;
  LINECOMENTS    : '//' (~('\n'|'\r'))* -&gt; skip;
  BLOCKCOMENTS    : '/*'( ~('/'|'*')|'/'~'*'|'*'~'/'|BLOCKCOMENTS)* '*/' -&gt; skip;
  
  ERR_UNKNOWN_CHAR
      :   . { lexicalErrors.add("UNKNOWN_CHAR " + getText()); }
      ;
  </code></pre>
  
  <p>Using this grammar, I can compile and successfully execute this:</p>
  
  <pre><code>let int x = 3;
  in print(x);
  </code></pre>
  
  <p>Or this:</p>
  
  <pre><code>let int x = 3;
  in print(2-x);
  </code></pre>
  
  <p>But for example, this prints 5 instead of -1</p>
  
  <pre><code>let
      int x = -3 + 2;
  in
      print(x);
  </code></pre>
  
  <p>This instead, works, it prints -1:</p>
  
  <pre><code>let
      int x = 2 - 3;
  in
      print(x);
  </code></pre>
  
  <p>Is there something wrong or missing in the grammar? Should I post also the java class that implements the code generation step?
  How can I distinguish correctly between the minus operator and the minus sign?</p>