octave-maintainers
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: undefined compound chaining behavior


From: Hossein Sajjadi
Subject: Re: undefined compound chaining behavior
Date: Thu, 12 Jun 2014 18:45:54 +0330

On 6/12/14, Jordi Gutiérrez Hermoso <address@hidden> wrote:
> On Wed, 2014-06-11 at 23:33 +0330, Hossein Sajjadi wrote:
>> To  show that undefined behavior is propagated from c++ through Octave
>> ,it is sufficient to prove that the compound operator that is used in
>> Octave is actually the operator that is used in C++.
>
> No, this is not sufficient. What is sufficient is to find UB in the
> C++ code, such as an expression like X += X += Y. Thankfully, you
> don't have to perform the search yourself, since compilers can do
> that. Since the compiler has not found such an expression in C++, it
> either does not exist or all compilers tested so far are buggy about
> this. Since it seems unlikely that all compilers are buggy, seeing how
> they do demonstrably detect this expression in other cases, it is safe
> to conclude such an expression does not exist.
>

It may be better to see the expression:
a=1
a+=a+=4

Duo to right associativity the expression grouped:
a+=(a+=4)
The c++ compiler may first evaluates lhs or rhs.
If lhs evaluated firt "a" evaluated and it's value (1) computed and
will be stored to be used later,
then rhs evaluated and value of "a" (1) sums with 4 results 5;
5 sums with 1 results 6.

If the compiler decides  to evaluate from right to left then the
result will be 10

I think this is the behavior of the gcc compiler that tranfers to
Octave so the expression always evaluated to 10.
But the standard imposes no requirement for the order of evaluation.

I did some test with Bison parser to understand  in the Bisom grammer :
{  $1->value.var += $3;$$ = $1->value.var;     }
is "+=" in c++ is the same as octave or not?
So I decide to modify mfcalc that is a simple infix calculator but it
does not have += operator.
In the examples directory I modify mfcalc.y and add the operator '@'
to represent +=
also I changed the precedence for '=' and '@' to be right associative.

%right '=' '@'
....
VAR '@' exp        {  $1->value.var += $3;$$ = $1->value.var;     }


in the command line type:
> bison mfcalc1.y
to produce a .c source file that can be compiled by the GCC to create
the calculator.
file mfcalc1.tab.c is produced. go to the line 1272 that we can see
definition of the operator += .
and appearance of += in .c code.
So we can conclude that it is C language that make decision that an
expression to be evaluated right to left or not and Octave can not
make any decision.

file mfcalc1.y attached.


-Hossein.

Attachment: mfcalc1.zip
Description: Zip archive


reply via email to

[Prev in Thread] Current Thread [Next in Thread]