Parser

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

examples/api/cpp/parser.cpp

 1/******************************************************************************
 2 * Top contributors (to current version):
 3 *   Andrew Reynolds
 4 *
 5 * This file is part of the cvc5 project.
 6 *
 7 * Copyright (c) 2009-2022 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  Solver slv;
27
28  // set that we should print success after each successful command
29  slv.setOption("print-success", "true");
30
31  // construct an input parser associated the solver above
32  InputParser parser(&slv);
33
34  std::stringstream ss;
35  ss << "(set-logic QF_LIA)" << std::endl;
36  ss << "(declare-fun a () Int)" << std::endl;
37  ss << "(declare-fun b () Int)" << std::endl;
38  ss << "(declare-fun c () Int)" << std::endl;
39  ss << "(assert (> a (+ b c)))" << std::endl;
40  ss << "(assert (< a b))" << std::endl;
41  ss << "(assert (> c 0))" << std::endl;
42  parser.setStreamInput(modes::InputLanguage::SMT_LIB_2_6, ss, "MyStream");
43
44  // get the symbol manager of the parser, used when invoking commands below
45  SymbolManager* sm = parser.getSymbolManager();
46
47  // parse commands until finished
48  Command cmd;
49  while (true)
50  {
51    cmd = parser.nextCommand();
52    if (cmd.isNull())
53    {
54      break;
55    }
56    std::cout << "Executing command " << cmd << ":" << std::endl;
57    // invoke the command on the solver and the symbol manager, print the result
58    // to std::cout
59    cmd.invoke(&slv, sm, std::cout);
60  }
61  std::cout << "Finished parsing commands" << std::endl;
62
63  // now, check sat with the solver
64  Result r = slv.checkSat();
65  std::cout << "expected: unsat" << std::endl;
66  std::cout << "result: " << r << std::endl;
67}