Ambiguity in C

Marv Rubenstein marv at ism780c.UUCP
Tue Oct 7 07:24:25 AEST 1986

There has been considerable discussion in this form concerning the meaning

     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

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

More information about the Comp.lang.c mailing list