Parser or regex ?

Fredrik Lundh fredrik at pythonware.com
Fri Dec 16 19:19:29 CET 2005


Fuzzyman wrote:

> I'm writing a module that takes user input as strings and (effectively)
> translates them to function calls with arguments and keyword
> arguments.to pass a list I use a sort of 'list constructor' - so the
> syntax looks a bit like :
>
>    checkname(arg1, "arg 2", 'arg 3', keywarg="value",
> keywarg2='value2', default=list("val1", 'val2'))
>
> Worst case anyway :-)
>
> I can handle this with regular expressions but they are becoming truly
> horrible. I wonder if anyone has any suggestions on optimising them. I
> could hand write a parser - which would be more code, probably slower -
> but less error prone. (Regualr expressions are subject to obscure
> errors - especially the ones I create).
>
> The trouble is that I have to pull out the separate arguments, then
> pull apart the keyword arguments and the list keyword arguments. This
> makes it a 'multi-pass' task - and I wondered if there was a better way
> to do it.

I'd use some variation of:

    http://online.effbot.org/2005_11_01_archive.htm#simple-parser-1

(that version can parse tuples, but it should be too hard to extend
it to handle keyword arguments)

</F>






More information about the Python-list mailing list