Compiler Design: How do you distinguish between "op_Minus number" and "negativeNumber"?

348 views Asked by At

How do you distinguish between the token sequence "op_Minus" "number" and simple a negative number?

1

There are 1 answers

0
Timo Geusch On BEST ANSWER

I would think it's context dependent and dependent on the number of operands you encounter while parsing the source code.

Depending on the type of syntax you choose/encounter, you either have something like this when you're dealing with subtraction:

operand1 - operand2

or if your language uses prefix notation, you'll end up with something like this:

- operand1 operand2
(- operand1 operand2)

In both cases you should be able to either deduce from the previous token (in the case of infix notation) or from the lookahead to the next token/next two tokens) if you're dealing with a subtraction or a negative number, given that the latter would only have a single operand.