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 "options/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 checkDeclaration(name
, CHECK_DECLARED
, type
);
58 Assert( isDeclared(name
, type
) );
62 case SYM_VARIABLE
: // Functions share var namespace
63 return d_declScope
->lookup(name
);
71 Expr
Parser::getVariable(const std::string
& name
) {
72 return getSymbol(name
, SYM_VARIABLE
);
75 Expr
Parser::getFunction(const std::string
& name
) {
76 return getSymbol(name
, SYM_VARIABLE
);
79 Type
Parser::getType(const std::string
& var_name
,
81 checkDeclaration(var_name
, CHECK_DECLARED
, type
);
82 Assert( isDeclared(var_name
, type
) );
83 Type t
= getSymbol(var_name
, type
).getType();
87 Type
Parser::getSort(const std::string
& name
) {
88 checkDeclaration(name
, CHECK_DECLARED
, SYM_SORT
);
89 Assert( isDeclared(name
, SYM_SORT
) );
90 Type t
= d_declScope
->lookupType(name
);
94 Type
Parser::getSort(const std::string
& name
,
95 const std::vector
<Type
>& params
) {
96 checkDeclaration(name
, CHECK_DECLARED
, SYM_SORT
);
97 Assert( isDeclared(name
, SYM_SORT
) );
98 Type t
= d_declScope
->lookupType(name
, params
);
102 size_t Parser::getArity(const std::string
& sort_name
){
103 checkDeclaration(sort_name
, CHECK_DECLARED
, SYM_SORT
);
104 Assert( isDeclared(sort_name
, SYM_SORT
) );
105 return d_declScope
->lookupArity(sort_name
);
108 /* Returns true if name is bound to a boolean variable. */
109 bool Parser::isBoolean(const std::string
& name
) {
110 return isDeclared(name
, SYM_VARIABLE
) && getType(name
).isBoolean();
113 /* Returns true if name is bound to a function-like thing (function,
114 * constructor, tester, or selector). */
115 bool Parser::isFunctionLike(const std::string
& name
) {
116 if(!isDeclared(name
, SYM_VARIABLE
)) {
119 Type type
= getType(name
);
120 return type
.isFunction() || type
.isConstructor() ||
121 type
.isTester() || type
.isSelector();
124 /* Returns true if name is bound to a defined function. */
125 bool Parser::isDefinedFunction(const std::string
& name
) {
126 // more permissive in type than isFunction(), because defined
127 // functions can be zero-ary and declared functions cannot.
128 return d_declScope
->isBoundDefinedFunction(name
);
131 /* Returns true if the Expr is a defined function. */
132 bool Parser::isDefinedFunction(Expr func
) {
133 // more permissive in type than isFunction(), because defined
134 // functions can be zero-ary and declared functions cannot.
135 return d_declScope
->isBoundDefinedFunction(func
);
138 /* Returns true if name is bound to a function returning boolean. */
139 bool Parser::isPredicate(const std::string
& name
) {
140 return isDeclared(name
, SYM_VARIABLE
) && getType(name
).isPredicate();
144 Parser::mkVar(const std::string
& name
, const Type
& type
,
146 Debug("parser") << "mkVar(" << name
<< ", " << type
<< ")" << std::endl
;
147 Expr expr
= d_exprManager
->mkVar(name
, type
);
148 defineVar(name
, expr
, levelZero
);
153 Parser::mkFunction(const std::string
& name
, const Type
& type
,
155 Debug("parser") << "mkVar(" << name
<< ", " << type
<< ")" << std::endl
;
156 Expr expr
= d_exprManager
->mkVar(name
, type
);
157 defineFunction(name
, expr
, levelZero
);
162 Parser::mkAnonymousFunction(const std::string
& prefix
, const Type
& type
) {
164 name
<< prefix
<< ':' << ++d_anonymousFunctionCount
;
165 return mkFunction(name
.str(), type
);
169 Parser::mkVars(const std::vector
<std::string
> names
,
172 std::vector
<Expr
> vars
;
173 for(unsigned i
= 0; i
< names
.size(); ++i
) {
174 vars
.push_back(mkVar(names
[i
], type
, levelZero
));
180 Parser::defineVar(const std::string
& name
, const Expr
& val
,
182 d_declScope
->bind(name
, val
, levelZero
);
183 Assert( isDeclared(name
) );
187 Parser::defineFunction(const std::string
& name
, const Expr
& val
,
189 d_declScope
->bindDefinedFunction(name
, val
, levelZero
);
190 Assert( isDeclared(name
) );
194 Parser::defineType(const std::string
& name
, const Type
& type
) {
195 d_declScope
->bindType(name
, type
);
196 Assert( isDeclared(name
, SYM_SORT
) );
200 Parser::defineType(const std::string
& name
,
201 const std::vector
<Type
>& params
,
203 d_declScope
->bindType(name
, params
, type
);
204 Assert( isDeclared(name
, SYM_SORT
) );
208 Parser::defineParameterizedType(const std::string
& name
,
209 const std::vector
<Type
>& params
,
211 if(Debug
.isOn("parser")) {
212 Debug("parser") << "defineParameterizedType(" << name
<< ", " << params
.size() << ", [";
213 if(params
.size() > 0) {
214 copy(params
.begin(), params
.end() - 1,
215 ostream_iterator
<Type
>(Debug("parser"), ", ") );
216 Debug("parser") << params
.back();
218 Debug("parser") << "], " << type
<< ")" << std::endl
;
220 defineType(name
, params
, type
);
224 Parser::mkSort(const std::string
& name
) {
225 Debug("parser") << "newSort(" << name
<< ")" << std::endl
;
226 Type type
= d_exprManager
->mkSort(name
);
227 defineType(name
, type
);
232 Parser::mkSortConstructor(const std::string
& name
, size_t arity
) {
233 Debug("parser") << "newSortConstructor(" << name
<< ", " << arity
<< ")"
235 SortConstructorType type
= d_exprManager
->mkSortConstructor(name
, arity
);
236 defineType(name
, vector
<Type
>(arity
), type
);
240 SortType
Parser::mkUnresolvedType(const std::string
& name
) {
241 SortType unresolved
= mkSort(name
);
242 d_unresolved
.insert(unresolved
);
247 Parser::mkUnresolvedTypeConstructor(const std::string
& name
,
249 SortConstructorType unresolved
= mkSortConstructor(name
, arity
);
250 d_unresolved
.insert(unresolved
);
255 Parser::mkUnresolvedTypeConstructor(const std::string
& name
,
256 const std::vector
<Type
>& params
) {
257 Debug("parser") << "newSortConstructor(P)(" << name
<< ", " << params
.size() << ")"
259 SortConstructorType unresolved
= d_exprManager
->mkSortConstructor(name
, params
.size());
260 defineType(name
, params
, unresolved
);
261 Type t
= getSort( name
, params
);
262 d_unresolved
.insert(unresolved
);
266 bool Parser::isUnresolvedType(const std::string
& name
) {
267 if(!isDeclared(name
, SYM_SORT
)) {
270 return d_unresolved
.find(getSort(name
)) != d_unresolved
.end();
273 std::vector
<DatatypeType
>
274 Parser::mkMutualDatatypeTypes(const std::vector
<Datatype
>& datatypes
) {
276 std::vector
<DatatypeType
> types
=
277 d_exprManager
->mkMutualDatatypeTypes(datatypes
, d_unresolved
);
279 Assert(datatypes
.size() == types
.size());
281 for(unsigned i
= 0; i
< datatypes
.size(); ++i
) {
282 DatatypeType t
= types
[i
];
283 const Datatype
& dt
= t
.getDatatype();
284 const std::string
& name
= dt
.getName();
285 Debug("parser-idt") << "define " << name
<< " as " << t
<< std::endl
;
286 if(isDeclared(name
, SYM_SORT
)) {
287 throw ParserException(name
+ " already declared");
289 if( t
.isParametric() ) {
290 std::vector
< Type
> paramTypes
= t
.getParamTypes();
291 defineType(name
, paramTypes
, t
);
295 for(Datatype::const_iterator j
= dt
.begin(),
299 const DatatypeConstructor
& ctor
= *j
;
300 Expr::printtypes::Scope
pts(Debug("parser-idt"), true);
301 Expr constructor
= ctor
.getConstructor();
302 Debug("parser-idt") << "+ define " << constructor
<< std::endl
;
303 string constructorName
= constructor
.toString();
304 if(isDeclared(constructorName
, SYM_VARIABLE
)) {
305 throw ParserException(constructorName
+ " already declared");
307 defineVar(constructorName
, constructor
);
308 Expr tester
= ctor
.getTester();
309 Debug("parser-idt") << "+ define " << tester
<< std::endl
;
310 string testerName
= tester
.toString();
311 if(isDeclared(testerName
, SYM_VARIABLE
)) {
312 throw ParserException(testerName
+ " already declared");
314 defineVar(testerName
, tester
);
315 for(DatatypeConstructor::const_iterator k
= ctor
.begin(),
319 Expr selector
= (*k
).getSelector();
320 Debug("parser-idt") << "+++ define " << selector
<< std::endl
;
321 string selectorName
= selector
.toString();
322 if(isDeclared(selectorName
, SYM_VARIABLE
)) {
323 throw ParserException(selectorName
+ " already declared");
325 defineVar(selectorName
, selector
);
330 // These are no longer used, and the ExprManager would have
331 // complained of a bad substitution if anything is left unresolved.
332 // Clear out the set.
333 d_unresolved
.clear();
338 DatatypeType
Parser::mkRecordType(const std::vector
< std::pair
<std::string
, Type
> >& typeIds
) {
339 DatatypeType
& dtt
= d_recordTypes
[typeIds
];
341 Datatype
dt("__cvc4_record");
342 Debug("datatypes") << "make new record_ctor" << std::endl
;
343 DatatypeConstructor
c("__cvc4_record_ctor");
344 for(std::vector
< std::pair
<std::string
, Type
> >::const_iterator i
= typeIds
.begin(); i
!= typeIds
.end(); ++i
) {
345 c
.addArg((*i
).first
, (*i
).second
);
347 dt
.addConstructor(c
);
348 dtt
= d_exprManager
->mkDatatypeType(dt
);
350 Debug("datatypes") << "use old record_ctor" << std::endl
;
355 DatatypeType
Parser::mkTupleType(const std::vector
<Type
>& types
) {
356 DatatypeType
& dtt
= d_tupleTypes
[types
];
358 Datatype
dt("__cvc4_tuple");
359 DatatypeConstructor
c("__cvc4_tuple_ctor");
360 for(std::vector
<Type
>::const_iterator i
= types
.begin(); i
!= types
.end(); ++i
) {
361 c
.addArg("__cvc4_tuple_stor", *i
);
363 dt
.addConstructor(c
);
364 dtt
= d_exprManager
->mkDatatypeType(dt
);
369 bool Parser::isDeclared(const std::string
& name
, SymbolType type
) {
372 return d_declScope
->isBound(name
);
374 return d_declScope
->isBoundType(name
);
380 void Parser::checkDeclaration(const std::string
& varName
,
381 DeclarationCheck check
,
383 throw(ParserException
) {
384 if(!d_checksEnabled
) {
390 if( !isDeclared(varName
, type
) ) {
391 parseError("Symbol " + varName
+ " not declared as a " +
392 (type
== SYM_VARIABLE
? "variable" : "type"));
396 case CHECK_UNDECLARED
:
397 if( isDeclared(varName
, type
) ) {
398 parseError("Symbol " + varName
+ " previously declared as a " +
399 (type
== SYM_VARIABLE
? "variable" : "type"));
411 void Parser::checkFunctionLike(const std::string
& name
) throw(ParserException
) {
412 if(d_checksEnabled
&& !isFunctionLike(name
)) {
413 parseError("Expecting function-like symbol, found '" + name
+ "'");
417 void Parser::checkArity(Kind kind
, unsigned numArgs
)
418 throw(ParserException
) {
419 if(!d_checksEnabled
) {
423 unsigned min
= d_exprManager
->minArity(kind
);
424 unsigned max
= d_exprManager
->maxArity(kind
);
426 if( numArgs
< min
|| numArgs
> max
) {
429 if( numArgs
< min
) {
430 ss
<< "at least " << min
<< " ";
432 ss
<< "at most " << max
<< " ";
434 ss
<< "arguments for operator '" << kind
<< "', ";
435 ss
<< "found " << numArgs
;
436 parseError(ss
.str());
440 void Parser::checkOperator(Kind kind
, unsigned numArgs
) throw(ParserException
) {
441 if( d_strictMode
&& d_logicOperators
.find(kind
) == d_logicOperators
.end() ) {
442 parseError( "Operator is not defined in the current logic: " + kindToString(kind
) );
444 checkArity(kind
, numArgs
);
447 void Parser::addOperator(Kind kind
) {
448 d_logicOperators
.insert(kind
);
451 void Parser::preemptCommand(Command
* cmd
) {
452 d_commandQueue
.push_back(cmd
);
455 Command
* Parser::nextCommand() throw(ParserException
) {
456 Debug("parser") << "nextCommand()" << std::endl
;
458 if(!d_commandQueue
.empty()) {
459 cmd
= d_commandQueue
.front();
460 d_commandQueue
.pop_front();
467 cmd
= d_input
->parseCommand();
468 d_commandQueue
.push_back(cmd
);
469 cmd
= d_commandQueue
.front();
470 d_commandQueue
.pop_front();
474 } catch(ParserException
& e
) {
477 } catch(Exception
& e
) {
480 // set the language of the stream, otherwise if it contains
481 // Exprs or Types it prints in the AST language
482 OutputLanguage outlang
=
483 language::toOutputLanguage(d_input
->getLanguage());
484 ss
<< Expr::setlanguage(outlang
) << e
;
485 parseError( ss
.str() );
489 Debug("parser") << "nextCommand() => " << cmd
<< std::endl
;
493 Expr
Parser::nextExpression() throw(ParserException
) {
494 Debug("parser") << "nextExpression()" << std::endl
;
498 result
= d_input
->parseExpr();
499 if(result
.isNull()) {
502 } catch(ParserException
& e
) {
505 } catch(Exception
& e
) {
509 parseError( ss
.str() );
512 Debug("parser") << "nextExpression() => " << result
<< std::endl
;
517 }/* CVC4::parser namespace */
518 }/* CVC4 namespace */