1 #include <barvinok/util.h>
2 #include "evalue_read.h"
4 #define ALLOC(type) (type*)malloc(sizeof(type))
5 #define ALLOCN(type,n) (type*)malloc((n) * sizeof(type))
7 enum token_type
{ TOKEN_UNKNOWN
= 256, TOKEN_VALUE
, TOKEN_IDENT
, TOKEN_GE
,
8 TOKEN_NE
, TOKEN_UNION
, TOKEN_VARS
};
22 static struct token
*token_new(int line
, int col
)
24 struct token
*tok
= ALLOC(struct token
);
30 void token_free(struct token
*tok
)
32 if (tok
->type
== TOKEN_VALUE
)
33 value_clear(tok
->u
.v
);
34 else if (tok
->type
== TOKEN_IDENT
)
51 struct token
*tokens
[5];
55 static struct stream
* stream_new()
58 struct stream
*s
= ALLOC(struct stream
);
62 s
->buffer
= (char*)malloc(s
->size
);
68 for (i
= 0; i
< 5; ++i
)
74 static struct stream
* stream_new_file(FILE *file
)
76 struct stream
*s
= stream_new();
81 static struct stream
* stream_new_str(const char *str
)
83 struct stream
*s
= stream_new();
88 static int stream_getc(struct stream
*s
)
113 static void stream_ungetc(struct stream
*s
, int c
)
122 static void stream_push_char(struct stream
*s
, int c
)
124 if (s
->len
>= s
->size
) {
125 s
->size
= (3*s
->size
)/2;
126 s
->buffer
= (char*)realloc(s
->buffer
, s
->size
);
128 s
->buffer
[s
->len
++] = c
;
131 static struct token
*stream_push_token(struct stream
*s
, struct token
*tok
)
133 assert(s
->n_token
< 5);
134 s
->tokens
[s
->n_token
++] = tok
;
137 static struct token
*stream_next_token(struct stream
*s
)
144 return s
->tokens
[--s
->n_token
];
149 while ((c
= stream_getc(s
)) != -1 && isspace(c
))
171 tok
= token_new(line
, col
);
172 tok
->type
= (enum token_type
)c
;
175 if (c
== '-' || isdigit(c
)) {
176 tok
= token_new(line
, col
);
177 tok
->type
= TOKEN_VALUE
;
178 value_init(tok
->u
.v
);
179 stream_push_char(s
, c
);
180 while ((c
= stream_getc(s
)) != -1 && isdigit(c
))
181 stream_push_char(s
, c
);
184 if (s
->len
== 1 && s
->buffer
[0] == '-')
185 value_set_si(tok
->u
.v
, -1);
187 stream_push_char(s
, '\0');
188 mpz_set_str(tok
->u
.v
, s
->buffer
, 0);
192 if (c
== '#' || isalpha(c
)) {
193 tok
= token_new(line
, col
);
194 stream_push_char(s
, c
);
195 while ((c
= stream_getc(s
)) != -1 && isalnum(c
))
196 stream_push_char(s
, c
);
199 stream_push_char(s
, '\0');
200 if (!strcmp(s
->buffer
, "#variables")) {
201 tok
->type
= TOKEN_VARS
;
202 } else if (s
->buffer
[0] == '#') {
203 tok
->type
= TOKEN_UNKNOWN
;
204 } else if (!strcmp(s
->buffer
, "UNION")) {
205 tok
->type
= TOKEN_UNION
;
207 tok
->type
= TOKEN_IDENT
;
208 tok
->u
.s
= strdup(s
->buffer
);
213 if ((c
= stream_getc(s
)) == '=') {
214 tok
= token_new(line
, col
);
215 tok
->type
= TOKEN_GE
;
222 if ((c
= stream_getc(s
)) == '=') {
223 tok
= token_new(line
, col
);
224 tok
->type
= TOKEN_NE
;
231 tok
= token_new(line
, col
);
232 tok
->type
= TOKEN_UNKNOWN
;
236 void stream_error(struct stream
*s
, struct token
*tok
, char *msg
)
238 int line
= tok
? tok
->line
: s
->line
;
239 int col
= tok
? tok
->col
: s
->col
;
240 fprintf(stderr
, "syntax error (%d, %d): %s\n", line
, col
, msg
);
243 fprintf(stderr
, "got '%c'\n", tok
->type
);
245 fprintf(stderr
, "got token type %d\n", tok
->type
);
249 static void stream_free(struct stream
*s
)
252 if (s
->n_token
!= 0) {
253 struct token
*tok
= stream_next_token(s
);
254 stream_error(s
, tok
, "unexpected token");
262 struct parameter
*next
;
265 struct parameter
*parameter_new(const char *name
, int pos
, struct parameter
*next
)
267 struct parameter
*p
= ALLOC(struct parameter
);
268 p
->name
= strdup(name
);
274 static int parameter_pos(struct parameter
**p
, const char *s
, int len
)
276 int pos
= *p
? (*p
)->pos
+1 : 0;
281 for (q
= *p
; q
; q
= q
->next
) {
282 if (strncmp(q
->name
, s
, len
) == 0)
288 *p
= parameter_new(s
, pos
, *p
);
292 static int optional_power(struct stream
*s
)
296 tok
= stream_next_token(s
);
299 if (tok
->type
!= '^') {
300 stream_push_token(s
, tok
);
304 tok
= stream_next_token(s
);
305 if (!tok
|| tok
->type
!= TOKEN_VALUE
) {
306 stream_error(s
, tok
, "expecting exponent");
308 stream_push_token(s
, tok
);
311 pow
= VALUE_TO_INT(tok
->u
.v
);
316 static evalue
*evalue_read_factor(struct stream
*s
, struct parameter
**p
);
317 static evalue
*evalue_read_term(struct stream
*s
, struct parameter
**p
);
319 static evalue
*create_fract_like(struct stream
*s
, evalue
*arg
, enode_type type
,
320 struct parameter
**p
)
324 pow
= optional_power(s
);
328 e
->x
.p
= new_enode(type
, pow
+2, -1);
329 value_clear(e
->x
.p
->arr
[0].d
);
330 e
->x
.p
->arr
[0] = *arg
;
332 evalue_set_si(&e
->x
.p
->arr
[1+pow
], 1, 1);
334 evalue_set_si(&e
->x
.p
->arr
[1+pow
], 0, 1);
339 static evalue
*create_relation(evalue
*arg
, int ne
)
345 e
->x
.p
= new_enode(relation
, 2+ne
, 0);
346 value_clear(e
->x
.p
->arr
[0].d
);
347 e
->x
.p
->arr
[0] = *arg
;
350 evalue_set_si(&e
->x
.p
->arr
[1], 0, 1);
351 evalue_set_si(&e
->x
.p
->arr
[1+ne
], 1, 1);
356 static evalue
*read_fract(struct stream
*s
, struct token
*tok
, struct parameter
**p
)
360 tok
= stream_next_token(s
);
362 assert(tok
->type
== '{');
365 arg
= evalue_read_term(s
, p
);
366 tok
= stream_next_token(s
);
367 if (!tok
|| tok
->type
!= '}') {
368 stream_error(s
, tok
, "expecting \"}\"");
370 stream_push_token(s
, tok
);
374 return create_fract_like(s
, arg
, fractional
, p
);
377 static evalue
*read_periodic(struct stream
*s
, struct parameter
**p
)
385 tok
= stream_next_token(s
);
386 assert(tok
&& tok
->type
== '[');
390 list
= (evalue
**)malloc(len
* sizeof(evalue
*));
394 evalue
*e
= evalue_read_term(s
, p
);
396 stream_error(s
, NULL
, "missing argument or list element");
401 list
= (evalue
**)realloc(list
, len
* sizeof(evalue
*));
405 tok
= stream_next_token(s
);
407 stream_error(s
, NULL
, "unexpected EOF");
410 if (tok
->type
!= ',')
415 if (n
== 1 && (tok
->type
== '=' || tok
->type
== TOKEN_NE
)) {
416 int ne
= tok
->type
== TOKEN_NE
;
418 tok
= stream_next_token(s
);
419 if (!tok
|| tok
->type
!= TOKEN_VALUE
) {
420 stream_error(s
, tok
, "expecting \"0\"");
422 stream_push_token(s
, tok
);
426 tok
= stream_next_token(s
);
427 if (!tok
|| tok
->type
!= ']') {
428 stream_error(s
, tok
, "expecting \"]\"");
430 stream_push_token(s
, tok
);
434 e
= create_relation(list
[0], ne
);
439 if (tok
->type
!= ']') {
440 stream_error(s
, tok
, "expecting \"]\"");
441 stream_push_token(s
, tok
);
447 tok
= stream_next_token(s
);
448 if (tok
&& tok
->type
== '_') {
451 tok
= stream_next_token(s
);
452 if (!tok
|| tok
->type
!= TOKEN_IDENT
) {
453 stream_error(s
, tok
, "expecting identifier");
455 stream_push_token(s
, tok
);
460 pos
= parameter_pos(p
, tok
->u
.s
, -1);
462 e
->x
.p
= new_enode(periodic
, n
, pos
+1);
464 value_clear(e
->x
.p
->arr
[n
].d
);
465 e
->x
.p
->arr
[n
] = *list
[n
];
470 stream_push_token(s
, tok
);
471 e
= create_fract_like(s
, list
[0], flooring
, p
);
474 stream_error(s
, tok
, "unexpected token");
476 stream_push_token(s
, tok
);
481 evalue_free(list
[n
]);
486 /* frees product on error */
487 static evalue
*read_factor_and_multiply(struct stream
*s
, struct parameter
**p
,
491 e2
= evalue_read_factor(s
, p
);
493 stream_error(s
, NULL
, "unexpected EOF");
494 evalue_free(product
);
502 static evalue
*evalue_read_factor(struct stream
*s
, struct parameter
**p
)
507 tok
= stream_next_token(s
);
511 if (tok
->type
== '(') {
513 e
= evalue_read_term(s
, p
);
514 tok
= stream_next_token(s
);
515 if (!tok
|| tok
->type
!= ')') {
516 stream_error(s
, tok
, "expecting \")\"");
518 stream_push_token(s
, tok
);
521 } else if (tok
->type
== TOKEN_VALUE
) {
524 value_set_si(e
->d
, 1);
526 value_assign(e
->x
.n
, tok
->u
.v
);
528 tok
= stream_next_token(s
);
529 if (tok
&& tok
->type
== '/') {
531 tok
= stream_next_token(s
);
532 if (!tok
|| tok
->type
!= TOKEN_VALUE
) {
533 stream_error(s
, tok
, "expecting denominator");
535 stream_push_token(s
, tok
);
538 value_assign(e
->d
, tok
->u
.v
);
540 } else if (tok
&& tok
->type
== TOKEN_IDENT
) {
541 stream_push_token(s
, tok
);
542 e
= read_factor_and_multiply(s
, p
, e
);
544 stream_push_token(s
, tok
);
545 } else if (tok
->type
== TOKEN_IDENT
) {
546 int pos
= parameter_pos(p
, tok
->u
.s
, -1);
547 int pow
= optional_power(s
);
551 e
->x
.p
= new_enode(polynomial
, pow
+1, pos
+1);
552 evalue_set_si(&e
->x
.p
->arr
[pow
], 1, 1);
554 evalue_set_si(&e
->x
.p
->arr
[pow
], 0, 1);
555 } else if (tok
->type
== '[') {
556 stream_push_token(s
, tok
);
557 e
= read_periodic(s
, p
);
558 } else if (tok
->type
== '{') {
559 stream_push_token(s
, tok
);
560 e
= read_fract(s
, tok
, p
);
563 tok
= stream_next_token(s
);
564 if (tok
&& tok
->type
== '*') {
566 e
= read_factor_and_multiply(s
, p
, e
);
568 stream_push_token(s
, tok
);
573 static evalue
*evalue_read_term(struct stream
*s
, struct parameter
**p
)
578 e
= evalue_read_factor(s
, p
);
582 tok
= stream_next_token(s
);
586 if (tok
->type
== '+' || tok
->type
== TOKEN_VALUE
) {
588 if (tok
->type
== '+')
591 stream_push_token(s
, tok
);
592 e2
= evalue_read_term(s
, p
);
594 stream_error(s
, NULL
, "unexpected EOF");
600 stream_push_token(s
, tok
);
608 struct constraint
*next
;
609 struct constraint
*union_next
;
612 static struct constraint
*constraint_new()
614 struct constraint
*c
= ALLOC(struct constraint
);
616 c
->v
= Vector_Alloc(16);
618 c
->union_next
= NULL
;
622 static void constraint_free(struct constraint
*c
)
625 struct constraint
*next
= c
->next
? c
->next
: c
->union_next
;
632 static void constraint_extend(struct constraint
*c
, int pos
)
635 if (pos
< c
->v
->Size
)
638 v
= Vector_Alloc((3*c
->v
->Size
)/2);
639 Vector_Copy(c
->v
->p
, v
->p
, c
->v
->Size
);
644 static int evalue_read_constraint(struct stream
*s
, struct parameter
**p
,
645 struct constraint
**constraints
,
646 struct constraint
*union_next
)
649 struct constraint
*c
= NULL
;
651 while ((tok
= stream_next_token(s
))) {
654 if (tok
->type
== '+')
656 else if (tok
->type
== TOKEN_IDENT
) {
658 c
= constraint_new();
659 pos
= parameter_pos(p
, tok
->u
.s
, -1);
660 constraint_extend(c
, 1+pos
);
661 value_set_si(c
->v
->p
[1+pos
], 1);
663 } else if (tok
->type
== TOKEN_VALUE
) {
665 c
= constraint_new();
666 tok2
= stream_next_token(s
);
667 if (tok2
&& tok2
->type
== TOKEN_IDENT
) {
668 pos
= parameter_pos(p
, tok2
->u
.s
, -1);
669 constraint_extend(c
, 1+pos
);
670 value_assign(c
->v
->p
[1+pos
], tok
->u
.v
);
675 stream_push_token(s
, tok2
);
676 value_assign(c
->v
->p
[0], tok
->u
.v
);
679 } else if (tok
->type
== TOKEN_GE
|| tok
->type
== '=') {
680 int type
= tok
->type
== TOKEN_GE
;
682 tok
= stream_next_token(s
);
683 if (!tok
|| tok
->type
!= TOKEN_VALUE
|| value_notzero_p(tok
->u
.v
)) {
684 stream_error(s
, tok
, "expecting \"0\"");
686 stream_push_token(s
, tok
);
690 c
->next
= *constraints
;
691 c
->union_next
= union_next
;
698 stream_push_token(s
, tok
);
700 stream_error(s
, tok
, "unexpected token");
709 static struct constraint
*evalue_read_domain(struct stream
*s
, struct parameter
**p
,
712 struct constraint
*constraints
= NULL
;
713 struct constraint
*union_next
= NULL
;
717 tok
= stream_next_token(s
);
720 stream_push_token(s
, tok
);
723 while (evalue_read_constraint(s
, p
, &constraints
, union_next
)) {
724 tok
= stream_next_token(s
);
726 if (tok
->type
== TOKEN_UNION
) {
728 tok
= stream_next_token(s
);
730 stream_error(s
, NULL
, "unexpected EOF");
733 stream_push_token(s
, tok
);
734 union_next
= constraints
;
738 stream_push_token(s
, tok
);
739 /* empty line separates domain from evalue */
740 if (tok
->line
> line
+1)
750 struct constraint
*constraints
;
752 struct section
*next
;
755 static char **extract_parameters(struct parameter
*p
, unsigned *nparam
)
760 *nparam
= p
? p
->pos
+1 : 0;
761 params
= ALLOCN(char *, *nparam
);
762 for (i
= 0; i
< *nparam
; ++i
) {
763 struct parameter
*next
= p
->next
;
764 params
[p
->pos
] = p
->name
;
771 static Polyhedron
*constraints2domain(struct constraint
*constraints
,
772 unsigned nparam
, unsigned MaxRays
)
777 struct constraint
*c
;
778 struct constraint
*union_next
= NULL
;
780 for (n
= 0, c
= constraints
; c
; ++n
, c
= c
->next
)
782 M
= Matrix_Alloc(n
, 1+nparam
+1);
784 struct constraint
*next
= constraints
->next
;
785 union_next
= constraints
->union_next
;
786 Vector_Copy(constraints
->v
->p
+1, M
->p
[n
]+1, nparam
);
787 if (constraints
->type
)
788 value_set_si(M
->p
[n
][0], 1);
789 value_assign(M
->p
[n
][1+nparam
], constraints
->v
->p
[0]);
790 constraints
->next
= NULL
;
791 constraints
->union_next
= NULL
;
792 constraint_free(constraints
);
795 D
= Constraints2Polyhedron(M
, MaxRays
);
799 D
= DomainConcat(D
, constraints2domain(union_next
, nparam
, MaxRays
));
803 static evalue
*evalue_read_partition(struct stream
*s
, struct parameter
*p
,
805 unsigned *nparam
, unsigned MaxRays
)
807 struct section
*part
= NULL
;
808 struct constraint
*constraints
;
812 while ((constraints
= evalue_read_domain(s
, &p
, MaxRays
))) {
813 struct section
*sect
;
814 evalue
*e
= evalue_read_term(s
, &p
);
816 stream_error(s
, NULL
, "missing evalue");
819 sect
= ALLOC(struct section
);
820 sect
->constraints
= constraints
;
831 *ppp
= extract_parameters(p
, nparam
);
834 e
->x
.p
= new_enode(partition
, 2*m
, *nparam
);
836 for (j
= 0; j
< m
; ++j
) {
838 struct section
*next
= part
->next
;
839 constraints
= part
->constraints
;
840 D
= constraints2domain(part
->constraints
, *nparam
, MaxRays
);
841 EVALUE_SET_DOMAIN(e
->x
.p
->arr
[2*j
], D
);
842 value_clear(e
->x
.p
->arr
[2*j
+1].d
);
843 e
->x
.p
->arr
[2*j
+1] = *part
->e
;
852 static evalue
*evalue_read(struct stream
*s
, const char *var_list
, char ***ppp
,
853 unsigned *nvar
, unsigned *nparam
, unsigned MaxRays
)
857 struct parameter
*p
= NULL
;
862 while ((next
= strchr(var_list
, ','))) {
864 parameter_pos(&p
, var_list
, next
-var_list
);
867 if (strlen(var_list
) > 0)
868 parameter_pos(&p
, var_list
, -1);
869 nv
= p
? p
->pos
+1 : 0;
873 if (!(tok
= stream_next_token(s
)))
876 if (tok
->type
== TOKEN_VARS
) {
879 tok
= stream_next_token(s
);
880 if (!tok
|| tok
->type
!= TOKEN_IDENT
) {
881 stream_error(s
, tok
, "expecting identifier");
885 parameter_pos(&p
, tok
->u
.s
, -1);
887 tok
= stream_next_token(s
);
888 if (!tok
|| tok
->type
!= ',')
895 nv
= p
? p
->pos
+1 : 0;
898 if (tok
->type
== '(') {
899 stream_push_token(s
, tok
);
900 e
= evalue_read_term(s
, &p
);
901 *ppp
= extract_parameters(p
, nparam
);
902 } else if (tok
->type
== TOKEN_VALUE
) {
903 struct token
*tok2
= stream_next_token(s
);
905 stream_push_token(s
, tok2
);
906 stream_push_token(s
, tok
);
907 if (tok2
&& (tok2
->type
== TOKEN_IDENT
|| tok2
->type
== TOKEN_GE
))
908 e
= evalue_read_partition(s
, p
, ppp
, nparam
, MaxRays
);
910 e
= evalue_read_term(s
, &p
);
911 *ppp
= extract_parameters(p
, nparam
);
913 } else if (tok
->type
== TOKEN_IDENT
) {
914 stream_push_token(s
, tok
);
915 e
= evalue_read_partition(s
, p
, ppp
, nparam
, MaxRays
);
917 stream_error(s
, tok
, "unexpected token");
918 *nparam
= nv
== -1 ? 0 : nv
;
929 evalue
*evalue_read_from_file(FILE *in
, const char *var_list
, char ***ppp
,
930 unsigned *nvar
, unsigned *nparam
, unsigned MaxRays
)
933 struct stream
*s
= stream_new_file(in
);
934 e
= evalue_read(s
, var_list
, ppp
, nvar
, nparam
, MaxRays
);
939 evalue
*evalue_read_from_str(const char *str
, const char *var_list
, char ***ppp
,
940 unsigned *nvar
, unsigned *nparam
, unsigned MaxRays
)
943 struct stream
*s
= stream_new_str(str
);
944 e
= evalue_read(s
, var_list
, ppp
, nvar
, nparam
, MaxRays
);