parser.h
Go to the documentation of this file.
00001 // 
00002 // Copyright (c) Benjamin Kaufmann
00003 // 
00004 // This file is part of Clasp. See http://www.cs.uni-potsdam.de/clasp/ 
00005 // 
00006 // Clasp is free software; you can redistribute it and/or modify
00007 // it under the terms of the GNU General Public License as published by
00008 // the Free Software Foundation; either version 2 of the License, or
00009 // (at your option) any later version.
00010 // 
00011 // Clasp is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 // GNU General Public License for more details.
00015 // 
00016 // You should have received a copy of the GNU General Public License
00017 // along with Clasp; if not, write to the Free Software
00018 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00019 //
00020 #ifndef CLASP_PARSER_H_INCLUDED
00021 #define CLASP_PARSER_H_INCLUDED
00022 
00023 #ifdef _MSC_VER
00024 #pragma once
00025 #endif
00026 
00027 #include <istream>
00028 #include <stdexcept>
00029 #include <clasp/literal.h>
00030 #include <clasp/claspfwd.h>
00035 namespace Clasp {
00036 
00038 struct ParseError : public ClaspError {
00039         ParseError(unsigned line, const char* msg);
00040         unsigned line;
00041 };
00042 
00043 struct Input_t {
00045         static InputFormat detectFormat(std::istream& prg);
00046 
00048 
00053         static bool parseLparse(std::istream& prg, Asp::LogicProgram& api);
00054 
00056 
00061         static bool parseDimacs(std::istream& prg, SatBuilder& api);
00062         
00064 
00069         static bool parseOPB(std::istream& prg, PBBuilder& api);
00070 };
00072 // PARSING BASE
00074 class StreamParser {
00075 public:
00076         StreamParser();
00077         virtual ~StreamParser();
00078         bool parse(StreamSource& prg);
00079 protected:
00080         StreamSource* input() const { return source_;  }
00081         bool          check(bool cond, const char* condError) const;
00082         virtual bool  doParse() = 0;
00083         bool          skipComments(const char* commentStr);
00084 private:
00085         StreamSource* source_;
00086 };
00088 // LPARSE PARSING
00090 class LparseParser : public StreamParser {
00091 public:
00092         explicit LparseParser(Asp::LogicProgram& prg);
00093         void setProgram(Asp::LogicProgram& prg);
00094 protected:
00095         virtual bool doParse();
00096         virtual bool parseRuleExtension(int ruleType) = 0;
00097         virtual bool endParse();
00098         Var  parseAtom();
00099         bool parseBody(uint32 lits, uint32 neg, bool weights);
00100         Asp::LogicProgram* builder() const { return builder_; }
00101         Asp::Rule*         active()  const { return active_;  }
00102         bool               addRule(const Asp::Rule& r) const;
00103 private:
00104         bool parseRules();
00105         bool parseRule(int ruleType);
00106         bool parseSymbolTable();
00107         bool parseComputeStatement();
00108         bool parseModels();
00109         bool knownRuleType(int rt) { return rt > 0 && rt <= 8 && rt != 4 && rt != 7; }
00110         Asp::LogicProgram* builder_;
00111         Asp::Rule*         active_;
00112 };
00113 class DefaultLparseParser : public LparseParser {
00114 public:
00115         DefaultLparseParser(Asp::LogicProgram& prg);
00116 protected:
00117         // Throws ParseError
00118         bool parseRuleExtension(int ruleType);
00119 };
00121 // DIMACS PARSING
00123 class DimacsParser : public StreamParser {
00124 public:
00125         explicit DimacsParser(SatBuilder& prg);
00126         void     setProgram(SatBuilder& prg);
00127 protected:
00128         bool doParse();
00129 private:
00130         void parseHeader();
00131         void parseClauses();
00132         SatBuilder* builder_;
00133         int         numVar_;
00134         bool        wcnf_;
00135 };
00137 // OPB PARSING
00139 class OPBParser : public StreamParser {
00140 public:
00141         explicit OPBParser(PBBuilder& prg);
00142         void setProgram(PBBuilder& prg);
00143 protected:
00144         bool doParse();
00145 private:
00146         void parseHeader();
00147         void parseOptObjective();
00148         void parseConstraint();
00149         void parseSum();
00150         void parseTerm();
00151         PBBuilder* builder_;
00152         weight_t   minCost_;
00153         weight_t   maxCost_;
00154         struct Constraint {
00155                 WeightLitVec lits;
00156                 weight_t     bound;
00157                 bool         eq;
00158         }          active_;
00159         LitVec     term_;
00160 };
00162 // StreamSource
00165 class StreamSource {
00166 public:
00167         explicit StreamSource(std::istream& is);
00169         char operator*() {
00170                 if (buffer_[pos_] == 0) { underflow(); }
00171                 return buffer_[pos_];
00172         }
00174         StreamSource& operator++() { ++pos_; **this; return *this; }
00175         
00177 
00180         bool parseInt(int& val);
00181         bool parseInt64(int64& val);
00182         bool parseInt(int& val, int min, int max);
00183         int  parseInt(int min, int max, const char* err) {
00184                 int x;
00185                 return parseInt(x, min, max) ? x : (error(err ? err : "Integer expected!"), 0);
00186         }
00187 
00189         bool match(char c) { return (**this == c) && (++*this, true); }
00191 
00198         bool matchEol();
00200         bool skipSpace() { while (match(' ') || match('\t')) { ; } return true; }
00202         bool skipWhite() { do { skipSpace(); } while (matchEol()); return true; }
00204         unsigned line() const { return line_; }
00205 
00206         void error(const char* err) const { throw ParseError(line(), err); }
00207 private:
00208         StreamSource(const std::istream&);
00209         StreamSource& operator=(const StreamSource&);
00210         void underflow();
00211         char buffer_[2048];
00212         std::istream& in_;
00213         unsigned pos_;
00214         unsigned line_;
00215 };
00217 inline void skipLine(StreamSource& in) { while (*in && !in.matchEol()) { ++in; } }
00218 
00220 
00229 inline bool match(StreamSource& in, char c, bool sw) { return (!sw || in.skipSpace()) && in.match(c); }
00230 
00232 
00242 inline bool match(StreamSource& in, const char* str, bool sw) {
00243         if (sw) in.skipSpace();
00244         while (*str && in.match(*str)) { ++str; }
00245         return *str == 0;
00246 }
00247 
00248 inline bool matchEol(StreamSource& in, bool sw) {
00249         if (sw) in.skipSpace();
00250         return in.matchEol();
00251 }
00252 
00254 
00259 bool readLine( StreamSource& in, PodVector<char>::type& buf );
00260 
00261 }
00262 #endif


clasp
Author(s): Benjamin Kaufmann
autogenerated on Thu Aug 27 2015 12:41:39