Parser
This example shows how to use the parser via the parser API.
1/******************************************************************************
2 * Top contributors (to current version):
3 * Andrew Reynolds, Aina Niemetz
4 *
5 * This file is part of the cvc5 project.
6 *
7 * Copyright (c) 2009-2024 by the authors listed in the file AUTHORS
8 * in the top-level source directory and their institutional affiliations.
9 * All rights reserved. See the file COPYING in the top-level source
10 * directory for licensing information.
11 * ****************************************************************************
12 *
13 * A simple demonstration of using the parser via C++ API.
14 */
15
16#include <cvc5/cvc5.h>
17#include <cvc5/cvc5_parser.h>
18
19#include <iostream>
20
21using namespace cvc5;
22using namespace cvc5::parser;
23
24int main()
25{
26 TermManager tm;
27 Solver slv(tm);
28
29 // set that we should print success after each successful command
30 slv.setOption("print-success", "true");
31
32 // construct an input parser associated the solver above
33 InputParser parser(&slv);
34
35 std::stringstream ss;
36 ss << "(set-logic QF_LIA)" << std::endl;
37 ss << "(declare-fun a () Int)" << std::endl;
38 ss << "(declare-fun b () Int)" << std::endl;
39 ss << "(declare-fun c () Int)" << std::endl;
40 ss << "(assert (> a (+ b c)))" << std::endl;
41 ss << "(assert (< a b))" << std::endl;
42 ss << "(assert (> c 0))" << std::endl;
43 parser.setStreamInput(modes::InputLanguage::SMT_LIB_2_6, ss, "MyStream");
44
45 // get the symbol manager of the parser, used when invoking commands below
46 SymbolManager* sm = parser.getSymbolManager();
47
48 // parse commands until finished
49 Command cmd;
50 while (true)
51 {
52 cmd = parser.nextCommand();
53 if (cmd.isNull())
54 {
55 break;
56 }
57 std::cout << "Executing command " << cmd << ":" << std::endl;
58 // invoke the command on the solver and the symbol manager, print the result
59 // to std::cout
60 cmd.invoke(&slv, sm, std::cout);
61 }
62 std::cout << "Finished parsing commands" << std::endl;
63
64 // now, check sat with the solver
65 Result r = slv.checkSat();
66 std::cout << "expected: unsat" << std::endl;
67 std::cout << "result: " << r << std::endl;
68}
1/******************************************************************************
2 * Top contributors (to current version):
3 * Aina Niemetz
4 *
5 * This file is part of the cvc5 project.
6 *
7 * Copyright (c) 2009-2024 by the authors listed in the file AUTHORS
8 * in the top-level source directory and their institutional affiliations.
9 * All rights reserved. See the file COPYING in the top-level source
10 * directory for licensing information.
11 * ****************************************************************************
12 *
13 * A simple demonstration of using the parser via the C API.
14 */
15
16#include <assert.h>
17#include <cvc5/c/cvc5.h>
18#include <cvc5/c/cvc5_parser.h>
19#include <stdio.h>
20
21int main()
22{
23 Cvc5TermManager* tm = cvc5_term_manager_new();
24 Cvc5* slv = cvc5_new(tm);
25
26 // set that we should print success after each successful command
27 cvc5_set_option(slv, "print-success", "true");
28
29 // construct an input parser associated the solver above
30 Cvc5InputParser* parser = cvc5_parser_new(slv, NULL);
31 cvc5_parser_set_str_input(
32 parser,
33 CVC5_INPUT_LANGUAGE_SMT_LIB_2_6,
34 "(set-logic QF_LIA)\n(declare-fun a () Int)\n(declare-fun b () "
35 "Int)\n(declare-fun c () Int)\n(assert (> a (+ b c)))\n(assert (< a "
36 "b))\n(assert (> c 0))\n",
37 "foo");
38
39 // get the symbol manager of the parser, used when invoking commands below
40 Cvc5SymbolManager* sm = cvc5_parser_get_sm(parser);
41
42 // parse commands until finished
43 Cvc5Command cmd;
44 do
45 {
46 const char* error_msg;
47 cmd = cvc5_parser_next_command(parser, &error_msg);
48 assert(error_msg == NULL);
49 if (cmd)
50 {
51 printf("Executing command %s:\n", cvc5_cmd_to_string(cmd));
52 // invoke the command on the solver and the symbol manager, print the
53 // result to stdout
54 printf("%s", cvc5_cmd_invoke(cmd, slv, sm));
55 }
56 } while (cmd);
57 printf("Finished parsing commands\n");
58
59 // now, check sat with the solver
60 Cvc5Result r = cvc5_check_sat(slv);
61 printf("expected: unsat\n");
62 printf("result: %s\n", cvc5_result_to_string(r));
63 cvc5_delete(slv);
64 cvc5_term_manager_delete(tm);
65}
1/******************************************************************************
2 * Top contributors (to current version):
3 * Mudathir Mohamed, Andrew Reynolds
4 *
5 * This file is part of the cvc5 project.
6 *
7 * Copyright (c) 2009-2024 by the authors listed in the file AUTHORS
8 * in the top-level source directory and their institutional affiliations.
9 * All rights reserved. See the file COPYING in the top-level source
10 * directory for licensing information.
11 * ****************************************************************************
12 *
13 * A simple demonstration of using the parser via Java API.
14 */
15
16import io.github.cvc5.*;
17import io.github.cvc5.modes.*;
18public class Parser
19{
20 public static void main(String args[]) throws CVC5ApiException
21 {
22 TermManager tm = new TermManager();
23 Solver slv = new Solver(tm);
24
25 // set that we should print success after each successful command
26 slv.setOption("print-success", "true");
27
28 // construct an input parser associated the solver above
29 InputParser parser = new InputParser(slv);
30
31 String ss = "";
32 ss += "(set-logic QF_LIA)\n";
33 ss += "(declare-fun a () Int)\n";
34 ss += "(declare-fun b () Int)\n";
35 ss += "(declare-fun c () Int)\n";
36 ss += "(assert (> a (+ b c)))\n";
37 ss += "(assert (< a b))\n";
38 ss += "(assert (> c 0))\n";
39 parser.setStringInput(InputLanguage.SMT_LIB_2_6, ss, "MyStream");
40
41 // get the symbol manager of the parser, used when invoking commands below
42 SymbolManager sm = parser.getSymbolManager();
43
44 // parse commands until finished
45 Command cmd;
46 while (true)
47 {
48 cmd = parser.nextCommand();
49 if (cmd.isNull())
50 {
51 break;
52 }
53 System.out.println("Executing command " + cmd + ":");
54 // invoke the command on the solver and the symbol manager, print the result
55 // to standard output
56 System.out.println(cmd.invoke(slv, sm));
57 }
58 System.out.println("Finished parsing commands");
59
60 // now, check sat with the solver
61 Result r = slv.checkSat();
62 System.out.println("expected: unsat");
63 System.out.println("result: " + r);
64 }
65}
1#!/usr/bin/env python
2###############################################################################
3# Top contributors (to current version):
4# Daniel Larraz, Andrew Reynolds
5#
6# This file is part of the cvc5 project.
7#
8# Copyright (c) 2009-2024 by the authors listed in the file AUTHORS
9# in the top-level source directory and their institutional affiliations.
10# All rights reserved. See the file COPYING in the top-level source
11# directory for licensing information.
12# #############################################################################
13#
14# A simple demonstration of using the parser via Python API
15##
16
17import cvc5
18
19if __name__ == "__main__":
20 tm = cvc5.TermManager()
21 slv = cvc5.Solver()
22
23 # set that we should print success after each successful command
24 slv.setOption("print-success", "true")
25
26 # construct an input parser associated the solver above
27 parser = cvc5.InputParser(slv)
28
29 input = """
30 (set-logic QF_LIA)
31 (declare-fun a () Int)
32 (declare-fun b () Int)
33 (declare-fun c () Int)
34 (assert (> a (+ b c)))
35 (assert (< a b))
36 (assert (> c 0))
37 """
38
39 parser.setStringInput(cvc5.InputLanguage.SMT_LIB_2_6, input, "MyInput")
40
41 # get the symbol manager of the parser, used when invoking commands below
42 sm = parser.getSymbolManager()
43
44 # parse commands until finished
45 while True:
46 cmd = parser.nextCommand()
47 if cmd.isNull():
48 break
49 print(f"Executing command {cmd}:")
50 # invoke the command on the solver and the symbol manager, print the result
51 print(cmd.invoke(slv, sm), end="")
52
53 print("Finished parsing commands")
54
55 # now, check sat with the solver
56 r = slv.checkSat()
57 print("expected: unsat")
58 print(f"result: {r}")
examples/api/smtlib/parser.smt2
1(set-logic QF_LIA)
2(declare-fun a () Int)
3(declare-fun b () Int)
4(declare-fun c () Int)
5(assert (> a (+ b c)))
6(assert (< a b))
7(assert (> c 0))
8(check-sat)