-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathgen_tree.py
More file actions
40 lines (34 loc) · 1.26 KB
/
gen_tree.py
File metadata and controls
40 lines (34 loc) · 1.26 KB
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
import sys
import ast
if(len(sys.argv) != 2):
print("Provide the path to generated tree file")
sys.exit(1)
path_to_tree = sys.argv[1]
path_to_dot = '.'.join(path_to_tree.split('.')[:-1])+".dot"
fdot= open(path_to_dot,"w")
fdot.write("digraph ParseTree {")
fdot.write("\n")
def writeGraph(outerList, stateId):
currentStateId=stateId
nextStateId=stateId+1
name=outerList[0]
if(len(outerList) > 1):
for innerList in outerList[1:]:
if(len(innerList)>0):
# Label Current Vertex
fdot.write(str(currentStateId) + "[label=\"" + str(name) + "\"]; ")
if ((innerList[0][0])=="\""):
innerList[0]=innerList[0][1:-1]
# Label Next Vertex
fdot.write(str(nextStateId) + "[label=\"" + str(innerList[0]) + "\"]; ")
# Mark Edge from Current Vertex to Next Vertex
# Edge from Outer List to Inner List
fdot.write(str(currentStateId) + "->" + str(nextStateId) + " ;\n")
nextStateId = writeGraph(innerList, nextStateId)
return nextStateId
with open(path_to_tree, 'r') as ftree:
output = ast.literal_eval(ftree.read())
# print(output)
writeGraph(output, 0)
fdot.write("}")
fdot.close()