SMT-RAT  24.02
Toolbox for Strategic and Parallel Satisfiability-Modulo-Theories Solving
ParserState.h
Go to the documentation of this file.
1 /*
2  * @file ParserState.h
3  * @author Gereon Kremer <gereon.kremer@cs.rwth-aachen.de>
4  */
5 
6 #pragma once
7 
8 #include "Common.h"
9 #include "TheoryTypes.h"
10 #include "FunctionInstantiator.h"
11 #include "../InstructionHandler.h"
12 
13 #include <stack>
14 
15 namespace smtrat {
16 namespace parser {
17 
18  struct ParserState {
19 
20  struct ExpressionScope {
21  private:
22  std::map<std::string, types::TermType> bindings;
23  public:
25  {
26  this->bindings = state.bindings;
27  }
28  void discharge(ParserState& state) {
29  state.bindings = std::move(this->bindings);
30  }
31  };
32 
33  struct ScriptScope {
34  private:
35  std::map<std::string, types::TermType> constants;
36  std::map<std::string, types::VariableType> variables;
37  std::map<std::string, carl::UninterpretedFunction> declared_functions;
38  std::map<std::string, const FunctionInstantiator*> defined_functions;
39  std::map<std::string, const IndexedFunctionInstantiator*> defined_indexed_functions;
40  std::map<std::string, const UserFunctionInstantiator*> defined_user_functions;
41  public:
42  ScriptScope(const ParserState& state)
43  {
44  this->constants = state.constants;
45  this->variables = state.variables;
46  this->declared_functions = state.declared_functions;
47  this->defined_functions = state.defined_functions;
48  this->defined_indexed_functions = state.defined_indexed_functions;
49  this->defined_user_functions = state.defined_user_functions;
50  }
51  void discharge(ParserState& state) {
52  state.constants = std::move(this->constants);
53  state.variables = std::move(this->variables);
54  state.declared_functions = std::move(this->declared_functions);
55  state.defined_functions = std::move(this->defined_functions);
56  state.defined_indexed_functions = std::move(this->defined_indexed_functions);
57  state.defined_user_functions = std::move(this->defined_user_functions);
58  }
59  };
60 
61  carl::Logic logic;
62 
63  std::set<types::VariableType> auxiliary_variables;
64  std::map<std::string, types::TermType> bindings;
65  std::map<std::string, types::TermType> constants;
66  std::map<std::string, types::VariableType> variables;
67  std::map<std::string, carl::UninterpretedFunction> declared_functions;
68  std::map<std::string, const FunctionInstantiator*> defined_functions;
69  std::map<std::string, const IndexedFunctionInstantiator*> defined_indexed_functions;
70  std::map<std::string, const UserFunctionInstantiator*> defined_user_functions;
72  std::vector<smtrat::ModelVariable> artificialVariables;
73 
75 
76  std::stack<ExpressionScope> expressionScopes;
77  std::stack<ScriptScope> scriptScopes;
78 
80  }
82  while (!scriptScopes.empty()) popScriptScope();
83  for (auto& it: defined_functions) delete it.second;
84  for (auto& it: defined_indexed_functions) delete it.second;
85  for (auto& it: defined_user_functions) delete it.second;
86  defined_functions.clear();
88  defined_user_functions.clear();
89  }
90 
92  expressionScopes.emplace(*this);
93  }
95  expressionScopes.top().discharge(*this);
96  expressionScopes.pop();
97  }
98  void pushScriptScope() {
99  assert(expressionScopes.empty());
100  scriptScopes.emplace(*this);
101  }
102  void popScriptScope() {
103  assert(expressionScopes.empty());
104  assert(!scriptScopes.empty());
105  scriptScopes.top().discharge(*this);
106  scriptScopes.pop();
107  }
108  std::size_t script_scope_size() const {
109  return scriptScopes.size();
110  }
111 
112  void reset() {
113  auxiliary_variables.clear();
114  bindings.clear();
115  constants.clear();
116  variables.clear();
117  declared_functions.clear();
118  defined_user_functions.clear();
119  global_formulas.clear();
120  artificialVariables.clear();
121  while (!expressionScopes.empty()) expressionScopes.pop();
122  while (!scriptScopes.empty()) scriptScopes.pop();
123  }
124 
125 
126  void errorMessage(const std::string& msg) {
127  std::cerr << "Parser error: " << msg << std::endl;
128  }
129  bool isSymbolFree(const std::string& name, bool output = true) {
130  std::stringstream out;
131  if (name == "true" || name == "false") out << "\"" << name << "\" is a reserved keyword.";
132  else if (variables.find(name) != variables.end()) out << "\"" << name << "\" has already been defined as a variable.";
133  else if (bindings.find(name) != bindings.end()) out << "\"" << name << "\" has already been defined as a binding to \"" << bindings[name] << "\".";
134  else if (constants.find(name) != constants.end()) out << "\"" << name << "\" has already been defined as a constant.";
135  else if (declared_functions.find(name) != declared_functions.end()) out << "\"" << name << "\" has already been declared as a function.";
136  else if (defined_functions.find(name) != defined_functions.end()) out << "\"" << name << "\" has already been defined as a function.";
137  else if (defined_indexed_functions.find(name) != defined_indexed_functions.end()) out << "\"" << name << "\" has already been defined as a function.";
138  else if (defined_user_functions.find(name) != defined_user_functions.end()) out << "\"" << name << "\" has already been defined as a function by the user.";
139  else return true;
140  if (output) SMTRAT_LOG_ERROR("smtrat.parser", out.str());
141  return false;
142  }
143 
144  template<typename Res, typename T>
145  bool resolveSymbol(const std::string& name, const std::map<std::string, T>& map, Res& result) const {
146  auto it = map.find(name);
147  if (it == map.end()) return false;
148  result = it->second;
149  return true;
150  }
151 
152  bool resolveSymbol(const std::string& name, types::TermType& r) const {
153  if (resolveSymbol(name, variables, r)) return true;
154  if (resolveSymbol(name, bindings, r)) return true;
155  if (resolveSymbol(name, constants, r)) return true;
156  return false;
157  }
158  bool resolveSymbol(const std::string& name, types::VariableType& r) const {
159  if (resolveSymbol(name, variables, r)) return true;
160  return false;
161  }
162 
163  void registerFunction(const std::string& name, const FunctionInstantiator* fi) {
164  if (!isSymbolFree(name)) {
165  SMTRAT_LOG_ERROR("smtrat.parser", "Failed to register function \"" << name << "\", name is already used.");
166  return;
167  }
168  defined_functions.emplace(name, fi);
169  }
170  void registerFunction(const std::string& name, const IndexedFunctionInstantiator* fi) {
171  if (!isSymbolFree(name)) {
172  SMTRAT_LOG_ERROR("smtrat.parser", "Failed to register indexed function \"" << name << "\", name is already used.");
173  return;
174  }
175  defined_indexed_functions.emplace(name, fi);
176  }
177  void registerFunction(const std::string& name, const UserFunctionInstantiator* fi) {
178  if (!isSymbolFree(name)) {
179  SMTRAT_LOG_ERROR("smtrat.parser", "Failed to register user function \"" << name << "\", name is already used.");
180  return;
181  }
182  defined_user_functions.emplace(name, fi);
183  }
184  };
185 
186 }
187 }
carl::mpl_variant_of< TermTypes >::type TermType
Variant type for all terms.
Definition: TheoryTypes.h:160
carl::mpl_variant_of< VariableTypes >::type VariableType
Variant type for all variables.
Definition: TheoryTypes.h:132
Class to create the formulas for axioms.
carl::Formulas< Poly > FormulasT
Definition: types.h:39
#define SMTRAT_LOG_ERROR(channel, msg)
Definition: logging.h:32
ExpressionScope(const ParserState &state)
Definition: ParserState.h:24
std::map< std::string, types::TermType > bindings
Definition: ParserState.h:22
std::map< std::string, const IndexedFunctionInstantiator * > defined_indexed_functions
Definition: ParserState.h:39
std::map< std::string, types::VariableType > variables
Definition: ParserState.h:36
std::map< std::string, const UserFunctionInstantiator * > defined_user_functions
Definition: ParserState.h:40
std::map< std::string, types::TermType > constants
Definition: ParserState.h:35
void discharge(ParserState &state)
Definition: ParserState.h:51
std::map< std::string, const FunctionInstantiator * > defined_functions
Definition: ParserState.h:38
ScriptScope(const ParserState &state)
Definition: ParserState.h:42
std::map< std::string, carl::UninterpretedFunction > declared_functions
Definition: ParserState.h:37
std::size_t script_scope_size() const
Definition: ParserState.h:108
void registerFunction(const std::string &name, const FunctionInstantiator *fi)
Definition: ParserState.h:163
std::map< std::string, const FunctionInstantiator * > defined_functions
Definition: ParserState.h:68
void registerFunction(const std::string &name, const IndexedFunctionInstantiator *fi)
Definition: ParserState.h:170
std::map< std::string, types::TermType > constants
Definition: ParserState.h:65
bool resolveSymbol(const std::string &name, types::TermType &r) const
Definition: ParserState.h:152
void errorMessage(const std::string &msg)
Definition: ParserState.h:126
bool isSymbolFree(const std::string &name, bool output=true)
Definition: ParserState.h:129
std::map< std::string, const IndexedFunctionInstantiator * > defined_indexed_functions
Definition: ParserState.h:69
std::map< std::string, types::VariableType > variables
Definition: ParserState.h:66
std::set< types::VariableType > auxiliary_variables
Definition: ParserState.h:63
bool resolveSymbol(const std::string &name, types::VariableType &r) const
Definition: ParserState.h:158
std::map< std::string, types::TermType > bindings
Definition: ParserState.h:64
void registerFunction(const std::string &name, const UserFunctionInstantiator *fi)
Definition: ParserState.h:177
bool resolveSymbol(const std::string &name, const std::map< std::string, T > &map, Res &result) const
Definition: ParserState.h:145
InstructionHandler & handler
Definition: ParserState.h:74
std::stack< ScriptScope > scriptScopes
Definition: ParserState.h:77
std::map< std::string, const UserFunctionInstantiator * > defined_user_functions
Definition: ParserState.h:70
std::vector< smtrat::ModelVariable > artificialVariables
Definition: ParserState.h:72
std::stack< ExpressionScope > expressionScopes
Definition: ParserState.h:76
ParserState(InstructionHandler &ih)
Definition: ParserState.h:79
std::map< std::string, carl::UninterpretedFunction > declared_functions
Definition: ParserState.h:67