diff --git a/fond4ltlf/parser/__init__.py b/fond4ltlf/parser/__init__.py index 417b166..16a58b7 100644 --- a/fond4ltlf/parser/__init__.py +++ b/fond4ltlf/parser/__init__.py @@ -17,6 +17,7 @@ # along with FOND4LTLf. If not, see . # """This module contains the implementation of the parsers for the PDDL formalism.""" + import inspect import os diff --git a/fond4ltlf/parser/lexer.py b/fond4ltlf/parser/lexer.py index 4941601..41b7ae2 100644 --- a/fond4ltlf/parser/lexer.py +++ b/fond4ltlf/parser/lexer.py @@ -20,7 +20,6 @@ class PDDLLexer(object): - reserved = { "define": "DEFINE_KEY", "domain": "DOMAIN_KEY", @@ -53,13 +52,11 @@ class PDDLLexer(object): } # List of token names. This is always required - tokens = ("NAME", "VARIABLE", "LPAREN", "RPAREN", "HYPHEN", "EQUALS") + tuple( - reserved.values() - ) + tokens = ("NAME", "VARIABLE", "LPAR", "RPAR", "HYPHEN", "EQUALS") + tuple(reserved.values()) # Regular expression rules for simple tokens - t_LPAREN = r"\(" - t_RPAREN = r"\)" + t_LPAR = r"\(" + t_RPAR = r"\)" t_HYPHEN = r"\-" t_EQUALS = r"=" @@ -113,4 +110,4 @@ def test(self, data): # Build the lexer and try it out m = PDDLLexer() m.build() # Build the lexer - m.test("(and (and) (oneof (ciao) (come) ))") # Test it + m.test("(and (and) (oneof (hello) (world) ))") # Test it diff --git a/fond4ltlf/parser/parser.py b/fond4ltlf/parser/parser.py index 929a6a2..6f35578 100644 --- a/fond4ltlf/parser/parser.py +++ b/fond4ltlf/parser/parser.py @@ -50,15 +50,15 @@ def __call__(self, s, **kwargs): def p_pddl(self, p): """pddl : domain - | problem""" + | problem""" p[0] = p[1] def p_domain(self, 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 predicates_def action_def_lst RPAREN - | LPAREN DEFINE_KEY domain_def predicates_def action_def_lst RPAREN - | LPAREN DEFINE_KEY domain_def action_def_lst RPAREN""" + """domain : LPAR DEFINE_KEY domain_def require_def types_def constants_def predicates_def action_def_lst RPAR + | LPAR DEFINE_KEY domain_def require_def types_def predicates_def action_def_lst RPAR + | LPAR DEFINE_KEY domain_def require_def predicates_def action_def_lst RPAR + | LPAR DEFINE_KEY domain_def predicates_def action_def_lst RPAR + | LPAR DEFINE_KEY domain_def action_def_lst RPAR""" if len(p) == 10: p[0] = Domain(p[3], p[4], p[5], p[6], p[7], p[8]) elif len(p) == 9: @@ -72,36 +72,36 @@ def p_domain(self, p): p[0] = Domain(p[3], [], [], [], [], p[4]) def p_problem(self, p): - """problem : LPAREN DEFINE_KEY problem_def domain_pdef objects_def init_def goal_def RPAREN""" + """problem : LPAR DEFINE_KEY problem_def domain_pdef objects_def init_def goal_def RPAR""" p[0] = Problem(p[3], p[4], p[5], p[6], p[7]) def p_domain_pdef(self, p): - """domain_pdef : LPAREN DOMAIN_PKEY NAME RPAREN""" + """domain_pdef : LPAR DOMAIN_PKEY NAME RPAR""" p[0] = p[3] def p_domain_def(self, p): - """domain_def : LPAREN DOMAIN_KEY NAME RPAREN""" + """domain_def : LPAR DOMAIN_KEY NAME RPAR""" p[0] = p[3] def p_problem_def(self, p): - """problem_def : LPAREN PROBLEM_KEY NAME RPAREN""" + """problem_def : LPAR PROBLEM_KEY NAME RPAR""" p[0] = p[3] def p_objects_def(self, p): - """objects_def : LPAREN OBJECTS_KEY typed_constants_lst RPAREN""" + """objects_def : LPAR OBJECTS_KEY typed_constants_lst RPAR""" p[0] = p[3] def p_init_def(self, p): - """init_def : LPAREN INIT_KEY LPAREN AND_KEY ground_predicates_lst RPAREN RPAREN - | LPAREN INIT_KEY ground_predicates_lst RPAREN""" + """init_def : LPAR INIT_KEY LPAR AND_KEY ground_predicates_lst RPAR RPAR + | LPAR INIT_KEY ground_predicates_lst RPAR""" if len(p) == 5: p[0] = p[3] elif len(p) == 8: p[0] = p[5] def p_goal_def(self, p): - """goal_def : LPAREN GOAL_KEY LPAREN AND_KEY ground_predicates_lst RPAREN RPAREN - | LPAREN GOAL_KEY ground_predicate RPAREN""" + """goal_def : LPAR GOAL_KEY LPAR AND_KEY ground_predicates_lst RPAR RPAR + | LPAR GOAL_KEY ground_predicate RPAR""" if len(p) == 8: p[0] = FormulaAnd(p[5]) else: @@ -109,12 +109,12 @@ def p_goal_def(self, p): p[0] = p[3] def p_require_def(self, p): - """require_def : LPAREN REQUIREMENTS_KEY require_key_lst RPAREN""" + """require_def : LPAR REQUIREMENTS_KEY require_key_lst RPAR""" p[0] = p[3] def p_require_key_lst(self, p): """require_key_lst : require_key require_key_lst - | require_key""" + | require_key""" if len(p) == 2: p[0] = [p[1]] else: @@ -122,19 +122,19 @@ def p_require_key_lst(self, p): def p_require_key(self, p): """require_key : STRIPS_KEY - | EQUALITY_KEY - | TYPING_KEY - | ADL_KEY - | ND_KEY""" + | EQUALITY_KEY + | TYPING_KEY + | ADL_KEY + | ND_KEY""" p[0] = str(p[1]) def p_types_def(self, p): - """types_def : LPAREN TYPES_KEY typed_lst_name RPAREN""" + """types_def : LPAR TYPES_KEY typed_lst_name RPAR""" p[0] = p[3] def p_constants_def(self, p): - """constants_def : LPAREN CONSTANTS_KEY typed_constants_lst RPAREN - | LPAREN CONSTANTS_KEY names_lst RPAREN""" + """constants_def : LPAR CONSTANTS_KEY typed_constants_lst RPAR + | LPAR CONSTANTS_KEY names_lst RPAR""" p[0] = p[3] def p_typed_lst_name(self, p): @@ -146,7 +146,7 @@ def p_typed_lst_name(self, p): def p_names_lst(self, p): """names_lst : NAME names_lst - | NAME""" + | NAME""" if len(p) == 1: p[0] = [] elif len(p) == 2: @@ -155,20 +155,20 @@ def p_names_lst(self, p): p[0] = [p[1]] + p[2] def p_predicates_def(self, p): - """predicates_def : LPAREN PREDICATES_KEY predicate_def_lst RPAREN""" + """predicates_def : LPAR PREDICATES_KEY predicate_def_lst RPAR""" p[0] = p[3] def p_predicate_def_lst(self, p): """predicate_def_lst : predicate_def predicate_def_lst - | predicate_def""" + | predicate_def""" if len(p) == 2: p[0] = [p[1]] else: p[0] = [p[1]] + p[2] def p_predicate_def(self, p): - """predicate_def : LPAREN NAME typed_variables_lst RPAREN - | LPAREN NAME RPAREN""" + """predicate_def : LPAR NAME typed_variables_lst RPAR + | LPAR NAME RPAR""" if len(p) == 4: p[0] = Predicate(p[2]) elif len(p) == 5: @@ -176,15 +176,15 @@ def p_predicate_def(self, p): def p_ground_predicates_lst(self, p): """ground_predicates_lst : ground_predicate ground_predicates_lst - | ground_predicate""" + | ground_predicate""" if len(p) == 2: p[0] = [p[1]] elif len(p) == 3: p[0] = [p[1]] + p[2] def p_ground_predicate(self, p): - """ground_predicate : LPAREN NAME RPAREN - | LPAREN NAME constants_lst RPAREN""" + """ground_predicate : LPAR NAME RPAR + | LPAR NAME constants_lst RPAR""" if len(p) == 4: p[0] = Predicate(p[2]) elif len(p) == 5: @@ -192,7 +192,7 @@ def p_ground_predicate(self, p): def p_constants_lst(self, p): """constants_lst : constant constants_lst - | constant""" + | constant""" if len(p) == 2: p[0] = [p[1]] elif len(p) == 3: @@ -200,7 +200,7 @@ def p_constants_lst(self, p): def p_typed_constants_lst(self, p): """typed_constants_lst : constants_lst HYPHEN type typed_constants_lst - | constants_lst HYPHEN type""" + | constants_lst HYPHEN type""" if len(p) == 4: p[0] = [Term.constant(value, p[3]) for value in p[1]] elif len(p) == 5: @@ -208,8 +208,8 @@ def p_typed_constants_lst(self, p): def p_action_def_lst(self, p): """action_def_lst : action_def action_def_lst - | action_def - | """ + | action_def + |""" if len(p) == 1: p[0] = [] elif len(p) == 2: @@ -218,36 +218,36 @@ def p_action_def_lst(self, p): p[0] = [p[1]] + p[2] def p_action_def(self, p): - """action_def : LPAREN ACTION_KEY NAME parameters_def precond_def effects_def RPAREN""" + """action_def : LPAR ACTION_KEY NAME parameters_def precond_def effects_def RPAR""" p[0] = Action(p[3], p[4], p[5], p[6]) def p_parameters_def(self, p): - """parameters_def : PARAMETERS_KEY LPAREN typed_variables_lst RPAREN - | PARAMETERS_KEY LPAREN RPAREN""" + """parameters_def : PARAMETERS_KEY LPAR typed_variables_lst RPAR + | PARAMETERS_KEY LPAR RPAR""" if len(p) == 4: p[0] = [] elif len(p) == 5: p[0] = p[3] def p_precond_def(self, p): - """precond_def : PRECONDITION_KEY LPAREN formula RPAREN""" + """precond_def : PRECONDITION_KEY LPAR formula RPAR""" p[0] = p[3] def p_formula(self, p): """formula : literal - | AND_KEY formula_lst - | OR_KEY formula_lst - | NOT_KEY formula - | IMPLY_KEY formula formula - | EXISTS_KEY LPAREN typed_variables_lst RPAREN formula - | FORALL_KEY LPAREN typed_variables_lst RPAREN formula - | LPAREN AND_KEY formula_lst RPAREN - | LPAREN OR_KEY formula_lst RPAREN - | LPAREN NOT_KEY formula RPAREN - | LPAREN IMPLY_KEY formula formula RPAREN - | LPAREN literal RPAREN - | LPAREN EXISTS_KEY LPAREN typed_variables_lst RPAREN formula RPAREN - | LPAREN FORALL_KEY LPAREN typed_variables_lst RPAREN formula RPAREN""" + | AND_KEY formula_lst + | OR_KEY formula_lst + | NOT_KEY formula + | IMPLY_KEY formula formula + | EXISTS_KEY LPAR typed_variables_lst RPAR formula + | FORALL_KEY LPAR typed_variables_lst RPAR formula + | LPAR AND_KEY formula_lst RPAR + | LPAR OR_KEY formula_lst RPAR + | LPAR NOT_KEY formula RPAR + | LPAR IMPLY_KEY formula formula RPAR + | LPAR literal RPAR + | LPAR EXISTS_KEY LPAR typed_variables_lst RPAR formula RPAR + | LPAR FORALL_KEY LPAR typed_variables_lst RPAR formula RPAR""" if len(p) == 2: p[0] = p[1] elif len(p) == 3: @@ -284,14 +284,14 @@ def p_formula(self, p): def p_formula_lst(self, p): """formula_lst : formula formula_lst - | formula""" + | formula""" if len(p) == 2: p[0] = [p[1]] elif len(p) == 3: p[0] = [p[1]] + p[2] def p_effects_def(self, p): - """effects_def : EFFECT_KEY LPAREN one_eff_formula RPAREN""" + """effects_def : EFFECT_KEY LPAR one_eff_formula RPAR""" p[0] = p[3] # def p_eff_formula(self, p): @@ -308,14 +308,14 @@ def p_effects_def(self, p): def p_one_eff_formula(self, p): """one_eff_formula : literal - | AND_KEY - | AND_KEY one_eff_formula_lst - | ONEOF_KEY atomic_eff_lst - | WHEN_KEY formula atomic_eff - | LPAREN ONEOF_KEY atomic_eff_lst RPAREN - | LPAREN WHEN_KEY formula atomic_eff RPAREN - | LPAREN FORALL_KEY LPAREN typed_variables_lst RPAREN atomic_eff RPAREN - | LPAREN FORALL_KEY LPAREN typed_variables_lst RPAREN LPAREN WHEN_KEY formula atomic_eff RPAREN RPAREN""" + | AND_KEY + | AND_KEY one_eff_formula_lst + | ONEOF_KEY atomic_eff_lst + | WHEN_KEY formula atomic_eff + | LPAR ONEOF_KEY atomic_eff_lst RPAR + | LPAR WHEN_KEY formula atomic_eff RPAR + | LPAR FORALL_KEY LPAR typed_variables_lst RPAR atomic_eff RPAR + | LPAR FORALL_KEY LPAR typed_variables_lst RPAR LPAR WHEN_KEY formula atomic_eff RPAR RPAR""" if len(p) == 2: if p[1] == "and": p[0] = FormulaAnd() @@ -341,7 +341,7 @@ def p_one_eff_formula(self, p): def p_one_eff_formula_lst(self, p): """one_eff_formula_lst : one_eff_formula one_eff_formula_lst - | one_eff_formula""" + | one_eff_formula""" if len(p) == 2: p[0] = [p[1]] elif len(p) == 3: @@ -349,7 +349,7 @@ def p_one_eff_formula_lst(self, p): def p_atomic_eff_lst(self, p): """atomic_eff_lst : atomic_eff atomic_eff_lst - | atomic_eff""" + | atomic_eff""" if len(p) == 2: p[0] = [p[1]] elif len(p) == 3: @@ -357,10 +357,10 @@ def p_atomic_eff_lst(self, p): def p_atomic_eff(self, p): """atomic_eff : literal - | AND_KEY literal_lst - | LPAREN AND_KEY RPAREN - | LPAREN AND_KEY literal_lst RPAREN - | LPAREN WHEN_KEY formula atomic_eff RPAREN""" + | AND_KEY literal_lst + | LPAR AND_KEY RPAR + | LPAR AND_KEY literal_lst RPAR + | LPAR WHEN_KEY formula atomic_eff RPAR""" if len(p) == 2: p[0] = p[1] elif len(p) == 3: @@ -380,27 +380,27 @@ def p_atomic_eff(self, p): def p_literal_lst(self, p): """literal_lst : literal literal_lst - | literal""" + | literal""" if len(p) == 2: p[0] = [p[1]] elif len(p) == 3: p[0] = [p[1]] + p[2] def p_literal(self, p): - """literal : LPAREN NOT_KEY predicate RPAREN - | predicate""" + """literal : LPAR NOT_KEY predicate RPAR + | predicate""" if len(p) == 2: p[0] = Literal.positive(p[1]) elif len(p) == 5: p[0] = Literal.negative(p[3]) def p_predicate(self, p): - """predicate : LPAREN NAME variables_lst RPAREN - | LPAREN EQUALS VARIABLE VARIABLE RPAREN - | LPAREN NAME RPAREN - | NAME variables_lst - | EQUALS VARIABLE VARIABLE - | NAME""" + """predicate : LPAR NAME variables_lst RPAR + | LPAR EQUALS VARIABLE VARIABLE RPAR + | LPAR NAME RPAR + | NAME variables_lst + | EQUALS VARIABLE VARIABLE + | NAME""" if len(p) == 2: p[0] = Predicate(p[1]) elif len(p) == 3: @@ -417,7 +417,7 @@ def p_predicate(self, p): def p_typed_variables_lst(self, p): """typed_variables_lst : variables_lst HYPHEN type typed_variables_lst - | variables_lst HYPHEN type""" + | variables_lst HYPHEN type""" if len(p) == 4: p[0] = [Term.variable(name, p[3]) for name in p[1]] else: @@ -425,7 +425,7 @@ def p_typed_variables_lst(self, p): def p_variables_lst(self, p): """variables_lst : VARIABLE variables_lst - | VARIABLE""" + | VARIABLE""" if len(p) == 2: p[0] = [p[1]] elif len(p) == 3: @@ -453,8 +453,4 @@ def p_error(self, p): domain = f.read() result = par(domain) - # states = {'1','2','3','4'} - # alpha = ['a','b','c'] - # transition = 'trans\n:parameters ()\n:precondition (not turnDomain)\n:effect (oneof (when (and (q2) (not a)) (and (q2) (turnDomain))) (when (and (q2) (a)) (turnDomain)))\n' - # result1 = result.get_new_domain(alpha, states, transition) print(result) diff --git a/pyproject.toml b/pyproject.toml index 3a0bded..fb8eb8a 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -14,7 +14,6 @@ keywords = [ "linear temporal logics", "pure-past linear temporal logic", "fond planning", - "", ] classifiers = [ @@ -87,7 +86,6 @@ exclude = [ "buck-out", "build", "dist", - "fond4ltlf/parser/*.py", "node_modules", "site-packages", "venv", diff --git a/tests/data/pddl-domains/robot-coffee/domain-fond.pddl b/tests/data/pddl-domains/robot-coffee/domain-fond.pddl index f4b1a38..c813c41 100644 --- a/tests/data/pddl-domains/robot-coffee/domain-fond.pddl +++ b/tests/data/pddl-domains/robot-coffee/domain-fond.pddl @@ -3,47 +3,45 @@ (:types office - room kitchen - room) -(:predicates (robot_at ?x - room) - (coffeeat ?x - room) - (connected ?x - room ?y - room) - (has_coffee) - ) + (:predicates (robot_at ?x - room) + (coffeeat ?x - room) + (connected ?x - room ?y - room) + (has_coffee) + ) -(:action move_to - :parameters - (?x - room - ?y - room) - :precondition - (and - (robot_at ?x) (connected ?x ?y)) - :effect - (and - (robot_at ?y) (not (robot_at ?x))) -) - -(:action prepare_coffee - :parameters - (?x - kitchen) - :precondition - (and - (robot_at ?x) (not (has_coffee))) - :effect - (has_coffee) -) - -(:action put_coffee - :parameters - (?x - office) - :precondition - (and - (robot_at ?x) (has_coffee)) - :effect - (and (not (has_coffee)) - (oneof - (and (coffeeat ?x)) - (and) - )) -) + (:action move_to + :parameters + (?x - room + ?y - room) + :precondition + (and + (robot_at ?x) (connected ?x ?y)) + :effect + (and + (robot_at ?y) (not (robot_at ?x))) + ) + (:action prepare_coffee + :parameters + (?x - kitchen) + :precondition + (and + (robot_at ?x) (not (has_coffee))) + :effect + (has_coffee) + ) + (:action put_coffee + :parameters + (?x - office) + :precondition + (and + (robot_at ?x) (has_coffee)) + :effect + (and (not (has_coffee)) + (oneof + (and (coffeeat ?x)) + (and) + )) + ) )