Generated on Thu Jan 16 2025 00:00:00 for Gecode by doxygen 1.14.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.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
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 <iostream>
58#include <algorithm>
59
64#include <gecode/flatzinc/parser.tab.hh>
66
67namespace 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
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
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
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 }
119
121 return SymbolEntry(ST_BOOLVAR, i);
122 }
123
125 return SymbolEntry(ST_FLOATVAR, i);
126 }
127
129 return SymbolEntry(ST_SETVAR, i);
130 }
131
136
140
144
148
151 return SymbolEntry(ST_INT, i);
152 }
153
155 return SymbolEntry(ST_BOOL, b);
156 }
157
159 return SymbolEntry(ST_SET, i);
160 }
161
163 return SymbolEntry(ST_FLOAT, i);
164 }
165
170
174
178
182
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
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
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
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:430
Strict weak ordering for output items.
Definition parser.hh:75
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
Gecode::FlatZinc::FlatZincSpace * fg
Definition parser.hh:199
SymbolTable< SymbolEntry > symbols
Definition parser.hh:202
AST::Array * getOutput(void)
Definition parser.hh:231
std::vector< varspec > boolvars
Definition parser.hh:205
ParserState(char *buf0, int length0, std::ostream &err0, Gecode::FlatZinc::FlatZincSpace *fg0)
Definition parser.hh:191
std::vector< varspec > setvars
Definition parser.hh:206
std::vector< double > floatvals
Definition parser.hh:210
std::vector< varspec > floatvars
Definition parser.hh:207
void output(std::string x, AST::Node *n)
Definition parser.hh:227
std::vector< ConExpr * > constraints
Definition parser.hh:211
std::vector< varspec > intvars
Definition parser.hh:204
std::vector< int > arrays
Definition parser.hh:208
std::vector< ConExpr * > domainConstraints
Definition parser.hh:213
int fillBuffer(char *lexBuf, unsigned int lexBufSize)
Definition parser.hh:218
std::vector< std::pair< std::string, AST::Node * > > _output
Definition parser.hh:200
ParserState(const std::string &b, std::ostream &err0, Gecode::FlatZinc::FlatZincSpace *fg0)
Definition parser.hh:186
std::vector< AST::SetLit > setvals
Definition parser.hh:209
Entries in the symbol table.
Definition parser.hh:105
SymbolEntry(void)
Default constructor.
Definition parser.hh:110
SymbolEntry(SymbolType t0, int i0)
Constructor.
Definition parser.hh:112
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:158
SymbolEntry se_fva(int i)
Construct float variable array entry.
Definition parser.hh:141
SymbolType
Types of symbols.
Definition parser.hh:85
SymbolEntry se_sv(int i)
Construct set variable entry.
Definition parser.hh:128
SymbolEntry se_bv(int i)
Construct Boolean variable entry.
Definition parser.hh:120
SymbolEntry se_b(bool b)
Construct Boolean entry.
Definition parser.hh:154
SymbolEntry se_sa(int i)
Construct set array entry.
Definition parser.hh:175
SymbolEntry se_bva(int i)
Construct Boolean variable array entry.
Definition parser.hh:137
SymbolEntry se_iva(int i)
Construct integer variable array entry.
Definition parser.hh:133
std::pair< std::string, Option< std::vector< int > * > > intvartype
Definition parser.hh:69
SymbolEntry se_ba(int i)
Construct Boolean array entry.
Definition parser.hh:171
std::pair< std::string, VarSpec * > varspec
Definition parser.hh:72
SymbolEntry se_sva(int i)
Construct set variable array entry.
Definition parser.hh:145
SymbolEntry se_i(int i)
Construct integer entry.
Definition parser.hh:150
SymbolEntry se_iv(int i)
Construct integer variable entry.
Definition parser.hh:116
SymbolEntry se_fa(int i)
Construct float array entry.
Definition parser.hh:179
SymbolEntry se_ia(int i)
Construct integer array entry.
Definition parser.hh:167
SymbolEntry se_f(int i)
Construct float entry.
Definition parser.hh:162
SymbolEntry se_fv(int i)
Construct float variable entry.
Definition parser.hh:124
Gecode toplevel namespace
Post propagator for SetVar SetOpType SetVar y
Definition set.hh:773
Post propagator for SetVar x
Definition set.hh:773
#define forceinline
Definition config.hpp:194