parser.hh
Go to the documentation of this file.
/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
* Main authors:
* Guido Tack <tack@gecode.org>
*
* Copyright:
* Guido Tack, 2007
*
* This file is part of Gecode, the generic constraint
* development environment:
* http://www.gecode.org
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
#ifndef __FLATZINC_PARSER_HH__
#define __FLATZINC_PARSER_HH__
#include <gecode/flatzinc.hh>
// This is a workaround for a bug in flex that only shows up
// with the Microsoft C++ compiler
#if defined(_MSC_VER)
#define YY_NO_UNISTD_H
#ifdef __cplusplus
extern "C" int isatty(int);
#endif
#endif
// The Microsoft C++ compiler marks certain functions as deprecated,
// so let's take the alternative definitions
#if defined(_MSC_VER)
#define strdup _strdup
#define fileno _fileno
#endif
#include <string>
#include <vector>
#include <iostream>
#include <algorithm>
#include <gecode/flatzinc/option.hh>
#include <gecode/flatzinc/varspec.hh>
#include <gecode/flatzinc/conexpr.hh>
#include <gecode/flatzinc/ast.hh>
#include <gecode/flatzinc/parser.tab.hpp>
#include <gecode/flatzinc/symboltable.hh>
namespace Gecode { namespace FlatZinc {
typedef std::pair<std::string,Option<std::vector<int>* > > intvartype;
class VarSpec;
typedef std::pair<std::string, VarSpec*> varspec;
class OutputOrder {
public:
bool operator ()(const std::pair<std::string,AST::Node*>& x,
const std::pair<std::string,AST::Node*>& y) {
return x.first < y.first;
}
};
enum SymbolType {
ST_INTVAR, //< Integer variable
ST_BOOLVAR, //< Boolean variable
ST_FLOATVAR, //< Float variable
ST_SETVAR, //< Set variable
ST_INTVARARRAY, //< Integer variable array
ST_BOOLVARARRAY, //< Boolean variable array
ST_SETVARARRAY, //< Set variable array
ST_FLOATVARARRAY, //< Float variable array
ST_INTVALARRAY, //< Integer array
ST_BOOLVALARRAY, //< Boolean array
ST_SETVALARRAY, //< Set array
ST_FLOATVALARRAY, //< Float array
ST_INT, //< Integer
ST_BOOL, //< Boolean
ST_SET, //< Set
ST_FLOAT //< Float
};
class SymbolEntry {
public:
SymbolType t; //< Type of entry
int i; //< Value of entry or array start index
SymbolEntry(void) {}
SymbolEntry(SymbolType t0, int i0) : t(t0), i(i0) {}
};
forceinline SymbolEntry se_iv(int i) {
return SymbolEntry(ST_INTVAR, i);
}
forceinline SymbolEntry se_bv(int i) {
return SymbolEntry(ST_BOOLVAR, i);
}
forceinline SymbolEntry se_fv(int i) {
return SymbolEntry(ST_FLOATVAR, i);
}
forceinline SymbolEntry se_sv(int i) {
return SymbolEntry(ST_SETVAR, i);
}
forceinline SymbolEntry se_iva(int i) {
return SymbolEntry(ST_INTVARARRAY, i);
}
forceinline SymbolEntry se_bva(int i) {
return SymbolEntry(ST_BOOLVARARRAY, i);
}
forceinline SymbolEntry se_fva(int i) {
return SymbolEntry(ST_FLOATVARARRAY, i);
}
forceinline SymbolEntry se_sva(int i) {
return SymbolEntry(ST_SETVARARRAY, i);
}
forceinline SymbolEntry se_i(int i) {
return SymbolEntry(ST_INT, i);
}
forceinline SymbolEntry se_b(bool b) {
return SymbolEntry(ST_BOOL, b);
}
forceinline SymbolEntry se_s(int i) {
return SymbolEntry(ST_SET, i);
}
forceinline SymbolEntry se_f(int i) {
return SymbolEntry(ST_FLOAT, i);
}
forceinline SymbolEntry se_ia(int i) {
return SymbolEntry(ST_INTVALARRAY, i);
}
forceinline SymbolEntry se_ba(int i) {
return SymbolEntry(ST_BOOLVALARRAY, i);
}
forceinline SymbolEntry se_sa(int i) {
return SymbolEntry(ST_SETVALARRAY, i);
}
forceinline SymbolEntry se_fa(int i) {
return SymbolEntry(ST_FLOATVALARRAY, i);
}
class ParserState {
public:
ParserState(const std::string& b, std::ostream& err0,
Gecode::FlatZinc::FlatZincSpace* fg0)
: buf(b.c_str()), pos(0), length(b.size()), fg(fg0),
hadError(false), err(err0) {}
ParserState(char* buf0, int length0, std::ostream& err0,
Gecode::FlatZinc::FlatZincSpace* fg0)
: buf(buf0), pos(0), length(length0), fg(fg0),
hadError(false), err(err0) {}
void* yyscanner;
const char* buf;
unsigned int pos, length;
Gecode::FlatZinc::FlatZincSpace* fg;
std::vector<std::pair<std::string,AST::Node*> > _output;
SymbolTable<SymbolEntry> symbols;
std::vector<varspec> intvars;
std::vector<varspec> boolvars;
std::vector<varspec> setvars;
std::vector<varspec> floatvars;
std::vector<int> arrays;
std::vector<AST::SetLit> setvals;
std::vector<double> floatvals;
std::vector<ConExpr*> constraints;
std::vector<ConExpr*> domainConstraints;
bool hadError;
std::ostream& err;
int fillBuffer(char* lexBuf, unsigned int lexBufSize) {
if (pos >= length)
return 0;
int num = std::min(length - pos, lexBufSize);
memcpy(lexBuf,buf+pos,num);
pos += num;
return num;
}
void output(std::string x, AST::Node* n) {
_output.push_back(std::pair<std::string,AST::Node*>(x,n));
}
AST::Array* getOutput(void) {
OutputOrder oo;
std::sort(_output.begin(),_output.end(),oo);
AST::Array* a = new AST::Array();
for (unsigned int i=0; i<_output.size(); i++) {
a->a.push_back(new AST::String(_output[i].first+" = "));
if (_output[i].second->isArray()) {
AST::Array* oa = _output[i].second->getArray();
for (unsigned int j=0; j<oa->a.size(); j++) {
a->a.push_back(oa->a[j]);
oa->a[j] = NULL;
}
delete _output[i].second;
} else {
a->a.push_back(_output[i].second);
}
a->a.push_back(new AST::String(";\n"));
}
return a;
}
};
}}
#endif
// STATISTICS: flatzinc-any
ast.hh
Gecode::FlatZinc::AST::Array
Array node
Definition ast.hh:231
Gecode::FlatZinc::AST::Array::a
std::vector< Node * > a
Definition ast.hh:233
Gecode::FlatZinc::AST::Node
A node in a FlatZinc abstract syntax tree.
Definition ast.hh:67
Gecode::FlatZinc::AST::String
String node
Definition ast.hh:300
Gecode::FlatZinc::FlatZincSpace
A space that can be initialized with a FlatZinc model.
Definition flatzinc.hh:428
Gecode::FlatZinc::OutputOrder
Strict weak ordering for output items.
Definition parser.hh:75
Gecode::FlatZinc::OutputOrder::operator()
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::ParserState::hadError
bool hadError
Definition parser.hh:215
Gecode::FlatZinc::ParserState::fg
Gecode::FlatZinc::FlatZincSpace * fg
Definition parser.hh:199
Gecode::FlatZinc::ParserState::length
unsigned int length
Definition parser.hh:198
Gecode::FlatZinc::ParserState::symbols
SymbolTable< SymbolEntry > symbols
Definition parser.hh:202
Gecode::FlatZinc::ParserState::getOutput
AST::Array * getOutput(void)
Definition parser.hh:231
Gecode::FlatZinc::ParserState::boolvars
std::vector< varspec > boolvars
Definition parser.hh:205
Gecode::FlatZinc::ParserState::ParserState
ParserState(char *buf0, int length0, std::ostream &err0, Gecode::FlatZinc::FlatZincSpace *fg0)
Definition parser.hh:191
Gecode::FlatZinc::ParserState::setvars
std::vector< varspec > setvars
Definition parser.hh:206
Gecode::FlatZinc::ParserState::err
std::ostream & err
Definition parser.hh:216
Gecode::FlatZinc::ParserState::floatvals
std::vector< double > floatvals
Definition parser.hh:210
Gecode::FlatZinc::ParserState::floatvars
std::vector< varspec > floatvars
Definition parser.hh:207
Gecode::FlatZinc::ParserState::yyscanner
void * yyscanner
Definition parser.hh:196
Gecode::FlatZinc::ParserState::output
void output(std::string x, AST::Node *n)
Definition parser.hh:227
Gecode::FlatZinc::ParserState::constraints
std::vector< ConExpr * > constraints
Definition parser.hh:211
Gecode::FlatZinc::ParserState::intvars
std::vector< varspec > intvars
Definition parser.hh:204
Gecode::FlatZinc::ParserState::pos
unsigned int pos
Definition parser.hh:198
Gecode::FlatZinc::ParserState::arrays
std::vector< int > arrays
Definition parser.hh:208
Gecode::FlatZinc::ParserState::domainConstraints
std::vector< ConExpr * > domainConstraints
Definition parser.hh:213
Gecode::FlatZinc::ParserState::fillBuffer
int fillBuffer(char *lexBuf, unsigned int lexBufSize)
Definition parser.hh:218
Gecode::FlatZinc::ParserState::_output
std::vector< std::pair< std::string, AST::Node * > > _output
Definition parser.hh:200
Gecode::FlatZinc::ParserState::buf
const char * buf
Definition parser.hh:197
Gecode::FlatZinc::ParserState::ParserState
ParserState(const std::string &b, std::ostream &err0, Gecode::FlatZinc::FlatZincSpace *fg0)
Definition parser.hh:186
Gecode::FlatZinc::ParserState::setvals
std::vector< AST::SetLit > setvals
Definition parser.hh:209
Gecode::FlatZinc::SymbolEntry
Entries in the symbol table.
Definition parser.hh:105
Gecode::FlatZinc::SymbolEntry::SymbolEntry
SymbolEntry(void)
Default constructor.
Definition parser.hh:110
Gecode::FlatZinc::SymbolEntry::i
int i
Definition parser.hh:108
Gecode::FlatZinc::SymbolEntry::SymbolEntry
SymbolEntry(SymbolType t0, int i0)
Constructor.
Definition parser.hh:112
Gecode::FlatZinc::SymbolEntry::t
SymbolType t
Definition parser.hh:107
Gecode::FlatZinc::SymbolTable
Symbol table mapping identifiers (strings) to values.
Definition symboltable.hh:51
Gecode::FlatZinc::VarSpec
Base class for variable specifications.
Definition varspec.hh:52
conexpr.hh
flatzinc.hh
Gecode::FlatZinc
Interpreter for the FlatZinc language.
Gecode::FlatZinc::se_s
SymbolEntry se_s(int i)
Construct set entry.
Definition parser.hh:158
Gecode::FlatZinc::se_fva
SymbolEntry se_fva(int i)
Construct float variable array entry.
Definition parser.hh:141
Gecode::FlatZinc::SymbolType
SymbolType
Types of symbols.
Definition parser.hh:85
Gecode::FlatZinc::ST_INTVARARRAY
@ ST_INTVARARRAY
Definition parser.hh:90
Gecode::FlatZinc::ST_FLOATVALARRAY
@ ST_FLOATVALARRAY
Definition parser.hh:97
Gecode::FlatZinc::ST_INTVALARRAY
@ ST_INTVALARRAY
Definition parser.hh:94
Gecode::FlatZinc::ST_BOOLVAR
@ ST_BOOLVAR
Definition parser.hh:87
Gecode::FlatZinc::ST_SETVALARRAY
@ ST_SETVALARRAY
Definition parser.hh:96
Gecode::FlatZinc::ST_BOOLVARARRAY
@ ST_BOOLVARARRAY
Definition parser.hh:91
Gecode::FlatZinc::ST_BOOLVALARRAY
@ ST_BOOLVALARRAY
Definition parser.hh:95
Gecode::FlatZinc::ST_FLOATVAR
@ ST_FLOATVAR
Definition parser.hh:88
Gecode::FlatZinc::ST_BOOL
@ ST_BOOL
Definition parser.hh:99
Gecode::FlatZinc::ST_INT
@ ST_INT
Definition parser.hh:98
Gecode::FlatZinc::ST_FLOAT
@ ST_FLOAT
Definition parser.hh:101
Gecode::FlatZinc::ST_FLOATVARARRAY
@ ST_FLOATVARARRAY
Definition parser.hh:93
Gecode::FlatZinc::ST_SET
@ ST_SET
Definition parser.hh:100
Gecode::FlatZinc::ST_SETVAR
@ ST_SETVAR
Definition parser.hh:89
Gecode::FlatZinc::ST_SETVARARRAY
@ ST_SETVARARRAY
Definition parser.hh:92
Gecode::FlatZinc::ST_INTVAR
@ ST_INTVAR
Definition parser.hh:86
Gecode::FlatZinc::se_sv
SymbolEntry se_sv(int i)
Construct set variable entry.
Definition parser.hh:128
Gecode::FlatZinc::se_bv
SymbolEntry se_bv(int i)
Construct Boolean variable entry.
Definition parser.hh:120
Gecode::FlatZinc::se_b
SymbolEntry se_b(bool b)
Construct Boolean entry.
Definition parser.hh:154
Gecode::FlatZinc::se_sa
SymbolEntry se_sa(int i)
Construct set array entry.
Definition parser.hh:175
Gecode::FlatZinc::se_bva
SymbolEntry se_bva(int i)
Construct Boolean variable array entry.
Definition parser.hh:137
Gecode::FlatZinc::se_iva
SymbolEntry se_iva(int i)
Construct integer variable array entry.
Definition parser.hh:133
Gecode::FlatZinc::intvartype
std::pair< std::string, Option< std::vector< int > * > > intvartype
Definition parser.hh:69
Gecode::FlatZinc::se_ba
SymbolEntry se_ba(int i)
Construct Boolean array entry.
Definition parser.hh:171
Gecode::FlatZinc::varspec
std::pair< std::string, VarSpec * > varspec
Definition parser.hh:72
Gecode::FlatZinc::se_sva
SymbolEntry se_sva(int i)
Construct set variable array entry.
Definition parser.hh:145
Gecode::FlatZinc::se_i
SymbolEntry se_i(int i)
Construct integer entry.
Definition parser.hh:150
Gecode::FlatZinc::se_iv
SymbolEntry se_iv(int i)
Construct integer variable entry.
Definition parser.hh:116
Gecode::FlatZinc::se_fa
SymbolEntry se_fa(int i)
Construct float array entry.
Definition parser.hh:179
Gecode::FlatZinc::se_ia
SymbolEntry se_ia(int i)
Construct integer array entry.
Definition parser.hh:167
Gecode::FlatZinc::se_f
SymbolEntry se_f(int i)
Construct float entry.
Definition parser.hh:162
Gecode::FlatZinc::se_fv
SymbolEntry se_fv(int i)
Construct float variable entry.
Definition parser.hh:124
Gecode
Gecode toplevel namespace
option.hh
forceinline
#define forceinline
Definition config.hpp:187
symboltable.hh
varspec.hh