Why is the following syntax correct:
x = y+++y;
Where it means
y++ + y
ory + ++y
which meansy * 2 + 1
(not sure about this, though: very ambiguous)
But this syntax is incorrect:
x = y+++++y;
Which should mean
y++ + ++y
, which meansy * 2 + 2
Is there a reason for the incorrectness of this syntax? (Edit: thank you for explaining why it is invalid syntax, but that is not my intention with this question.)
(Edit: ofcourse I'm not using this in real code, purely in interest of parsers/lexers; but I wonder why the parser doesn't like this; the last example even looks less ambiguous than the first one.)
(Edit:
int i = 0;
int j = (i = 3)+++i;
Is invalid too, though it seems very unambiguous to me, (i = 3)
is a value, thus (value +
value) and then the ++i
value token.)
Java Identifiers All Java components require names. Names used for classes, variables, and methods are called identifiers. All identifiers should begin with a letter (A to Z or a to z), currency character ($) or an underscore (_). After the first character, identifiers can have any combination of characters.
What is Java Syntax? Java Syntax is a basic of the language, all the main rules, commands, constructions to write programs that the compiler and computer “understands”. Every programming language has its syntax as well as human language.
Example explainedEvery line of code that runs in Java must be inside a class . In our example, we named the class Main. A class should always start with an uppercase first letter. Note: Java is case-sensitive: "MyClass" and "myclass" has different meaning.
In Java, there are 51 keywords. Some of the keywords in Java are: abstract, boolean, byte, char, long, short, int, finally, extends, implements, interface, package, new, while, for, do, break, continue, return, public, private, this, case, final, default, etc.
The parsing is greedy, that is , it looks for the longest matching token first. This simplify the implementations a lot (presumably). Also the Java language spec(3.2) says
Java always uses the longest possible translation at each step, even if the result does not ultimately make a correct Java program, while another lexical translation would
So, for y+++++y;
the parser/tokenizer will break it down something like this:
y
++
(as there is no +++
operator, ++
is the longest that match the syntax of java)++
(as there is no +++
operator, ++
is the longest that match the syntax of java)+
(This was the first thing that matches the syntax now)y
Effectively it is parsed as (y++) (++) (+y)
the ++
operator is defined for a variable, however the first expression (y++
) returns a value. You can't apply the next operator (++
) to a value.
This means that x = y+++y;
would be parsed as y++ + y
, which is nothing wrong with.
In short, because of the order in which Java expressions are evaluated, the parser thinks you're trying to use the ++ operator on an expression, which is illegal.
I think that this:
x = y+++++y;
is getting parsed as something like this:
x = ((y++)++) + y);
or something like this:
x = (y + (++(++y)));
I'll assume that this is just an academic question and that you're not actually trying to use code like that in the real world. Not spacing operators well only leads to pain and suffering.
The Java language spec lists all the rules for evaluating expressions here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With