Other 'significant' blanks

Donn Seeley donn at utah-gr.UUCP
Fri Dec 14 01:14:40 AEST 1984


From: Henry Spencer (henry at utzoo.UUCP):

	K&R actually says explicitly that (e.g.) "+=" is two tokens;
	hence space between them is allowable (section 7.14).  However,
	practically no C compilers other than the original Ritchie
	compiler have done it this way.  One reason for this is that it
	makes the language non-LALR(1), so all the yacc-based parsers
	croak. ...

It may be the case that YACC parsers can't correctly handle '+=' as two
tokens, but it's incorrect to say that they don't try to.  The PCC
(which has a YACC parser and is the basis of many specific C compilers)
considers '+=' to be two tokens and avoids problems most of the time by
using the rule 'shift when there is a shift-reduce conflict.' Last time
I checked on our 4.2 BSD PCC, there were 7 shift-reduce conflicts...
Precedence is also used to resolve conflicts.  (I should note that
'=+', the old style assignment operator, IS considered a single token
by the PCC.) Apart from the fact that the PCC allows space between the
arithmetic operator and the equals sign, you can also see the
'two-token' effect by comparing the following two statements:

	a && b += c;
	a && b  = c;

The first statement is parsed as 'a && (b += c)' because the PCC sees a
'+' as the token following the 'b', and a '+' is higher in precedence
than '&&', so it shifts; the 'two-token' effect prevents the compiler
from noticing that the precedence of an assignment operator is lower
than '&&'.  The second statement parses as '(a && b) = c' and earns an
error because 'a && b' is not an lvalue.

Donn Seeley    University of Utah CS Dept    donn at utah-cs.arpa
40 46' 6"N 111 50' 34"W    (801) 581-5668    decvax!utah-cs!donn



More information about the Comp.lang.c mailing list