-
Notifications
You must be signed in to change notification settings - Fork 4
/
parse.py
104 lines (77 loc) · 2.26 KB
/
parse.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
from matplotlib import pyplot as plt
from src.structures import Grammar, Graph, Production
def get_breaks(f):
breaks = []
for i, line in f:
if line == "-->\n":
labels = i + 1
indexes = i + 2
start = i + 3
elif line == "--<\n":
stop = i - 1
breaks.append((labels, indexes, start, stop))
return breaks
def read_vlabels(f, labels, indexes):
i = f[indexes][1].split()
l = f[labels][1].split()
vlabels = []
for j in range(len(l)):
vlabels.append((int(i[j]), l[j]))
return dict(vlabels)
def read_edges(f, start, stop):
edges = []
for i, line in f:
if start <= i <= stop:
edges.append(get_edge(line))
return edges
def get_edge(s):
res = s.split()
res[0] = int(res[0])
res[len(res)-1] = int(res[len(res)-1])
return res
def edges_to_adj(e, n, labels):
adj = [[] for _ in range(n+1)]
for u, s, v in e:
adj[u].append((v, s))
adj[v].append((u, s))
out = []
for u in labels:
out.append((u, adj[u]))
return dict(out)
def get_graph(f, labels, indexes, start, stop):
vlabels = read_vlabels(f, labels, indexes)
n = 0
for label in vlabels:
n = max(n, label)
edges = read_edges(f, start, stop)
adjacency_list = edges_to_adj(edges, n, vlabels)
return Graph(vlabels, adjacency_list)
def parse(f):
ef = list(enumerate(f))
breaks = get_breaks(ef)
G = get_graph(ef, breaks[0][0], breaks[0][1], breaks[0][2], breaks[0][3])
prods = []
lkr = []
for i in range(1, len(breaks)):
labels, indexes, start, stop = breaks[i]
g = get_graph(ef, labels, indexes, start, stop)
lkr.append(g)
if i % 3 == 0:
prods.append(Production(lkr[0], lkr[1], lkr[2]))
lkr = []
return Grammar(G, prods)
# test
if __name__ == "__main__":
with open("ex2.txt") as f:
grammar = parse(f)
g = grammar.input_graph
prods = grammar.production_list
g.visualize()
plt.show()
for prod in prods:
prod.left.visualize()
plt.show()
prod.connector.visualize()
plt.show()
prod.right.visualize()
plt.show()