Generated on Fri Aug 24 2012 04:52:06 for Gecode by doxygen 1.8.1.2
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: 2010-07-29 00:43:32 +1000 (Thu, 29 Jul 2010) $ by $Author: tack $
11  * $Revision: 11293 $
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_INT, //< Integer
102  ST_BOOL, //< Boolean
103  ST_SET //< Set
104  };
105 
107  class SymbolEntry {
108  public:
109  SymbolType t; //< Type of entry
110  int i; //< Value of entry or array start index
112  SymbolEntry(void) {}
114  SymbolEntry(SymbolType t0, int i0) : t(t0), i(i0) {}
115 
116  };
117 
120  return SymbolEntry(ST_INTVAR, i);
121  }
124  return SymbolEntry(ST_BOOLVAR, i);
125  }
128  return SymbolEntry(ST_FLOATVAR, i);
129  }
132  return SymbolEntry(ST_SETVAR, i);
133  }
134 
137  return SymbolEntry(ST_INTVARARRAY, i);
138  }
141  return SymbolEntry(ST_BOOLVARARRAY, i);
142  }
145  return SymbolEntry(ST_FLOATVARARRAY, i);
146  }
149  return SymbolEntry(ST_SETVARARRAY, i);
150  }
151 
154  return SymbolEntry(ST_INT, i);
155  }
158  return SymbolEntry(ST_BOOL, b);
159  }
162  return SymbolEntry(ST_SET, i);
163  }
164 
167  return SymbolEntry(ST_INTVALARRAY, i);
168  }
171  return SymbolEntry(ST_BOOLVALARRAY, i);
172  }
175  return SymbolEntry(ST_SETVALARRAY, i);
176  }
177 
179  class ParserState {
180  public:
181  ParserState(const std::string& b, std::ostream& err0,
183  : buf(b.c_str()), pos(0), length(b.size()), fg(fg0),
184  hadError(false), err(err0) {}
185 
186  ParserState(char* buf0, int length0, std::ostream& err0,
188  : buf(buf0), pos(0), length(length0), fg(fg0),
189  hadError(false), err(err0) {}
190 
191  void* yyscanner;
192  const char* buf;
193  unsigned int pos, length;
195  std::vector<std::pair<std::string,AST::Node*> > _output;
196 
198 
199  // SymbolTable<int> intvarTable;
200  // SymbolTable<int> boolvarTable;
201  // SymbolTable<int> floatvarTable;
202  // SymbolTable<int> setvarTable;
203  // SymbolTable<std::vector<int> > intvararrays;
204  // SymbolTable<std::vector<int> > boolvararrays;
205  // SymbolTable<std::vector<int> > floatvararrays;
206  // SymbolTable<std::vector<int> > setvararrays;
207  // SymbolTable<std::vector<int> > intvalarrays;
208  // SymbolTable<std::vector<int> > boolvalarrays;
209  // SymbolTable<int> intvals;
210  // SymbolTable<bool> boolvals;
211  // SymbolTable<AST::SetLit> setvals;
212  // SymbolTable<std::vector<AST::SetLit> > setvalarrays;
213 
214  std::vector<varspec> intvars;
215  std::vector<varspec> boolvars;
216  std::vector<varspec> setvars;
217  std::vector<int> arrays;
218  std::vector<AST::SetLit> setvals;
219 
220  std::vector<ConExpr*> domainConstraints;
221 
222  bool hadError;
223  std::ostream& err;
224 
225  int fillBuffer(char* lexBuf, unsigned int lexBufSize) {
226  if (pos >= length)
227  return 0;
228  int num = std::min(length - pos, lexBufSize);
229  memcpy(lexBuf,buf+pos,num);
230  pos += num;
231  return num;
232  }
233 
234  void output(std::string x, AST::Node* n) {
235  _output.push_back(std::pair<std::string,AST::Node*>(x,n));
236  }
237 
239  OutputOrder oo;
240  std::sort(_output.begin(),_output.end(),oo);
241  AST::Array* a = new AST::Array();
242  for (unsigned int i=0; i<_output.size(); i++) {
243  a->a.push_back(new AST::String(_output[i].first+" = "));
244  if (_output[i].second->isArray()) {
245  AST::Array* oa = _output[i].second->getArray();
246  for (unsigned int j=0; j<oa->a.size(); j++) {
247  a->a.push_back(oa->a[j]);
248  oa->a[j] = NULL;
249  }
250  delete _output[i].second;
251  } else {
252  a->a.push_back(_output[i].second);
253  }
254  a->a.push_back(new AST::String(";\n"));
255  }
256  return a;
257  }
258 
259  };
260 
261 }}
262 
263 #endif
264 
265 // STATISTICS: flatzinc-any