Adding Integer and Rational constants to SMT
authorChristopher L. Conway <christopherleeconway@gmail.com>
Tue, 27 Apr 2010 20:44:47 +0000 (20:44 +0000)
committerChristopher L. Conway <christopherleeconway@gmail.com>
Tue, 27 Apr 2010 20:44:47 +0000 (20:44 +0000)
src/parser/antlr_input.cpp
src/parser/antlr_input.h
src/parser/parser_state.cpp
src/parser/smt/Smt.g
test/unit/parser/parser_white.h

index b8caf5dede7eb8e8cf84c4ef5b580421bea5f2b6..47420a015e15f47ca8264de89e3019a2eca96d14 100644 (file)
@@ -91,6 +91,16 @@ pANTLR3_COMMON_TOKEN_STREAM AntlrInput::getTokenStream() {
   return d_tokenStream;
 }
 
+void AntlrInput::lexerError(pANTLR3_BASE_RECOGNIZER recognizer) {
+  pANTLR3_LEXER lexer = (pANTLR3_LEXER)(recognizer->super);
+  AlwaysAssert(lexer!=NULL);
+  ParserState *parserState = (ParserState*)(lexer->super);
+  AlwaysAssert(parserState!=NULL);
+
+  // Call the error display routine
+  parserState->parseError("Error finding next token.");
+}
+
 void AntlrInput::parseError(const std::string& message)
     throw (ParserException) {
   Debug("parser") << "Throwing exception: "
@@ -125,6 +135,11 @@ void AntlrInput::setLexer(pANTLR3_LEXER pLexer) {
   }
 
   d_tokenStream = buffer->commonTstream;
+
+  // ANTLR isn't using super, AFAICT.
+  d_lexer->super = getParserState();
+  // Override default lexer error reporting
+  d_lexer->rec->reportError = &lexerError;
 }
 
 void AntlrInput::setParser(pANTLR3_PARSER pParser) {
index dee7c1491096727a38aa3c0967a9338f7c9e4f1b..a36853d7bc0234a1881d0942a36a284e7b592023 100644 (file)
@@ -67,6 +67,9 @@ class AntlrInput : public Input {
   /** Turns an ANTLR3 exception into a message for the user and calls <code>parseError</code>. */
   static void reportError(pANTLR3_BASE_RECOGNIZER recognizer);
 
+  /** Builds a message for a lexer error and calls <code>parseError</code>. */
+  static void lexerError(pANTLR3_BASE_RECOGNIZER recognizer);
+
 public:
 
   /** Create a file input.
index 827d5fcaa45705d42142d8ed851d050740c6040b..204dd3469a85b31af747967415c235e3bab0fddc 100644 (file)
@@ -115,15 +115,6 @@ ParserState::defineVar(const std::string& name, const Expr& val) {
   Assert(isDeclared(name));
 }
 
-/*
-void
-ParserState::undefineVar(const std::string& name) {
-  Assert(isDeclared(name));
-  d_declScope.unbind(name);
-  Assert(!isDeclared(name));
-}
-*/
-
 Type
 ParserState::mkSort(const std::string& name) {
   Debug("parser") << "newSort(" << name << ")" << std::endl;
index 67290ada28d346fb8820f7f54b1faef0126d5c7a..83e3447ac0699ce0c19ad887aaa43b8401e43070 100644 (file)
@@ -61,7 +61,9 @@ namespace CVC4 {
 #include "expr/type.h"
 #include "parser/antlr_input.h"
 #include "parser/parser_state.h"
+#include "util/integer.h"
 #include "util/output.h"
+#include "util/rational.h"
 #include <vector>
 
 using namespace CVC4;
@@ -229,6 +231,13 @@ annotatedFormula[CVC4::Expr& expr]
     /* constants */
   | TRUE_TOK          { expr = MK_CONST(true); }
   | FALSE_TOK         { expr = MK_CONST(false); }
+  | NUMERAL_TOK
+    { Integer num( AntlrInput::tokenText($NUMERAL_TOK) );
+      expr = MK_CONST(num); }
+  | RATIONAL_TOK
+    { // FIXME: This doesn't work because an SMT rational is not a valid GMP rational string
+      Rational rat( AntlrInput::tokenText($RATIONAL_TOK) );
+      expr = MK_CONST(rat); }
     // NOTE: Theory constants go here
     /* TODO: let, flet, quantifiers, arithmetic constants */
   ;
@@ -525,7 +534,11 @@ WHITESPACE
  * Matches a numeral from the input (non-empty sequence of digits).
  */
 NUMERAL_TOK
-  :  (DIGIT)+
+  : DIGIT+
+  ;
+
+RATIONAL_TOK
+  : DIGIT+ '.' DIGIT+
   ;
 
 /**
@@ -557,7 +570,8 @@ ALPHA
  * Matches the digits (0-9)
  */
 fragment DIGIT :   '0'..'9';
-
+// fragment NON_ZERO_DIGIT : '1'..'9';
+// fragment NUMERAL_SEQ : '0' | NON_ZERO_DIGIT DIGIT*;
 
 /**
  * Matches an allowed escaped character.
index 317817e1572854ad9695e51cca32cac269444d0c..1d19525d6c9274889d103ed281b19fcb7463f73d 100644 (file)
@@ -109,7 +109,10 @@ const string goodSmtExprs[] = {
     "(and (iff a b) (not a))",
     "(iff (xor a b) (and (or a b) (not (and a b))))",
     "(ite a (f x) y)",
-    "(if_then_else a (f x) y)"
+    "(if_then_else a (f x) y)",
+    "1",
+    "0"// ,
+//    "1.5"
 };
 
 const int numGoodSmtExprs = sizeof(goodSmtExprs) / sizeof(string);
@@ -134,7 +137,9 @@ const string badSmtExprs[] = {
     "(not a b)", // wrong arity
     "not a", // needs parens
     "(ite a x)", // wrong arity
-    "(a b)" // using non-function as function
+    "(a b)", // using non-function as function
+    ".5", // rational constants must have integer prefix
+    "1." // rational constants must have fractional suffix
 };
 
 const int numBadSmtExprs = sizeof(badSmtExprs) / sizeof(string);