K
Keith Thompson
BartC said:OK, so the C standard uses a far more pedantic and unwieldy way of
expressing syntax than I would, if it has to enforce each of the 17
precedence levels in the grammar!
(I would write the formal syntax as just:
expr ? expr : expr
worrying about any restrictions on each expr later, while the production of
'expr' doesn't itself worry about precedence (which would just be an
attribute of a binary operator).
I've implemented such a conditional operator for real, using this informal
approach, and it works fine. Although I do insist in enclosing the thing in
parentheses.)
If you "insist on enclosing the thing in parentheses", then you're
describing a different grammar than the one the C standard describes.
Enforcing the 17 precedence levels by defining them in the grammar makes
some things easier; you don't need another level of logic to resolve
operator precedence.
And I would think that defining every kind of expression as "expr" would
create a lot of cases where an expression can't be parsed unambiguously.
Perhaps your approach is valid, but the one used by the standard is
certainly valid as well. (Personally I like the standard's approach
better, but that's a matter of opinion.)
[...]
You have to look further into the Standard to discover that
'assignment-expression' just means 'any expression'.
So look further into the Standard.
Whatever the merits of
defining the grammar in this way, it's not very intuitive; the above
suggests that any conditional expression is a form of assignment!
As I said in my other response, the names are a little unintuitive
*unless you understand what they really mean*. Other than radically
changing the way the grammar is defined (would you advocate doing
that?), I can't think of a better way of naming the various productions.