PbParser.C 19.2 KB
Newer Older
Masahiko Sakai's avatar
Masahiko Sakai committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/**************************************************************************************[PbParser.C]
Copyright (c) 2005-2010, Niklas Een, Niklas Sorensson

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.
**************************************************************************************************/

#include "PbParser.h"
#include "File.h"
#include "Debug.h"

Masahiko Sakai's avatar
Masahiko Sakai committed
24
// For soft constraint handling
Masahiko Sakai's avatar
Masahiko Sakai committed
25 26 27
int n_defsofts=0;
vec<Lit> soft_goal_ps; vec<Int> soft_goal_Cs;  // literls and coefficients for soft goal.

Masahiko Sakai's avatar
Masahiko Sakai committed
28 29 30 31
// For Dimacs format
bool wcnf_mode = false;
Int top_weight = Int_MAX;

Masahiko Sakai's avatar
Masahiko Sakai committed
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
//=================================================================================================
// Parser buffers (streams):


class FileBuffer {
    File    in;
    int     next;
public:
    int     line;
    FileBuffer(cchar* input_file) : in(input_file, "rb") {
        if (in.null()) reportf("ERROR! Could not open file for reading: %s\n", input_file), exit(0);
        next = in.getCharQ();
        line = 1; }
   ~FileBuffer() {}
    int  operator *  () { return next; }
    void operator ++ () { if (next == '\n') line++; next = in.getCharQ(); }
};


class StringBuffer {
    cchar*  ptr;
    cchar*  last;
public:
    int     line;
    StringBuffer(cchar* text)           { ptr = text; last = ptr + strlen(text); line = 1; }
    StringBuffer(cchar* text, int size) { ptr = text; last = ptr + size; line = 1; }
   ~StringBuffer() {}
    int  operator *  () { return (ptr >= last) ? EOF : *ptr; }
    void operator ++ () { if (*ptr == '\n') line++; ++ptr; }
};



//=================================================================================================
// PB Parser:


/*
The 'B' (parser Buffer) parameter should implement:
    
    operator *      -- Peek at current token. Should return a character 0-255 or 'EOF'.
    operator ++     -- Advance to next token.
    line            -- Public member variable.

The 'S' (Solver) parameter should implement:

    void allocConstrs(int n_vars, int n_constrs)
        -- Called before any of the below methods. Sets the size of the problem.

    int  getVar(cchar* name)
        -- Called during parsing to convert string names to indices. Ownership of 'name' 
        remains with caller (the string should be copied).

    void addGoal(const vec<Lit>& ps, const vec<Int>& Cs)
        -- Called before any constraint is adde to establish goal function:
                "minimize( Cs[0]*ps[0] + ... + Cs[n-1]*ps[n-1] )"

    bool addConstr(const vec<Lit>& ps, const vec<Int>& Cs, Int rhs, int ineq)
        -- Called when a constraint has been parsed. Constraint is of type:
                "Cs[0]*ps[0] + ... + Cs[n-1]*ps[n-1] >= rhs" ('rhs'=right-hand side). 
        'ineq' determines the inequality used: -2 for <, -1 for <=, 0 for ==, 1 for >=, 2 for >. 
        Should return TRUE if successful, FALSE if conflict detected.
*/


template<class B>
static void skipWhitespace(B& in) {     // not including newline
Masahiko Sakai's avatar
Masahiko Sakai committed
99
    while (*in == ' ' || *in == '\t' || *in == '\r')
Masahiko Sakai's avatar
Masahiko Sakai committed
100 101 102 103 104 105 106 107 108 109 110
        ++in; }

template<class B>
static void skipLine(B& in) {
    for (;;){
        if (*in == EOF) return;
        if (*in == '\n') { ++in; return; }
        ++in; } }

template<class B>
static void skipComments(B& in) {      // skip comment and empty lines (assuming we are at beginning of line)
Masahiko Sakai's avatar
Masahiko Sakai committed
111
  //    reportf("skipComments (%c)\n", *in);
Masahiko Sakai's avatar
Masahiko Sakai committed
112 113 114 115
    while (*in == '*' || *in == '\n') skipLine(in); }

template<class B>
static bool skipEndOfLine(B& in) {     // skip newline AND trailing comment/empty lines
Masahiko Sakai's avatar
Masahiko Sakai committed
116 117
  //reportf("skipEndOfLine (%c)\n", *in);
    skipWhitespace(in);
Masahiko Sakai's avatar
Masahiko Sakai committed
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
    if (*in == '\n') ++in;
    else             return false;
    return true; }

template<class B>
static bool skipText(B& in, cchar* text) {
    while (*text != 0){
        if (*in != *text) return false;
        ++in, ++text; }
    return true; }

template<class B>
static Int parseInt(B& in) {
    Int     val(0);
    bool    neg = false;
    skipWhitespace(in);
    if      (*in == '-') neg = true, ++in;
    else if (*in == '+') ++in;
    skipWhitespace(in);     // BE NICE: allow "- 3" and "+  4" etc.
    if (*in < '0' || *in > '9')
        throw nsprintf("Expected digit, not: %c", *in);
    while (*in >= '0' && *in <= '9'){
      #ifdef NO_GMP
        val *= 2;
        if (val < 0 || val > Int(9223372036854775807LL >> 20)) throw xstrdup("Integer overflow. Use BigNum-version.");      // (20 extra bits should be enough...)
        val *= 5;
      #else
        val *= 10;
      #endif
        val += (*in - '0');
        ++in; }
    return neg ? -val : val; }

template<class B>
static char* parseIdent(B& in, vec<char>& tmp) {   // 'tmp' is cleared, then filled with the parsed string. '(char*)tmp' is returned for convenience.
    skipWhitespace(in);
    if ((*in < 'a' || *in > 'z') && (*in < 'A' || *in > 'Z') && *in != '_') throw nsprintf("Expected start of identifier, not: %c", *in);
    tmp.clear();
    tmp.push(*in);
    ++in;
    while ((*in >= 'a' && *in <= 'z') || (*in >= 'A' && *in <= 'Z') || (*in >= '0' && *in <= '9') || *in == '_')
        tmp.push(*in),
        ++in;
    tmp.push(0);
    return (char*)tmp; }


template<class B, class S>
void parseExpr(B& in, S& solver, vec<Lit>& out_ps, vec<Int>& out_Cs, vec<char>& tmp, bool old_format)
    // NOTE! only uses "getVar()" method of solver; doesn't add anything.
    // 'tmp' is a tempory, passed to avoid frequent memory reallocation.
{
    bool empty = true;
    for(;;){
        skipWhitespace(in);
        if ((*in < '0' || *in > '9') && *in != '+' && *in != '-') break;
        out_Cs.push(parseInt(in));
        skipWhitespace(in);
        if (old_format){
            if (*in != '*') throw xstrdup("Missing '*' after coefficient.");
            ++in;
	} else {
	    if(*in == '*') throw xstrdup("May be in old format.  Try option \'-of\'.");
	}
	if(*in == '~') {
	  ++in;
	  out_ps.push(Lit(solver.getVar(parseIdent(in, tmp)),true));
	} else
	  out_ps.push(Lit(solver.getVar(parseIdent(in, tmp))));
        empty = false;
    }
    if (empty) throw xstrdup("Empty expression.");
}


template<class B, class S>
void parseSize(B& in, S& solver)
{
    int n_vars, n_constrs;

    if (*in != '*') return;
    ++in;
    skipWhitespace(in);

    if (!skipText(in, "#variable=")) goto Abort;
    n_vars = toint(parseInt(in));

    skipWhitespace(in);
    if (!skipText(in, "#constraint=")) goto Abort;
    n_constrs = toint(parseInt(in));

    solver.allocConstrs(n_vars, n_constrs);

  Abort:
    skipLine(in);
    skipComments(in);
}

// Parsing variables interested in model count.
template<class B, class S>
void parseInterestVars(B& in, S& solver)
{
    //  reportf("parseInterestVars\n");
    skipWhitespace(in);
    if (!skipText(in, "interest:")) return;      // Not specified. If file is syntactically correct, no characters will have been consumed (expecting integer).

    vec<Var> vs; 
    skipWhitespace(in);
    if (*in == ';'){
Masahiko Sakai's avatar
Masahiko Sakai committed
227
      ++in; skipWhitespace(in);
Masahiko Sakai's avatar
Masahiko Sakai committed
228 229 230 231 232
        skipLine(in);
    }else{
      vec<char> tmp;
      for(;;){
        skipWhitespace(in);
Masahiko Sakai's avatar
Masahiko Sakai committed
233
	//        if (!('a' <= *in && *in <= 'z') && !('A' <= *in && *in <= 'Z')) throw xstrdup("Invalid literal.");
Masahiko Sakai's avatar
Masahiko Sakai committed
234 235 236 237 238
	vs.push(solver.getVar(parseIdent(in, tmp)));
      }
      skipWhitespace(in);
      if (!skipText(in, ";")) throw xstrdup("Expecting ';' after variable sequence.");
    }
Masahiko Sakai's avatar
Masahiko Sakai committed
239 240
    //skipEndOfLine(in);
    if (!skipEndOfLine(in)) throw xstrdup("Garbage after ';'.");
Masahiko Sakai's avatar
Masahiko Sakai committed
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266

    if(opt_verbosity >= 2) {
      /**/reportf("interest vars: "); 
      if(opt_verbosity >= 3) for(int i=0; i<vs.size(); i++) dump(Lit(vs[i])),reportf("(%d), ", vs[i]); reportf("\n");
    }
    if(vs.size() > 0) {
      assert(vs.size()==vs[vs.size()-1]+1);
      solver.addInterestVars(vs[vs.size()-1]+1);
    }
}

template<class B, class S>
void parseGoal(B& in, S& solver, bool old_format)
{
    skipWhitespace(in);
    if (skipText(in, "soft:")) {
      opt_command = cmd_Soft ;
      skipWhitespace(in);
      if (*in == ';'){
	++in;
      }else{
	opt_goal = parseInt(in) - 1 ;
	//      dump(opt_goal),reportf("\n");
	skipWhitespace(in);
	if (!skipText(in, ";")) throw xstrdup("Expecting ';' after goal function.");
      }
Masahiko Sakai's avatar
Masahiko Sakai committed
267 268
      //skipEndOfLine(in);
      if (!skipEndOfLine(in)) throw xstrdup("Garbage after ';'.");
Masahiko Sakai's avatar
Masahiko Sakai committed
269 270 271 272 273 274 275 276 277 278 279 280 281
      return;
    } else if (!skipText(in, "min:")) return;      // No goal specified. If file is syntactically correct, no characters will have been consumed (expecting integer).

    vec<Lit> ps; vec<Int> Cs; vec<char> tmp;
    skipWhitespace(in);
    if (*in == ';'){
        ++in;
        skipLine(in);
    }else{
        parseExpr(in, solver, ps, Cs, tmp, old_format);
        skipWhitespace(in);
        if (!skipText(in, ";")) throw xstrdup("Expecting ';' after goal function.");
    }
Masahiko Sakai's avatar
Masahiko Sakai committed
282 283
    //skipEndOfLine(in);
    if (!skipEndOfLine(in)) throw xstrdup("Garbage after ';'.");
Masahiko Sakai's avatar
Masahiko Sakai committed
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320

    solver.addGoal(ps, Cs);
    solver.addInterestVars(ps.size());
}

// -2 for <, -1 for <=, 0 for ==, 1 for >=, 2 for >.
template<class B>
int parseInequality(B& in)
{
    int ineq;
    skipWhitespace(in);
    if (*in == '<'){
        ++in;
        if (*in == '=') ineq = -1, ++in;
        else            ineq = -2;
    }else if (*in == '>'){
        ++in;
        if (*in == '=') ineq = +1, ++in;
        else            ineq = +2;
    }else{
        if (*in == '='){
            ++in;
            if (*in == '=') ++in;
            ineq = 0;
        }else
            throw nsprintf("Expected inequality, not: %c", *in);
    }
    return ineq;
}

template<class B, class S>
bool parseConstrs(B& in, S& solver, bool old_format)
{
  vec<Lit> ps; vec<Int> Cs; vec<char> tmp; 
    int     ineq;
    Int     rhs;
    while (*in != EOF){
Masahiko Sakai's avatar
Masahiko Sakai committed
321 322
      Lit dlt1=lit_Undef,dlt2=lit_Undef; //reportf("(%c)",*in);
      if (*in == '*' || *in == '\n') {skipComments(in); continue;}
Masahiko Sakai's avatar
Masahiko Sakai committed
323
      if (*in == 'd') { // definition of constraint
Masahiko Sakai's avatar
Masahiko Sakai committed
324
	Lit tl;
Masahiko Sakai's avatar
Masahiko Sakai committed
325 326 327 328 329 330 331
	//**/ reportf("Definition detected");
	++in;
        skipWhitespace(in);
	if(*in == '~') {
	  ++in;
	  //	  dlp = new (xmalloc<Lit>(1))
	  //	    Lit(solver.getVar(parseIdent(in, tmp)),true);
Masahiko Sakai's avatar
Masahiko Sakai committed
332
            tl = Lit(solver.getVar(parseIdent(in, tmp)),true);
Masahiko Sakai's avatar
Masahiko Sakai committed
333 334
	    //	} else dlp = new (xmalloc<Lit>(1))
	    //		 Lit(solver.getVar(parseIdent(in, tmp)));
Masahiko Sakai's avatar
Masahiko Sakai committed
335
	} else tl = Lit(solver.getVar(parseIdent(in, tmp)));
Masahiko Sakai's avatar
Masahiko Sakai committed
336
	skipWhitespace(in);
Masahiko Sakai's avatar
Masahiko Sakai committed
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
        if (*in == '<'){
          ++in;
	  if (*in == '='){
	    ++in;
	    dlt2=tl;
	    if (*in == '>'){
	      ++in;
              dlt1=tl;
	    }
	  } else throw nsprintf("Expecting '<=>' or '<=' in definition.");
	} else if (*in == '='){
	  ++in;
	  if (*in == '>'){
	    ++in;
	    dlt1=tl;
	  } else throw nsprintf("Expecting '=>' in definition.");
	} else throw nsprintf("Expecting '<=>', '<=', or '=>' in definition.");
Masahiko Sakai's avatar
Masahiko Sakai committed
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
      } else if (*in == '[') { // soft constraint
	//**/ reportf("Soft constraint detected. ");
	++in;
	skipWhitespace(in);
 
	char        buf[30];
	sprintf(buf, "@defsofts%d", n_defsofts++);
	dlt1 = Lit(solver.getVar(buf),true);    // negative literal
 
	//  Add a code to register (weight,dlt1) into the goal.
	soft_goal_ps.push(~dlt1),soft_goal_Cs.push(parseInt(in));
	skipWhitespace(in);
	if (!skipText(in, "]")) throw xstrdup("Expecting ']' in definition.");
      }
      
      // normal constraint
      parseExpr(in, solver, ps, Cs, tmp, old_format);
      ineq = parseInequality(in);
      rhs  = parseInt(in);
      
      skipWhitespace(in);
Masahiko Sakai's avatar
Masahiko Sakai committed
375 376 377 378
      if (!skipText(in, ";")) throw xstrdup("Expecting ';' after constraint."); 
      // skipEndOfLine(in);
      if (!skipEndOfLine(in)) throw xstrdup("Garbage after ';'.");
     
Masahiko Sakai's avatar
Masahiko Sakai committed
379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460
      if (!solver.addConstr(ps, Cs, rhs, ineq, Int_MAX, -2, dlt1, dlt2))
	return false;
      ps.clear();
      Cs.clear();
    }
    return true;
}


//=================================================================================================
// Main parser functions:


template<class B, class S>
static bool parse_PB(B& in, S& solver, bool old_format, bool abort_on_error)
{
    try{
        parseSize(in, solver);
	parseInterestVars(in, solver);
        parseGoal(in, solver, old_format);
        solver.var_dec_mode = !opt_branch_goal_vars;
	bool result = parseConstrs(in, solver, old_format);
	if(opt_command == cmd_Soft)
	  solver.addGoal(soft_goal_ps, soft_goal_Cs);
	return result;

    }catch (cchar* msg){
        if (abort_on_error){
             reportf("PARSE ERROR! [line %d] %s\n", in.line, msg);
            xfree(msg);
            if (opt_satlive && !opt_try)
	      printf("s UNSUPPORTED\n");
            exit(opt_try ? 5 : 0);
        }else
            throw msg;
    }
}

// PB parser functions: Returns TRUE if successful, FALSE if conflict detected during parsing.
// If 'abort_on_error' is false, a 'cchar*' error message may be thrown.
//
void parse_PB_file(cchar* filename, PbSolver& solver, bool old_format, bool abort_on_error) {
    FileBuffer buf(filename);
    parse_PB(buf, solver, old_format, abort_on_error); }

//=================================================================================================
// Debug:


#if 0
#include "Debug.h"
#include "Map.h"
#define Solver DummySolver

struct DummySolver {
    Map<cchar*, int> name2index;
    vec<cchar*>      index2name;

    int getVar(cchar* name) {
        int ret;
        if (!name2index.peek(name, ret)){
            index2name.push(xstrdup(name));
            ret = name2index.set(index2name.last(), index2name.size()-1); }
        return ret; }

    void alloc(int n_vars, int n_constrs) {
        printf("alloc(%d, %d)\n", n_vars, n_constrs); }
    void addGoal(vec<Lit>& ps, vec<Int>& Cs) {
        printf("MIN: "); dump(ps, Cs); printf("\n"); }
    bool addConstr(vec<Lit>& ps, vec<Int>& Cs, Int rhs, int ineq) {
        static cchar* ineq_name[5] = { "<", "<=" ,"==", ">=", ">" };
        printf("CONSTR: "); dump(ps, Cs); printf(" %s ", ineq_name[ineq+2]); dump(rhs); printf("\n");
        return true; }
};

void test(void)
{
    DummySolver     solver;
    debug_names = &solver.index2name;
    parseFile_PB("test.pb", solver, true);
}
#endif
Masahiko Sakai's avatar
Masahiko Sakai committed
461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617


//================================================================================================
// Dimacs Parser:

template<class B>
static void skipDimacsComments(B& in) {      // skip comment and empty lines (assuming we are at beginning of line)
  //    reportf("skipDimacsComments (%c)\n", *in);
    while (*in == 'c' || *in == '\n') skipLine(in); }

template<class B>
static bool skipDimacsEndOfLine(B& in) {     // skip newline AND trailing comment/empty lines
  //    reportf("skipDimacsEndOfLine (%c)\n", *in);
    skipWhitespace(in);
    if (*in == '\n') ++in;
    else             return false;
    skipDimacsComments(in);
    return true; }

template<class B, class S>
void parseDimacsSize(B& in, S& solver)  // return true if wcnf mode
{
    int n_vars, n_constrs;

    skipDimacsComments(in);
    
    if (*in != 'p') return;
    ++in;
    skipWhitespace(in);

    if (!skipText(in, "cnf")) {
      if (skipText(in, "wcnf"))
	wcnf_mode=true;
      else goto Abort;
    }    

    skipWhitespace(in);
    n_vars = toint(parseInt(in));
    skipWhitespace(in);
    n_constrs = toint(parseInt(in));

    solver.allocConstrs(n_vars, n_constrs);

    skipWhitespace(in);
    if (*in != '\n')
      top_weight = parseInt(in);

    reportf("DimacsSize: n_vars=%d, n_constrs=%d, top_weight=", n_vars, n_constrs); dump(top_weight); reportf("\n");
    
  Abort:
    skipLine(in);
    skipDimacsComments(in);
}

template<class B>
static char* parseIntAsIdent(B& in, vec<char>& tmp) {   // 'tmp' is cleared, then filled with the parsed string. '(char*)tmp' is returned for convenience.
    skipWhitespace(in);
    if (*in < '1' || *in > '9') throw nsprintf("Expected start of variable, not: %c", *in);
    tmp.clear();
    tmp.push(*in);
    ++in;
    while (*in >= '0' && *in <= '9')
        tmp.push(*in),
        ++in;
    tmp.push(0);
    return (char*)tmp; }


template<class B, class S>
bool parseDimacsConstrs(B& in, S& solver, bool max_sat_mode)
{
  vec<Lit> ps; vec<Int> Cs; Int weight; vec<char> tmp;

    while (*in != EOF){
      Lit dlt1=lit_Undef;
      bool empty = true;

      //      reportf("reading clause:%c\n", *in);
      skipWhitespace(in); 
      //      if ((*in < '0' || *in > '9') && *in != '-') throw xstrdup("Invalid literal.");
      if (max_sat_mode) {
	weight = 1;
	if (wcnf_mode)
	  weight = parseInt(in);
	//	reportf("clause with weight "),dump(weight),reportf("\n");
	if(weight < top_weight) {
	  char        buf[30];
	  sprintf(buf, "@defsofts%d", n_defsofts++);
	  dlt1 = Lit(solver.getVar(buf),true);    // negative literal
	  
	  //  Add a code to register (weight,dlt1) into the goal.
	  //          reportf("adding a literal for goal with weight "),dump(weight),reportf("\n");
	  soft_goal_ps.push(~dlt1),soft_goal_Cs.push(weight);
	}
      }
 
      // normal constraint
      for(;;){
	skipWhitespace(in);
        if(*in == '0') { ++in; break; }	
	if(*in == '-') {
	  ++in;
	  ps.push(Lit(solver.getVar(parseIntAsIdent(in, tmp)),true));
	} else
	  ps.push(Lit(solver.getVar(parseIntAsIdent(in, tmp))));
  	Cs.push(1);
	
        empty = false;
      }
      if (empty) throw xstrdup("Empty constraint.");
      skipDimacsEndOfLine(in);

      assert(ps.size() > 0);
      //      reportf("addConstr\n");
      if (!solver.addConstr(ps, Cs, 1, 1, Int_MAX, -2, dlt1, lit_Undef))
	return false;
      ps.clear(); Cs.clear();
      //      reportf("NextConstr\n");
    }
    return true;
}

//================================================================================================
// Dimacs main parser functions:

template<class B, class S>
static bool parse_Dimacs(B& in, S& solver, bool max_sat_mode, bool abort_on_error)
{
    Pair<bool,Int> style; Int top_weight;
    try{
        parseDimacsSize(in, solver);
	
        solver.var_dec_mode = !opt_branch_goal_vars;
	bool result = parseDimacsConstrs(in, solver, max_sat_mode);
	if(max_sat_mode && result)
	  solver.addGoal(soft_goal_ps, soft_goal_Cs);
	return result;

    }catch (cchar* msg){
        if (abort_on_error){
             reportf("PARSE ERROR! [line %d] %s\n", in.line, msg);
            xfree(msg);
            if (opt_satlive && !opt_try)
	      printf("s UNSUPPORTED\n");
            exit(opt_try ? 5 : 0);
        }else
            throw msg;
    }
}

// PB parser functions: Returns TRUE if successful, FALSE if conflict detected during parsing.
// If 'abort_on_error' is false, a 'cchar*' error message may be thrown.
//
void parse_Dimacs_file(cchar* filename, PbSolver& solver, bool max_sat_mode, bool abort_on_error) {
    FileBuffer buf(filename);
    parse_Dimacs(buf, solver, max_sat_mode, abort_on_error); }