[net.lang.c] Precedence Question

pedz@smu.UUCP (02/10/84)

#N:smu:13800001:000:747
smu!pedz    Feb  9 00:17:00 1984

I have a small question for you C people.  We were writting a
compiler for C and came accross this interesting problem.  Is the
statement:
	cat = foo ? fu = bar : mouse;

legal?  Specifically the assignment operator (which has lower
precedence than the ?: thing) between the ? and the :.  If it
is legal, at what point does bar get assigned to fu, before or
after the assignment to cat?  If after (since it has lower
precedence) then I would assume cat is assign the value of fu, after
which fu is assigned the value of bar.  Right or wrong.  Note, the
VAX C compiler accepts it and its actions can easily be determined.
That is not the question.  The question is "according to K & R ...".

Respond vai mail and I will post.
Perry
parsec!smu!pedz

mather@uicsl.UUCP (02/12/84)

#R:smu:13800001:uicsl:6400012:000:876
uicsl!mather    Feb 10 14:13:00 1984

>Is	cat = foo ? fu = bar : mouse;	legal?
>
>	legal 'according to K & R ..."?

Well obviously it is legal. The question is:
is bar assigned to fu before the result of ?: is assigned to cat?

Perhaps this question has deeper undertones than I can perceive, but
my understanding of precedence and order of evaluation was ?: (as a
unit) had a higher order of precedence than = so that 'cat' gets the
value of 'bar' or 'mouse' depending on the value of 'foo', rather than
assigning 'cat' to 'foo'. But that's not what's happening. 'foo' is
evaluated, if true, e2 (fu = bar) is evaluated and assigned to 'cat'.
'mouse' is left alone. If foo is 0 (false), then 'mouse' is evaluated and
assigned to 'cat'. 'fu' is never set to 'bar' in this case.
(K&R top of p48).

foo		is e1		in  e1 ? e2 : e3
fu = bar	is e2
mouse		is e3

					B.C.Mather
					Le Maitre
					...uiucdcs!uicsl!mather

johnl@haddock.UUCP (02/14/84)

#R:smu:13800001:haddock:12400004:000:480
haddock!johnl    Feb 13 15:18:00 1984

In regard to this:

	cat = foo ? fu = bar : mouse;

Precedence has nothing to do with it.  Precedence only matters when there
are two possible legal parses, such as "a + b * c" which could be either
"(a + b) * c" or "a + (b * c)" without precedence to disambiguate.

The only possible parse for the first expression is:

	cat = foo ? (fu = bar) : mouse;

so that the "fu = bar" assignment happens before the assignment to cat
if it happens at all.  Golly.

John Levine, ima!johnl

keesan@bbncca.ARPA (Morris Keesan) (02/14/84)

-----------------------------   
A trivial quibble:

John Levine (ima!johnl) says

> In regard to this:
>
> 	  cat = foo ? fu = bar : mouse;

> Precedence has nothing to do with it.

and

> The only possible parse for the first expression is:
>
>	 cat = foo ? (fu = bar) : mouse;
----------------
    I agree that the question originally posed by smu!pedz has nothing to do
with precedence, but rather with order of evaluation.  However, precedence
does ordeN{
-- 
					Morris M. Keesan
					{decvax,linus,wjh12,ima}!bbncca!keesan
					keesan @ BBN-UNIX.ARPA

keesan@bbncca.ARPA (Morris Keesan) (02/14/84)

-----------------------------
(Pardon the first attempt at this if I didn't manage to cancel it in time -- 
I've just moved to a new office, and my terminal connection is very flaky.)
-----------------------------
A trivial quibble:
John Levine (ima!johnl) says
> In regard to this:
> 
> 	  cat = foo ? fu = bar : mouse;
>
> Precedence has nothing to do with it.
and
> The only possible parse for the first expression is:
>
>	 cat = foo ? (fu = bar) : mouse;
-----
    I agree that the original question posed by smu!pedz has nothing to do
with precedence, but is an "order of evaluation" question.  However, precedence
does enter into the parsing of the expression, and it's exactly the precedence
rule that Pedz was misapplying, the precedence of ?: over assignment, that
makes the correct parse

	cat = (foo ? (fu = bar) : mouse);

instead of

	(cat = foo) ? (fu = bar) : mouse;

which makes all the difference in the world as far as what gets assigned to cat.
-- 
					Morris M. Keesan
					{decvax,linus,wjh12,ima}!bbncca!keesan
					keesan @ BBN-UNIX.ARPA

pedz@smu.UUCP (02/22/84)

#R:smu:13800001:smu:13800003:000:2659
smu!pedz    Feb 21 21:58:00 1984

You people must take me for a complete fool.  The responses I have
received are mostly stupid and unqualified.  The reponses 1 and 2
listed above are a good example of people talking about subjects which
they know nothing about.  The response 3 above is more reasonable
however he seems to have forgotten the original question.  The fact
that the assign between the ? and the : is the one in question, not
the first (left hand) assignement which is not even part of the real
question.

I realize that an assignment results in a value.  If I was this
dumb do you think I would have posed the problem in the first place?
The fact that precedence of the assignement operator is lower than the
precedence of the ?: operator results in the original expressing being
incorrect (illegal).  The fellow in a note futher down in this file
agrees with me.  The reason I now say that the expression is illegal
is because of all of the weak arguments I have heard to the contrary.
Arguements such as, "the colon is not an operator",  "precedence is
needed only for ambiquities", "precedence has nothing to do with the
order of evaluation", "precedence has nothing to do with legal
syntax", etc.

It is obvious to me that my respondants are far less knowledgable than
I about the subject.  So I will tell you the root of the problem and
why the other fellow's compiler decides the expression is illegal but
the Vax compiler does not.

It all has to do with the machine used for parsing.  The Vax compiler
(I am assuming) is a product of yacc and is thus an LALR compiler.  I
wrote some input to yacc to simulate this part of the C syntax and it
produced a parser which also accepted the syntax.  The problem is that
there is no way to tell yacc about ternary operators in a way which it
fully understands.  The other fellow's compiler is a recursive descent
compiler (again I am assuming).  (Are you following all of this?)
With a recursive descent compiler it would be impossible (unless you
really tried) to make a parser which would accept the syntax in
question by accident.

Thus it all comes down to the fact that one method of generating a
compiler yields a parser which accepts the syntax unless the designer
specifically avoids the problem and another method which yields a
parser which does not accept the syntax unless the designer
specifically wants it.

I hope you appreciate me taking the time to tell all of you people
this little story about compilers and the world of parsers.  I has
been a real drag for me.

Please flame, respond, curse, etc via mail since I have given up
reading these notes for lack of intellectual stimulation.

Perry
parsec!smu!pedz

jas@druxy.UUCP (02/24/84)

smu!pedz suggests (among other things) that

foo ? cow = bell : dong

is not a legal C expression, and says that the VAX C compiler
erroneously accepts it because it is a bottom-up compiler generated
by yacc, as opposed to a recursive-descent compiler, which would
(correctly, he contends) reject the expression.  What follows
is an excerpt from a response I mailed to him:

I'm not sure what top-down versus
bottom-up parsing has to do with it.  The C grammar as published
in Kernighan and Ritchie is neither LALR(1) nor LL(1); it is, in
fact, highly ambiguous, and uses operator precedence rules to
disambiguate.  It is possible to write both top-down and bottom-up
parsers that either accept or do not accept "foo ? cow = bell : dong".
I contend, though, that only parsers that DO accept it are correct.
Here is why:

The relevant production from the C grammar is:

expression --> expression ? expression : expression

"cow = bell" is an expression, and thus legal between the '?' and the
':'.  The precedence of the operators is irrelevant here, because
this is not an ambiguous construct: i.e., there is no syntactically
correct way to group this construct other than

( foo ? ( cow = bell ) : dong )

Operator precedence only enters into the picture when the above expression
is combined with others in a way that would be ambiguous, were it not
for precedence rules.  For example:

bar = foo ? cow = bell : dong

Here, without precedence rules, there would be two possible parses:

(1)  ( ( bar = foo ) ? ( cow = bell ) : dong )   and
(2)  ( bar = ( foo ? ( cow = bell ) : dong ) )

We need to know that assignment has a lower precedence than ?: to
determine that (2) is the correct parse.

Jim Shankland
..!ihnp4!druxy!jas

jdb@mordor.UUCP (02/27/84)

I tried to compile a program containing the line

	a = b ? c = d : e;

with the V7 PDP-11 compiler.  It complained that it was an illegal
conditional expression.

Do more recent versions of Ritchie's compiler still flag this as an
error?
-- 
  John Bruner (S-1 Project, Lawrence Livermore National Laboratory)
  MILNET: jdb@s1-c	UUCP: ...!decvax!decwrl!mordor!jdb

jas@druxy.UUCP (ShanklandJA) (02/29/84)

Regarding which compilers accept:

a = b ? c = d : e

the USG 3.0 PDP11 compiler does not, the USG 5.0 VAX and WECo 3B20S
compilers do.  This is yet another area in which the C manual is just
a trifle vague.  My feeling is that it is more correct to accept it
than not to.  I am prepared to back this up with reasonable arguments
if anyone's interested, but not with a "proof" (because ultimately it's
a matter of opinion).  Yet another issue for the ANSI committee to deal
with.  (Anyone on that committee listening?)

Jim Shankland
..!ihnp4!druxy!jas

crl@pur-phy.UUCP (Charles LaBrec) (03/08/84)

We are running 2.8 BSD and the Ritchie compiler does indeed flag 

	a = b ? c = d : e;

as an error.  Pcc doesn't, by the way.  Adding parentheses around
the 2nd assignment is ok, as it should be.

Charles LaBrec
UUCP:		pur-ee!Physics:crl, purdue!Physics:crl
INTERNET:	crl @ pur-phy.UUCP

mcewan@uiucdcs.UUCP (mcewan ) (03/16/84)

#R:smu:13800001:uiucdcs:27600031:000:2138
uiucdcs!mcewan    Mar 15 16:33:00 1984

/**** uiucdcs:net.lang.c / smu!pedz /  9:58 pm  Feb 21, 1984 ****/
You people must take me for a complete fool.  The responses I have
received are mostly stupid and unqualified.  The reponses 1 and 2
listed above are a good example of people talking about subjects which
they know nothing about.  The response 3 above is more reasonable
however he seems to have forgotten the original question.  The fact
that the assign between the ? and the : is the one in question, not
the first (left hand) assignement which is not even part of the real
question.

I realize that an assignment results in a value.  If I was this
dumb do you think I would have posed the problem in the first place?
The fact that precedence of the assignement operator is lower than the
precedence of the ?: operator results in the original expressing being
incorrect (illegal).  The fellow in a note futher down in this file
agrees with me.  The reason I now say that the expression is illegal
is because of all of the weak arguments I have heard to the contrary.
Arguements such as, "the colon is not an operator",  "precedence is
needed only for ambiquities", "precedence has nothing to do with the
order of evaluation", "precedence has nothing to do with legal
syntax", etc.

It is obvious to me that my respondants are far less knowledgable than
I about the subject.

I hope you appreciate me taking the time to tell all of you people
this little story about compilers and the world of parsers.  I has
been a real drag for me.

Perry
parsec!smu!pedz
/* ---------- */

Sorry about wasting your time, Perry. Its hard for us to tell just
how stupid a person is from one little note (you're very good at it,
though. You can tell someone is a drooling idiot just by noting that
he disagrees with you.)  I don't really think that you're a complete
fool. I just think that you're a flaming asshole. I hope that you
can find another net somewhere that is filled with people who are
as super-brilliant as you are.

			The opinions expressed are my own and not
			necessarily those of any sane person.

			Scott McEwan
			pur-ee!uiucdcs!mcewan

	"Hitler was an idealist.""

cuda@ihuxv.UUCP (mike nelson) (04/03/84)

Shouldn't the correct equation be   

c = ( a = b ) ? d : e  

I found if you include assignment or just about any other
operators in between the ? and : or after, the compiler throws up.

Happy Hunting

			Mike Nelson
			ihuxv!cuda