Python's BNF

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Wed Feb 27 17:09:27 EST 2008


En Wed, 27 Feb 2008 15:23:01 -0200, <MartinRinehart at gmail.com> escribi�:

> I spent too long Googling for Python's BNF. Eventually found it at
> Python.org, but only by accident.
>
> There's a link to the program, top-right of page. If anyone wants to
> look at this no-longer-quite-newbie's code and give a constructive
> critique, I'd be grateful.

- Try not to use so many globals. By example:

ofile = open( '\decaf\python-parse-bnf.html', 'w' )
writeHTML()
ofile.close()

I'd pass ofile as an argument to writeHTML - along with *what* to write.

- The "\" is an escape character. Use either
'\\decaf\\python-parse-bnf.html'
or
r'\decaf\python-parse-bnf.html'
or
'/decaf/python-parse-bnf.html'
(the later works more-or-less with all internal Windows functions, but  
usually not from the command line)

- Instead of:

     while i < len( bnf ):
         s = bnf[i]

use

     for s in bnf:

That is, iterate over the list elements (what you want), not over the  
indexes. In case you want also the index, use: for i,item in  
enumerate(items):

- The fileRead function could be written simply as: return  
open(...).readlines(); but you don't have to keep all the contents in  
memory at once, so forget about fileRead and make prepareDict iterate over  
the file, line by line; that's easy:

     for s in open(...):
         if startDef:...

- This whole block:

     plist = []
     for pname in productions:
         plist.append( pname )
     plist.sort()

is the same as:

     plist = sorted(productions)

-- 
Gabriel Genellina




More information about the Python-list mailing list