Better way to do parsing?

Bengt Richter bokr at oz.net
Wed May 4 10:53:01 EDT 2005


On Wed, 04 May 2005 07:13:22 -0300, =?ISO-8859-1?Q?Andr=E9_Roberge?= <andre.roberge at gmail.com> wrote:

>Hi all,
>
>I posted the following on the python tutor list 3 days ago ... and 
>haven't heard a peep from anyone - which is highly unusual on that list.
>
>[Apologies for the slightly longer post due to code
>with tests cases included at the end .]
>====================
>I have created a "severely restricted" environment within with
>users can learn the basics of programming in Python.
>
>[note: eval, exec, chr, input, raw_input are not allowed.]
>
>Within that environment, I want to have the user test the five
>valid forms that an import statement can have, by attempting to
>import a fake module whose name is "useful".  Other "import"
>statements are disallowed.
>
>1. import useful
>2. from useful import *
>3. from useful import valid_function1 [, valid_function2, ...]
>4. from useful import valid_function as better_named_function
>5. import useful as not_so_useful_after_all
>
>As far as I can tell, the following works, but it looks rather
>"clunky" to me.  My *very limited* experience with the
>"re" module may have something to do with this.
>
>Any suggestion would be most welcome.
>
I don't in any way want to discourage your enthusiastic pursuit of
your goal, but I suspect you might have more fun with another approach,
unless you really want to learn about the limitations of regexes first ;-)
(Don't get me wrong, regexes are great for what they do best).

I.e., if you want to parse python, there are modules that will help you
a lot more than just re will. If you want to validate source
according to your own rules before compiling, you could walk the
ast and raise exceptions where your rules are violated. Or if you
want to emulate excution as you walk the tree, you can do that too.
Either way, this post of Michael Spencer's ought to give you food for thought:

    http://mail.python.org/pipermail/python-list/2005-March/270760.html

Also for more general (not just python syntax) parssing the pyparsing
program referenced in the followon post looks very nice, though I have not tried it.

    http://pyparsing.sourceforge.net/


Regards,
Bengt Richter



More information about the Python-list mailing list