annotate python/pyburg.py @ 364:c49459768aaa

Work on globals
author Windel Bouwman
date Wed, 19 Mar 2014 20:24:03 +0100
parents 818be710e13d
children d2ddfe134c48
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))),
357
818be710e13d Added acceptance function to burg
Windel Bouwman
parents: 354
diff changeset
79 ('STRING', r"'[^']*'", lambda typ, val: ('string', val[1:-1])),
319
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
80 ('OTHER', r'[:;\|\(\),]', lambda typ, val: (val, val)),
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
81 ('SKIP', r'[ ]', None)
318
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
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
84 lines = txt.split('\n')
322
44f336460c2a Half of use of burg spec for arm
Windel Bouwman
parents: 321
diff changeset
85 header_lines = []
318
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
86
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
87 def tokenize():
322
44f336460c2a Half of use of burg spec for arm
Windel Bouwman
parents: 321
diff changeset
88 section = 0
318
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
89 for line in lines:
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
90 line = line.strip()
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
91 if not line:
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
92 continue # Skip empty lines
319
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
93 elif line == '%%':
322
44f336460c2a Half of use of burg spec for arm
Windel Bouwman
parents: 321
diff changeset
94 section += 1
44f336460c2a Half of use of burg spec for arm
Windel Bouwman
parents: 321
diff changeset
95 if section == 1:
44f336460c2a Half of use of burg spec for arm
Windel Bouwman
parents: 321
diff changeset
96 yield Token('header', header_lines)
318
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
97 yield Token('%%', '%%')
319
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
98 else:
322
44f336460c2a Half of use of burg spec for arm
Windel Bouwman
parents: 321
diff changeset
99 if section == 0:
44f336460c2a Half of use of burg spec for arm
Windel Bouwman
parents: 321
diff changeset
100 header_lines.append(line)
44f336460c2a Half of use of burg spec for arm
Windel Bouwman
parents: 321
diff changeset
101 else:
44f336460c2a Half of use of burg spec for arm
Windel Bouwman
parents: 321
diff changeset
102 yield from baselex.tokenize(tok_spec, line)
319
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
103 yield Token(EOF, EOF)
318
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
104 self.tokens = tokenize()
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
105 self.token = self.tokens.__next__()
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
106
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
107 def next_token(self):
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
108 t = self.token
319
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
109 if t.typ != EOF:
318
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
110 self.token = self.tokens.__next__()
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
111 return t
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
112
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
113
319
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
114 class Rule:
320
84d67cce67b7 Working burg
Windel Bouwman
parents: 319
diff changeset
115 """ A rewrite rule. Specifies a tree that can be rewritten into a result
84d67cce67b7 Working burg
Windel Bouwman
parents: 319
diff changeset
116 at a specific cost """
357
818be710e13d Added acceptance function to burg
Windel Bouwman
parents: 354
diff changeset
117 def __init__(self, non_term, tree, cost, acceptance, template):
319
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
118 self.non_term = non_term
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
119 self.tree = tree
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
120 self.cost = cost
357
818be710e13d Added acceptance function to burg
Windel Bouwman
parents: 354
diff changeset
121 self.acceptance = acceptance
319
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
357
818be710e13d Added acceptance function to burg
Windel Bouwman
parents: 354
diff changeset
157 def add_rule(self, non_term, tree, cost, acceptance, template):
818be710e13d Added acceptance function to burg
Windel Bouwman
parents: 354
diff changeset
158 template = template.strip()
320
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'
357
818be710e13d Added acceptance function to burg
Windel Bouwman
parents: 354
diff changeset
161 rule = Rule(non_term, tree, cost, acceptance, 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:
357
818be710e13d Added acceptance function to burg
Windel Bouwman
parents: 354
diff changeset
234 args = ', '.join('c{}'.format(x) for x in range(rule.num_nts))
818be710e13d Added acceptance function to burg
Windel Bouwman
parents: 354
diff changeset
235 args = ', ' + args
320
84d67cce67b7 Working burg
Windel Bouwman
parents: 319
diff changeset
236 else:
84d67cce67b7 Working burg
Windel Bouwman
parents: 319
diff changeset
237 args = ''
357
818be710e13d Added acceptance function to burg
Windel Bouwman
parents: 354
diff changeset
238 # Create template function:
322
44f336460c2a Half of use of burg spec for arm
Windel Bouwman
parents: 321
diff changeset
239 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
240 template = rule.template
44f336460c2a Half of use of burg spec for arm
Windel Bouwman
parents: 321
diff changeset
241 for t in template.split(';'):
44f336460c2a Half of use of burg spec for arm
Windel Bouwman
parents: 321
diff changeset
242 self.print(' {}'.format(t.strip()))
357
818be710e13d Added acceptance function to burg
Windel Bouwman
parents: 354
diff changeset
243 # Create acceptance function:
818be710e13d Added acceptance function to burg
Windel Bouwman
parents: 354
diff changeset
244 if rule.acceptance:
818be710e13d Added acceptance function to burg
Windel Bouwman
parents: 354
diff changeset
245 self.print(' def A{}(self, tree):'.format(rule.nr))
818be710e13d Added acceptance function to burg
Windel Bouwman
parents: 354
diff changeset
246 for t in rule.acceptance.split(';'):
818be710e13d Added acceptance function to burg
Windel Bouwman
parents: 354
diff changeset
247 self.print(' {}'.format(t.strip()))
319
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
248 self.emit_state()
320
84d67cce67b7 Working burg
Windel Bouwman
parents: 319
diff changeset
249 self.print(' def gen(self, tree):')
84d67cce67b7 Working burg
Windel Bouwman
parents: 319
diff changeset
250 self.print(' self.burm_label(tree)')
84d67cce67b7 Working burg
Windel Bouwman
parents: 319
diff changeset
251 self.print(' if not tree.state.has_goal("{}"):'.format(self.system.goal))
357
818be710e13d Added acceptance function to burg
Windel Bouwman
parents: 354
diff changeset
252 self.print(' raise Exception("Tree {} not covered".format(tree))')
323
e9fe6988497c Used burg for generating expressions
Windel Bouwman
parents: 322
diff changeset
253 self.print(' return self.apply_rules(tree, "{}")'.format(self.system.goal))
319
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
254
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
255 def emit_record(self, rule, state_var):
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
256 # TODO: check for rules fullfilled (by not using 999999)
357
818be710e13d Added acceptance function to burg
Windel Bouwman
parents: 354
diff changeset
257 acc = ''
818be710e13d Added acceptance function to burg
Windel Bouwman
parents: 354
diff changeset
258 if rule.acceptance:
818be710e13d Added acceptance function to burg
Windel Bouwman
parents: 354
diff changeset
259 acc = ' and self.A{}(tree)'.format(rule.nr)
322
44f336460c2a Half of use of burg spec for arm
Windel Bouwman
parents: 321
diff changeset
260 self.print(' nts = self.nts({})'.format(rule.nr))
44f336460c2a Half of use of burg spec for arm
Windel Bouwman
parents: 321
diff changeset
261 self.print(' kids = self.kids(tree, {})'.format(rule.nr))
357
818be710e13d Added acceptance function to burg
Windel Bouwman
parents: 354
diff changeset
262 self.print(' if all(x.state.has_goal(y) for x, y in zip(kids, nts)){}:'.format(acc))
354
5477e499b039 Added some sort of string functionality
Windel Bouwman
parents: 323
diff changeset
263 self.print(' c = sum(x.state.get_cost(y) for x, y in zip(kids, nts)) + {}'.format(rule.cost))
5477e499b039 Added some sort of string functionality
Windel Bouwman
parents: 323
diff changeset
264 self.print(' tree.state.set_cost("{}", c, {})'.format(rule.non_term, rule.nr))
320
84d67cce67b7 Working burg
Windel Bouwman
parents: 319
diff changeset
265 for cr in self.system.symbols[rule.non_term].chain_rules:
354
5477e499b039 Added some sort of string functionality
Windel Bouwman
parents: 323
diff changeset
266 self.print(' # Chain rule: {}'.format(cr))
5477e499b039 Added some sort of string functionality
Windel Bouwman
parents: 323
diff changeset
267 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
268
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
269 def emit_state(self):
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
270 """ Emit a function that assigns a new state to a node """
320
84d67cce67b7 Working burg
Windel Bouwman
parents: 319
diff changeset
271 self.print(' def burm_state(self, tree):')
322
44f336460c2a Half of use of burg spec for arm
Windel Bouwman
parents: 321
diff changeset
272 self.print(' tree.state = State()')
319
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
273 for term in self.system.terminals:
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
274 self.emitcase(term)
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
275 self.print()
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
276
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
277 def emitcase(self, term):
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
278 rules = [rule for rule in self.system.rules if rule.tree.name == term]
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
279 for rule in rules:
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
280 condition = self.emittest(rule.tree, 'tree')
322
44f336460c2a Half of use of burg spec for arm
Windel Bouwman
parents: 321
diff changeset
281 self.print(' if {}:'.format(condition))
319
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
282 self.emit_record(rule, 'state')
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
283
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
284 def compute_kids(self, t, root_name):
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
285 """ 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
286 if t.name in self.system.non_terminals:
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
287 return [root_name], [t.name]
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
288 else:
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
289 k = []
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
290 nts = []
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
291 for i, c in enumerate(t.children):
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
292 pfx = root_name + '.children[{}]'.format(i)
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
293 kf, dummy = self.compute_kids(c, pfx)
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
294 nts.extend(dummy)
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
295 k.extend(kf)
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
296 return k, nts
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
297
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
298
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
299 def emittest(self, tree, prefix):
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
300 """ Generate condition for a tree pattern """
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
301 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
302 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
303 child_tests = ('({})'.format(ct) for ct in child_tests)
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
304 child_tests = ' and '.join(child_tests)
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
305 child_tests = ' and ' + child_tests if child_tests else ''
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
306 tst = '{}.name == "{}"'.format(prefix, tree.name)
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
307 return tst + child_tests
318
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
308
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
309
321
8c569fbe60e4 Load yacc and burg dynamic
Windel Bouwman
parents: 320
diff changeset
310 def make_argument_parser():
8c569fbe60e4 Load yacc and burg dynamic
Windel Bouwman
parents: 320
diff changeset
311 """ Constructs an argument parser """
318
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
312 parser = argparse.ArgumentParser(description='pyburg bottom up rewrite system generator compiler compiler')
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
313 parser.add_argument('source', type=argparse.FileType('r'), \
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
314 help='the parser specification')
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
315 parser.add_argument('-o', '--output', type=argparse.FileType('w'), \
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
316 default=sys.stdout)
321
8c569fbe60e4 Load yacc and burg dynamic
Windel Bouwman
parents: 320
diff changeset
317 return parser
8c569fbe60e4 Load yacc and burg dynamic
Windel Bouwman
parents: 320
diff changeset
318
322
44f336460c2a Half of use of burg spec for arm
Windel Bouwman
parents: 321
diff changeset
319 def load_as_module(filename):
44f336460c2a Half of use of burg spec for arm
Windel Bouwman
parents: 321
diff changeset
320 """ 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
321 ob = io.StringIO()
44f336460c2a Half of use of burg spec for arm
Windel Bouwman
parents: 321
diff changeset
322 args = argparse.Namespace(source=open(filename), output=ob)
44f336460c2a Half of use of burg spec for arm
Windel Bouwman
parents: 321
diff changeset
323 main(args)
44f336460c2a Half of use of burg spec for arm
Windel Bouwman
parents: 321
diff changeset
324
44f336460c2a Half of use of burg spec for arm
Windel Bouwman
parents: 321
diff changeset
325 matcher_mod = types.ModuleType('generated_matcher')
44f336460c2a Half of use of burg spec for arm
Windel Bouwman
parents: 321
diff changeset
326 exec(ob.getvalue(), matcher_mod.__dict__)
44f336460c2a Half of use of burg spec for arm
Windel Bouwman
parents: 321
diff changeset
327 return matcher_mod
44f336460c2a Half of use of burg spec for arm
Windel Bouwman
parents: 321
diff changeset
328
321
8c569fbe60e4 Load yacc and burg dynamic
Windel Bouwman
parents: 320
diff changeset
329
8c569fbe60e4 Load yacc and burg dynamic
Windel Bouwman
parents: 320
diff changeset
330 def main(args):
318
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
331 src = args.source.read()
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
332 args.source.close()
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
333
319
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
334 # Parse specification into burgsystem:
318
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
335 l = BurgLexer()
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
336 p = BurgParser()
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
337 l.feed(src)
319
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
338 burg_system = p.parse(l)
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
339
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
340 # Generate matcher:
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
341 generator = BurgGenerator()
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
342 generator.generate(burg_system, args.output)
318
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
343
321
8c569fbe60e4 Load yacc and burg dynamic
Windel Bouwman
parents: 320
diff changeset
344
318
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents:
diff changeset
345 if __name__ == '__main__':
321
8c569fbe60e4 Load yacc and burg dynamic
Windel Bouwman
parents: 320
diff changeset
346 # Parse arguments:
8c569fbe60e4 Load yacc and burg dynamic
Windel Bouwman
parents: 320
diff changeset
347 args = make_argument_parser().parse_args()
8c569fbe60e4 Load yacc and burg dynamic
Windel Bouwman
parents: 320
diff changeset
348 main(args)