I have heard many people saying that don't rely strictly on operator
precedence, instead evaluate expression
using C grammar rules.
They often give example about ternary operator.
But I still don't understant how ternary operator is not in accordance
with precedence.
I searched but couldn't find a satisfactory answer.
Please explain to me how that ternary operator is an exception.
And what are the other cases which don't follow precedence but rely on
grammar rules ?
There are many ways to describe a grammar. The ISO Standard
describes C's grammar with a version of Backus-Naur Form (also
known as Backus Normal Form, or BNF). This type of description
can be very precise, and has other advantages as well.
But BNF has disadvantages, too. One of them is that you can't
analyze a snippet of code in isolation to determine whether it's
syntactically correct or what it's meaning might be; you must parse
the entire translation unit to be sure. That's inconvenient for a
programmer who's looking at a single expression in the middle of
a big program, wondering whether `x << 4 + y' means '(x << 4) + y'
or `x << (4 + y)'.
"Precedence" and "associativity" are used in a different but
equivalent description of (part of) C's grammar. We can answer the
question of the previous paragraph by memorizing a simple rule: We say
that `+' has "higher precedence" or "binds more tightly" than `<<'.
Therefore the second interpretation above is the right one: The `+'
"takes precedence" over the `<<', just as `*' "takes precedence" over
`+' in `a + 3 * b'. This is a quicker route to comprehension than
assignment-expression
::= conditional-expression
::= logical-OR-expression
::= logical-AND-expression
::= inclusive-OR-expression
::= exclusive-OR-expression
::= AND-expression
::= equality-expression
::= relational-expression
::= shift-expression
::= shift-expression `<<' additive-expression
...
At this point (having identified just *one* of the expression's
five tokens!) we might take the shortcut of observing that the
`+' must be part of `additive-expression', so that its effect
is already incorporated in whatever the `<<' manipulates -- but
even with the shortcut, we've made a lot more steps and done a
lot more work than the easier notion of precedence required. If
we didn't see the opportunity for a shortcut, there would be even
more steps and even more work before we'd figured things out.
So: "Precedence" and "associativity" are alternative ways to
describe that part of C's grammar that deals with expressions,
with operators and operands. Humans have an easier time working
with the memorized P&A rules than with trying to parse expressions
using a BNF grammar, which is why little tables of P&A rules are
popular. But the outcome -- the way the expression decomposes
into its parts -- must be the same under a P&A analysis as would
be obtained by a different parsing algorithm: If the two disagreed,
at least one of them would be wrong and hence not useful as a
short-hand summary of the other.
I'm not sure what example you have in mind concerning the `?:'
operator. Perhaps if you'd post it, someone will exhibit how it's
generated from the formal grammar and how it's analyzed by P&A.