BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Weird + - behavior

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (Language C++)
View previous topic :: View next topic  
Author Message
Bruce Salzman
Guest





PostPosted: Thu May 10, 2007 2:45 am    Post subject: Weird + - behavior Reply with quote



Why doesn't this generate a syntax error in BDS2006?

#include <iostream>

#pragma argsused
int main(int argc, char* argv[])
{
int i = 1 + - - + - 1;
std::cout << i;
return 1;
}

(It prints 0, BTW).

--
Bruce
Back to top
Alan Bellingham
Guest





PostPosted: Thu May 10, 2007 4:01 am    Post subject: Re: Weird + - behavior Reply with quote



" Bruce Salzman" <bruce (AT) nospam (DOT) org> wrote:

Quote:
Why doesn't this generate a syntax error in BDS2006?

#include <iostream

#pragma argsused
int main(int argc, char* argv[])
{
int i = 1 + - - + - 1;
std::cout << i;
return 1;
}

(It prints 0, BTW).

I guess you got the parser confused, and it keeps applying unary
operators to the following expression. i.e.

int i = 1 + (- (- (+ (- (1)))));

int i = 1 + (- (- (+ (-1))));

int i = 1 + (- (- (- 1)));

int i = 1 + (- (1));

int i = 1 + -1;

int i = 1 - 1;

int i = 0;

(which you reported.)

Alan Bellingham
--
Team Thai Kingdom
<url:http://www.borland.com/newsgroups/> Borland newsgroup descriptions
<url:http://www.borland.com/newsgroups/netiquette.html> netiquette
Back to top
Alex Bakaev [TeamB]
Guest





PostPosted: Thu May 10, 2007 4:57 am    Post subject: Re: Weird + - behavior Reply with quote



Alan Bellingham wrote:

Quote:
I guess you got the parser confused, and it keeps applying unary


I don't think it's confused. At least it's as confused as Comeau online Smile
Back to top
Eliot Frank
Guest





PostPosted: Thu May 10, 2007 6:08 pm    Post subject: Re: Weird + - behavior Reply with quote

Bruce Salzman wrote:
Quote:
Why doesn't this generate a syntax error in BDS2006?

int i = 1 + - - + - 1;

In C++, plus ('+') is a unary operator just like minus ('-'). So you
have an expression of the form,

expr1 '+' expr2

where expr1 is 1 and expr2 is - - + - 1. Then expr2 is parsed as

unaryop- expr3

where unaryop- is - and expr3 is - + - 1. Etc etc etc.

-Eliot
Back to top
Chris Uzdavinis (TeamB)
Guest





PostPosted: Thu May 10, 2007 6:21 pm    Post subject: Re: Weird + - behavior Reply with quote

Alan Bellingham <alanb (AT) episys (DOT) com> writes:

Quote:
" Bruce Salzman" <bruce (AT) nospam (DOT) org> wrote:

Why doesn't this generate a syntax error in BDS2006?

#include <iostream

#pragma argsused
int main(int argc, char* argv[])
{
int i = 1 + - - + - 1;
std::cout << i;
return 1;
}

(It prints 0, BTW).

I guess you got the parser confused, and it keeps applying unary
operators to the following expression. i.e.

Do you mean by this that you think it is wrong? If so, which part of
the expression?

I think this is a valid program:

int main() {

- - - - - - 1;
}

Why shouldn't it be valid? Unary negation has right-to-left
associtivity.

--
Chris (TeamB);
Back to top
Chris Uzdavinis (TeamB)
Guest





PostPosted: Thu May 10, 2007 6:25 pm    Post subject: Re: Weird + - behavior Reply with quote

Eliot Frank <eliot_h_frank (AT) yahoosky (DOT) comski> writes:

Quote:
Bruce Salzman wrote:
Why doesn't this generate a syntax error in BDS2006?
int i = 1 + - - + - 1;

In C++, plus ('+') is a unary operator just like minus ('-').

They are both unary and binary, depending on usage.

Quote:
So you have an expression of the form,

expr1 '+' expr2

Here + is binary.

Quote:

where expr1 is 1 and expr2 is - - + - 1. Then expr2 is parsed as
unaryop- expr3

Here + is unary.

Quote:
where unaryop- is - and expr3 is - + - 1. Etc etc etc.

Exactly. This is simple right-to-left associtivity of the unary
operator+ and opeartor-.


Chris (TeamB);
Back to top
Bruce Salzman
Guest





PostPosted: Thu May 10, 2007 7:53 pm    Post subject: Re: Weird + - behavior Reply with quote

"Chris Uzdavinis (TeamB)" <chris (AT) uzdavinis (DOT) com> wrote in message
news:86r6postdk.fsf (AT) explicit (DOT) atdesk.com...
Quote:
Exactly. This is simple right-to-left associtivity of the unary
operator+ and opeartor-.


Yikes! I didn't realize that you could string unary operators together
that way. I stumbled over this by way of a typo:
+ +i
instead of
++i

An optional warning would be nice :^)

--
Bruce
Back to top
Chris Uzdavinis (TeamB)
Guest





PostPosted: Thu May 10, 2007 7:57 pm    Post subject: Re: Weird + - behavior Reply with quote

" Bruce Salzman" <bruce (AT) nospam (DOT) org> writes:

Quote:
"Chris Uzdavinis (TeamB)" <chris (AT) uzdavinis (DOT) com> wrote in message
news:86r6postdk.fsf (AT) explicit (DOT) atdesk.com...
Exactly. This is simple right-to-left associtivity of the unary
operator+ and opeartor-.


Yikes! I didn't realize that you could string unary operators together
that way. I stumbled over this by way of a typo:
+ +i
instead of
++i

An optional warning would be nice :^)

Probably the biggest surprise is when we accidently insert a space;

for (int i = 10; i > 0; - -i)
{
std::cout << i << std::endl;
}

But mathematically it makes sense to be able to negate something, even
if that something is itself negating something-else, and so on.

Perhaps a warning would be useful, provided there was a syntax that
could indicate you meant to do this. Perhaps using parenthesis would
silence the warning:

x = -(-i); // ok, no warning
x = - -i ; // warning

But it still seems an argument in favor of fully parenthesizing every
expression, because someone may get confused. Basic math is simple,
but throw in the comma, some bit-shifts, a ternary ?: operator, some
address-of and dereferencing, array indexing, and suddenly it's not
clear at all.

This is one reason I like the syntax of scheme. There simply are no
precedence charts to remember, and there are never ambiguities like
this. The "problem" of lots of parenthesis was solved 40 years ago
with features in the editor: pretty-printing, paren balancing,
colorization, and "jump to corresponding delimiter" options. :)

--
Chris (TeamB);
Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (Language C++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.