[Python编程(第4版)].(Programming.Python.4th.Edition).Mark.Lutz.文字版

(yzsuai) #1
Of course, not everyone can donate all their extension modules to the public domain,
but there’s a growing library of available components that you can pick up for free and
a community of experts to query. Visit the PyPI site at http://www.python.org for links
to Python software resources, or search the Web at large. With at least one million
Python users out there as I write this book, much can be found in the prior-art
department.
Unless, of course, the wheel does not work: We’ve also seen a handful of cases in this
book where standard libraries were either not adequate or were broken altogether. For
instance, the Python 3.1 email package issues we explored in Chapter 13 required us
to code workarounds of our own. In such cases, you may still ultimately need to code
your own infrastructure support. The “not invented here” syndrome can still claim
victory when software dependencies break down.
Still, you’re generally better off trying to use the standard support provided by Python
in most cases, even if doing so requires manually coded fixes. In the email package
example, fixing its problems seems much easier than coding an email parser and gen-
erator from scratch—a task far too large to have even attempted in this book. Python’s
batteries included approach to development can be amazingly productive—even when
some of those batteries require a charge.

Custom Language Parsers


Although toolkits abound in this domain, Python’s status as a general-purpose pro-
gramming language also makes it a reasonable vehicle for writing hand-coded parsers
for custom language analysis tasks. For instance, recursive descent parsing is a fairly
well-known technique for analyzing language-based information. Though not as pow-
erful as some language tools, recursive descent parses are sufficient for a wide variety
of language-related goals.


To illustrate, this section develops a custom parser for a simple grammar—it parses
and evaluates arithmetic expression strings. Though language analysis is the main topic
here, this example also demonstrates the utility of Python as a general-purpose pro-
gramming language. Although Python is often used as a frontend or rapid development
language in tactical modes, it’s also often useful for the kinds of strategic work you may
have formerly done in a systems development language such as C or C++.


The Expression Grammar


The grammar that our parser will recognize can be described as follows:


goal -> <expr> END [number, variable, ( ]
goal -> <assign> END [set]

assign -> 'set' <variable> <expr> [set]

expr -> <factor> <expr-tail> [number, variable, ( ]

1440 | Chapter 19: Text and Language

Free download pdf