-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.py
220 lines (173 loc) · 5.42 KB
/
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
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
"""Parse ctl formulas using ply."""
import ply.yacc as yacc
import ply.lex as lex
import ast
from ast import *
from nodes import *
from grammar import *
import logging
class Lexer:
reserved = {
'A' : 'A',
'E' : 'E',
'U' : 'U',
'AG' : 'AG',
'EG' : 'EG',
'AF' : 'AF',
'EF' : 'EF',
'AX' : 'AX',
'EX' : 'EX',
}
tokens = [
'atomic_proposition',
'LPAREN',
'RPAREN',
'AND',
'OR',
'IMPLIES',
'NOT',
'WHITESPACE',
'FALSE',
'TRUE',
] + list(reserved.values())
t_LPAREN = r'\('
t_RPAREN = r'\)'
t_AND = r'&'
t_OR = r'\|'
t_IMPLIES = r'->'
t_NOT = r'!'
t_WHITESPACE = r'\s+'
t_ignore = ' \t'
def __init__(self):
self.lexer = lex.lex(module=self)
self.lexer.begin('INITIAL')
def input(self, data):
self.lexer.input(data)
def token(self):
return self.lexer.token()
def t_FALSE(self, t):
r'false|False|FALSE'
t.value = False
return t
def t_TRUE(self, t):
r'true|True|TRUE'
t.value = True
return t
def t_atomic_proposition(self, t):
r'[a-zA-Z0-9_][a-zA-Z0-9_]*'
t.type = self.reserved.get(t.value, 'atomic_proposition')
return t
def t_error(self, t):
raise Exception('Illegal character %s' % t.value[0])
class Parser(object):
'''
Available node types:
A(), E(), AU(), AG(), EG(), AF(), EF(), AX(), EX(), EU()
Not(), And(), Or(), Implies()
'''
tokens = Lexer.tokens
precendence = (
('left', 'OR'),
('left', 'AND'),
('right', 'NOT'),
('left', 'IMPLIES'),
('left', 'A', 'E'),
('left', 'U'),
('left', 'AX', 'EX', 'AF', 'EF', 'AG', 'EG'),
)
def __init__(self, subset):
self.functions = [getattr(Parser, f)
for f in dir(self) if f.startswith('p_')]
for fattr in grammar[subset]:
ply_fun, production = fattr
for f in self.functions:
if f.__name__ == ply_fun:
f.__doc__ = production
self.parser = yacc.yacc(module=self)
def parse(self, ctl_formula):
self.parsedata = ctl_formula
return self.parser.parse(ctl_formula, lexer=Lexer())
def p_ctl(self, p):
p[0] = Module(p[1])
logging.debug('p_start: %s' % ast.dump(p[0]))
def p_paren_expr(self, p):
p[0] = p[2]
logging.debug('p_ctl_expr: %s' % p[0])
def p_true_expr(self, p):
p[0] = TRUE()
logging.debug('p_ctl_expr: %s' % p[0])
def p_false_expr(self, p):
p[0] = FALSE()
logging.debug('p_ctl_expr: %s' % p[0])
def p_atomic_proposition_expr(self, p):
p[0] = AtomicProposition(p[1])
logging.debug('p_ctl_expr: %s' % p[0])
def p_not_expr(self, p):
p[0] = Not(p[2])
logging.debug('p_ctl_expr: %s' % p[0])
def p_and_expr(self, p):
p[0] = And(p[1], p[3])
logging.debug('p_ctl_expr: %s' % p[0])
def p_or_expr(self, p):
p[0] = Or(p[1], p[3])
logging.debug('p_ctl_expr: %s' % p[0])
def p_implies_expr(self, p):
p[0] = Implies(p[1], p[3])
logging.debug('p_ctl_expr: %s' % p[0])
def p_A_expr(self, p):
p[0] = A(p[2])
logging.debug('p_ctl_expr: %s' % p[0])
def p_E_expr(self, p):
p[0] = E(p[2])
logging.debug('p_ctl_expr: %s' % p[0])
def p_AU_expr(self, p):
p[0] = AU(p[2], p[4])
logging.debug('p_ctl_expr: %s' % p[0])
def p_EU_expr(self, p):
p[0] = EU(p[2], p[4])
logging.debug('p_ctl_expr: %s' % p[0])
def p_AG_expr(self, p):
p[0] = AG(p[2])
logging.debug('p_ctl_expr: %s' % p[0])
def p_EG_expr(self, p):
p[0] = EG(p[2])
logging.debug('p_ctl_expr: %s' % p[0])
def p_AF_expr(self, p):
p[0] = AF(p[2])
logging.debug('p_ctl_expr: %s' % p[0])
def p_EF_expr(self, p):
p[0] = EF(p[2])
logging.debug('p_ctl_expr: %s' % p[0])
def p_AX_expr(self, p):
p[0] = AX(p[2])
logging.debug('p_ctl_expr: %s' % p[0])
def p_EX_expr(self, p):
p[0] = EX(p[2])
logging.debug('p_ctl_expr: %s' % p[0])
def p_error(self, p):
# use logging.error and give a more detailed error message
# e.g. "Syntax error at '%s'" % p.value and color the error
# in the formula
dummy = 'Syntax error in formula '
logging.error('Syntax error in formula: %s' % self.parsedata)
for char in self.parsedata:
if char == p.value:
logging.error(
' ' * (len(dummy) + self.parsedata.index(char) - 1) + '\033[1;31m^\033[0m')
break
# Logger configuration
debug = False
if debug:
logging.basicConfig(level=logging.DEBUG,
format='%(levelname)s: %(message)s')
else:
logging.basicConfig(level=logging.INFO,
format='%(levelname)s: %(message)s')
# set error and warning colors
logging.addLevelName(logging.ERROR, "\033[1;31m%s\033[1;0m" % logging.getLevelName(logging.ERROR))
logging.addLevelName(logging.WARNING, "\033[1;33m%s\033[1;0m" % logging.getLevelName(logging.WARNING))
logging.addLevelName(logging.DEBUG, "\033[1;34m%s\033[1;0m" % logging.getLevelName(logging.DEBUG))
# set up parser
def parse(ctl_formula, subset='ctl'):
parser = Parser(subset)
return parser.parse(ctl_formula)