Slight inconsistency

8 views
Skip to first unread message

Tom Neff

unread,
Apr 23, 1990, 10:18:49 PM4/23/90
to
This compiles:

print ()[0];

This doesn't:

print '-',()[0];

Any idea why?

Brandon S. Allbery KB8JRR

unread,
Apr 25, 1990, 7:19:11 PM4/25/90
to
As quoted from <15...@bfmny0.UU.NET> by tn...@bfmny0.UU.NET (Tom Neff):
+---------------

| This compiles: print ()[0];
| This doesn't: print '-',()[0];
+---------------

It's the usual... the former is parsed as (print())[0], the latter as
(print(('-'),(()[0]))). The latter contains (()[0]), which is obviously
not a sensible expression.

Remember that operations like "print" are parsed as functions with optional
parentheses about the argument list. So "print ()" is parsed as "print" with
an empty argument list, i.e. no arguments. "print ()" == "(print)".

++Brandon
--
Brandon S. Allbery KB8JRR, all...@NCoast.ORG (Internet), BALLBERY (MCI Mail)
ALLBERY (Delphi), uunet!cwjcc.cwru.edu!ncoast!allbery (UUCP), BrandonA (A-OL)

Larry Wall

unread,
Apr 25, 1990, 12:20:30 PM4/25/90
to
In article <15...@bfmny0.UU.NET> tn...@bfmny0.UU.NET (Tom Neff) writes:
: This compiles:

How 'bout, 'cuz the syntax doesn't all subscripting a null list? The
first one happens to compile because a print () is transmogrified by
the tokener into a (print), so it looks to the parser like

(print)[0];

There are probably better ways to do that, but it would make the parser
more complex. I do, occasionally, struggle feebly against complexity... :-)

Larry

Reply all
Reply to author
Forward
0 new messages