Generated on Tue Apr 18 10:21:32 2017 for Gecode by doxygen 1.6.3

parser.hh

Go to the documentation of this file.
00001 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
00002 /*
00003  *  Main authors:
00004  *     Guido Tack <tack@gecode.org>
00005  *
00006  *  Copyright:
00007  *     Guido Tack, 2007
00008  *
00009  *  Last modified:
00010  *     $Date: 2016-04-19 17:19:45 +0200 (Tue, 19 Apr 2016) $ by $Author: schulte $
00011  *     $Revision: 14967 $
00012  *
00013  *  This file is part of Gecode, the generic constraint
00014  *  development environment:
00015  *     http://www.gecode.org
00016  *
00017  *  Permission is hereby granted, free of charge, to any person obtaining
00018  *  a copy of this software and associated documentation files (the
00019  *  "Software"), to deal in the Software without restriction, including
00020  *  without limitation the rights to use, copy, modify, merge, publish,
00021  *  distribute, sublicense, and/or sell copies of the Software, and to
00022  *  permit persons to whom the Software is furnished to do so, subject to
00023  *  the following conditions:
00024  *
00025  *  The above copyright notice and this permission notice shall be
00026  *  included in all copies or substantial portions of the Software.
00027  *
00028  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00029  *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00030  *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00031  *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
00032  *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
00033  *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
00034  *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00035  *
00036  */
00037 
00038 #ifndef __FLATZINC_PARSER_HH__
00039 #define __FLATZINC_PARSER_HH__
00040 
00041 #include <gecode/flatzinc.hh>
00042 
00043 // This is a workaround for a bug in flex that only shows up
00044 // with the Microsoft C++ compiler
00045 #if defined(_MSC_VER)
00046 #define YY_NO_UNISTD_H
00047 #ifdef __cplusplus
00048 extern "C" int isatty(int);
00049 #endif
00050 #endif
00051 
00052 // The Microsoft C++ compiler marks certain functions as deprecated,
00053 // so let's take the alternative definitions
00054 #if defined(_MSC_VER)
00055 #define strdup _strdup
00056 #define fileno _fileno
00057 #endif
00058 
00059 #include <string>
00060 #include <vector>
00061 #include <iostream>
00062 #include <algorithm>
00063 
00064 #include <gecode/flatzinc/option.hh>
00065 #include <gecode/flatzinc/varspec.hh>
00066 #include <gecode/flatzinc/conexpr.hh>
00067 #include <gecode/flatzinc/ast.hh>
00068 #include <gecode/flatzinc/parser.tab.hh>
00069 #include <gecode/flatzinc/symboltable.hh>
00070 
00071 namespace Gecode { namespace FlatZinc {
00072 
00073   typedef std::pair<std::string,Option<std::vector<int>* > > intvartype;
00074 
00075   class VarSpec;
00076   typedef std::pair<std::string, VarSpec*> varspec;
00077 
00079   class OutputOrder {
00080   public:
00082     bool operator ()(const std::pair<std::string,AST::Node*>& x,
00083                      const std::pair<std::string,AST::Node*>& y) {
00084       return x.first < y.first;
00085     }
00086   };
00087 
00089   enum SymbolType {
00090     ST_INTVAR,        //< Integer variable
00091     ST_BOOLVAR,       //< Boolean variable
00092     ST_FLOATVAR,      //< Float variable
00093     ST_SETVAR,        //< Set variable
00094     ST_INTVARARRAY,   //< Integer variable array
00095     ST_BOOLVARARRAY,  //< Boolean variable array
00096     ST_SETVARARRAY,   //< Set variable array
00097     ST_FLOATVARARRAY, //< Float variable array
00098     ST_INTVALARRAY,   //< Integer array
00099     ST_BOOLVALARRAY,  //< Boolean array
00100     ST_SETVALARRAY,   //< Set array
00101     ST_FLOATVALARRAY, //< Float array
00102     ST_INT,           //< Integer
00103     ST_BOOL,          //< Boolean
00104     ST_SET,           //< Set
00105     ST_FLOAT          //< Float
00106   };
00107 
00109   class SymbolEntry {
00110   public:
00111     SymbolType t; //< Type of entry
00112     int i;        //< Value of entry or array start index
00114     SymbolEntry(void) {}
00116     SymbolEntry(SymbolType t0, int i0) : t(t0), i(i0) {}
00117   };
00118 
00120   forceinline SymbolEntry se_iv(int i) {
00121     return SymbolEntry(ST_INTVAR, i);
00122   }
00124   forceinline SymbolEntry se_bv(int i) {
00125     return SymbolEntry(ST_BOOLVAR, i);
00126   }
00128   forceinline SymbolEntry se_fv(int i) {
00129     return SymbolEntry(ST_FLOATVAR, i);
00130   }
00132   forceinline SymbolEntry se_sv(int i) {
00133     return SymbolEntry(ST_SETVAR, i);
00134   }
00135 
00137   forceinline SymbolEntry se_iva(int i) {
00138     return SymbolEntry(ST_INTVARARRAY, i);
00139   }
00141   forceinline SymbolEntry se_bva(int i) {
00142     return SymbolEntry(ST_BOOLVARARRAY, i);
00143   }
00145   forceinline SymbolEntry se_fva(int i) {
00146     return SymbolEntry(ST_FLOATVARARRAY, i);
00147   }
00149   forceinline SymbolEntry se_sva(int i) {
00150     return SymbolEntry(ST_SETVARARRAY, i);
00151   }
00152 
00154   forceinline SymbolEntry se_i(int i) {
00155     return SymbolEntry(ST_INT, i);
00156   }
00158   forceinline SymbolEntry se_b(bool b) {
00159     return SymbolEntry(ST_BOOL, b);
00160   }
00162   forceinline SymbolEntry se_s(int i) {
00163     return SymbolEntry(ST_SET, i);
00164   }
00166   forceinline SymbolEntry se_f(int i) {
00167     return SymbolEntry(ST_FLOAT, i);
00168   }
00169 
00171   forceinline SymbolEntry se_ia(int i) {
00172     return SymbolEntry(ST_INTVALARRAY, i);
00173   }
00175   forceinline SymbolEntry se_ba(int i) {
00176     return SymbolEntry(ST_BOOLVALARRAY, i);
00177   }
00179   forceinline SymbolEntry se_sa(int i) {
00180     return SymbolEntry(ST_SETVALARRAY, i);
00181   }
00183   forceinline SymbolEntry se_fa(int i) {
00184     return SymbolEntry(ST_FLOATVALARRAY, i);
00185   }
00186 
00188   class ParserState {
00189   public:
00190     ParserState(const std::string& b, std::ostream& err0,
00191                 Gecode::FlatZinc::FlatZincSpace* fg0)
00192     : buf(b.c_str()), pos(0), length(b.size()), fg(fg0),
00193       hadError(false), err(err0) {}
00194 
00195     ParserState(char* buf0, int length0, std::ostream& err0,
00196                 Gecode::FlatZinc::FlatZincSpace* fg0)
00197     : buf(buf0), pos(0), length(length0), fg(fg0),
00198       hadError(false), err(err0) {}
00199 
00200     void* yyscanner;
00201     const char* buf;
00202     unsigned int pos, length;
00203     Gecode::FlatZinc::FlatZincSpace* fg;
00204     std::vector<std::pair<std::string,AST::Node*> > _output;
00205 
00206     SymbolTable<SymbolEntry> symbols;
00207 
00208     std::vector<varspec> intvars;
00209     std::vector<varspec> boolvars;
00210     std::vector<varspec> setvars;
00211     std::vector<varspec> floatvars;
00212     std::vector<int> arrays;
00213     std::vector<AST::SetLit> setvals;
00214     std::vector<double> floatvals;
00215     std::vector<ConExpr*> constraints;
00216 
00217     std::vector<ConExpr*> domainConstraints;
00218 
00219     bool hadError;
00220     std::ostream& err;
00221 
00222     int fillBuffer(char* lexBuf, unsigned int lexBufSize) {
00223       if (pos >= length)
00224         return 0;
00225       int num = std::min(length - pos, lexBufSize);
00226       memcpy(lexBuf,buf+pos,num);
00227       pos += num;
00228       return num;
00229     }
00230 
00231     void output(std::string x, AST::Node* n) {
00232       _output.push_back(std::pair<std::string,AST::Node*>(x,n));
00233     }
00234 
00235     AST::Array* getOutput(void) {
00236       OutputOrder oo;
00237       std::sort(_output.begin(),_output.end(),oo);
00238       AST::Array* a = new AST::Array();
00239       for (unsigned int i=0; i<_output.size(); i++) {
00240         a->a.push_back(new AST::String(_output[i].first+" = "));
00241         if (_output[i].second->isArray()) {
00242           AST::Array* oa = _output[i].second->getArray();
00243           for (unsigned int j=0; j<oa->a.size(); j++) {
00244             a->a.push_back(oa->a[j]);
00245             oa->a[j] = NULL;
00246           }
00247           delete _output[i].second;
00248         } else {
00249           a->a.push_back(_output[i].second);
00250         }
00251         a->a.push_back(new AST::String(";\n"));
00252       }
00253       return a;
00254     }
00255 
00256   };
00257 
00258 }}
00259 
00260 #endif
00261 
00262 // STATISTICS: flatzinc-any