this repo has no description
1/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */ 2/* 3 * Main authors: 4 * Guido Tack <tack@gecode.org> 5 * 6 * Copyright: 7 * Guido Tack, 2007 8 * 9 * This file is part of Gecode, the generic constraint 10 * development environment: 11 * http://www.gecode.org 12 * 13 * Permission is hereby granted, free of charge, to any person obtaining 14 * a copy of this software and associated documentation files (the 15 * "Software"), to deal in the Software without restriction, including 16 * without limitation the rights to use, copy, modify, merge, publish, 17 * distribute, sublicense, and/or sell copies of the Software, and to 18 * permit persons to whom the Software is furnished to do so, subject to 19 * the following conditions: 20 * 21 * The above copyright notice and this permission notice shall be 22 * included in all copies or substantial portions of the Software. 23 * 24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 27 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 28 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 29 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 30 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 31 * 32 */ 33 34#ifndef FLATZINC_PARSER_HH 35#define FLATZINC_PARSER_HH 36 37#include <gecode/flatzinc.hh> 38 39// This is a workaround for a bug in flex that only shows up 40// with the Microsoft C++ compiler 41#if defined(_MSC_VER) 42#define YY_NO_UNISTD_H 43#ifdef __cplusplus 44extern "C" int isatty(int); 45#endif 46#endif 47 48// The Microsoft C++ compiler marks certain functions as deprecated, 49// so let's take the alternative definitions 50#if defined(_MSC_VER) 51#define strdup _strdup 52#define fileno _fileno 53#endif 54 55#include <string> 56#include <vector> 57#include <iostream> 58#include <algorithm> 59 60#include <gecode/flatzinc/option.hh> 61#include <gecode/flatzinc/varspec.hh> 62#include <gecode/flatzinc/conexpr.hh> 63#include <gecode/flatzinc/ast.hh> 64#include <gecode/flatzinc/parser.tab.hpp> 65#include <gecode/flatzinc/symboltable.hh> 66 67namespace Gecode { namespace FlatZinc { 68 69 typedef std::pair<std::string,Option<std::vector<int>* > > intvartype; 70 71 class VarSpec; 72 typedef std::pair<std::string, VarSpec*> varspec; 73 74 /// Strict weak ordering for output items 75 class OutputOrder { 76 public: 77 /// Return if \a x is less than \a y, based on first component 78 bool operator ()(const std::pair<std::string,AST::Node*>& x, 79 const std::pair<std::string,AST::Node*>& y) { 80 return x.first < y.first; 81 } 82 }; 83 84 /// Types of symbols 85 enum SymbolType { 86 ST_INTVAR, //< Integer variable 87 ST_BOOLVAR, //< Boolean variable 88 ST_FLOATVAR, //< Float variable 89 ST_SETVAR, //< Set variable 90 ST_INTVARARRAY, //< Integer variable array 91 ST_BOOLVARARRAY, //< Boolean variable array 92 ST_SETVARARRAY, //< Set variable array 93 ST_FLOATVARARRAY, //< Float variable array 94 ST_INTVALARRAY, //< Integer array 95 ST_BOOLVALARRAY, //< Boolean array 96 ST_SETVALARRAY, //< Set array 97 ST_FLOATVALARRAY, //< Float array 98 ST_INT, //< Integer 99 ST_BOOL, //< Boolean 100 ST_SET, //< Set 101 ST_FLOAT //< Float 102 }; 103 104 /// Entries in the symbol table 105 class SymbolEntry { 106 public: 107 SymbolType t; //< Type of entry 108 int i; //< Value of entry or array start index 109 /// Default constructor 110 SymbolEntry(void) {} 111 /// Constructor 112 SymbolEntry(SymbolType t0, int i0) : t(t0), i(i0) {} 113 }; 114 115 /// Construct integer variable entry 116 forceinline SymbolEntry se_iv(int i) { 117 return SymbolEntry(ST_INTVAR, i); 118 } 119 /// Construct Boolean variable entry 120 forceinline SymbolEntry se_bv(int i) { 121 return SymbolEntry(ST_BOOLVAR, i); 122 } 123 /// Construct float variable entry 124 forceinline SymbolEntry se_fv(int i) { 125 return SymbolEntry(ST_FLOATVAR, i); 126 } 127 /// Construct set variable entry 128 forceinline SymbolEntry se_sv(int i) { 129 return SymbolEntry(ST_SETVAR, i); 130 } 131 132 /// Construct integer variable array entry 133 forceinline SymbolEntry se_iva(int i) { 134 return SymbolEntry(ST_INTVARARRAY, i); 135 } 136 /// Construct Boolean variable array entry 137 forceinline SymbolEntry se_bva(int i) { 138 return SymbolEntry(ST_BOOLVARARRAY, i); 139 } 140 /// Construct float variable array entry 141 forceinline SymbolEntry se_fva(int i) { 142 return SymbolEntry(ST_FLOATVARARRAY, i); 143 } 144 /// Construct set variable array entry 145 forceinline SymbolEntry se_sva(int i) { 146 return SymbolEntry(ST_SETVARARRAY, i); 147 } 148 149 /// Construct integer entry 150 forceinline SymbolEntry se_i(int i) { 151 return SymbolEntry(ST_INT, i); 152 } 153 /// Construct Boolean entry 154 forceinline SymbolEntry se_b(bool b) { 155 return SymbolEntry(ST_BOOL, b); 156 } 157 /// Construct set entry 158 forceinline SymbolEntry se_s(int i) { 159 return SymbolEntry(ST_SET, i); 160 } 161 /// Construct float entry 162 forceinline SymbolEntry se_f(int i) { 163 return SymbolEntry(ST_FLOAT, i); 164 } 165 166 /// Construct integer array entry 167 forceinline SymbolEntry se_ia(int i) { 168 return SymbolEntry(ST_INTVALARRAY, i); 169 } 170 /// Construct Boolean array entry 171 forceinline SymbolEntry se_ba(int i) { 172 return SymbolEntry(ST_BOOLVALARRAY, i); 173 } 174 /// Construct set array entry 175 forceinline SymbolEntry se_sa(int i) { 176 return SymbolEntry(ST_SETVALARRAY, i); 177 } 178 /// Construct float array entry 179 forceinline SymbolEntry se_fa(int i) { 180 return SymbolEntry(ST_FLOATVALARRAY, i); 181 } 182 183 /// %State of the %FlatZinc parser 184 class ParserState { 185 public: 186 ParserState(const std::string& b, std::ostream& err0, 187 Gecode::FlatZinc::FlatZincSpace* fg0) 188 : buf(b.c_str()), pos(0), length(b.size()), fg(fg0), 189 hadError(false), err(err0) {} 190 191 ParserState(char* buf0, int length0, std::ostream& err0, 192 Gecode::FlatZinc::FlatZincSpace* fg0) 193 : buf(buf0), pos(0), length(length0), fg(fg0), 194 hadError(false), err(err0) {} 195 196 void* yyscanner; 197 const char* buf; 198 unsigned int pos, length; 199 Gecode::FlatZinc::FlatZincSpace* fg; 200 std::vector<std::pair<std::string,AST::Node*> > _output; 201 202 SymbolTable<SymbolEntry> symbols; 203 204 std::vector<varspec> intvars; 205 std::vector<varspec> boolvars; 206 std::vector<varspec> setvars; 207 std::vector<varspec> floatvars; 208 std::vector<int> arrays; 209 std::vector<AST::SetLit> setvals; 210 std::vector<double> floatvals; 211 std::vector<ConExpr*> constraints; 212 213 std::vector<ConExpr*> domainConstraints; 214 215 int status_idx = -1; 216 std::vector<std::array<int, 3>> int_uniform; 217 std::vector<std::array<int, 2>> int_sol; 218 std::vector<std::array<int, 2>> int_lastval; 219 220 bool hadError; 221 std::ostream& err; 222 223 int fillBuffer(char* lexBuf, unsigned int lexBufSize) { 224 if (pos >= length) 225 return 0; 226 int num = std::min(length - pos, lexBufSize); 227 memcpy(lexBuf,buf+pos,num); 228 pos += num; 229 return num; 230 } 231 232 void output(std::string x, AST::Node* n) { 233 _output.push_back(std::pair<std::string,AST::Node*>(x,n)); 234 } 235 236 AST::Array* getOutput(void) { 237 OutputOrder oo; 238 std::sort(_output.begin(),_output.end(),oo); 239 AST::Array* a = new AST::Array(); 240 for (unsigned int i=0; i<_output.size(); i++) { 241 a->a.push_back(new AST::String(_output[i].first+" = ")); 242 if (_output[i].second->isArray()) { 243 AST::Array* oa = _output[i].second->getArray(); 244 for (unsigned int j=0; j<oa->a.size(); j++) { 245 a->a.push_back(oa->a[j]); 246 oa->a[j] = nullptr; 247 } 248 delete _output[i].second; 249 } else { 250 a->a.push_back(_output[i].second); 251 } 252 a->a.push_back(new AST::String(";\n")); 253 } 254 return a; 255 } 256 257 }; 258 259}} 260 261#endif 262 263// STATISTICS: flatzinc-any