x-----x
is parsed as (x--) -- -x
, and here --
is applied to an expression which is not a variable. That is not allowed.
The reason for this is the following. The first stage in parsing is tokenizing the input stream: the input stream, which consists of characters, is grouped in chunks which are called tokens. Tokens are strings which are meaningful to Java, e.g. keywords, operators, or identifiers.
Tokenizing is greedy: as long as another character can be added to the token such that it is still a valid token, the character is added. Thus, for example forLoop
is considered as a single identifier, and not as the keyword for
followed by the identifier Loop
.
The strings -
and --
are both valid tokens in Java. So when the tokenizer encounters ---
, it reads the first character. Although it knows that -
is a valid token, it first looks at the next character, and decides that --
is a valid token also, so the first token returned will be --
, not -
.