forked from PAPAGENO-devels/papageno
-
Notifications
You must be signed in to change notification settings - Fork 0
/
papageno.py
executable file
·311 lines (262 loc) · 10.3 KB
/
papageno.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
#!/usr/bin/env python2
import sys, os, re, argparse
from papageno_lib import input_parse, scanner, bitPack, classes, check, matrix, code_emission, transform
commandline_args = input_parse.parse_commandline_args()
print commandline_args;
rules,axiom,cPreamble = input_parse.parse_grammar_description(commandline_args)
# Infer (non)terminals.
nonterminals, terminals = check.inferTokens(rules, axiom)
if commandline_args.verbose==2:
print "Inferred nonterminals: %r" % nonterminals
print "Inferred terminals: %r" % terminals
# Detect bad rules.
check.detectBadRules(nonterminals, terminals, axiom, rules)
# Detect repeated rhs.
repeatedError = check.detectRepeatedRhs(rules)
# Delete repeated rhs.
if repeatedError:
newAxiom = '_NewAxiom'
if commandline_args.verbose==2:
print "The previous grammar had %s rules, %s nonterminals, %s terminals" % (len(rules), len(nonterminals), len(terminals))
print "Grammar after elimination of repeated rhs with newAxiom: %s" %newAxiom
newRules, newNonterminalsSet = transform.deleteRepeatedRhs(nonterminals, terminals, axiom, newAxiom, rules)
rules = []
for rhs, lhs in newRules.items():
currentRule = classes.Rule()
rules.append(currentRule)
if len(lhs) == 1:
currentRule.lhs = next(iter(lhs))
else:
currentRule.lhs = "_" + "__".join(sorted(lhs))
for token in rhs:
if token in terminals:
currentRule.rhs.append(token)
elif len(token) == 1:
currentRule.rhs.append(next(iter(token)))
else:
currentRule.rhs.append("_" + "__".join(sorted(token)))
nonterminals = []
for n in newNonterminalsSet:
if len(n) == 1:
nonterminals.append(next(iter(n)))
else:
nonterminals.append("_" + "__".join(sorted(n)))
axiom = newAxiom
#These statements have been done in check.py for the old set of nonterminals
nonterminals.remove(axiom)
nonterminals.insert(0, axiom)
repeatedError = False
if commandline_args.verbose==2:
print "Printing new nonterminals:"
print nonterminals
print "Printing new rules:"
for rule in rules:
sys.stdout.write(rule.toString() + "\n")
numNewRules = len(rules)
numNewNonterminals = len(nonterminals)
print "There are %s rules, %s nonterminals" % (numNewRules, numNewNonterminals)
# Detect bad rules.
check.detectBadRules(nonterminals, terminals, axiom, rules)
# Detect unused nonterminals.
unusedNTerm = check.detectUnusedNTerm(nonterminals, axiom, rules)
# Detect non-lhs nonterminals.
check.detectUndefNTerm(nonterminals, rules, unusedNTerm)
# Detect unused terminals.
check.detectUnusedTerm(terminals, rules)
# Detect lhs terminals.
check.detectDefinedTerm(terminals, rules)
# Compute precedence matrix.
origMatrix, conflictError = matrix.buildAndCheckMatrix(nonterminals, terminals, rules)
# Interrupt generation in case of errors.
if repeatedError or conflictError:
if raw_input("Grammar is not in the required operator precedence form. Continue (y/n)?") != "y":
sys.exit(-1)
# Build real matrix.
realMatrix = matrix.toRealMatrix(origMatrix, terminals)
# Build integer matrix.
intMatrix, rowLen = matrix.toIntMatrix(realMatrix, terminals)
if commandline_args.verbose==2:
sys.stdout.write("Printing human readable precedence matrix.\n")
sys.stdout.write("%10s " % "i\\j")
for j in terminals:
sys.stdout.write("%10s " % j)
sys.stdout.write("\n")
for i in terminals:
sys.stdout.write("%10s " % i)
for j in terminals:
sys.stdout.write("%10s " % realMatrix[i][j])
sys.stdout.write("\n")
if commandline_args.verbose==1:
sys.stdout.write("Printing real matrix.\n")
for i in xrange(0, len(terminals)):
for j in xrange(0, rowLen):
sys.stdout.write("0x%2x " % intMatrix[i*rowLen + j])
sys.stdout.write("\n")
sys.stdout.write("Printing real conceptual matrix.\n")
sys.stdout.write("%10s " % "i\\j")
for j in xrange(0, len(terminals)):
sys.stdout.write("%10s " % terminals[j])
sys.stdout.write("\n")
for i in xrange(0, len(terminals)):
sys.stdout.write("%10s " % terminals[i])
for j in xrange(0, len(terminals)):
sys.stdout.write("%10s " % bitPack.getPrecedence(intMatrix, i, j, rowLen))
sys.stdout.write("\n")
sys.stdout.write("Printing C matrix.\n")
sys.stdout.write("uint8_t __matrix[__ROW_LEN*__TERM_LEN] = {%d" % intMatrix[0])
for i in xrange(1, len(intMatrix)):
sys.stdout.write(", %d" % intMatrix[i])
sys.stdout.write("}\n")
# Create reduction tree.
root = classes.ReductionNode(len(rules))
for index in xrange(0, len(rules)):
rule = rules[index]
node = root
for rhsToken in rule.rhs:
tempNode = node.hasSonWith(rhsToken)
if not tempNode:
tempNode = classes.ReductionNode(len(rules))
node.sons[rhsToken] = tempNode
node = tempNode
node.rule_id = index
if commandline_args.verbose==2:
sys.stdout.write("Reduction tree\nroot:%s" % root.recursiveToString(0))
# Vectorize reduction tree.
# compute the number of nodes
treeSize = 1 + root.getSubtreeSize()
# allocate a list large enough to contain them
vectorTree = [0]*treeSize
current_position = 1
vectorTree, vectorTree[0], current_position = root.subtreeToVector(vectorTree, current_position, nonterminals, terminals)
# Print vector tree.
if commandline_args.verbose==1:
sys.stdout.write("Vectorized reduction tree\n")
current_position = vectorTree[0]
level = 0
workList = []
workList.append([current_position, level, "root"])
while len(workList) != 0:
item = workList.pop(0)
current_position = item[0]
level = item[1]
label = item[2]
sys.stdout.write(" "*level)
sys.stdout.write("%s:%d\n" % (label, vectorTree[current_position]))
sonsNumber = vectorTree[current_position + 1]/2
current_position += 2
for i in xrange(0, sonsNumber):
label = bitPack.intToToken(vectorTree[current_position + i*2], nonterminals, terminals)
sonOffset = vectorTree[current_position + i*2 + 1]
workList.insert(i, [sonOffset, level + 1, label])
# Print C vector tree.
if commandline_args.verbose==1:
sys.stdout.write("C vectorized reduction tree\n")
sys.stdout.write("uint16_t __reduction_tree = {%d" % vectorTree[0])
for i in xrange(1, len(vectorTree)):
sys.stdout.write(", %d" % vectorTree[i])
sys.stdout.write("};\n")
# Create rewrite rules.
rewrite = dict()
for nonterminal in nonterminals:
rewrite[nonterminal] = []
modified = True
while modified:
modified = False
for rule in rules:
lhs = rule.lhs
token = rule.rhs[0]
if len(rule.rhs) != 1 or token in terminals:
continue
if token not in rewrite[lhs]:
modified = True
rewrite[lhs].append(token)
else:
for ttoken in rewrite[token]:
if ttoken not in rewrite[lhs]:
modified = True
rewrite[lhs].append(ttoken)
# Create inverse rewrite rules.
invRewrite = dict()
for nonterminal in nonterminals:
invRewrite[nonterminal] = [nonterminal]
for nonterminal in nonterminals:
for token in rewrite[nonterminal]:
invRewrite[token].append(nonterminal)
# Print inverse rewrite rules.
if commandline_args.verbose==2:
print "Rewrite rules"
for nonterminal in nonterminals:
sys.stdout.write("Rewrite(%s) = {" % nonterminal)
for token in invRewrite[nonterminal]:
sys.stdout.write(" %s" % token)
sys.stdout.write(" }\n")
# Create array rewrite rules.
rewriteSize = len(nonterminals)
for nonterminal in nonterminals:
rewriteSize += 1 + len(invRewrite[nonterminal])
realRewrite = [0]*rewriteSize
topOfArray = len(nonterminals)
for nonterminal in nonterminals:
index = nonterminals.index(nonterminal)
realRewrite[index] = topOfArray
realRewrite[topOfArray] = len(invRewrite[nonterminal])
topOfArray += 1
for token in invRewrite[nonterminal]:
realRewrite[topOfArray] = bitPack.tokenToPackedInt(token, nonterminals, terminals)
topOfArray += 1
# Print array rewrite rules.
if commandline_args.verbose==1:
print "Array rewrite rules"
for i in xrange(0, len(nonterminals)):
sys.stdout.write("Rewrite(%s) = {" % nonterminals[i])
offset = realRewrite[i]
end = offset + realRewrite[offset] + 1
offset += 1
while offset != end:
sys.stdout.write(" <%d:%s>" % (realRewrite[offset], bitPack.packedIntToToken(realRewrite[offset], nonterminals, terminals)))
offset += 1
sys.stdout.write(" }\n")
# Print C rewrite rules.
if commandline_args.verbose==1:
print "C rewrite rules"
sys.stdout.write("uint32_t rewrite[] = {%d" % realRewrite[0])
for i in xrange(1, len(realRewrite)):
sys.stdout.write(", %d" % realRewrite[i])
sys.stdout.write("};\n")
# Create rhs mapping for $x substitutions and headerName.
for rule in rules:
rule.tokenMap["lhs"] = "p_" + rule.lhs
for i in xrange(0, len(rule.rhs)):
rule.tokenMap[i + 1] = "p_" + rule.rhs[i] + "%d" % (i + 1)
# Execute $x substitutions.
for rule in rules:
rule.text = rule.text.replace("$$", rule.tokenMap["lhs"] + "->value")
for i in xrange(0, len(rule.rhs)):
rule.text = rule.text.replace("$%d" % (i + 1), rule.tokenMap[i + 1] + "->value")
if commandline_args.verbose==1:
print "Rules tokenmaps."
for rule in rules:
print rule.tokenMap
# Compute average rule length
average_rule_len=0.0
for rule in rules:
average_rule_len=average_rule_len+len(rule.rhs)
average_rule_len=average_rule_len/len(rules)
# Generate output files.
# Generate grammar_tokens.h
code_emission.emit_grammar_symbols(nonterminals,terminals,axiom,commandline_args.out_header)
# Generate grammar_semantics.h
code_emission.emit_semantic_actions_header(rules,commandline_args.out_header)
# Generate grammar_semantics.c
code_emission.emit_semantic_actions(rules,cPreamble,commandline_args.out_core)
# Generate grammar.h
code_emission.emit_grammar_header(rules,commandline_args.out_header)
# Generate grammar.c
code_emission.emit_grammar(terminals, nonterminals, axiom, rules ,commandline_args.out_core)
# Generate matrix.h
code_emission.emit_precedence_matrix_header(intMatrix,len(terminals),commandline_args.out_header)
# Generate reduction_tree.c
code_emission.emit_reduction_tree(vectorTree,commandline_args.out_header)
# Generate rewrite_rules.h
code_emission.emit_rewrite_rules(realRewrite,commandline_args.out_header)
code_emission.emit_config_header(commandline_args.cache_line_size,average_rule_len,commandline_args.token_avg_size,commandline_args.prealloc_stack,commandline_args.recombination,commandline_args.out_header)