How is the expression x---y
parsed? Is it a legal expression?

- 222,467
- 53
- 283
- 367

- 51
- 1
-
1http://stackoverflow.com/questions/5341202 (and its answers) may be of interest. – Steve Jessop Apr 13 '11 at 12:44
-
See this [Maximal Munch Principle.](http://en.wikipedia.org/wiki/Maximal_munch) – Nawaz Apr 13 '11 at 13:02
4 Answers
It's legal and parsed as x--
-
y
.
I believe the first two minus signs are interpreted as a post-decrement operator because it's the longest token following x
that is legal to appear. This leaves the third minus to play the role of subtraction.
This is in accordance with the Maximal Much Rule[1]
This is related to operator precedence. Have a look at this table.
The decrement/increment operator has precedence over the arithmetic operations. It will be parsed as x-- - y
.
To correct my answer: The parser matches the longest token first, so --
is chosen over the arithmetic -
. Resulting in the expression being parsed as x-- - y
-
1
-
14
-
1This doesn't have to do with precedence, but with how the language is *parsed*. – Jon Apr 13 '11 at 12:46
-
The operator precedence is exactly what leads to the tokenization rules. Operator precedence just describes what the parser does with operators of any kind. – halfdan Apr 13 '11 at 12:47
-
1@halfdan: That is completely wrong. *Operator precedence* determines how the compiler builds expression trees out of streams of tokens. *Tokenization rules* determine how the lexer parses source code into a stream of such tokens. – Jon Apr 13 '11 at 12:48
-
3`--` is chosen as the first token, because it's the longest sequence that gives a valid token. This has nothing to do with operator precedence. – Mike Seymour Apr 13 '11 at 12:51
-
2The expression `---x` is tokenized as `-- - x`, even though prefix `--` has a higher precedence than unary `-`. (This is invalid for builtin types, but may be okay for a class with weird overloaded operators.) Token parsing is simply "greedy" and takes as many characters as possible as soon as possible. – aschepler Apr 13 '11 at 12:55
-
Indeed guys. You are correct, mixed up some things in my mind. I added a comment to my answer. Sorry for the confusion. – halfdan Apr 13 '11 at 12:55
-
Operator precedence is a non-issue here. For that matter, C++ doesn't have any real operator precedence, and the linked-to table is wrong in a couple of details. (It has to be, because C++ doesn't have real operator precedence.) – James Kanze Apr 13 '11 at 13:11
-
It's the "maximal munch" rule being applied :) 2.4p3: "If the input stream has been parsed into preprocessing tokens up to a given character, the next preprocessing token is the longest sequence of characters that could constitute a preprocessing token, even if that would cause further lexical analysis to fail." – Johannes Schaub - litb Apr 13 '11 at 16:54
For all data types, it's parsed as x-- - y
. If it's some class
object then you have to define post decrement operator and minus operator, it will give compiler error if you just define pre decrement operator. That means, x-- - y
is forced in any case.

- 68,093
- 33
- 169
- 336