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-06-17 13:45:55 +0200 (Thu, 17 Jun 2010) $ by $Author: tack $ 00011 * $Revision: 11083 $ 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 class ParserState { 00090 public: 00091 ParserState(const std::string& b, std::ostream& err0, 00092 Gecode::FlatZinc::FlatZincSpace* fg0) 00093 : buf(b.c_str()), pos(0), length(b.size()), fg(fg0), 00094 hadError(false), err(err0) {} 00095 00096 ParserState(char* buf0, int length0, std::ostream& err0, 00097 Gecode::FlatZinc::FlatZincSpace* fg0) 00098 : buf(buf0), pos(0), length(length0), fg(fg0), 00099 hadError(false), err(err0) {} 00100 00101 void* yyscanner; 00102 const char* buf; 00103 unsigned int pos, length; 00104 Gecode::FlatZinc::FlatZincSpace* fg; 00105 std::vector<std::pair<std::string,AST::Node*> > _output; 00106 00107 SymbolTable<int> intvarTable; 00108 SymbolTable<int> boolvarTable; 00109 SymbolTable<int> floatvarTable; 00110 SymbolTable<int> setvarTable; 00111 SymbolTable<std::vector<int> > intvararrays; 00112 SymbolTable<std::vector<int> > boolvararrays; 00113 SymbolTable<std::vector<int> > floatvararrays; 00114 SymbolTable<std::vector<int> > setvararrays; 00115 SymbolTable<std::vector<int> > intvalarrays; 00116 SymbolTable<std::vector<int> > boolvalarrays; 00117 SymbolTable<int> intvals; 00118 SymbolTable<bool> boolvals; 00119 SymbolTable<AST::SetLit> setvals; 00120 SymbolTable<std::vector<AST::SetLit> > setvalarrays; 00121 00122 std::vector<varspec> intvars; 00123 std::vector<varspec> boolvars; 00124 std::vector<varspec> setvars; 00125 00126 std::vector<ConExpr*> domainConstraints; 00127 00128 bool hadError; 00129 std::ostream& err; 00130 00131 int fillBuffer(char* lexBuf, unsigned int lexBufSize) { 00132 if (pos >= length) 00133 return 0; 00134 int num = std::min(length - pos, lexBufSize); 00135 memcpy(lexBuf,buf+pos,num); 00136 pos += num; 00137 return num; 00138 } 00139 00140 void output(std::string x, AST::Node* n) { 00141 _output.push_back(std::pair<std::string,AST::Node*>(x,n)); 00142 } 00143 00144 AST::Array* getOutput(void) { 00145 OutputOrder oo; 00146 std::sort(_output.begin(),_output.end(),oo); 00147 AST::Array* a = new AST::Array(); 00148 for (unsigned int i=0; i<_output.size(); i++) { 00149 a->a.push_back(new AST::String(_output[i].first+" = ")); 00150 if (_output[i].second->isArray()) { 00151 AST::Array* oa = _output[i].second->getArray(); 00152 for (unsigned int j=0; j<oa->a.size(); j++) { 00153 a->a.push_back(oa->a[j]); 00154 oa->a[j] = NULL; 00155 } 00156 delete _output[i].second; 00157 } else { 00158 a->a.push_back(_output[i].second); 00159 } 00160 a->a.push_back(new AST::String(";\n")); 00161 } 00162 return a; 00163 } 00164 00165 }; 00166 00167 }} 00168 00169 #endif 00170 00171 // STATISTICS: flatzinc-any