Generated on Sat May 25 2013 18:00:32 for Gecode by doxygen 1.8.3.1
parser.hh
Go to the documentation of this file.
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  * Last modified:
10  * $Date: 2012-03-30 05:58:02 +0200 (Fri, 30 Mar 2012) $ by $Author: tack $
11  * $Revision: 12665 $
12  *
13  * This file is part of Gecode, the generic constraint
14  * development environment:
15  * http://www.gecode.org
16  *
17  * Permission is hereby granted, free of charge, to any person obtaining
18  * a copy of this software and associated documentation files (the
19  * "Software"), to deal in the Software without restriction, including
20  * without limitation the rights to use, copy, modify, merge, publish,
21  * distribute, sublicense, and/or sell copies of the Software, and to
22  * permit persons to whom the Software is furnished to do so, subject to
23  * the following conditions:
24  *
25  * The above copyright notice and this permission notice shall be
26  * included in all copies or substantial portions of the Software.
27  *
28  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
29  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
30  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
31  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
32  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
33  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
34  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35  *
36  */
37 
38 #ifndef __FLATZINC_PARSER_HH__
39 #define __FLATZINC_PARSER_HH__
40 
41 #include <gecode/flatzinc.hh>
42 
43 // This is a workaround for a bug in flex that only shows up
44 // with the Microsoft C++ compiler
45 #if defined(_MSC_VER)
46 #define YY_NO_UNISTD_H
47 #ifdef __cplusplus
48 extern "C" int isatty(int);
49 #endif
50 #endif
51 
52 // The Microsoft C++ compiler marks certain functions as deprecated,
53 // so let's take the alternative definitions
54 #if defined(_MSC_VER)
55 #define strdup _strdup
56 #define fileno _fileno
57 #endif
58 
59 #include <string>
60 #include <vector>
61 #include <iostream>
62 #include <algorithm>
63 
67 #include <gecode/flatzinc/ast.hh>
68 #include <gecode/flatzinc/parser.tab.hh>
70 
71 namespace Gecode { namespace FlatZinc {
72 
73  typedef std::pair<std::string,Option<std::vector<int>* > > intvartype;
74 
75  class VarSpec;
76  typedef std::pair<std::string, VarSpec*> varspec;
77 
79  class OutputOrder {
80  public:
82  bool operator ()(const std::pair<std::string,AST::Node*>& x,
83  const std::pair<std::string,AST::Node*>& y) {
84  return x.first < y.first;
85  }
86  };
87 
89  enum SymbolType {
90  ST_INTVAR, //< Integer variable
91  ST_BOOLVAR, //< Boolean variable
92  ST_FLOATVAR, //< Float variable
93  ST_SETVAR, //< Set variable
94  ST_INTVARARRAY, //< Integer variable array
95  ST_BOOLVARARRAY, //< Boolean variable array
96  ST_SETVARARRAY, //< Set variable array
97  ST_FLOATVARARRAY, //< Float variable array
98  ST_INTVALARRAY, //< Integer array
99  ST_BOOLVALARRAY, //< Boolean array
100  ST_SETVALARRAY, //< Set array
101  ST_FLOATVALARRAY, //< Float array
102  ST_INT, //< Integer
103  ST_BOOL, //< Boolean
104  ST_SET, //< Set
105  ST_FLOAT //< Float
106  };
107 
109  class SymbolEntry {
110  public:
111  SymbolType t; //< Type of entry
112  int i; //< Value of entry or array start index
114  SymbolEntry(void) {}
116  SymbolEntry(SymbolType t0, int i0) : t(t0), i(i0) {}
117  };
118 
121  return SymbolEntry(ST_INTVAR, i);
122  }
125  return SymbolEntry(ST_BOOLVAR, i);
126  }
129  return SymbolEntry(ST_FLOATVAR, i);
130  }
133  return SymbolEntry(ST_SETVAR, i);
134  }
135 
138  return SymbolEntry(ST_INTVARARRAY, i);
139  }
142  return SymbolEntry(ST_BOOLVARARRAY, i);
143  }
146  return SymbolEntry(ST_FLOATVARARRAY, i);
147  }
150  return SymbolEntry(ST_SETVARARRAY, i);
151  }
152 
155  return SymbolEntry(ST_INT, i);
156  }
159  return SymbolEntry(ST_BOOL, b);
160  }
163  return SymbolEntry(ST_SET, i);
164  }
167  return SymbolEntry(ST_FLOAT, i);
168  }
169 
172  return SymbolEntry(ST_INTVALARRAY, i);
173  }
176  return SymbolEntry(ST_BOOLVALARRAY, i);
177  }
180  return SymbolEntry(ST_SETVALARRAY, i);
181  }
184  return SymbolEntry(ST_FLOATVALARRAY, i);
185  }
186 
188  class ParserState {
189  public:
190  ParserState(const std::string& b, std::ostream& err0,
192  : buf(b.c_str()), pos(0), length(b.size()), fg(fg0),
193  hadError(false), err(err0) {}
194 
195  ParserState(char* buf0, int length0, std::ostream& err0,
197  : buf(buf0), pos(0), length(length0), fg(fg0),
198  hadError(false), err(err0) {}
199 
200  void* yyscanner;
201  const char* buf;
202  unsigned int pos, length;
204  std::vector<std::pair<std::string,AST::Node*> > _output;
205 
207 
208  std::vector<varspec> intvars;
209  std::vector<varspec> boolvars;
210  std::vector<varspec> setvars;
211  std::vector<varspec> floatvars;
212  std::vector<int> arrays;
213  std::vector<AST::SetLit> setvals;
214  std::vector<double> floatvals;
215 
216  std::vector<ConExpr*> domainConstraints;
217 
218  bool hadError;
219  std::ostream& err;
220 
221  int fillBuffer(char* lexBuf, unsigned int lexBufSize) {
222  if (pos >= length)
223  return 0;
224  int num = std::min(length - pos, lexBufSize);
225  memcpy(lexBuf,buf+pos,num);
226  pos += num;
227  return num;
228  }
229 
230  void output(std::string x, AST::Node* n) {
231  _output.push_back(std::pair<std::string,AST::Node*>(x,n));
232  }
233 
235  OutputOrder oo;
236  std::sort(_output.begin(),_output.end(),oo);
237  AST::Array* a = new AST::Array();
238  for (unsigned int i=0; i<_output.size(); i++) {
239  a->a.push_back(new AST::String(_output[i].first+" = "));
240  if (_output[i].second->isArray()) {
241  AST::Array* oa = _output[i].second->getArray();
242  for (unsigned int j=0; j<oa->a.size(); j++) {
243  a->a.push_back(oa->a[j]);
244  oa->a[j] = NULL;
245  }
246  delete _output[i].second;
247  } else {
248  a->a.push_back(_output[i].second);
249  }
250  a->a.push_back(new AST::String(";\n"));
251  }
252  return a;
253  }
254 
255  };
256 
257 }}
258 
259 #endif
260 
261 // STATISTICS: flatzinc-any