5

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

sth
  • 222,467
  • 53
  • 283
  • 367

4 Answers4

10

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]

[1]. http://en.wikipedia.org/wiki/Maximal_munch

crisron
  • 363
  • 2
  • 5
  • 21
Jon
  • 428,835
  • 81
  • 738
  • 806
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

Community
  • 1
  • 1
halfdan
  • 33,545
  • 8
  • 78
  • 87
  • 1
    +1 for mentioning operator precedence *and* linking a source. – Mephane Apr 13 '11 at 12:46
  • 14
    Tokenization rules, not operator precedence. – aschepler Apr 13 '11 at 12:46
  • 1
    This 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
  • 2
    The 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
1

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.

iammilind
  • 68,093
  • 33
  • 169
  • 336
0

(x--)-y; Which compiler do you use? What are the types of x and y?

GregC
  • 7,737
  • 2
  • 53
  • 67