Forum: Ruby operator precedence of assignment

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Daniel DeLorme (Guest)
on 2007-01-02 14:27
(Received via mailing list)
How do you think ruby parses this expression?
   a = 1 + b = 2 + c = 4 + d = 8

Originally I thought that operator precedence would result in
   a = (1 + b) = (2 + c) = (4 + d) = 8
and since an assignment requires a variable, that would result in a
SyntaxError.
But instead it seems that everything on the right side of the assignment
operator is evaluated first:
   a = (1 + (b = (2 + (c = (4 + (d = 8))))))

That had me really puzzled at first but I guess the behavior makes a
certain
sense, and it's more useful than a syntax error. Ruby's tolerant parser
wins again!

Daniel
06e8ca56db1b93248c94037070833d91?d=identicon&s=25 Gavin K. (phrogz)
on 2007-01-02 17:02
(Received via mailing list)
Daniel DeLorme wrote:
> How do you think ruby parses this expression?
>    a = 1 + b = 2 + c = 4 + d = 8

Per...
http://phrogz.net/ProgrammingRuby/language.html#op...
...the + operator has much higher precedence than the assignment
operator. That's interesting that it doesn't parse as you expected.
It's what I would have expected, too.
Yukihiro Matsumoto (Guest)
on 2007-01-02 17:56
(Received via mailing list)
Hi,

In message "Re: operator precedence of assignment"
    on Tue, 2 Jan 2007 22:26:53 +0900, Daniel DeLorme <dan-ml@dan42.com>
writes:
|
|How do you think ruby parses this expression?
|   a = 1 + b = 2 + c = 4 + d = 8
|
|Originally I thought that operator precedence would result in
|   a = (1 + b) = (2 + c) = (4 + d) = 8
|and since an assignment requires a variable, that would result in a SyntaxError.
|But instead it seems that everything on the right side of the assignment
|operator is evaluated first:
|   a = (1 + (b = (2 + (c = (4 + (d = 8))))))
|
|That had me really puzzled at first but I guess the behavior makes a certain
|sense, and it's more useful than a syntax error. Ruby's tolerant parser wins again!

Ah, for your information, that surprised me as well.  Perhaps bison
generated parser is smarter and eagerer than I expected.  I have no
idea what to do.

							matz.
Robert Klemme (Guest)
on 2007-01-02 18:06
(Received via mailing list)
On 02.01.2007 17:56, Yukihiro Matsumoto wrote:
> |and since an assignment requires a variable, that would result in a SyntaxError.
> |But instead it seems that everything on the right side of the assignment
> |operator is evaluated first:
> |   a = (1 + (b = (2 + (c = (4 + (d = 8))))))
> |
> |That had me really puzzled at first but I guess the behavior makes a certain
> |sense, and it's more useful than a syntax error. Ruby's tolerant parser wins again!
>
> Ah, for your information, that surprised me as well.  Perhaps bison
> generated parser is smarter and eagerer than I expected.  I have no
> idea what to do.

I think Daniel did not want you to do anything - in fact he seems rather
positively amazed. :-)

(The only thing that comes to mind is thank him for the praise. :-))

Kind regards

	robert
Yukihiro Matsumoto (Guest)
on 2007-01-02 18:10
(Received via mailing list)
Hi,

In message "Re: operator precedence of assignment"
    on Wed, 3 Jan 2007 02:05:06 +0900, Robert Klemme
<shortcutter@googlemail.com> writes:

|I think Daniel did not want you to do anything - in fact he seems rather
|positively amazed. :-)

_That_ made me wonder what I should do.

|(The only thing that comes to mind is thank him for the praise. :-))

The praise should go to yacc, not me. ;-)

							matz.
Robert Klemme (Guest)
on 2007-01-02 22:02
(Received via mailing list)
On 02.01.2007 18:09, Yukihiro Matsumoto wrote:
> |(The only thing that comes to mind is thank him for the praise. :-))
>
> The praise should go to yacc, not me. ;-)

But *you* chose it, didn't you?  So... :-)

	robert
This topic is locked and can not be replied to.