ANN: Introducing PLY-1.0 (Python Lex-Yacc)

David Beazley beazley at cs.uchicago.edu
Wed Jun 20 08:57:32 EDT 2001


M.-A. Lemburg writes:
 > 
 > Just a suggestion: PLY seems to use the same logic for attaching
 > grammar snippets to functions as SPARK does. IMHO, this is bad
 > design since doc-strings should really only be used for documentation
 > and not include vital information for the program logic.

Actually, I thought the doc string hack was one of the neatest
programming tricks I've ever seen (which is exactly why I copied it
from SPARK).  Why would I write a different documentation string for a
grammar rule anyways?  The grammar rule in the docstring not only
tells the parser generator what to do, but it precisely documents what
the function does at the same time.  I don't know what inspired John
to take this approach in SPARK, but it's pure genius if you ask me :-).

 > Note that in Python 2.1 we have function attributes which were
 > added for exactly this reason, so the doc-string approach is
 > not really needed anymore. I'd suggest to move to these for one 
 > of the next releases.

A fine idea, but the implementation is completely useless because
there is no syntactically convenient way to attach the
attributes. IMHO, having to type something like this is an even more
horrible design than using the docstrings (not to mention that it
looks ugly and unnatural):

   def p_expr_plus(t):
        t[0] = t[1] + t[3]

   p_expr_plus.grammar = 'expr : expr PLUS expr'

I don't have any plans to abandon the use of doc strings because I
like the way that they work. If I were to use function attributes for
anything, I would probably use them for some purpose other than
grammar specification.  I'd have to think about that however.

Cheers,

Dave






More information about the Python-list mailing list