Generated on Sat Feb 7 2015 02:01:13 for Gecode by doxygen 1.8.9.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: 2014-08-07 05:31:59 +0200 (Thu, 07 Aug 2014) $ by $Author: tack $
11  * $Revision: 14189 $
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  std::vector<ConExpr*> constraints;
216 
217  std::vector<ConExpr*> domainConstraints;
218 
219  bool hadError;
220  std::ostream& err;
221 
222  int fillBuffer(char* lexBuf, unsigned int lexBufSize) {
223  if (pos >= length)
224  return 0;
225  int num = std::min(length - pos, lexBufSize);
226  memcpy(lexBuf,buf+pos,num);
227  pos += num;
228  return num;
229  }
230 
231  void output(std::string x, AST::Node* n) {
232  _output.push_back(std::pair<std::string,AST::Node*>(x,n));
233  }
234 
236  OutputOrder oo;
237  std::sort(_output.begin(),_output.end(),oo);
238  AST::Array* a = new AST::Array();
239  for (unsigned int i=0; i<_output.size(); i++) {
240  a->a.push_back(new AST::String(_output[i].first+" = "));
241  if (_output[i].second->isArray()) {
242  AST::Array* oa = _output[i].second->getArray();
243  for (unsigned int j=0; j<oa->a.size(); j++) {
244  a->a.push_back(oa->a[j]);
245  oa->a[j] = NULL;
246  }
247  delete _output[i].second;
248  } else {
249  a->a.push_back(_output[i].second);
250  }
251  a->a.push_back(new AST::String(";\n"));
252  }
253  return a;
254  }
255 
256  };
257 
258 }}
259 
260 #endif
261 
262 // STATISTICS: flatzinc-any
ParserState(char *buf0, int length0, std::ostream &err0, Gecode::FlatZinc::FlatZincSpace *fg0)
Definition: parser.hh:195
std::pair< std::string, VarSpec * > varspec
Definition: parser.hh:75
int size(void) const
Return size of array (number of elements)
Definition: array.hpp:1662
SymbolTable< SymbolEntry > symbols
Definition: parser.hh:206
SymbolEntry se_iva(int i)
Construct integer variable array entry.
Definition: parser.hh:137
SymbolType
Types of symbols.
Definition: parser.hh:89
AST::Array * getOutput(void)
Definition: parser.hh:235
std::vector< varspec > intvars
Definition: parser.hh:208
std::vector< ConExpr * > constraints
Definition: parser.hh:215
std::pair< std::string, Option< std::vector< int > * > > intvartype
Definition: parser.hh:73
SymbolEntry se_sv(int i)
Construct set variable entry.
Definition: parser.hh:132
std::vector< Node * > a
Definition: ast.hh:237
std::vector< double > floatvals
Definition: parser.hh:214
SymbolEntry se_sva(int i)
Construct set variable array entry.
Definition: parser.hh:149
std::vector< varspec > floatvars
Definition: parser.hh:211
Array * getArray(void)
Cast this node to an array node.
Definition: ast.hh:400
SymbolEntry(SymbolType t0, int i0)
Constructor.
Definition: parser.hh:116
SymbolEntry se_fa(int i)
Construct float array entry.
Definition: parser.hh:183
void sort(TaskViewArray< TaskView > &t)
Sort task view array t according to sto and inc (increasing or decreasing)
Definition: sort.hpp:137
SymbolEntry se_i(int i)
Construct integer entry.
Definition: parser.hh:154
SymbolEntry se_sa(int i)
Construct set array entry.
Definition: parser.hh:179
const FloatNum min
Smallest allowed float value.
Definition: float.hh:833
Gecode::IntArgs i(4, 1, 2, 3, 4)
Entries in the symbol table.
Definition: parser.hh:109
int n
Number of negative literals for node type.
Definition: bool-expr.cpp:238
SymbolEntry se_iv(int i)
Construct integer variable entry.
Definition: parser.hh:120
std::vector< varspec > setvars
Definition: parser.hh:210
std::vector< std::pair< std::string, AST::Node * > > _output
Definition: parser.hh:204
std::vector< int > arrays
Definition: parser.hh:212
SymbolEntry(void)
Default constructor.
Definition: parser.hh:114
unsigned int size(I &i)
Size of all ranges of range iterator i.
Gecode::FlatZinc::FlatZincSpace * fg
Definition: parser.hh:203
Symbol table mapping identifiers (strings) to values.
Definition: symboltable.hh:55
SymbolEntry se_f(int i)
Construct float entry.
Definition: parser.hh:166
bool operator()(const std::pair< std::string, AST::Node * > &x, const std::pair< std::string, AST::Node * > &y)
Return if x is less than y, based on first component.
Definition: parser.hh:82
std::vector< varspec > boolvars
Definition: parser.hh:209
SymbolEntry se_b(bool b)
Construct Boolean entry.
Definition: parser.hh:158
SymbolEntry se_s(int i)
Construct set entry.
Definition: parser.hh:162
void output(std::string x, AST::Node *n)
Definition: parser.hh:231
std::vector< ConExpr * > domainConstraints
Definition: parser.hh:217
Node * x
Pointer to corresponding Boolean expression node.
Definition: bool-expr.cpp:253
ParserState(const std::string &b, std::ostream &err0, Gecode::FlatZinc::FlatZincSpace *fg0)
Definition: parser.hh:190
SymbolEntry se_ia(int i)
Construct integer array entry.
Definition: parser.hh:171
SymbolEntry se_fv(int i)
Construct float variable entry.
Definition: parser.hh:128
#define forceinline
Definition: config.hpp:132
SymbolEntry se_bva(int i)
Construct Boolean variable array entry.
Definition: parser.hh:141
Base class for variable specifications.
Definition: varspec.hh:56
Strict weak ordering for output items.
Definition: parser.hh:79
A space that can be initialized with a FlatZinc model.
Definition: flatzinc.hh:371
struct Gecode::@518::NNF::@57::@58 b
For binary nodes (and, or, eqv)
SymbolEntry se_fva(int i)
Construct float variable array entry.
Definition: parser.hh:145
Gecode toplevel namespace
SymbolEntry se_bv(int i)
Construct Boolean variable entry.
Definition: parser.hh:124
A node in a FlatZinc abstract syntax tree.
Definition: ast.hh:71
int fillBuffer(char *lexBuf, unsigned int lexBufSize)
Definition: parser.hh:222
SymbolEntry se_ba(int i)
Construct Boolean array entry.
Definition: parser.hh:175
std::vector< AST::SetLit > setvals
Definition: parser.hh:213
struct Gecode::@518::NNF::@57::@59 a
For atomic nodes.
State of the FlatZinc parser
Definition: parser.hh:188