Interpolation (was: Keyword calling gotcha ?)

Tim Peters tim_one at email.msn.com
Sun May 30 00:49:13 EDT 1999


[Jim Meier, having fun with syntax]
> ...
> I've been wanting to play with Python's syntax for a while now, so I
> thought I'd give it a try.
> ...
> I took a (very) quick stab at sticking this into the grammar file
> (Grammer/Grammar), but have no idea where to go from there. The
> Tokenizer/Parser sources seem large and daunting - could anyone give me
> a roadmap before I drown myself? I have played with Bison./YACC and made
> some working, useable toy languages, but this seems to be very
> different.

Better preparation would have been had you played with *implementing* Bison
and  YACC:  in effect, Python is doing the stuff they do (well, really only
as much as it needs to), plus implementing the full Python grammar too.
It's not large or daunting given all that it's doing -- provided you've done
it all before <wink>.

I posted a link earlier to the Best Parsing Book on Earth; read the first
few chapters of that first.  It's the only roadmap you're going to find.
BTW, Python's parser code is old and somewhat brittle, and may be the
hardest part of the language implementation to fiddle with.  You can do it,
though!  Try something, and change it when it doesn't work <0.5 wink>.

> oh, this is how I changed  Grammar/Grammar:
>
> (at the very end:)
> arglist:  argument (',' argument)* [',']
> argument: [test '='] test       # Really [keyword '='] test
>
> becomes:
> arglist:  argument (',' argument)* ['*' NAME [('*'|'*' '*') NAME]]
> [('**'|'*' '*') NAME]
>
> is there a more sensible way to do this?

Offhand, I don't know -- analysis is difficult by hand, and it's faster to
just try it anyway.  Note that you're at least missing some commas, so
immediately more plausible would be:

arglist:  argument (',' argument)* [',' '*' NAME] [',' '*' '*' NAME] [',']

But that still insists on having at least one "real argument" first, so
isn't what you want either.  Write out concrete instances of everything you
want to accept, and make sure your grammar allows all and only those.

grammars-aren't-easy-ly y'rs  - tim






More information about the Python-list mailing list