annotate python/pyburg.py @ 320:84d67cce67b7

Working burg
author Windel Bouwman
date Sun, 19 Jan 2014 16:09:44 +0100
parents 8d07a4254f04
children 8c569fbe60e4
rev   line source
318
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
1 #!/usr/bin/python
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
2
319
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
3 """
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
4 Bottom up rewrite generator.
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
5
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
6 This script takes as input a description of patterns and outputs a
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
7 matcher class that can match trees given the patterns.
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
8
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
9 Patterns are specified as follows:
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
10 reg -> ADDI32(reg, reg) 2 (. add $1 $2 .)
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
11 reg -> MULI32(reg, reg) 3 (. .)
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
12 or a multiply add:
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
13 reg -> ADDI32(MULI32(reg, reg), reg) 4 (. muladd $1, $2, $3 .)
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
14 The general specification pattern is:
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
15 [result] -> [tree] [cost] [template code]
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
16
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
17 A tree is described using parenthesis notation. For example a node X with
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
18 three
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
19 child nodes is described as:
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
20 X(a, b, b)
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
21 Trees can be nested:
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
22 X(Y(a, a), a)
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
23 The 'a' in the example above indicates an open connection to a next tree
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
24 pattern.
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
25
320
84d67cce67b7 Working burg
Windel Bouwman
parents: 319
diff changeset
26 In the example above 'reg' is a non-terminal. ADDI32 is a terminal. non-terminals
84d67cce67b7 Working burg
Windel Bouwman
parents: 319
diff changeset
27 cannot have child nodes. A special case occurs in this case:
84d67cce67b7 Working burg
Windel Bouwman
parents: 319
diff changeset
28 reg -> rc
84d67cce67b7 Working burg
Windel Bouwman
parents: 319
diff changeset
29 where 'rc' is a non-terminal. This is an example of a chain rule. Chain rules
84d67cce67b7 Working burg
Windel Bouwman
parents: 319
diff changeset
30 can be used to allow several variants of non-terminals.
84d67cce67b7 Working burg
Windel Bouwman
parents: 319
diff changeset
31
319
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
32 The generated matcher uses dynamic programming to find the best match of the
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
33 tree. This strategy consists of two steps:
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
34 - label: During this phase the given tree is traversed in a bottom up way.
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
35 each node is labelled with a possible matching rule and the corresponding cost.
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
36 - select: In this step, the tree is traversed again, selecting at each point
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
37 the cheapest way to get to the goal.
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
38
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
39 """
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
40
318
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
41 import sys
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
42 import argparse
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
43 from ppci import Token
319
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
44 import burg_parser # Automatically generated
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
45 from pyyacc import ParserException, EOF
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
46 import baselex
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
47 from tree import Tree
318
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
48
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
49
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
50 class BurgLexer:
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
51 def feed(self, txt):
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
52 tok_spec = [
319
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
53 ('id', r'[A-Za-z][A-Za-z\d_]*', lambda typ, val: (typ, val)),
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
54 ('kw', r'%[A-Za-z][A-Za-z\d_]*', lambda typ, val: (val, val)),
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
55 ('number', r'\d+', lambda typ, val: (typ, int(val))),
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
56 ('STRING', r"'[^']*'", lambda typ, val: ('id', val[1:-1])),
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
57 ('template', r"\(\..*\.\)", lambda typ, val: (typ, val)),
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
58 ('OTHER', r'[:;\|\(\),]', lambda typ, val: (val, val)),
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
59 ('SKIP', r'[ ]', None)
318
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
60 ]
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
61
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
62 lines = txt.split('\n')
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
63
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
64 def tokenize():
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
65 for line in lines:
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
66 line = line.strip()
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
67 if not line:
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
68 continue # Skip empty lines
319
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
69 elif line == '%%':
318
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
70 yield Token('%%', '%%')
319
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
71 else:
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
72 yield from baselex.tokenize(tok_spec, line)
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
73 yield Token(EOF, EOF)
318
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
74 self.tokens = tokenize()
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
75 self.token = self.tokens.__next__()
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
76
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
77 def next_token(self):
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
78 t = self.token
319
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
79 if t.typ != EOF:
318
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
80 self.token = self.tokens.__next__()
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
81 return t
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
82
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
83
319
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
84 class Rule:
320
84d67cce67b7 Working burg
Windel Bouwman
parents: 319
diff changeset
85 """ A rewrite rule. Specifies a tree that can be rewritten into a result
84d67cce67b7 Working burg
Windel Bouwman
parents: 319
diff changeset
86 at a specific cost """
319
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
87 def __init__(self, non_term, tree, cost, template):
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
88 self.non_term = non_term
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
89 self.tree = tree
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
90 self.cost = cost
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
91 self.template = template
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
92 self.nr = 0
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
93
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
94 def __repr__(self):
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
95 return '{} -> {} ${}'.format(self.non_term, self.tree, self.cost)
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
96
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
97
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
98 class Symbol:
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
99 def __init__(self, name):
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
100 self.name = name
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
101
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
102
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
103 class Term(Symbol):
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
104 pass
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
105
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
106
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
107 class Nonterm(Symbol):
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
108 def __init__(self, name):
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
109 super().__init__(name)
320
84d67cce67b7 Working burg
Windel Bouwman
parents: 319
diff changeset
110 self.chain_rules = []
319
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
111
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
112
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
113 class BurgSystem:
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
114 def __init__(self):
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
115 self.rules = []
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
116 self.symbols = {}
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
117 self.goal = None
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
118
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
119 def symType(self, t):
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
120 return (s.name for s in self.symbols.values() if type(s) is t)
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
121
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
122 terminals = property(lambda s: s.symType(Term))
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
123
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
124 non_terminals = property(lambda s: s.symType(Nonterm))
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
125
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
126 def add_rule(self, non_term, tree, cost, template):
320
84d67cce67b7 Working burg
Windel Bouwman
parents: 319
diff changeset
127 template = template[2:-2].strip()
84d67cce67b7 Working burg
Windel Bouwman
parents: 319
diff changeset
128 if not template:
84d67cce67b7 Working burg
Windel Bouwman
parents: 319
diff changeset
129 template = 'pass'
319
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
130 rule = Rule(non_term, tree, cost, template)
320
84d67cce67b7 Working burg
Windel Bouwman
parents: 319
diff changeset
131 if len(tree.children) == 0 and tree.name not in self.terminals:
84d67cce67b7 Working burg
Windel Bouwman
parents: 319
diff changeset
132 print('chain:', rule)
84d67cce67b7 Working burg
Windel Bouwman
parents: 319
diff changeset
133 self.non_term(tree.name).chain_rules.append(rule)
319
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
134 self.non_term(rule.non_term)
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
135 self.rules.append(rule)
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
136 rule.nr = len(self.rules)
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
137
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
138 def non_term(self, name):
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
139 if name in self.terminals:
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
140 raise BurgError('Cannot redefine terminal')
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
141 if not self.goal:
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
142 self.goal = name
320
84d67cce67b7 Working burg
Windel Bouwman
parents: 319
diff changeset
143 return self.install(name, Nonterm)
319
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
144
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
145 def tree(self, name, *args):
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
146 return Tree(name, *args)
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
147
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
148 def install(self, name, t):
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
149 assert type(name) is str
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
150 if name in self.symbols:
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
151 assert type(self.symbols[name]) is t
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
152 else:
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
153 self.symbols[name] = t(name)
320
84d67cce67b7 Working burg
Windel Bouwman
parents: 319
diff changeset
154 return self.symbols[name]
319
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
155
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
156 def add_terminal(self, terminal):
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
157 self.install(terminal, Term)
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
158
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
159
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
160 class BurgError(Exception):
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
161 pass
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
162
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
163
318
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
164 class BurgParser(burg_parser.Parser):
319
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
165 """ Derived from automatically generated parser """
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
166 def parse(self, l):
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
167 self.system = BurgSystem()
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
168 super().parse(l)
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
169 return self.system
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
170
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
171
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
172 class BurgGenerator:
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
173 def print(self, *args):
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
174 """ Print helper function that prints to output file """
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
175 print(*args, file=self.output_file)
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
176
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
177 def generate(self, system, output_file):
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
178 """ Generate script that implements the burg spec """
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
179 self.output_file = output_file
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
180 self.system = system
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
181
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
182 self.print('#!/usr/bin/python')
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
183 self.print('from tree import Tree, BaseMatcher, State')
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
184 self.print()
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
185 self.print('class Matcher(BaseMatcher):')
320
84d67cce67b7 Working burg
Windel Bouwman
parents: 319
diff changeset
186 self.print(' def __init__(self):')
84d67cce67b7 Working burg
Windel Bouwman
parents: 319
diff changeset
187 self.print(' self.kid_functions = {}')
84d67cce67b7 Working burg
Windel Bouwman
parents: 319
diff changeset
188 self.print(' self.nts_map = {}')
84d67cce67b7 Working burg
Windel Bouwman
parents: 319
diff changeset
189 self.print(' self.pat_f = {}')
319
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
190 for rule in self.system.rules:
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
191 kids, dummy = self.compute_kids(rule.tree, 't')
320
84d67cce67b7 Working burg
Windel Bouwman
parents: 319
diff changeset
192 rule.num_nts = len(dummy)
319
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
193 lf = 'lambda t: [{}]'.format(', '.join(kids), rule)
320
84d67cce67b7 Working burg
Windel Bouwman
parents: 319
diff changeset
194 pf = 'self.P{}'.format(rule.nr)
84d67cce67b7 Working burg
Windel Bouwman
parents: 319
diff changeset
195 self.print(' # {}: {}'.format(rule.nr, rule))
84d67cce67b7 Working burg
Windel Bouwman
parents: 319
diff changeset
196 self.print(' self.kid_functions[{}] = {}'.format(rule.nr, lf))
84d67cce67b7 Working burg
Windel Bouwman
parents: 319
diff changeset
197 self.print(' self.nts_map[{}] = {}'.format(rule.nr, dummy))
84d67cce67b7 Working burg
Windel Bouwman
parents: 319
diff changeset
198 self.print(' self.pat_f[{}] = {}'.format(rule.nr, pf))
84d67cce67b7 Working burg
Windel Bouwman
parents: 319
diff changeset
199 self.print()
84d67cce67b7 Working burg
Windel Bouwman
parents: 319
diff changeset
200 for rule in self.system.rules:
84d67cce67b7 Working burg
Windel Bouwman
parents: 319
diff changeset
201 if rule.num_nts > 0:
84d67cce67b7 Working burg
Windel Bouwman
parents: 319
diff changeset
202 args = ', ' + ', '.join('nt{}'.format(x) for x in range(rule.num_nts))
84d67cce67b7 Working burg
Windel Bouwman
parents: 319
diff changeset
203 else:
84d67cce67b7 Working burg
Windel Bouwman
parents: 319
diff changeset
204 args = ''
84d67cce67b7 Working burg
Windel Bouwman
parents: 319
diff changeset
205 self.print(' def P{}(self{}):'.format(rule.nr, args))
84d67cce67b7 Working burg
Windel Bouwman
parents: 319
diff changeset
206 self.print(' {}'.format(rule.template))
319
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
207 self.emit_state()
320
84d67cce67b7 Working burg
Windel Bouwman
parents: 319
diff changeset
208 self.print(' def gen(self, tree):')
84d67cce67b7 Working burg
Windel Bouwman
parents: 319
diff changeset
209 self.print(' self.burm_label(tree)')
84d67cce67b7 Working burg
Windel Bouwman
parents: 319
diff changeset
210 self.print(' if not tree.state.has_goal("{}"):'.format(self.system.goal))
84d67cce67b7 Working burg
Windel Bouwman
parents: 319
diff changeset
211 self.print(' raise Exception("Tree not covered")')
84d67cce67b7 Working burg
Windel Bouwman
parents: 319
diff changeset
212 self.print(' self.apply_rules(tree, "{}")'.format(self.system.goal))
319
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
213
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
214 def emit_record(self, rule, state_var):
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
215 # TODO: check for rules fullfilled (by not using 999999)
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
216 self.print(' nts = self.nts({})'.format(rule.nr))
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
217 self.print(' kids = self.kids(tree, {})'.format(rule.nr))
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
218 self.print(' c = sum(x.state.get_cost(y) for x, y in zip(kids, nts)) + {}'.format(rule.cost))
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
219 self.print(' tree.state.set_cost("{}", c, {})'.format(rule.non_term, rule.nr))
320
84d67cce67b7 Working burg
Windel Bouwman
parents: 319
diff changeset
220 for cr in self.system.symbols[rule.non_term].chain_rules:
84d67cce67b7 Working burg
Windel Bouwman
parents: 319
diff changeset
221 self.print(' # Chain rule: {}'.format(cr))
84d67cce67b7 Working burg
Windel Bouwman
parents: 319
diff changeset
222 self.print(' tree.state.set_cost("{}", c + {}, {})'.format(cr.non_term, cr.cost, cr.nr))
319
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
223
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
224 def emit_state(self):
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
225 """ Emit a function that assigns a new state to a node """
320
84d67cce67b7 Working burg
Windel Bouwman
parents: 319
diff changeset
226 self.print(' def burm_state(self, tree):')
84d67cce67b7 Working burg
Windel Bouwman
parents: 319
diff changeset
227 self.print(' tree.state = State()')
319
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
228 for term in self.system.terminals:
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
229 self.emitcase(term)
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
230 self.print()
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
231
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
232 def emitcase(self, term):
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
233 rules = [rule for rule in self.system.rules if rule.tree.name == term]
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
234 for rule in rules:
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
235 condition = self.emittest(rule.tree, 'tree')
320
84d67cce67b7 Working burg
Windel Bouwman
parents: 319
diff changeset
236 self.print(' if {}:'.format(condition))
319
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
237 self.emit_record(rule, 'state')
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
238
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
239 def compute_kids(self, t, root_name):
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
240 """ Compute of a pattern the blanks that must be provided from below in the tree """
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
241 if t.name in self.system.non_terminals:
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
242 return [root_name], [t.name]
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
243 else:
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
244 k = []
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
245 nts = []
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
246 for i, c in enumerate(t.children):
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
247 pfx = root_name + '.children[{}]'.format(i)
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
248 kf, dummy = self.compute_kids(c, pfx)
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
249 nts.extend(dummy)
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
250 k.extend(kf)
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
251 return k, nts
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
252
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
253
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
254 def emittest(self, tree, prefix):
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
255 """ Generate condition for a tree pattern """
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
256 ct = (c for c in tree.children if c.name not in self.system.non_terminals)
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
257 child_tests = (self.emittest(c, prefix + '.children[{}]'.format(i)) for i, c in enumerate(ct))
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
258 child_tests = ('({})'.format(ct) for ct in child_tests)
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
259 child_tests = ' and '.join(child_tests)
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
260 child_tests = ' and ' + child_tests if child_tests else ''
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
261 tst = '{}.name == "{}"'.format(prefix, tree.name)
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
262 return tst + child_tests
318
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
263
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
264
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
265 def main():
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
266 # Parse arguments:
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
267 parser = argparse.ArgumentParser(description='pyburg bottom up rewrite system generator compiler compiler')
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
268 parser.add_argument('source', type=argparse.FileType('r'), \
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
269 help='the parser specification')
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
270 parser.add_argument('-o', '--output', type=argparse.FileType('w'), \
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
271 default=sys.stdout)
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
272 args = parser.parse_args()
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
273 src = args.source.read()
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
274 args.source.close()
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
275
319
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
276 # Parse specification into burgsystem:
318
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
277 l = BurgLexer()
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
278 p = BurgParser()
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
279 l.feed(src)
319
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
280 burg_system = p.parse(l)
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
281
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
282 # Generate matcher:
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
283 generator = BurgGenerator()
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
284 generator.generate(burg_system, args.output)
318
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
285
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
286 if __name__ == '__main__':
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
287 main()