418 views

# Determine if a symbol is Unary or not

1. 7 weeks ago

### Dave S

Dec 9 San Diego, California USA

If the context of a line of code the + and - symbols can denote two different situations.

• A binary operation (ie. requires TWO arguments) X = 3 - 2
• A Unary operation (ie. applies only to the next argument) X = -3

What I am trying to insure is that I have all the situations that indicate a UNARY scenario.
This is what I have, and I *THINK* it covers everything... but I'm not 100% sure
Note : all these assume that whitespace is ignored

• If the symbol preceding the +/- is a (
• the +/- is 1st symbol on a line
• the +/- is preceded by an operator (* / + -) or equal sign (=)
• the +/- is preceded by a keyword but NOT a variable name
2. ### Scott C

I guess you should add the edge case of when a calculation is broken into multiple lines by the `_` line continuation operator?

For myself, I dislike this kind of syntax ambiguity when it comes to treating a positive value as a negative. But because there is no opposite function to `Abs` (which is true in many languages), I prefer to write variables in this case like `(myPositiveNum * -1)` rather than `-myPositiveNum`. Except of course when I'm writing a literal negative value, e.g., `-3`.

You get the same result with `(myPositiveNum * -1)`, plus it stands out more in the code when you're reading it.

I got caught the other day when I was reading some example code in the forum and totally missed the use of inverse assignment on a variable name. Had me really confused for a bit.

3. ### Dave S

Dec 9 San Diego, California USA

well the abiguity exists.... and I'm trying to "remove" it by examining the context in which it exists.

And your suggestion of "_" I have covered with the EOL case :)

4. ### Julian S

Dec 9 Pre-Release Testers, Xojo Pro UK

Just looking down my test list for my reformat code script:

```i = -(a+b)
a(a,-1)
i = a^-b (I assume you have all operators covered?)```
5. ### Robert W

Do you need to handle comma separators in multi parameter functions?
Such as:
y=Atan2(a,-b)

6. ### Rick A

Dec 10 Pre-Release Testers (Brazil. UTC-3:00)
Edited 7 weeks ago

Create a lexical analyzer. What you called "symbol" is an operator, if the previous token was a literal or an identifier or an expression ( ")" ), the "-" is "subtract token" otherwise consider it a "negate token".
https://www.rosettacode.org/wiki/Compiler

7. ### Dave S

Dec 10 San Diego, California USA

@Rick A Create a lexical analyzer

Pretty much what I am doing.

so it boils down to this
A + or - symbol are considered to be a mathematical operator unless

it is preceded by ) or , or = or any mathematical operator (+-/*^ etc) or any comparison operator (> < etc)

8. ### Rick A

Dec 10 Pre-Release Testers (Brazil. UTC-3:00)
Edited 7 weeks ago

@Dave S A + or - symbol are considered to be a mathematical operator unless...

"-" is unary if in its context it is equivalent to (-x)
it's "binary" when is preceded by another "expression".
So, in "n = -x", - is unary, and "n = y -x" it should be binary.
in "n = (2*4) -x" also binary
in "n = 2 * -x" is unary, the same as "n = 2 * (-x)"

9. ### Dave S

Dec 10 San Diego, California USA

which is what I just said I thought?

10. ### Rick A

Dec 10 Pre-Release Testers (Brazil. UTC-3:00)

I didn't read what I said in your words. What's a "mathematical operator"? Both the unary and binary are.

11. ### Rick A

Dec 10 Pre-Release Testers (Brazil. UTC-3:00)
Edited 7 weeks ago

The "what" operators are is defined in the context of the "invented language". You make the rules.
But in a wide context, usually, a "-" is unary when NOT preceded by a expression ( (2*3), (2+a), ...) or an identifier (a variable), it means, a second operand between the operator.

12. ### Norman P

Dec 10 Pre-Release Testers, Xojo Pro outside
Edited 7 weeks ago

and then what do you do with

x = x * - 2
or
x = x * - (3*1)

you also have to skip / etc whitespace IF you allow unary operators to have whitespace between them & the number/expression

As far as I recall parser generators like flex/bison dont try to resolve this at the level of a "token" because it can be ambiguous (ie/ there's no token for "expression") and this should be part of the semantic analysis

13. ### Rick A

Dec 10 Pre-Release Testers (Brazil. UTC-3:00)

Compilers can be multi pass (multiple steps like a dumb lexical analysis, followed by syntactical and semantical) or have less steps, sometimes just one pass, because you constructed an smart analyzer that can keep contexts while going and infer things from the tree.

Just in the firsts pages of the Niklaus Wirth's "Compiler Construction" you find basic answers for all those questions http://www.ethoberon.ethz.ch/WirthPubl/CBEAll.pdf

14. ### Rick A

Dec 10 Pre-Release Testers (Brazil. UTC-3:00)
Edited 7 weeks ago

@Norman P and then what do you do with

x = x * - 2
or
x = x * - (3*1)

The first translates to x = x * y (y=-2) by the basic rudimentary rule I said, the second is ok also with x = x * y (y=-z, (z=3*1))

15. ### Norman P

Dec 10 Pre-Release Testers, Xojo Pro outside
Edited 7 weeks ago

here then

x = x * - (foo*bar)

theres still no TOKEN for "expression"
thats usually left to the actual parser NOT the tokenizer to decide if the - is unary or not

EDIT : and this has NOTHING to do with whether or not the compiler is single pass or multi-pass

16. ### Rick A

Dec 10 Pre-Release Testers (Brazil. UTC-3:00)
Edited 7 weeks ago

The token "(" is here comes an expression.

One pass compilers makes interleaved processing of lexic, syntax and semantic analysis.

https://en.wikipedia.org/wiki/One-pass_compiler

17. ### Norman P

Dec 10 Pre-Release Testers, Xojo Pro outside

( may not be the only lead in to an expression depending on the grammar for whatever language it is you're parsing

18. ### Rick A

Dec 11 Pre-Release Testers (Brazil. UTC-3:00)

@Norman P ( may not be the only lead in to an expression depending on the grammar for whatever language it is you're parsing

Correct.