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(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 free_evalue_refs(list
[n
]);
488 static evalue
*evalue_read_factor(struct stream
*s
, struct parameter
**p
)
493 tok
= stream_next_token(s
);
497 if (tok
->type
== '(') {
499 e
= evalue_read_term(s
, p
);
500 tok
= stream_next_token(s
);
501 if (!tok
|| tok
->type
!= ')') {
502 stream_error(s
, tok
, "expecting \")\"");
504 stream_push_token(s
, tok
);
507 } else if (tok
->type
== TOKEN_VALUE
) {
510 value_set_si(e
->d
, 1);
512 value_assign(e
->x
.n
, tok
->u
.v
);
514 tok
= stream_next_token(s
);
515 if (tok
&& tok
->type
== '/') {
517 tok
= stream_next_token(s
);
518 if (!tok
|| tok
->type
!= TOKEN_VALUE
) {
519 stream_error(s
, tok
, "expecting denominator");
521 stream_push_token(s
, tok
);
524 value_assign(e
->d
, tok
->u
.v
);
527 stream_push_token(s
, tok
);
528 } else if (tok
->type
== TOKEN_IDENT
) {
529 int pos
= parameter_pos(p
, tok
->u
.s
, -1);
530 int pow
= optional_power(s
);
534 e
->x
.p
= new_enode(polynomial
, pow
+1, pos
+1);
535 evalue_set_si(&e
->x
.p
->arr
[pow
], 1, 1);
537 evalue_set_si(&e
->x
.p
->arr
[pow
], 0, 1);
538 } else if (tok
->type
== '[') {
539 stream_push_token(s
, tok
);
540 e
= read_periodic(s
, p
);
541 } else if (tok
->type
== '{') {
542 stream_push_token(s
, tok
);
543 e
= read_fract(s
, tok
, p
);
546 tok
= stream_next_token(s
);
547 if (tok
&& tok
->type
== '*') {
550 e2
= evalue_read_factor(s
, p
);
552 stream_error(s
, NULL
, "unexpected EOF");
556 free_evalue_refs(e2
);
559 stream_push_token(s
, tok
);
564 static evalue
*evalue_read_term(struct stream
*s
, struct parameter
**p
)
569 e
= evalue_read_factor(s
, p
);
573 tok
= stream_next_token(s
);
577 if (tok
->type
== '+') {
580 e2
= evalue_read_term(s
, p
);
582 stream_error(s
, NULL
, "unexpected EOF");
586 free_evalue_refs(e2
);
589 stream_push_token(s
, tok
);
597 struct constraint
*next
;
598 struct constraint
*union_next
;
601 static struct constraint
*constraint_new()
603 struct constraint
*c
= ALLOC(struct constraint
);
605 c
->v
= Vector_Alloc(16);
607 c
->union_next
= NULL
;
611 static void constraint_free(struct constraint
*c
)
614 struct constraint
*next
= c
->next
? c
->next
: c
->union_next
;
621 static void constraint_extend(struct constraint
*c
, int pos
)
624 if (pos
< c
->v
->Size
)
627 v
= Vector_Alloc((3*c
->v
->Size
)/2);
628 Vector_Copy(c
->v
->p
, v
->p
, c
->v
->Size
);
633 static int evalue_read_constraint(struct stream
*s
, struct parameter
**p
,
634 struct constraint
**constraints
,
635 struct constraint
*union_next
)
638 struct constraint
*c
= NULL
;
640 while ((tok
= stream_next_token(s
))) {
643 if (tok
->type
== '+')
645 else if (tok
->type
== TOKEN_IDENT
) {
647 c
= constraint_new();
648 pos
= parameter_pos(p
, tok
->u
.s
, -1);
649 constraint_extend(c
, 1+pos
);
650 value_set_si(c
->v
->p
[1+pos
], 1);
652 } else if (tok
->type
== TOKEN_VALUE
) {
654 c
= constraint_new();
655 tok2
= stream_next_token(s
);
656 if (tok2
&& tok2
->type
== TOKEN_IDENT
) {
657 pos
= parameter_pos(p
, tok2
->u
.s
, -1);
658 constraint_extend(c
, 1+pos
);
659 value_assign(c
->v
->p
[1+pos
], tok
->u
.v
);
664 stream_push_token(s
, tok2
);
665 value_assign(c
->v
->p
[0], tok
->u
.v
);
668 } else if (tok
->type
== TOKEN_GE
|| tok
->type
== '=') {
669 int type
= tok
->type
== TOKEN_GE
;
671 tok
= stream_next_token(s
);
672 if (!tok
|| tok
->type
!= TOKEN_VALUE
|| value_notzero_p(tok
->u
.v
)) {
673 stream_error(s
, tok
, "expecting \"0\"");
675 stream_push_token(s
, tok
);
679 c
->next
= *constraints
;
680 c
->union_next
= union_next
;
687 stream_push_token(s
, tok
);
689 stream_error(s
, tok
, "unexpected token");
698 static struct constraint
*evalue_read_domain(struct stream
*s
, struct parameter
**p
,
701 struct constraint
*constraints
= NULL
;
702 struct constraint
*union_next
= NULL
;
706 tok
= stream_next_token(s
);
709 stream_push_token(s
, tok
);
712 while (evalue_read_constraint(s
, p
, &constraints
, union_next
)) {
713 tok
= stream_next_token(s
);
715 if (tok
->type
== TOKEN_UNION
) {
717 tok
= stream_next_token(s
);
719 stream_error(s
, NULL
, "unexpected EOF");
722 stream_push_token(s
, tok
);
723 union_next
= constraints
;
727 stream_push_token(s
, tok
);
728 /* empty line separates domain from evalue */
729 if (tok
->line
> line
+1)
739 struct constraint
*constraints
;
741 struct section
*next
;
744 static char **extract_parameters(struct parameter
*p
, unsigned *nparam
)
749 *nparam
= p
? p
->pos
+1 : 0;
750 params
= ALLOCN(char *, *nparam
);
751 for (i
= 0; i
< *nparam
; ++i
) {
752 struct parameter
*next
= p
->next
;
753 params
[p
->pos
] = p
->name
;
760 static Polyhedron
*constraints2domain(struct constraint
*constraints
,
761 unsigned nparam
, unsigned MaxRays
)
766 struct constraint
*c
;
767 struct constraint
*union_next
= NULL
;
769 for (n
= 0, c
= constraints
; c
; ++n
, c
= c
->next
)
771 M
= Matrix_Alloc(n
, 1+nparam
+1);
773 struct constraint
*next
= constraints
->next
;
774 union_next
= constraints
->union_next
;
775 Vector_Copy(constraints
->v
->p
+1, M
->p
[n
]+1, nparam
);
776 if (constraints
->type
)
777 value_set_si(M
->p
[n
][0], 1);
778 value_assign(M
->p
[n
][1+nparam
], constraints
->v
->p
[0]);
779 constraints
->next
= NULL
;
780 constraints
->union_next
= NULL
;
781 constraint_free(constraints
);
784 D
= Constraints2Polyhedron(M
, MaxRays
);
788 D
= DomainConcat(D
, constraints2domain(union_next
, nparam
, MaxRays
));
792 static evalue
*evalue_read_partition(struct stream
*s
, struct parameter
*p
,
794 unsigned *nparam
, unsigned MaxRays
)
796 struct section
*part
= NULL
;
797 struct constraint
*constraints
;
801 while ((constraints
= evalue_read_domain(s
, &p
, MaxRays
))) {
802 evalue
*e
= evalue_read_term(s
, &p
);
804 stream_error(s
, NULL
, "missing evalue");
807 struct section
*sect
= ALLOC(struct section
);
808 sect
->constraints
= constraints
;
819 *ppp
= extract_parameters(p
, nparam
);
822 e
->x
.p
= new_enode(partition
, 2*m
, *nparam
);
824 for (j
= 0; j
< m
; ++j
) {
826 struct section
*next
= part
->next
;
827 constraints
= part
->constraints
;
828 D
= constraints2domain(part
->constraints
, *nparam
, MaxRays
);
829 EVALUE_SET_DOMAIN(e
->x
.p
->arr
[2*j
], D
);
830 value_clear(e
->x
.p
->arr
[2*j
+1].d
);
831 e
->x
.p
->arr
[2*j
+1] = *part
->e
;
840 static evalue
*evalue_read(struct stream
*s
, const char *var_list
, char ***ppp
,
841 unsigned *nvar
, unsigned *nparam
, unsigned MaxRays
)
845 struct parameter
*p
= NULL
;
850 while ((next
= strchr(var_list
, ','))) {
852 parameter_pos(&p
, var_list
, next
-var_list
);
855 if (strlen(var_list
) > 0)
856 parameter_pos(&p
, var_list
, -1);
857 nv
= p
? p
->pos
+1 : 0;
861 if (!(tok
= stream_next_token(s
)))
864 if (tok
->type
== TOKEN_VARS
) {
867 tok
= stream_next_token(s
);
868 if (!tok
|| tok
->type
!= TOKEN_IDENT
) {
869 stream_error(s
, tok
, "expecting identifier");
873 parameter_pos(&p
, tok
->u
.s
, -1);
875 tok
= stream_next_token(s
);
876 if (!tok
|| tok
->type
!= ',')
883 nv
= p
? p
->pos
+1 : 0;
886 if (tok
->type
== '(') {
887 stream_push_token(s
, tok
);
888 e
= evalue_read_term(s
, &p
);
889 *ppp
= extract_parameters(p
, nparam
);
890 } else if (tok
->type
== TOKEN_VALUE
) {
891 struct token
*tok2
= stream_next_token(s
);
893 stream_push_token(s
, tok2
);
894 stream_push_token(s
, tok
);
895 if (tok2
&& (tok2
->type
== TOKEN_IDENT
|| tok2
->type
== TOKEN_GE
))
896 e
= evalue_read_partition(s
, p
, ppp
, nparam
, MaxRays
);
898 e
= evalue_read_term(s
, &p
);
899 *ppp
= extract_parameters(p
, nparam
);
901 } else if (tok
->type
== TOKEN_IDENT
) {
902 stream_push_token(s
, tok
);
903 e
= evalue_read_partition(s
, p
, ppp
, nparam
, MaxRays
);
905 stream_error(s
, tok
, "unexpected token");
906 *nparam
= nv
== -1 ? 0 : nv
;
917 evalue
*evalue_read_from_file(FILE *in
, const char *var_list
, char ***ppp
,
918 unsigned *nvar
, unsigned *nparam
, unsigned MaxRays
)
921 struct stream
*s
= stream_new_file(in
);
922 e
= evalue_read(s
, var_list
, ppp
, nvar
, nparam
, MaxRays
);
927 evalue
*evalue_read_from_str(const char *str
, const char *var_list
, char ***ppp
,
928 unsigned *nvar
, unsigned *nparam
, unsigned MaxRays
)
931 struct stream
*s
= stream_new_str(str
);
932 e
= evalue_read(s
, var_list
, ppp
, nvar
, nparam
, MaxRays
);