annotate python/pyburg.py @ 323:e9fe6988497c

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