this repo has no description
at develop 15 kB view raw
1/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */ 2 3/* 4 * Main authors: 5 * Guido Tack <guido.tack@monash.edu> 6 */ 7 8/* This Source Code Form is subject to the terms of the Mozilla Public 9 * License, v. 2.0. If a copy of the MPL was not distributed with this 10 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 11 12%option reentrant 13%option bison-bridge bison-locations 14%option noyywrap 15%option stack 16 17%{ 18#if defined __GNUC__ 19#pragma GCC diagnostic ignored "-Wunused-function" 20#pragma GCC diagnostic ignored "-Wunused-parameter" 21#pragma GCC diagnostic ignored "-Wsign-compare" 22#pragma GCC diagnostic ignored "-Wdeprecated" 23#elif defined _MSC_VER 24#pragma warning(push, 1) 25#endif 26 27namespace MiniZinc{ class ParserLocation; } 28#define YYLTYPE MiniZinc::ParserLocation 29#define YYLTYPE_IS_DECLARED 1 30#define YYLTYPE_IS_TRIVIAL 0 31 32#include <minizinc/parser.hh> 33 34int utf8len(const char* s) { 35 int l=0; 36 for (int i=0; s[i] != '\0'; i++) 37 if ((s[i] & 0xc0) != 0x80) 38 l++; 39 return l; 40} 41 42int yy_input_proc(char* buf, int size, yyscan_t yyscanner); 43#define YY_INPUT(buf, result, max_size) \ 44 result = yy_input_proc(buf, max_size, yyscanner); 45 46#define YY_USER_ACTION \ 47 { MiniZinc::ParserState* parm = \ 48 static_cast<MiniZinc::ParserState*>(yyget_extra(yyscanner)); \ 49 yylloc->first_line(yylloc->last_line()); \ 50 yylloc->first_column(yylloc->last_column()+1); \ 51 if(parm->hadNewline) { \ 52 parm->hadNewline=false; \ 53 parm->lineStartPos += parm->nTokenNextStart; \ 54 parm->nTokenNextStart=1; \ 55 yylloc->last_line(yylloc->last_line()+1); \ 56 yylloc->first_line(yylloc->last_line()); \ 57 yylloc->first_column(1); \ 58 } \ 59 if(yytext[0] == '\n') { \ 60 parm->hadNewline=true; \ 61 parm->nTokenNextStart+=0; \ 62 } else { \ 63 parm->nTokenNextStart+=yyleng; \ 64 } \ 65 yylloc->last_column(yylloc->first_column()+utf8len(yytext)-1); \ 66 } 67 68bool hexstrtointval(const char* s, long long int& v) { 69 std::istringstream iss(s); 70 iss >> std::hex >> v; 71 return !iss.fail(); 72} 73 74bool octstrtointval(const char* s, long long int& v) { 75 std::istringstream iss(s); 76 iss >> std::oct >> v; 77 return !iss.fail(); 78} 79 80bool fast_strtointval(const char* s, long long int& v) { 81 MiniZinc::IntVal x = 0; 82 try { 83 for (; *s != '\0'; ++s) { 84 x = (x*10) + (*s - '0'); 85 } 86 } catch (MiniZinc::ArithmeticError&) { 87 return false; 88 } 89 v = x.toInt(); 90 return true; 91} 92 93bool strtofloatval(const char* s, double& v) { 94 std::istringstream iss(s); 95 iss >> v; 96 return !iss.fail(); 97} 98 99void clearBuffer(void* parm) { 100 MiniZinc::ParserState* pp = 101 static_cast<MiniZinc::ParserState*>(parm); 102 pp->stringBuffer = ""; 103} 104 105void appendBufferString(void* parm, const char* s) { 106 MiniZinc::ParserState* pp = 107 static_cast<MiniZinc::ParserState*>(parm); 108 pp->stringBuffer += s; 109} 110 111void appendBufferChar(void* parm, char s) { 112 MiniZinc::ParserState* pp = 113 static_cast<MiniZinc::ParserState*>(parm); 114 pp->stringBuffer += s; 115} 116 117char* bufferData(void* parm) { 118 MiniZinc::ParserState* pp = 119 static_cast<MiniZinc::ParserState*>(parm); 120 return strdup(pp->stringBuffer.c_str()); 121} 122 123%} 124 125%x string 126%x string_quote 127%x multilinecomment 128%x doccomment 129%x doccomment_file 130%s bracket_exp 131%s quoted_exp 132 133%% 134 135<*>\x0 { return MZN_INVALID_NULL; } 136 137\xa { } 138[ \f\xd\t] { /* ignore whitespace */ } 139 140"/**" { yy_push_state(doccomment,yyscanner); clearBuffer(yyget_extra(yyscanner)); } 141<doccomment>{ 142 "*/" { yylval->sValue = bufferData(yyget_extra(yyscanner)); 143 yy_pop_state(yyscanner); return MZN_DOC_COMMENT; } 144 [^*\xa]+ { appendBufferString(yyget_extra(yyscanner), yytext); } 145 "*" { appendBufferString(yyget_extra(yyscanner), yytext); } 146 \xa { appendBufferString(yyget_extra(yyscanner), yytext); } 147} 148 149"/***" { yy_push_state(doccomment_file,yyscanner); clearBuffer(yyget_extra(yyscanner)); } 150<doccomment_file>{ 151 "*/" { yylval->sValue = bufferData(yyget_extra(yyscanner)); 152 yy_pop_state(yyscanner); return MZN_DOC_FILE_COMMENT; } 153 [^*\xa]+ { appendBufferString(yyget_extra(yyscanner), yytext); } 154 "*" { appendBufferString(yyget_extra(yyscanner), yytext); } 155 \xa { appendBufferString(yyget_extra(yyscanner), yytext); } 156} 157 158"/*" { yy_push_state(multilinecomment,yyscanner); } 159<multilinecomment>{ 160 "*/" { yy_pop_state(yyscanner); } 161 [^*\xa]+ { } 162 "*" { } 163 \xa { } 164} 165 166 167"[" { return MZN_LEFT_BRACKET; } 168"[|" { return MZN_LEFT_2D_BRACKET; } 169"]" { return MZN_RIGHT_BRACKET; } 170"|]" { return MZN_RIGHT_2D_BRACKET; } 171%[^\xa]* { /* ignore comments */ } 172 173"true" { yylval->iValue = 1; return MZN_BOOL_LITERAL; } 174"false" { yylval->iValue = 0; return MZN_BOOL_LITERAL; } 175 1760[xX]([0-9a-fA-F]*\.[0-9a-fA-F]+|[0-9a-fA-F]+\.)([pP][+-]?[0-9]+)|(0[xX][0-9a-fA-F]+[pP][+-]?[0-9]+) { 177 if (strtofloatval(yytext, yylval->dValue)) 178 return MZN_FLOAT_LITERAL; 179 else 180 return MZN_INVALID_FLOAT_LITERAL; 181 } 182 1830[xX][0-9A-Fa-f]+ { 184 if (hexstrtointval(yytext+2, yylval->iValue)) 185 return MZN_INTEGER_LITERAL; 186 else 187 return MZN_INVALID_INTEGER_LITERAL; 188 } 1890o[0-7]+ { 190 if (octstrtointval(yytext+2, yylval->iValue)) 191 return MZN_INTEGER_LITERAL; 192 else 193 return MZN_INVALID_INTEGER_LITERAL; 194 } 195[0-9]+ { 196 if (fast_strtointval(yytext, yylval->iValue)) 197 return MZN_INTEGER_LITERAL; 198 else 199 return MZN_INVALID_INTEGER_LITERAL; 200 } 201 202[0-9]+\.[0-9]+ { 203 if (strtofloatval(yytext, yylval->dValue)) 204 return MZN_FLOAT_LITERAL; 205 else 206 return MZN_INVALID_FLOAT_LITERAL; 207 } 208[0-9]+\.[0-9]+[Ee][+-]?[0-9]+ { 209 if (strtofloatval(yytext, yylval->dValue)) 210 return MZN_FLOAT_LITERAL; 211 else 212 return MZN_INVALID_FLOAT_LITERAL; 213 } 214[0-9]+[Ee][+-]?[0-9]+ { 215 if (strtofloatval(yytext, yylval->dValue)) 216 return MZN_FLOAT_LITERAL; 217 else 218 return MZN_INVALID_FLOAT_LITERAL; 219 } 220[:;|{},\[\]\.] { 221 return *yytext; } 222\.\. { return MZN_DOTDOT; } 223"'\.\.'" { return MZN_DOTDOT_QUOTED; } 224:: { return MZN_COLONCOLON; } 225_ { return MZN_UNDERSCORE; } 226"ann" { return MZN_ANN; } 227"annotation" { return MZN_ANNOTATION; } 228"any" { return MZN_ANY; } 229"array" { return MZN_ARRAY; } 230"bool" { return MZN_BOOL; } 231"case" { return MZN_CASE; } 232"constraint" { return MZN_CONSTRAINT; } 233"default" { return MZN_DEFAULT; } 234"div" { return MZN_IDIV; } 235"'div'" { return MZN_IDIV_QUOTED; } 236"diff" { return MZN_DIFF; } 237"'diff'" { return MZN_DIFF_QUOTED; } 238"else" { return MZN_ELSE; } 239"elseif" { return MZN_ELSEIF; } 240"endif" { return MZN_ENDIF; } 241"enum" { return MZN_ENUM; } 242"float" { return MZN_FLOAT; } 243"function" { return MZN_FUNCTION; } 244"if" { return MZN_IF; } 245"include" { return MZN_INCLUDE; } 246"infinity" { return MZN_INFINITY; } 247"intersect" { return MZN_INTERSECT; } 248"'intersect'" { return MZN_INTERSECT_QUOTED; } 249"in" { return MZN_IN; } 250"'in'" { return MZN_IN_QUOTED; } 251"int" { return MZN_INT; } 252"let" { return MZN_LET; } 253"list" { return MZN_LIST; } 254"maximize" { yylval->bValue = false; return MZN_MAXIMIZE; } 255"minimize" { yylval->bValue = true; return MZN_MINIMIZE; } 256"mod" { return MZN_MOD; } 257"'mod'" { return MZN_MOD_QUOTED; } 258"not" { return MZN_NOT; } 259"'not'" { return MZN_NOT_QUOTED; } 260"of" { return MZN_OF; } 261"output" { return MZN_OUTPUT; } 262"opt" { return MZN_OPT; } 263"par" { return MZN_PAR; } 264"predicate" { return MZN_PREDICATE; } 265"record" { return MZN_RECORD; } 266"satisfy" { return MZN_SATISFY; } 267"set" { return MZN_SET; } 268"solve" { return MZN_SOLVE; } 269"string" { return MZN_STRING; } 270"subset" { return MZN_SUBSET; } 271"'subset'" { return MZN_SUBSET_QUOTED; } 272"superset" { return MZN_SUPERSET; } 273"'superset'" { return MZN_SUPERSET_QUOTED; } 274"symdiff" { return MZN_SYMDIFF; } 275"'symdiff'" { return MZN_SYMDIFF_QUOTED; } 276"test" { return MZN_TEST; } 277"then" { return MZN_THEN; } 278"tuple" { return MZN_TUPLE; } 279"type" { return MZN_TYPE; } 280"union" { return MZN_UNION; } 281"'union'" { return MZN_UNION_QUOTED; } 282"var" { return MZN_VAR; } 283"variant_record" { return MZN_VARIANT_RECORD; } 284"where" { return MZN_WHERE; } 285"xor" { return MZN_XOR; } 286"'xor'" { return MZN_XOR_QUOTED; } 287"+" { return MZN_PLUS; } 288"'+'" { return MZN_PLUS_QUOTED; } 289"-" { return MZN_MINUS; } 290"'-'" { return MZN_MINUS_QUOTED; } 291"*" { return MZN_MULT; } 292"'*'" { return MZN_MULT_QUOTED; } 293"/" { return MZN_DIV; } 294"'/'" { return MZN_DIV_QUOTED; } 295"^" { return MZN_POW; } 296"'^'" { return MZN_POW_QUOTED; } 297"++" { return MZN_PLUSPLUS; } 298"'++'" { return MZN_PLUSPLUS_QUOTED; } 299"<>" { return MZN_ABSENT; } 300"<" { return MZN_LE; } 301"'<'" { return MZN_LE_QUOTED; } 302"<=" { return MZN_LQ; } 303"'<='" { return MZN_LQ_QUOTED; } 304">" { return MZN_GR; } 305"'>'" { return MZN_GR_QUOTED; } 306">=" { return MZN_GQ; } 307"'>='" { return MZN_GQ_QUOTED; } 308"==" { return MZN_EQ; } 309"'=='" { return MZN_EQ_QUOTED; } 310"=" { return MZN_EQ; } 311"'='" { return MZN_EQ_QUOTED; } 312"!=" { return MZN_NQ; } 313"'!='" { return MZN_NQ_QUOTED; } 314"->" { return MZN_IMPL; } 315"'->'" { return MZN_IMPL_QUOTED; } 316"<-" { return MZN_RIMPL; } 317"'<-'" { return MZN_RIMPL_QUOTED; } 318"<->" { return MZN_EQUIV; } 319"'<->'" { return MZN_EQUIV_QUOTED; } 320"\\/" { return MZN_OR; } 321"'\\/'" { return MZN_OR_QUOTED; } 322"/\\" { return MZN_AND; } 323"'/\\'" { return MZN_AND_QUOTED; } 324 325"~+" { return MZN_WEAK_PLUS; } 326"~*" { return MZN_WEAK_MULT; } 327"~=" { return MZN_WEAK_EQ; } 328"~-" { return MZN_WEAK_MINUS; } 329 330"'~"[+*=-]"'" { 331 yylval->sValue = strdup(yytext+1); 332 yylval->sValue[strlen(yytext)-2] = 0; 333 return MZN_IDENTIFIER; } 334 335"_objective" { yylval->sValue = strdup(yytext); return MZN_IDENTIFIER; } 336 337[A-Za-z][A-Za-z0-9_]* { 338 yylval->sValue = strdup(yytext); return MZN_IDENTIFIER; } 339"'"[^\\'\xa\xd\x0]*"'" { 340 yylval->sValue = strdup(yytext); return MZN_IDENTIFIER; } 341_[A-Za-z][A-Za-z0-9_]* { 342 MiniZinc::ParserState* parm = 343 static_cast<MiniZinc::ParserState*>(yyget_extra(yyscanner)); 344 if (parm->isFlatZinc) { 345 yylval->sValue = strdup(yytext); return MZN_IDENTIFIER; 346 } else { 347 return FLATZINC_IDENTIFIER; 348 } 349 } 350 351"\xE2\x88\x80" { yylval->sValue = strdup("forall"); return MZN_IDENTIFIER; } 352"\xE2\x88\x83" { yylval->sValue = strdup("exists"); return MZN_IDENTIFIER; } 353"\xE2\x88\x88" { return MZN_IN; } 354"\xE2\x8A\x86" { return MZN_SUBSET; } 355"\xE2\x8A\x87" { return MZN_SUPERSET; } 356"\xE2\x88\x9E" { return MZN_INFINITY; } 357"\xC2\xAC" { return MZN_NOT; } 358"\xE2\x86\x90" { return MZN_RIMPL; } 359"\xE2\x86\x92" { return MZN_IMPL; } 360"\xE2\x86\x94" { return MZN_EQUIV; } 361"\xE2\x88\xA7" { return MZN_AND; } 362"\xE2\x88\xA8" { return MZN_OR; } 363"\xE2\x89\xA0" { return MZN_NQ; } 364"\xE2\x89\xA4" { return MZN_LQ; } 365"\xE2\x89\xA5" { return MZN_GQ; } 366"\xE2\x88\xAA" { return MZN_UNION; } 367"\xE2\x88\xA9" { return MZN_INTERSECT; } 368 369$$[A-Za-z][A-Za-z0-9_]* { 370 yylval->sValue = strdup(yytext+1); return MZN_TI_ENUM_IDENTIFIER; } 371 372$[A-Za-z][A-Za-z0-9_]* { 373 yylval->sValue = strdup(yytext+1); return MZN_TI_IDENTIFIER; } 374 375"(" { yy_push_state(bracket_exp,yyscanner); return *yytext; } 376<bracket_exp>")" { yy_pop_state(yyscanner); return *yytext; } 377<quoted_exp>")" { yy_pop_state(yyscanner); yy_pop_state(yyscanner); yy_push_state(string_quote,yyscanner); 378 clearBuffer(yyget_extra(yyscanner)); } 379 380\" { yy_push_state(string,yyscanner); clearBuffer(yyget_extra(yyscanner)); } 381<string,string_quote>[^\\"\xa\xd\x0]* { appendBufferString(yyget_extra(yyscanner), yytext); } 382<string,string_quote>\\n { appendBufferChar(yyget_extra(yyscanner), '\n'); } 383<string,string_quote>\\t { appendBufferChar(yyget_extra(yyscanner), '\t'); } 384<string,string_quote>\\[\\'] { appendBufferChar(yyget_extra(yyscanner), yytext[1]); } 385<string,string_quote>\\[\\"] { appendBufferChar(yyget_extra(yyscanner), yytext[1]); } 386<string>\\"(" { yylval->sValue = bufferData(yyget_extra(yyscanner)); 387 yy_push_state(quoted_exp,yyscanner); return MZN_STRING_QUOTE_START; } 388<string_quote>\\"(" { yylval->sValue = bufferData(yyget_extra(yyscanner)); 389 yy_push_state(quoted_exp,yyscanner); return MZN_STRING_QUOTE_MID; } 390<string>\" { yylval->sValue = bufferData(yyget_extra(yyscanner)); 391 yy_pop_state(yyscanner); return MZN_STRING_LITERAL; } 392<string_quote>\" { yylval->sValue = bufferData(yyget_extra(yyscanner)); 393 yy_pop_state(yyscanner); return MZN_STRING_QUOTE_END; } 394<string,string_quote>. { return (unsigned char)yytext[0]; } 395<string,string_quote>[\xa\xd\x0] { return MZN_END_OF_LINE_IN_STRING; } 396<string,string_quote><<EOF>> { yy_pop_state(yyscanner); return MZN_UNTERMINATED_STRING; } 397 398`[A-Za-z][A-Za-z0-9_]*` { 399 yylval->sValue = strdup(yytext+1); 400 yylval->sValue[strlen(yytext)-2] = 0; 401 return MZN_QUOTED_IDENTIFIER; } 402 403. { return (unsigned char)yytext[0]; } 404 405%% 406int yy_input_proc(char* buf, int size, yyscan_t yyscanner) { 407 MiniZinc::ParserState* parm = 408 static_cast<MiniZinc::ParserState*>(yyget_extra(yyscanner)); 409 return parm->fillBuffer(buf, size); 410 // work around warning that yyunput is unused 411 yyunput (0,buf,yyscanner); 412}