[Python-Dev] Grammar help.
Thomas Wouters
thomas@xs4all.net
Fri, 7 Jul 2000 00:04:07 +0200
Argh, my head hurts ! :P
I need some help with the Python Grammar. Or rather, the Parser.
I'm trying to add some new syntax to the current CVS snapshot, something
Guido said he would like to see: built-in 'range'-like list creation, so
that '[1:10]' is the same as 'range(1,10)'. The code isn't the problem,
that's all done and it all works, but the problem is that it somehow broke
normal list literals ! :-P
Here's what I did. The current definition of 'atom' is:
atom: '(' [testlist] ')' | '[' [testlist] ']' | << ... >>
testlist: test (',' test)* [',']
I tried adding it like this:
atom: '(' [testlist] ')' | '[' [listmaker] ']' | << ... >>
testlist: test (',' test)* [',']
listmaker: testlist | rangemaker
rangemaker: [test] ':' test
And as I said, it works -- for the new syntax. But not for the old one.
When I start the interpreter, here's what I see:
XXX ambiguity!
[snip]
XXX ambiguity!
'import site' failed; traceback:
File "./Lib/site.py", line 93
prefixes = [sys.prefix]
^
>>> [2:20]
[2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
>>> [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
File "<stdin>", line 1
[2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
^
SyntaxError: invalid syntax
So, for some reason it doesn't recognize the old list literals als valid
syntax. And it probably has to do with ambiguity ;) But I can't, for the
life of me, figure out what's so ambiguous about it. As far as I can tell,
a bare 'test' can't be a ':', and 'rangemaker' always needs a ':'. Is this
some kind of limitation in the Parser that I need to work around ? I tried
reading the parser code, and I'm glad my head is reattached ;P
Hints-greatly-appreciated-ly y'rs,
--
Thomas Wouters <thomas@xs4all.net>
Hi! I'm a .signature virus! copy me into your .signature file to help me spread!