Python Parsers Comparison

Warren Postma embed at geocities.com
Thu Apr 20 17:24:20 CEST 2000


> For example, a comma-separated list of nested vectors would be a good
example:
>
>     [ 1, 2 ], [ 3, 4 ]
>     [ [ 1,2 ],[ 3,4 ] ], [ 5.6 ]
>     etc.
>

Wherever I am able to do so, I use eval() and design my grammar to be
basically PYTHON.  Very nice scheme, and you can even use 'restricted
execution' if safety is a concern.

In the above cases:

safedict={"__builtins__":{},"NULL":None} # your own symbols, and constants
str = '[1,2,],[1,2],[[1,2],[1,2]],[NULL,NULL]'
x = eval("["+str+"]",safedict,safedict)
print x

If you are in control of the grammar, and you are inventing it, why not
stick with python, if on the other hand, the grammar is something you're
stuck with 'as-is', and it's not a proper subset of some Python construct,
then all bets are off.

I certainly WOULD like to see some tutorial material on constructing such
parsers, because I don't know the first thing about them. We studied this
stuff in undergrad C.S., but it was trivial stuff (BNF grammars, some simple
parsing code techniques in C).  We never did learn the Nifty Hardcode Stuff.

Warren





More information about the Python-list mailing list