[Python编程(第4版)].(Programming.Python.4th.Edition).Mark.Lutz.文字版
grammar definitions. Better yet, we could use an integration that already exists— interfaces to such common parser generators ar ...
Of course, not everyone can donate all their extension modules to the public domain, but there’s a growing library of available ...
expr-tail -> ^ [END, ) ] expr-tail -> '+' <factor> <expr-tail> [+] expr-tail -> '-' <factor> <expr ...
naive about the string itself. This modular structure keeps the code relatively simple. And it’s another example of the object-o ...
elif self.text[ix] in string.digits: str = '' while self.text[ix] in string.digits: str += self.text[ix] ix += 1 if self.text[ix ...
self.lex.newtext(text[0]) # reuse this parser? try: self.lex.scan() # get first token self.Goal() # parse a sentence except Synt ...
self.lex.scan() left = left / self.Term() else: raise SyntaxError() def Term(self): if self.lex.token == 'num': val = self.lex.m ...
could simply run a // in our evaluation logic to retain prior behavior (or allow for both / and // in our grammar), but we’ll fo ...
5.0 5.0 3.5 0.8 0.8 0.8 6.0 6.0 0.666666666667 4 9.0 4.0 100 3.14159 6.28318 3.334 As usual, we can also test and use the system ...
Lesson 3: Divide and Conquer As the custom parser system demonstrates, modular program design is almost always a major win. By u ...
99 Enter=> a * a * a 970299 Enter=> stop >>> Adding a Parse Tree Interpreter One weakness in the parser1 program ...
ROOTS class BinaryNode(TreeNode): def init(self, left, right): # inherited methods self.left, self.right = left, right # left/ri ...
def trace(self, level): print('.' * level + self.name) COMPOSITES class AssignNode(TreeNode): def init(self, var, val): self.var ...
self.lex.start = varinfo[1] self.lex.showerror() # returns None def interpret(self, tree): result = tree.apply(self.vars) # tree ...
def Term(self): if self.lex.token == 'num': leaf = NumNode(self.lex.match('num')) return leaf elif self.lex.token == 'var': leaf ...
own directory on the import search path, though, so we have to use package-relative import syntax for the latter case, and impor ...
>>> p = parser2.Parser() >>> p.traceme = True >>> p.parse('5 + 4 * 2') [+] ...5 ...[*] ......4 ...... ...
Exploring Parse Trees with the PyTree GUI But wait—there is a better way to explore parse tree structures. Figure 19-1 shows the ...
PyTree makes it easy to learn about and experiment with the parser. To determine the tree shape produced for a given expression, ...
written in Python, it is both easily customized and widely portable across window platforms. And because it is implemented with ...
«
70
71
72
73
74
75
76
77
78
79
»
Free download pdf