Topic: Controlling precedence rules?


Author: sor@rs6.iaee.tuwien.ac.at (Zhenya Sorokin)
Date: 20 Jan 1995 07:59:49 GMT
Raw View
In some program, both matrix multiplication and matrix-vector
multiplication are defined. I.e. there are defined both A=B*C and
x=A*y (uppercase matrix, lowercase vector).
However, an expression like
 x=A*B*y
may be expanded as x=(A*B)*y and as x=A*(B*y).


What are the default precedence rules is this case?
Is this defined by the language standard? If yes, how?
Can this be a subject of compiler's optimization?
Does the usage of a standard symbol * change something (suppose I
redefine matrix-vector product to use non-standard symbol, e.g.
x=A^y)?

--
Zhenya Sorokin

E-mail     : sorokin@ps1.iaee.tuwien.ac.at, sor@rs6.iaee.tuwien.ac.at
Paper-mail : E. Sorokin, Gusshausstr. 27/359-9, 1040 Vienna, Austria
Voice-mail : +43(1)58801-3703, -3948
Flame-mail : /dev/null




Author: pjl@graceland.att.com (Paul J. Lucas)
Date: Fri, 20 Jan 1995 14:07:03 GMT
Raw View
In <3fnqhl$382@news.tuwien.ac.at> sor@rs6.iaee.tuwien.ac.at (Zhenya Sorokin) writes:

>In some program, both matrix multiplication and matrix-vector
>multiplication are defined. I.e. there are defined both A=B*C and
>x=A*y (uppercase matrix, lowercase vector).
>However, an expression like
> x=A*B*y
>may be expanded as x=(A*B)*y and as x=A*(B*y).

>What are the default precedence rules is this case?

 The same as always.  Operator overload does not affect
 precedence.

>Is this defined by the language standard? If yes, how?

 Consult the handy operator chart found in most C++ books.

>Can this be a subject of compiler's optimization?

 No.

>Does the usage of a standard symbol * change something (suppose I
>redefine matrix-vector product to use non-standard symbol, e.g.
>x=A^y)?

 Then you get different precedence (and associativity) and
 confused readers.
--
 - Paul J. Lucas   #ifndef COMMON_KNOWLEDGE
   AT&T Bell Laboratories #include <stddisclaimer.h>
   Naperville, IL  #endif




Author: fjh@munta.cs.mu.OZ.AU (Fergus Henderson)
Date: Sat, 21 Jan 1995 01:11:05 GMT
Raw View
sor@rs6.iaee.tuwien.ac.at (Zhenya Sorokin) writes:

>In some program, both matrix multiplication and matrix-vector
>multiplication are defined. I.e. there are defined both A=B*C and
>x=A*y (uppercase matrix, lowercase vector).
>However, an expression like
> x=A*B*y
>may be expanded as x=(A*B)*y and as x=A*(B*y).

No, the grammar makes it clear that it can only be parsed
as `x = (A * B) * y'.

>What are the default precedence rules is this case?

The binary "*" operator is left-associative.

>Is this defined by the language standard? If yes, how?

Yes, it's defined by the language grammar.

>Can this be a subject of compiler's optimization?

No.  Or to be more accurate, the compiler can only do those sorts
of optimizations in cases where the operator really is associative,
so that the behaviour of the program remains the same.

>Does the usage of a standard symbol * change something (suppose I
>redefine matrix-vector product to use non-standard symbol, e.g.
>x=A^y)?

Yes, it does depend on which operator you use.  Some of the binary
operators are left-associative and others are right-associative.

--
Fergus Henderson - fjh@munta.cs.mu.oz.au
all [L] (programming_language(L), L \= "Mercury") => better("Mercury", L) ;-)




Author: stanleyr@acf4.nyu.edu (Rick Stanley)
Date: 20 Jan 1995 12:57:48 GMT
Raw View
sor@rs6.iaee.tuwien.ac.at (Zhenya Sorokin) writes:


>In some program, both matrix multiplication and matrix-vector
>multiplication are defined. I.e. there are defined both A=B*C and
>x=A*y (uppercase matrix, lowercase vector).
>However, an expression like
> x=A*B*y
>may be expanded as x=(A*B)*y and as x=A*(B*y).

>
>What are the default precedence rules is this case?
>Is this defined by the language standard? If yes, how?
>Can this be a subject of compiler's optimization?
>Does the usage of a standard symbol * change something (suppose I
>redefine matrix-vector product to use non-standard symbol, e.g.
>x=A^y)?

The operator precedence and associativity is the same for built in types,
and for user defined overloaded operator functions.  You cannot change
the precedence or associativity except by forcing it with ()'s.

x = A * B * y;  will be eveluated as x = ((A * B) * y);

This is the same for class objects as well as ints.  Check you manuals
and reference books (ARM, C++ FAQ, etc...) for more information.

--
Rick Stanley                         |              RSI
stanleyr@acf4.nyu.edu                |         (212) 740-6600
Information Technologies Institute   |        C++ & C Language
New York University   NYC, NY  USA   | Programming, Training, Consulting