File: yacc_read.c

package info (click to toggle)
lp-solve 5.5.0.10-10
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 5,960 kB
  • ctags: 5,066
  • sloc: ansic: 48,376; yacc: 557; sh: 133; makefile: 57
file content (1145 lines) | stat: -rw-r--r-- 28,977 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
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
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
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
/*
   ============================================================================
   NAME : yacc_read.c

   PURPOSE : translation of lp-problem and storage in sparse matrix

   SHORT : Subroutines for yacc program to store the input in an intermediate
   data-structure. The yacc and lex programs translate the input.  First the
   problemsize is determined and the date is read into an intermediate
   structure, then readinput fills the sparse matrix.

   USAGE : call yyparse(); to start reading the input.  call readinput(); to
   fill the sparse matrix.
   ============================================================================
   Rows : contains the amount of rows + 1. Rows-1 is the amount of constraints
   (no bounds) Rows also contains the rownr 0 which is the objective function

   Columns : contains the amount of columns (different variable names found in
   the constraints)

   Nonnuls : contains the amount of nonnuls = sum of different entries of all
   columns in the constraints and in the objectfunction

   Hash_tab : contains all columnnames on the first level of the structure the
   row information is kept under each column structure in a linked list (also
   the objective funtion is in this structure) Bound information is also
   stored under under the column name

   First_rside : points to a linked list containing all relational operators
   and the righthandside values of the constraints the linked list is in
   reversed order with respect to the rownumbers
   ============================================================================ */
#include <string.h>
#include <limits.h>
#include <setjmp.h>
#include "lpkit.h"
#include "yacc_read.h"

#ifdef FORTIFY
# include "lp_fortify.h"
#endif


#define tol 1.0e-10
#define coldatastep 100

#define HASHSIZE       10007  /* A prime number! */

static short      Maximise;
static short      Ignore_int_decl;
static short      int_decl;
static short      Ignore_sec_decl;
static short      sos_decl;
static short      Ignore_free_decl;
static int        Rows;
static int        Columns;
static int        Non_zeros;
static short      *relat;
static int        Verbose;
static hashtable  *Hash_tab;
static hashtable  *Hash_constraints;
static jmp_buf    jump_buf;
static int        *lineno;
static int        Lin_term_count;
static char       *title;

struct structSOSvars {
  char                 *name;
  REAL                 weight;
  struct structSOSvars *next;
};

static struct structSOS {
  char                 *name;
  short                type;
  int                  Nvars;
  int                  weight;
  struct structSOSvars *SOSvars, *LastSOSvars;
  struct structSOS     *next;
} *FirstSOS, *LastSOS;

static struct _tmp_store_struct
{
  char    *name;
  int     row;
  REAL    value;
  REAL    rhs_value;
  short   relat;
} tmp_store;

static struct rside /* contains relational operator and rhs value */
{
  int           row;
  REAL          value;
  REAL          range_value;
  struct rside  *next;
  short         relat;
  short         range_relat;
  char		negate;
} *First_rside, *rs;

struct column
{
  int            row;
  REAL           value;
  struct  column *next;
  struct  column *prev;
};

static struct structcoldata {
  int               must_be_int;
  int               must_be_sec;
  int               must_be_free;
  REAL              upbo;
  REAL              lowbo;
  struct  column   *firstcol;
  struct  column   *col;
} *coldata;

static void error(int verbose, char *string)
{
  if(Verbose >= verbose)
    report(NULL, verbose, "%s on line %d\n", string, *lineno);
}

/*
 * error handling routine for yyparse()
 */
void read_error(char *string)
{
  error(CRITICAL, string);
}

/* called when lex gets a fatal error */
void lex_fatal_error(char *msg)
{
  read_error(msg);
  longjmp(jump_buf, 1);
}

void add_row()
{
  Rows++;
  rs = NULL;
  Lin_term_count = 0;
}

void check_int_sec_sos_free_decl(int within_int_decl, int within_sec_decl, int sos_decl0, int within_free_decl)
{
  Ignore_int_decl = TRUE;
  Ignore_sec_decl = TRUE;
  Ignore_free_decl = TRUE;
  sos_decl = 0;
  if(within_int_decl) {
    Ignore_int_decl = FALSE;
    int_decl = (short) within_int_decl;
  }
  else if(within_sec_decl) {
    Ignore_sec_decl = FALSE;
  }
  else if(sos_decl0) {
    sos_decl = (short) sos_decl0;
  }
  else if(within_free_decl) {
    Ignore_free_decl = FALSE;
  }
}

static void add_int_var(char *name, short int_decl)
{
  hashelem *hp;

  if((hp = findhash(name, Hash_tab)) == NULL) {
    char buf[256];

    sprintf(buf, "Unknown variable %s declared integer, ignored", name);
    error(NORMAL, buf);
  }
  else if(coldata[hp->index].must_be_int) {
    char buf[256];

    sprintf(buf, "Variable %s declared integer more than once, ignored", name);
    error(NORMAL, buf);
  }
  else {
    coldata[hp->index].must_be_int = TRUE;
    if(int_decl == 2) {
      if(coldata[hp->index].lowbo != -DEF_INFINITE * (REAL) 10.0) {
        char buf[256];

        sprintf(buf, "Variable %s: lower bound on variable redefined", name);
        error(NORMAL, buf);
      }
      coldata[hp->index].lowbo = 0;
      if(coldata[hp->index].upbo < DEF_INFINITE) {
        char buf[256];

        sprintf(buf, "Variable %s: upper bound on variable redefined", name);
        error(NORMAL, buf);
      }
      coldata[hp->index].upbo = 1;
    }
  }
}

static void add_sec_var(char *name)
{
  hashelem *hp;

  if((hp = findhash(name, Hash_tab)) == NULL) {
    char buf[256];

    sprintf(buf, "Unknown variable %s declared semi-continuous, ignored", name);
    error(NORMAL, buf);
  }
  else if(coldata[hp->index].must_be_sec) {
    char buf[256];

    sprintf(buf, "Variable %s declared semi-continuous more than once, ignored", name);
    error(NORMAL, buf);
  }
  else
    coldata[hp->index].must_be_sec = TRUE;
}

static void add_free_var(char *name)
{
  hashelem *hp;

  if((hp = findhash(name, Hash_tab)) == NULL) {
    char buf[256];

    sprintf(buf, "Unknown variable %s declared free, ignored", name);
    error(NORMAL, buf);
  }
  else if(coldata[hp->index].must_be_free) {
    char buf[256];

    sprintf(buf, "Variable %s declared free more than once, ignored", name);
    error(NORMAL, buf);
  }
  else
    coldata[hp->index].must_be_free = TRUE;
}

static int add_sos_name(char *name)
{
  struct structSOS *SOS;

  if(CALLOC(SOS, 1, struct structSOS) == NULL)
    return(FALSE);

  if(MALLOC(SOS->name, strlen(name) + 1, char) == NULL)
  {
    FREE(SOS);
    return(FALSE);
  }
  strcpy(SOS->name, name);
  SOS->type = 0;

  if(FirstSOS == NULL)
    FirstSOS = SOS;
  else
    LastSOS->next = SOS;
  LastSOS = SOS;

  return(TRUE);
}

static int add_sos_var(char *name)
{
  struct structSOSvars *SOSvar;

  if(name != NULL) {
    if(CALLOC(SOSvar, 1, struct structSOSvars) == NULL)
      return(FALSE);

    if(MALLOC(SOSvar->name, strlen(name) + 1, char) == NULL)
    {
      FREE(SOSvar);
      return(FALSE);
    }
    strcpy(SOSvar->name, name);

    if(LastSOS->SOSvars == NULL)
      LastSOS->SOSvars = SOSvar;
    else
      LastSOS->LastSOSvars->next = SOSvar;
    LastSOS->LastSOSvars = SOSvar;
    LastSOS->Nvars = LastSOS->Nvars + 1;
  }
  LastSOS->LastSOSvars->weight = 0;

  return(TRUE);
}

void storevarandweight(char *name)
{
  if(!Ignore_int_decl)
    add_int_var(name, int_decl);
  else if(!Ignore_sec_decl)
    add_sec_var(name);
  else if(sos_decl==1)
    add_sos_name(name);
  else if(sos_decl==2)
    add_sos_var(name);
  else if(!Ignore_free_decl)
    add_free_var(name);
}

int set_sos_type(int SOStype)
{
  if(LastSOS != NULL)
    LastSOS->type = (short) SOStype;
  return(TRUE);
}

int set_sos_weight(double weight, int sos_decl)
{
  if(LastSOS != NULL)
    if(sos_decl==1)
      LastSOS->weight = (int) (weight+.1);
    else
      LastSOS->LastSOSvars->weight = weight;
  return(TRUE);
}

void set_obj_dir(int maximise)
{
  Maximise = (short) maximise;
}

static int inccoldata(void)
{
  if(Columns == 0)
    CALLOC(coldata, coldatastep, struct structcoldata);
  else if((Columns%coldatastep) == 0)
    REALLOC(coldata, Columns + coldatastep, struct structcoldata);

  if(coldata != NULL) {
    coldata[Columns].upbo = (REAL) DEF_INFINITE;
    coldata[Columns].lowbo = (REAL) -DEF_INFINITE * (REAL) 10.0; /* temporary. If still this value then 0 will be taken */
    coldata[Columns].col = NULL;
    coldata[Columns].firstcol = NULL;
    coldata[Columns].must_be_int = FALSE;
    coldata[Columns].must_be_sec = FALSE;
    coldata[Columns].must_be_free = FALSE;
  }

  return(coldata != NULL);
}

/*
 * initialisation of hashstruct and globals.
 */
static int init_read(int verbose)
{
  int ok = FALSE;

  Verbose = verbose;
  set_obj_dir(TRUE);
  Rows = 0;
  Non_zeros = 0;
  Columns = 0;
  FirstSOS = LastSOS = NULL;
  Lin_term_count = 0;
  if (CALLOC(First_rside, 1, struct rside) != NULL) {
    rs = First_rside;
    rs->value = rs->range_value = 0;
    /* first row (nr 0) is always the objective function */
    rs->relat = OF;
    rs->range_relat = -1;
    Hash_tab = NULL;
    Hash_constraints = NULL;
    if (((Hash_tab = create_hash_table(HASHSIZE, 0)) == NULL) ||
        ((Hash_constraints = create_hash_table(HASHSIZE, 0)) == NULL)){
      FREE(First_rside);
      FREE(Hash_tab);
      FREE(Hash_constraints);
    }
    else
      ok = TRUE;
  }
  return(ok);
} /* init */

/*
 * clears the tmp_store variable after all information has been copied
 */
void null_tmp_store(int init_Lin_term_count)
{
  tmp_store.value = 0;
  tmp_store.rhs_value = 0;
  FREE(tmp_store.name);
  if(init_Lin_term_count)
    Lin_term_count = 0;
}

/*
 * variable : pointer to text array with name of variable
 * row      : the rownumber of the constraint
 * value    : value of matrixelement
 *            A(row, variable).
 * Sign     : (global)  determines the sign of value.
 * store()  : stores value in matrix
 *	      A(row, variable). If A(row, variable) already contains data,
 *	      value is added to the existing value.
 */
static int store(char *variable,
		  int row,
		  REAL value)
{
  hashelem *h_tab_p;
  struct column *col_p;

  if(value == 0) {
    char buf[256];

    sprintf(buf, "(store) Warning, variable %s has an effective coefficient of 0, Ignored", variable);
    error(NORMAL, buf);
    /* return(TRUE); */
  }

  if((h_tab_p = findhash(variable, Hash_tab)) == NULL) {
    if (((h_tab_p = puthash(variable, Columns, NULL, Hash_tab)) == NULL)
       ) return(FALSE);
    inccoldata();
    Columns++; /* counter for calloc of final array */
    if(value) {
      if (CALLOC(col_p, 1, struct column) == NULL)
        return(FALSE);
      Non_zeros++; /* for calloc of final arrays */
      col_p->row = row;
      col_p->value = value;
      coldata[h_tab_p->index].firstcol = coldata[h_tab_p->index].col = col_p;
    }
  }
  else if((coldata[h_tab_p->index].col == NULL) || (coldata[h_tab_p->index].col->row != row)) {
    if(value) {
      if (CALLOC(col_p, 1, struct column) == NULL)
        return(FALSE);
      Non_zeros++; /* for calloc of final arrays */
      if(coldata[h_tab_p->index].col != NULL)
        coldata[h_tab_p->index].col->prev = col_p;
      else
        coldata[h_tab_p->index].firstcol = col_p;
      col_p->value = value;
      col_p->row = row;
      col_p->next = coldata[h_tab_p->index].col;
      coldata[h_tab_p->index].col = col_p;
    }
  }
  else if(value) {
    coldata[h_tab_p->index].col->value += value;
    if(fabs(coldata[h_tab_p->index].col->value) < tol) /* eliminitate rounding errors */
      coldata[h_tab_p->index].col->value = 0;
  }
  return(TRUE);
} /* store */

static int storefirst(void)
{
    struct rside *rp;

    if ((rs != NULL) && (rs->row == tmp_store.row))
      return(TRUE);

    /* make space for the rhs information */
    if (CALLOC(rp, 1, struct rside) == NULL)
      return(FALSE);
    rp->next = First_rside;
    First_rside = rs = rp;
    rs->row = /* row */ tmp_store.row;
    rs->value = tmp_store.rhs_value;
    rs->relat = tmp_store.relat;
    rs->range_relat = -1;

    if(tmp_store.name != NULL) {
      if(tmp_store.value != 0) {
	if (!store(tmp_store.name, tmp_store.row, tmp_store.value))
	  return(FALSE);
      }
      else {
	char buf[256];

	sprintf(buf, "Warning, variable %s has an effective coefficient of 0, ignored", tmp_store.name);
	error(NORMAL, buf);
      }
    }
    null_tmp_store(FALSE);
    return(TRUE);
}

/*
 * store relational operator given in yylex[0] in the rightside list.
 * Also checks if it constraint was a bound and if so stores it in the
 * boundslist
 */
int store_re_op(char *yytext, int HadConstraint, int HadVar, int Had_lineair_sum)
{
  short tmp_relat;

  switch(yytext[0]) {

  case '=':
    tmp_relat = EQ;
    break;

  case '>':
    tmp_relat = GE;
    break;

  case '<':
    tmp_relat = LE;
    break;

  case 0:
    if(rs != NULL)
      tmp_relat = rs->relat;
    else
      tmp_relat = tmp_store.relat;
    break;

  default:
    {
      char buf[256];

      sprintf(buf, "Error: unknown relational operator %s", yytext);
      error(CRITICAL, buf);
    }
    return(FALSE);
    break;
  }

  if(/* Lin_term_count > 1 */ HadConstraint && HadVar) {/* it is not a bound */
    if(Lin_term_count <= 1)
      if(!storefirst())
        return(FALSE);
    rs->relat = tmp_relat;
  }
  else if(/* Lin_term_count == 0 */ HadConstraint && !Had_lineair_sum /* HadVar */ /* && (rs != NULL) */) { /* it is a range */
    if(Lin_term_count == 1)
      if(!storefirst())
        return(FALSE);
    if(rs == NULL) { /* range before row, already reported */
      error(CRITICAL, "Error: range for undefined row");
      return(FALSE);
    }

    if(rs->negate)
      switch (tmp_relat) {
      case LE:
        tmp_relat = GE;
        break;
      case GE:
        tmp_relat = LE;
        break;
      }

    if(rs->range_relat != -1) {
      error(CRITICAL, "Error: There was already a range for this row");
      return(FALSE);
    }
    else if(tmp_relat == rs->relat) {
      error(CRITICAL, "Error: relational operator for range is the same as relation operator for equation");
      return(FALSE);
    }
    else
      rs->range_relat = tmp_relat;
  }
  else /* could be a bound */
    tmp_store.relat = tmp_relat;

  return(TRUE);
} /* store_re_op */

int negate_constraint()
{
    if(rs != NULL)
      rs->negate = TRUE;

    return(TRUE);
}

/*
 * store RHS value in the rightside structure
 * if type = true then
 */
int rhs_store(REAL value, int HadConstraint, int HadVar, int Had_lineair_sum)
{
  if(/* Lin_term_count > 1 */ (HadConstraint && HadVar) || (Rows == 0)){ /* not a bound */
    if (Rows == 0)
      value = -value;
    /* if(Lin_term_count < 2) */
    if(rs == NULL)
      tmp_store.rhs_value += value;
    else

    if(rs == NULL) {
      error(CRITICAL, "Error: No variable specified");
      return(FALSE);
    }
    else
      rs->value += value;
  }
  else if(/* Lin_term_count == 0 */ HadConstraint && !HadVar) { /* a range */
    if(rs == NULL) /* if range before row, already reported */
      tmp_store.rhs_value += value;
    else if(rs->range_relat < 0) /* was a bad range; ignore */;
    else {
      if(rs->negate)
        value = -value;
      if(((rs->relat == LE) && (rs->range_relat == GE) &&
         (rs->value < value)) ||
        ((rs->relat == GE) && (rs->range_relat == LE) &&
         (rs->value > value)) ||
	((rs->relat == EQ) || (rs->range_relat == EQ))) {
        rs->range_relat = -2;
	error(CRITICAL, "Error: range restriction conflicts");
	return(FALSE);
      }
      else
        rs->range_value += value;
    }
  }
  else /* a bound */
    tmp_store.rhs_value += value;
  return(TRUE);
} /* RHS_store */

/*
 * store all data in the right place
 * count the amount of lineair terms in a constraint
 * only store in data-structure if the constraint is not a bound
 */
int var_store(char *var, REAL value, int HadConstraint, int HadVar, int Had_lineair_sum)
{
  int row;

  row = Rows;

  /* also in a bound the same var name can occur more than once. Check for
     this. Don't increment Lin_term_count */

  if(Lin_term_count != 1 || tmp_store.name == NULL || strcmp(tmp_store.name, var) != 0)
    Lin_term_count++;

  /* always store objective function with rownr == 0. */
  if(row == 0)
    return(store(var,  row,  value));

  if(Lin_term_count == 1) { /* don't store yet. could be a bound */
    if(MALLOC(tmp_store.name, strlen(var) + 1, char) != NULL)
      strcpy(tmp_store.name, var);
    tmp_store.row = row;
    tmp_store.value += value;
    return(TRUE);
  }

  if(Lin_term_count == 2) { /* now you can also store the first variable */
    if(!storefirst())
      return(FALSE);
    /* null_tmp_store(FALSE); */
  }

  return(store(var, row, value));
} /* var_store */



/*
 * store the information in tmp_store because it is a bound
 */
int store_bounds(int warn)
{
  if(tmp_store.value != 0) {
    hashelem *h_tab_p;
    REAL boundvalue;

    if((h_tab_p = findhash(tmp_store.name, Hash_tab)) == NULL) {
      /* a new columnname is found, create an entry in the hashlist */
      if ((h_tab_p = puthash(tmp_store.name, Columns, NULL, Hash_tab)) == NULL) {
        error(CRITICAL, "Not enough memory");
        return(FALSE);
      }
      inccoldata();
      Columns++; /* counter for calloc of final array */
    }

    if(tmp_store.value < 0) { /* divide by negative number, */
      /* relational operator may change */
      if(tmp_store.relat == GE)
	tmp_store.relat = LE;
      else if(tmp_store.relat == LE)
	tmp_store.relat = GE;
    }

    boundvalue = tmp_store.rhs_value / tmp_store.value;

#if FALSE
    /* Check sanity of bound; all variables should be positive */
    if(   ((tmp_store.relat == EQ) && (boundvalue < 0))
       || ((tmp_store.relat == LE) && (boundvalue < 0))) { /* Error */
      error(CRITICAL, "Error: variables must always be non-negative");
      return(FALSE);
    }
#endif

#if FALSE
    if((tmp_store.relat == GE) && (boundvalue <= 0)) /* Warning */
      error(NORMAL, "Warning: useless bound; variables are always >= 0");
#endif

    /* bound seems to be sane, add it */
    if((tmp_store.relat == GE) || (tmp_store.relat == EQ)) {
      if(boundvalue > coldata[h_tab_p->index].lowbo - tol)
	coldata[h_tab_p->index].lowbo = boundvalue;
      else if(warn)
        error(NORMAL, "Ineffective lower bound, ignored");
    }
    if((tmp_store.relat == LE) || (tmp_store.relat == EQ)) {
      if(boundvalue < coldata[h_tab_p->index].upbo + tol)
	coldata[h_tab_p->index].upbo  = boundvalue;
      else if (warn)
        error(NORMAL, "Ineffective upper bound, ignored");
    }

    /* check for empty range */
    if((warn) && (coldata[h_tab_p->index].upbo + tol < coldata[h_tab_p->index].lowbo)) {
      error(CRITICAL, "Error: bound contradicts earlier bounds");
      return(FALSE);
    }
  }
  else /* tmp_store.value = 0 ! */ {
    char buf[256];

    if((tmp_store.rhs_value == 0) ||
       ((tmp_store.rhs_value > 0) && (tmp_store.relat == LE)) ||
       ((tmp_store.rhs_value < 0) && (tmp_store.relat == GE))) {
      sprintf(buf, "Variable %s has an effective coefficient of 0 in bound, ignored",
	      tmp_store.name);
      if(warn)
        error(NORMAL, buf);
    }
    else {
      sprintf(buf, "Error, variable %s has an effective coefficient of 0 in bound",
	      tmp_store.name);
      error(CRITICAL, buf);
      return(FALSE);
    }
  }

  /* null_tmp_store(FALSE); */
  tmp_store.rhs_value = 0;

  return(TRUE);
} /* store_bounds */

int set_title(char *name)
{
  title = strdup(name);
  return(TRUE);
}

int add_constraint_name(char *name)
{
  int row;
  hashelem *hp;

  if((hp = findhash(name, Hash_constraints)) != NULL) {
    row = hp->index;
    rs = First_rside;
    while ((rs != NULL) && (rs->row != row))
      rs = rs->next;
  }
  else {
    row = Rows;
    if (((hp = puthash(name, row, NULL, Hash_constraints)) == NULL)
       ) return(FALSE);
    if(row)
      rs = NULL;
  }

  return(TRUE);
}

/*
 * transport the data from the intermediate structure to the sparse matrix
 * and free the intermediate structure
 */
static int readinput(lprec *lp)
{
  int    i, count, index;
  struct column *cp, *tcp;
  hashelem *hp;
  struct rside *rp;
  MYBOOL *negate = NULL;
  REAL *row = NULL, a;
  int *rowno = NULL;

  if(lp != NULL) {
    if (CALLOC(negate, 1 + Rows, MYBOOL) == NULL)
      return(FALSE);

    /* fill names with the rownames */
    hp = Hash_constraints->first;
    while(hp != NULL) {
      if (!set_row_name(lp, hp->index, hp->name))
        return(FALSE);
      hp = hp->nextelem;
    }
  }

  for(i = Rows;i >= 0;i--) {
    rp = First_rside;
    if((lp != NULL) && (rp != NULL)) {
      negate[i] = rp->negate;
      if (rp->negate) {
        switch (rp->relat) {
        case LE:
          rp->relat = GE;
          break;
        case GE:
          rp->relat = LE;
          break;
        }
        switch (rp->range_relat) {
        case LE:
          rp->range_relat = GE;
          break;
        case GE:
          rp->range_relat = LE;
          break;
        }
        rp->range_value = -rp->range_value;
        rp->value = -rp->value;
      }

      if((rp->range_relat >= 0) && (rp->value == lp->infinite)) {
        rp->value = rp->range_value;
        rp->relat = rp->range_relat;
        rp->range_relat = -1;
      }
      else if((rp->range_relat >= 0) && (rp->value == -lp->infinite)) {
        rp->value = rp->range_value;
        rp->relat = rp->range_relat;
        rp->range_relat = -1;
      }
      if ((rp->range_relat >= 0) && (rp->range_value == rp->value)) {
        rp->relat = EQ;
        rp->range_relat = EQ;
      }
      if(i) {
        set_constr_type(lp, i, rp->relat);
        relat[i] = rp->relat;
      }
      set_rh(lp, i, rp->value);
      if (rp->range_relat >= 0)
        /* lp->orig_upbo[i] = my_abs(rp->range_value - rp->value); */
        /* set_uprange(lp, i, my_abs(rp->range_value - rp->value)); */
        set_rh_range(lp, i, rp->range_value - rp->value);
    }
    if(rp != NULL) {
      First_rside = rp->next;
      free(rp); /* free memory when data has been read */
    }
    else
      First_rside = NULL;
  }

  while(First_rside != NULL) {
    rp = First_rside;
    First_rside = rp->next;
    free(rp); /* free memory when data has been read */
  }

  /* start reading the Hash_list structure */
  index = 0;

  if((lp != NULL) &&
     ((MALLOC(row, 1 + Rows, REAL) == NULL) || (MALLOC(rowno, 1 + Rows, int) == NULL))) {
    FREE(negate);
    FREE(row);
    FREE(rowno);
    return(FALSE);
  }

  /* for(i = 0; i < Hash_tab->size; i++) {
    hp = Hash_tab->table[i]; */
    hp = Hash_tab->first;
    while(hp != NULL) {
      count = 0;
      index++;
      cp = coldata[hp->index].firstcol;
      while(cp != NULL) {
        if(lp != NULL) {
	  rowno[count] = cp->row;
	  a = cp->value;
	  if (negate[cp->row])
	    a = -a;
          row[count++] = a;
	}
	tcp = cp;
	/* cp = cp->next; */
	cp = cp->prev;
	free(tcp); /* free memory when data has been read */
      }

      if(lp != NULL) {
        add_columnex(lp, count, row, rowno);
        /* check for bound */
        if(coldata[hp->index].lowbo == -DEF_INFINITE * 10.0)
  	  /* lp->orig_lowbo[Rows+index] = 0.0; */
          set_lowbo(lp, index, 0);
        else
  	  /* lp->orig_lowbo[Rows+index] = coldata[hp->index].lowbo; */
          set_lowbo(lp, index, coldata[hp->index].lowbo);
        /* lp->orig_upbo[Rows+index] = coldata[hp->index].upbo; */
        set_upbo(lp, index, coldata[hp->index].upbo);

        /* check if it must be an integer variable */
        if(coldata[hp->index].must_be_int) {
  	  /* lp->must_be_int[Rows + index]=TRUE; */
          set_int(lp, index, TRUE);
        }
        if(coldata[hp->index].must_be_sec) {
          set_semicont(lp, index, TRUE);
        }
        if(coldata[hp->index].must_be_free) {
          set_unbounded(lp, index);
        }

        /* copy name of column variable */
        if (!set_col_name(lp, index, hp->name)) {
          FREE(negate);
          FREE(row);
	  FREE(rowno);
          return(FALSE);
        }

        /* put matrix values in intermediate row */
        /* cp = hp->col; */
        /* cp = hp->firstcol; */
      }

      /* thp = hp; */
      /* hp = hp->next; */
      /* free(thp->name); */
      /* free(thp); */ /* free memory when data has been read */

      hp = hp->nextelem;

    }
    /* Hash_tab->table[i] = NULL; */

  FREE(coldata);

  while(FirstSOS != NULL)
  {
    struct structSOSvars *SOSvars, *SOSvars1;
    int *sosvars, n;
    REAL *weights;
    hashelem *hp;

    LastSOS = FirstSOS;
    FirstSOS = FirstSOS->next;
    SOSvars = LastSOS->SOSvars;
    if(lp != NULL) {
      MALLOC(sosvars, LastSOS->Nvars, int);
      MALLOC(weights, LastSOS->Nvars, double);
    }
    else {
      sosvars = NULL;
      weights = NULL;
    }
    n = 0;
    while(SOSvars != NULL)
    {
      SOSvars1 = SOSvars;
      SOSvars = SOSvars->next;
      if((lp != NULL) && ((hp = findhash(SOSvars1->name, lp->colname_hashtab)) != NULL)) {
        sosvars[n] = hp->index;
	weights[n++] = SOSvars1->weight;
      }
      FREE(SOSvars1->name);
      FREE(SOSvars1);
    }
    if(lp != NULL) {
      add_SOS(lp, LastSOS->name, LastSOS->type, LastSOS->weight, n, sosvars, weights);
      FREE(weights);
      FREE(sosvars);
    }
    FREE(LastSOS->name);
    FREE(LastSOS);
  }

  /* the following should be replaced by a call to the MPS print routine MB */

#if 0
  if(Verbose) {
    int j;

    printf("\n");
    printf("**********Data read**********\n");
    printf("Rows    : %d\n", Rows);
    printf("Columns : %d\n", Columns);
    printf("Nonnuls : %d\n", Non_zeros);
    printf("NAME          LPPROB\n");
    printf("ROWS\n");
    for(i = 0; i <= Rows; i++) {
      if(relat[i] == LE)
	printf(" L  ");
      else if(relat[i] == EQ)
	printf(" E  ");
      else if(relat[i] == GE)
	printf(" G  ");
      else if(relat[i] == OF)
	printf(" N  ");
      printf("%s\n", get_row_name(lp, i));
    }

    printf("COLUMNS\n");
    j = 0;
    for(i = 0; i < Non_zeros; i++) {
      if(i == lp->col_end[j])
	j++;
      printf("    %-8s  %-8s  %g\n", get_col_name(lp, j),
	     get_row_name(lp, lp->mat[i].row_nr), (double)lp->mat[i].value);
    }

    printf("RHS\n");
    for(i = 0; i <= Rows; i++) {
      printf("    RHS       %-8s  %g\n", get_row_name(lp, i),
	     (double)lp->orig_rhs[i]);
    }

    printf("RANGES\n");
    for(i = 1; i <= Rows; i++)
      if((lp->orig_upbo[i] != lp->infinite) && (lp->orig_upbo[i] != 0)) {
	printf("    RGS       %-8s  %g\n", get_row_name(lp, i),
	       (double)lp->orig_upbo[i]);
      }
      else if((lp->orig_lowbo[i] != 0)) {
	printf("    RGS       %-8s  %g\n", get_row_name(lp, i),
	       (double)-lp->orig_lowbo[i]);
      }

    printf("BOUNDS\n");
    for(i = Rows + 1; i <= Rows + Columns; i++) {
      if((lp->orig_lowbo[i] != 0) && (lp->orig_upbo[i] < lp->infinite) &&
         (lp->orig_lowbo[i] == lp->orig_upbo[i])) {
        printf(" FX BND       %-8s  %g\n", get_col_name(lp, i - Rows),
               (double)lp->orig_upbo[i]);
      }
      else {
        if(lp->orig_upbo[i] < lp->infinite)
  	  printf(" UP BND       %-8s  %g\n", get_col_name(lp, i - Rows),
  		 (double)lp->orig_upbo[i]);
        if(lp->orig_lowbo[i] > 0)
  	  printf(" LO BND       %-8s  %g\n", get_col_name(lp, i - Rows),
  		 (double)lp->orig_lowbo[i]);
      }
    }

    printf("ENDATA\n");
  }
#endif

  FREE(row);
  FREE(rowno);
  FREE(negate);
  return(TRUE);
} /* readinput */

lprec *yacc_read(lprec *lp, int verbose, char *lp_name, int *_lineno, int (*parse) (void), void (*delete_allocated_memory) (void))
{
  REAL *orig_upbo;
  int stat = -1;
  lprec *lp0 = lp;

  lineno = _lineno;
  title = lp_name;

  if(!init_read(verbose))
    error(CRITICAL, "init_read failed");
  else if (setjmp(jump_buf) == 0)
    stat = parse();

  delete_allocated_memory();

  Rows--;

  relat = NULL;
  if((stat != 0) || (CALLOC(relat, Rows + 1, short) != NULL)) {
    if(stat == 0) {
      if(lp == NULL) {
        lp = make_lp(Rows, 0);
      }
      else {
        int NRows;

	for(NRows = get_Nrows(lp); NRows < Rows; NRows++)
	  add_constraintex(lp, 0, NULL, NULL, LE, 0);
      }
    }
    else
      lp = NULL;
    if ((stat != 0) || (lp != NULL)) {
      if(lp != NULL) {
        set_verbose(lp, Verbose);
      }

      if (!readinput(lp)) {
	if((lp != NULL) && (lp0 == NULL))
          delete_lp(lp);
	lp = NULL;
      }

      if(lp != NULL) {
	set_lp_name(lp, title);
	if(Maximise)
	  set_maxim(lp);

	if(Rows) {
	  int row;

	  MALLOCCPY(orig_upbo, lp->orig_upbo, 1 + Rows, REAL);
	  for(row = 1; row <= Rows; row++)
	    set_constr_type(lp, row, relat[row]);

	  memcpy(lp->orig_upbo, orig_upbo, (1 + Rows) * sizeof(*orig_upbo)); /* restore upper bounds (range) */
	  FREE(orig_upbo);
	}
      }
      if((title != NULL) && (title != lp_name))
        free(title);

      free_hash_table(Hash_tab);
      free_hash_table(Hash_constraints);
    }
    FREE(relat);
  }
  null_tmp_store(FALSE);
  return(lp);
}