Generated on Mon Feb 8 2021 00:00:00 for Gecode by doxygen 1.8.20
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  * 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
44 extern "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 
63 #include <gecode/flatzinc/ast.hh>
64 #include <gecode/flatzinc/parser.tab.hh>
66 
67 namespace 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 
75  class OutputOrder {
76  public:
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 
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 
105  class SymbolEntry {
106  public:
107  SymbolType t; //< Type of entry
108  int i; //< Value of entry or array start index
110  SymbolEntry(void) {}
112  SymbolEntry(SymbolType t0, int i0) : t(t0), i(i0) {}
113  };
114 
117  return SymbolEntry(ST_INTVAR, i);
118  }
121  return SymbolEntry(ST_BOOLVAR, i);
122  }
125  return SymbolEntry(ST_FLOATVAR, i);
126  }
129  return SymbolEntry(ST_SETVAR, i);
130  }
131 
134  return SymbolEntry(ST_INTVARARRAY, i);
135  }
138  return SymbolEntry(ST_BOOLVARARRAY, i);
139  }
142  return SymbolEntry(ST_FLOATVARARRAY, i);
143  }
146  return SymbolEntry(ST_SETVARARRAY, i);
147  }
148 
151  return SymbolEntry(ST_INT, i);
152  }
155  return SymbolEntry(ST_BOOL, b);
156  }
159  return SymbolEntry(ST_SET, i);
160  }
163  return SymbolEntry(ST_FLOAT, i);
164  }
165 
168  return SymbolEntry(ST_INTVALARRAY, i);
169  }
172  return SymbolEntry(ST_BOOLVALARRAY, i);
173  }
176  return SymbolEntry(ST_SETVALARRAY, i);
177  }
180  return SymbolEntry(ST_FLOATVALARRAY, i);
181  }
182 
184  class ParserState {
185  public:
186  ParserState(const std::string& b, std::ostream& err0,
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,
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;
200  std::vector<std::pair<std::string,AST::Node*> > _output;
201 
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  bool hadError;
216  std::ostream& err;
217 
218  int fillBuffer(char* lexBuf, unsigned int lexBufSize) {
219  if (pos >= length)
220  return 0;
221  int num = std::min(length - pos, lexBufSize);
222  memcpy(lexBuf,buf+pos,num);
223  pos += num;
224  return num;
225  }
226 
227  void output(std::string x, AST::Node* n) {
228  _output.push_back(std::pair<std::string,AST::Node*>(x,n));
229  }
230 
232  OutputOrder oo;
233  std::sort(_output.begin(),_output.end(),oo);
234  AST::Array* a = new AST::Array();
235  for (unsigned int i=0; i<_output.size(); i++) {
236  a->a.push_back(new AST::String(_output[i].first+" = "));
237  if (_output[i].second->isArray()) {
238  AST::Array* oa = _output[i].second->getArray();
239  for (unsigned int j=0; j<oa->a.size(); j++) {
240  a->a.push_back(oa->a[j]);
241  oa->a[j] = NULL;
242  }
243  delete _output[i].second;
244  } else {
245  a->a.push_back(_output[i].second);
246  }
247  a->a.push_back(new AST::String(";\n"));
248  }
249  return a;
250  }
251 
252  };
253 
254 }}
255 
256 #endif
257 
258 // STATISTICS: flatzinc-any
State of the FlatZinc parser
Definition: parser.hh:184
A node in a FlatZinc abstract syntax tree.
Definition: ast.hh:67
Post propagator for SetVar x
Definition: set.hh:767
void * yyscanner
Definition: parser.hh:196
@ ST_INTVARARRAY
Definition: parser.hh:90
SymbolEntry se_b(bool b)
Construct Boolean entry.
Definition: parser.hh:154
Post propagator for SetVar SetOpType SetVar y
Definition: set.hh:767
SymbolEntry se_s(int i)
Construct set entry.
Definition: parser.hh:158
SymbolEntry se_i(int i)
Construct integer entry.
Definition: parser.hh:150
@ ST_FLOATVALARRAY
Definition: parser.hh:97
@ ST_FLOAT
Definition: parser.hh:101
SymbolEntry se_fv(int i)
Construct float variable entry.
Definition: parser.hh:124
SymbolType t
Definition: parser.hh:107
int fillBuffer(char *lexBuf, unsigned int lexBufSize)
Definition: parser.hh:218
SymbolEntry se_bva(int i)
Construct Boolean variable array entry.
Definition: parser.hh:137
A space that can be initialized with a FlatZinc model.
Definition: flatzinc.hh:428
unsigned int size(I &i)
Size of all ranges of range iterator i.
Array node
Definition: ast.hh:231
@ ST_INTVALARRAY
Definition: parser.hh:94
std::vector< Node * > a
Definition: ast.hh:233
SymbolEntry(void)
Default constructor.
Definition: parser.hh:110
String node
Definition: ast.hh:300
SymbolEntry(SymbolType t0, int i0)
Constructor.
Definition: parser.hh:112
SymbolEntry se_fva(int i)
Construct float variable array entry.
Definition: parser.hh:141
ParserState(const std::string &b, std::ostream &err0, Gecode::FlatZinc::FlatZincSpace *fg0)
Definition: parser.hh:186
std::vector< varspec > floatvars
Definition: parser.hh:207
const FloatNum min
Smallest allowed float value.
Definition: float.hh:846
@ ST_SETVARARRAY
Definition: parser.hh:92
unsigned int length
Definition: parser.hh:198
SymbolEntry se_ba(int i)
Construct Boolean array entry.
Definition: parser.hh:171
std::vector< varspec > intvars
Definition: parser.hh:204
std::vector< std::pair< std::string, AST::Node * > > _output
Definition: parser.hh:200
SymbolEntry se_ia(int i)
Construct integer array entry.
Definition: parser.hh:167
Gecode toplevel namespace
@ ST_FLOATVARARRAY
Definition: parser.hh:93
bool hadError
Definition: parser.hh:215
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:78
@ ST_SETVAR
Definition: parser.hh:89
std::pair< std::string, VarSpec * > varspec
Definition: parser.hh:71
Entries in the symbol table.
Definition: parser.hh:105
Symbol table mapping identifiers (strings) to values.
Definition: symboltable.hh:51
SymbolTable< SymbolEntry > symbols
Definition: parser.hh:202
Base class for variable specifications.
Definition: varspec.hh:52
ParserState(char *buf0, int length0, std::ostream &err0, Gecode::FlatZinc::FlatZincSpace *fg0)
Definition: parser.hh:191
std::vector< varspec > boolvars
Definition: parser.hh:205
SymbolEntry se_bv(int i)
Construct Boolean variable entry.
Definition: parser.hh:120
@ ST_INTVAR
Definition: parser.hh:86
void sort(TaskViewArray< TaskView > &t)
Sort task view array t according to sto and inc (increasing or decreasing)
Definition: sort.hpp:133
AST::Array * getOutput(void)
Definition: parser.hh:231
std::pair< std::string, Option< std::vector< int > * > > intvartype
Definition: parser.hh:69
struct Gecode::@602::NNF::@65::@66 b
For binary nodes (and, or, eqv)
Gecode::FlatZinc::FlatZincSpace * fg
Definition: parser.hh:199
struct Gecode::@602::NNF::@65::@67 a
For atomic nodes.
@ ST_SETVALARRAY
Definition: parser.hh:96
@ ST_BOOLVAR
Definition: parser.hh:87
std::vector< double > floatvals
Definition: parser.hh:210
SymbolEntry se_sva(int i)
Construct set variable array entry.
Definition: parser.hh:145
std::vector< int > arrays
Definition: parser.hh:208
SymbolEntry se_sa(int i)
Construct set array entry.
Definition: parser.hh:175
std::vector< ConExpr * > domainConstraints
Definition: parser.hh:213
Strict weak ordering for output items.
Definition: parser.hh:75
std::vector< AST::SetLit > setvals
Definition: parser.hh:209
SymbolEntry se_fa(int i)
Construct float array entry.
Definition: parser.hh:179
SymbolEntry se_iva(int i)
Construct integer variable array entry.
Definition: parser.hh:133
int i
Definition: parser.hh:108
SymbolEntry se_iv(int i)
Construct integer variable entry.
Definition: parser.hh:116
SymbolType
Types of symbols.
Definition: parser.hh:85
#define forceinline
Definition: config.hpp:192
SymbolEntry se_sv(int i)
Construct set variable entry.
Definition: parser.hh:128
@ ST_SET
Definition: parser.hh:100
@ ST_FLOATVAR
Definition: parser.hh:88
std::vector< ConExpr * > constraints
Definition: parser.hh:211
SymbolEntry se_f(int i)
Construct float entry.
Definition: parser.hh:162
void output(std::string x, AST::Node *n)
Definition: parser.hh:227
int n
Number of negative literals for node type.
Definition: bool-expr.cpp:234
std::vector< varspec > setvars
Definition: parser.hh:206
@ ST_INT
Definition: parser.hh:98
@ ST_BOOLVALARRAY
Definition: parser.hh:95
const char * buf
Definition: parser.hh:197
Gecode::IntArgs i({1, 2, 3, 4})
@ ST_BOOLVARARRAY
Definition: parser.hh:91
std::ostream & err
Definition: parser.hh:216
unsigned int pos
Definition: parser.hh:198
@ ST_BOOL
Definition: parser.hh:99