[net.lang.c] Ambiguity in C

marv@ism780c.UUCP (Marv Rubenstein) (10/07/86)

There has been considerable discussion in this form concerning the meaning
of:

     sizeof (int) - 1

Some people have suggested that the expression is ambiguous.  Now what most
authors on compilers mean when they speak about ambiguity applies to the
*grammar* for a language.  A grammar is said to be ambiguous if there exists
two parse trees for a sentence in the language.  To quote David Gries, "Note
that we call the grammar ambiguous, and not the language itself.  By changing
an ambiguous grammar -- without of course changing the sentences of it -- we
may sometimes arrive at an unambiguous grammar for the same set set of
sentences."

So the question one should ask is: Is the *grammar* for c as defined in K&R
ambiguous?  My answer, of course the grammar is ambiguous.

For example, based on the operator table of page 49 one could conclude that
the parse tree for the subject expression is:

	sizeof   -- unary operator
	  |
	(int)    -- unary operator
	  |
	  -      -- unary operator
	  |
	  1      -- constant


On the other hand from reading page 177 and 178 one could conclude that the
parse tree for the subject expression is:

				 -      -- infix subtraction
				/ \
			       /   \
			      /     \
	   unit --   sizeof(int)     1  -- constant

Here, I take the word `unit' (not defined in the text) to mean `constant'.

One of the major reasons for discussions that are going on in this form is
that the *grammar* for c given in  K&R is written in English with the result
that it is at the least ambiguous and it is at the worst incomplete and self
contradictory.  So, how do we make the grammar for c unambiguous?  One way
is to write the grammar in a formal language and subject it to analysis by a
grammar analysis program.  I wonder if the standards committee has done this
for their proposed new language (still called c).

And now a question.  Is it reasonable to design a language (advertised as
portable) that has ambiguous semantics? (e.g., dealers choice on evaluation
order).  I know it is too late for c.  I ask the question for the next
`portable' language.

	Marv Rubinstein.  Compilerist -- Interactive Systems

henry@utzoo.UUCP (Henry Spencer) (10/09/86)

> ... So, how do we make the grammar for c unambiguous?  One way
> is to write the grammar in a formal language and subject it to analysis by a
> grammar analysis program.  I wonder if the standards committee has done this
> for their proposed new language (still called c).

The grammar in the X3J11 drafts is a bit more formal than the one in K&R,
and has been very carefully munged so that the "sizeof(int)-1" ambiguity
is gone.  (Personally I never considered this a serious problem, but it's
pleasant that they solved it.)
-- 
				Henry Spencer @ U of Toronto Zoology
				{allegra,ihnp4,decvax,pyramid}!utzoo!henry