[comp.std.c] dollar signs in identifiers

karl@haddock.ima.isc.com (Karl Heuer) (06/07/90)

In article <371@necssd.NEC.COM> harrison@necssd.NEC.COM (Mark Harrison) writes:
>	#if VMS
>	extern char * x$something;  /* however it's done */
>	#endif

As noted in the previous article, the quote is shielded by the comment.  That
`$' is a bit trickier, though.  It lexes into a single pp-token, which is
excluded along with its neighbors on a non-VMS system (assuming the symbol VMS
has the obvious meaning); I presume that if there's ever an ANSI compiler on
VMS, it will issue a trivial diagnostic, and, since the presence of the
invalid token throws us into the realm of Undefined Behavior, the compiler
will conveniently choose to define this situation to mean `glue these together
into a single token'.

Or more likely, such a compiler will support something like `#pragma enable
extended_identifiers', since that would allow the expected behavior for
macros, too.  (The Standard requires that the strictly conforming program
	#include <stdio.h>
	#define foo$bar /* define foo to be dollar-bar */
	#ifdef foo
	int main(void) { printf("yes\n"); return 0; }
	#else
	int main(void) { printf("no\n"); return 0; }
	#endif
must print "yes"; normal VMS behavior would be to treat `foo$bar' as a single
token and print "no".)

Personally, I think the treatment of unexpected characters should have been
implementation-defined.  But hey, DEC and Apollo both had representatives on
the Committee, and if they're satisfied, who am I to complain?

Karl W. Z. Heuer (karl@ima.ima.isc.com or harvard!ima!karl), The Walking Lint

walter@hpcllca.HP.COM (Walter Murray) (06/09/90)

Karl Heuer writes:

> (The Standard requires that the strictly conforming program
> 	#include <stdio.h>
> 	#define foo$bar /* define foo to be dollar-bar */
> 	#ifdef foo
> 	int main(void) { printf("yes\n"); return 0; }
> 	#else
> 	int main(void) { printf("no\n"); return 0; }
> 	#endif
> must print "yes"; normal VMS behavior would be to treat `foo$bar' as a single
> token and print "no".)

I don't see how this can be a strictly conforming program.  The source
character set is implementation-defined (page 11, lines 3-4), and there
is nothing in the standard that requires $ to be a member.  (See also
page 11, lines 29-32.)

In fact, I would have trouble convincing myself that either of the 
following is a strictly conforming program:

   /* Program 1 */ int main () { /* $ */ }
   /* Program 2 */ int main () { '$'; }

Any thoughts?

Looking at this from a different perspective, must a conforming
implementation on an 8-bit-byte machine be prepared to accept all
256 possible bytes in a string literal?  Must it map them to 256
distinct values?  I think not.  The mapping of source file characters
to the source character set in translation phase 1 is not defined
by the standard.

Walter Murray
-------------

 

gwyn@smoke.BRL.MIL (Doug Gwyn) (06/12/90)

In article <16490021@hpcllca.HP.COM> walter@hpcllca.HP.COM (Walter Murray) writes:
-I don't see how this can be a strictly conforming program.  The source
-character set is implementation-defined (page 11, lines 3-4), and there
-is nothing in the standard that requires $ to be a member.  (See also
-page 11, lines 29-32.)

Quite right; a strictly conforming program cannot use '$' in identifiers.

-In fact, I would have trouble convincing myself that either of the 
-following is a strictly conforming program:
-   /* Program 1 */ int main () { /* $ */ }
-   /* Program 2 */ int main () { '$'; }

I agree.  Indeed, I stopped using ` characters in comments when I realized
that they could impair portability.

-Looking at this from a different perspective, must a conforming
-implementation on an 8-bit-byte machine be prepared to accept all
-256 possible bytes in a string literal?  Must it map them to 256
-distinct values?  I think not.

I agree again.  Further note that multibyte considerations mean that
even the sequence '*' '/' may not be seen in a comment, if through
use of funny characters the parser has entered some shift state.

karl@haddock.ima.isc.com (Karl Heuer) (06/14/90)

In article <13082@smoke.BRL.MIL> gwyn@smoke.BRL.MIL (Doug Gwyn) writes:
>In article <16490021@hpcllca.HP.COM> walter@hpcllca.HP.COM (Walter Murray) writes:
>>I don't see how this can be a strictly conforming program.  [`$' need not be
>>a member of the source character set.]

I'll reluctantly concede that point.  (But see below.)

>Quite right; a strictly conforming program cannot use '$' in identifiers.

Not relevant; the program in question wasn't using it in an identifier.  That
was, in fact, the point: the Standard requires that any implementation in
which `$' is in the source character set (in practice, any implementation in
the ASCII-or-superset world) *must* accept the program and generate a result
contrary to what is produced by the traditional DEC or Apollo compilers.

>I stopped using ` characters in comments when I realized that they could
>impair portability.

I figure it's a Quality of Implementation issue.  An implementation that
disallows "`" in a comment probably won't survive long in a free market.
(And if some of the characters in my source file cause a change in shift
state when interpreted in the implementation's character set, then the vendor
had better supply a translator that maps my character set into his.)

Karl W. Z. Heuer (karl@ima.ima.isc.com or harvard!ima!karl), The Walking Lint