precedence of && (was: precedence of ?:)

T. William Wells bill at twwells.com
Fri Sep 15 04:10:49 AEST 1989


In article <8034 at goofy.megatest.UUCP> djones at megatest.UUCP (Dave Jones) writes:
: The grammar at the back of the book, presumably close to the ANSII
: standard, does not use precedence for "disambiguation". Too easy, I guess.
: Instead it has lot's of very confusing different kinds of expressions.
: Among them are "assignment-expression" and "logical-and-expression",
: for example. A crock, it you ask me. Presumably, (if they got it right),
: those rules imply the precedence given in the K&R table, except that, as
: we saw, some expressions are rejected by the grammar rather than by
: "semantic" rules, such as the L-value rule. I would like to hear the
: rationale behind creating such a convoluted grammar, when the precedence
: rules are so much easier to understand.

The precedence rules are wrong. The grammar is right. The reason 
for the grammar is that precedence rules can not be sufficient to
unambiguously specify the parse; the grammar is. Postfix operators
and ?: are the culprits. There is no simpler way, however, to 
properly specify the grammar, in pure BNF, than by having one 
itty-bitty rule set for each "level of precedence". Such is life.

:                                          I can just picture a beginning
: programmer trying to push and pop his mental stack through that maze
: as he tries to figure out why his program is in error.

It actually isn't too hard, once you realize that the bulk of the
grammar contains rule chains like:

	Z:
		Y

	Y
		Y op Z

you just find the operator you want, look up towards the top of the
grammar to see if the surrounding operators would also parse (in
which case, they have precedence; this is a consequence of the
grammar being unambiguous), and if all goes well, you know how that
piece fits. Repeat for all the other pieces and you have your parse.

Besides which, the standard is explicitly *not* intended for use by a
beginning programmer.

: (If I ever write a C compiler, I'm going to use precedence, and if
: anybody ever complains that they got an "L-value" error message when they
: distinctly asked for a "syntax" error message, maybe I'll offer to wash
: their car.)

I wrote a precedence parser for C expressions. It was a mass of
kludges on top of fudges. I threw it away. C is just not specifiable
by precedence.

---
Bill                    { uunet | novavax | ankh | sunvice } !twwells!bill
bill at twwells.com



More information about the Comp.lang.c mailing list