-
Notifications
You must be signed in to change notification settings - Fork 3
/
grammar_helper.py
executable file
·56 lines (43 loc) · 1.25 KB
/
grammar_helper.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
#!/usr/bin/python3 -i
# Load stuff
forward_graph = {}
reverse_graph = {}
inf = open("vhdl_grammar.dot", 'r')
for l in inf.readlines():
if "->" in l:
l = l.strip()
head, tail = l.split("->")
head = head.strip()
tail = tail.strip()
if tail[-1] == ";":
tail = tail[:-1]
if head not in forward_graph:
forward_graph[head] = set()
if tail not in reverse_graph:
reverse_graph[tail] = set()
forward_graph[head].add(tail)
reverse_graph[tail].add(head)
def bfs(graph, start, limit):
visited = set()
todo = set()
todo.add((start, 0))
while todo:
node, depth = todo.pop()
if limit != -1 and depth == limit:
continue
indent = depth
if node in visited:
continue
if node not in graph:
continue
print("\n" + " " * indent + node + " ->")
visited.add(node)
next_nodes = graph[node]
for next_node in next_nodes:
print(" " * (indent + 1) + next_node)
todo.add((next_node, indent + 1))
indent += 1
def fwd(start, limit=-1):
bfs(forward_graph, start, limit)
def back(start, limit=-1):
bfs(reverse_graph, start, limit)