Parser

This example shows how to use the parser via the parser API.

examples/api/cpp/parser.cpp

 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}