How to distinguish operator '-' from negative number for tokenizer

I am creating an infix parser, so I need to create a tokenizer. It works well, except for one thing: I don't understand how to distinguish a negative number from the "-" operator.

For example, if I have:

23 / -23 

Tokens should be 23 , / and -23 , but if I have an expression like

 23-22 

Then the tokens should be 23 , - and 22 .

I found a dirty workaround that is, if I come across a “-” followed by a number, I look at the previous character and if that character is a number or “)”, I consider “-” as an operator and not a number. Besides being kind of ugly, it doesn't work for expressions like

 --56 

where he receives the following tokens: - and -56 , where he should receive --56

Any suggestion?

+5
source share
1 answer

In the first example, tokens should be 23 , / , - and 23 .

Then the solution should evaluate the tokens in accordance with the rules of associativity and priority. - cannot communicate with / , but can, for example, up to 23.

If you encounter --56 , it is divided by - , - , 56 , and the rules will take care of this problem. No special cases required.

+8
source

Source: https://habr.com/ru/post/1205364/


All Articles