-4
a=5;b=10;a----------------++++++++++++++++++b
# Output: 15

The above is the output of a python script. Could you please explain how it works? I was quite surprised that it works and gives an answer!!

However a similar such expression doesn't work in Java,C,C++ For instance:

 a=10;    
 b=5;
 System.out.println(a---b);//error!!
 cout<<a---b;//error
 printf("%d\n",a---b);//error

Does this have something to do with the compiler - lexical analyser/syntactical analyser? I understand there is a "leftmost, eager and greedy" approach taken by compilers, but I dont exactly understand how that works in this case.

Varun Rao
  • 393
  • 1
  • 5
  • 19
  • but `a---b` works and it´s similar aswell – SomeJavaGuy Sep 14 '16 at 09:43
  • 1
    In e.g. Java, what would `a--b` mean? Would it mean `a --b`? `a-- b`? `a - -b`? It will assume it's one of the first two cases, and neither of those are valid. – Some programmer dude Sep 14 '16 at 09:47
  • 2
    Which of those three other langauges do you want an answer for? In C the answer is "it is a syntax error". Or are you asking a Python question? – Weather Vane Sep 14 '16 at 09:48
  • 1
    The first `-` is subtraction, the last `+` is a unary operator operating on `b` and the rest are unary operators operating on the result of the previously evaluated one. It's valid but perverse code in Python. Other languages do their own thing. – nigel222 Sep 14 '16 at 09:55
  • 1
    Answer: because they are different languages and they follow different syntax. Period. – Sourav Ghosh Sep 14 '16 at 09:55
  • Why all the downvotes? Sure, "why doesn't this work in other languages" isn't a great question, but "how does this get evaluated?" is a perfectly acceptable one. – Aran-Fey Sep 14 '16 at 09:57
  • I didn't downvote, but the poster should have sat down at the Python interpreter and worked it out by typing `0-1`, `0--1`, `0+-1`, `0+-+++1` etc. – nigel222 Sep 14 '16 at 10:00
  • Oh, and I was curious enough myself to find out for myself what would happen were b a string. (I guessed error, and was right). – nigel222 Sep 14 '16 at 10:03
  • Make sure to always include 4 different language tags in your questions, to attract 4 times more down votes. – Lundin Sep 14 '16 at 11:22

1 Answers1

6

In Python, that's

a - ---------------++++++++++++++++++b

that is, subtracting ---------------++++++++++++++++++b from a.

The prefix -, negation, changes the sign of a number, the prefix + does nothing.
Example:

>>> -+2
-2
>>> +-2
-2
>>> --2
2

Since there are 15 negations, a - ---------------++++++++++++++++++b is equivalent to a - -b, which is a + b.

Java, C, and C++ have prefix and postfix -- and ++ (decrement and increment) operators, and by the "maximal munch rule", your expression would read as

(((((((((((((((((a--)--)--)--)--)--)--)--)++)++)++)++)++)++)++)++)++) b

which, even if those operations on a were legal, would be as much nonsense as a b is.
(Note that a---b is valid; it means a-- - b.)

Details about the workings of the increment and decrement operators can be found in any suitable book.

molbdnilo
  • 64,751
  • 3
  • 43
  • 82