forked from ssardina-research/pypddl-translator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pddlparser.py
571 lines (446 loc) · 15.1 KB
/
pddlparser.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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
# This file is part of pypddl-parser.
# pypddl-parser is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# pypddl-parser is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with pypddl-parser. If not, see <http://www.gnu.org/licenses/>.
from ply import lex
from ply import yacc
from term import Term
from literal import Literal
from predicate import Predicate
from action import Action
from domain import Domain
from problem import Problem
tokens = (
'NAME',
'VARIABLE',
'PROBABILITY',
'LPAREN',
'RPAREN',
'HYPHEN',
'EQUALS',
'DEFINE_KEY',
'DOMAIN_KEY',
'REQUIREMENTS_KEY',
'STRIPS_KEY',
'EQUALITY_KEY',
'TYPING_KEY',
'PROBABILISTIC_EFFECTS_KEY',
'TYPES_KEY',
'CONSTANTS_KEY',
'PREDICATES_KEY',
'ACTION_KEY',
'PARAMETERS_KEY',
'PRECONDITION_KEY',
'EFFECT_KEY',
'AND_KEY',
'NOT_KEY',
'ONEOF_KEY',
'PROBABILISTIC_KEY',
'PROBLEM_KEY',
'OBJECTS_KEY',
'INIT_KEY',
'GOAL_KEY',
'WHEN_KEY',
'OR_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',
':probabilistic-effects' : 'PROBABILISTIC_EFFECTS_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',
'oneof' : 'ONEOF_KEY',
'when' : 'WHEN_KEY',
'or' : 'OR_KEY',
'probabilistic' : 'PROBABILISTIC_KEY',
'problem' : 'PROBLEM_KEY',
':domain' : 'DOMAIN_KEY',
':objects' : 'OBJECTS_KEY',
':constants' : 'CONSTANTS_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_PROBABILITY(t):
r'[0-1]\.\d+'
t.value = float(t.value)
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
| domain problem'''
if len(p) == 2:
p[0] = p[1]
elif len(p) == 3:
p[0] = (p[1], p[2])
###################################################3
# Rules for PLANNING DOMAIN
###################################################3
def p_domain(p):
'''domain : LPAREN DEFINE_KEY domain_def require_def types_def constants_def predicates_def action_def_lst RPAREN
| LPAREN DEFINE_KEY domain_def require_def types_def predicates_def action_def_lst RPAREN
| LPAREN DEFINE_KEY domain_def require_def constants_def predicates_def action_def_lst RPAREN
| LPAREN DEFINE_KEY domain_def require_def predicates_def action_def_lst RPAREN'''
if len(p) == 10: # both types and constants are given
p[0] = Domain(p[3], p[4], p[5][1], p[6][1], p[7], p[8]) # both :types and :constants
elif len(p) == 9:
if p[5][0] == "types":
p[0] = Domain(p[3], p[4], p[5][1], {}, p[6], p[7]) # :types but no constants
elif p[5][0] == "constants":
p[0] = Domain(p[3], p[4], {}, p[5][1], p[6], p[7]) # no :types, but :constants
elif len(p) == 8:
p[0] = Domain(p[3], p[4], {}, {}, p[5], p[6]) # no :constants or :types
def p_domain_def(p):
'''domain_def : LPAREN DOMAIN_KEY NAME RPAREN'''
p[0] = p[3]
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
| PROBABILISTIC_EFFECTS_KEY'''
p[0] = str(p[1])
def p_types_def(p):
'''types_def : LPAREN TYPES_KEY typed_names_lst RPAREN'''
p[0] = ("types", p[3])
def p_constants_def(p):
'''constants_def : LPAREN CONSTANTS_KEY typed_names_lst RPAREN'''
p[0] = ("constants", p[3])
# Used for processing :types and :constants
# list of names, possibly typed using hyphen -
def p_typed_names_lst(p):
'''typed_names_lst : names_lst HYPHEN type typed_names_lst
| names_lst HYPHEN type
| names_lst'''
if len(p) == 2:
p[0] = dict({ '' : p[1]})
elif len(p) == 4:
p[0] = dict({p[3] : p[1]})
elif len(p) == 5:
if p[3] in p[4]:
p[4][p[3]] = p[4][p[3]] + p[1]
else:
p[4][p[3]] = p[1]
p[0] = p[4] # p[4] is already a dictionary, add one entry more
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 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] = Action(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 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 effect_body'''
if len(p) == 3:
p[0] = p[2]
def p_effect_body(p):
'''effect_body : LPAREN NAME unlabeled_effect RPAREN
| effect_body LPAREN NAME unlabeled_effect RPAREN
| unlabeled_effect'''
if len(p) == 5:
p[0] = [(p[2],p[3])]
elif len(p) == 6:
p[0] = p[1] + [(p[3],p[4])]
elif len(p) == 2:
p[0] = p[1]
def p_unlabeled_effect(p):
'''unlabeled_effect : LPAREN ONEOF_KEY effect_body RPAREN
| deterministic_effect unlabeled_effect
| deterministic_effect'''
if len(p) == 5:
p[0] = p[3]
elif len(p) == 2:
p[0] = [p[1]]
elif len(p) == 3:
p[0] = [p[1]] + p[2]
def p_deterministic_effect(p):
'''deterministic_effect : LPAREN AND_KEY effects_lst RPAREN
| effects_lst'''
if len(p) == 2:
p[0] = p[1] # effect is just on literal, no AND
elif len(p) == 5:
p[0] = p[3] # effect description has an AND
def p_when_effects(p):
'''when_effects : when_if when_then '''
p[0] = [p[1],p[2]] # it is a list, with the element in [0] being the conditions and the element in [1] being the effects
def p_when_if(p):
'''when_if : simple_effect
| LPAREN AND_KEY simple_effects_lst RPAREN'''
if len(p) == 2:
p[0] = [p[1]]
elif len(p) == 5:
p[0] = p[3]
def p_when_then(p):
'''when_then : simple_effect
| LPAREN AND_KEY simple_effects_lst RPAREN'''
if len(p) == 2:
p[0] = [p[1]]
elif len(p) == 5:
p[0] = p[3]
def p_effects_lst(p):
'''effects_lst : effect effects_lst
| effect'''
if len(p) == 2:
p[0] = [p[1]]
elif len(p) == 3:
p[0] = [p[1]] + p[2]
def p_simple_effects_lst(p):
'''simple_effects_lst : simple_effect simple_effects_lst
| simple_effect'''
if len(p) == 2:
p[0] = [p[1]]
elif len(p) == 3:
p[0] = [p[1]] + p[2]
def p_effect(p):
'''effect : literal
| LPAREN PROBABILISTIC_KEY PROBABILITY literal RPAREN
| LPAREN WHEN_KEY when_effects RPAREN'''
if len(p) == 2:
p[0] = (1.0, p[1])
elif len(p) == 6:
p[0] = (p[3], p[4])
elif len(p) == 5:
p[0] = p[3]
def p_simple_effect(p):
'''simple_effect : literal'''
if len(p) == 2:
p[0] = (1.0, p[1])
def p_literals_lst(p):
'''literals_lst : literal literals_lst
| literal
| or_literal literals_lst
| or_literal'''
if len(p) == 2:
p[0] = [p[1]]
elif len(p) == 3:
p[0] = [p[1]] + p[2]
def p_or_literal(p):
'''or_literal : LPAREN OR_KEY literals_lst RPAREN'''
if len(p) == 5:
p[0] = p[3]
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
| LPAREN NAME constants_lst 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] = [ Term.variable(p[1]) ]
elif len(p) == 3:
p[0] = [ Term.variable(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_variable(p):
'''variable : VARIABLE'''
p[0] = p[1]
###################################################3
# Rules for PLANNING PROBLEM
###################################################3
def p_problem(p):
'''problem : plan_problem'''
p[0] = p[1]
def p_plan_problem(p):
'''plan_problem : LPAREN DEFINE_KEY plan_problem_def domain_def objects_def init_def goal_def RPAREN
| LPAREN DEFINE_KEY plan_problem_def domain_def init_def goal_def RPAREN'''
if len(p) == 9:
p[0] = Problem(p[3], p[4], p[5], p[6], p[7])
elif len(p) == 8:
p[0] = Problem(p[3], p[4], {}, p[5], p[6])
def p_plan_problem_def(p):
'''plan_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
| LPAREN OBJECTS_KEY 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 literals_lst RPAREN RPAREN
| LPAREN GOAL_KEY literal RPAREN
| LPAREN GOAL_KEY LPAREN ONEOF_KEY effect_body RPAREN RPAREN'''
if len(p) == 8:
p[0] = p[5]
elif len(p) == 5:
p[0] = [p[3]]
elif len(p) == 14:
p[0] = (p[6],p[9])
def p_error(p):
print("Error: syntax error when parsing '{}'".format(p))
# build parser
yacc.yacc()
class PDDLParser(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