Topic: Precedence of operators


Author: joerg.barfurth@attglobal.net (Joerg Barfurth)
Date: Thu, 22 Feb 2001 23:34:08 GMT
Raw View
Martin Aupperle <MikeAlpha@NoSpam_csi.com> wrote:

> The Standard sys:
>=20
> "The precedence of operators is not directly specified but can be
> derived from the syntax."
>=20
> This means that there *is* a clearly defined precedence (otherwise it
> could  not be derived).=20

I think your conclusion goes too far. Particularly as there *isn't* a
clearly defined precedence (in the sense you seem to have in mind) ;-)

Furthermore your quote is from a (non-normative) footnote, so you cannot
derive normative conclusions from it.

For any particular expression the precedence of operator instances in
the expression (i.e. binding of operands, associativity of operators and
therefore partial ordering of evaluation) can be derived from the
syntax.=20

For some combinations of operators, but not for all, these rules could
be reduced to operator precedence (in the global sense).

> Now I have 2 questions:
>=20
> 1.) How can I derive it?=20

If you use the syntax grammar to build the (unique) parse tree for an
expression, which faithfully has nodes for all nonterminals, this tree
represents the order of evaluation of subexpressions. IOW you could use
this tree to generate a fully parenthesized version of the same
expression (by putting parantheses around each non-terminal), which
would have the same meaning as the original expression.

> 2.) Why does the Standard not give it explicitely, if it is well
> defined?=20

There are cases where precedence isn't easily stated as a sequential
ordering of operators and a simple left/right associativity.

Notably the grammar defines:

    conditional-expression:
        logical-or-expression ? expression : assignment-expression

    assignment-expression:
        conditional-expression
        logical-or-expression assignment-operator assignment-expression
        throw expression

    expression
        assignment-expression
        expression , assignment-expression

So the expression

   a1=3Db1,c1=3Dd1 ? a2=3Db2,c2=3Dd2 : a3=3Db3,c3=3Dd3;

is parsed as (IMHO)

   ( a1=3Db1 , c1=3D(d1 ? (a2=3Db2 , c2=3Dd2) : a3=3Db3) ), c3=3Dd3;

[NOTE: I have omitted many parens and used whitespace instead for
readability]
  =20
So, while top-level operators from a logical-or-expression (e.g. '||')
have 'higher precedence' than any of comma (','), assigment (e.g. '=3D')
and conditional ('?:') operators, the relationship between the latter
ones can not simply be described by precedence and associativity.

Assignment has 'higher precedence' than comma. But the conditional
operator has a 'mixed precedence' wrt assignment, depending on operand
position. And between the '?' and ':' of the ternary operator even comma
takes 'higher precedence' than the conditional.

BTW: iirc this even departs from the definition that is used in C.

HTH, J=F6rg

--=20
J=F6rg Barfurth                         joerg.barfurth@attglobal.net
<<<<<<<<<<<<< using std::disclaimer;  <<<<<<<<<<<<<<<<<<<<<<<<<<<<
Software Developer                    http://www.OpenOffice.org
StarOffice Configuration              http://www.sun.com/staroffice

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]





Author: Bill Wade <wrwade@swbell.net>
Date: Sat, 24 Feb 2001 16:18:52 GMT
Raw View
I take the view that a few operators (function call, parenthesis and
conditional) may have one or more middle operands that higher precedence,
compared to the rest of the expression.

Ignoring "middle" operands, you can say that assignment and conditional both
have the same precedence and associate right to left.

I admit that this view of precedence and associativity isn't much clearer
than the grammar view.



"Joerg Barfurth" <joerg.barfurth@attglobal.net> wrote

So, while top-level operators from a logical-or-expression (e.g. '||')
have 'higher precedence' than any of comma (','), assigment (e.g. '=')
and conditional ('?:') operators, the relationship between the latter
ones can not simply be described by precedence and associativity.

Assignment has 'higher precedence' than comma. But the conditional
operator has a 'mixed precedence' wrt assignment, depending on operand
position. And between the '?' and ':' of the ternary operator even comma
takes 'higher precedence' than the conditional.


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]





Author: "Ken Hagan" <K.Hagan@thermoteknix.co.uk>
Date: Mon, 26 Feb 2001 22:37:35 GMT
Raw View
"Joerg Barfurth" <joerg.barfurth@attglobal.net> wrote...
>
> For some combinations of operators, but not for all, these rules could
> be reduced to operator precedence (in the global sense).
>
> There are cases where precedence isn't easily stated as a sequential
> ordering of operators and a simple left/right associativity.
>
> So the expression
>    a1=b1,c1=d1 ? a2=b2,c2=d2 : a3=b3,c3=d3;
> is parsed as (IMHO)
>    ( a1=b1 , c1=(d1 ? (a2=b2 , c2=d2) : a3=b3) ), c3=d3;

Would I be correct in guessing that apart from the ternary operator,
precedence in C++ works out the same as in C and as written in the
various tables that one sees in books etc?

Put another way, is the problem just with ?: ? (!)

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]





Author: MikeAlpha@NoSpam_csi.com (Martin Aupperle)
Date: Wed, 21 Feb 2001 19:51:16 GMT
Raw View
The Standard sys:

"The precedence of operators is not directly specified but can be
derived from the syntax."

This means that there *is* a clearly defined precedence (otherwise it
could  not be derived). Now I have 2 questions:

1.) How can I derive it?
2.) Why does the Standard not give it explicitely, if it is well
defined?

Thanks - Martin


------------------------------------------------
Martin Aupperle
MikeAlpha@NoSpam_csi.com
(remove NoSpam_)
------------------------------------------------

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]





Author: "James Kuyper Jr." <kuyper@wizard.net>
Date: Thu, 22 Feb 2001 05:10:12 GMT
Raw View
Martin Aupperle wrote:
>
> The Standard sys:
>
> "The precedence of operators is not directly specified but can be
> derived from the syntax."
>
> This means that there *is* a clearly defined precedence (otherwise it
> could  not be derived). Now I have 2 questions:
>
> 1.) How can I derive it?

Let's take, for example, the binary operators with highest precedence.
The relevant grammar rules are:

_multiplicative-expression:_
 _pm-expression
 _multiplicative-expression_ * _pm-expression_
 _multiplicative-expression_ / _pm-expression_
 _multiplicative-expression_ % _pm-expression_

_additive-expression:_
 _multiplicative-expression_
 _additive-expression_ + _multiplicative-expression_
 _additive-expression_ - _multiplicative-expression_

What these rules mean is that the following expression can only be
parsed as shown:

  a * b   + c
 _pm-expression_ * _pm-expression_ + _pm-expression
 _multiplicative-expression_    + _multiplicative-expression_
   _additive-expression_

Therefore, multiplication has higher precedence than addition, because
the only way to parse it recognises the multiplication first, then the
addition. Associativity is also encoded in these rules - it's determined
by which side of the binary rule the target type occurs on. The rule

 _multiplicative-expression_ / _pm-expression_

indicates that a/b/c is parsed as (a/b)/c. If the two sides were
reversed, it would indicate that the expression should be parsed as
a/(b/c).

> 2.) Why does the Standard not give it explicitely, if it is well
> defined?

The grammar can describe operator precedence and associativity, but it
can also describe more complicated structures that can't be described by
precedence and associativity alone. It's better to use a single
consistent method to describe the entire system, rather than to describe
one part of it using a grammar, and another part using precedence and
associativity.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]