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

(yzsuai) #1
99
Enter=> a * a * a
970299
Enter=> stop
>>>

Adding a Parse Tree Interpreter


One weakness in the parser1 program is that it embeds expression evaluation logic in
the parsing logic: the result is computed while the string is being parsed. This makes
evaluation quick, but it can also make it difficult to modify the code, especially in larger
systems. To simplify, we could restructure the program to keep expression parsing and
evaluation separate. Instead of evaluating the string, the parser can build up an inter-
mediate representation of it that can be evaluated later. As an added incentive, building
the representation separately makes it available to other analysis tools (e.g., optimizers,
viewers, and so on)—they can be run as separate passes over the tree.


Example 19-16 shows a variant of parser1 that implements this idea. The parser ana-
lyzes the string and builds up a parse tree—that is, a tree of class instances that repre-
sents the expression and that may be evaluated in a separate step. The parse tree is built
from classes that “know” how to evaluate themselves: to compute the expression, we
just ask the tree to evaluate itself. Root nodes in the tree ask their children to evaluate
themselves, and then combine the results by applying a single operator. In effect, eval-
uation in this version is simply a recursive traversal of a tree of embedded class instances
constructed by the parser.


Example 19-16. PP4E\Lang\Parser\parser2.py


"""
Separate expression parsing from evaluation by building an explicit parse tree
"""


TraceDefault = False
class UndefinedError(Exception): pass


if name == 'main':
from scanner import Scanner, SyntaxError, LexicalError # if run here
else:
from .scanner import Scanner, SyntaxError, LexicalError # from PyTree


#################################################################################


the interpreter (a smart objects tree)


#################################################################################


class TreeNode:
def validate(self, dict): # default error check
pass
def apply(self, dict): # default evaluator
pass
def trace(self, level): # default unparser
print('.' * level + '')


Custom Language Parsers| 1449
Free download pdf