Reputation: 1
I have problem running input stream for my grammar, here is part of my grammar
expression
: ....
| ( '+' | '-' | '&' |) expression
| expression ('+'|'-') expression
....
;
when input stream like 2+2-2 or (2+2+2)-2, error occurs
line 10:30 extraneous input '-2' expecting {',', '^',...}
why my grammar can not distinguish between -2 and 2-2 ? anyone help me please !
Upvotes: 0
Views: 660
Reputation: 99869
It appears that you have written your lexer in such a way that -2
results in a single token. Instead of handling negative numbers in this way, treat -
as a unary operator and allow your parser handle the negation operation. If you try to handle negative numbers directly in the lexer, input such as 2-2
will be sent to the parser as 2 consecutive number tokens (2
followed by -2
) instead of the desired 3 tokens (2
, -
, 2
).
Upvotes: 2