318
|
1
|
|
2 class Tree:
|
319
|
3 """ Tree node with a name and possibly some child nodes """
|
318
|
4 def __init__(self, name, *args):
|
|
5 self.name = name
|
322
|
6 self.value = None
|
318
|
7 self.children = args
|
|
8
|
|
9 def __repr__(self):
|
319
|
10 if self.children:
|
|
11 ch = ', '.join(str(c) for c in self.children)
|
|
12 return '{}({})'.format(self.name, ch)
|
|
13 else:
|
|
14 return '{}'.format(self.name)
|
|
15
|
|
16
|
|
17 class State:
|
320
|
18 """ State used to label tree nodes """
|
319
|
19 def __init__(self):
|
|
20 self.labels = {}
|
320
|
21
|
319
|
22 def has_goal(self, goal):
|
|
23 return goal in self.labels
|
320
|
24
|
319
|
25 def get_cost(self, goal):
|
|
26 return self.labels[goal][0]
|
320
|
27
|
319
|
28 def get_rule(self, goal):
|
|
29 return self.labels[goal][1]
|
320
|
30
|
319
|
31 def set_cost(self, goal, cost, rule):
|
|
32 if self.has_goal(goal):
|
|
33 if self.get_cost(goal) > cost:
|
|
34 self.labels[goal] = (cost, rule)
|
|
35 else:
|
|
36 self.labels[goal] = (cost, rule)
|
|
37
|
320
|
38
|
319
|
39 class BaseMatcher:
|
320
|
40 """ Base class for matcher objects. """
|
319
|
41 def kids(self, tree, rule):
|
|
42 return self.kid_functions[rule](tree)
|
|
43
|
|
44 def nts(self, rule):
|
|
45 return self.nts_map[rule]
|
|
46
|
|
47 def burm_label(self, tree):
|
320
|
48 """ Label all nodes in the tree bottom up """
|
319
|
49 for c in tree.children:
|
|
50 self.burm_label(c)
|
|
51 self.burm_state(tree)
|
|
52
|
|
53 def apply_rules(self, tree, goal):
|
|
54 rule = tree.state.get_rule(goal)
|
320
|
55 results = [self.apply_rules(kid_tree, kid_goal)
|
323
|
56 for kid_tree, kid_goal in zip(self.kids(tree, rule), self.nts(rule))]
|
|
57 return self.pat_f[rule](tree, *results)
|