-
Notifications
You must be signed in to change notification settings - Fork 0
/
ebnf_codegen.py
340 lines (244 loc) · 9.01 KB
/
ebnf_codegen.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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
import sys
import ebnf_semantic
class StateNode(object):
# case label:
# switch (block[i]) {
# case c (c in cases): next_state = $TO_STATE_ENUM(cases[c]);
# default: next_state = $TO_STATE_ENUM(cases[None]);
# }
#
# break;
def __init__(self):
self.uid = id(self)
self.cases = None
def __repr__(self):
return "%s(%s, %s)"%(type(self).__name__, repr(self.uid), repr(self.cases))
def pretty_print(self, labels):
print "state_%s:"%labels[self.uid]
print "\tswitch (c) {"
for cinput in self.cases:
if cinput == None:
continue
else:
print "\t\tcase %s: goto state_%s;"%(repr(cinput), labels[self.cases[cinput]])
if None in self.cases:
print "\t\tdefault: goto state_%s;"%(labels[self.cases[None]])
print "\t}\n"
class StateGraph(object):
def __init__(self, nodes, entry, success_edges, error_edges):
self.nodes = nodes
self.entry = entry
self.success_edges = success_edges
self.error_edges = error_edges
def __repr__(self):
return "%s(%s, %s, %s, %s)"%(type(self).__name__, repr(self.nodes), repr(self.entry), repr(self.success_edges), repr(self.error_edges))
def pretty_print(self):
labels = {}
for i, state in enumerate(self.nodes):
labels[state] = str(i)
labels["success"] = "success"
labels["error"] = "error"
print "entry label: state_%s\n"%labels[self.entry]
for state in self.nodes:
self.nodes[state].pretty_print(labels)
def replace_success_with_error(graph):
for state, cinput in graph.success_edges:
graph.nodes[state].cases[cinput] = "error"
graph.error_edges.extend(graph.success_edges)
graph.success_edges = []
def merge_optionals_and_repetitions(pattern, subgraphs):
merged_subgraphs = []
i = 0
while i < len(pattern.terms):
term = pattern.terms[i]
subgraph = subgraphs[i]
i += 1
while i < len(pattern.terms) and type(term) in [ebnf_semantic.Optional, ebnf_semantic.Repetition]:
term = pattern.terms[i]
if type(term) not in [ebnf_semantic.Optional, ebnf_semantic.Repetition]:
replace_success_with_error(subgraph)
subgraph = merge_graphs([subgraph, subgraphs[i]])
i += 1
merged_subgraphs.append(subgraph)
return merged_subgraphs
def merge_cases_list(cases_list):
new_cases = {}
for cases, graph in cases_list:
for cinput, state in cases.items():
if cinput not in new_cases:
new_cases[cinput] = []
if state != "error":
new_cases[cinput].append((graph, state))
for cinput in new_cases:
for graph, state in new_cases[cinput]:
if state == "success":
new_cases[cinput] = [(graph, "success")]
break
return new_cases
def minimize_graph(graph):
# Last step of merging, minimize the DFA by combining common states
back_edges = {}
for state in graph.nodes:
cases = graph.nodes[state].cases
for cinput in cases:
next_state = cases[cinput]
if next_state not in back_edges:
back_edges[next_state] = {}
if state not in back_edges[next_state]:
back_edges[next_state][state] = []
back_edges[next_state][state].append(cinput)
back_edges["success"] = {}
for state, cinput in graph.success_edges:
if state not in back_edges["success"]:
back_edges["success"][state] = []
back_edges["success"][state].append(cinput)
back_edges["error"] = {}
for state, cinput in graph.error_edges:
if state not in back_edges["error"]:
back_edges["error"][state] = []
back_edges["error"][state].append(cinput)
merge_stack = ["success", "error"]
while merge_stack:
merged_state = merge_stack.pop()
predecessors = back_edges[merged_state]
replacements = {}
cache = {}
for state in predecessors:
state_cases = frozenset(graph.nodes[state].cases.items())
if state_cases not in cache:
cache[state_cases] = state
else:
replacements[state] = cache[state_cases]
if cache[state_cases] not in merge_stack:
merge_stack.append(cache[state_cases])
for state in replacements:
for prev_state in back_edges[state]:
if prev_state in graph.nodes:
for cinput in back_edges[state][prev_state]:
graph.nodes[prev_state].cases[cinput] = replacements[state]
for cinput, next_state in graph.nodes[state].cases.items():
del back_edges[next_state][state]
if replacements[state] not in back_edges[next_state]:
back_edges[next_state][replacements[state]] = []
back_edges[next_state][replacements[state]].append(cinput)
del graph.nodes[state]
graph.success_edges = [(state, cinput) for state, cinput in graph.success_edges if state in graph.nodes]
graph.error_edges = [(state, cinput) for state, cinput in graph.error_edges if state in graph.nodes]
def merge_graphs(graphs):
# Convert from NDFA to DFA and then minimize
nodes = graphs[0].nodes
entry = graphs[0].entry
success_edges = []
error_edges = []
entry_node = graphs[0].nodes[entry]
merge_stack = []
node_stack = []
merge_stack.append([(graph, graph.entry) for graph in graphs])
node_stack.append(entry_node)
merge_cache = set({})
while merge_stack:
states_to_merge = merge_stack.pop()
node = node_stack.pop()
state_tuple = tuple([state for graph, state in states_to_merge])
if (state_tuple, node) in merge_cache:
continue
merge_cache.add((state_tuple, node))
cases_choices = merge_cases_list([(graph.nodes[state].cases, graph) for graph, state in states_to_merge])
nodes[node.uid] = node
for cinput in cases_choices:
if len(cases_choices[cinput]) == 0:
node.cases[cinput] = "error"
error_edges.append((node.uid, cinput))
elif set([state for graph, state in cases_choices[cinput]]) == set(["success"]):
node.cases[cinput] = "success"
success_edges.append((node.uid, cinput))
else:
graph, state = cases_choices[cinput][0]
new_node = graph.nodes[state]
node.cases[cinput] = new_node.uid
merge_stack.append(cases_choices[cinput])
node_stack.append(new_node)
new_graph = StateGraph(nodes, entry, success_edges, error_edges)
minimize_graph(new_graph)
return new_graph
def build_state_graph_for_terminal(pattern, ast_info):
terminal = pattern.terminal
nodes = {}
error_edges = []
concat_nodes = [StateNode() for i in range(len(terminal))]
for i in range(len(terminal) - 1):
node = concat_nodes[i]
node.cases = {terminal[i]: concat_nodes[i + 1].uid, None: "error"}
nodes[node.uid] = node
error_edges.append((node.uid, None))
node = concat_nodes[-1]
node.cases = {terminal[-1]: "success", None: "error"}
nodes[node.uid] = node
success_edges = [(node.uid, terminal[-1])]
error_edges.append((node.uid, None))
entry = concat_nodes[0].uid
return StateGraph(nodes, entry, success_edges, error_edges)
def build_state_graph_for_optional(pattern, ast_info):
subgraph = build_state_graph_demux(pattern.rhs, ast_info)
for edge in subgraph.error_edges:
state = edge[0]
cinput = edge[1]
subgraph.nodes[state].cases[cinput] = "success"
subgraph.success_edges.append(edge)
subgraph.error_edges = []
return subgraph
def build_state_graph_for_repetition(pattern, ast_info):
subgraph = build_state_graph_demux(pattern.rhs, ast_info)
for edge in subgraph.success_edges:
state = edge[0]
cinput = edge[1]
subgraph.nodes[state].cases[cinput] = subgraph.entry
success_edges = []
for edge in subgraph.error_edges:
state = edge[0]
cinput = edge[1]
subgraph.nodes[state].cases[cinput] = "success"
success_edges.append(edge)
subgraph.success_edges = success_edges
subgraph.error_edges = []
return subgraph
def build_state_graph_for_concatenation(pattern, ast_info):
subgraphs = [build_state_graph_demux(term, ast_info) for term in pattern.terms]
subgraphs = merge_optionals_and_repetitions(pattern, subgraphs)
nodes = {}
error_edges = []
for i in range(len(subgraphs) - 1):
term = pattern.terms[i]
subgraph = subgraphs[i]
next_subgraph = subgraphs[i + 1]
for state, cinput in subgraph.success_edges:
subgraph.nodes[state].cases[cinput] = next_subgraph.entry
error_edges.extend(subgraph.error_edges)
nodes.update(subgraph.nodes)
nodes.update(subgraphs[-1].nodes)
entry = subgraphs[0].entry
success_edges = subgraphs[-1].success_edges
error_edges.extend(subgraphs[-1].error_edges)
return StateGraph(nodes, entry, success_edges, error_edges)
def build_state_graph_for_alternation(pattern, ast_info):
graphs = [build_state_graph_demux(clause, ast_info) for clause in pattern.clauses]
return merge_graphs(graphs)
def build_state_graph_demux(pattern, ast_info):
builders = {ebnf_semantic.Alternation: build_state_graph_for_alternation,
ebnf_semantic.Concatenation: build_state_graph_for_concatenation,
ebnf_semantic.Repetition: build_state_graph_for_repetition,
ebnf_semantic.Optional: build_state_graph_for_optional,
ebnf_semantic.Terminal: build_state_graph_for_terminal}
return builders[type(pattern)](pattern, ast_info)
def build_state_graph(ast_info):
return build_state_graph_demux(ast_info.rules[ast_info.top_id], ast_info)
if len(sys.argv) != 3:
print "Usage: %s <grammar file> <top_id>"%(sys.argv[0])
quit()
filename = sys.argv[1]
top_id = sys.argv[2]
with open(filename, 'r') as f:
description = f.read()
ast_info = ebnf_semantic.create_ast(description, top_id)
graph = build_state_graph(ast_info)
graph.pretty_print()