Topic: Operator precedence
Author: Pieter van Beek <vanbeek@kobasoft.nl>
Date: 1999/12/28 Raw View
Hi all,
There seems to be no operator precedence table in de ansi C++ standard.
I always used the one from Stroustrup, and only found out yesterday this
topic is not covered by the standard.
AIMS?
Pieter.
--
KOBAYASHI SOFTWARE
Tel. +31 20 4165015
Rich text (HTML) e-mail is preferred
PGP Key ID: 0x823C9CEC
Fingerprint: CE4E E2D5 C18C 45AD 2DBD 08E5 BACA 342F 823C 9CEC
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Ron Natalie <ron@sensor.com>
Date: 1999/12/29 Raw View
Pieter van Beek wrote:
>
> Hi all,
>
> There seems to be no operator precedence table in de ansi C++ standard.
> I always used the one from Stroustrup, and only found out yesterday this
> topic is not covered by the standard.
>
> AIMS?
>
Precedence is not defined by that table, even in C, it is implied by
the language grammar. The inclusion of the table in K&R and Stroustrup
is purely informational. Note the comment from Stroustrup at the bottom
of page 121:
A few grammar rules cannot be expressed in terms of precedence
(also known as binding strength) and associativity.
This has lead to some confusion with the ?: operator interpretation.
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: James Kuyper <kuyper@wizard.net>
Date: 1999/12/29 Raw View
Pieter van Beek wrote:
>
> Hi all,
>
> There seems to be no operator precedence table in de ansi C++ standard.
> I always used the one from Stroustrup, and only found out yesterday this
> topic is not covered by the standard.
The C++ language cannot be described exactly in terms of precedence
rules for operators. Instead, grammar rules are used. Some of those
grammar rules imply relationships which are equivalent to those
described by precedence rules, but others are impossible to describe
that way.
The ?: operator, in particular, causes problems simply because it's the
only ternary operator. It's been argued that '?expression:' can be
thought of as being a binary operator, and that when considered as such,
has effectively equal precedence with assignment operators.
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Darin Adler <darin@bentspoon.com>
Date: 1999/12/29 Raw View
Pieter van Beek <vanbeek@kobasoft.nl> wrote:
> There seems to be no operator precedence table in de ansi C++ standard.
> I always used the one from Stroustrup, and only found out yesterday this
> topic is not covered by the standard.
The reason for this is that you can't precisely define the relationships
between the C++ operators in terms of a precedence table. Stroustrup writes
in his book, right below the operator summary chart, "A few [of the C++]
grammar rules cannot be expressed in terms of precedence (also known as
binding strength) and associativity."
In the standard, where it's important to be precise, a chart would do harm,
because it would be redundant and would necessarily conflict with the
information already present in the grammar in the cases where the rules
can't be expressed in terms of precedence. Thus the standard doesn't define
the syntax in terms of "precedence", "binding strength", or "associativity";
instead it presents the grammar.
The standard also addresses this issue explicitly. A footnote in section 5/4
says, "The precedence of operators is not directly specified, but it can be
derived from the syntax."
-- Darin
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: nimel@my-deja.com
Date: 1999/12/29 Raw View
In article <3868B1A1.D47627B4@kobasoft.nl>,
Pieter van Beek <vanbeek@kobasoft.nl> wrote:
> Hi all,
>
> There seems to be no operator precedence table in de ansi C++
> standard.
No there isn't.
> I always used the one from Stroustrup, and only found out yesterday
> this topic is not covered by the standard.
Of course it is covered. See Annex A.
/Niklas Mellin
Sent via Deja.com http://www.deja.com/
Before you buy.
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Pieter van Beek <vanbeek@kobasoft.nl>
Date: 1999/12/29 Raw View
Ron Natalie wrote:
> Pieter van Beek wrote:
> >
> > Hi all,
> >
> > There seems to be no operator precedence table in de ansi C++ standard.
> > I always used the one from Stroustrup, and only found out yesterday this
> > topic is not covered by the standard.
> >
> > AIMS?
> >
>
> Precedence is not defined by that table, even in C, it is implied by
> the language grammar. The inclusion of the table in K&R and Stroustrup
> is purely informational. Note the comment from Stroustrup at the bottom
> of page 121:
> A few grammar rules cannot be expressed in terms of precedence
> (also known as binding strength) and associativity.
>
> This has lead to some confusion with the ?: operator interpretation.
If this is so, then how know, from the standard, how for example
*array_of_arrays[3] will be interpreted? Or *foo->bar() ?
Pieter
--
KOBAYASHI SOFTWARE
Tel. +31 20 4165015
Rich text (HTML) e-mail is preferred
PGP Key ID: 0x823C9CEC
Fingerprint: CE4E E2D5 C18C 45AD 2DBD 08E5 BACA 342F 823C 9CEC
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Pieter van Beek <vanbeek@kobasoft.nl>
Date: 1999/12/29 Raw View
Ron Natalie wrote:
> Pieter van Beek wrote:
> >
> > Hi all,
> >
> > There seems to be no operator precedence table in de ansi C++ standard.
> > I always used the one from Stroustrup, and only found out yesterday this
> > topic is not covered by the standard.
> >
> > AIMS?
> >
>
> Precedence is not defined by that table, even in C, it is implied by
> the language grammar. The inclusion of the table in K&R and Stroustrup
> is purely informational. Note the comment from Stroustrup at the bottom
> of page 121:
> A few grammar rules cannot be expressed in terms of precedence
> (also known as binding strength) and associativity.
>
> This has lead to some confusion with the ?: operator interpretation.
If this is so, then how know, from the standard, how for example
*array_of_arrays[3] will be interpreted? Or *foo->bar() ?
Pieter
--
KOBAYASHI SOFTWARE
Tel. +31 20 4165015
Rich text (HTML) e-mail is preferred
PGP Key ID: 0x823C9CEC
Fingerprint: CE4E E2D5 C18C 45AD 2DBD 08E5 BACA 342F 823C 9CEC
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: James Kuyper <kuyper@wizard.net>
Date: 1999/12/29 Raw View
Pieter van Beek wrote:
>
> Ron Natalie wrote:
....
> > Precedence is not defined by that table, even in C, it is implied by
> > the language grammar. The inclusion of the table in K&R and Stroustrup
> > is purely informational. Note the comment from Stroustrup at the bottom
> > of page 121:
> > A few grammar rules cannot be expressed in terms of precedence
> > (also known as binding strength) and associativity.
> >
> > This has lead to some confusion with the ?: operator interpretation.
>
> If this is so, then how know, from the standard, how for example
> *array_of_arrays[3] will be interpreted? Or *foo->bar() ?
Here are the relevant grammar rules:
5.1 p1:
_primary-expression_:
...
_id-expression_
_id-expression_:
_unqualified-id_
_qualified-id_
_unqualified-id_:
_identifier_
...
5.2 p1:
_postfix-expression_:
_primary-expression_
_postfix-expression_ [ _expression_ ]
...
5.3 p1:
_unary-expression_:
_postfix-expression_
...
_unary-operator_ _cast-expression_
...
_unary-operator_: one of
* & + - | ~
5.4 p2:
_cast-expression_:
_unary-expression_
( _type-id_ ) _cast-expression_
Thus, *array_of_arrays[3] is parsed as follows:
* array_of_arrays [ 3 ]
_identifier_
_unqualified-id_
_id-expression_
_primary-expression_ [ _expression_ ]
_postfix-expression_
_unary_expression_
_unary-operator_ _cast_expression_
_unary-expression_
The fact that [] is applies ahead of * is therefore encoded in the
grammar itself, without need for separate precedence rules.
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: "Bill Wade" <bill.wade@stoner.com>
Date: 1998/01/30 Raw View
Zhiming Zhou wrote in message <34D0D0B5.BFB28C88@utsw.swmed.edu>...
>In many books, including "The C++ Programming language, 2ed" (pp. 90),
>conditional operator (? :) has higher precedence than assignment (=).
>However, in the book "The C++ Programming language, 3ed" (pp. 121), it's
>the other way around. Can somebody tell me why?
The answer is not symmetric. The conditional operator has a higher
precedence than an assignment on its left. It has a lower precedence than
an assignment on its right. It has a lower precedence than anything (even a
comma expression) in its middle.
The meaning of
a = b ? c = d : e = f;
is
a = ( b ? (c=d) : (e = f) )
So ?: has a higher precedence than the assignment on the left, but lower
precedence than the assignment on the right.
This may be clearer from the grammar
conditional-exp:
logical-or-exp:
logical-or-exp ? exp : assignment-exp
assignment-exp:
conditional-exp
logical-or-exp assignment-operator assignment-exp
throw-exp
exp:
assignment-exp
exp,assignment-exp
In the example above there is no parenthesis around
(a=b)
because the left argument of ?: cannot be an assignment. There must be
parenthesis around (e=f) because the left argument of assignment may not be
a conditional.
Ignoring the middle argument I think you'd be correct in saying that
assignment and conditional have the same precedence and bind right to left.
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: Zhiming Zhou <zhou02@UTSW.SWMED.EDU>
Date: 1998/01/29 Raw View
In many books, including "The C++ Programming language, 2ed" (pp. 90),
conditional operator (? :) has higher precedence than assignment (=).
However, in the book "The C++ Programming language, 3ed" (pp. 121), it's
the other way around. Can somebody tell me why?
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: clamage@Eng.Sun.COM (Steve Clamage)
Date: 1998/01/30 Raw View
In article BFB28C88@utsw.swmed.edu, Zhiming Zhou <zhou02@UTSW.SWMED.EDU> writes:
>In many books, including "The C++ Programming language, 2ed" (pp. 90),
>conditional operator (? :) has higher precedence than assignment (=).
>However, in the book "The C++ Programming language, 3ed" (pp. 121), it's
>the other way around. Can somebody tell me why?
It's an error in the book. See the errata list on Stroustrup's home page
http://www.research.att.com/~bs/3rd_errata.html
---
Steve Clamage, stephen.clamage@sun.com
---
[ 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 ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]