-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsg_parser.py
37 lines (23 loc) · 920 Bytes
/
sg_parser.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
#!/usr/bin/env python
def load_sg(file):
sg = { "transitions": [], "inputs": [], "outputs": [] }
ignored = [".state", ".end"]
with open(file, "r") as fid:
lines = fid.read().splitlines()
for line in lines:
if not line or line[0] == '#':
continue # ignore blank and comment lines
words = line.split()
if words[0] in ignored: pass
elif words[0] == ".model": sg["model"] = words[1]
elif words[0] == ".inputs": sg["inputs"] = words[1:]
elif words[0] == ".outputs": sg["outputs"] = words[1:]
elif words[0] == ".marking": sg["initial_state"] = words[1][1:-1]
elif words[0][0] == ".": raise Exception("Unsupported statement: %s" % words[0])
else: sg["transitions"].append(words)
return sg
def main():
sg = load_sg("examples/d-element/spec.sg")
print(sg)
if __name__ == '__main__':
main()