def test_parse_logic_expression(self): for string in [ r"(P)(Q)", r"John", r"(John)", r"Man(x)", r"!Man(x)", r"(Man(x) && Tall(x) && Walks(x))", r"(\x.Man(x))", r"(\x.Man(x))(John)", r"\x.\y.Sees(x,y)", r"(\x.\y.Sees(x,y))(a,b)", r"P(x) && P(y)", r"P(x) && Q(y) && R(z)", r"Q(x) || Q(y)", r"\P.\Q.P(x) && Q(x)", r"\P.\Q.(P(x) && Q(x))", r"(\x.\y.Likes(x,y))(John)(Mary)", r"(\x.\y.Likes(x,y))(John, Mary)", r"(\P.\Q.(P(x) && Q(x)))(\x.Dog(x))(\x.Bark(x))" ]: try: x = logic.parse_logic_expression(string) y = logic.parse_logic_expression(str(x)) except: raise AssertionError, "Failed to parse '{0}'".format(string) self.assertEquals(x, y) self.assertEquals(str(x), str(y))
def parse(starting_rule, text): text_with_indexes = enumerate([ None ] + text.lower().split()) table = [ Column(i, token) for i, token in text_with_indexes ] table[0].add(State( GAMMA_RULE, Production(logic.parse_logic_expression("S"), (starting_rule, "S")), [], 0, table[0])) for i, column in enumerate(table): for state in column: if state.is_completed(): complete(column, state) else: term = state.get_next_term() if isinstance(term, Rule): predict(column, term) elif i + 1 < len(table): scan(table[i + 1], state, term) # XXX(sandello): You can uncomment this line to see full dump of # the chart table. # # column.dump(only_completed = False) # Find Gamma rule in the last table column or fail otherwise. result = [] for state in table[-1]: if state.name == GAMMA_RULE and state.is_completed(): result.extend( (state.get_semantics(), tree) for tree in build_trees(state, table)) return result
def get_term_and_semantics(n, part): if part.find(":") > 0: part, semantics = part.split(":", 1) if len(semantics) == 0 or semantics[0] not in [":", "="]: raise RuntimeError, "Malformed line #{0}: Invalid semantic expression for term '{1}'".format( n + 1, part ) if semantics[0] == ":": try: semantics = logic.parse_logic_expression(semantics[1:]) except: raise RuntimeError, "Malformed line #{0}: Unable to parse semantic expression for term '{1}'".format( n + 1, part ) elif semantics[0] == "=": semantics = semantics[1:] else: semantics = logic_ast_nodes.Empty() if re.match(RE_TERMINAL, part): return part, semantics if re.match(RE_NON_TERMINAL, part): if part not in non_terminals: non_terminals[part] = Rule(part) return non_terminals[part], semantics raise RuntimeError, "(unreachable)"
def test_replace_variable(self): def test(source, variable, replacement, result): x = logic.parse_logic_expression(source) y = logic.parse_logic_expression(replacement) z = str(result) self.assertEquals(z, str(x.replace_variable(variable, y))) self.assertEquals(r"Q(x)", str(logic.parse_logic_expression(r"P(x)").replace_variable("P", nodes.Variable("Q")))) test(r"P(x,y,z)", "x", "y", r"P(y,y,z)") test(r"(\x.P(x,y))(P(x,y))", "x", "z", r"(\x.P(x,y))(P(z,y))") test(r"(\y.(\x.P(x,y,z))(x))", "x", "z", r"(\y.(\x.P(x,y,z))(z))") test(r"(\y.(\x.P(x,y,z))(x))", "z", "x", r"(\y.(\x'.P(x',y,x))(x))")
def test(source, result): x = logic.parse_logic_expression(source) y = logic.parse_logic_expression(result) self.assertEquals(str(y), str(x.simplify()))
def test(source, bindings, result): x = logic.parse_logic_expression(source) y = dict(map(lambda x: (x[0], logic.parse_logic_expression(x[1])), bindings.items())) z = str(result) self.assertEquals(z, str(x.replace_with_bindings(y)))
def test(source, variable, replacement, result): x = logic.parse_logic_expression(source) y = logic.parse_logic_expression(replacement) z = str(result) self.assertEquals(z, str(x.replace_variable(variable, y)))
def test(source, variables): x = logic.parse_logic_expression(source) y = list(variables) self.assertEqual(set(y), x.free_variables())