Generated on for Gecode by doxygen 1.18.0
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.dev>
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.dev
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
44extern "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 <unordered_map>
58#include <array>
59#include <iostream>
60#include <algorithm>
61
66#include <gecode/flatzinc/parser.tab.hpp>
68
69namespace Gecode { namespace FlatZinc {
70
71 typedef std::pair<std::string,Option<std::vector<int>* > > intvartype;
72
73 class VarSpec;
74 typedef std::pair<std::string, VarSpec*> varspec;
75
78 public:
80 bool operator ()(const std::pair<std::string,AST::Node*>& x,
81 const std::pair<std::string,AST::Node*>& y) {
82 return x.first < y.first;
83 }
84 };
85
88 ST_INTVAR, //< Integer variable
89 ST_BOOLVAR, //< Boolean variable
90 ST_FLOATVAR, //< Float variable
91 ST_SETVAR, //< Set variable
92 ST_INTVARARRAY, //< Integer variable array
93 ST_BOOLVARARRAY, //< Boolean variable array
94 ST_SETVARARRAY, //< Set variable array
95 ST_FLOATVARARRAY, //< Float variable array
96 ST_INTVALARRAY, //< Integer array
97 ST_BOOLVALARRAY, //< Boolean array
98 ST_SETVALARRAY, //< Set array
99 ST_FLOATVALARRAY, //< Float array
100 ST_INT, //< Integer
101 ST_BOOL, //< Boolean
102 ST_SET, //< Set
103 ST_FLOAT //< Float
104 };
105
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
118 forceinline SymbolEntry se_iv(int i) {
119 return SymbolEntry(ST_INTVAR, i);
120 }
121
122 forceinline SymbolEntry se_bv(int i) {
123 return SymbolEntry(ST_BOOLVAR, i);
124 }
125
126 forceinline SymbolEntry se_fv(int i) {
127 return SymbolEntry(ST_FLOATVAR, i);
128 }
129
130 forceinline SymbolEntry se_sv(int i) {
131 return SymbolEntry(ST_SETVAR, i);
132 }
133
135 forceinline SymbolEntry se_iva(int i) {
136 return SymbolEntry(ST_INTVARARRAY, i);
137 }
138
139 forceinline SymbolEntry se_bva(int i) {
140 return SymbolEntry(ST_BOOLVARARRAY, i);
141 }
142
143 forceinline SymbolEntry se_fva(int i) {
144 return SymbolEntry(ST_FLOATVARARRAY, i);
145 }
146
147 forceinline SymbolEntry se_sva(int i) {
148 return SymbolEntry(ST_SETVARARRAY, i);
149 }
150
152 forceinline SymbolEntry se_i(int i) {
153 return SymbolEntry(ST_INT, i);
154 }
155
156 forceinline SymbolEntry se_b(bool b) {
157 return SymbolEntry(ST_BOOL, b);
158 }
159
160 forceinline SymbolEntry se_s(int i) {
161 return SymbolEntry(ST_SET, i);
162 }
163
164 forceinline SymbolEntry se_f(int i) {
165 return SymbolEntry(ST_FLOAT, i);
166 }
167
169 forceinline SymbolEntry se_ia(int i) {
170 return SymbolEntry(ST_INTVALARRAY, i);
171 }
172
173 forceinline SymbolEntry se_ba(int i) {
174 return SymbolEntry(ST_BOOLVALARRAY, i);
175 }
176
177 forceinline SymbolEntry se_sa(int i) {
178 return SymbolEntry(ST_SETVALARRAY, i);
179 }
180
181 forceinline SymbolEntry se_fa(int i) {
182 return SymbolEntry(ST_FLOATVALARRAY, i);
183 }
184
187 public:
188 ParserState(const std::string& b, std::ostream& err0,
190 : yyscanner(nullptr), buf(b.c_str()), pos(0), length(b.size()), fg(fg0),
191 hadError(false), err(err0) {}
192
193 ParserState(char* buf0, int length0, std::ostream& err0,
195 : yyscanner(nullptr), buf(buf0), pos(0), length(length0), fg(fg0),
196 hadError(false), err(err0) {}
197
199
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;
215 std::unordered_map<int,AST::SharedArray*> shared_value_arrays;
216 std::vector<AST::SetLit> setvals;
217 std::vector<double> floatvals;
218 std::vector<ConExpr*> constraints;
219
220 std::vector<ConExpr*> domainConstraints;
221
222 int status_idx = -1;
223 int complete_idx = -1;
224 std::vector<std::array<int, 2>> last_val_int;
225 std::vector<std::array<int, 2>> sol_int;
226 std::vector<std::array<int, 3>> uniform_int;
227 std::vector<std::array<int, 2>> sol_bool;
228 std::vector<std::array<int, 2>> last_val_bool;
229#ifdef GECODE_HAS_SET_VARS
230 std::vector<std::array<int, 2>> sol_set;
231 std::vector<std::array<int, 2>> last_val_set;
232#endif
233#ifdef GECODE_HAS_FLOAT_VARS
234 std::vector<std::array<int, 2>> last_val_float;
235 std::vector<std::array<int, 2>> sol_float;
236 std::vector<std::tuple<FloatVal, FloatVal, int>> uniform_float;
237#endif
238
240 std::ostream& err;
241
242 int fillBuffer(char* lexBuf, unsigned int lexBufSize) {
243 if (pos >= length)
244 return 0;
245 int num = std::min(length - pos, lexBufSize);
246 memcpy(lexBuf,buf+pos,num);
247 pos += num;
248 return num;
249 }
250
251 void output(std::string x, AST::Node* n) {
252 _output.push_back(std::pair<std::string,AST::Node*>(x,n));
253 }
254
256 OutputOrder oo;
257 std::sort(_output.begin(),_output.end(),oo);
258 AST::Array* a = new AST::Array();
259 for (unsigned int i=0; i<_output.size(); i++) {
260 a->a.push_back(new AST::String(_output[i].first+" = "));
261 if (_output[i].second->isArray()) {
262 AST::Array* oa = _output[i].second->getArray();
263 for (unsigned int j=0; j<oa->a.size(); j++) {
264 a->a.push_back(oa->a[j]);
265 oa->a[j] = nullptr;
266 }
267 delete _output[i].second;
268 } else {
269 a->a.push_back(_output[i].second);
270 }
271 _output[i].second = nullptr;
272 a->a.push_back(new AST::String(";\n"));
273 }
274 return a;
275 }
276
277 };
278
279}}
280
281#endif
282
283// STATISTICS: flatzinc-any
std::vector< Node * > a
Definition ast.hh:242
A node in a FlatZinc abstract syntax tree.
Definition ast.hh:67
Array * getArray(void)
Cast this node to an array node.
Definition ast.hh:446
A space that can be initialized with a FlatZinc model.
Definition flatzinc.hh:424
Strict weak ordering for output items.
Definition parser.hh:77
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:80
std::vector< std::array< int, 3 > > uniform_int
Definition parser.hh:226
Gecode::FlatZinc::FlatZincSpace * fg
Definition parser.hh:203
SymbolTable< SymbolEntry > symbols
Definition parser.hh:206
AST::Array * getOutput(void)
Definition parser.hh:255
std::vector< std::array< int, 2 > > last_val_bool
Definition parser.hh:228
std::vector< varspec > boolvars
Definition parser.hh:209
ParserState(char *buf0, int length0, std::ostream &err0, Gecode::FlatZinc::FlatZincSpace *fg0)
Definition parser.hh:193
std::vector< varspec > setvars
Definition parser.hh:210
std::vector< std::array< int, 2 > > sol_bool
Definition parser.hh:227
std::vector< std::array< int, 2 > > last_val_float
Definition parser.hh:234
std::unordered_map< int, AST::SharedArray * > shared_value_arrays
One shared node per array parameter that has been referenced, by position in arrays (see shared_value...
Definition parser.hh:215
std::vector< double > floatvals
Definition parser.hh:217
std::vector< std::array< int, 2 > > last_val_set
Definition parser.hh:231
std::vector< varspec > floatvars
Definition parser.hh:211
void output(std::string x, AST::Node *n)
Definition parser.hh:251
std::vector< std::array< int, 2 > > sol_set
Definition parser.hh:230
std::vector< ConExpr * > constraints
Definition parser.hh:218
std::vector< varspec > intvars
Definition parser.hh:208
std::vector< std::array< int, 2 > > sol_float
Definition parser.hh:235
std::vector< int > arrays
Definition parser.hh:212
std::vector< ConExpr * > domainConstraints
Definition parser.hh:220
int fillBuffer(char *lexBuf, unsigned int lexBufSize)
Definition parser.hh:242
std::vector< std::pair< std::string, AST::Node * > > _output
Definition parser.hh:204
std::vector< std::array< int, 2 > > last_val_int
Definition parser.hh:224
std::vector< std::tuple< FloatVal, FloatVal, int > > uniform_float
Definition parser.hh:236
std::vector< std::array< int, 2 > > sol_int
Definition parser.hh:225
ParserState(const std::string &b, std::ostream &err0, Gecode::FlatZinc::FlatZincSpace *fg0)
Definition parser.hh:188
std::vector< AST::SetLit > setvals
Definition parser.hh:216
Entries in the symbol table.
Definition parser.hh:107
SymbolEntry(void)
Default constructor.
Definition parser.hh:112
SymbolEntry(SymbolType t0, int i0)
Constructor.
Definition parser.hh:114
Symbol table mapping identifiers (strings) to values.
Base class for variable specifications.
Definition varspec.hh:52
Interpreter for the FlatZinc language.
SymbolEntry se_s(int i)
Construct set entry.
Definition parser.hh:160
SymbolEntry se_fva(int i)
Construct float variable array entry.
Definition parser.hh:143
SymbolType
Types of symbols.
Definition parser.hh:87
SymbolEntry se_sv(int i)
Construct set variable entry.
Definition parser.hh:130
SymbolEntry se_bv(int i)
Construct Boolean variable entry.
Definition parser.hh:122
SymbolEntry se_b(bool b)
Construct Boolean entry.
Definition parser.hh:156
SymbolEntry se_sa(int i)
Construct set array entry.
Definition parser.hh:177
SymbolEntry se_bva(int i)
Construct Boolean variable array entry.
Definition parser.hh:139
SymbolEntry se_iva(int i)
Construct integer variable array entry.
Definition parser.hh:135
std::pair< std::string, Option< std::vector< int > * > > intvartype
Definition parser.hh:71
SymbolEntry se_ba(int i)
Construct Boolean array entry.
Definition parser.hh:173
std::pair< std::string, VarSpec * > varspec
Definition parser.hh:74
SymbolEntry se_sva(int i)
Construct set variable array entry.
Definition parser.hh:147
SymbolEntry se_i(int i)
Construct integer entry.
Definition parser.hh:152
SymbolEntry se_iv(int i)
Construct integer variable entry.
Definition parser.hh:118
SymbolEntry se_fa(int i)
Construct float array entry.
Definition parser.hh:181
SymbolEntry se_ia(int i)
Construct integer array entry.
Definition parser.hh:169
SymbolEntry se_f(int i)
Construct float entry.
Definition parser.hh:164
SymbolEntry se_fv(int i)
Construct float variable entry.
Definition parser.hh:126
Gecode toplevel namespace