help-octave
[Top][All Lists]
Advanced

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

Re: precedence


From: John W. Eaton
Subject: Re: precedence
Date: Tue, 6 Aug 2002 15:49:54 -0500

On  6-Aug-2002, Paul Kienzle <address@hidden> wrote:

| On Tue, Aug 06, 2002 at 12:40:23PM -0500, John W. Eaton wrote:
|
| >    1. Parentheses '('
| >    2. Power (.^), matrix power (.^)
| >    3. Unary plus (+), Unary minus (-), logical negation (~)
| >    4. Transpose (.'), complex conjugate transpose ('),
| 
| Really?  So why does A^x' equal A^(x') in Octave 2.1.35?

Oops.  I took the list from the precedence table in the parse.y file,
but there is more to it than that.  I believe those rules only apply
when there is a shift-reduce conflict, and the parser has to decide
whether to shift another token or reduce using one of the rules.  But
for this case, we have

  postfix_expr    : primary_expr
                    ...
                  | postfix_expr TRANSPOSE
                  ;

  prefix_expr     : postfix_expr
                    ...
                  ;

  binary_expr     : prefix_expr POW prefix_expr
                    ...
                  ;

so the token sequence  A ^ x ' is parsed as  A ^ ( x ' ) because the
POW operator needs to have two prefix expressions.  So the precedence
rules aren't used.  I will have to rework the grammar a bit to get the
Matlab-compatible behavior.

| > Differences:
| > 
| >   * In Matlab, transpose and power have the same precedence, higher
| >     than unary +/- and negation.
| 
| Transpose and unary negation are associative, so no problem switching
| the precedence of those.
| 
| I would rather have A^x' equal (A^x)' than the current octave behaviour of
| A^x' equal A^(x').

OK.

| >   * In Matlab, element-wise AND (&) has higher precedence than
| >     element-wise OR (|), in Octave they have the same precedence.
| > 
| >   * In Matlab, short-circuit AND (&&) has higher precedence than
| >     short-circuit OR (||), in Octave they have the same precedence.
| 
| I use parentheses when mixing "and" and "or" in an expression so
| that I don't have to think about precedence, so this won't affect me.
| 
| I believe matlab's precedence corresponds to the usual convention
| in logic so I suppose it is a better choice.  

Right, it is proably better.  I'm not sure why I chose to make these
have the same precedence.  I think I would have followed C (same as
Matlab is now) unless there was a good reason not to (like Matlab had
them at the same level way back when I first checked).  Perhaps Matlab
changed at some point?

| You might consider warning users if they have mixed "and" and "or" in the
| same expression.

Sure, that would be good to do.

Thanks,

jwe



-------------------------------------------------------------
Octave is freely available under the terms of the GNU GPL.

Octave's home on the web:  http://www.octave.org
How to fund new projects:  http://www.octave.org/funding.html
Subscription information:  http://www.octave.org/archive.html
-------------------------------------------------------------



reply via email to

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