Q:

My Antlr4 Grammar doesn't understand math expressions

i have this Grammar
grammar Arith;

exp : LPAREN exp RPAREN
    | fun
    | num
    | exp (OP exp)+
    ;

num : LPAREN num RPAREN 
    | LESS num 
    | INT 
    | INT 'b'
    | '0x' INT 
    ;

fun : LPAREN fun RPAREN
    | LESS fun 
    | FUN_TXT LPAREN exp RPAREN
    | 'pow' LPAREN exp ',' exp RPAREN
    ;

INT : ('0'..'9')+ ;

LPAREN : '(' ;
RPAREN : ')' ;

FUN_TXT : 'log' | 'acos' | 'asin' | 'atan' | 'cos' | 'abs' | 'sin' | 'sqrt' | 'tan' ;

OP : ADD | LESS | MUL | DIV | MOD ;

ADD : '+' ;
LESS : '-' ;
MUL : '*' ;
DIV: '/' ;
MOD: '%' ;
WS : [ \t\r\n] -> skip ;

I try to insert sin(-1) but the lexer said me "no viable alternative at input '-'".
I think that program translate it in "exp -> exp (OP exp)+" instead of "exp -> fun(num) -> fun(LESS num)"
Could someone help me to understand what i've forget and how change my rules in the right way? 
Thanks

A:

First I would simplify your rules for num and fun
num : INT 
    | INT 'b'
    | '0x' INT 
    ;

fun : FUN_TXT LPAREN exp RPAREN
    | 'pow' LPAREN exp ',' exp RPAREN
    ;

Brackets and minuses are handled by the exp rule.
You also need to separate the ADD and SUB from the multiplicative operators to get precedence right. The calculator example for the Antlr grammars uses
expression 
    : multiplyingExpression ((PLUS|MINUS) multiplyingExpression)*
    ;

multiplyingExpression  
    : powExpression ((TIMES|DIV) powExpression)*
    ;

powExpression
    : atom (POW expression)?
    ;

atom 
    : scientific
    | variable
    | LPAREN expression RPAREN
    | func
    ;

scientific
    : number (E number)?
    ;

func
    : funcname LPAREN expression RPAREN
    ;

I would be inclined to start from that.

