parser.hh
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 #ifndef __FLATZINC_PARSER_HH__
00039 #define __FLATZINC_PARSER_HH__
00040
00041 #include <gecode/flatzinc.hh>
00042
00043
00044
00045 #if defined(_MSC_VER)
00046 #define YY_NO_UNISTD_H
00047 #ifdef __cplusplus
00048 extern "C" int isatty(int);
00049 #endif
00050 #endif
00051
00052
00053
00054 #if defined(_MSC_VER)
00055 #define strdup _strdup
00056 #define fileno _fileno
00057 #endif
00058
00059 #include <string>
00060 #include <vector>
00061 #include <iostream>
00062
00063 #include <gecode/flatzinc/option.hh>
00064 #include <gecode/flatzinc/varspec.hh>
00065 #include <gecode/flatzinc/conexpr.hh>
00066 #include <gecode/flatzinc/ast.hh>
00067 #include <gecode/flatzinc/parser.tab.hh>
00068 #include <gecode/flatzinc/symboltable.hh>
00069
00070 namespace Gecode { namespace FlatZinc {
00071
00072 typedef std::pair<std::string,Option<std::vector<int>* > > intvartype;
00073
00074 class VarSpec;
00075 typedef std::pair<std::string, VarSpec*> varspec;
00076
00078 class ParserState {
00079 public:
00080 ParserState(const std::string& b, std::ostream& err0,
00081 Gecode::FlatZinc::FlatZincSpace* fg0)
00082 : buf(b.c_str()), pos(0), length(b.size()), fg(fg0),
00083 hadError(false), err(err0) {}
00084
00085 ParserState(char* buf0, int length0, std::ostream& err0,
00086 Gecode::FlatZinc::FlatZincSpace* fg0)
00087 : buf(buf0), pos(0), length(length0), fg(fg0),
00088 hadError(false), err(err0) {}
00089
00090 void* yyscanner;
00091 const char* buf;
00092 unsigned int pos, length;
00093 Gecode::FlatZinc::FlatZincSpace* fg;
00094 std::vector<std::pair<std::string,AST::Node*> > _output;
00095
00096 SymbolTable<int> intvarTable;
00097 SymbolTable<int> boolvarTable;
00098 SymbolTable<int> floatvarTable;
00099 SymbolTable<int> setvarTable;
00100 SymbolTable<std::vector<int> > intvararrays;
00101 SymbolTable<std::vector<int> > boolvararrays;
00102 SymbolTable<std::vector<int> > floatvararrays;
00103 SymbolTable<std::vector<int> > setvararrays;
00104 SymbolTable<std::vector<int> > intvalarrays;
00105 SymbolTable<std::vector<int> > boolvalarrays;
00106 SymbolTable<int> intvals;
00107 SymbolTable<bool> boolvals;
00108 SymbolTable<AST::SetLit> setvals;
00109 SymbolTable<std::vector<AST::SetLit> > setvalarrays;
00110
00111 std::vector<varspec> intvars;
00112 std::vector<varspec> boolvars;
00113 std::vector<varspec> setvars;
00114
00115 std::vector<ConExpr*> domainConstraints;
00116
00117 bool hadError;
00118 std::ostream& err;
00119
00120 int fillBuffer(char* lexBuf, unsigned int lexBufSize) {
00121 if (pos >= length)
00122 return 0;
00123 int num = std::min(length - pos, lexBufSize);
00124 memcpy(lexBuf,buf+pos,num);
00125 pos += num;
00126 return num;
00127 }
00128
00129 void output(std::string x, AST::Node* n) {
00130 _output.push_back(std::pair<std::string,AST::Node*>(x,n));
00131 }
00132
00133 AST::Array* getOutput(void) {
00134 AST::Array* a = new AST::Array();
00135 for (unsigned int i=0; i<_output.size(); i++) {
00136 a->a.push_back(new AST::String(_output[i].first+" = "));
00137 if (_output[i].second->isArray()) {
00138 AST::Array* oa = _output[i].second->getArray();
00139 for (unsigned int j=0; j<oa->a.size(); j++) {
00140 a->a.push_back(oa->a[j]);
00141 oa->a[j] = NULL;
00142 }
00143 delete _output[i].second;
00144 } else {
00145 a->a.push_back(_output[i].second);
00146 }
00147 a->a.push_back(new AST::String(";\n"));
00148 }
00149 return a;
00150 }
00151
00152 };
00153
00154 }}
00155
00156 #endif
00157
00158