Re: How to do multi-dimensional arrays?

Guido.van.Rossum@cwi.nl
Fri, 20 Jan 1995 14:42:28 +0100

> Note that for tuple-indexed dictionaries and tuple-indexed classes,
> the syntax MUST be:
>
> array[(i,j)]
>
> and not:
>
> array[i,j]
>
> which is a syntax error.

That's a good one. Quoting from the current syntax:

trailer: '(' [testlist] ')' | '[' subscript ']' | '.' NAME
subscript: test | [test] ':' [test]

I suppose this dates back to the times when the only subscriptable
objects were sequences (which always have integer indices) and
old-style dictionaries (which always had string keys).

This could be changed to

subscript: testlist | [testlist] ':' [testlist]

without problems (and without changing compile.c, even!).

Unfortunately my parser generator isn't powerful enough to let it be

subscript: testlist | [test] ':' [test]

(it would have to decide whether to go and parse a testlist or a test
based on the first symbol, and since a testlist begins with a test it
can't -- and unlile Yacc it can't decide later).

An alternative (which would require changing compile.c) would be to
leave subscript alone and change the definition of trailer to

trailer: '(' [testlist] ')' | '[' subscriptlist ']' | '.' NAME
subscriptlist: subscript (',' subscript)**

The difference would be the interpretation of expressions like

a[i:j,k]

-- while always illegal, grouping it like

a[(i:j), k]

makes more sense in the light of a possible future extension to
multi-dimensional slices than grouping it like

a[i : (j,k)]

As always, I'll leave it to a volunteer to experiment with this. Just
edit Grammar/Grammar and run the Parser/pgen script.

--Guido van Rossum, CWI, Amsterdam <mailto:Guido.van.Rossum@cwi.nl>
<http://www.cwi.nl/cwi/people/Guido.van.Rossum.html>