-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.py
374 lines (281 loc) · 8.51 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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
from ply import lex
from ply import yacc
from term import Term
from predicate import Predicate
from literal import Literal
from action import Operator
from domain import Domain
from problem import Problem
tokens = (
'NAME',
'VARIABLE',
'LPAREN',
'RPAREN',
'HYPHEN',
'EQUALS',
'DEFINE_KEY',
'DOMAIN_KEY',
'REQUIREMENTS_KEY',
'STRIPS_KEY',
'EQUALITY_KEY',
'TYPING_KEY',
'TYPES_KEY',
'PREDICATES_KEY',
'ACTION_KEY',
'PARAMETERS_KEY',
'PRECONDITION_KEY',
'EFFECT_KEY',
'AND_KEY',
'NOT_KEY',
'PROBLEM_KEY',
'OBJECTS_KEY',
'INIT_KEY',
'GOAL_KEY'
)
t_LPAREN = r'\('
t_RPAREN = r'\)'
t_HYPHEN = r'\-'
t_EQUALS = r'='
t_ignore = ' \t'
reserved = {
'define' : 'DEFINE_KEY',
'domain' : 'DOMAIN_KEY',
':requirements' : 'REQUIREMENTS_KEY',
':strips' : 'STRIPS_KEY',
':equality' : 'EQUALITY_KEY',
':typing' : 'TYPING_KEY',
':types' : 'TYPES_KEY',
':predicates' : 'PREDICATES_KEY',
':action' : 'ACTION_KEY',
':parameters' : 'PARAMETERS_KEY',
':precondition' : 'PRECONDITION_KEY',
':effect' : 'EFFECT_KEY',
'and' : 'AND_KEY',
'not' : 'NOT_KEY',
'problem' : 'PROBLEM_KEY',
':domain' : 'DOMAIN_KEY',
':objects' : 'OBJECTS_KEY',
':init' : 'INIT_KEY',
':goal' : 'GOAL_KEY'
}
def t_KEYWORD(t):
r':?[a-zA-z_][a-zA-Z_0-9\-]*'
t.type = reserved.get(t.value, 'NAME')
return t
def t_NAME(t):
r'[a-zA-z_][a-zA-Z_0-9\-]*'
return t
def t_VARIABLE(t):
r'\?[a-zA-z_][a-zA-Z_0-9\-]*'
return t
def t_newline(t):
r'\n+'
t.lineno += len(t.value)
def t_error(t):
print("Error: illegal character '{0}'".format(t.value[0]))
t.lexer.skip(1)
# build the lexer
lex.lex()
def p_pddl(p):
'''pddl : domain
| problem'''
p[0] = p[1]
def p_domain(p):
'''domain : LPAREN DEFINE_KEY domain_def require_def types_def predicates_def action_def_lst RPAREN'''
p[0] = Domain(p[3], p[4], p[5], p[6], p[7])
def p_problem(p):
'''problem : LPAREN DEFINE_KEY problem_def domain_def objects_def init_def goal_def RPAREN'''
p[0] = Problem(p[3], p[4], p[5], p[6], p[7])
def p_domain_def(p):
'''domain_def : LPAREN DOMAIN_KEY NAME RPAREN'''
p[0] = p[3]
def p_problem_def(p):
'''problem_def : LPAREN PROBLEM_KEY NAME RPAREN'''
p[0] = p[3]
def p_objects_def(p):
'''objects_def : LPAREN OBJECTS_KEY typed_constants_lst RPAREN'''
p[0] = p[3]
def p_init_def(p):
'''init_def : LPAREN INIT_KEY LPAREN AND_KEY ground_predicates_lst RPAREN RPAREN
| LPAREN INIT_KEY ground_predicates_lst RPAREN'''
if len(p) == 5:
p[0] = p[3]
elif len(p) == 8:
p[0] = p[5]
def p_goal_def(p):
'''goal_def : LPAREN GOAL_KEY LPAREN AND_KEY ground_predicates_lst RPAREN RPAREN'''
p[0] = p[5]
def p_require_def(p):
'''require_def : LPAREN REQUIREMENTS_KEY require_key_lst RPAREN'''
p[0] = p[3]
def p_require_key_lst(p):
'''require_key_lst : require_key require_key_lst
| require_key'''
if len(p) == 2:
p[0] = [p[1]]
elif len(p) == 3:
p[0] = [p[1]] + p[2]
def p_require_key(p):
'''require_key : STRIPS_KEY
| EQUALITY_KEY
| TYPING_KEY'''
p[0] = str(p[1])
def p_types_def(p):
'''types_def : LPAREN TYPES_KEY names_lst RPAREN'''
p[0] = p[3]
def p_predicates_def(p):
'''predicates_def : LPAREN PREDICATES_KEY predicate_def_lst RPAREN'''
p[0] = p[3]
def p_predicate_def_lst(p):
'''predicate_def_lst : predicate_def predicate_def_lst
| predicate_def'''
if len(p) == 2:
p[0] = [p[1]]
elif len(p) == 3:
p[0] = [p[1]] + p[2]
def p_predicate_def(p):
'''predicate_def : LPAREN NAME typed_variables_lst RPAREN
| LPAREN NAME RPAREN'''
if len(p) == 4:
p[0] = Predicate(p[2])
elif len(p) == 5:
p[0] = Predicate(p[2], p[3])
def p_action_def_lst(p):
'''action_def_lst : action_def action_def_lst
| action_def'''
if len(p) == 2:
p[0] = [p[1]]
elif len(p) == 3:
p[0] = [p[1]] + p[2]
def p_action_def(p):
'''action_def : LPAREN ACTION_KEY NAME parameters_def action_def_body RPAREN'''
p[0] = Operator(p[3], p[4], p[5][0], p[5][1])
def p_parameters_def(p):
'''parameters_def : PARAMETERS_KEY LPAREN typed_variables_lst RPAREN
| PARAMETERS_KEY LPAREN RPAREN'''
if len(p) == 4:
p[0] = []
elif len(p) == 5:
p[0] = p[3]
def p_action_def_body(p):
'''action_def_body : precond_def effects_def'''
p[0] = (p[1], p[2])
def p_precond_def(p):
'''precond_def : PRECONDITION_KEY LPAREN AND_KEY literals_lst RPAREN
| PRECONDITION_KEY literal'''
if len(p) == 3:
p[0] = [p[2]]
elif len(p) == 6:
p[0] = p[4]
def p_effects_def(p):
'''effects_def : EFFECT_KEY LPAREN AND_KEY literals_lst RPAREN
| EFFECT_KEY literal'''
if len(p) == 3:
p[0] = [p[2]]
elif len(p) == 6:
p[0] = p[4]
def p_literals_lst(p):
'''literals_lst : literal literals_lst
| literal'''
if len(p) == 2:
p[0] = [p[1]]
elif len(p) == 3:
p[0] = [p[1]] + p[2]
def p_literal(p):
'''literal : LPAREN NOT_KEY predicate RPAREN
| predicate'''
if len(p) == 2:
p[0] = Literal.positive(p[1])
elif len(p) == 5:
p[0] = Literal.negative(p[3])
def p_ground_predicates_lst(p):
'''ground_predicates_lst : ground_predicate ground_predicates_lst
| ground_predicate'''
if len(p) == 2:
p[0] = [p[1]]
elif len(p) == 3:
p[0] = [p[1]] + p[2]
def p_predicate(p):
'''predicate : LPAREN NAME variables_lst RPAREN
| LPAREN EQUALS VARIABLE VARIABLE RPAREN
| LPAREN NAME RPAREN'''
if len(p) == 4:
p[0] = Predicate(p[2])
elif len(p) == 5:
p[0] = Predicate(p[2], p[3])
elif len(p) == 6:
p[0] = Predicate('=', [p[3], p[4]])
def p_ground_predicate(p):
'''ground_predicate : LPAREN NAME constants_lst RPAREN
| LPAREN NAME RPAREN'''
if len(p) == 4:
p[0] = Predicate(p[2])
elif len(p) == 5:
p[0] = Predicate(p[2], p[3])
def p_typed_constants_lst(p):
'''typed_constants_lst : constants_lst HYPHEN type typed_constants_lst
| constants_lst HYPHEN type'''
if len(p) == 4:
p[0] = [ Term.constant(value, p[3]) for value in p[1] ]
elif len(p) == 5:
p[0] = [ Term.constant(value, p[3]) for value in p[1] ] + p[4]
def p_typed_variables_lst(p):
'''typed_variables_lst : variables_lst HYPHEN type typed_variables_lst
| variables_lst HYPHEN type'''
if len(p) == 4:
p[0] = [ Term.variable(name, p[3]) for name in p[1] ]
elif len(p) == 5:
p[0] = [ Term.variable(name, p[3]) for name in p[1] ] + p[4]
def p_constants_lst(p):
'''constants_lst : constant constants_lst
| constant'''
if len(p) == 2:
p[0] = [ Term.constant(p[1]) ]
elif len(p) == 3:
p[0] = [ Term.constant(p[1]) ] + p[2]
def p_variables_lst(p):
'''variables_lst : VARIABLE variables_lst
| VARIABLE'''
if len(p) == 2:
p[0] = [p[1]]
elif len(p) == 3:
p[0] = [p[1]] + p[2]
def p_names_lst(p):
'''names_lst : NAME names_lst
| NAME'''
if len(p) == 1:
p[0] = []
elif len(p) == 2:
p[0] = [p[1]]
elif len(p) == 3:
p[0] = [p[1]] + p[2]
def p_type(p):
'''type : NAME'''
p[0] = p[1]
def p_constant(p):
'''constant : NAME'''
p[0] = p[1]
def p_error(p):
print("Error: syntax error when parsing '{}'".format(p))
# build parser
yacc.yacc()
class Parser(object):
@classmethod
def parse(cls, filename):
data = cls.__read_input(filename)
return yacc.parse(data)
@classmethod
def __read_input(cls, filename):
with open(filename, 'r', encoding='utf-8') as file:
data = ''
for line in file:
line = line.rstrip().lower()
line = cls.__strip_comments(line)
data += '\n' + line
return data
@classmethod
def __strip_comments(cls, line):
pos = line.find(';')
if pos != -1:
line = line[:pos]
return line