Syntax bug, in 1.8.5? return not (some expr) <-- syntax error vsreturn (not (some expr)) <-- fine

Discussion in 'Ruby' started by Good Night Moon, Jul 22, 2007.

  1. Well, it's all in the subject, to me it looks like a parsing bug:

    class MyRect
    def intersects_good(other)
    return (not ((other.x1 < self.x0 or self.x1 < other.x0) and
    (other.y1 < self.y0 or self.y1 < other.y0)))
    end

    def intersects_bad(other)
    return not ((other.x1 < self.x0 or self.x1 < other.x0) and
    (other.y1 < self.y0 or self.y1 < other.y0))
    end
    end

    foo> ruby -c myrect.rb
    myrect.rb:8: syntax error, unexpected kNOT, expecting kEND
    return not ((other.x1 < self.x0 or self.x1 < other.x0) and
    ^

    Any comment? Is this known? Or just some hair in the Ruby grammar?
     
    Good Night Moon, Jul 22, 2007
    #1
    1. Advertising

  2. Good Night Moon

    Bil Kleb Guest

    Re: Syntax bug, in 1.8.5? return not (some expr) <-- syntax errorvs return (not (some expr)) <-- fine

    Good Night Moon wrote:
    >
    > myrect.rb:8: syntax error, unexpected kNOT, expecting kEND
    > return not ((other.x1 < self.x0 or self.x1 < other.x0) and


    You need to use the '!' operator instead of the 'not' "conjunction".
    It's a precedence thing -- the parser is seeing 'return not' -- see
    parse.y in the source.

    Regards,
    --
    Bil Kleb
    http://fun3d.larc.nasa.gov
     
    Bil Kleb, Jul 22, 2007
    #2
    1. Advertising

  3. Re: Syntax bug, in 1.8.5? return not (some expr) <-- syntax error vs return (not (some expr)) <-- fine

    On 22/07/2007, at 5:35 PM, Good Night Moon wrote:

    > Well, it's all in the subject, to me it looks like a parsing bug:
    >
    > class MyRect
    > def intersects_good(other)
    > return (not ((other.x1 < self.x0 or self.x1 < other.x0) and
    > (other.y1 < self.y0 or self.y1 < other.y0)))
    > end
    >
    > def intersects_bad(other)
    > return not ((other.x1 < self.x0 or self.x1 < other.x0) and
    > (other.y1 < self.y0 or self.y1 < other.y0))
    > end
    > end
    >
    > foo> ruby -c myrect.rb
    > myrect.rb:8: syntax error, unexpected kNOT, expecting kEND
    > return not ((other.x1 < self.x0 or self.x1 < other.x0) and
    > ^
    >
    > Any comment? Is this known? Or just some hair in the Ruby grammar?


    There's a difference between [not, and, or] and [!, &&, ||]
    Not sure what it is, but I've found it better to use the latter.
    Still, if you wrap the whole statement in another set of brackets
    (like intersects_good) it will work.

    def intersects_bad(other)
    return (not ((other.x1 < self.x0 or self.x1 < other.x0) and
    (other.y1 < self.y0 or self.y1 < other.y0)))
    end

    by the way, what's the difference between the two methods? They
    appear identical.

    Cheers,
    Dave
     
    Sharon Phillips, Jul 22, 2007
    #3
  4. Parsing bug with 'return not' (was, Syntax bug)

    On Sun, 22 Jul 2007 03:50:01 -0400, Bil Kleb wrote:
    >> myrect.rb:8: syntax error, unexpected kNOT, expecting kEND
    >> return not ((other.x1 < self.x0 or self.x1 < other.x0) and

    >
    > You need to use the '!' operator instead of the 'not' "conjunction".
    > It's a precedence thing -- the parser is seeing 'return not' -- see
    > parse.y in the source.


    Thanks, maybe I'll look. It's not just precedence though, or else
    'return' itself is part of it:

    def ok
    return (not true)
    end

    def fails
    return not true
    end

    And note that within an 'if', it works as you'd expect:

    def with_parens_ok
    if (not true)
    puts "not true"
    end
    end

    def no_parens_ok
    if not true
    puts "not true"
    end
    end
     
    Good Night Moon, Jul 22, 2007
    #4
  5. Re: Syntax bug, in 1.8.5? return not (some expr) <-- syntax errorvs return (not (some expr)) <-- fine

    On Sun, 22 Jul 2007 16:51:26 +0900, Sharon Phillips wrote:
    > There's a difference between [not, and, or] and [!, &&, ||] Not sure
    > what it is, but I've found it better to use the latter. Still, if you
    > wrap the whole statement in another set of brackets (like
    > intersects_good) it will work.
    >
    > def intersects_bad(other)
    > return (not ((other.x1 < self.x0 or self.x1 < other.x0) and
    > (other.y1 < self.y0 or self.y1 < other.y0)))
    > end
    >
    > by the way, what's the difference between the two methods? They appear
    > identical.


    Thanks. The difference is just to point out what looks like
    a bug; the parens shouldn't make a difference. Look at
    my other reply in this thread for elaboration.
     
    Good Night Moon, Jul 22, 2007
    #5
  6. Good Night Moon

    Guest

    Re: Syntax bug, in 1.8.5? return not (some expr) <-- syntax errorvs return (not (some expr)) <-- fine

    Hi --

    On Mon, 23 Jul 2007, Good Night Moon wrote:

    > On Sun, 22 Jul 2007 16:51:26 +0900, Sharon Phillips wrote:
    >> There's a difference between [not, and, or] and [!, &&, ||] Not sure
    >> what it is, but I've found it better to use the latter. Still, if you
    >> wrap the whole statement in another set of brackets (like
    >> intersects_good) it will work.
    >>
    >> def intersects_bad(other)
    >> return (not ((other.x1 < self.x0 or self.x1 < other.x0) and
    >> (other.y1 < self.y0 or self.y1 < other.y0)))
    >> end
    >>
    >> by the way, what's the difference between the two methods? They appear
    >> identical.

    >
    > Thanks. The difference is just to point out what looks like
    > a bug; the parens shouldn't make a difference. Look at
    > my other reply in this thread for elaboration.


    I don't think it's a bug. Have a look at these examples:

    def x; return 1 and puts "I'm gone!"; end
    SyntaxError: compile error
    (irb):3: void value expression

    def x; 2 not 3; end
    SyntaxError: compile error
    (irb):5: syntax error, unexpected kNOT, expecting kEND

    These both make sense. 'and' is right-associative, so you're trying
    to do something after having already returned, which doesn't work.
    And in general you can't do "x not y", which is what "return not ..."
    is read as.


    David

    --
    * Books:
    RAILS ROUTING (new! http://www.awprofessional.com/title/0321509242)
    RUBY FOR RAILS (http://www.manning.com/black)
    * Ruby/Rails training
    & consulting: Ruby Power and Light, LLC (http://www.rubypal.com)
     
    , Jul 22, 2007
    #6
  7. Re: Parsing bug with 'return not' (was, Syntax bug)

    On 7/22/07, Good Night Moon <> wrote:
    > On Sun, 22 Jul 2007 03:50:01 -0400, Bil Kleb wrote:
    > >> myrect.rb:8: syntax error, unexpected kNOT, expecting kEND
    > >> return not ((other.x1 < self.x0 or self.x1 < other.x0) and

    > >
    > > You need to use the '!' operator instead of the 'not' "conjunction".
    > > It's a precedence thing -- the parser is seeing 'return not' -- see
    > > parse.y in the source.

    >
    > Thanks, maybe I'll look. It's not just precedence though, or else
    > 'return' itself is part of it:


    if binds differently than return. It is precedence.
     
    Gregory Brown, Jul 23, 2007
    #7
  8. Good Night Moon

    Roger Pack Guest

    Re: Syntax bug, in 1.8.5? return not (some expr) <-- syntax

    I agree that
    if not File.exists? "a"
    do stuff
    end

    would be nice intuitively. I wish...

    unknown wrote:
    > Hi --
    >
    > On Mon, 23 Jul 2007, Good Night Moon wrote:
    >
    >>>
    >>> by the way, what's the difference between the two methods? They appear
    >>> identical.

    >>
    >> Thanks. The difference is just to point out what looks like
    >> a bug; the parens shouldn't make a difference. Look at
    >> my other reply in this thread for elaboration.

    >
    > I don't think it's a bug. Have a look at these examples:
    >
    > def x; return 1 and puts "I'm gone!"; end
    > SyntaxError: compile error
    > (irb):3: void value expression
    >
    > def x; 2 not 3; end
    > SyntaxError: compile error
    > (irb):5: syntax error, unexpected kNOT, expecting kEND
    >
    > These both make sense. 'and' is right-associative, so you're trying
    > to do something after having already returned, which doesn't work.
    > And in general you can't do "x not y", which is what "return not ..."
    > is read as.
    >
    >
    > David


    --
    Posted via http://www.ruby-forum.com/.
     
    Roger Pack, Jul 23, 2007
    #8
  9. Good Night Moon

    F. Senault Guest

    Re: Syntax bug, in 1.8.5? return not (some expr) <-- syntax

    Le 24 juillet 2007 à 00:22, Roger Pack a écrit :

    > I agree that
    > if not File.exists? "a"
    > do stuff
    > end
    >
    > would be nice intuitively. I wish...


    unless File.exists? "a"
    do stuff
    end

    do stuff unless File.exists? "a"

    Isn't it even more intuitive ?

    Fred
    --
    If you no longer admin for the fsckers do they really exist?
    (Vagn Scott in the SDM)
     
    F. Senault, Jul 23, 2007
    #9
  10. Re: Parsing bug with 'return not' (was, Syntax bug)

    On 7/22/07, Gregory Brown <> wrote:
    > On 7/22/07, Good Night Moon <> wrote:
    > > On Sun, 22 Jul 2007 03:50:01 -0400, Bil Kleb wrote:
    > > >> myrect.rb:8: syntax error, unexpected kNOT, expecting kEND
    > > >> return not ((other.x1 < self.x0 or self.x1 < other.x0) and
    > > >
    > > > You need to use the '!' operator instead of the 'not' "conjunction".
    > > > It's a precedence thing -- the parser is seeing 'return not' -- see
    > > > parse.y in the source.

    > >
    > > Thanks, maybe I'll look. It's not just precedence though, or else
    > > 'return' itself is part of it:

    >
    > if binds differently than return. It is precedence.


    Except that return isn't an operator. As far as I can tell, looking
    at parse.y for ruby1.8.6 the parse tree for the original statement
    which raised the question

    return not true

    should be:

    command_call
    kRETURN expr
    kNOT expr
    primary
    var_ref
    variable
    kTRUE

    There definitely looks to be something strange here.

    --
    Rick DeNatale

    My blog on Ruby
    http://talklikeaduck.denhaven2.com/
     
    Rick DeNatale, Jul 25, 2007
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Irmen de Jong
    Replies:
    0
    Views:
    341
    Irmen de Jong
    Dec 1, 2003
  2. Bengt Richter
    Replies:
    6
    Views:
    333
    Paul Rubin
    Jul 14, 2005
  3. Johannes Schaub (litb)
    Replies:
    10
    Views:
    734
    Francesco S. Carta
    Oct 1, 2010
  4. Replies:
    1
    Views:
    113
  5. Replies:
    4
    Views:
    120
    Thomas 'PointedEars' Lahn
    Dec 2, 2005
Loading...

Share This Page