class TestParserExtensibility(TestCase):

    def setUp(self):
        self.ts_parser = TSSmtLibParser()
        self.smt_parser = SmtLibParser()

    def test_wrong(self):
        txt = """
        (declare-fun A () Bool)
        (declare-fun B () Bool)
        (assert (and A B))
        (check-sat)
        (exit)
        """
        with self.assertRaises(UnknownSmtLibCommandError):
            self.ts_parser.get_ts(cStringIO(txt))
        script = self.smt_parser.get_script(cStringIO(txt))
        self.assertIsNotNone(script)

    def test_basic(self):
        txt = """
        (declare-fun A () Bool)
        (declare-fun B () Bool)
        (init (and A B))
        (trans (=> A (next A)))
        (exit)
        """
        with self.assertRaises(UnknownSmtLibCommandError):
            self.smt_parser.get_script(cStringIO(txt))
        ts = self.ts_parser.get_ts(cStringIO(txt))
        self.assertIsNotNone(ts)
Beispiel #2
0
    def __init__(self, env=None, interactive=False):
        SmtLibParser.__init__(self, env, interactive)

        # Add new commands
        #
        # The mapping function takes care of consuming the command
        # name from the input stream, e.g., '(init' . Therefore,
        # _cmd_init will receive the rest of the stream, in our
        # example, '(and A B)) ...'
        self.commands["init"] = self._cmd_init
        self.commands["trans"] = self._cmd_trans

        # Remove unused commands
        #
        # If some commands are not compatible with the extension, they
        # can be removed from the parser. If found, they will cause
        # the raising of the exception UnknownSmtLibCommandError
        del self.commands["check-sat"]
        del self.commands["get-value"]
        # ...

        # Add 'next' function
        #
        # New operators can be added similarly as done for commands.
        # e.g., 'next'. The helper function _operator_adapter,
        # simplifies the writing of such extensions.  In this example,
        # we will rewrite the content of the next without the need of
        # introducing a new pySMT operator. If you are interested in a
        # simple way of handling new operators in pySMT see
        # pysmt.test.test_dwf.
        self.interpreted["next"] = self._operator_adapter(self._next_var)
Beispiel #3
0
 def parse(self, file_id):
     fname = SMTLIB_FILE_PATTERN % file_id
     reset_env()
     parser = SmtLibParser()
     script = parser.get_script_fname(fname)
     self.assertIsNotNone(script)
     return script
Beispiel #4
0
 def test_parse_bvconst_width(self):
     smtlib_input = "(assert (> #x10 #x10))"
     parser = SmtLibParser()
     buffer_ = cStringIO(smtlib_input)
     expr = parser.get_script(buffer_).get_last_formula()
     const = expr.args()[0]
     self.assertEqual(const.bv_width(), 8, const.bv_width())
Beispiel #5
0
    def test_dumped_logic(self):
        # Dumped logic matches the logic in the example
        fs = get_example_formulae()

        for (f_out, _, _, logic) in fs:
            buf_out = cStringIO()
            script_out = smtlibscript_from_formula(f_out)
            script_out.serialize(outstream=buf_out)

            buf_in = cStringIO(buf_out.getvalue())
            parser = SmtLibParser()
            script_in = parser.get_script(buf_in)
            for cmd in script_in:
                if cmd.name == "set-logic":
                    logic_in = cmd.args[0]
                    if logic == logics.QF_BOOL:
                        self.assertEqual(logic_in, logics.QF_UF)
                    elif logic == logics.BOOL:
                        self.assertEqual(logic_in, logics.LRA)
                    else:
                        self.assertEqual(logic_in, logic, script_in)
                    break
            else: # Loops exited normally
                print("-"*40)
                print(script_in)
Beispiel #6
0
 def test_define_funs_arg_and_fun(self):
     smtlib_script = "\n".join(['(define-fun f ((n Int)) Int n)', '(declare-fun n () Real)'])
     stream = cStringIO(smtlib_script)
     parser = SmtLibParser()
     _ = parser.get_script(stream)
     # No exceptions are thrown
     self.assertTrue(True)
Beispiel #7
0
 def test_define_funs_same_args(self):
     # n is defined once as an Int and once as a Real
     smtlib_script = "\n".join(['(define-fun f ((n Int)) Int n)', '(define-fun f ((n Real)) Real n)'])
     stream = cStringIO(smtlib_script)
     parser = SmtLibParser()
     _ = parser.get_script(stream)
     # No exceptions are thrown
     self.assertTrue(True)
Beispiel #8
0
 def test_parse_declare_const(self):
     smtlib_input = """
     (declare-const s Int)
     (check-sat)"""
     parser = SmtLibParser()
     buffer_ = cStringIO(smtlib_input)
     script = parser.get_script(buffer_)
     self.assertIsNotNone(script)
Beispiel #9
0
 def test_smtlib_define_fun_serialization(self):
     smtlib_input = "(define-fun init ((x Bool)) Bool (and x (and x (and x (and x (and x (and x x)))))))"
     parser = SmtLibParser()
     buffer_ = cStringIO(smtlib_input)
     s = parser.get_script(buffer_)
     for c in s:
         res = c.serialize_to_string(daggify=False)
     self.assertEqual(res.replace('__x0', 'x'), smtlib_input)
Beispiel #10
0
    def smtlib_solver(self, stream):
        smt_parser = SmtLibParser()
        name = self.args.solver
        if name == "auto":
            solver = Solver()
        else:
            solver = Solver(name=name)

        for cmd in smt_parser.get_command_generator(stream):
            r = evaluate_command(cmd, solver)
            self.print_result(cmd, r)
Beispiel #11
0
    def test_complex_annotations_values(self):
        source ="""\
(declare-fun |"v__AT0"| () Bool)
(define-fun .def_1 () Bool (! |"v__AT0"| :next (+ 1     meaningless)))
"""
        buf = StringIO(source)
        parser = SmtLibParser()
        script = parser.get_script(buf)
        ann = script.annotations
        v0 = self.env.formula_manager.get_symbol('"v__AT0"')
        v1_str = next(iter(ann[v0]["next"]))
        self.assertEquals(v1_str, "(+ 1     meaningless)")
Beispiel #12
0
    def test_annotations_colon_values(self):
        source ="""\
(declare-fun |"v__AT0"| () Bool)
(define-fun .def_1 () Bool (! |"v__AT0"| :next :this_is_considered_a_value))
"""
        buf = StringIO(source)
        parser = SmtLibParser()
        script = parser.get_script(buf)
        ann = script.annotations
        v0 = self.env.formula_manager.get_symbol('"v__AT0"')
        v1_str = next(iter(ann[v0]["next"]))
        self.assertEquals(v1_str, ":this_is_considered_a_value")
Beispiel #13
0
 def test_parse_exception(self):
     from pysmt.exceptions import PysmtSyntaxError
     smtlib_input = "(declare-const x x x Int)" +\
                    "(check-sat)"
     parser = SmtLibParser()
     buffer_ = cStringIO(smtlib_input)
     try:
         parser.get_script(buffer_)
         self.assertFalse(True)
     except PysmtSyntaxError as ex:
         self.assertEqual(ex.pos_info[0], 0)
         self.assertEqual(ex.pos_info[1], 19)
Beispiel #14
0
    def test_all_parsing(self):
        # Create a small file that tests all commands of smt-lib 2
        parser = SmtLibParser()

        nie = 0
        for cmd in DEMO_SMTSCRIPT:
            try:
                next(parser.get_command_generator(cStringIO(cmd)))
            except NotImplementedError:
                nie += 1
        # There are currently 3 not-implemented commands
        self.assertEquals(nie, 3)
    def __init__(self, env=None, interactive=False):
        SmtLibParser.__init__(self, env, interactive)

        # Add new commands
        self.commands["init"] = self._cmd_init
        self.commands["trans"] = self._cmd_trans

        # Remove unused commands
        del self.commands["check-sat"]
        del self.commands["get-value"]
        # ...

        # Add 'next' function
        self.interpreted["next"] = self._operator_adapter(self._next_var)
Beispiel #16
0
    def test_interpreting_annotations(self):
        source ="""\
(declare-fun |"v__AT0"| () Bool)
(declare-fun |"v__AT1"| () Bool)
(define-fun .def_1 () Bool (! |"v__AT0"| :next |"v__AT1"|))
"""
        buf = StringIO(source)
        parser = SmtLibParser()
        script = parser.get_script(buf)
        ann = script.annotations
        v0 = self.env.formula_manager.get_symbol('"v__AT0"')
        v1_str = next(iter(ann[v0]["next"]))
        self.env.formula_manager.get_symbol(v1_str)
        self.assertEquals(v1_str, '"v__AT1"')
Beispiel #17
0
    def test_string_constant_quote_escaping_parsing(self):
        script = """
        (declare-const x String)
        (assert (= x "a""b"))
        (assert (= x "\"\""))
        (assert (= x "\"\"\"\""))
        """

        p = SmtLibParser()
        buffer = cStringIO(script)
        s = p.get_script(buffer)
        self.assertEqual('a"b', s.commands[1].args[0].arg(1).constant_value())
        self.assertEqual('"', s.commands[2].args[0].arg(1).constant_value())
        self.assertEqual('""', s.commands[3].args[0].arg(1).constant_value())
Beispiel #18
0
    def test_parse_examples_daggified_bv(self):
        fs = get_example_formulae()

        for (f_out, _, _, logic) in fs:
            if logic != logics.QF_BV:
                # See test_parse_examples_daggified
                continue
            buf_out = cStringIO()
            script_out = smtlibscript_from_formula(f_out)
            script_out.serialize(outstream=buf_out, daggify=True)
            buf_in = cStringIO(buf_out.getvalue())
            parser = SmtLibParser()
            script_in = parser.get_script(buf_in)
            f_in = script_in.get_last_formula()
            self.assertValid(Iff(f_in, f_out), f_in.serialize())
Beispiel #19
0
    def __init__(self, args, environment, logic, LOGICS=None, **options):
        Solver.__init__(self,
                        environment,
                        logic=logic,
                        **options)
        self.to = self.environment.typeso
        if LOGICS is not None: self.LOGICS = LOGICS
        self.args = args
        self.declared_vars = set()
        self.declared_sorts = set()
        self.solver = Popen(args, stdout=PIPE, stderr=PIPE, stdin=PIPE,
                            bufsize=-1)
        # Give time to the process to start-up
        time.sleep(0.01)
        self.parser = SmtLibParser(interactive=True)
        if PY2:
            self.solver_stdin = self.solver.stdin
            self.solver_stdout = self.solver.stdout
        else:
            self.solver_stdin = TextIOWrapper(self.solver.stdin)
            self.solver_stdout = TextIOWrapper(self.solver.stdout)

        # Initialize solver
        self.options(self)
        self.set_logic(logic)
Beispiel #20
0
    def __init__(self, args, environment, logic, user_options=None, LOGICS=None):
        Solver.__init__(self, environment, logic=logic, user_options=user_options)

        if LOGICS is not None:
            self.LOGICS = LOGICS
        self.args = args
        self.declared_vars = set()
        self.solver = Popen(args, stdout=PIPE, stderr=PIPE, stdin=PIPE)
        self.parser = SmtLibParser(interactive=True)
        if PY2:
            self.solver_stdin = self.solver.stdin
            self.solver_stdout = self.solver.stdout
        else:
            self.solver_stdin = TextIOWrapper(self.solver.stdin)
            self.solver_stdout = TextIOWrapper(self.solver.stdout)

        self.dbg = False

        # Initialize solver
        self._send_command(SmtLibCommand(smtcmd.SET_OPTION, [":print-success", "false"]))
        self._send_command(SmtLibCommand(smtcmd.SET_OPTION, [":produce-models", "true"]))

        if self.options is not None:
            for o, v in iteritems(self.options):
                self._send_command(SmtLibCommand(smtcmd.SET_OPTION, [o, v]))
        self._send_command(SmtLibCommand(smtcmd.SET_LOGIC, [logic]))
Beispiel #21
0
    def test_parse_examples(self):
        fs = get_example_formulae()

        for (f_out, _, _, logic) in fs:
            if logic == logics.QF_BV:
                # See test_parse_examples_bv
                continue
            buf_out = cStringIO()
            script_out = smtlibscript_from_formula(f_out)
            script_out.serialize(outstream=buf_out)

            buf_in = cStringIO(buf_out.getvalue())
            parser = SmtLibParser()
            script_in = parser.get_script(buf_in)
            f_in = script_in.get_last_formula()
            self.assertEqual(f_in.simplify(), f_out.simplify())
Beispiel #22
0
    def __init__(self, args, environment, logic, user_options=None,
                 LOGICS=None):
        Solver.__init__(self,
                        environment,
                        logic=logic,
                        user_options=user_options)
        # Flag used to debug interaction with the solver
        self.dbg = False

        if LOGICS is not None: self.LOGICS = LOGICS
        self.args = args
        self.declared_vars = set()
        self.solver = Popen(args, stdout=PIPE, stderr=PIPE, stdin=PIPE)
        self.parser = SmtLibParser(interactive=True)
        if PY2:
            self.solver_stdin = self.solver.stdin
            self.solver_stdout = self.solver.stdout
        else:
            self.solver_stdin = TextIOWrapper(self.solver.stdin)
            self.solver_stdout = TextIOWrapper(self.solver.stdout)

        # Initialize solver
        self.set_option(":print-success", "true")
        if self.options.generate_models:
            self.set_option(":produce-models", "true")
        # Redirect diagnostic output to stdout
        self.set_option(":diagnostic-output-channel", '"stdout"')
        if self.options is not None:
            for o,v in iteritems(self.options):
                self.set_option(o,v)
        self.set_logic(logic)
Beispiel #23
0
def validateModel(smtFile, modelFile):
    try:
        if (not path.exists(smtFile)):
            raise Exception("File not found: {}".format(smtFile))

        if (not path.exists(modelFile)):
            raise Exception("File not found: {}".format(modelFile))

        parser = SmtLibParser()

        formula = readSmtFile(parser, smtFile)
        symbols = getSymbols(parser)
        model = readModel(parser, modelFile)

        checkFullModel(model, symbols)

        result = simplify(formula.substitute(model))

        if (not result.is_constant()):
            print("INVALID: Did not provide a full model.")
        elif (not result.is_true()):
            print("INVALID: Model does not evaluate to true.")
        else:
            print("VALID")
    except Exception as e:
        print(e)
        sys.exit(1)
    def test_parse_examples(self):
        fs = get_example_formulae()

        for (f_out, _, _, logic) in fs:
            if logic == logics.QF_BV:
                # See test_parse_examples_bv
                continue
            buf = cStringIO()
            script_out = smtlibscript_from_formula(f_out)
            script_out.serialize(outstream=buf)
            #print(buf)

            buf.seek(0)
            parser = SmtLibParser()
            script_in = parser.get_script(buf)
            f_in = script_in.get_last_formula()
            self.assertEqual(f_in.simplify(), f_out.simplify())
Beispiel #25
0
    def test_parse_examples(self):
        fs = get_example_formulae()

        for (f_out, _, _, logic) in fs:
            if logic == QF_BV:
                # See test_parse_examples_bv
                continue
            buf_out = cStringIO()
            script_out = smtlibscript_from_formula(f_out)
            script_out.serialize(outstream=buf_out)

            buf_in = cStringIO(buf_out.getvalue())
            parser = SmtLibParser()
            script_in = parser.get_script(buf_in)
            f_in = script_in.get_last_formula()

            self.assertEqual(f_in, f_out)
Beispiel #26
0
def execute_script_fname(smtfile, logic, expected_result):
    """Read and call a Solver to solve the instance"""

    reset_env()
    assert os.path.exists(smtfile), smtfile
    parser = SmtLibParser()
    script = parser.get_script_fname(smtfile)
    try:
        log = script.evaluate(Solver(logic=logic))
    except NoSolverAvailableError:
        raise SkipTest("No solver for logic %s." % logic)

    res = check_sat_filter(log)
    if res:
        assert expected_result == "sat"
    else:
        assert expected_result == "unsat"
Beispiel #27
0
def execute_script_fname(smtfile):
    """Read and call a Solver to solve the instance"""
    print(smtfile)
    reset_env()
    assert os.path.exists(smtfile)
    parser = SmtLibParser()
    solver = NoopSolver(get_env())

    start = time.clock()
    script = parser.get_script_fname(smtfile)
    end = time.clock()

    script.evaluate(solver)
    res = solver.get_asserted_formula()
    assert res is not None

    return (smtfile, (end - start))
Beispiel #28
0
def main(args):
    from pysmt.logics import QF_AUFBV
    from pysmt.smtlib.parser import SmtLibParser
    from pysmt.shortcuts import get_env

    file = args[0]
    config = parseArgs(args[1:])

    logging.basicConfig(format='[%(name)s] %(levelname)s: %(message)s', level=config.getLogLevel())

    logger = logging.getLogger('Ablector')

    t = time.clock()
    with tempfile.NamedTemporaryFile() as tmpF:
        with open(os.devnull, 'w') as FNULL:
            process = subprocess.Popen(["boolector", "-ds", args[0]], stdout=tmpF, stderr=FNULL)
            process.communicate()
        with open(tmpF.name) as f:
            lines = f.readlines()
            isUnsat = False
            isCheck = False
            for l in lines:
                if l.strip() == "(assert false)":
                    isUnsat = True
                if l.strip() == "(check-sat)":
                    isCheck = True
            if isUnsat and isCheck:
                logger.info("ABLECTOR TIME: {0:.6f}".format(time.clock()-t))
                print("UNSAT")
                exit()
    timeOffset = (time.clock() - t)
    config.addTimeOffset(timeOffset)
    
    parser = SmtLibParser()
    
    with open(file) as f:
        script = parser.get_script(f)
        a = AblectorSolver(get_env(), QF_AUFBV, config)
        script.evaluate(a)
        if a.last_result:
            print("SAT")
            # NOTE(steuber): This model also contains function assignments which were  assigned in previous rounds! This means that there may be *wrong* assignments for MUL, SDIV etc!
            # The function assignments therefore consist of a mix of wrong (later found to be irrelevant) assignments and right assignments
            # a.btor.Print_model()
        else:
            print("UNSAT")
Beispiel #29
0
def execute_script_fname(smtfile, logic, expected_result):
    """Read and call a Solver to solve the instance"""

    reset_env()
    assert os.path.exists(smtfile), smtfile
    parser = SmtLibParser()
    script = parser.get_script_fname(smtfile)
    try:
        log = script.evaluate(Solver(logic=logic))
    except NoSolverAvailableError:
        raise unittest.SkipTest("No solver for logic %s." % logic)

    res = check_sat_filter(log)
    if res:
        assert expected_result == "sat"
    else:
        assert expected_result == "unsat"
Beispiel #30
0
    def __init__(self, args, environment, logic, options=None):
        Solver.__init__(self, environment, logic=logic, options=options)

        self.args = args
        self.declared_vars = set()
        self.solver = Popen(args, stdout=PIPE, stderr=PIPE, stdin=PIPE)
        self.parser = SmtLibParser()
        self.dbg = False

        # Initialize solver
        self._send_command(
            SmtLibCommand(smtcmd.SET_OPTION, [":print-success", "false"]))
        self._send_command(
            SmtLibCommand(smtcmd.SET_OPTION, [":produce-models", "true"]))
        if options is not None:
            for o, v in iteritems(options):
                self._send_command(SmtLibCommand(smtcmd.SET_OPTION, [o, v]))
        self._send_command(SmtLibCommand(smtcmd.SET_LOGIC, [logic]))
 def test_parse_bvx_var(self):
     """bvX is a valid identifier."""
     smtlib_input = """
     (declare-fun bv1 () (_ BitVec 8))
     (assert (bvult (_ bv0 8) (bvmul (bvadd bv1 (_ bv1 8)) (_ bv5 8))))
     (check-sat)"""
     parser = SmtLibParser()
     buffer_ = cStringIO(smtlib_input)
     script = parser.get_script(buffer_)
     # Check Parsed result
     iscript = iter(script)
     cmd = next(iscript)
     self.assertEqual(cmd.name, DECLARE_FUN)
     bv1 = cmd.args[0]
     self.assertEqual(bv1.symbol_type().width, 8)
     cmd = next(iscript)
     parsed_f = cmd.args[0]
     target_f = BVULT(BV(0, 8), BVMul(BVAdd(bv1, BV(1, 8)), BV(5, 8)))
     self.assertEqual(parsed_f, target_f)
Beispiel #32
0
 def __init__(self,var_next,init,safe,reach,goal,depth=0,id='0'):
     self.next = var_next
     self.prev = {n: v for v, n in self.next.items()}
     self.init = init
     self.safe = safe
     self.reach = reach
     self.goal = goal
     self.parser = SmtLibParser()
     self.depth=depth
     self.id=id
     self.formula_print_depth = 10
Beispiel #33
0
def execute_script_fname(smtfile, logic, expected_result):
    """Read and call a Solver to solve the instance"""

    reset_env()
    Solver = get_env().factory.Solver
    parser = SmtLibParser()
    script = parser.get_script_fname(smtfile)
    try:
        log = script.evaluate(Solver(logic=logic, incremental=False,
                                     generate_models=False))
    except NoSolverAvailableError:
        raise SkipTest("No solver for logic %s." % logic)
    except SolverReturnedUnknownResultError:
        if not logic.quantifier_free:
            warnings.warn("Test (%s, %s) could not be solver due to quantifiers." % (logic, smtfile))
            return
        raise

    res = check_sat_filter(log)
    assert expected_result == res
Beispiel #34
0
def execute_script_fname(smtfile, logic, expected_result):
    """Read and call a Solver to solve the instance"""

    reset_env()
    Solver = get_env().factory.Solver
    parser = SmtLibParser()
    script = parser.get_script_fname(smtfile)
    try:
        log = script.evaluate(Solver(logic=logic, incremental=False,
                                     generate_models=False))
    except NoSolverAvailableError:
        raise SkipTest("No solver for logic %s." % logic)
    except SolverReturnedUnknownResultError:
        if not logic.quantifier_free:
            warnings.warn("Test (%s, %s) could not be solver due to quantifiers." % (logic, smtfile))
            return
        raise

    res = check_sat_filter(log)
    assert expected_result == res
Beispiel #35
0
 def test_parse_bvx_var(self):
     """bvX is a valid identifier."""
     smtlib_input = """
     (declare-fun bv1 () (_ BitVec 8))
     (assert (bvult (_ bv0 8) (bvmul (bvadd bv1 (_ bv1 8)) (_ bv5 8))))
     (check-sat)"""
     parser = SmtLibParser()
     buffer_ = cStringIO(smtlib_input)
     script = parser.get_script(buffer_)
     # Check Parsed result
     iscript = iter(script)
     cmd = next(iscript)
     self.assertEqual(cmd.name, DECLARE_FUN)
     bv1 = cmd.args[0]
     self.assertEqual(bv1.symbol_type().width, 8)
     cmd = next(iscript)
     parsed_f = cmd.args[0]
     target_f = BVULT(BV(0, 8),
                      BVMul(BVAdd(bv1, BV(1, 8)), BV(5, 8)))
     self.assertEqual(parsed_f, target_f)
Beispiel #36
0
    def _recover_declarations(self, data):
        decls = {'consts': {}, 'funs': {}}

        parser = SmtLibParser()
        parser._reset()
        script = SmtLibScript()
        try:
            for cmd in parser.get_command_generator(cStringIO(data)):
                script.add_command(cmd)
        except RuntimeError:
            # Oblivious hack to prevent oop pysmt-related error
            pass
        script.annotations = parser.cache.annotations
        symbol_decls = script.filter_by_command_name('declare-fun')
        self._recover_declared_symbols(symbol_decls, decls)
        symbol_decls = script.filter_by_command_name('declare-const')
        self._recover_declared_symbols(symbol_decls, decls)
        asserts = script.filter_by_command_name('assert')
        self._prune_unused_symbols(asserts, decls)

        return decls
Beispiel #37
0
    def __init__(self, args, environment, logic, LOGICS=None, **options):
        Solver.__init__(self, environment, logic=logic, **options)
        self.to = self.environment.typeso
        if LOGICS is not None: self.LOGICS = LOGICS
        self.args = args
        self.declared_vars = [set()]
        self.declared_sorts = [set()]
        self.solver = Popen(args,
                            stdout=PIPE,
                            stderr=PIPE,
                            stdin=PIPE,
                            bufsize=-1)
        # Give time to the process to start-up
        time.sleep(0.01)
        self.parser = SmtLibParser(interactive=True)
        self.solver_stdin = TextIOWrapper(self.solver.stdin)
        self.solver_stdout = TextIOWrapper(self.solver.stdout)

        # Initialize solver
        self.options(self)
        self.set_logic(logic)
Beispiel #38
0
class SMTParser:
    def __init__(self, tokens):
        self.p = SmtLibParser()
        self.tokens = tokens

    def expect(self, *allowed):
        t = self.tokens.consume()
        if t not in allowed:
            raise PysmtSyntaxError("Invalid token, expected any of {}, got '{}'".format(allowed, t))
        return t

    def expect_assignment_tuple(self):
        self.expect('(')
        cmd = self.expect('define-fun')
        vname = self.p.parse_atom(self.tokens, cmd)
        self.expect('(')
        self.expect(')')
        t = self.p.parse_type(self.tokens, cmd)
        value = self.p.get_expression(self.tokens)
        self.expect(')')

        return Symbol(vname, t), getattr(pysmt.shortcuts, t.name)(value.constant_value())

    def consume_assignment_list(self):
        self.expect('(')
        self.expect('model')
        """Parses a list of expressions from the tokens"""

        assignments = []
        while True:
            next_token = self.tokens.consume()
            self.tokens.add_extra_token(next_token)  # push it back
            if next_token == ')':
                break

            assignments.append(self.expect_assignment_tuple())

        self.expect(')')

        return assignments
Beispiel #39
0
 def test_parse_consume(self):
     smt_script = """
     (model
     (define-fun STRING_cmd_line_arg_1_1000 () String "AAAAAAAAAAAA")
     )
     """
     tokens = Tokenizer(StringIO(smt_script), interactive=True)
     parser = SmtLibParser()
     tokens.consume()
     tokens.consume()
     next_token = tokens.consume()
     tokens.add_extra_token(next_token)
     tokens.consume()
Beispiel #40
0
    def test_parse_examples_bv(self):
        """For BV we represent a superset of the operators defined in SMT-LIB.

        We verify the correctness of the serialization process by
        checking the equivalence of the original and serialized
        expression.
        """
        fs = get_example_formulae()

        for (f_out, _, _, logic) in fs:
            if logic != logics.QF_BV:
                continue
            buf_out = StringIO()
            script_out = smtlibscript_from_formula(f_out)
            script_out.serialize(outstream=buf_out)

            buf_in = StringIO(buf_out.getvalue())
            parser = SmtLibParser()
            script_in = parser.get_script(buf_in)
            f_in = script_in.get_last_formula()

            self.assertValid(Iff(f_in, f_out))
    def test_vmt(self):
        parser = SmtLibParser()
        fname = os.path.join(SMTLIB_DIR, "small_set/vmt/c432_0f.vmt")
        script = parser.get_script_fname(fname)

        ann = script.annotations

        self.assertIn("A_1__AT0 ->", str(ann))

        a1 = Symbol("A_1__AT0")

        self.assertIn(a1, ann)
        self.assertTrue(ann.has_annotation(a1, "next"))
        self.assertFalse(ann.has_annotation(a1, "non-existent"))
        self.assertTrue(ann.has_annotation(a1, "next", "A_1__AT1"))
        self.assertFalse(ann.has_annotation(a1, "next", "non-existent"))

        self.assertIn("A_1__AT1", ann.annotations(a1)["next"])
        self.assertIn("A_1__AT1", ann[a1]["next"])

        curr_a1 = ann.all_annotated_formulae("next", "A_1__AT1")
        self.assertEqual(curr_a1, set([a1]))
Beispiel #42
0
    def test_vmt(self):
        parser = SmtLibParser()
        fname = os.path.join(SMTLIB_DIR, "small_set/vmt/c432_0f.vmt")
        script = parser.get_script_fname(fname)

        ann = script.annotations

        self.assertIn("A_1__AT0 ->", str(ann))

        a1 = Symbol("A_1__AT0")

        self.assertIn(a1, ann)
        self.assertTrue(ann.has_annotation(a1, "next"))
        self.assertFalse(ann.has_annotation(a1, "non-existent"))
        self.assertTrue(ann.has_annotation(a1, "next", "A_1__AT1"))
        self.assertFalse(ann.has_annotation(a1, "next", "non-existent"))

        self.assertIn("A_1__AT1", ann.annotations(a1)["next"])
        self.assertIn("A_1__AT1", ann[a1]["next"])

        curr_a1 = ann.all_annotated_formulae("next", "A_1__AT1")
        self.assertEqual(curr_a1, set([a1]))
Beispiel #43
0
    def load_from(self, infile):
        """
            Loads the encoding from an input file.
        """

        with open(infile, 'r') as fp:
            file_content = fp.readlines()

        # empty intervals for the standard encoding
        self.intvs, self.imaps, self.ivars = {}, {}, {}

        for line in file_content:
            if line[0] != ';':
                break
            elif line.startswith('; i '):
                f, arr = line[4:].strip().split(': ', 1)
                f = f.replace('-', '_')
                self.intvs[f], self.imaps[f], self.ivars[f] = [], {}, []

                for i, pair in enumerate(arr.split(', ')):
                    ub, symb = pair.split('<->')

                    if ub[0] != '+':
                        ub = float(ub)
                    symb = Symbol(symb, typename=BOOL)

                    self.intvs[f].append(ub)
                    self.ivars[f].append(symb)
                    self.imaps[f][ub] = i

            elif line.startswith('; features:'):
                self.feats = line[11:].strip().split(', ')
            elif line.startswith('; classes:'):
                self.nofcl = int(line[10:].strip())

        parser = SmtLibParser()
        script = parser.get_script(StringIO(''.join(file_content)))

        self.enc = script.get_last_formula()
Beispiel #44
0
    def test_parse_examples_bv(self):
        """For BV we represent a superset of the operators defined in SMT-LIB.

        We verify the correctness of the serialization process by
        checking the equivalence of the original and serialized
        expression.
        """
        fs = get_example_formulae()

        for (f_out, _, _, logic) in fs:
            if logic != logics.QF_BV:
                continue
            buf_out = cStringIO()
            script_out = smtlibscript_from_formula(f_out)
            script_out.serialize(outstream=buf_out)

            buf_in = cStringIO(buf_out.getvalue())
            parser = SmtLibParser()
            script_in = parser.get_script(buf_in)
            f_in = script_in.get_last_formula()

            self.assertValid(Iff(f_in, f_out))
Beispiel #45
0
def main():
    config = parseArgs(sys.argv[1:])
    logging.basicConfig(format='[%(name)s] %(levelname)s: %(message)s',
                        level=config.getLogLevel())
    folderName = os.path.join(os.path.dirname(__file__), 'smtlib')
    onlyfiles = [
        os.path.join(folderName, f) for f in os.listdir(folderName)
        if os.path.isfile(os.path.join(folderName, f)) and f.endswith(".smt2")
    ]
    for filePath in onlyfiles:
        with open(filePath, "r") as f:
            print(filePath)
            reset_env()
            parser = SmtLibParser()
            parser._reset()
            script = parser.get_script(f)
            status = "unsat"
            for f in script.filter_by_command_name("set-info"):
                if f.args[0] == ":status":
                    status = f.args[1]
                    assert (status == "sat" or status == "unsat")
            a = AblectorSolver(get_env(), QF_AUFBV, config)
            script.evaluate(a)
            b = BoolectorSolver(get_env(), QF_AUFBV, generate_models=True)

            if a.last_result:  # Check if assignment actually makes sense...
                assert (status == "sat")
                with open(filePath) as f:
                    newScriptSrc = ""
                    content = f.readlines()
                    for line in content:
                        if line.startswith(";ASSERT "):
                            varName = line[8:].strip()
                            print(varName + ": " +
                                  a.btor.Match_by_symbol(varName).assignment)
                            newScriptSrc += "(assert (= " + varName + " #b" + a.btor.Match_by_symbol(
                                varName).assignment + "))\n"
                        else:
                            newScriptSrc += line
                    parser._reset()
                    scriptWithValues = parser.get_script(
                        io.StringIO(newScriptSrc))
                    scriptWithValues.evaluate(b)
                    assert (b.last_result)
                print("SAT")
            else:
                assert (status == "unsat")
                print("UNSAT")
Beispiel #46
0
def parse_input_formula(path):
    with open(path, "r") as f:
        script_str = f.read()
    parser = SmtLibParser()
    script = parser.get_script_fname(path)
    smtlib_formula = script.get_strict_formula()

    #verify that the input formula is in cnf
    assert (smtlib_formula.is_and())
    for clause in smtlib_formula.args():
        assert (clause.is_or())
        for l in clause.args():
            assert (l.is_literal())

    #transform the input formula to a set of sets of literals
    cnf_formula = [
        list(set(clause.args())) for clause in smtlib_formula.args()
    ]

    #obtain all the boolean variables of the input formula
    free_vars = smtlib_formula.get_free_variables()

    return cnf_formula, free_vars
Beispiel #47
0
    def test_declare_sort(self):
        class SmtLibIgnore(SmtLibIgnoreMixin):
            declare_sort_history = []
            def declare_sort(self, name, arity):
                self.declare_sort_history.append((name, arity))

        mock = SmtLibIgnore()
        parser = SmtLibParser()
        smtlib_script = '\n'.join(['(declare-sort s0 0)', \
                                   '(declare-sort s1 1)', \
                                   '(declare-const c0 s0)', \
                                   '(declare-const c1 (s1 Int))'])
        outstream = cStringIO(smtlib_script)
        script = parser.get_script(outstream)
        script.evaluate(solver=mock)

        self.assertEqual(len(mock.declare_sort_history), 2)
        s0_name, s0_arity = mock.declare_sort_history[0]
        s1_name, s1_arity = mock.declare_sort_history[1]
        self.assertEqual(s0_name, "s0")
        self.assertEqual(s0_arity, 0)
        self.assertEqual(s1_name, "s1")
        self.assertEqual(s1_arity, 1)
Beispiel #48
0
    def test_declare_sort(self):
        class SmtLibIgnore(SmtLibIgnoreMixin):
            declare_sort_history = []
            def declare_sort(self, name, arity):
                self.declare_sort_history.append((name, arity))

        mock = SmtLibIgnore()
        parser = SmtLibParser()
        smtlib_script = '\n'.join(['(declare-sort s0 0)', \
                                   '(declare-sort s1 1)', \
                                   '(declare-const c0 s0)', \
                                   '(declare-const c1 (s1 Int))'])
        outstream = StringIO(smtlib_script)
        script = parser.get_script(outstream)
        script.evaluate(solver=mock)

        self.assertEqual(len(mock.declare_sort_history), 2)
        s0_name, s0_arity = mock.declare_sort_history[0]
        s1_name, s1_arity = mock.declare_sort_history[1]
        self.assertEqual(s0_name, "s0")
        self.assertEqual(s0_arity, 0)
        self.assertEqual(s1_name, "s1")
        self.assertEqual(s1_arity, 1)
Beispiel #49
0
def generate_special_paths(con_loc, ppc_str):
    parser = SmtLibParser()
    special_list = []
    script = parser.get_script(cStringIO(ppc_str))
    path_condition = script.get_last_formula()
    angelic_count = int(len(re.findall("angelic!(.+?)!0", str(path_condition.serialize()))) / 4)
    if angelic_count > 1:
        false_path = generate_false_path(path_condition)
        true_path = generate_true_path(path_condition)
        if true_path:
            special_list.append((con_loc, true_path, len(str(true_path.serialize()))))
        if false_path:
            special_list.append((con_loc, false_path, len(str(false_path.serialize()))))
    return special_list
    def test_dumped_logic(self):
        # Dumped logic matches the logic in the example
        fs = get_example_formulae()

        for (f_out, _, _, logic) in fs:
            buf_out = cStringIO()
            script_out = smtlibscript_from_formula(f_out)
            script_out.serialize(outstream=buf_out)
            buf_in = cStringIO(buf_out.getvalue())
            parser = SmtLibParser()
            script_in = parser.get_script(buf_in)
            for cmd in script_in:
                if cmd.name == "set-logic":
                    logic_in = cmd.args[0]
                    if logic == logics.QF_BOOL:
                        self.assertEqual(logic_in, logics.QF_UF)
                    elif logic == logics.BOOL:
                        self.assertEqual(logic_in, logics.LRA)
                    else:
                        self.assertEqual(logic_in, logic, script_in)
                    break
            else:  # Loops exited normally
                print("-" * 40)
                print(script_in)
Beispiel #51
0
def generate_flipped_path(ppc):
    """
    This function will check if a selected path is feasible
           ppc : partial path conditoin at chosen control loc
           chosen_control_loc: branch location selected for flip
           returns satisfiability of the negated path
    """
    parser = SmtLibParser()
    script = parser.get_script(cStringIO(ppc))
    formula = script.get_last_formula()
    prefix = formula.arg(0)
    constraint = formula.arg(1)
    new_path = And(prefix, Not(constraint))

    assert str(new_path.serialize()) != str(formula.serialize())
    return new_path
def validateModel(smtFile, modelFile, inputFile):
    try:
        if not path.exists(smtFile):
            raise Exception("File not found: {}".format(smtFile))

        if not path.exists(modelFile):
            raise Exception("File not found: {}".format(modelFile))

        if path.getsize(modelFile) == 0:
            print("model_validator_status=UNKNOWN")
            print("model_validator_error=no_output")
            sys.exit(0)

        parser = SmtLibParser()

        (formula, symbols) = readSmtFile(parser, smtFile)
        model, interpretation = readModel(parser, modelFile, inputFile)

        checkFullModel(model, interpretation, symbols)
        simplifier = SmtLibModelValidationSimplifier()
        result = simplifier.simplify(formula.substitute(model, interpretation))

        if result.is_false():
            print("model_validator_status=INVALID")
            print("model_validator_error=model_evaluates_to_false")
        elif result.is_true():
            print("model_validator_status=VALID")
            print("model_validator_error=none")
            print("starexec-result=sat")
        else:
            print("model_validator_status=UNKNOWN")
            print("model_validator_error=not_full_model")
    except Exception as e:
        print("model_validator_status=UNKNOWN")
        print("model_validator_error=unhandled_exception")
        print("model_validator_exception=\"{}\"".format(
            str(e).replace("'", "\\'").replace('"', '\\"').replace('\n', ' ')))
        sys.exit(0)
Beispiel #53
0
def generate_path_for_negation():
    constraint_list = []
    parser = SmtLibParser()
    emitter.normal("\tgenerating path for negation of patch constraint")
    for control_loc, sym_path in values.LIST_PPC:
        if control_loc == values.CONF_LOC_PATCH:
            script = parser.get_script(cStringIO(sym_path))
            formula = script.get_last_formula()
            patch_constraint = formula
            if formula.is_and():
                patch_constraint = formula.arg(1)
            constraint_list.append(patch_constraint.serialize())
    if not constraint_list:
        return None
    last_sym_path = values.LAST_PPC_FORMULA
    # script = parser.get_script(cStringIO(last_sym_path))
    # formula = script.get_last_formula()
    negated_path = None
    while constraint_list:
        constraint = last_sym_path
        if last_sym_path.is_and():
            constraint = last_sym_path.arg(1)
        constraint_str = constraint.serialize()
        if constraint_str in constraint_list:
            constraint_list.remove(constraint_str)
            constraint = Not(constraint)
        if negated_path is None:
            negated_path = constraint
        else:
            negated_path = And(negated_path, constraint)
        if last_sym_path.is_and():
            last_sym_path = last_sym_path.arg(0)
        else:
            break
    negated_path = And(negated_path, last_sym_path)
    return negated_path
Beispiel #54
0
import sys
import time
import json

from six.moves import cStringIO
from pysmt.shortcuts import Solver
from pysmt.smtlib.parser import SmtLibParser

if __name__ == '__main__':

    filename = sys.argv[1]
    solver_name = sys.argv[2]
    smt_file = open(filename, 'r').read().replace('\r', "")

    parser = SmtLibParser()
    script = parser.get_script(cStringIO(smt_file))

    #    with Solver(name=solver_name) as solver:

    solver = Solver(name=solver_name)
    error = False
    s = time.time()
    try:
        log = script.evaluate(solver)
        e = time.time()
    except:
        e = time.time()
        error = True
        log = []
    """
    print json.dumps({
Beispiel #55
0
import sys
import pysmt
from pysmt.rewritings import PrenexNormalizer, Ackermannizer
from pysmt.smtlib.script import SmtLibScript
from pysmt.smtlib.parser import SmtLibParser
from pysmt.shortcuts import to_smtlib, write_smtlib
from six.moves import cStringIO


parser = SmtLibParser()

with open("/home/yoniz/git/hermes/dispatcher/dispatcher/examples/Assessment2/nodtbbg.smt2", 'r') as f:
    smtlib_str = f.read();
stream = cStringIO(smtlib_str)
script = parser.get_script(stream)
formula = script.get_last_formula()
ackermanization = Ackermannizer()
ackermized_formula = ackermanization.do_ackermannization(formula)
write_smtlib(ackermized_formula, "/home/yoniz/git/hermes/dispatcher/dispatcher/examples/Assessment2/nodtbbg_ack.smt2" )


Beispiel #56
0
 def test_define_funs_arg_and_fun(self):
     smtlib_script = "\n".join(
         ['(define-fun f ((n Int)) Int n)', '(declare-fun n () Real)'])
     stream = cStringIO(smtlib_script)
     parser = SmtLibParser()
     script = parser.get_script(stream)
Beispiel #57
0
 def __init__(self, tokens):
     self.p = SmtLibParser()
     self.tokens = tokens
Beispiel #58
0
class SmtLibSolver(Solver):
    """Wrapper for using a solver via textual SMT-LIB interface.

    The solver is launched in a subprocess using args as arguments of
    the executable. Interaction with the solver occurs via pipe.
    """
    def __init__(self,
                 args,
                 environment,
                 logic,
                 user_options=None,
                 LOGICS=None):
        Solver.__init__(self,
                        environment,
                        logic=logic,
                        user_options=user_options)

        if LOGICS is not None: self.LOGICS = LOGICS
        self.args = args
        self.declared_vars = set()
        self.solver = Popen(args, stdout=PIPE, stderr=PIPE, stdin=PIPE)
        self.parser = SmtLibParser()
        if PY2:
            self.solver_stdin = self.solver.stdin
            self.solver_stdout = self.solver.stdout
        else:
            self.solver_stdin = TextIOWrapper(self.solver.stdin)
            self.solver_stdout = TextIOWrapper(self.solver.stdout)

        self.dbg = False

        # Initialize solver
        self._send_command(
            SmtLibCommand(smtcmd.SET_OPTION, [":print-success", "false"]))
        self._send_command(
            SmtLibCommand(smtcmd.SET_OPTION, [":produce-models", "true"]))

        if self.options is not None:
            for o, v in iteritems(self.options):
                self._send_command(SmtLibCommand(smtcmd.SET_OPTION, [o, v]))
        self._send_command(SmtLibCommand(smtcmd.SET_LOGIC, [logic]))

    def get_default_options(self, logic=None, user_options=None):
        res = {}
        for o, v in iteritems(user_options):
            if o not in ["generate_models", "unsat_cores_mode"]:
                res[o] = v
        return res

    def _send_command(self, cmd):
        if self.dbg: print("Sending: " + cmd.serialize_to_string())
        cmd.serialize(self.solver_stdin, daggify=True)
        self.solver_stdin.write("\n")
        self.solver_stdin.flush()

    def _get_answer(self):
        res = self.solver_stdout.readline().strip()
        if self.dbg: print("Read: " + str(res))
        return res

    def _get_value_answer(self):
        lst = self.parser.get_assignment_list(self.solver_stdout)
        if self.dbg: print("Read: " + str(lst))
        return lst

    def _declare_variable(self, symbol):
        cmd = SmtLibCommand(smtcmd.DECLARE_FUN, [symbol])
        self._send_command(cmd)
        self.declared_vars.add(symbol)

    def solve(self, assumptions=None):
        assert assumptions is None
        self._send_command(SmtLibCommand(smtcmd.CHECK_SAT, []))
        ans = self._get_answer()
        if ans == "sat":
            return True
        elif ans == "unsat":
            return False
        elif ans == "unknown":
            raise SolverReturnedUnknownResultError
        else:
            raise UnknownSolverAnswerError("Solver returned: " + ans)

    def reset_assertions(self):
        self._send_command(SmtLibCommand(smtcmd.RESET_ASSERTIONS, []))
        return

    def add_assertion(self, formula, named=None):
        deps = formula.get_free_variables()
        for d in deps:
            if d not in self.declared_vars:
                self._declare_variable(d)
        self._send_command(SmtLibCommand(smtcmd.ASSERT, [formula]))

    def push(self, levels=1):
        self._send_command(SmtLibCommand(smtcmd.PUSH, [levels]))

    def pop(self, levels=1):
        self._send_command(SmtLibCommand(smtcmd.POP, [levels]))

    def get_value(self, item):
        self._send_command(SmtLibCommand(smtcmd.GET_VALUE, [item]))
        lst = self._get_value_answer()
        assert len(lst) == 1
        assert len(lst[0]) == 2
        return lst[0][1]

    def print_model(self, name_filter=None):
        if name_filter is not None:
            raise NotImplementedError
        for v in self.declared_vars:
            print("%s = %s" % (v, self.get_value(v)))

    def get_model(self):
        assignment = {}
        for s in self.environment.formula_manager.get_all_symbols():
            if s.is_term():
                v = self.get_value(s)
                assignment[s] = v
        return EagerModel(assignment=assignment, environment=self.environment)

    def exit(self):
        self._send_command(SmtLibCommand(smtcmd.EXIT, []))
        self.solver_stdin.close()
        self.solver_stdout.close()
        self.solver.terminate()
        return