I have created a tree with antlr4.The tree performs the calculation steps, for , -., * and /. The tree works perfectly for positive numbers, my problem is, for negative numbers, like when I enter (9-40)*10 or so, it doesn't work anymore.
My code is:
calculator:
(d)*| ;
c:
c '*' c | c '/' c | c ' ' c | c '-' c | '(' c ')' | b | a ;
d:
a '=' c ;
b : '-'?[0-9] ;
a: [a-zA-Z][a-zA-Z0-9]*;
WS : [ \t\r\n] -> skip ;
CodePudding user response:
(please be careful to verify your source before your post. The grammar, as posted, is not valid ANTLR. Probably because you changed rule names to a
, b
, etc. and forgot to keep Lexer rules uppercase.)
If I assume to be the case, here's a modified grammar to address your problem.
grammar calc
;
calculator: (d)*;
c
: c '*' c
| c '/' c
| c ' ' c
| c '-' c
| '(' c ')'
| '-'? NBR // <-- The fix
| ID
;
d: ID '=' c;
NBR: [0-9] ; // <-- removed the '-'?
ID: [a-zA-Z][a-zA-Z0-9]*;
WS: [ \t\r\n] -> skip;
The source of the problem is, that by putting the -
in the Lexer rule, ANTLR will match the "-40" as a NBR token, since it's a longer string of characters than "40". The solution is to handle the negation as an alternative in you parser rule for c (aka expression)
It's always a good idea to use the ANTLR tool (aka grun
) the the -tokens
option to get a dump of your token stream to ensure that your Lexer rules are giving the parser the token stream that it needs.