Parser with Shared Symbol Manager

This example shows how to use the parser via the parser API and use it to parse additional terms in another input source.

examples/api/cpp/parser_sym_manager.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 multiple parsers with the same symbol manager
14 * via C++ API.
15 */
16
17#include <cvc5/cvc5.h>
18#include <cvc5/cvc5_parser.h>
19
20#include <iostream>
21
22using namespace cvc5;
23using namespace cvc5::parser;
24
25int main()
26{
27  Solver slv;
28
29  SymbolManager sm(&slv);
30
31  // construct an input parser associated the solver above
32  InputParser parser(&slv, &sm);
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 () Bool)" << std::endl;
39  parser.setStreamInput(modes::InputLanguage::SMT_LIB_2_6, ss, "MyStream");
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    std::cout << "Executing command " << cmd << ":" << std::endl;
51    // invoke the command on the solver and the symbol manager, print the result
52    // to std::cout
53    cmd.invoke(&slv, &sm, std::cout);
54  }
55  std::cout << "Finished parsing commands" << std::endl;
56
57  // Note that sm now has a,b,c in its symbol table.
58
59  // Now, construct a new parser with the same symbol manager. We will parse
60  // terms with it.
61  InputParser parser2(&slv, &sm);
62  std::stringstream ss2;
63  ss2 << "(+ a b)" << std::endl;
64  ss2 << "(- a 10)" << std::endl;
65  ss2 << "(>= 0 45)" << std::endl;
66  ss2 << "(and c c)" << std::endl;
67  ss2 << "true" << std::endl;
68  parser2.setStreamInput(modes::InputLanguage::SMT_LIB_2_6, ss2, "MyStream2");
69
70  // parse terms until finished
71  Term t;
72  do
73  {
74    t = parser2.nextTerm();
75    std::cout << "Parsed term: " << t << std::endl;
76  } while (!t.isNull());
77  std::cout << "Finished parsing terms" << std::endl;
78}