1 /********************* */
4 ** Original author: dejan
5 ** Major contributors: cconway, mdeters
6 ** Minor contributors (to current version): ajreynol
7 ** This file is part of the CVC4 prototype.
8 ** Copyright (c) 2009, 2010, 2011 The Analysis of Computer Systems Group (ACSys)
9 ** Courant Institute of Mathematical Sciences
10 ** New York University
11 ** See the file COPYING in the top-level source directory for licensing
12 ** information.\endverbatim
14 ** \brief Parser state implementation.
16 ** Parser state implementation.
24 #include "parser/input.h"
25 #include "parser/parser.h"
26 #include "parser/parser_exception.h"
27 #include "expr/command.h"
28 #include "expr/expr.h"
29 #include "expr/kind.h"
30 #include "expr/type.h"
31 #include "util/output.h"
32 #include "util/options.h"
33 #include "util/Assert.h"
34 #include "parser/cvc/cvc_input.h"
35 #include "parser/smt/smt_input.h"
38 using namespace CVC4::kind
;
43 Parser::Parser(ExprManager
* exprManager
, Input
* input
, bool strictMode
, bool parseOnly
) :
44 d_exprManager(exprManager
),
46 d_declScopeAllocated(),
47 d_declScope(&d_declScopeAllocated
),
48 d_anonymousFunctionCount(0),
50 d_checksEnabled(true),
51 d_strictMode(strictMode
),
52 d_parseOnly(parseOnly
) {
53 d_input
->setParser(*this);
56 Expr
Parser::getSymbol(const std::string
& name
, SymbolType type
) {
57 Assert( isDeclared(name
, type
) );
61 case SYM_VARIABLE
: // Functions share var namespace
62 return d_declScope
->lookup(name
);
70 Expr
Parser::getVariable(const std::string
& name
) {
71 return getSymbol(name
, SYM_VARIABLE
);
74 Expr
Parser::getFunction(const std::string
& name
) {
75 return getSymbol(name
, SYM_VARIABLE
);
78 Type
Parser::getType(const std::string
& var_name
,
80 Assert( isDeclared(var_name
, type
) );
81 Type t
= getSymbol(var_name
, type
).getType();
85 Type
Parser::getSort(const std::string
& name
) {
86 Assert( isDeclared(name
, SYM_SORT
) );
87 Type t
= d_declScope
->lookupType(name
);
91 Type
Parser::getSort(const std::string
& name
,
92 const std::vector
<Type
>& params
) {
93 Assert( isDeclared(name
, SYM_SORT
) );
94 Type t
= d_declScope
->lookupType(name
, params
);
98 size_t Parser::getArity(const std::string
& sort_name
){
99 Assert( isDeclared(sort_name
, SYM_SORT
) );
100 return d_declScope
->lookupArity(sort_name
);
103 /* Returns true if name is bound to a boolean variable. */
104 bool Parser::isBoolean(const std::string
& name
) {
105 return isDeclared(name
, SYM_VARIABLE
) && getType(name
).isBoolean();
108 /* Returns true if name is bound to a function-like thing (function,
109 * constructor, tester, or selector). */
110 bool Parser::isFunctionLike(const std::string
& name
) {
111 if(!isDeclared(name
, SYM_VARIABLE
)) {
114 Type type
= getType(name
);
115 return type
.isFunction() || type
.isConstructor() ||
116 type
.isTester() || type
.isSelector();
119 /* Returns true if name is bound to a defined function. */
120 bool Parser::isDefinedFunction(const std::string
& name
) {
121 // more permissive in type than isFunction(), because defined
122 // functions can be zero-ary and declared functions cannot.
123 return d_declScope
->isBoundDefinedFunction(name
);
126 /* Returns true if the Expr is a defined function. */
127 bool Parser::isDefinedFunction(Expr func
) {
128 // more permissive in type than isFunction(), because defined
129 // functions can be zero-ary and declared functions cannot.
130 return d_declScope
->isBoundDefinedFunction(func
);
133 /* Returns true if name is bound to a function returning boolean. */
134 bool Parser::isPredicate(const std::string
& name
) {
135 return isDeclared(name
, SYM_VARIABLE
) && getType(name
).isPredicate();
139 Parser::mkVar(const std::string
& name
, const Type
& type
) {
140 Debug("parser") << "mkVar(" << name
<< ", " << type
<< ")" << std::endl
;
141 Expr expr
= d_exprManager
->mkVar(name
, type
);
142 defineVar(name
, expr
);
147 Parser::mkFunction(const std::string
& name
, const Type
& type
) {
148 Debug("parser") << "mkVar(" << name
<< ", " << type
<< ")" << std::endl
;
149 Expr expr
= d_exprManager
->mkVar(name
, type
);
150 defineFunction(name
, expr
);
155 Parser::mkAnonymousFunction(const std::string
& prefix
, const Type
& type
) {
157 name
<< prefix
<< ':' << ++d_anonymousFunctionCount
;
158 return mkFunction(name
.str(), type
);
162 Parser::mkVars(const std::vector
<std::string
> names
,
164 std::vector
<Expr
> vars
;
165 for(unsigned i
= 0; i
< names
.size(); ++i
) {
166 vars
.push_back(mkVar(names
[i
], type
));
172 Parser::defineVar(const std::string
& name
, const Expr
& val
) {
173 d_declScope
->bind(name
, val
);
174 Assert( isDeclared(name
) );
178 Parser::defineFunction(const std::string
& name
, const Expr
& val
) {
179 d_declScope
->bindDefinedFunction(name
, val
);
180 Assert( isDeclared(name
) );
184 Parser::defineType(const std::string
& name
, const Type
& type
) {
185 d_declScope
->bindType(name
, type
);
186 Assert( isDeclared(name
, SYM_SORT
) );
190 Parser::defineType(const std::string
& name
,
191 const std::vector
<Type
>& params
,
193 d_declScope
->bindType(name
, params
, type
);
194 Assert( isDeclared(name
, SYM_SORT
) );
198 Parser::defineParameterizedType(const std::string
& name
,
199 const std::vector
<Type
>& params
,
201 if(Debug
.isOn("parser")) {
202 Debug("parser") << "defineParameterizedType(" << name
<< ", " << params
.size() << ", [";
203 if(params
.size() > 0) {
204 copy(params
.begin(), params
.end() - 1,
205 ostream_iterator
<Type
>(Debug("parser"), ", ") );
206 Debug("parser") << params
.back();
208 Debug("parser") << "], " << type
<< ")" << std::endl
;
210 defineType(name
, params
, type
);
214 Parser::mkSort(const std::string
& name
) {
215 Debug("parser") << "newSort(" << name
<< ")" << std::endl
;
216 Type type
= d_exprManager
->mkSort(name
);
217 defineType(name
, type
);
222 Parser::mkSortConstructor(const std::string
& name
, size_t arity
) {
223 Debug("parser") << "newSortConstructor(" << name
<< ", " << arity
<< ")"
225 SortConstructorType type
= d_exprManager
->mkSortConstructor(name
, arity
);
226 defineType(name
, vector
<Type
>(arity
), type
);
230 SortType
Parser::mkUnresolvedType(const std::string
& name
) {
231 SortType unresolved
= mkSort(name
);
232 d_unresolved
.insert(unresolved
);
237 Parser::mkUnresolvedTypeConstructor(const std::string
& name
,
239 SortConstructorType unresolved
= mkSortConstructor(name
,arity
);
240 d_unresolved
.insert(unresolved
);
245 Parser::mkUnresolvedTypeConstructor(const std::string
& name
,
246 const std::vector
<Type
>& params
) {
247 Debug("parser") << "newSortConstructor(P)(" << name
<< ", " << params
.size() << ")"
249 SortConstructorType unresolved
= d_exprManager
->mkSortConstructor(name
, params
.size());
250 defineType(name
, params
, unresolved
);
251 Type t
= getSort( name
, params
);
252 d_unresolved
.insert(unresolved
);
256 bool Parser::isUnresolvedType(const std::string
& name
) {
257 if(!isDeclared(name
, SYM_SORT
)) {
260 return d_unresolved
.find(getSort(name
)) != d_unresolved
.end();
263 std::vector
<DatatypeType
>
264 Parser::mkMutualDatatypeTypes(const std::vector
<Datatype
>& datatypes
) {
266 std::vector
<DatatypeType
> types
=
267 d_exprManager
->mkMutualDatatypeTypes(datatypes
, d_unresolved
);
269 Assert(datatypes
.size() == types
.size());
271 for(unsigned i
= 0; i
< datatypes
.size(); ++i
) {
272 DatatypeType t
= types
[i
];
273 const Datatype
& dt
= t
.getDatatype();
274 const std::string
& name
= dt
.getName();
275 Debug("parser-idt") << "define " << name
<< " as " << t
<< std::endl
;
276 if(isDeclared(name
, SYM_SORT
)) {
277 throw ParserException(name
+ " already declared");
279 if( t
.isParametric() ) {
280 std::vector
< Type
> paramTypes
= t
.getParamTypes();
281 defineType(name
, paramTypes
, t
);
285 for(Datatype::const_iterator j
= dt
.begin(),
289 const DatatypeConstructor
& ctor
= *j
;
290 Expr::printtypes::Scope
pts(Debug("parser-idt"), true);
291 Expr constructor
= ctor
.getConstructor();
292 Debug("parser-idt") << "+ define " << constructor
<< std::endl
;
293 string constructorName
= constructor
.toString();
294 if(isDeclared(constructorName
, SYM_VARIABLE
)) {
295 throw ParserException(constructorName
+ " already declared");
297 defineVar(constructorName
, constructor
);
298 Expr tester
= ctor
.getTester();
299 Debug("parser-idt") << "+ define " << tester
<< std::endl
;
300 string testerName
= tester
.toString();
301 if(isDeclared(testerName
, SYM_VARIABLE
)) {
302 throw ParserException(testerName
+ " already declared");
304 defineVar(testerName
, tester
);
305 for(DatatypeConstructor::const_iterator k
= ctor
.begin(),
309 Expr selector
= (*k
).getSelector();
310 Debug("parser-idt") << "+++ define " << selector
<< std::endl
;
311 string selectorName
= selector
.toString();
312 if(isDeclared(selectorName
, SYM_VARIABLE
)) {
313 throw ParserException(selectorName
+ " already declared");
315 defineVar(selectorName
, selector
);
320 // These are no longer used, and the ExprManager would have
321 // complained of a bad substitution if anything is left unresolved.
322 // Clear out the set.
323 d_unresolved
.clear();
328 bool Parser::isDeclared(const std::string
& name
, SymbolType type
) {
331 return d_declScope
->isBound(name
);
333 return d_declScope
->isBoundType(name
);
339 void Parser::checkDeclaration(const std::string
& varName
,
340 DeclarationCheck check
,
342 throw(ParserException
) {
343 if(!d_checksEnabled
) {
349 if( !isDeclared(varName
, type
) ) {
350 parseError("Symbol " + varName
+ " not declared as a " +
351 (type
== SYM_VARIABLE
? "variable" : "type"));
355 case CHECK_UNDECLARED
:
356 if( isDeclared(varName
, type
) ) {
357 parseError("Symbol " + varName
+ " previously declared as a " +
358 (type
== SYM_VARIABLE
? "variable" : "type"));
370 void Parser::checkFunctionLike(const std::string
& name
) throw(ParserException
) {
371 if(d_checksEnabled
&& !isFunctionLike(name
)) {
372 parseError("Expecting function-like symbol, found '" + name
+ "'");
376 void Parser::checkArity(Kind kind
, unsigned numArgs
)
377 throw(ParserException
) {
378 if(!d_checksEnabled
) {
382 unsigned min
= d_exprManager
->minArity(kind
);
383 unsigned max
= d_exprManager
->maxArity(kind
);
385 if( numArgs
< min
|| numArgs
> max
) {
388 if( numArgs
< min
) {
389 ss
<< "at least " << min
<< " ";
391 ss
<< "at most " << max
<< " ";
393 ss
<< "arguments for operator '" << kind
<< "', ";
394 ss
<< "found " << numArgs
;
395 parseError(ss
.str());
399 void Parser::checkOperator(Kind kind
, unsigned numArgs
) throw(ParserException
) {
400 if( d_strictMode
&& d_logicOperators
.find(kind
) == d_logicOperators
.end() ) {
401 parseError( "Operator is not defined in the current logic: " + kindToString(kind
) );
403 checkArity(kind
, numArgs
);
406 void Parser::addOperator(Kind kind
) {
407 d_logicOperators
.insert(kind
);
410 void Parser::preemptCommand(Command
* cmd
) {
411 d_commandQueue
.push_back(cmd
);
414 Command
* Parser::nextCommand() throw(ParserException
) {
415 Debug("parser") << "nextCommand()" << std::endl
;
417 if(!d_commandQueue
.empty()) {
418 cmd
= d_commandQueue
.front();
419 d_commandQueue
.pop_front();
426 cmd
= d_input
->parseCommand();
427 d_commandQueue
.push_back(cmd
);
428 cmd
= d_commandQueue
.front();
429 d_commandQueue
.pop_front();
433 } catch(ParserException
& e
) {
436 } catch(Exception
& e
) {
439 // set the language of the stream, otherwise if it contains
440 // Exprs or Types it prints in the AST language
441 OutputLanguage outlang
=
442 language::toOutputLanguage(d_input
->getLanguage());
443 ss
<< Expr::setlanguage(outlang
) << e
;
444 parseError( ss
.str() );
448 Debug("parser") << "nextCommand() => " << cmd
<< std::endl
;
452 Expr
Parser::nextExpression() throw(ParserException
) {
453 Debug("parser") << "nextExpression()" << std::endl
;
457 result
= d_input
->parseExpr();
458 if(result
.isNull()) {
461 } catch(ParserException
& e
) {
464 } catch(Exception
& e
) {
468 parseError( ss
.str() );
471 Debug("parser") << "nextExpression() => " << result
<< std::endl
;
476 }/* CVC4::parser namespace */
477 }/* CVC4 namespace */