Generated on Thu Mar 22 10:39:32 2012 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: 2010-07-28 16:43:32 +0200 (Wed, 28 Jul 2010) $ by $Author: tack $
00011  *     $Revision: 11293 $
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_INT,           //< Integer
00102     ST_BOOL,          //< Boolean
00103     ST_SET            //< Set
00104   };
00105 
00107   class SymbolEntry {
00108   public:
00109     SymbolType t; //< Type of entry
00110     int i;        //< Value of entry or array start index
00112     SymbolEntry(void) {}
00114     SymbolEntry(SymbolType t0, int i0) : t(t0), i(i0) {}
00115 
00116   };
00117 
00119   forceinline SymbolEntry se_iv(int i) {
00120     return SymbolEntry(ST_INTVAR, i);
00121   }
00123   forceinline SymbolEntry se_bv(int i) {
00124     return SymbolEntry(ST_BOOLVAR, i);
00125   }
00127   forceinline SymbolEntry se_fv(int i) {
00128     return SymbolEntry(ST_FLOATVAR, i);
00129   }
00131   forceinline SymbolEntry se_sv(int i) {
00132     return SymbolEntry(ST_SETVAR, i);
00133   }
00134 
00136   forceinline SymbolEntry se_iva(int i) {
00137     return SymbolEntry(ST_INTVARARRAY, i);
00138   }
00140   forceinline SymbolEntry se_bva(int i) {
00141     return SymbolEntry(ST_BOOLVARARRAY, i);
00142   }
00144   forceinline SymbolEntry se_fva(int i) {
00145     return SymbolEntry(ST_FLOATVARARRAY, i);
00146   }
00148   forceinline SymbolEntry se_sva(int i) {
00149     return SymbolEntry(ST_SETVARARRAY, i);
00150   }
00151 
00153   forceinline SymbolEntry se_i(int i) {
00154     return SymbolEntry(ST_INT, i);
00155   }
00157   forceinline SymbolEntry se_b(bool b) {
00158     return SymbolEntry(ST_BOOL, b);
00159   }
00161   forceinline SymbolEntry se_s(int i) {
00162     return SymbolEntry(ST_SET, i);
00163   }
00164 
00166   forceinline SymbolEntry se_ia(int i) {
00167     return SymbolEntry(ST_INTVALARRAY, i);
00168   }
00170   forceinline SymbolEntry se_ba(int i) {
00171     return SymbolEntry(ST_BOOLVALARRAY, i);
00172   }
00174   forceinline SymbolEntry se_sa(int i) {
00175     return SymbolEntry(ST_SETVALARRAY, i);
00176   }
00177 
00179   class ParserState {
00180   public:
00181     ParserState(const std::string& b, std::ostream& err0,
00182                 Gecode::FlatZinc::FlatZincSpace* fg0)
00183     : buf(b.c_str()), pos(0), length(b.size()), fg(fg0),
00184       hadError(false), err(err0) {}
00185 
00186     ParserState(char* buf0, int length0, std::ostream& err0,
00187                 Gecode::FlatZinc::FlatZincSpace* fg0)
00188     : buf(buf0), pos(0), length(length0), fg(fg0),
00189       hadError(false), err(err0) {}
00190   
00191     void* yyscanner;
00192     const char* buf;
00193     unsigned int pos, length;
00194     Gecode::FlatZinc::FlatZincSpace* fg;
00195     std::vector<std::pair<std::string,AST::Node*> > _output;
00196 
00197     SymbolTable<SymbolEntry> symbols;
00198 
00199     // SymbolTable<int> intvarTable;
00200     // SymbolTable<int> boolvarTable;
00201     // SymbolTable<int> floatvarTable;
00202     // SymbolTable<int> setvarTable;
00203     // SymbolTable<std::vector<int> > intvararrays;
00204     // SymbolTable<std::vector<int> > boolvararrays;
00205     // SymbolTable<std::vector<int> > floatvararrays;
00206     // SymbolTable<std::vector<int> > setvararrays;
00207     // SymbolTable<std::vector<int> > intvalarrays;
00208     // SymbolTable<std::vector<int> > boolvalarrays;
00209     // SymbolTable<int> intvals;
00210     // SymbolTable<bool> boolvals;
00211     // SymbolTable<AST::SetLit> setvals;
00212     // SymbolTable<std::vector<AST::SetLit> > setvalarrays;
00213 
00214     std::vector<varspec> intvars;
00215     std::vector<varspec> boolvars;
00216     std::vector<varspec> setvars;
00217     std::vector<int> arrays;
00218     std::vector<AST::SetLit> setvals;
00219 
00220     std::vector<ConExpr*> domainConstraints;
00221 
00222     bool hadError;
00223     std::ostream& err;
00224   
00225     int fillBuffer(char* lexBuf, unsigned int lexBufSize) {
00226       if (pos >= length)
00227         return 0;
00228       int num = std::min(length - pos, lexBufSize);
00229       memcpy(lexBuf,buf+pos,num);
00230       pos += num;
00231       return num;    
00232     }
00233 
00234     void output(std::string x, AST::Node* n) {
00235       _output.push_back(std::pair<std::string,AST::Node*>(x,n));
00236     }
00237     
00238     AST::Array* getOutput(void) {
00239       OutputOrder oo;
00240       std::sort(_output.begin(),_output.end(),oo);
00241       AST::Array* a = new AST::Array();
00242       for (unsigned int i=0; i<_output.size(); i++) {
00243         a->a.push_back(new AST::String(_output[i].first+" = "));
00244         if (_output[i].second->isArray()) {
00245           AST::Array* oa = _output[i].second->getArray();
00246           for (unsigned int j=0; j<oa->a.size(); j++) {
00247             a->a.push_back(oa->a[j]);
00248             oa->a[j] = NULL;
00249           }
00250           delete _output[i].second;
00251         } else {
00252           a->a.push_back(_output[i].second);
00253         }
00254         a->a.push_back(new AST::String(";\n"));
00255       }
00256       return a;
00257     }
00258   
00259   };
00260 
00261 }}
00262 
00263 #endif
00264 
00265 // STATISTICS: flatzinc-any