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