-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspec.py
56 lines (47 loc) · 1.15 KB
/
spec.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
#!/usr/bin/env python3
import sys
from parser import ltl_parser
def prefix(f):
'''
Convert an infix formula to prefix form
Example:
infix: ['always', ['p1', 'implies', ['eventually', 'p2']]]
prefix: ['always', ['implies', 'p1', ['eventually', 'p2']]]
'''
if isinstance(f, list):
if len(f) == 2:
f[0], f[1] = prefix(f[0]), prefix(f[1])
return f
else:
f[0], f[1], f[2] = prefix(f[1]), prefix(f[0]), prefix(f[2])
return f
else:
return f
def pprefix(f):
'''
Convert prefix formula from list form to human readable string format
Example:
prefix formula: ['always', ['p1', 'implies', ['eventually', 'p2']]]
human readable: always(implies(p1, eventually p2))
'''
if isinstance(f, list):
if len(f) == 2:
if isinstance(f[1], str):
return pprefix(f[0]) + ' ' + pprefix(f[1])
else:
return pprefix(f[0]) + '(' + pprefix(f[1]) + ')'
else:
return pprefix(f[0]) + '(' + pprefix(f[1]) + ', ' + pprefix(f[2]) + ')'
else:
return f
def spec(file):
try:
f = open(file, "r")
formula = f.read()
f.close()
infix = ltl_parser(formula)
#print(infix)
return prefix(infix)
except Exception as e:
print(e)
sys.exit(1)