Re: A bug in the interpreter

new topic     » goto parent     » topic index » view thread      » older message » newer message

Darly Border wrote:

>I was experimenting with some code recently, when I discovered a
>bug in the parser. The following code will parse without
>complaint, but does not work properly.
>
>for n = 1 to 10 do
>   if 4 < n < 8 then
>      ? n
>   end if
>end for
>
>This program should display the numbers 5,6,and 7. It actually
>displays the number 1 through 10.
>
>The problem is with function rexpr() in parser.e which parses
>the expression 4 < n < 8. If the value of n is 1, it calcualtes
>4 < 1 which is FALSE. It then calculates FALSE < 8 which is
>meaningless because FALSE is not a number. Because TRUE and
>FALSE are represented by the numbers 1 and 0, the computer can
>perform the calcualtion. And because both 0 and 1 are less than
>8, the expression will evaluate to TRUE no matter what value n
>contains.

While I think "4 < n < 8" is a nice shortcut, what about
"(4 < n) < 8"?  It is also accepted by Euphoria.  How should it
be evaluated?

Perhaps a question to ask is are Euphoria expressions meant to be
more C-like or Pascal-like.  Or something in between?  Something
entirely different?

C will accept both of the above expressions, but will evaluate
both as the latter expression, where "4 < n" returns a 0 or 1
depending on the truth value of the expression.  Basically the
way Euphoria is evaluating it now.

Pascal will accept neither expression, because relational
operators require an integer expression, and "4 < n" is a boolean
expression, parenthesized or not.

It would be kind of cool if Euphoria could properly handle "4 < n
< 8", but you'd have to define 'properly', and you'd also have to
have special code for "(4 < n) < 8", so that the relational
operators behaved consistently.

I think it would bother me if Euphoria handled "4 < n < 8"
'properly' (e.g., not as it does now), but continued to support
"(4 < n) < 8".  That seems awfully inconsistent.

I think the viable solutions are:

1) Leave it the way it is.  It might be nice to document the
behavior.  Essentially C-like behavior.

2) Make expressions more Pascal-like, and thus prohibit "4 < n <
8" and "(4 < n) < 8" altogether.  The former expression is easy
to prohibit via a grammar change.  The latter would require
expressions to be typed as number vs. boolean. This might be a
lot of work, and it might introduce unwanted side effects.

3)  Make "4 < n < 8" work the way a non-C programmer thinks it
should work, e.g., "4 < n" and "n < 8".  And then prohibit "(4 <
n) < 8", as it would make no sense anymore.  You've already
solved the first problem.  However, as in number 2 above,
prohibiting the latter expression might be a lot of work, and it
might introduce unwanted side effects.

This reminds me of a question I've had:  is there an official
Euphoria grammar?

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu