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