00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 #ifndef __FLATZINC_PARSER_HH__
00035 #define __FLATZINC_PARSER_HH__
00036
00037 #include <gecode/flatzinc.hh>
00038
00039
00040
00041 #if defined(_MSC_VER)
00042 #define YY_NO_UNISTD_H
00043 #ifdef __cplusplus
00044 extern "C" int isatty(int);
00045 #endif
00046 #endif
00047
00048
00049
00050 #if defined(_MSC_VER)
00051 #define strdup _strdup
00052 #define fileno _fileno
00053 #endif
00054
00055 #include <string>
00056 #include <vector>
00057 #include <iostream>
00058 #include <algorithm>
00059
00060 #include <gecode/flatzinc/option.hh>
00061 #include <gecode/flatzinc/varspec.hh>
00062 #include <gecode/flatzinc/conexpr.hh>
00063 #include <gecode/flatzinc/ast.hh>
00064 #include <gecode/flatzinc/parser.tab.hh>
00065 #include <gecode/flatzinc/symboltable.hh>
00066
00067 namespace Gecode { namespace FlatZinc {
00068
00069 typedef std::pair<std::string,Option<std::vector<int>* > > intvartype;
00070
00071 class VarSpec;
00072 typedef std::pair<std::string, VarSpec*> varspec;
00073
00075 class OutputOrder {
00076 public:
00078 bool operator ()(const std::pair<std::string,AST::Node*>& x,
00079 const std::pair<std::string,AST::Node*>& y) {
00080 return x.first < y.first;
00081 }
00082 };
00083
00085 enum SymbolType {
00086 ST_INTVAR,
00087 ST_BOOLVAR,
00088 ST_FLOATVAR,
00089 ST_SETVAR,
00090 ST_INTVARARRAY,
00091 ST_BOOLVARARRAY,
00092 ST_SETVARARRAY,
00093 ST_FLOATVARARRAY,
00094 ST_INTVALARRAY,
00095 ST_BOOLVALARRAY,
00096 ST_SETVALARRAY,
00097 ST_FLOATVALARRAY,
00098 ST_INT,
00099 ST_BOOL,
00100 ST_SET,
00101 ST_FLOAT
00102 };
00103
00105 class SymbolEntry {
00106 public:
00107 SymbolType t;
00108 int i;
00110 SymbolEntry(void) {}
00112 SymbolEntry(SymbolType t0, int i0) : t(t0), i(i0) {}
00113 };
00114
00116 forceinline SymbolEntry se_iv(int i) {
00117 return SymbolEntry(ST_INTVAR, i);
00118 }
00120 forceinline SymbolEntry se_bv(int i) {
00121 return SymbolEntry(ST_BOOLVAR, i);
00122 }
00124 forceinline SymbolEntry se_fv(int i) {
00125 return SymbolEntry(ST_FLOATVAR, i);
00126 }
00128 forceinline SymbolEntry se_sv(int i) {
00129 return SymbolEntry(ST_SETVAR, i);
00130 }
00131
00133 forceinline SymbolEntry se_iva(int i) {
00134 return SymbolEntry(ST_INTVARARRAY, i);
00135 }
00137 forceinline SymbolEntry se_bva(int i) {
00138 return SymbolEntry(ST_BOOLVARARRAY, i);
00139 }
00141 forceinline SymbolEntry se_fva(int i) {
00142 return SymbolEntry(ST_FLOATVARARRAY, i);
00143 }
00145 forceinline SymbolEntry se_sva(int i) {
00146 return SymbolEntry(ST_SETVARARRAY, i);
00147 }
00148
00150 forceinline SymbolEntry se_i(int i) {
00151 return SymbolEntry(ST_INT, i);
00152 }
00154 forceinline SymbolEntry se_b(bool b) {
00155 return SymbolEntry(ST_BOOL, b);
00156 }
00158 forceinline SymbolEntry se_s(int i) {
00159 return SymbolEntry(ST_SET, i);
00160 }
00162 forceinline SymbolEntry se_f(int i) {
00163 return SymbolEntry(ST_FLOAT, i);
00164 }
00165
00167 forceinline SymbolEntry se_ia(int i) {
00168 return SymbolEntry(ST_INTVALARRAY, i);
00169 }
00171 forceinline SymbolEntry se_ba(int i) {
00172 return SymbolEntry(ST_BOOLVALARRAY, i);
00173 }
00175 forceinline SymbolEntry se_sa(int i) {
00176 return SymbolEntry(ST_SETVALARRAY, i);
00177 }
00179 forceinline SymbolEntry se_fa(int i) {
00180 return SymbolEntry(ST_FLOATVALARRAY, i);
00181 }
00182
00184 class ParserState {
00185 public:
00186 ParserState(const std::string& b, std::ostream& err0,
00187 Gecode::FlatZinc::FlatZincSpace* fg0)
00188 : buf(b.c_str()), pos(0), length(b.size()), fg(fg0),
00189 hadError(false), err(err0) {}
00190
00191 ParserState(char* buf0, int length0, std::ostream& err0,
00192 Gecode::FlatZinc::FlatZincSpace* fg0)
00193 : buf(buf0), pos(0), length(length0), fg(fg0),
00194 hadError(false), err(err0) {}
00195
00196 void* yyscanner;
00197 const char* buf;
00198 unsigned int pos, length;
00199 Gecode::FlatZinc::FlatZincSpace* fg;
00200 std::vector<std::pair<std::string,AST::Node*> > _output;
00201
00202 SymbolTable<SymbolEntry> symbols;
00203
00204 std::vector<varspec> intvars;
00205 std::vector<varspec> boolvars;
00206 std::vector<varspec> setvars;
00207 std::vector<varspec> floatvars;
00208 std::vector<int> arrays;
00209 std::vector<AST::SetLit> setvals;
00210 std::vector<double> floatvals;
00211 std::vector<ConExpr*> constraints;
00212
00213 std::vector<ConExpr*> domainConstraints;
00214
00215 bool hadError;
00216 std::ostream& err;
00217
00218 int fillBuffer(char* lexBuf, unsigned int lexBufSize) {
00219 if (pos >= length)
00220 return 0;
00221 int num = std::min(length - pos, lexBufSize);
00222 memcpy(lexBuf,buf+pos,num);
00223 pos += num;
00224 return num;
00225 }
00226
00227 void output(std::string x, AST::Node* n) {
00228 _output.push_back(std::pair<std::string,AST::Node*>(x,n));
00229 }
00230
00231 AST::Array* getOutput(void) {
00232 OutputOrder oo;
00233 std::sort(_output.begin(),_output.end(),oo);
00234 AST::Array* a = new AST::Array();
00235 for (unsigned int i=0; i<_output.size(); i++) {
00236 a->a.push_back(new AST::String(_output[i].first+" = "));
00237 if (_output[i].second->isArray()) {
00238 AST::Array* oa = _output[i].second->getArray();
00239 for (unsigned int j=0; j<oa->a.size(); j++) {
00240 a->a.push_back(oa->a[j]);
00241 oa->a[j] = NULL;
00242 }
00243 delete _output[i].second;
00244 } else {
00245 a->a.push_back(_output[i].second);
00246 }
00247 a->a.push_back(new AST::String(";\n"));
00248 }
00249 return a;
00250 }
00251
00252 };
00253
00254 }}
00255
00256 #endif
00257
00258