3 /***********************************************************************
4 * This code is part of GLPK (GNU Linear Programming Kit).
6 * Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
7 * 2009, 2010 Andrew Makhorin, Department for Applied Informatics,
8 * Moscow Aviation Institute, Moscow, Russia. All rights reserved.
9 * E-mail: <mao@gnu.org>.
11 * GLPK is free software: you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
16 * GLPK is distributed in the hope that it will be useful, but WITHOUT
17 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
18 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
19 * License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with GLPK. If not, see <http://www.gnu.org/licenses/>.
23 ***********************************************************************/
27 #define dmp_get_atomv dmp_get_atom
29 /**********************************************************************/
30 /* * * PROCESSING MODEL SECTION * * */
31 /**********************************************************************/
33 /*----------------------------------------------------------------------
34 -- enter_context - enter current token into context queue.
36 -- This routine enters the current token into the context queue. */
38 void enter_context(MPL *mpl)
40 if (mpl->token == T_EOF)
42 else if (mpl->token == T_STRING)
46 xassert(0 <= mpl->c_ptr && mpl->c_ptr < CONTEXT_SIZE);
47 mpl->context[mpl->c_ptr++] = ' ';
48 if (mpl->c_ptr == CONTEXT_SIZE) mpl->c_ptr = 0;
49 for (s = image; *s != '\0'; s++)
50 { mpl->context[mpl->c_ptr++] = *s;
51 if (mpl->c_ptr == CONTEXT_SIZE) mpl->c_ptr = 0;
56 /*----------------------------------------------------------------------
57 -- print_context - print current content of context queue.
59 -- This routine prints current content of the context queue. */
61 void print_context(MPL *mpl)
63 while (mpl->c_ptr > 0)
66 memmove(mpl->context, mpl->context+1, CONTEXT_SIZE-1);
67 mpl->context[CONTEXT_SIZE-1] = (char)c;
69 xprintf("Context: %s%.*s\n", mpl->context[0] == ' ' ? "" : "...",
70 CONTEXT_SIZE, mpl->context);
74 /*----------------------------------------------------------------------
75 -- get_char - scan next character from input text file.
77 -- This routine scans a next ASCII character from the input text file.
78 -- In case of end-of-file, the character is assigned EOF. */
80 void get_char(MPL *mpl)
82 if (mpl->c == EOF) goto done;
83 if (mpl->c == '\n') mpl->line++;
89 warning(mpl, "final NL missing before end of file");
97 error(mpl, "control character 0x%02X not allowed", c);
103 /*----------------------------------------------------------------------
104 -- append_char - append character to current token.
106 -- This routine appends the current character to the current token and
107 -- then scans a next character. */
109 void append_char(MPL *mpl)
110 { xassert(0 <= mpl->imlen && mpl->imlen <= MAX_LENGTH);
111 if (mpl->imlen == MAX_LENGTH)
112 { switch (mpl->token)
115 error(mpl, "symbolic name %s... too long", mpl->image);
118 error(mpl, "symbol %s... too long", mpl->image);
121 error(mpl, "numeric literal %s... too long", mpl->image);
124 error(mpl, "string literal too long");
129 mpl->image[mpl->imlen++] = (char)mpl->c;
130 mpl->image[mpl->imlen] = '\0';
135 /*----------------------------------------------------------------------
136 -- get_token - scan next token from input text file.
138 -- This routine scans a next token from the input text file using the
139 -- standard finite automation technique. */
141 void get_token(MPL *mpl)
142 { /* save the current token */
143 mpl->b_token = mpl->token;
144 mpl->b_imlen = mpl->imlen;
145 strcpy(mpl->b_image, mpl->image);
146 mpl->b_value = mpl->value;
147 /* if the next token is already scanned, make it current */
150 mpl->token = mpl->f_token;
151 mpl->imlen = mpl->f_imlen;
152 strcpy(mpl->image, mpl->f_image);
153 mpl->value = mpl->f_value;
156 loop: /* nothing has been scanned so far */
159 mpl->image[0] = '\0';
161 /* skip any uninteresting characters */
162 while (mpl->c == ' ' || mpl->c == '\n') get_char(mpl);
163 /* recognize and construct the token */
165 { /* end-of-file reached */
168 else if (mpl->c == '#')
169 { /* comment; skip anything until end-of-line */
170 while (mpl->c != '\n' && mpl->c != EOF) get_char(mpl);
173 else if (!mpl->flag_d && (isalpha(mpl->c) || mpl->c == '_'))
174 { /* symbolic name or reserved keyword */
176 while (isalnum(mpl->c) || mpl->c == '_') append_char(mpl);
177 if (strcmp(mpl->image, "and") == 0)
179 else if (strcmp(mpl->image, "by") == 0)
181 else if (strcmp(mpl->image, "cross") == 0)
182 mpl->token = T_CROSS;
183 else if (strcmp(mpl->image, "diff") == 0)
185 else if (strcmp(mpl->image, "div") == 0)
187 else if (strcmp(mpl->image, "else") == 0)
189 else if (strcmp(mpl->image, "if") == 0)
191 else if (strcmp(mpl->image, "in") == 0)
193 #if 1 /* 21/VII-2006 */
194 else if (strcmp(mpl->image, "Infinity") == 0)
195 mpl->token = T_INFINITY;
197 else if (strcmp(mpl->image, "inter") == 0)
198 mpl->token = T_INTER;
199 else if (strcmp(mpl->image, "less") == 0)
201 else if (strcmp(mpl->image, "mod") == 0)
203 else if (strcmp(mpl->image, "not") == 0)
205 else if (strcmp(mpl->image, "or") == 0)
207 else if (strcmp(mpl->image, "s") == 0 && mpl->c == '.')
208 { mpl->token = T_SPTP;
211 sptp: { enter_context(mpl);
212 error(mpl, "keyword s.t. incomplete");
215 if (mpl->c != '.') goto sptp;
218 else if (strcmp(mpl->image, "symdiff") == 0)
219 mpl->token = T_SYMDIFF;
220 else if (strcmp(mpl->image, "then") == 0)
222 else if (strcmp(mpl->image, "union") == 0)
223 mpl->token = T_UNION;
224 else if (strcmp(mpl->image, "within") == 0)
225 mpl->token = T_WITHIN;
227 else if (!mpl->flag_d && isdigit(mpl->c))
228 { /* numeric literal */
229 mpl->token = T_NUMBER;
230 /* scan integer part */
231 while (isdigit(mpl->c)) append_char(mpl);
232 /* scan optional fractional part */
236 { /* hmm, it is not the fractional part, it is dots that
237 follow the integer part */
239 mpl->image[mpl->imlen] = '\0';
243 frac: while (isdigit(mpl->c)) append_char(mpl);
245 /* scan optional decimal exponent */
246 if (mpl->c == 'e' || mpl->c == 'E')
248 if (mpl->c == '+' || mpl->c == '-') append_char(mpl);
249 if (!isdigit(mpl->c))
250 { enter_context(mpl);
251 error(mpl, "numeric literal %s incomplete", mpl->image);
253 while (isdigit(mpl->c)) append_char(mpl);
255 /* there must be no letter following the numeric literal */
256 if (isalpha(mpl->c) || mpl->c == '_')
257 { enter_context(mpl);
258 error(mpl, "symbol %s%c... should be enclosed in quotes",
261 conv: /* convert numeric literal to floating-point */
262 if (str2num(mpl->image, &mpl->value))
263 err: { enter_context(mpl);
264 error(mpl, "cannot convert numeric literal %s to floating-p"
265 "oint number", mpl->image);
268 else if (mpl->c == '\'' || mpl->c == '"')
269 { /* character string */
271 mpl->token = T_STRING;
274 { if (mpl->c == '\n' || mpl->c == EOF)
275 { enter_context(mpl);
276 error(mpl, "unexpected end of line; string literal incom"
281 if (mpl->c != quote) break;
286 else if (!mpl->flag_d && mpl->c == '+')
287 mpl->token = T_PLUS, append_char(mpl);
288 else if (!mpl->flag_d && mpl->c == '-')
289 mpl->token = T_MINUS, append_char(mpl);
290 else if (mpl->c == '*')
291 { mpl->token = T_ASTERISK, append_char(mpl);
293 mpl->token = T_POWER, append_char(mpl);
295 else if (mpl->c == '/')
296 { mpl->token = T_SLASH, append_char(mpl);
298 { /* comment sequence */
302 { /* do not call enter_context at this point */
303 error(mpl, "unexpected end of file; comment sequence "
306 else if (mpl->c == '*')
308 if (mpl->c == '/') break;
317 else if (mpl->c == '^')
318 mpl->token = T_POWER, append_char(mpl);
319 else if (mpl->c == '<')
320 { mpl->token = T_LT, append_char(mpl);
322 mpl->token = T_LE, append_char(mpl);
323 else if (mpl->c == '>')
324 mpl->token = T_NE, append_char(mpl);
325 #if 1 /* 11/II-2008 */
326 else if (mpl->c == '-')
327 mpl->token = T_INPUT, append_char(mpl);
330 else if (mpl->c == '=')
331 { mpl->token = T_EQ, append_char(mpl);
332 if (mpl->c == '=') append_char(mpl);
334 else if (mpl->c == '>')
335 { mpl->token = T_GT, append_char(mpl);
337 mpl->token = T_GE, append_char(mpl);
338 #if 1 /* 14/VII-2006 */
339 else if (mpl->c == '>')
340 mpl->token = T_APPEND, append_char(mpl);
343 else if (mpl->c == '!')
344 { mpl->token = T_NOT, append_char(mpl);
346 mpl->token = T_NE, append_char(mpl);
348 else if (mpl->c == '&')
349 { mpl->token = T_CONCAT, append_char(mpl);
351 mpl->token = T_AND, append_char(mpl);
353 else if (mpl->c == '|')
354 { mpl->token = T_BAR, append_char(mpl);
356 mpl->token = T_OR, append_char(mpl);
358 else if (!mpl->flag_d && mpl->c == '.')
359 { mpl->token = T_POINT, append_char(mpl);
361 { /* dots; the first dot was read on the previous call to the
362 scanner, so the current character is the second dot */
365 strcpy(mpl->image, "..");
368 else if (mpl->c == '.')
369 mpl->token = T_DOTS, append_char(mpl);
370 else if (isdigit(mpl->c))
371 { /* numeric literal that begins with the decimal point */
372 mpl->token = T_NUMBER, append_char(mpl);
376 else if (mpl->c == ',')
377 mpl->token = T_COMMA, append_char(mpl);
378 else if (mpl->c == ':')
379 { mpl->token = T_COLON, append_char(mpl);
381 mpl->token = T_ASSIGN, append_char(mpl);
383 else if (mpl->c == ';')
384 mpl->token = T_SEMICOLON, append_char(mpl);
385 else if (mpl->c == '(')
386 mpl->token = T_LEFT, append_char(mpl);
387 else if (mpl->c == ')')
388 mpl->token = T_RIGHT, append_char(mpl);
389 else if (mpl->c == '[')
390 mpl->token = T_LBRACKET, append_char(mpl);
391 else if (mpl->c == ']')
392 mpl->token = T_RBRACKET, append_char(mpl);
393 else if (mpl->c == '{')
394 mpl->token = T_LBRACE, append_char(mpl);
395 else if (mpl->c == '}')
396 mpl->token = T_RBRACE, append_char(mpl);
397 #if 1 /* 11/II-2008 */
398 else if (mpl->c == '~')
399 mpl->token = T_TILDE, append_char(mpl);
401 else if (isalnum(mpl->c) || strchr("+-._", mpl->c) != NULL)
403 xassert(mpl->flag_d);
404 mpl->token = T_SYMBOL;
405 while (isalnum(mpl->c) || strchr("+-._", mpl->c) != NULL)
407 switch (str2num(mpl->image, &mpl->value))
409 mpl->token = T_NUMBER;
420 { enter_context(mpl);
421 error(mpl, "character %c not allowed", mpl->c);
423 /* enter the current token into the context queue */
425 /* reset the flag, which may be set by indexing_expression() and
426 is used by expression_list() */
431 /*----------------------------------------------------------------------
432 -- unget_token - return current token back to input stream.
434 -- This routine returns the current token back to the input stream, so
435 -- the previously scanned token becomes the current one. */
437 void unget_token(MPL *mpl)
438 { /* save the current token, which becomes the next one */
439 xassert(!mpl->f_scan);
441 mpl->f_token = mpl->token;
442 mpl->f_imlen = mpl->imlen;
443 strcpy(mpl->f_image, mpl->image);
444 mpl->f_value = mpl->value;
445 /* restore the previous token, which becomes the current one */
446 mpl->token = mpl->b_token;
447 mpl->imlen = mpl->b_imlen;
448 strcpy(mpl->image, mpl->b_image);
449 mpl->value = mpl->b_value;
453 /*----------------------------------------------------------------------
454 -- is_keyword - check if current token is given non-reserved keyword.
456 -- If the current token is given (non-reserved) keyword, this routine
457 -- returns non-zero. Otherwise zero is returned. */
459 int is_keyword(MPL *mpl, char *keyword)
461 mpl->token == T_NAME && strcmp(mpl->image, keyword) == 0;
464 /*----------------------------------------------------------------------
465 -- is_reserved - check if current token is reserved keyword.
467 -- If the current token is a reserved keyword, this routine returns
468 -- non-zero. Otherwise zero is returned. */
470 int is_reserved(MPL *mpl)
472 mpl->token == T_AND && mpl->image[0] == 'a' ||
473 mpl->token == T_BY ||
474 mpl->token == T_CROSS ||
475 mpl->token == T_DIFF ||
476 mpl->token == T_DIV ||
477 mpl->token == T_ELSE ||
478 mpl->token == T_IF ||
479 mpl->token == T_IN ||
480 mpl->token == T_INTER ||
481 mpl->token == T_LESS ||
482 mpl->token == T_MOD ||
483 mpl->token == T_NOT && mpl->image[0] == 'n' ||
484 mpl->token == T_OR && mpl->image[0] == 'o' ||
485 mpl->token == T_SYMDIFF ||
486 mpl->token == T_THEN ||
487 mpl->token == T_UNION ||
488 mpl->token == T_WITHIN;
491 /*----------------------------------------------------------------------
492 -- make_code - generate pseudo-code (basic routine).
494 -- This routine generates specified pseudo-code. It is assumed that all
495 -- other translator routines use this basic routine. */
497 CODE *make_code(MPL *mpl, int op, OPERANDS *arg, int type, int dim)
502 /* generate pseudo-code */
505 code->vflag = 0; /* is inherited from operand(s) */
506 /* copy operands and also make them referring to the pseudo-code
507 being generated, because the latter becomes the parent for all
509 memset(&code->arg, '?', sizeof(OPERANDS));
512 code->arg.num = arg->num;
515 code->arg.str = arg->str;
518 code->arg.index.slot = arg->index.slot;
519 code->arg.index.next = arg->index.next;
523 for (e = arg->par.list; e != NULL; e = e->next)
524 { xassert(e->x != NULL);
525 xassert(e->x->up == NULL);
527 code->vflag |= e->x->vflag;
529 code->arg.par.par = arg->par.par;
530 code->arg.par.list = arg->par.list;
533 for (e = arg->set.list; e != NULL; e = e->next)
534 { xassert(e->x != NULL);
535 xassert(e->x->up == NULL);
537 code->vflag |= e->x->vflag;
539 code->arg.set.set = arg->set.set;
540 code->arg.set.list = arg->set.list;
543 for (e = arg->var.list; e != NULL; e = e->next)
544 { xassert(e->x != NULL);
545 xassert(e->x->up == NULL);
547 code->vflag |= e->x->vflag;
549 code->arg.var.var = arg->var.var;
550 code->arg.var.list = arg->var.list;
551 #if 1 /* 15/V-2010 */
552 code->arg.var.suff = arg->var.suff;
555 #if 1 /* 15/V-2010 */
557 for (e = arg->con.list; e != NULL; e = e->next)
558 { xassert(e->x != NULL);
559 xassert(e->x->up == NULL);
561 code->vflag |= e->x->vflag;
563 code->arg.con.con = arg->con.con;
564 code->arg.con.list = arg->con.list;
565 code->arg.con.suff = arg->con.suff;
570 for (e = arg->list; e != NULL; e = e->next)
571 { xassert(e->x != NULL);
572 xassert(e->x->up == NULL);
574 code->vflag |= e->x->vflag;
576 code->arg.list = arg->list;
579 xassert(arg->slice != NULL);
580 code->arg.slice = arg->slice;
610 /* unary operation */
611 xassert(arg->arg.x != NULL);
612 xassert(arg->arg.x->up == NULL);
613 arg->arg.x->up = code;
614 code->vflag |= arg->arg.x->vflag;
615 code->arg.arg.x = arg->arg.x;
629 if (op == O_UNIFORM) code->vflag = 1;
631 if (op == O_NORMAL) code->vflag = 1;
653 /* binary operation */
654 xassert(arg->arg.x != NULL);
655 xassert(arg->arg.x->up == NULL);
656 arg->arg.x->up = code;
657 code->vflag |= arg->arg.x->vflag;
658 xassert(arg->arg.y != NULL);
659 xassert(arg->arg.y->up == NULL);
660 arg->arg.y->up = code;
661 code->vflag |= arg->arg.y->vflag;
662 code->arg.arg.x = arg->arg.x;
663 code->arg.arg.y = arg->arg.y;
668 /* ternary operation */
669 xassert(arg->arg.x != NULL);
670 xassert(arg->arg.x->up == NULL);
671 arg->arg.x->up = code;
672 code->vflag |= arg->arg.x->vflag;
673 xassert(arg->arg.y != NULL);
674 xassert(arg->arg.y->up == NULL);
675 arg->arg.y->up = code;
676 code->vflag |= arg->arg.y->vflag;
677 if (arg->arg.z != NULL)
678 { xassert(arg->arg.z->up == NULL);
679 arg->arg.z->up = code;
680 code->vflag |= arg->arg.z->vflag;
682 code->arg.arg.x = arg->arg.x;
683 code->arg.arg.y = arg->arg.y;
684 code->arg.arg.z = arg->arg.z;
688 /* n-ary operation */
689 for (e = arg->list; e != NULL; e = e->next)
690 { xassert(e->x != NULL);
691 xassert(e->x->up == NULL);
693 code->vflag |= e->x->vflag;
695 code->arg.list = arg->list;
705 /* iterated operation */
706 domain = arg->loop.domain;
707 xassert(domain != NULL);
708 if (domain->code != NULL)
709 { xassert(domain->code->up == NULL);
710 domain->code->up = code;
711 code->vflag |= domain->code->vflag;
713 for (block = domain->list; block != NULL; block =
715 { xassert(block->code != NULL);
716 xassert(block->code->up == NULL);
717 block->code->up = code;
718 code->vflag |= block->code->vflag;
720 if (arg->loop.x != NULL)
721 { xassert(arg->loop.x->up == NULL);
722 arg->loop.x->up = code;
723 code->vflag |= arg->loop.x->vflag;
725 code->arg.loop.domain = arg->loop.domain;
726 code->arg.loop.x = arg->loop.x;
731 /* set other attributes of the pseudo-code */
736 memset(&code->value, '?', sizeof(VALUE));
740 /*----------------------------------------------------------------------
741 -- make_unary - generate pseudo-code for unary operation.
743 -- This routine generates pseudo-code for unary operation. */
745 CODE *make_unary(MPL *mpl, int op, CODE *x, int type, int dim)
750 code = make_code(mpl, op, &arg, type, dim);
754 /*----------------------------------------------------------------------
755 -- make_binary - generate pseudo-code for binary operation.
757 -- This routine generates pseudo-code for binary operation. */
759 CODE *make_binary(MPL *mpl, int op, CODE *x, CODE *y, int type,
767 code = make_code(mpl, op, &arg, type, dim);
771 /*----------------------------------------------------------------------
772 -- make_ternary - generate pseudo-code for ternary operation.
774 -- This routine generates pseudo-code for ternary operation. */
776 CODE *make_ternary(MPL *mpl, int op, CODE *x, CODE *y, CODE *z,
782 /* third operand can be NULL */
786 code = make_code(mpl, op, &arg, type, dim);
790 /*----------------------------------------------------------------------
791 -- numeric_literal - parse reference to numeric literal.
793 -- This routine parses primary expression using the syntax:
795 -- <primary expression> ::= <numeric literal> */
797 CODE *numeric_literal(MPL *mpl)
800 xassert(mpl->token == T_NUMBER);
801 arg.num = mpl->value;
802 code = make_code(mpl, O_NUMBER, &arg, A_NUMERIC, 0);
803 get_token(mpl /* <numeric literal> */);
807 /*----------------------------------------------------------------------
808 -- string_literal - parse reference to string literal.
810 -- This routine parses primary expression using the syntax:
812 -- <primary expression> ::= <string literal> */
814 CODE *string_literal(MPL *mpl)
817 xassert(mpl->token == T_STRING);
818 arg.str = dmp_get_atomv(mpl->pool, strlen(mpl->image)+1);
819 strcpy(arg.str, mpl->image);
820 code = make_code(mpl, O_STRING, &arg, A_SYMBOLIC, 0);
821 get_token(mpl /* <string literal> */);
825 /*----------------------------------------------------------------------
826 -- create_arg_list - create empty operands list.
828 -- This routine creates operands list, which is initially empty. */
830 ARG_LIST *create_arg_list(MPL *mpl)
837 /*----------------------------------------------------------------------
838 -- expand_arg_list - append operand to operands list.
840 -- This routine appends new operand to specified operands list. */
842 ARG_LIST *expand_arg_list(MPL *mpl, ARG_LIST *list, CODE *x)
843 { ARG_LIST *tail, *temp;
845 /* create new operands list entry */
846 tail = alloc(ARG_LIST);
849 /* and append it to the operands list */
853 { for (temp = list; temp->next != NULL; temp = temp->next);
859 /*----------------------------------------------------------------------
860 -- arg_list_len - determine length of operands list.
862 -- This routine returns the number of operands in operands list. */
864 int arg_list_len(MPL *mpl, ARG_LIST *list)
869 for (temp = list; temp != NULL; temp = temp->next) len++;
873 /*----------------------------------------------------------------------
874 -- subscript_list - parse subscript list.
876 -- This routine parses subscript list using the syntax:
878 -- <subscript list> ::= <subscript>
879 -- <subscript list> ::= <subscript list> , <subscript>
880 -- <subscript> ::= <expression 5> */
882 ARG_LIST *subscript_list(MPL *mpl)
885 list = create_arg_list(mpl);
887 { /* parse subscript expression */
888 x = expression_5(mpl);
889 /* convert it to symbolic type, if necessary */
890 if (x->type == A_NUMERIC)
891 x = make_unary(mpl, O_CVTSYM, x, A_SYMBOLIC, 0);
892 /* check that now the expression is of symbolic type */
893 if (x->type != A_SYMBOLIC)
894 error(mpl, "subscript expression has invalid type");
895 xassert(x->dim == 0);
896 /* and append it to the subscript list */
897 list = expand_arg_list(mpl, list, x);
898 /* check a token that follows the subscript expression */
899 if (mpl->token == T_COMMA)
900 get_token(mpl /* , */);
901 else if (mpl->token == T_RBRACKET)
904 error(mpl, "syntax error in subscript list");
909 #if 1 /* 15/V-2010 */
910 /*----------------------------------------------------------------------
911 -- object_reference - parse reference to named object.
913 -- This routine parses primary expression using the syntax:
915 -- <primary expression> ::= <dummy index>
916 -- <primary expression> ::= <set name>
917 -- <primary expression> ::= <set name> [ <subscript list> ]
918 -- <primary expression> ::= <parameter name>
919 -- <primary expression> ::= <parameter name> [ <subscript list> ]
920 -- <primary expression> ::= <variable name> <suffix>
921 -- <primary expression> ::= <variable name> [ <subscript list> ]
923 -- <primary expression> ::= <constraint name> <suffix>
924 -- <primary expression> ::= <constraint name> [ <subscript list> ]
926 -- <dummy index> ::= <symbolic name>
927 -- <set name> ::= <symbolic name>
928 -- <parameter name> ::= <symbolic name>
929 -- <variable name> ::= <symbolic name>
930 -- <constraint name> ::= <symbolic name>
931 -- <suffix> ::= <empty> | .lb | .ub | .status | .val | .dual */
933 CODE *object_reference(MPL *mpl)
945 /* find the object in the symbolic name table */
946 xassert(mpl->token == T_NAME);
947 node = avl_find_node(mpl->tree, mpl->image);
949 error(mpl, "%s not defined", mpl->image);
950 /* check the object type and obtain its dimension */
951 switch (avl_get_node_type(node))
954 slot = (DOMAIN_SLOT *)avl_get_node_link(node);
960 set = (SET *)avl_get_node_link(node);
963 /* if a set object is referenced in its own declaration and
964 the dimen attribute is not specified yet, use dimen 1 by
966 if (set->dimen == 0) set->dimen = 1;
969 /* model parameter */
970 par = (PARAMETER *)avl_get_node_link(node);
976 var = (VARIABLE *)avl_get_node_link(node);
981 /* model constraint or objective */
982 con = (CONSTRAINT *)avl_get_node_link(node);
987 xassert(node != node);
989 get_token(mpl /* <symbolic name> */);
990 /* parse optional subscript list */
991 if (mpl->token == T_LBRACKET)
992 { /* subscript list is specified */
994 error(mpl, "%s cannot be subscripted", name);
995 get_token(mpl /* [ */);
996 list = subscript_list(mpl);
997 if (dim != arg_list_len(mpl, list))
998 error(mpl, "%s must have %d subscript%s rather than %d",
999 name, dim, dim == 1 ? "" : "s", arg_list_len(mpl, list));
1000 xassert(mpl->token == T_RBRACKET);
1001 get_token(mpl /* ] */);
1004 { /* subscript list is not specified */
1006 error(mpl, "%s must be subscripted", name);
1007 list = create_arg_list(mpl);
1009 /* parse optional suffix */
1010 if (!mpl->flag_s && avl_get_node_type(node) == A_VARIABLE)
1014 if (mpl->token == T_POINT)
1015 { get_token(mpl /* . */);
1016 if (mpl->token != T_NAME)
1017 error(mpl, "invalid use of period");
1018 if (!(avl_get_node_type(node) == A_VARIABLE ||
1019 avl_get_node_type(node) == A_CONSTRAINT))
1020 error(mpl, "%s cannot have a suffix", name);
1021 if (strcmp(mpl->image, "lb") == 0)
1023 else if (strcmp(mpl->image, "ub") == 0)
1025 else if (strcmp(mpl->image, "status") == 0)
1027 else if (strcmp(mpl->image, "val") == 0)
1029 else if (strcmp(mpl->image, "dual") == 0)
1032 error(mpl, "suffix .%s invalid", mpl->image);
1033 get_token(mpl /* suffix */);
1035 /* generate pseudo-code to take value of the object */
1036 switch (avl_get_node_type(node))
1038 arg.index.slot = slot;
1039 arg.index.next = slot->list;
1040 code = make_code(mpl, O_INDEX, &arg, A_SYMBOLIC, 0);
1045 arg.set.list = list;
1046 code = make_code(mpl, O_MEMSET, &arg, A_ELEMSET,
1051 arg.par.list = list;
1052 if (par->type == A_SYMBOLIC)
1053 code = make_code(mpl, O_MEMSYM, &arg, A_SYMBOLIC, 0);
1055 code = make_code(mpl, O_MEMNUM, &arg, A_NUMERIC, 0);
1058 if (!mpl->flag_s && (suff == DOT_STATUS || suff == DOT_VAL
1059 || suff == DOT_DUAL))
1060 error(mpl, "invalid reference to status, primal value, o"
1061 "r dual value of variable %s above solve statement",
1064 arg.var.list = list;
1065 arg.var.suff = suff;
1066 code = make_code(mpl, O_MEMVAR, &arg, suff == DOT_NONE ?
1067 A_FORMULA : A_NUMERIC, 0);
1070 if (!mpl->flag_s && (suff == DOT_STATUS || suff == DOT_VAL
1071 || suff == DOT_DUAL))
1072 error(mpl, "invalid reference to status, primal value, o"
1073 "r dual value of %s %s above solve statement",
1074 con->type == A_CONSTRAINT ? "constraint" : "objective"
1077 arg.con.list = list;
1078 arg.con.suff = suff;
1079 code = make_code(mpl, O_MEMCON, &arg, A_NUMERIC, 0);
1082 xassert(node != node);
1088 /*----------------------------------------------------------------------
1089 -- numeric_argument - parse argument passed to built-in function.
1091 -- This routine parses an argument passed to numeric built-in function
1092 -- using the syntax:
1094 -- <arg> ::= <expression 5> */
1096 CODE *numeric_argument(MPL *mpl, char *func)
1098 x = expression_5(mpl);
1099 /* convert the argument to numeric type, if necessary */
1100 if (x->type == A_SYMBOLIC)
1101 x = make_unary(mpl, O_CVTNUM, x, A_NUMERIC, 0);
1102 /* check that now the argument is of numeric type */
1103 if (x->type != A_NUMERIC)
1104 error(mpl, "argument for %s has invalid type", func);
1105 xassert(x->dim == 0);
1109 #if 1 /* 15/VII-2006 */
1110 CODE *symbolic_argument(MPL *mpl, char *func)
1112 x = expression_5(mpl);
1113 /* convert the argument to symbolic type, if necessary */
1114 if (x->type == A_NUMERIC)
1115 x = make_unary(mpl, O_CVTSYM, x, A_SYMBOLIC, 0);
1116 /* check that now the argument is of symbolic type */
1117 if (x->type != A_SYMBOLIC)
1118 error(mpl, "argument for %s has invalid type", func);
1119 xassert(x->dim == 0);
1124 #if 1 /* 15/VII-2006 */
1125 CODE *elemset_argument(MPL *mpl, char *func)
1127 x = expression_9(mpl);
1128 if (x->type != A_ELEMSET)
1129 error(mpl, "argument for %s has invalid type", func);
1130 xassert(x->dim > 0);
1135 /*----------------------------------------------------------------------
1136 -- function_reference - parse reference to built-in function.
1138 -- This routine parses primary expression using the syntax:
1140 -- <primary expression> ::= abs ( <arg> )
1141 -- <primary expression> ::= ceil ( <arg> )
1142 -- <primary expression> ::= floor ( <arg> )
1143 -- <primary expression> ::= exp ( <arg> )
1144 -- <primary expression> ::= log ( <arg> )
1145 -- <primary expression> ::= log10 ( <arg> )
1146 -- <primary expression> ::= max ( <arg list> )
1147 -- <primary expression> ::= min ( <arg list> )
1148 -- <primary expression> ::= sqrt ( <arg> )
1149 -- <primary expression> ::= sin ( <arg> )
1150 -- <primary expression> ::= cos ( <arg> )
1151 -- <primary expression> ::= atan ( <arg> )
1152 -- <primary expression> ::= atan2 ( <arg> , <arg> )
1153 -- <primary expression> ::= round ( <arg> )
1154 -- <primary expression> ::= round ( <arg> , <arg> )
1155 -- <primary expression> ::= trunc ( <arg> )
1156 -- <primary expression> ::= trunc ( <arg> , <arg> )
1157 -- <primary expression> ::= Irand224 ( )
1158 -- <primary expression> ::= Uniform01 ( )
1159 -- <primary expression> ::= Uniform ( <arg> , <arg> )
1160 -- <primary expression> ::= Normal01 ( )
1161 -- <primary expression> ::= Normal ( <arg> , <arg> )
1162 -- <primary expression> ::= card ( <arg> )
1163 -- <primary expression> ::= length ( <arg> )
1164 -- <primary expression> ::= substr ( <arg> , <arg> )
1165 -- <primary expression> ::= substr ( <arg> , <arg> , <arg> )
1166 -- <primary expression> ::= str2time ( <arg> , <arg> )
1167 -- <primary expression> ::= time2str ( <arg> , <arg> )
1168 -- <primary expression> ::= gmtime ( )
1169 -- <arg list> ::= <arg>
1170 -- <arg list> ::= <arg list> , <arg> */
1172 CODE *function_reference(MPL *mpl)
1177 /* determine operation code */
1178 xassert(mpl->token == T_NAME);
1179 if (strcmp(mpl->image, "abs") == 0)
1181 else if (strcmp(mpl->image, "ceil") == 0)
1183 else if (strcmp(mpl->image, "floor") == 0)
1185 else if (strcmp(mpl->image, "exp") == 0)
1187 else if (strcmp(mpl->image, "log") == 0)
1189 else if (strcmp(mpl->image, "log10") == 0)
1191 else if (strcmp(mpl->image, "sqrt") == 0)
1193 else if (strcmp(mpl->image, "sin") == 0)
1195 else if (strcmp(mpl->image, "cos") == 0)
1197 else if (strcmp(mpl->image, "atan") == 0)
1199 else if (strcmp(mpl->image, "min") == 0)
1201 else if (strcmp(mpl->image, "max") == 0)
1203 else if (strcmp(mpl->image, "round") == 0)
1205 else if (strcmp(mpl->image, "trunc") == 0)
1207 else if (strcmp(mpl->image, "Irand224") == 0)
1209 else if (strcmp(mpl->image, "Uniform01") == 0)
1211 else if (strcmp(mpl->image, "Uniform") == 0)
1213 else if (strcmp(mpl->image, "Normal01") == 0)
1215 else if (strcmp(mpl->image, "Normal") == 0)
1217 else if (strcmp(mpl->image, "card") == 0)
1219 else if (strcmp(mpl->image, "length") == 0)
1221 else if (strcmp(mpl->image, "substr") == 0)
1223 else if (strcmp(mpl->image, "str2time") == 0)
1225 else if (strcmp(mpl->image, "time2str") == 0)
1227 else if (strcmp(mpl->image, "gmtime") == 0)
1230 error(mpl, "function %s unknown", mpl->image);
1231 /* save symbolic name of the function */
1232 strcpy(func, mpl->image);
1233 xassert(strlen(func) < sizeof(func));
1234 get_token(mpl /* <symbolic name> */);
1235 /* check the left parenthesis that follows the function name */
1236 xassert(mpl->token == T_LEFT);
1237 get_token(mpl /* ( */);
1238 /* parse argument list */
1239 if (op == O_MIN || op == O_MAX)
1240 { /* min and max allow arbitrary number of arguments */
1241 arg.list = create_arg_list(mpl);
1242 /* parse argument list */
1244 { /* parse argument and append it to the operands list */
1245 arg.list = expand_arg_list(mpl, arg.list,
1246 numeric_argument(mpl, func));
1247 /* check a token that follows the argument */
1248 if (mpl->token == T_COMMA)
1249 get_token(mpl /* , */);
1250 else if (mpl->token == T_RIGHT)
1253 error(mpl, "syntax error in argument list for %s", func);
1256 else if (op == O_IRAND224 || op == O_UNIFORM01 || op ==
1257 O_NORMAL01 || op == O_GMTIME)
1258 { /* Irand224, Uniform01, Normal01, gmtime need no arguments */
1259 if (mpl->token != T_RIGHT)
1260 error(mpl, "%s needs no arguments", func);
1262 else if (op == O_UNIFORM || op == O_NORMAL)
1263 { /* Uniform and Normal need two arguments */
1264 /* parse the first argument */
1265 arg.arg.x = numeric_argument(mpl, func);
1266 /* check a token that follows the first argument */
1267 if (mpl->token == T_COMMA)
1269 else if (mpl->token == T_RIGHT)
1270 error(mpl, "%s needs two arguments", func);
1272 error(mpl, "syntax error in argument for %s", func);
1273 get_token(mpl /* , */);
1274 /* parse the second argument */
1275 arg.arg.y = numeric_argument(mpl, func);
1276 /* check a token that follows the second argument */
1277 if (mpl->token == T_COMMA)
1278 error(mpl, "%s needs two argument", func);
1279 else if (mpl->token == T_RIGHT)
1282 error(mpl, "syntax error in argument for %s", func);
1284 else if (op == O_ATAN || op == O_ROUND || op == O_TRUNC)
1285 { /* atan, round, and trunc need one or two arguments */
1286 /* parse the first argument */
1287 arg.arg.x = numeric_argument(mpl, func);
1288 /* parse the second argument, if specified */
1289 if (mpl->token == T_COMMA)
1291 { case O_ATAN: op = O_ATAN2; break;
1292 case O_ROUND: op = O_ROUND2; break;
1293 case O_TRUNC: op = O_TRUNC2; break;
1294 default: xassert(op != op);
1296 get_token(mpl /* , */);
1297 arg.arg.y = numeric_argument(mpl, func);
1299 /* check a token that follows the last argument */
1300 if (mpl->token == T_COMMA)
1301 error(mpl, "%s needs one or two arguments", func);
1302 else if (mpl->token == T_RIGHT)
1305 error(mpl, "syntax error in argument for %s", func);
1307 else if (op == O_SUBSTR)
1308 { /* substr needs two or three arguments */
1309 /* parse the first argument */
1310 arg.arg.x = symbolic_argument(mpl, func);
1311 /* check a token that follows the first argument */
1312 if (mpl->token == T_COMMA)
1314 else if (mpl->token == T_RIGHT)
1315 error(mpl, "%s needs two or three arguments", func);
1317 error(mpl, "syntax error in argument for %s", func);
1318 get_token(mpl /* , */);
1319 /* parse the second argument */
1320 arg.arg.y = numeric_argument(mpl, func);
1321 /* parse the third argument, if specified */
1322 if (mpl->token == T_COMMA)
1324 get_token(mpl /* , */);
1325 arg.arg.z = numeric_argument(mpl, func);
1327 /* check a token that follows the last argument */
1328 if (mpl->token == T_COMMA)
1329 error(mpl, "%s needs two or three arguments", func);
1330 else if (mpl->token == T_RIGHT)
1333 error(mpl, "syntax error in argument for %s", func);
1335 else if (op == O_STR2TIME)
1336 { /* str2time needs two arguments, both symbolic */
1337 /* parse the first argument */
1338 arg.arg.x = symbolic_argument(mpl, func);
1339 /* check a token that follows the first argument */
1340 if (mpl->token == T_COMMA)
1342 else if (mpl->token == T_RIGHT)
1343 error(mpl, "%s needs two arguments", func);
1345 error(mpl, "syntax error in argument for %s", func);
1346 get_token(mpl /* , */);
1347 /* parse the second argument */
1348 arg.arg.y = symbolic_argument(mpl, func);
1349 /* check a token that follows the second argument */
1350 if (mpl->token == T_COMMA)
1351 error(mpl, "%s needs two argument", func);
1352 else if (mpl->token == T_RIGHT)
1355 error(mpl, "syntax error in argument for %s", func);
1357 else if (op == O_TIME2STR)
1358 { /* time2str needs two arguments, numeric and symbolic */
1359 /* parse the first argument */
1360 arg.arg.x = numeric_argument(mpl, func);
1361 /* check a token that follows the first argument */
1362 if (mpl->token == T_COMMA)
1364 else if (mpl->token == T_RIGHT)
1365 error(mpl, "%s needs two arguments", func);
1367 error(mpl, "syntax error in argument for %s", func);
1368 get_token(mpl /* , */);
1369 /* parse the second argument */
1370 arg.arg.y = symbolic_argument(mpl, func);
1371 /* check a token that follows the second argument */
1372 if (mpl->token == T_COMMA)
1373 error(mpl, "%s needs two argument", func);
1374 else if (mpl->token == T_RIGHT)
1377 error(mpl, "syntax error in argument for %s", func);
1380 { /* other functions need one argument */
1382 arg.arg.x = elemset_argument(mpl, func);
1383 else if (op == O_LENGTH)
1384 arg.arg.x = symbolic_argument(mpl, func);
1386 arg.arg.x = numeric_argument(mpl, func);
1387 /* check a token that follows the argument */
1388 if (mpl->token == T_COMMA)
1389 error(mpl, "%s needs one argument", func);
1390 else if (mpl->token == T_RIGHT)
1393 error(mpl, "syntax error in argument for %s", func);
1395 /* make pseudo-code to call the built-in function */
1396 if (op == O_SUBSTR || op == O_SUBSTR3 || op == O_TIME2STR)
1397 code = make_code(mpl, op, &arg, A_SYMBOLIC, 0);
1399 code = make_code(mpl, op, &arg, A_NUMERIC, 0);
1400 /* the reference ends with the right parenthesis */
1401 xassert(mpl->token == T_RIGHT);
1402 get_token(mpl /* ) */);
1406 /*----------------------------------------------------------------------
1407 -- create_domain - create empty domain.
1409 -- This routine creates empty domain, which is initially empty, i.e.
1410 -- has no domain blocks. */
1412 DOMAIN *create_domain(MPL *mpl)
1414 domain = alloc(DOMAIN);
1415 domain->list = NULL;
1416 domain->code = NULL;
1420 /*----------------------------------------------------------------------
1421 -- create_block - create empty domain block.
1423 -- This routine creates empty domain block, which is initially empty,
1424 -- i.e. has no domain slots. */
1426 DOMAIN_BLOCK *create_block(MPL *mpl)
1427 { DOMAIN_BLOCK *block;
1428 block = alloc(DOMAIN_BLOCK);
1431 block->backup = NULL;
1436 /*----------------------------------------------------------------------
1437 -- append_block - append domain block to specified domain.
1439 -- This routine adds given domain block to the end of the block list of
1440 -- specified domain. */
1442 void append_block(MPL *mpl, DOMAIN *domain, DOMAIN_BLOCK *block)
1443 { DOMAIN_BLOCK *temp;
1444 xassert(mpl == mpl);
1445 xassert(domain != NULL);
1446 xassert(block != NULL);
1447 xassert(block->next == NULL);
1448 if (domain->list == NULL)
1449 domain->list = block;
1451 { for (temp = domain->list; temp->next != NULL; temp =
1458 /*----------------------------------------------------------------------
1459 -- append_slot - create and append new slot to domain block.
1461 -- This routine creates new domain slot and adds it to the end of slot
1462 -- list of specified domain block.
1464 -- The parameter name is symbolic name of the dummy index associated
1465 -- with the slot (the character string must be allocated). NULL means
1466 -- the dummy index is not explicitly specified.
1468 -- The parameter code is pseudo-code for computing symbolic value, at
1469 -- which the dummy index is bounded. NULL means the dummy index is free
1470 -- in the domain scope. */
1472 DOMAIN_SLOT *append_slot(MPL *mpl, DOMAIN_BLOCK *block, char *name,
1474 { DOMAIN_SLOT *slot, *temp;
1475 xassert(block != NULL);
1476 slot = alloc(DOMAIN_SLOT);
1482 if (block->list == NULL)
1485 { for (temp = block->list; temp->next != NULL; temp =
1492 /*----------------------------------------------------------------------
1493 -- expression_list - parse expression list.
1495 -- This routine parses a list of one or more expressions enclosed into
1496 -- the parentheses using the syntax:
1498 -- <primary expression> ::= ( <expression list> )
1499 -- <expression list> ::= <expression 13>
1500 -- <expression list> ::= <expression 13> , <expression list>
1502 -- Note that this construction may have three different meanings:
1504 -- 1. If <expression list> consists of only one expression, <primary
1505 -- expression> is a parenthesized expression, which may be of any
1506 -- valid type (not necessarily 1-tuple).
1508 -- 2. If <expression list> consists of several expressions separated by
1509 -- commae, where no expression is undeclared symbolic name, <primary
1510 -- expression> is a n-tuple.
1512 -- 3. If <expression list> consists of several expressions separated by
1513 -- commae, where at least one expression is undeclared symbolic name
1514 -- (that denotes a dummy index), <primary expression> is a slice and
1515 -- can be only used as constituent of indexing expression. */
1518 /* maximal number of components allowed within parentheses */
1520 CODE *expression_list(MPL *mpl)
1523 struct { char *name; CODE *code; } list[1+max_dim];
1524 int flag_x, next_token, dim, j, slice = 0;
1525 xassert(mpl->token == T_LEFT);
1526 /* the flag, which allows recognizing undeclared symbolic names
1527 as dummy indices, will be automatically reset by get_token(),
1528 so save it before scanning the next token */
1529 flag_x = mpl->flag_x;
1530 get_token(mpl /* ( */);
1531 /* parse <expression list> */
1532 for (dim = 1; ; dim++)
1533 { if (dim > max_dim)
1534 error(mpl, "too many components within parentheses");
1535 /* current component of <expression list> can be either dummy
1536 index or expression */
1537 if (mpl->token == T_NAME)
1538 { /* symbolic name is recognized as dummy index only if:
1539 the flag, which allows that, is set, and
1540 the name is followed by comma or right parenthesis, and
1541 the name is undeclared */
1542 get_token(mpl /* <symbolic name> */);
1543 next_token = mpl->token;
1546 (next_token == T_COMMA || next_token == T_RIGHT) &&
1547 avl_find_node(mpl->tree, mpl->image) == NULL))
1548 { /* this is not dummy index */
1551 /* all dummy indices within the same slice must have unique
1553 for (j = 1; j < dim; j++)
1554 { if (list[j].name != NULL && strcmp(list[j].name,
1556 error(mpl, "duplicate dummy index %s not allowed",
1559 /* current component of <expression list> is dummy index */
1561 = dmp_get_atomv(mpl->pool, strlen(mpl->image)+1);
1562 strcpy(list[dim].name, mpl->image);
1563 list[dim].code = NULL;
1564 get_token(mpl /* <symbolic name> */);
1565 /* <expression list> is a slice, because at least one dummy
1566 index has appeared */
1568 /* note that the context ( <dummy index> ) is not allowed,
1569 i.e. in this case <primary expression> is considered as
1570 a parenthesized expression */
1571 if (dim == 1 && mpl->token == T_RIGHT)
1572 error(mpl, "%s not defined", list[dim].name);
1575 expr: { /* current component of <expression list> is expression */
1576 code = expression_13(mpl);
1577 /* if the current expression is followed by comma or it is
1578 not the very first expression, entire <expression list>
1579 is n-tuple or slice, in which case the current expression
1580 should be converted to symbolic type, if necessary */
1581 if (mpl->token == T_COMMA || dim > 1)
1582 { if (code->type == A_NUMERIC)
1583 code = make_unary(mpl, O_CVTSYM, code, A_SYMBOLIC, 0);
1584 /* now the expression must be of symbolic type */
1585 if (code->type != A_SYMBOLIC)
1586 error(mpl, "component expression has invalid type");
1587 xassert(code->dim == 0);
1589 list[dim].name = NULL;
1590 list[dim].code = code;
1592 /* check a token that follows the current component */
1593 if (mpl->token == T_COMMA)
1594 get_token(mpl /* , */);
1595 else if (mpl->token == T_RIGHT)
1598 error(mpl, "right parenthesis missing where expected");
1600 /* generate pseudo-code for <primary expression> */
1601 if (dim == 1 && !slice)
1602 { /* <primary expression> is a parenthesized expression */
1603 code = list[1].code;
1606 { /* <primary expression> is a n-tuple */
1607 arg.list = create_arg_list(mpl);
1608 for (j = 1; j <= dim; j++)
1609 arg.list = expand_arg_list(mpl, arg.list, list[j].code);
1610 code = make_code(mpl, O_TUPLE, &arg, A_TUPLE, dim);
1613 { /* <primary expression> is a slice */
1614 arg.slice = create_block(mpl);
1615 for (j = 1; j <= dim; j++)
1616 append_slot(mpl, arg.slice, list[j].name, list[j].code);
1617 /* note that actually pseudo-codes with op = O_SLICE are never
1619 code = make_code(mpl, O_SLICE, &arg, A_TUPLE, dim);
1621 get_token(mpl /* ) */);
1622 /* if <primary expression> is a slice, there must be the keyword
1623 'in', which follows the right parenthesis */
1624 if (slice && mpl->token != T_IN)
1625 error(mpl, "keyword in missing where expected");
1626 /* if the slice flag is set and there is the keyword 'in', which
1627 follows <primary expression>, the latter must be a slice */
1628 if (flag_x && mpl->token == T_IN && !slice)
1630 error(mpl, "syntax error in indexing expression");
1632 error(mpl, "0-ary slice not allowed");
1637 /*----------------------------------------------------------------------
1638 -- literal set - parse literal set.
1640 -- This routine parses literal set using the syntax:
1642 -- <literal set> ::= { <member list> }
1643 -- <member list> ::= <member expression>
1644 -- <member list> ::= <member list> , <member expression>
1645 -- <member expression> ::= <expression 5>
1647 -- It is assumed that the left curly brace and the very first member
1648 -- expression that follows it are already parsed. The right curly brace
1649 -- remains unscanned on exit. */
1651 CODE *literal_set(MPL *mpl, CODE *code)
1654 xassert(code != NULL);
1655 arg.list = create_arg_list(mpl);
1656 /* parse <member list> */
1658 { /* all member expressions must be n-tuples; so, if the current
1659 expression is not n-tuple, convert it to 1-tuple */
1660 if (code->type == A_NUMERIC)
1661 code = make_unary(mpl, O_CVTSYM, code, A_SYMBOLIC, 0);
1662 if (code->type == A_SYMBOLIC)
1663 code = make_unary(mpl, O_CVTTUP, code, A_TUPLE, 1);
1664 /* now the expression must be n-tuple */
1665 if (code->type != A_TUPLE)
1666 error(mpl, "member expression has invalid type");
1667 /* all member expressions must have identical dimension */
1668 if (arg.list != NULL && arg.list->x->dim != code->dim)
1669 error(mpl, "member %d has %d component%s while member %d ha"
1671 j-1, arg.list->x->dim, arg.list->x->dim == 1 ? "" : "s",
1672 j, code->dim, code->dim == 1 ? "" : "s");
1673 /* append the current expression to the member list */
1674 arg.list = expand_arg_list(mpl, arg.list, code);
1675 /* check a token that follows the current expression */
1676 if (mpl->token == T_COMMA)
1677 get_token(mpl /* , */);
1678 else if (mpl->token == T_RBRACE)
1681 error(mpl, "syntax error in literal set");
1682 /* parse the next expression that follows the comma */
1683 code = expression_5(mpl);
1685 /* generate pseudo-code for <literal set> */
1686 code = make_code(mpl, O_MAKE, &arg, A_ELEMSET, arg.list->x->dim);
1690 /*----------------------------------------------------------------------
1691 -- indexing_expression - parse indexing expression.
1693 -- This routine parses indexing expression using the syntax:
1695 -- <indexing expression> ::= <literal set>
1696 -- <indexing expression> ::= { <indexing list> }
1697 -- <indexing expression> ::= { <indexing list> : <logical expression> }
1698 -- <indexing list> ::= <indexing element>
1699 -- <indexing list> ::= <indexing list> , <indexing element>
1700 -- <indexing element> ::= <basic expression>
1701 -- <indexing element> ::= <dummy index> in <basic expression>
1702 -- <indexing element> ::= <slice> in <basic expression>
1703 -- <dummy index> ::= <symbolic name>
1704 -- <slice> ::= ( <expression list> )
1705 -- <basic expression> ::= <expression 9>
1706 -- <logical expression> ::= <expression 13>
1708 -- This routine creates domain for <indexing expression>, where each
1709 -- domain block corresponds to <indexing element>, and each domain slot
1710 -- corresponds to individual indexing position. */
1712 DOMAIN *indexing_expression(MPL *mpl)
1714 DOMAIN_BLOCK *block;
1717 xassert(mpl->token == T_LBRACE);
1718 get_token(mpl /* { */);
1719 if (mpl->token == T_RBRACE)
1720 error(mpl, "empty indexing expression not allowed");
1721 /* create domain to be constructed */
1722 domain = create_domain(mpl);
1723 /* parse either <member list> or <indexing list> that follows the
1726 { /* domain block for <indexing element> is not created yet */
1728 /* pseudo-code for <basic expression> is not generated yet */
1730 /* check a token, which <indexing element> begins with */
1731 if (mpl->token == T_NAME)
1732 { /* it is a symbolic name */
1735 /* symbolic name is recognized as dummy index only if it is
1736 followed by the keyword 'in' and not declared */
1737 get_token(mpl /* <symbolic name> */);
1738 next_token = mpl->token;
1740 if (!(next_token == T_IN &&
1741 avl_find_node(mpl->tree, mpl->image) == NULL))
1742 { /* this is not dummy index; the symbolic name begins an
1743 expression, which is either <basic expression> or the
1744 very first <member expression> in <literal set> */
1747 /* create domain block with one slot, which is assigned the
1749 block = create_block(mpl);
1750 name = dmp_get_atomv(mpl->pool, strlen(mpl->image)+1);
1751 strcpy(name, mpl->image);
1752 append_slot(mpl, block, name, NULL);
1753 get_token(mpl /* <symbolic name> */);
1754 /* the keyword 'in' is already checked above */
1755 xassert(mpl->token == T_IN);
1756 get_token(mpl /* in */);
1757 /* <basic expression> that follows the keyword 'in' will be
1760 else if (mpl->token == T_LEFT)
1761 { /* it is the left parenthesis; parse expression that begins
1762 with this parenthesis (the flag is set in order to allow
1763 recognizing slices; see the routine expression_list) */
1765 code = expression_9(mpl);
1766 if (code->op != O_SLICE)
1767 { /* this is either <basic expression> or the very first
1768 <member expression> in <literal set> */
1771 /* this is a slice; besides the corresponding domain block
1772 is already created by expression_list() */
1773 block = code->arg.slice;
1774 code = NULL; /* <basic expression> is not parsed yet */
1775 /* the keyword 'in' following the slice is already checked
1776 by expression_list() */
1777 xassert(mpl->token == T_IN);
1778 get_token(mpl /* in */);
1779 /* <basic expression> that follows the keyword 'in' will be
1782 expr: /* parse expression that follows either the keyword 'in' (in
1783 which case it can be <basic expression) or the left brace
1784 (in which case it can be <basic expression> as well as the
1785 very first <member expression> in <literal set>); note that
1786 this expression can be already parsed above */
1787 if (code == NULL) code = expression_9(mpl);
1788 /* check the type of the expression just parsed */
1789 if (code->type != A_ELEMSET)
1790 { /* it is not <basic expression> and therefore it can only
1791 be the very first <member expression> in <literal set>;
1792 however, then there must be no dummy index neither slice
1793 between the left brace and this expression */
1795 error(mpl, "domain expression has invalid type");
1796 /* parse the rest part of <literal set> and make this set
1797 be <basic expression>, i.e. the construction {a, b, c}
1798 is parsed as it were written as {A}, where A = {a, b, c}
1799 is a temporary elemental set */
1800 code = literal_set(mpl, code);
1802 /* now pseudo-code for <basic set> has been built */
1803 xassert(code != NULL);
1804 xassert(code->type == A_ELEMSET);
1805 xassert(code->dim > 0);
1806 /* if domain block for the current <indexing element> is still
1807 not created, create it for fake slice of the same dimension
1811 block = create_block(mpl);
1812 for (j = 1; j <= code->dim; j++)
1813 append_slot(mpl, block, NULL, NULL);
1815 /* number of indexing positions in <indexing element> must be
1816 the same as dimension of n-tuples in basic set */
1818 for (slot = block->list; slot != NULL; slot = slot->next)
1820 if (dim != code->dim)
1821 error(mpl,"%d %s specified for set of dimension %d",
1822 dim, dim == 1 ? "index" : "indices", code->dim);
1824 /* store pseudo-code for <basic set> in the domain block */
1825 xassert(block->code == NULL);
1827 /* and append the domain block to the domain */
1828 append_block(mpl, domain, block);
1829 /* the current <indexing element> has been completely parsed;
1830 include all its dummy indices into the symbolic name table
1831 to make them available for referencing from expressions;
1832 implicit declarations of dummy indices remain valid while
1833 the corresponding domain scope is valid */
1834 for (slot = block->list; slot != NULL; slot = slot->next)
1835 if (slot->name != NULL)
1837 xassert(avl_find_node(mpl->tree, slot->name) == NULL);
1838 node = avl_insert_node(mpl->tree, slot->name);
1839 avl_set_node_type(node, A_INDEX);
1840 avl_set_node_link(node, (void *)slot);
1842 /* check a token that follows <indexing element> */
1843 if (mpl->token == T_COMMA)
1844 get_token(mpl /* , */);
1845 else if (mpl->token == T_COLON || mpl->token == T_RBRACE)
1848 error(mpl, "syntax error in indexing expression");
1850 /* parse <logical expression> that follows the colon */
1851 if (mpl->token == T_COLON)
1852 { get_token(mpl /* : */);
1853 code = expression_13(mpl);
1854 /* convert the expression to logical type, if necessary */
1855 if (code->type == A_SYMBOLIC)
1856 code = make_unary(mpl, O_CVTNUM, code, A_NUMERIC, 0);
1857 if (code->type == A_NUMERIC)
1858 code = make_unary(mpl, O_CVTLOG, code, A_LOGICAL, 0);
1859 /* now the expression must be of logical type */
1860 if (code->type != A_LOGICAL)
1861 error(mpl, "expression following colon has invalid type");
1862 xassert(code->dim == 0);
1863 domain->code = code;
1864 /* the right brace must follow the logical expression */
1865 if (mpl->token != T_RBRACE)
1866 error(mpl, "syntax error in indexing expression");
1868 get_token(mpl /* } */);
1872 /*----------------------------------------------------------------------
1873 -- close_scope - close scope of indexing expression.
1875 -- The routine closes the scope of indexing expression specified by its
1876 -- domain and thereby makes all dummy indices introduced in the indexing
1877 -- expression no longer available for referencing. */
1879 void close_scope(MPL *mpl, DOMAIN *domain)
1880 { DOMAIN_BLOCK *block;
1883 xassert(domain != NULL);
1884 /* remove all dummy indices from the symbolic names table */
1885 for (block = domain->list; block != NULL; block = block->next)
1886 { for (slot = block->list; slot != NULL; slot = slot->next)
1887 { if (slot->name != NULL)
1888 { node = avl_find_node(mpl->tree, slot->name);
1889 xassert(node != NULL);
1890 xassert(avl_get_node_type(node) == A_INDEX);
1891 avl_delete_node(mpl->tree, node);
1898 /*----------------------------------------------------------------------
1899 -- iterated_expression - parse iterated expression.
1901 -- This routine parses primary expression using the syntax:
1903 -- <primary expression> ::= <iterated expression>
1904 -- <iterated expression> ::= sum <indexing expression> <expression 3>
1905 -- <iterated expression> ::= prod <indexing expression> <expression 3>
1906 -- <iterated expression> ::= min <indexing expression> <expression 3>
1907 -- <iterated expression> ::= max <indexing expression> <expression 3>
1908 -- <iterated expression> ::= exists <indexing expression>
1910 -- <iterated expression> ::= forall <indexing expression>
1912 -- <iterated expression> ::= setof <indexing expression> <expression 5>
1914 -- Note that parsing "integrand" depends on the iterated operator. */
1916 #if 1 /* 07/IX-2008 */
1917 static void link_up(CODE *code)
1918 { /* if we have something like sum{(i+1,j,k-1) in E} x[i,j,k],
1919 where i and k are dummy indices defined out of the iterated
1920 expression, we should link up pseudo-code for computing i+1
1921 and k-1 to pseudo-code for computing the iterated expression;
1922 this is needed to invalidate current value of the iterated
1923 expression once i or k have been changed */
1924 DOMAIN_BLOCK *block;
1926 for (block = code->arg.loop.domain->list; block != NULL;
1927 block = block->next)
1928 { for (slot = block->list; slot != NULL; slot = slot->next)
1929 { if (slot->code != NULL)
1930 { xassert(slot->code->up == NULL);
1931 slot->code->up = code;
1939 CODE *iterated_expression(MPL *mpl)
1944 /* determine operation code */
1945 xassert(mpl->token == T_NAME);
1946 if (strcmp(mpl->image, "sum") == 0)
1948 else if (strcmp(mpl->image, "prod") == 0)
1950 else if (strcmp(mpl->image, "min") == 0)
1952 else if (strcmp(mpl->image, "max") == 0)
1954 else if (strcmp(mpl->image, "forall") == 0)
1956 else if (strcmp(mpl->image, "exists") == 0)
1958 else if (strcmp(mpl->image, "setof") == 0)
1961 error(mpl, "operator %s unknown", mpl->image);
1962 strcpy(opstr, mpl->image);
1963 xassert(strlen(opstr) < sizeof(opstr));
1964 get_token(mpl /* <symbolic name> */);
1965 /* check the left brace that follows the operator name */
1966 xassert(mpl->token == T_LBRACE);
1967 /* parse indexing expression that controls iterating */
1968 arg.loop.domain = indexing_expression(mpl);
1969 /* parse "integrand" expression and generate pseudo-code */
1975 arg.loop.x = expression_3(mpl);
1976 /* convert the integrand to numeric type, if necessary */
1977 if (arg.loop.x->type == A_SYMBOLIC)
1978 arg.loop.x = make_unary(mpl, O_CVTNUM, arg.loop.x,
1980 /* now the integrand must be of numeric type or linear form
1981 (the latter is only allowed for the sum operator) */
1982 if (!(arg.loop.x->type == A_NUMERIC ||
1983 op == O_SUM && arg.loop.x->type == A_FORMULA))
1984 err: error(mpl, "integrand following %s{...} has invalid type"
1986 xassert(arg.loop.x->dim == 0);
1987 /* generate pseudo-code */
1988 code = make_code(mpl, op, &arg, arg.loop.x->type, 0);
1992 arg.loop.x = expression_12(mpl);
1993 /* convert the integrand to logical type, if necessary */
1994 if (arg.loop.x->type == A_SYMBOLIC)
1995 arg.loop.x = make_unary(mpl, O_CVTNUM, arg.loop.x,
1997 if (arg.loop.x->type == A_NUMERIC)
1998 arg.loop.x = make_unary(mpl, O_CVTLOG, arg.loop.x,
2000 /* now the integrand must be of logical type */
2001 if (arg.loop.x->type != A_LOGICAL) goto err;
2002 xassert(arg.loop.x->dim == 0);
2003 /* generate pseudo-code */
2004 code = make_code(mpl, op, &arg, A_LOGICAL, 0);
2007 arg.loop.x = expression_5(mpl);
2008 /* convert the integrand to 1-tuple, if necessary */
2009 if (arg.loop.x->type == A_NUMERIC)
2010 arg.loop.x = make_unary(mpl, O_CVTSYM, arg.loop.x,
2012 if (arg.loop.x->type == A_SYMBOLIC)
2013 arg.loop.x = make_unary(mpl, O_CVTTUP, arg.loop.x,
2015 /* now the integrand must be n-tuple */
2016 if (arg.loop.x->type != A_TUPLE) goto err;
2017 xassert(arg.loop.x->dim > 0);
2018 /* generate pseudo-code */
2019 code = make_code(mpl, op, &arg, A_ELEMSET, arg.loop.x->dim);
2024 /* close the scope of the indexing expression */
2025 close_scope(mpl, arg.loop.domain);
2026 #if 1 /* 07/IX-2008 */
2032 /*----------------------------------------------------------------------
2033 -- domain_arity - determine arity of domain.
2035 -- This routine returns arity of specified domain, which is number of
2036 -- its free dummy indices. */
2038 int domain_arity(MPL *mpl, DOMAIN *domain)
2039 { DOMAIN_BLOCK *block;
2042 xassert(mpl == mpl);
2044 for (block = domain->list; block != NULL; block = block->next)
2045 for (slot = block->list; slot != NULL; slot = slot->next)
2046 if (slot->code == NULL) arity++;
2050 /*----------------------------------------------------------------------
2051 -- set_expression - parse set expression.
2053 -- This routine parses primary expression using the syntax:
2055 -- <primary expression> ::= { }
2056 -- <primary expression> ::= <indexing expression> */
2058 CODE *set_expression(MPL *mpl)
2061 xassert(mpl->token == T_LBRACE);
2062 get_token(mpl /* { */);
2063 /* check a token that follows the left brace */
2064 if (mpl->token == T_RBRACE)
2065 { /* it is the right brace, so the resultant is an empty set of
2068 /* generate pseudo-code to build the resultant set */
2069 code = make_code(mpl, O_MAKE, &arg, A_ELEMSET, 1);
2070 get_token(mpl /* } */);
2073 { /* the next token begins an indexing expression */
2075 arg.loop.domain = indexing_expression(mpl);
2076 arg.loop.x = NULL; /* integrand is not used */
2077 /* close the scope of the indexing expression */
2078 close_scope(mpl, arg.loop.domain);
2079 /* generate pseudo-code to build the resultant set */
2080 code = make_code(mpl, O_BUILD, &arg, A_ELEMSET,
2081 domain_arity(mpl, arg.loop.domain));
2082 #if 1 /* 07/IX-2008 */
2089 /*----------------------------------------------------------------------
2090 -- branched_expression - parse conditional expression.
2092 -- This routine parses primary expression using the syntax:
2094 -- <primary expression> ::= <branched expression>
2095 -- <branched expression> ::= if <logical expression> then <expression 9>
2096 -- <branched expression> ::= if <logical expression> then <expression 9>
2097 -- else <expression 9>
2098 -- <logical expression> ::= <expression 13> */
2100 CODE *branched_expression(MPL *mpl)
2101 { CODE *code, *x, *y, *z;
2102 xassert(mpl->token == T_IF);
2103 get_token(mpl /* if */);
2104 /* parse <logical expression> that follows 'if' */
2105 x = expression_13(mpl);
2106 /* convert the expression to logical type, if necessary */
2107 if (x->type == A_SYMBOLIC)
2108 x = make_unary(mpl, O_CVTNUM, x, A_NUMERIC, 0);
2109 if (x->type == A_NUMERIC)
2110 x = make_unary(mpl, O_CVTLOG, x, A_LOGICAL, 0);
2111 /* now the expression must be of logical type */
2112 if (x->type != A_LOGICAL)
2113 error(mpl, "expression following if has invalid type");
2114 xassert(x->dim == 0);
2115 /* the keyword 'then' must follow the logical expression */
2116 if (mpl->token != T_THEN)
2117 error(mpl, "keyword then missing where expected");
2118 get_token(mpl /* then */);
2119 /* parse <expression> that follows 'then' and check its type */
2120 y = expression_9(mpl);
2121 if (!(y->type == A_NUMERIC || y->type == A_SYMBOLIC ||
2122 y->type == A_ELEMSET || y->type == A_FORMULA))
2123 error(mpl, "expression following then has invalid type");
2124 /* if the expression that follows the keyword 'then' is elemental
2125 set, the keyword 'else' cannot be omitted; otherwise else-part
2127 if (mpl->token != T_ELSE)
2128 { if (y->type == A_ELEMSET)
2129 error(mpl, "keyword else missing where expected");
2133 get_token(mpl /* else */);
2134 /* parse <expression> that follow 'else' and check its type */
2135 z = expression_9(mpl);
2136 if (!(z->type == A_NUMERIC || z->type == A_SYMBOLIC ||
2137 z->type == A_ELEMSET || z->type == A_FORMULA))
2138 error(mpl, "expression following else has invalid type");
2139 /* convert to identical types, if necessary */
2140 if (y->type == A_FORMULA || z->type == A_FORMULA)
2141 { if (y->type == A_SYMBOLIC)
2142 y = make_unary(mpl, O_CVTNUM, y, A_NUMERIC, 0);
2143 if (y->type == A_NUMERIC)
2144 y = make_unary(mpl, O_CVTLFM, y, A_FORMULA, 0);
2145 if (z->type == A_SYMBOLIC)
2146 z = make_unary(mpl, O_CVTNUM, z, A_NUMERIC, 0);
2147 if (z->type == A_NUMERIC)
2148 z = make_unary(mpl, O_CVTLFM, z, A_FORMULA, 0);
2150 if (y->type == A_SYMBOLIC || z->type == A_SYMBOLIC)
2151 { if (y->type == A_NUMERIC)
2152 y = make_unary(mpl, O_CVTSYM, y, A_SYMBOLIC, 0);
2153 if (z->type == A_NUMERIC)
2154 z = make_unary(mpl, O_CVTSYM, z, A_SYMBOLIC, 0);
2156 /* now both expressions must have identical types */
2157 if (y->type != z->type)
2158 error(mpl, "expressions following then and else have incompati"
2160 /* and identical dimensions */
2161 if (y->dim != z->dim)
2162 error(mpl, "expressions following then and else have different"
2163 " dimensions %d and %d, respectively", y->dim, z->dim);
2164 skip: /* generate pseudo-code to perform branching */
2165 code = make_ternary(mpl, O_FORK, x, y, z, y->type, y->dim);
2169 /*----------------------------------------------------------------------
2170 -- primary_expression - parse primary expression.
2172 -- This routine parses primary expression using the syntax:
2174 -- <primary expression> ::= <numeric literal>
2175 -- <primary expression> ::= Infinity
2176 -- <primary expression> ::= <string literal>
2177 -- <primary expression> ::= <dummy index>
2178 -- <primary expression> ::= <set name>
2179 -- <primary expression> ::= <set name> [ <subscript list> ]
2180 -- <primary expression> ::= <parameter name>
2181 -- <primary expression> ::= <parameter name> [ <subscript list> ]
2182 -- <primary expression> ::= <variable name>
2183 -- <primary expression> ::= <variable name> [ <subscript list> ]
2184 -- <primary expression> ::= <built-in function> ( <argument list> )
2185 -- <primary expression> ::= ( <expression list> )
2186 -- <primary expression> ::= <iterated expression>
2187 -- <primary expression> ::= { }
2188 -- <primary expression> ::= <indexing expression>
2189 -- <primary expression> ::= <branched expression>
2191 -- For complete list of syntactic rules for <primary expression> see
2192 -- comments to the corresponding parsing routines. */
2194 CODE *primary_expression(MPL *mpl)
2196 if (mpl->token == T_NUMBER)
2197 { /* parse numeric literal */
2198 code = numeric_literal(mpl);
2200 #if 1 /* 21/VII-2006 */
2201 else if (mpl->token == T_INFINITY)
2202 { /* parse "infinity" */
2205 code = make_code(mpl, O_NUMBER, &arg, A_NUMERIC, 0);
2206 get_token(mpl /* Infinity */);
2209 else if (mpl->token == T_STRING)
2210 { /* parse string literal */
2211 code = string_literal(mpl);
2213 else if (mpl->token == T_NAME)
2215 get_token(mpl /* <symbolic name> */);
2216 next_token = mpl->token;
2218 /* check a token that follows <symbolic name> */
2221 /* parse reference to subscripted object */
2222 code = object_reference(mpl);
2225 /* parse reference to built-in function */
2226 code = function_reference(mpl);
2229 /* parse iterated expression */
2230 code = iterated_expression(mpl);
2233 /* parse reference to unsubscripted object */
2234 code = object_reference(mpl);
2238 else if (mpl->token == T_LEFT)
2239 { /* parse parenthesized expression */
2240 code = expression_list(mpl);
2242 else if (mpl->token == T_LBRACE)
2243 { /* parse set expression */
2244 code = set_expression(mpl);
2246 else if (mpl->token == T_IF)
2247 { /* parse conditional expression */
2248 code = branched_expression(mpl);
2250 else if (is_reserved(mpl))
2251 { /* other reserved keywords cannot be used here */
2252 error(mpl, "invalid use of reserved keyword %s", mpl->image);
2255 error(mpl, "syntax error in expression");
2259 /*----------------------------------------------------------------------
2260 -- error_preceding - raise error if preceding operand has wrong type.
2262 -- This routine is called to raise error if operand that precedes some
2263 -- infix operator has invalid type. */
2265 void error_preceding(MPL *mpl, char *opstr)
2266 { error(mpl, "operand preceding %s has invalid type", opstr);
2270 /*----------------------------------------------------------------------
2271 -- error_following - raise error if following operand has wrong type.
2273 -- This routine is called to raise error if operand that follows some
2274 -- infix operator has invalid type. */
2276 void error_following(MPL *mpl, char *opstr)
2277 { error(mpl, "operand following %s has invalid type", opstr);
2281 /*----------------------------------------------------------------------
2282 -- error_dimension - raise error if operands have different dimension.
2284 -- This routine is called to raise error if two operands of some infix
2285 -- operator have different dimension. */
2287 void error_dimension(MPL *mpl, char *opstr, int dim1, int dim2)
2288 { error(mpl, "operands preceding and following %s have different di"
2289 "mensions %d and %d, respectively", opstr, dim1, dim2);
2293 /*----------------------------------------------------------------------
2294 -- expression_0 - parse expression of level 0.
2296 -- This routine parses expression of level 0 using the syntax:
2298 -- <expression 0> ::= <primary expression> */
2300 CODE *expression_0(MPL *mpl)
2302 code = primary_expression(mpl);
2306 /*----------------------------------------------------------------------
2307 -- expression_1 - parse expression of level 1.
2309 -- This routine parses expression of level 1 using the syntax:
2311 -- <expression 1> ::= <expression 0>
2312 -- <expression 1> ::= <expression 0> <power> <expression 1>
2313 -- <expression 1> ::= <expression 0> <power> <expression 2>
2314 -- <power> ::= ^ | ** */
2316 CODE *expression_1(MPL *mpl)
2319 x = expression_0(mpl);
2320 if (mpl->token == T_POWER)
2321 { strcpy(opstr, mpl->image);
2322 xassert(strlen(opstr) < sizeof(opstr));
2323 if (x->type == A_SYMBOLIC)
2324 x = make_unary(mpl, O_CVTNUM, x, A_NUMERIC, 0);
2325 if (x->type != A_NUMERIC)
2326 error_preceding(mpl, opstr);
2327 get_token(mpl /* ^ | ** */);
2328 if (mpl->token == T_PLUS || mpl->token == T_MINUS)
2329 y = expression_2(mpl);
2331 y = expression_1(mpl);
2332 if (y->type == A_SYMBOLIC)
2333 y = make_unary(mpl, O_CVTNUM, y, A_NUMERIC, 0);
2334 if (y->type != A_NUMERIC)
2335 error_following(mpl, opstr);
2336 x = make_binary(mpl, O_POWER, x, y, A_NUMERIC, 0);
2341 /*----------------------------------------------------------------------
2342 -- expression_2 - parse expression of level 2.
2344 -- This routine parses expression of level 2 using the syntax:
2346 -- <expression 2> ::= <expression 1>
2347 -- <expression 2> ::= + <expression 1>
2348 -- <expression 2> ::= - <expression 1> */
2350 CODE *expression_2(MPL *mpl)
2352 if (mpl->token == T_PLUS)
2353 { get_token(mpl /* + */);
2354 x = expression_1(mpl);
2355 if (x->type == A_SYMBOLIC)
2356 x = make_unary(mpl, O_CVTNUM, x, A_NUMERIC, 0);
2357 if (!(x->type == A_NUMERIC || x->type == A_FORMULA))
2358 error_following(mpl, "+");
2359 x = make_unary(mpl, O_PLUS, x, x->type, 0);
2361 else if (mpl->token == T_MINUS)
2362 { get_token(mpl /* - */);
2363 x = expression_1(mpl);
2364 if (x->type == A_SYMBOLIC)
2365 x = make_unary(mpl, O_CVTNUM, x, A_NUMERIC, 0);
2366 if (!(x->type == A_NUMERIC || x->type == A_FORMULA))
2367 error_following(mpl, "-");
2368 x = make_unary(mpl, O_MINUS, x, x->type, 0);
2371 x = expression_1(mpl);
2375 /*----------------------------------------------------------------------
2376 -- expression_3 - parse expression of level 3.
2378 -- This routine parses expression of level 3 using the syntax:
2380 -- <expression 3> ::= <expression 2>
2381 -- <expression 3> ::= <expression 3> * <expression 2>
2382 -- <expression 3> ::= <expression 3> / <expression 2>
2383 -- <expression 3> ::= <expression 3> div <expression 2>
2384 -- <expression 3> ::= <expression 3> mod <expression 2> */
2386 CODE *expression_3(MPL *mpl)
2388 x = expression_2(mpl);
2390 { if (mpl->token == T_ASTERISK)
2391 { if (x->type == A_SYMBOLIC)
2392 x = make_unary(mpl, O_CVTNUM, x, A_NUMERIC, 0);
2393 if (!(x->type == A_NUMERIC || x->type == A_FORMULA))
2394 error_preceding(mpl, "*");
2395 get_token(mpl /* * */);
2396 y = expression_2(mpl);
2397 if (y->type == A_SYMBOLIC)
2398 y = make_unary(mpl, O_CVTNUM, y, A_NUMERIC, 0);
2399 if (!(y->type == A_NUMERIC || y->type == A_FORMULA))
2400 error_following(mpl, "*");
2401 if (x->type == A_FORMULA && y->type == A_FORMULA)
2402 error(mpl, "multiplication of linear forms not allowed");
2403 if (x->type == A_NUMERIC && y->type == A_NUMERIC)
2404 x = make_binary(mpl, O_MUL, x, y, A_NUMERIC, 0);
2406 x = make_binary(mpl, O_MUL, x, y, A_FORMULA, 0);
2408 else if (mpl->token == T_SLASH)
2409 { if (x->type == A_SYMBOLIC)
2410 x = make_unary(mpl, O_CVTNUM, x, A_NUMERIC, 0);
2411 if (!(x->type == A_NUMERIC || x->type == A_FORMULA))
2412 error_preceding(mpl, "/");
2413 get_token(mpl /* / */);
2414 y = expression_2(mpl);
2415 if (y->type == A_SYMBOLIC)
2416 y = make_unary(mpl, O_CVTNUM, y, A_NUMERIC, 0);
2417 if (y->type != A_NUMERIC)
2418 error_following(mpl, "/");
2419 if (x->type == A_NUMERIC)
2420 x = make_binary(mpl, O_DIV, x, y, A_NUMERIC, 0);
2422 x = make_binary(mpl, O_DIV, x, y, A_FORMULA, 0);
2424 else if (mpl->token == T_DIV)
2425 { if (x->type == A_SYMBOLIC)
2426 x = make_unary(mpl, O_CVTNUM, x, A_NUMERIC, 0);
2427 if (x->type != A_NUMERIC)
2428 error_preceding(mpl, "div");
2429 get_token(mpl /* div */);
2430 y = expression_2(mpl);
2431 if (y->type == A_SYMBOLIC)
2432 y = make_unary(mpl, O_CVTNUM, y, A_NUMERIC, 0);
2433 if (y->type != A_NUMERIC)
2434 error_following(mpl, "div");
2435 x = make_binary(mpl, O_IDIV, x, y, A_NUMERIC, 0);
2437 else if (mpl->token == T_MOD)
2438 { if (x->type == A_SYMBOLIC)
2439 x = make_unary(mpl, O_CVTNUM, x, A_NUMERIC, 0);
2440 if (x->type != A_NUMERIC)
2441 error_preceding(mpl, "mod");
2442 get_token(mpl /* mod */);
2443 y = expression_2(mpl);
2444 if (y->type == A_SYMBOLIC)
2445 y = make_unary(mpl, O_CVTNUM, y, A_NUMERIC, 0);
2446 if (y->type != A_NUMERIC)
2447 error_following(mpl, "mod");
2448 x = make_binary(mpl, O_MOD, x, y, A_NUMERIC, 0);
2456 /*----------------------------------------------------------------------
2457 -- expression_4 - parse expression of level 4.
2459 -- This routine parses expression of level 4 using the syntax:
2461 -- <expression 4> ::= <expression 3>
2462 -- <expression 4> ::= <expression 4> + <expression 3>
2463 -- <expression 4> ::= <expression 4> - <expression 3>
2464 -- <expression 4> ::= <expression 4> less <expression 3> */
2466 CODE *expression_4(MPL *mpl)
2468 x = expression_3(mpl);
2470 { if (mpl->token == T_PLUS)
2471 { if (x->type == A_SYMBOLIC)
2472 x = make_unary(mpl, O_CVTNUM, x, A_NUMERIC, 0);
2473 if (!(x->type == A_NUMERIC || x->type == A_FORMULA))
2474 error_preceding(mpl, "+");
2475 get_token(mpl /* + */);
2476 y = expression_3(mpl);
2477 if (y->type == A_SYMBOLIC)
2478 y = make_unary(mpl, O_CVTNUM, y, A_NUMERIC, 0);
2479 if (!(y->type == A_NUMERIC || y->type == A_FORMULA))
2480 error_following(mpl, "+");
2481 if (x->type == A_NUMERIC && y->type == A_FORMULA)
2482 x = make_unary(mpl, O_CVTLFM, x, A_FORMULA, 0);
2483 if (x->type == A_FORMULA && y->type == A_NUMERIC)
2484 y = make_unary(mpl, O_CVTLFM, y, A_FORMULA, 0);
2485 x = make_binary(mpl, O_ADD, x, y, x->type, 0);
2487 else if (mpl->token == T_MINUS)
2488 { if (x->type == A_SYMBOLIC)
2489 x = make_unary(mpl, O_CVTNUM, x, A_NUMERIC, 0);
2490 if (!(x->type == A_NUMERIC || x->type == A_FORMULA))
2491 error_preceding(mpl, "-");
2492 get_token(mpl /* - */);
2493 y = expression_3(mpl);
2494 if (y->type == A_SYMBOLIC)
2495 y = make_unary(mpl, O_CVTNUM, y, A_NUMERIC, 0);
2496 if (!(y->type == A_NUMERIC || y->type == A_FORMULA))
2497 error_following(mpl, "-");
2498 if (x->type == A_NUMERIC && y->type == A_FORMULA)
2499 x = make_unary(mpl, O_CVTLFM, x, A_FORMULA, 0);
2500 if (x->type == A_FORMULA && y->type == A_NUMERIC)
2501 y = make_unary(mpl, O_CVTLFM, y, A_FORMULA, 0);
2502 x = make_binary(mpl, O_SUB, x, y, x->type, 0);
2504 else if (mpl->token == T_LESS)
2505 { if (x->type == A_SYMBOLIC)
2506 x = make_unary(mpl, O_CVTNUM, x, A_NUMERIC, 0);
2507 if (x->type != A_NUMERIC)
2508 error_preceding(mpl, "less");
2509 get_token(mpl /* less */);
2510 y = expression_3(mpl);
2511 if (y->type == A_SYMBOLIC)
2512 y = make_unary(mpl, O_CVTNUM, y, A_NUMERIC, 0);
2513 if (y->type != A_NUMERIC)
2514 error_following(mpl, "less");
2515 x = make_binary(mpl, O_LESS, x, y, A_NUMERIC, 0);
2523 /*----------------------------------------------------------------------
2524 -- expression_5 - parse expression of level 5.
2526 -- This routine parses expression of level 5 using the syntax:
2528 -- <expression 5> ::= <expression 4>
2529 -- <expression 5> ::= <expression 5> & <expression 4> */
2531 CODE *expression_5(MPL *mpl)
2533 x = expression_4(mpl);
2535 { if (mpl->token == T_CONCAT)
2536 { if (x->type == A_NUMERIC)
2537 x = make_unary(mpl, O_CVTSYM, x, A_SYMBOLIC, 0);
2538 if (x->type != A_SYMBOLIC)
2539 error_preceding(mpl, "&");
2540 get_token(mpl /* & */);
2541 y = expression_4(mpl);
2542 if (y->type == A_NUMERIC)
2543 y = make_unary(mpl, O_CVTSYM, y, A_SYMBOLIC, 0);
2544 if (y->type != A_SYMBOLIC)
2545 error_following(mpl, "&");
2546 x = make_binary(mpl, O_CONCAT, x, y, A_SYMBOLIC, 0);
2554 /*----------------------------------------------------------------------
2555 -- expression_6 - parse expression of level 6.
2557 -- This routine parses expression of level 6 using the syntax:
2559 -- <expression 6> ::= <expression 5>
2560 -- <expression 6> ::= <expression 5> .. <expression 5>
2561 -- <expression 6> ::= <expression 5> .. <expression 5> by
2562 -- <expression 5> */
2564 CODE *expression_6(MPL *mpl)
2566 x = expression_5(mpl);
2567 if (mpl->token == T_DOTS)
2568 { if (x->type == A_SYMBOLIC)
2569 x = make_unary(mpl, O_CVTNUM, x, A_NUMERIC, 0);
2570 if (x->type != A_NUMERIC)
2571 error_preceding(mpl, "..");
2572 get_token(mpl /* .. */);
2573 y = expression_5(mpl);
2574 if (y->type == A_SYMBOLIC)
2575 y = make_unary(mpl, O_CVTNUM, y, A_NUMERIC, 0);
2576 if (y->type != A_NUMERIC)
2577 error_following(mpl, "..");
2578 if (mpl->token == T_BY)
2579 { get_token(mpl /* by */);
2580 z = expression_5(mpl);
2581 if (z->type == A_SYMBOLIC)
2582 z = make_unary(mpl, O_CVTNUM, z, A_NUMERIC, 0);
2583 if (z->type != A_NUMERIC)
2584 error_following(mpl, "by");
2588 x = make_ternary(mpl, O_DOTS, x, y, z, A_ELEMSET, 1);
2593 /*----------------------------------------------------------------------
2594 -- expression_7 - parse expression of level 7.
2596 -- This routine parses expression of level 7 using the syntax:
2598 -- <expression 7> ::= <expression 6>
2599 -- <expression 7> ::= <expression 7> cross <expression 6> */
2601 CODE *expression_7(MPL *mpl)
2603 x = expression_6(mpl);
2605 { if (mpl->token == T_CROSS)
2606 { if (x->type != A_ELEMSET)
2607 error_preceding(mpl, "cross");
2608 get_token(mpl /* cross */);
2609 y = expression_6(mpl);
2610 if (y->type != A_ELEMSET)
2611 error_following(mpl, "cross");
2612 x = make_binary(mpl, O_CROSS, x, y, A_ELEMSET,
2621 /*----------------------------------------------------------------------
2622 -- expression_8 - parse expression of level 8.
2624 -- This routine parses expression of level 8 using the syntax:
2626 -- <expression 8> ::= <expression 7>
2627 -- <expression 8> ::= <expression 8> inter <expression 7> */
2629 CODE *expression_8(MPL *mpl)
2631 x = expression_7(mpl);
2633 { if (mpl->token == T_INTER)
2634 { if (x->type != A_ELEMSET)
2635 error_preceding(mpl, "inter");
2636 get_token(mpl /* inter */);
2637 y = expression_7(mpl);
2638 if (y->type != A_ELEMSET)
2639 error_following(mpl, "inter");
2640 if (x->dim != y->dim)
2641 error_dimension(mpl, "inter", x->dim, y->dim);
2642 x = make_binary(mpl, O_INTER, x, y, A_ELEMSET, x->dim);
2650 /*----------------------------------------------------------------------
2651 -- expression_9 - parse expression of level 9.
2653 -- This routine parses expression of level 9 using the syntax:
2655 -- <expression 9> ::= <expression 8>
2656 -- <expression 9> ::= <expression 9> union <expression 8>
2657 -- <expression 9> ::= <expression 9> diff <expression 8>
2658 -- <expression 9> ::= <expression 9> symdiff <expression 8> */
2660 CODE *expression_9(MPL *mpl)
2662 x = expression_8(mpl);
2664 { if (mpl->token == T_UNION)
2665 { if (x->type != A_ELEMSET)
2666 error_preceding(mpl, "union");
2667 get_token(mpl /* union */);
2668 y = expression_8(mpl);
2669 if (y->type != A_ELEMSET)
2670 error_following(mpl, "union");
2671 if (x->dim != y->dim)
2672 error_dimension(mpl, "union", x->dim, y->dim);
2673 x = make_binary(mpl, O_UNION, x, y, A_ELEMSET, x->dim);
2675 else if (mpl->token == T_DIFF)
2676 { if (x->type != A_ELEMSET)
2677 error_preceding(mpl, "diff");
2678 get_token(mpl /* diff */);
2679 y = expression_8(mpl);
2680 if (y->type != A_ELEMSET)
2681 error_following(mpl, "diff");
2682 if (x->dim != y->dim)
2683 error_dimension(mpl, "diff", x->dim, y->dim);
2684 x = make_binary(mpl, O_DIFF, x, y, A_ELEMSET, x->dim);
2686 else if (mpl->token == T_SYMDIFF)
2687 { if (x->type != A_ELEMSET)
2688 error_preceding(mpl, "symdiff");
2689 get_token(mpl /* symdiff */);
2690 y = expression_8(mpl);
2691 if (y->type != A_ELEMSET)
2692 error_following(mpl, "symdiff");
2693 if (x->dim != y->dim)
2694 error_dimension(mpl, "symdiff", x->dim, y->dim);
2695 x = make_binary(mpl, O_SYMDIFF, x, y, A_ELEMSET, x->dim);
2703 /*----------------------------------------------------------------------
2704 -- expression_10 - parse expression of level 10.
2706 -- This routine parses expression of level 10 using the syntax:
2708 -- <expression 10> ::= <expression 9>
2709 -- <expression 10> ::= <expression 9> <rho> <expression 9>
2710 -- <rho> ::= < | <= | = | == | >= | > | <> | != | in | not in | ! in |
2711 -- within | not within | ! within */
2713 CODE *expression_10(MPL *mpl)
2717 x = expression_9(mpl);
2735 op = O_WITHIN; break;
2737 strcpy(opstr, mpl->image);
2738 get_token(mpl /* not | ! */);
2739 if (mpl->token == T_IN)
2741 else if (mpl->token == T_WITHIN)
2744 error(mpl, "invalid use of %s", opstr);
2750 strcat(opstr, mpl->image);
2751 xassert(strlen(opstr) < sizeof(opstr));
2755 #if 1 /* 02/VIII-2008 */
2761 if (!(x->type == A_NUMERIC || x->type == A_SYMBOLIC))
2762 error_preceding(mpl, opstr);
2763 get_token(mpl /* <rho> */);
2764 y = expression_9(mpl);
2765 if (!(y->type == A_NUMERIC || y->type == A_SYMBOLIC))
2766 error_following(mpl, opstr);
2767 if (x->type == A_NUMERIC && y->type == A_SYMBOLIC)
2768 x = make_unary(mpl, O_CVTSYM, x, A_SYMBOLIC, 0);
2769 if (x->type == A_SYMBOLIC && y->type == A_NUMERIC)
2770 y = make_unary(mpl, O_CVTSYM, y, A_SYMBOLIC, 0);
2771 x = make_binary(mpl, op, x, y, A_LOGICAL, 0);
2773 #if 0 /* 02/VIII-2008 */
2778 if (x->type == A_SYMBOLIC)
2779 x = make_unary(mpl, O_CVTNUM, x, A_NUMERIC, 0);
2780 if (x->type != A_NUMERIC)
2781 error_preceding(mpl, opstr);
2782 get_token(mpl /* <rho> */);
2783 y = expression_9(mpl);
2784 if (y->type == A_SYMBOLIC)
2785 y = make_unary(mpl, O_CVTNUM, y, A_NUMERIC, 0);
2786 if (y->type != A_NUMERIC)
2787 error_following(mpl, opstr);
2788 x = make_binary(mpl, op, x, y, A_LOGICAL, 0);
2793 if (x->type == A_NUMERIC)
2794 x = make_unary(mpl, O_CVTSYM, x, A_SYMBOLIC, 0);
2795 if (x->type == A_SYMBOLIC)
2796 x = make_unary(mpl, O_CVTTUP, x, A_TUPLE, 1);
2797 if (x->type != A_TUPLE)
2798 error_preceding(mpl, opstr);
2799 get_token(mpl /* <rho> */);
2800 y = expression_9(mpl);
2801 if (y->type != A_ELEMSET)
2802 error_following(mpl, opstr);
2803 if (x->dim != y->dim)
2804 error_dimension(mpl, opstr, x->dim, y->dim);
2805 x = make_binary(mpl, op, x, y, A_LOGICAL, 0);
2809 if (x->type != A_ELEMSET)
2810 error_preceding(mpl, opstr);
2811 get_token(mpl /* <rho> */);
2812 y = expression_9(mpl);
2813 if (y->type != A_ELEMSET)
2814 error_following(mpl, opstr);
2815 if (x->dim != y->dim)
2816 error_dimension(mpl, opstr, x->dim, y->dim);
2817 x = make_binary(mpl, op, x, y, A_LOGICAL, 0);
2825 /*----------------------------------------------------------------------
2826 -- expression_11 - parse expression of level 11.
2828 -- This routine parses expression of level 11 using the syntax:
2830 -- <expression 11> ::= <expression 10>
2831 -- <expression 11> ::= not <expression 10>
2832 -- <expression 11> ::= ! <expression 10> */
2834 CODE *expression_11(MPL *mpl)
2837 if (mpl->token == T_NOT)
2838 { strcpy(opstr, mpl->image);
2839 xassert(strlen(opstr) < sizeof(opstr));
2840 get_token(mpl /* not | ! */);
2841 x = expression_10(mpl);
2842 if (x->type == A_SYMBOLIC)
2843 x = make_unary(mpl, O_CVTNUM, x, A_NUMERIC, 0);
2844 if (x->type == A_NUMERIC)
2845 x = make_unary(mpl, O_CVTLOG, x, A_LOGICAL, 0);
2846 if (x->type != A_LOGICAL)
2847 error_following(mpl, opstr);
2848 x = make_unary(mpl, O_NOT, x, A_LOGICAL, 0);
2851 x = expression_10(mpl);
2855 /*----------------------------------------------------------------------
2856 -- expression_12 - parse expression of level 12.
2858 -- This routine parses expression of level 12 using the syntax:
2860 -- <expression 12> ::= <expression 11>
2861 -- <expression 12> ::= <expression 12> and <expression 11>
2862 -- <expression 12> ::= <expression 12> && <expression 11> */
2864 CODE *expression_12(MPL *mpl)
2867 x = expression_11(mpl);
2869 { if (mpl->token == T_AND)
2870 { strcpy(opstr, mpl->image);
2871 xassert(strlen(opstr) < sizeof(opstr));
2872 if (x->type == A_SYMBOLIC)
2873 x = make_unary(mpl, O_CVTNUM, x, A_NUMERIC, 0);
2874 if (x->type == A_NUMERIC)
2875 x = make_unary(mpl, O_CVTLOG, x, A_LOGICAL, 0);
2876 if (x->type != A_LOGICAL)
2877 error_preceding(mpl, opstr);
2878 get_token(mpl /* and | && */);
2879 y = expression_11(mpl);
2880 if (y->type == A_SYMBOLIC)
2881 y = make_unary(mpl, O_CVTNUM, y, A_NUMERIC, 0);
2882 if (y->type == A_NUMERIC)
2883 y = make_unary(mpl, O_CVTLOG, y, A_LOGICAL, 0);
2884 if (y->type != A_LOGICAL)
2885 error_following(mpl, opstr);
2886 x = make_binary(mpl, O_AND, x, y, A_LOGICAL, 0);
2894 /*----------------------------------------------------------------------
2895 -- expression_13 - parse expression of level 13.
2897 -- This routine parses expression of level 13 using the syntax:
2899 -- <expression 13> ::= <expression 12>
2900 -- <expression 13> ::= <expression 13> or <expression 12>
2901 -- <expression 13> ::= <expression 13> || <expression 12> */
2903 CODE *expression_13(MPL *mpl)
2906 x = expression_12(mpl);
2908 { if (mpl->token == T_OR)
2909 { strcpy(opstr, mpl->image);
2910 xassert(strlen(opstr) < sizeof(opstr));
2911 if (x->type == A_SYMBOLIC)
2912 x = make_unary(mpl, O_CVTNUM, x, A_NUMERIC, 0);
2913 if (x->type == A_NUMERIC)
2914 x = make_unary(mpl, O_CVTLOG, x, A_LOGICAL, 0);
2915 if (x->type != A_LOGICAL)
2916 error_preceding(mpl, opstr);
2917 get_token(mpl /* or | || */);
2918 y = expression_12(mpl);
2919 if (y->type == A_SYMBOLIC)
2920 y = make_unary(mpl, O_CVTNUM, y, A_NUMERIC, 0);
2921 if (y->type == A_NUMERIC)
2922 y = make_unary(mpl, O_CVTLOG, y, A_LOGICAL, 0);
2923 if (y->type != A_LOGICAL)
2924 error_following(mpl, opstr);
2925 x = make_binary(mpl, O_OR, x, y, A_LOGICAL, 0);
2933 /*----------------------------------------------------------------------
2934 -- set_statement - parse set statement.
2936 -- This routine parses set statement using the syntax:
2938 -- <set statement> ::= set <symbolic name> <alias> <domain>
2940 -- <alias> ::= <empty>
2941 -- <alias> ::= <string literal>
2942 -- <domain> ::= <empty>
2943 -- <domain> ::= <indexing expression>
2944 -- <attributes> ::= <empty>
2945 -- <attributes> ::= <attributes> , dimen <numeric literal>
2946 -- <attributes> ::= <attributes> , within <expression 9>
2947 -- <attributes> ::= <attributes> , := <expression 9>
2948 -- <attributes> ::= <attributes> , default <expression 9>
2950 -- Commae in <attributes> are optional and may be omitted anywhere. */
2952 SET *set_statement(MPL *mpl)
2955 xassert(is_keyword(mpl, "set"));
2956 get_token(mpl /* set */);
2957 /* symbolic name must follow the keyword 'set' */
2958 if (mpl->token == T_NAME)
2960 else if (is_reserved(mpl))
2961 error(mpl, "invalid use of reserved keyword %s", mpl->image);
2963 error(mpl, "symbolic name missing where expected");
2964 /* there must be no other object with the same name */
2965 if (avl_find_node(mpl->tree, mpl->image) != NULL)
2966 error(mpl, "%s multiply declared", mpl->image);
2967 /* create model set */
2969 set->name = dmp_get_atomv(mpl->pool, strlen(mpl->image)+1);
2970 strcpy(set->name, mpl->image);
2981 get_token(mpl /* <symbolic name> */);
2982 /* parse optional alias */
2983 if (mpl->token == T_STRING)
2984 { set->alias = dmp_get_atomv(mpl->pool, strlen(mpl->image)+1);
2985 strcpy(set->alias, mpl->image);
2986 get_token(mpl /* <string literal> */);
2988 /* parse optional indexing expression */
2989 if (mpl->token == T_LBRACE)
2990 { set->domain = indexing_expression(mpl);
2991 set->dim = domain_arity(mpl, set->domain);
2993 /* include the set name in the symbolic names table */
2995 node = avl_insert_node(mpl->tree, set->name);
2996 avl_set_node_type(node, A_SET);
2997 avl_set_node_link(node, (void *)set);
2999 /* parse the list of optional attributes */
3001 { if (mpl->token == T_COMMA)
3002 get_token(mpl /* , */);
3003 else if (mpl->token == T_SEMICOLON)
3005 if (is_keyword(mpl, "dimen"))
3006 { /* dimension of set members */
3008 get_token(mpl /* dimen */);
3009 if (!(mpl->token == T_NUMBER &&
3010 1.0 <= mpl->value && mpl->value <= 20.0 &&
3011 floor(mpl->value) == mpl->value))
3012 error(mpl, "dimension must be integer between 1 and 20");
3013 dimen = (int)(mpl->value + 0.5);
3015 error(mpl, "at most one dimension attribute allowed");
3017 error(mpl, "dimension %d conflicts with dimension %d alr"
3018 "eady determined", dimen, set->dimen);
3021 get_token(mpl /* <numeric literal> */);
3023 else if (mpl->token == T_WITHIN || mpl->token == T_IN)
3024 { /* restricting superset */
3025 WITHIN *within, *temp;
3026 if (mpl->token == T_IN && !mpl->as_within)
3027 { warning(mpl, "keyword in understood as within");
3030 get_token(mpl /* within */);
3031 /* create new restricting superset list entry and append it
3032 to the within-list */
3033 within = alloc(WITHIN);
3034 within->code = NULL;
3035 within->next = NULL;
3036 if (set->within == NULL)
3037 set->within = within;
3039 { for (temp = set->within; temp->next != NULL; temp =
3041 temp->next = within;
3043 /* parse an expression that follows 'within' */
3044 within->code = expression_9(mpl);
3045 if (within->code->type != A_ELEMSET)
3046 error(mpl, "expression following within has invalid type"
3048 xassert(within->code->dim > 0);
3049 /* check/set dimension of set members */
3050 if (set->dimen == 0) set->dimen = within->code->dim;
3051 if (set->dimen != within->code->dim)
3052 error(mpl, "set expression following within must have di"
3053 "mension %d rather than %d",
3054 set->dimen, within->code->dim);
3056 else if (mpl->token == T_ASSIGN)
3057 { /* assignment expression */
3058 if (!(set->assign == NULL && set->option == NULL &&
3059 set->gadget == NULL))
3060 err: error(mpl, "at most one := or default/data allowed");
3061 get_token(mpl /* := */);
3062 /* parse an expression that follows ':=' */
3063 set->assign = expression_9(mpl);
3064 if (set->assign->type != A_ELEMSET)
3065 error(mpl, "expression following := has invalid type");
3066 xassert(set->assign->dim > 0);
3067 /* check/set dimension of set members */
3068 if (set->dimen == 0) set->dimen = set->assign->dim;
3069 if (set->dimen != set->assign->dim)
3070 error(mpl, "set expression following := must have dimens"
3071 "ion %d rather than %d",
3072 set->dimen, set->assign->dim);
3074 else if (is_keyword(mpl, "default"))
3075 { /* expression for default value */
3076 if (!(set->assign == NULL && set->option == NULL)) goto err;
3077 get_token(mpl /* := */);
3078 /* parse an expression that follows 'default' */
3079 set->option = expression_9(mpl);
3080 if (set->option->type != A_ELEMSET)
3081 error(mpl, "expression following default has invalid typ"
3083 xassert(set->option->dim > 0);
3084 /* check/set dimension of set members */
3085 if (set->dimen == 0) set->dimen = set->option->dim;
3086 if (set->dimen != set->option->dim)
3087 error(mpl, "set expression following default must have d"
3088 "imension %d rather than %d",
3089 set->dimen, set->option->dim);
3091 #if 1 /* 12/XII-2008 */
3092 else if (is_keyword(mpl, "data"))
3093 { /* gadget to initialize the set by data from plain set */
3097 if (!(set->assign == NULL && set->gadget == NULL)) goto err;
3098 get_token(mpl /* data */);
3099 set->gadget = gadget = alloc(GADGET);
3100 /* set name must follow the keyword 'data' */
3101 if (mpl->token == T_NAME)
3103 else if (is_reserved(mpl))
3104 error(mpl, "invalid use of reserved keyword %s",
3107 error(mpl, "set name missing where expected");
3108 /* find the set in the symbolic name table */
3109 node = avl_find_node(mpl->tree, mpl->image);
3111 error(mpl, "%s not defined", mpl->image);
3112 if (avl_get_node_type(node) != A_SET)
3113 err1: error(mpl, "%s not a plain set", mpl->image);
3114 gadget->set = avl_get_node_link(node);
3115 if (gadget->set->dim != 0) goto err1;
3116 if (gadget->set == set)
3117 error(mpl, "set cannot be initialized by itself");
3118 /* check and set dimensions */
3119 if (set->dim >= gadget->set->dimen)
3120 err2: error(mpl, "dimension of %s too small", mpl->image);
3121 if (set->dimen == 0)
3122 set->dimen = gadget->set->dimen - set->dim;
3123 if (set->dim + set->dimen > gadget->set->dimen)
3125 else if (set->dim + set->dimen < gadget->set->dimen)
3126 error(mpl, "dimension of %s too big", mpl->image);
3127 get_token(mpl /* set name */);
3128 /* left parenthesis must follow the set name */
3129 if (mpl->token == T_LEFT)
3130 get_token(mpl /* ( */);
3132 error(mpl, "left parenthesis missing where expected");
3133 /* parse permutation of component numbers */
3134 for (k = 0; k < gadget->set->dimen; k++) fff[k] = 0;
3137 { if (mpl->token != T_NUMBER)
3138 error(mpl, "component number missing where expected");
3139 if (str2int(mpl->image, &i) != 0)
3140 err3: error(mpl, "component number must be integer between "
3141 "1 and %d", gadget->set->dimen);
3142 if (!(1 <= i && i <= gadget->set->dimen)) goto err3;
3144 error(mpl, "component %d multiply specified", i);
3145 gadget->ind[k++] = i, fff[i-1] = 1;
3146 xassert(k <= gadget->set->dimen);
3147 get_token(mpl /* number */);
3148 if (mpl->token == T_COMMA)
3149 get_token(mpl /* , */);
3150 else if (mpl->token == T_RIGHT)
3153 error(mpl, "syntax error in data attribute");
3155 if (k < gadget->set->dimen)
3156 error(mpl, "there are must be %d components rather than "
3157 "%d", gadget->set->dimen, k);
3158 get_token(mpl /* ) */);
3162 error(mpl, "syntax error in set statement");
3164 /* close the domain scope */
3165 if (set->domain != NULL) close_scope(mpl, set->domain);
3166 /* if dimension of set members is still unknown, set it to 1 */
3167 if (set->dimen == 0) set->dimen = 1;
3168 /* the set statement has been completely parsed */
3169 xassert(mpl->token == T_SEMICOLON);
3170 get_token(mpl /* ; */);
3174 /*----------------------------------------------------------------------
3175 -- parameter_statement - parse parameter statement.
3177 -- This routine parses parameter statement using the syntax:
3179 -- <parameter statement> ::= param <symbolic name> <alias> <domain>
3181 -- <alias> ::= <empty>
3182 -- <alias> ::= <string literal>
3183 -- <domain> ::= <empty>
3184 -- <domain> ::= <indexing expression>
3185 -- <attributes> ::= <empty>
3186 -- <attributes> ::= <attributes> , integer
3187 -- <attributes> ::= <attributes> , binary
3188 -- <attributes> ::= <attributes> , symbolic
3189 -- <attributes> ::= <attributes> , <rho> <expression 5>
3190 -- <attributes> ::= <attributes> , in <expression 9>
3191 -- <attributes> ::= <attributes> , := <expression 5>
3192 -- <attributes> ::= <attributes> , default <expression 5>
3193 -- <rho> ::= < | <= | = | == | >= | > | <> | !=
3195 -- Commae in <attributes> are optional and may be omitted anywhere. */
3197 PARAMETER *parameter_statement(MPL *mpl)
3199 int integer_used = 0, binary_used = 0, symbolic_used = 0;
3200 xassert(is_keyword(mpl, "param"));
3201 get_token(mpl /* param */);
3202 /* symbolic name must follow the keyword 'param' */
3203 if (mpl->token == T_NAME)
3205 else if (is_reserved(mpl))
3206 error(mpl, "invalid use of reserved keyword %s", mpl->image);
3208 error(mpl, "symbolic name missing where expected");
3209 /* there must be no other object with the same name */
3210 if (avl_find_node(mpl->tree, mpl->image) != NULL)
3211 error(mpl, "%s multiply declared", mpl->image);
3212 /* create model parameter */
3213 par = alloc(PARAMETER);
3214 par->name = dmp_get_atomv(mpl->pool, strlen(mpl->image)+1);
3215 strcpy(par->name, mpl->image);
3219 par->type = A_NUMERIC;
3227 get_token(mpl /* <symbolic name> */);
3228 /* parse optional alias */
3229 if (mpl->token == T_STRING)
3230 { par->alias = dmp_get_atomv(mpl->pool, strlen(mpl->image)+1);
3231 strcpy(par->alias, mpl->image);
3232 get_token(mpl /* <string literal> */);
3234 /* parse optional indexing expression */
3235 if (mpl->token == T_LBRACE)
3236 { par->domain = indexing_expression(mpl);
3237 par->dim = domain_arity(mpl, par->domain);
3239 /* include the parameter name in the symbolic names table */
3241 node = avl_insert_node(mpl->tree, par->name);
3242 avl_set_node_type(node, A_PARAMETER);
3243 avl_set_node_link(node, (void *)par);
3245 /* parse the list of optional attributes */
3247 { if (mpl->token == T_COMMA)
3248 get_token(mpl /* , */);
3249 else if (mpl->token == T_SEMICOLON)
3251 if (is_keyword(mpl, "integer"))
3253 error(mpl, "at most one integer allowed");
3254 if (par->type == A_SYMBOLIC)
3255 error(mpl, "symbolic parameter cannot be integer");
3256 if (par->type != A_BINARY) par->type = A_INTEGER;
3258 get_token(mpl /* integer */);
3260 else if (is_keyword(mpl, "binary"))
3261 bin: { if (binary_used)
3262 error(mpl, "at most one binary allowed");
3263 if (par->type == A_SYMBOLIC)
3264 error(mpl, "symbolic parameter cannot be binary");
3265 par->type = A_BINARY;
3267 get_token(mpl /* binary */);
3269 else if (is_keyword(mpl, "logical"))
3270 { if (!mpl->as_binary)
3271 { warning(mpl, "keyword logical understood as binary");
3276 else if (is_keyword(mpl, "symbolic"))
3277 { if (symbolic_used)
3278 error(mpl, "at most one symbolic allowed");
3279 if (par->type != A_NUMERIC)
3280 error(mpl, "integer or binary parameter cannot be symbol"
3282 /* the parameter may be referenced from expressions given
3283 in the same parameter declaration, so its type must be
3284 completed before parsing that expressions */
3285 if (!(par->cond == NULL && par->in == NULL &&
3286 par->assign == NULL && par->option == NULL))
3287 error(mpl, "keyword symbolic must precede any other para"
3288 "meter attributes");
3289 par->type = A_SYMBOLIC;
3291 get_token(mpl /* symbolic */);
3293 else if (mpl->token == T_LT || mpl->token == T_LE ||
3294 mpl->token == T_EQ || mpl->token == T_GE ||
3295 mpl->token == T_GT || mpl->token == T_NE)
3296 { /* restricting condition */
3297 CONDITION *cond, *temp;
3299 /* create new restricting condition list entry and append
3300 it to the conditions list */
3301 cond = alloc(CONDITION);
3304 cond->rho = O_LT, strcpy(opstr, mpl->image); break;
3306 cond->rho = O_LE, strcpy(opstr, mpl->image); break;
3308 cond->rho = O_EQ, strcpy(opstr, mpl->image); break;
3310 cond->rho = O_GE, strcpy(opstr, mpl->image); break;
3312 cond->rho = O_GT, strcpy(opstr, mpl->image); break;
3314 cond->rho = O_NE, strcpy(opstr, mpl->image); break;
3316 xassert(mpl->token != mpl->token);
3318 xassert(strlen(opstr) < sizeof(opstr));
3321 if (par->cond == NULL)
3324 { for (temp = par->cond; temp->next != NULL; temp =
3328 #if 0 /* 13/VIII-2008 */
3329 if (par->type == A_SYMBOLIC &&
3330 !(cond->rho == O_EQ || cond->rho == O_NE))
3331 error(mpl, "inequality restriction not allowed");
3333 get_token(mpl /* rho */);
3334 /* parse an expression that follows relational operator */
3335 cond->code = expression_5(mpl);
3336 if (!(cond->code->type == A_NUMERIC ||
3337 cond->code->type == A_SYMBOLIC))
3338 error(mpl, "expression following %s has invalid type",
3340 xassert(cond->code->dim == 0);
3341 /* convert to the parameter type, if necessary */
3342 if (par->type != A_SYMBOLIC && cond->code->type ==
3344 cond->code = make_unary(mpl, O_CVTNUM, cond->code,
3346 if (par->type == A_SYMBOLIC && cond->code->type !=
3348 cond->code = make_unary(mpl, O_CVTSYM, cond->code,
3351 else if (mpl->token == T_IN || mpl->token == T_WITHIN)
3352 { /* restricting superset */
3354 if (mpl->token == T_WITHIN && !mpl->as_in)
3355 { warning(mpl, "keyword within understood as in");
3358 get_token(mpl /* in */);
3359 /* create new restricting superset list entry and append it
3364 if (par->in == NULL)
3367 { for (temp = par->in; temp->next != NULL; temp =
3371 /* parse an expression that follows 'in' */
3372 in->code = expression_9(mpl);
3373 if (in->code->type != A_ELEMSET)
3374 error(mpl, "expression following in has invalid type");
3375 xassert(in->code->dim > 0);
3376 if (in->code->dim != 1)
3377 error(mpl, "set expression following in must have dimens"
3378 "ion 1 rather than %d", in->code->dim);
3380 else if (mpl->token == T_ASSIGN)
3381 { /* assignment expression */
3382 if (!(par->assign == NULL && par->option == NULL))
3383 err: error(mpl, "at most one := or default allowed");
3384 get_token(mpl /* := */);
3385 /* parse an expression that follows ':=' */
3386 par->assign = expression_5(mpl);
3387 /* the expression must be of numeric/symbolic type */
3388 if (!(par->assign->type == A_NUMERIC ||
3389 par->assign->type == A_SYMBOLIC))
3390 error(mpl, "expression following := has invalid type");
3391 xassert(par->assign->dim == 0);
3392 /* convert to the parameter type, if necessary */
3393 if (par->type != A_SYMBOLIC && par->assign->type ==
3395 par->assign = make_unary(mpl, O_CVTNUM, par->assign,
3397 if (par->type == A_SYMBOLIC && par->assign->type !=
3399 par->assign = make_unary(mpl, O_CVTSYM, par->assign,
3402 else if (is_keyword(mpl, "default"))
3403 { /* expression for default value */
3404 if (!(par->assign == NULL && par->option == NULL)) goto err;
3405 get_token(mpl /* default */);
3406 /* parse an expression that follows 'default' */
3407 par->option = expression_5(mpl);
3408 if (!(par->option->type == A_NUMERIC ||
3409 par->option->type == A_SYMBOLIC))
3410 error(mpl, "expression following default has invalid typ"
3412 xassert(par->option->dim == 0);
3413 /* convert to the parameter type, if necessary */
3414 if (par->type != A_SYMBOLIC && par->option->type ==
3416 par->option = make_unary(mpl, O_CVTNUM, par->option,
3418 if (par->type == A_SYMBOLIC && par->option->type !=
3420 par->option = make_unary(mpl, O_CVTSYM, par->option,
3424 error(mpl, "syntax error in parameter statement");
3426 /* close the domain scope */
3427 if (par->domain != NULL) close_scope(mpl, par->domain);
3428 /* the parameter statement has been completely parsed */
3429 xassert(mpl->token == T_SEMICOLON);
3430 get_token(mpl /* ; */);
3434 /*----------------------------------------------------------------------
3435 -- variable_statement - parse variable statement.
3437 -- This routine parses variable statement using the syntax:
3439 -- <variable statement> ::= var <symbolic name> <alias> <domain>
3441 -- <alias> ::= <empty>
3442 -- <alias> ::= <string literal>
3443 -- <domain> ::= <empty>
3444 -- <domain> ::= <indexing expression>
3445 -- <attributes> ::= <empty>
3446 -- <attributes> ::= <attributes> , integer
3447 -- <attributes> ::= <attributes> , binary
3448 -- <attributes> ::= <attributes> , <rho> <expression 5>
3449 -- <rho> ::= >= | <= | = | ==
3451 -- Commae in <attributes> are optional and may be omitted anywhere. */
3453 VARIABLE *variable_statement(MPL *mpl)
3455 int integer_used = 0, binary_used = 0;
3456 xassert(is_keyword(mpl, "var"));
3458 error(mpl, "variable statement must precede solve statement");
3459 get_token(mpl /* var */);
3460 /* symbolic name must follow the keyword 'var' */
3461 if (mpl->token == T_NAME)
3463 else if (is_reserved(mpl))
3464 error(mpl, "invalid use of reserved keyword %s", mpl->image);
3466 error(mpl, "symbolic name missing where expected");
3467 /* there must be no other object with the same name */
3468 if (avl_find_node(mpl->tree, mpl->image) != NULL)
3469 error(mpl, "%s multiply declared", mpl->image);
3470 /* create model variable */
3471 var = alloc(VARIABLE);
3472 var->name = dmp_get_atomv(mpl->pool, strlen(mpl->image)+1);
3473 strcpy(var->name, mpl->image);
3477 var->type = A_NUMERIC;
3481 get_token(mpl /* <symbolic name> */);
3482 /* parse optional alias */
3483 if (mpl->token == T_STRING)
3484 { var->alias = dmp_get_atomv(mpl->pool, strlen(mpl->image)+1);
3485 strcpy(var->alias, mpl->image);
3486 get_token(mpl /* <string literal> */);
3488 /* parse optional indexing expression */
3489 if (mpl->token == T_LBRACE)
3490 { var->domain = indexing_expression(mpl);
3491 var->dim = domain_arity(mpl, var->domain);
3493 /* include the variable name in the symbolic names table */
3495 node = avl_insert_node(mpl->tree, var->name);
3496 avl_set_node_type(node, A_VARIABLE);
3497 avl_set_node_link(node, (void *)var);
3499 /* parse the list of optional attributes */
3501 { if (mpl->token == T_COMMA)
3502 get_token(mpl /* , */);
3503 else if (mpl->token == T_SEMICOLON)
3505 if (is_keyword(mpl, "integer"))
3507 error(mpl, "at most one integer allowed");
3508 if (var->type != A_BINARY) var->type = A_INTEGER;
3510 get_token(mpl /* integer */);
3512 else if (is_keyword(mpl, "binary"))
3513 bin: { if (binary_used)
3514 error(mpl, "at most one binary allowed");
3515 var->type = A_BINARY;
3517 get_token(mpl /* binary */);
3519 else if (is_keyword(mpl, "logical"))
3520 { if (!mpl->as_binary)
3521 { warning(mpl, "keyword logical understood as binary");
3526 else if (is_keyword(mpl, "symbolic"))
3527 error(mpl, "variable cannot be symbolic");
3528 else if (mpl->token == T_GE)
3530 if (var->lbnd != NULL)
3531 { if (var->lbnd == var->ubnd)
3532 error(mpl, "both fixed value and lower bound not allo"
3535 error(mpl, "at most one lower bound allowed");
3537 get_token(mpl /* >= */);
3538 /* parse an expression that specifies the lower bound */
3539 var->lbnd = expression_5(mpl);
3540 if (var->lbnd->type == A_SYMBOLIC)
3541 var->lbnd = make_unary(mpl, O_CVTNUM, var->lbnd,
3543 if (var->lbnd->type != A_NUMERIC)
3544 error(mpl, "expression following >= has invalid type");
3545 xassert(var->lbnd->dim == 0);
3547 else if (mpl->token == T_LE)
3549 if (var->ubnd != NULL)
3550 { if (var->ubnd == var->lbnd)
3551 error(mpl, "both fixed value and upper bound not allo"
3554 error(mpl, "at most one upper bound allowed");
3556 get_token(mpl /* <= */);
3557 /* parse an expression that specifies the upper bound */
3558 var->ubnd = expression_5(mpl);
3559 if (var->ubnd->type == A_SYMBOLIC)
3560 var->ubnd = make_unary(mpl, O_CVTNUM, var->ubnd,
3562 if (var->ubnd->type != A_NUMERIC)
3563 error(mpl, "expression following <= has invalid type");
3564 xassert(var->ubnd->dim == 0);
3566 else if (mpl->token == T_EQ)
3569 if (!(var->lbnd == NULL && var->ubnd == NULL))
3570 { if (var->lbnd == var->ubnd)
3571 error(mpl, "at most one fixed value allowed");
3572 else if (var->lbnd != NULL)
3573 error(mpl, "both lower bound and fixed value not allo"
3576 error(mpl, "both upper bound and fixed value not allo"
3579 strcpy(opstr, mpl->image);
3580 xassert(strlen(opstr) < sizeof(opstr));
3581 get_token(mpl /* = | == */);
3582 /* parse an expression that specifies the fixed value */
3583 var->lbnd = expression_5(mpl);
3584 if (var->lbnd->type == A_SYMBOLIC)
3585 var->lbnd = make_unary(mpl, O_CVTNUM, var->lbnd,
3587 if (var->lbnd->type != A_NUMERIC)
3588 error(mpl, "expression following %s has invalid type",
3590 xassert(var->lbnd->dim == 0);
3591 /* indicate that the variable is fixed, not bounded */
3592 var->ubnd = var->lbnd;
3594 else if (mpl->token == T_LT || mpl->token == T_GT ||
3596 error(mpl, "strict bound not allowed");
3598 error(mpl, "syntax error in variable statement");
3600 /* close the domain scope */
3601 if (var->domain != NULL) close_scope(mpl, var->domain);
3602 /* the variable statement has been completely parsed */
3603 xassert(mpl->token == T_SEMICOLON);
3604 get_token(mpl /* ; */);
3608 /*----------------------------------------------------------------------
3609 -- constraint_statement - parse constraint statement.
3611 -- This routine parses constraint statement using the syntax:
3613 -- <constraint statement> ::= <subject to> <symbolic name> <alias>
3614 -- <domain> : <constraint> ;
3615 -- <subject to> ::= <empty>
3616 -- <subject to> ::= subject to
3617 -- <subject to> ::= subj to
3618 -- <subject to> ::= s.t.
3619 -- <alias> ::= <empty>
3620 -- <alias> ::= <string literal>
3621 -- <domain> ::= <empty>
3622 -- <domain> ::= <indexing expression>
3623 -- <constraint> ::= <formula> , >= <formula>
3624 -- <constraint> ::= <formula> , <= <formula>
3625 -- <constraint> ::= <formula> , = <formula>
3626 -- <constraint> ::= <formula> , <= <formula> , <= <formula>
3627 -- <constraint> ::= <formula> , >= <formula> , >= <formula>
3628 -- <formula> ::= <expression 5>
3630 -- Commae in <constraint> are optional and may be omitted anywhere. */
3632 CONSTRAINT *constraint_statement(MPL *mpl)
3634 CODE *first, *second, *third;
3638 error(mpl, "constraint statement must precede solve statement")
3640 if (is_keyword(mpl, "subject"))
3641 { get_token(mpl /* subject */);
3642 if (!is_keyword(mpl, "to"))
3643 error(mpl, "keyword subject to incomplete");
3644 get_token(mpl /* to */);
3646 else if (is_keyword(mpl, "subj"))
3647 { get_token(mpl /* subj */);
3648 if (!is_keyword(mpl, "to"))
3649 error(mpl, "keyword subj to incomplete");
3650 get_token(mpl /* to */);
3652 else if (mpl->token == T_SPTP)
3653 get_token(mpl /* s.t. */);
3654 /* the current token must be symbolic name of constraint */
3655 if (mpl->token == T_NAME)
3657 else if (is_reserved(mpl))
3658 error(mpl, "invalid use of reserved keyword %s", mpl->image);
3660 error(mpl, "symbolic name missing where expected");
3661 /* there must be no other object with the same name */
3662 if (avl_find_node(mpl->tree, mpl->image) != NULL)
3663 error(mpl, "%s multiply declared", mpl->image);
3664 /* create model constraint */
3665 con = alloc(CONSTRAINT);
3666 con->name = dmp_get_atomv(mpl->pool, strlen(mpl->image)+1);
3667 strcpy(con->name, mpl->image);
3671 con->type = A_CONSTRAINT;
3676 get_token(mpl /* <symbolic name> */);
3677 /* parse optional alias */
3678 if (mpl->token == T_STRING)
3679 { con->alias = dmp_get_atomv(mpl->pool, strlen(mpl->image)+1);
3680 strcpy(con->alias, mpl->image);
3681 get_token(mpl /* <string literal> */);
3683 /* parse optional indexing expression */
3684 if (mpl->token == T_LBRACE)
3685 { con->domain = indexing_expression(mpl);
3686 con->dim = domain_arity(mpl, con->domain);
3688 /* include the constraint name in the symbolic names table */
3690 node = avl_insert_node(mpl->tree, con->name);
3691 avl_set_node_type(node, A_CONSTRAINT);
3692 avl_set_node_link(node, (void *)con);
3694 /* the colon must precede the first expression */
3695 if (mpl->token != T_COLON)
3696 error(mpl, "colon missing where expected");
3697 get_token(mpl /* : */);
3698 /* parse the first expression */
3699 first = expression_5(mpl);
3700 if (first->type == A_SYMBOLIC)
3701 first = make_unary(mpl, O_CVTNUM, first, A_NUMERIC, 0);
3702 if (!(first->type == A_NUMERIC || first->type == A_FORMULA))
3703 error(mpl, "expression following colon has invalid type");
3704 xassert(first->dim == 0);
3705 /* relational operator must follow the first expression */
3706 if (mpl->token == T_COMMA) get_token(mpl /* , */);
3715 error(mpl, "strict inequality not allowed");
3717 error(mpl, "constraint must be equality or inequality");
3722 strcpy(opstr, mpl->image);
3723 xassert(strlen(opstr) < sizeof(opstr));
3724 get_token(mpl /* rho */);
3725 /* parse the second expression */
3726 second = expression_5(mpl);
3727 if (second->type == A_SYMBOLIC)
3728 second = make_unary(mpl, O_CVTNUM, second, A_NUMERIC, 0);
3729 if (!(second->type == A_NUMERIC || second->type == A_FORMULA))
3730 error(mpl, "expression following %s has invalid type", opstr);
3731 xassert(second->dim == 0);
3732 /* check a token that follow the second expression */
3733 if (mpl->token == T_COMMA)
3734 { get_token(mpl /* , */);
3735 if (mpl->token == T_SEMICOLON) goto err;
3737 if (mpl->token == T_LT || mpl->token == T_LE ||
3738 mpl->token == T_EQ || mpl->token == T_GE ||
3739 mpl->token == T_GT || mpl->token == T_NE)
3740 { /* it is another relational operator, therefore the constraint
3741 is double inequality */
3742 if (rho == T_EQ || mpl->token != rho)
3743 error(mpl, "double inequality must be ... <= ... <= ... or "
3744 "... >= ... >= ...");
3745 /* the first expression cannot be linear form */
3746 if (first->type == A_FORMULA)
3747 error(mpl, "leftmost expression in double inequality cannot"
3749 get_token(mpl /* rho */);
3750 /* parse the third expression */
3751 third = expression_5(mpl);
3752 if (third->type == A_SYMBOLIC)
3753 third = make_unary(mpl, O_CVTNUM, second, A_NUMERIC, 0);
3754 if (!(third->type == A_NUMERIC || third->type == A_FORMULA))
3755 error(mpl, "rightmost expression in double inequality const"
3756 "raint has invalid type");
3757 xassert(third->dim == 0);
3758 /* the third expression also cannot be linear form */
3759 if (third->type == A_FORMULA)
3760 error(mpl, "rightmost expression in double inequality canno"
3761 "t be linear form");
3764 { /* the constraint is equality or single inequality */
3767 /* close the domain scope */
3768 if (con->domain != NULL) close_scope(mpl, con->domain);
3769 /* convert all expressions to linear form, if necessary */
3770 if (first->type != A_FORMULA)
3771 first = make_unary(mpl, O_CVTLFM, first, A_FORMULA, 0);
3772 if (second->type != A_FORMULA)
3773 second = make_unary(mpl, O_CVTLFM, second, A_FORMULA, 0);
3775 third = make_unary(mpl, O_CVTLFM, third, A_FORMULA, 0);
3776 /* arrange expressions in the constraint */
3778 { /* the constraint is equality or single inequality */
3781 /* first <= second */
3787 /* first >= second */
3793 /* first = second */
3799 xassert(rho != rho);
3803 { /* the constraint is double inequality */
3806 /* first <= second <= third */
3812 /* first >= second >= third */
3818 xassert(rho != rho);
3821 /* the constraint statement has been completely parsed */
3822 if (mpl->token != T_SEMICOLON)
3823 err: error(mpl, "syntax error in constraint statement");
3824 get_token(mpl /* ; */);
3828 /*----------------------------------------------------------------------
3829 -- objective_statement - parse objective statement.
3831 -- This routine parses objective statement using the syntax:
3833 -- <objective statement> ::= <verb> <symbolic name> <alias> <domain> :
3835 -- <verb> ::= minimize
3836 -- <verb> ::= maximize
3837 -- <alias> ::= <empty>
3838 -- <alias> ::= <string literal>
3839 -- <domain> ::= <empty>
3840 -- <domain> ::= <indexing expression>
3841 -- <formula> ::= <expression 5> */
3843 CONSTRAINT *objective_statement(MPL *mpl)
3846 if (is_keyword(mpl, "minimize"))
3848 else if (is_keyword(mpl, "maximize"))
3851 xassert(mpl != mpl);
3853 error(mpl, "objective statement must precede solve statement");
3854 get_token(mpl /* minimize | maximize */);
3855 /* symbolic name must follow the verb 'minimize' or 'maximize' */
3856 if (mpl->token == T_NAME)
3858 else if (is_reserved(mpl))
3859 error(mpl, "invalid use of reserved keyword %s", mpl->image);
3861 error(mpl, "symbolic name missing where expected");
3862 /* there must be no other object with the same name */
3863 if (avl_find_node(mpl->tree, mpl->image) != NULL)
3864 error(mpl, "%s multiply declared", mpl->image);
3865 /* create model objective */
3866 obj = alloc(CONSTRAINT);
3867 obj->name = dmp_get_atomv(mpl->pool, strlen(mpl->image)+1);
3868 strcpy(obj->name, mpl->image);
3877 get_token(mpl /* <symbolic name> */);
3878 /* parse optional alias */
3879 if (mpl->token == T_STRING)
3880 { obj->alias = dmp_get_atomv(mpl->pool, strlen(mpl->image)+1);
3881 strcpy(obj->alias, mpl->image);
3882 get_token(mpl /* <string literal> */);
3884 /* parse optional indexing expression */
3885 if (mpl->token == T_LBRACE)
3886 { obj->domain = indexing_expression(mpl);
3887 obj->dim = domain_arity(mpl, obj->domain);
3889 /* include the constraint name in the symbolic names table */
3891 node = avl_insert_node(mpl->tree, obj->name);
3892 avl_set_node_type(node, A_CONSTRAINT);
3893 avl_set_node_link(node, (void *)obj);
3895 /* the colon must precede the objective expression */
3896 if (mpl->token != T_COLON)
3897 error(mpl, "colon missing where expected");
3898 get_token(mpl /* : */);
3899 /* parse the objective expression */
3900 obj->code = expression_5(mpl);
3901 if (obj->code->type == A_SYMBOLIC)
3902 obj->code = make_unary(mpl, O_CVTNUM, obj->code, A_NUMERIC, 0);
3903 if (obj->code->type == A_NUMERIC)
3904 obj->code = make_unary(mpl, O_CVTLFM, obj->code, A_FORMULA, 0);
3905 if (obj->code->type != A_FORMULA)
3906 error(mpl, "expression following colon has invalid type");
3907 xassert(obj->code->dim == 0);
3908 /* close the domain scope */
3909 if (obj->domain != NULL) close_scope(mpl, obj->domain);
3910 /* the objective statement has been completely parsed */
3911 if (mpl->token != T_SEMICOLON)
3912 error(mpl, "syntax error in objective statement");
3913 get_token(mpl /* ; */);
3917 #if 1 /* 11/II-2008 */
3918 /***********************************************************************
3919 * table_statement - parse table statement
3921 * This routine parses table statement using the syntax:
3923 * <table statement> ::= <input table statement>
3924 * <table statement> ::= <output table statement>
3926 * <input table statement> ::=
3927 * table <table name> <alias> IN <argument list> :
3928 * <input set> [ <field list> ] , <input list> ;
3929 * <alias> ::= <empty>
3930 * <alias> ::= <string literal>
3931 * <argument list> ::= <expression 5>
3932 * <argument list> ::= <argument list> <expression 5>
3933 * <argument list> ::= <argument list> , <expression 5>
3934 * <input set> ::= <empty>
3935 * <input set> ::= <set name> <-
3936 * <field list> ::= <field name>
3937 * <field list> ::= <field list> , <field name>
3938 * <input list> ::= <input item>
3939 * <input list> ::= <input list> , <input item>
3940 * <input item> ::= <parameter name>
3941 * <input item> ::= <parameter name> ~ <field name>
3943 * <output table statement> ::=
3944 * table <table name> <alias> <domain> OUT <argument list> :
3946 * <domain> ::= <indexing expression>
3947 * <output list> ::= <output item>
3948 * <output list> ::= <output list> , <output item>
3949 * <output item> ::= <expression 5>
3950 * <output item> ::= <expression 5> ~ <field name> */
3952 TABLE *table_statement(MPL *mpl)
3954 TABARG *last_arg, *arg;
3955 TABFLD *last_fld, *fld;
3956 TABIN *last_in, *in;
3957 TABOUT *last_out, *out;
3960 char name[MAX_LENGTH+1];
3961 xassert(is_keyword(mpl, "table"));
3962 get_token(mpl /* solve */);
3963 /* symbolic name must follow the keyword table */
3964 if (mpl->token == T_NAME)
3966 else if (is_reserved(mpl))
3967 error(mpl, "invalid use of reserved keyword %s", mpl->image);
3969 error(mpl, "symbolic name missing where expected");
3970 /* there must be no other object with the same name */
3971 if (avl_find_node(mpl->tree, mpl->image) != NULL)
3972 error(mpl, "%s multiply declared", mpl->image);
3973 /* create data table */
3975 tab->name = dmp_get_atomv(mpl->pool, strlen(mpl->image)+1);
3976 strcpy(tab->name, mpl->image);
3977 get_token(mpl /* <symbolic name> */);
3978 /* parse optional alias */
3979 if (mpl->token == T_STRING)
3980 { tab->alias = dmp_get_atomv(mpl->pool, strlen(mpl->image)+1);
3981 strcpy(tab->alias, mpl->image);
3982 get_token(mpl /* <string literal> */);
3986 /* parse optional indexing expression */
3987 if (mpl->token == T_LBRACE)
3988 { /* this is output table */
3989 tab->type = A_OUTPUT;
3990 tab->u.out.domain = indexing_expression(mpl);
3991 if (!is_keyword(mpl, "OUT"))
3992 error(mpl, "keyword OUT missing where expected");
3993 get_token(mpl /* OUT */);
3996 { /* this is input table */
3997 tab->type = A_INPUT;
3998 if (!is_keyword(mpl, "IN"))
3999 error(mpl, "keyword IN missing where expected");
4000 get_token(mpl /* IN */);
4002 /* parse argument list */
4003 tab->arg = last_arg = NULL;
4005 { /* create argument list entry */
4006 arg = alloc(TABARG);
4007 /* parse argument expression */
4008 if (mpl->token == T_COMMA || mpl->token == T_COLON ||
4009 mpl->token == T_SEMICOLON)
4010 error(mpl, "argument expression missing where expected");
4011 arg->code = expression_5(mpl);
4012 /* convert the result to symbolic type, if necessary */
4013 if (arg->code->type == A_NUMERIC)
4015 make_unary(mpl, O_CVTSYM, arg->code, A_SYMBOLIC, 0);
4016 /* check that now the result is of symbolic type */
4017 if (arg->code->type != A_SYMBOLIC)
4018 error(mpl, "argument expression has invalid type");
4019 /* add the entry to the end of the list */
4021 if (last_arg == NULL)
4024 last_arg->next = arg;
4026 /* argument expression has been parsed */
4027 if (mpl->token == T_COMMA)
4028 get_token(mpl /* , */);
4029 else if (mpl->token == T_COLON || mpl->token == T_SEMICOLON)
4032 xassert(tab->arg != NULL);
4033 /* argument list must end with colon */
4034 if (mpl->token == T_COLON)
4035 get_token(mpl /* : */);
4037 error(mpl, "colon missing where expected");
4038 /* parse specific part of the table statement */
4040 { case A_INPUT: goto input_table;
4041 case A_OUTPUT: goto output_table;
4042 default: xassert(tab != tab);
4045 /* parse optional set name */
4046 if (mpl->token == T_NAME)
4047 { node = avl_find_node(mpl->tree, mpl->image);
4049 error(mpl, "%s not defined", mpl->image);
4050 if (avl_get_node_type(node) != A_SET)
4051 error(mpl, "%s not a set", mpl->image);
4052 tab->u.in.set = (SET *)avl_get_node_link(node);
4053 if (tab->u.in.set->assign != NULL)
4054 error(mpl, "%s needs no data", mpl->image);
4055 if (tab->u.in.set->dim != 0)
4056 error(mpl, "%s must be a simple set", mpl->image);
4057 get_token(mpl /* <symbolic name> */);
4058 if (mpl->token == T_INPUT)
4059 get_token(mpl /* <- */);
4061 error(mpl, "delimiter <- missing where expected");
4063 else if (is_reserved(mpl))
4064 error(mpl, "invalid use of reserved keyword %s", mpl->image);
4066 tab->u.in.set = NULL;
4067 /* parse field list */
4068 tab->u.in.fld = last_fld = NULL;
4070 if (mpl->token == T_LBRACKET)
4071 get_token(mpl /* [ */);
4073 error(mpl, "field list missing where expected");
4075 { /* create field list entry */
4076 fld = alloc(TABFLD);
4077 /* parse field name */
4078 if (mpl->token == T_NAME)
4080 else if (is_reserved(mpl))
4082 "invalid use of reserved keyword %s", mpl->image);
4084 error(mpl, "field name missing where expected");
4085 fld->name = dmp_get_atomv(mpl->pool, strlen(mpl->image)+1);
4086 strcpy(fld->name, mpl->image);
4087 get_token(mpl /* <symbolic name> */);
4088 /* add the entry to the end of the list */
4090 if (last_fld == NULL)
4091 tab->u.in.fld = fld;
4093 last_fld->next = fld;
4096 /* field name has been parsed */
4097 if (mpl->token == T_COMMA)
4098 get_token(mpl /* , */);
4099 else if (mpl->token == T_RBRACKET)
4102 error(mpl, "syntax error in field list");
4104 /* check that the set dimen is equal to the number of fields */
4105 if (tab->u.in.set != NULL && tab->u.in.set->dimen != nflds)
4106 error(mpl, "there must be %d field%s rather than %d",
4107 tab->u.in.set->dimen, tab->u.in.set->dimen == 1 ? "" : "s",
4109 get_token(mpl /* ] */);
4110 /* parse optional input list */
4111 tab->u.in.list = last_in = NULL;
4112 while (mpl->token == T_COMMA)
4113 { get_token(mpl /* , */);
4114 /* create input list entry */
4116 /* parse parameter name */
4117 if (mpl->token == T_NAME)
4119 else if (is_reserved(mpl))
4121 "invalid use of reserved keyword %s", mpl->image);
4123 error(mpl, "parameter name missing where expected");
4124 node = avl_find_node(mpl->tree, mpl->image);
4126 error(mpl, "%s not defined", mpl->image);
4127 if (avl_get_node_type(node) != A_PARAMETER)
4128 error(mpl, "%s not a parameter", mpl->image);
4129 in->par = (PARAMETER *)avl_get_node_link(node);
4130 if (in->par->dim != nflds)
4131 error(mpl, "%s must have %d subscript%s rather than %d",
4132 mpl->image, nflds, nflds == 1 ? "" : "s", in->par->dim);
4133 if (in->par->assign != NULL)
4134 error(mpl, "%s needs no data", mpl->image);
4135 get_token(mpl /* <symbolic name> */);
4136 /* parse optional field name */
4137 if (mpl->token == T_TILDE)
4138 { get_token(mpl /* ~ */);
4139 /* parse field name */
4140 if (mpl->token == T_NAME)
4142 else if (is_reserved(mpl))
4144 "invalid use of reserved keyword %s", mpl->image);
4146 error(mpl, "field name missing where expected");
4147 xassert(strlen(mpl->image) < sizeof(name));
4148 strcpy(name, mpl->image);
4149 get_token(mpl /* <symbolic name> */);
4152 { /* field name is the same as the parameter name */
4153 xassert(strlen(in->par->name) < sizeof(name));
4154 strcpy(name, in->par->name);
4156 /* assign field name */
4157 in->name = dmp_get_atomv(mpl->pool, strlen(name)+1);
4158 strcpy(in->name, name);
4159 /* add the entry to the end of the list */
4161 if (last_in == NULL)
4162 tab->u.in.list = in;
4169 /* parse output list */
4170 tab->u.out.list = last_out = NULL;
4172 { /* create output list entry */
4173 out = alloc(TABOUT);
4174 /* parse expression */
4175 if (mpl->token == T_COMMA || mpl->token == T_SEMICOLON)
4176 error(mpl, "expression missing where expected");
4177 if (mpl->token == T_NAME)
4178 { xassert(strlen(mpl->image) < sizeof(name));
4179 strcpy(name, mpl->image);
4183 out->code = expression_5(mpl);
4184 /* parse optional field name */
4185 if (mpl->token == T_TILDE)
4186 { get_token(mpl /* ~ */);
4187 /* parse field name */
4188 if (mpl->token == T_NAME)
4190 else if (is_reserved(mpl))
4192 "invalid use of reserved keyword %s", mpl->image);
4194 error(mpl, "field name missing where expected");
4195 xassert(strlen(mpl->image) < sizeof(name));
4196 strcpy(name, mpl->image);
4197 get_token(mpl /* <symbolic name> */);
4199 /* assign field name */
4200 if (name[0] == '\0')
4201 error(mpl, "field name required");
4202 out->name = dmp_get_atomv(mpl->pool, strlen(name)+1);
4203 strcpy(out->name, name);
4204 /* add the entry to the end of the list */
4206 if (last_out == NULL)
4207 tab->u.out.list = out;
4209 last_out->next = out;
4211 /* output item has been parsed */
4212 if (mpl->token == T_COMMA)
4213 get_token(mpl /* , */);
4214 else if (mpl->token == T_SEMICOLON)
4217 error(mpl, "syntax error in output list");
4219 /* close the domain scope */
4220 close_scope(mpl,tab->u.out.domain);
4222 /* the table statement must end with semicolon */
4223 if (mpl->token != T_SEMICOLON)
4224 error(mpl, "syntax error in table statement");
4225 get_token(mpl /* ; */);
4230 /*----------------------------------------------------------------------
4231 -- solve_statement - parse solve statement.
4233 -- This routine parses solve statement using the syntax:
4235 -- <solve statement> ::= solve ;
4237 -- The solve statement can be used at most once. */
4239 void *solve_statement(MPL *mpl)
4240 { xassert(is_keyword(mpl, "solve"));
4242 error(mpl, "at most one solve statement allowed");
4244 get_token(mpl /* solve */);
4245 /* semicolon must follow solve statement */
4246 if (mpl->token != T_SEMICOLON)
4247 error(mpl, "syntax error in solve statement");
4248 get_token(mpl /* ; */);
4252 /*----------------------------------------------------------------------
4253 -- check_statement - parse check statement.
4255 -- This routine parses check statement using the syntax:
4257 -- <check statement> ::= check <domain> : <expression 13> ;
4258 -- <domain> ::= <empty>
4259 -- <domain> ::= <indexing expression>
4261 -- If <domain> is omitted, colon following it may also be omitted. */
4263 CHECK *check_statement(MPL *mpl)
4265 xassert(is_keyword(mpl, "check"));
4266 /* create check descriptor */
4270 get_token(mpl /* check */);
4271 /* parse optional indexing expression */
4272 if (mpl->token == T_LBRACE)
4273 { chk->domain = indexing_expression(mpl);
4275 if (mpl->token != T_COLON)
4276 error(mpl, "colon missing where expected");
4279 /* skip optional colon */
4280 if (mpl->token == T_COLON) get_token(mpl /* : */);
4281 /* parse logical expression */
4282 chk->code = expression_13(mpl);
4283 if (chk->code->type != A_LOGICAL)
4284 error(mpl, "expression has invalid type");
4285 xassert(chk->code->dim == 0);
4286 /* close the domain scope */
4287 if (chk->domain != NULL) close_scope(mpl, chk->domain);
4288 /* the check statement has been completely parsed */
4289 if (mpl->token != T_SEMICOLON)
4290 error(mpl, "syntax error in check statement");
4291 get_token(mpl /* ; */);
4295 #if 1 /* 15/V-2010 */
4296 /*----------------------------------------------------------------------
4297 -- display_statement - parse display statement.
4299 -- This routine parses display statement using the syntax:
4301 -- <display statement> ::= display <domain> : <display list> ;
4302 -- <display statement> ::= display <domain> <display list> ;
4303 -- <domain> ::= <empty>
4304 -- <domain> ::= <indexing expression>
4305 -- <display list> ::= <display entry>
4306 -- <display list> ::= <display list> , <display entry>
4307 -- <display entry> ::= <dummy index>
4308 -- <display entry> ::= <set name>
4309 -- <display entry> ::= <set name> [ <subscript list> ]
4310 -- <display entry> ::= <parameter name>
4311 -- <display entry> ::= <parameter name> [ <subscript list> ]
4312 -- <display entry> ::= <variable name>
4313 -- <display entry> ::= <variable name> [ <subscript list> ]
4314 -- <display entry> ::= <constraint name>
4315 -- <display entry> ::= <constraint name> [ <subscript list> ]
4316 -- <display entry> ::= <expression 13> */
4318 DISPLAY *display_statement(MPL *mpl)
4320 DISPLAY1 *entry, *last_entry;
4321 xassert(is_keyword(mpl, "display"));
4322 /* create display descriptor */
4323 dpy = alloc(DISPLAY);
4325 dpy->list = last_entry = NULL;
4326 get_token(mpl /* display */);
4327 /* parse optional indexing expression */
4328 if (mpl->token == T_LBRACE)
4329 dpy->domain = indexing_expression(mpl);
4330 /* skip optional colon */
4331 if (mpl->token == T_COLON) get_token(mpl /* : */);
4332 /* parse display list */
4334 { /* create new display entry */
4335 entry = alloc(DISPLAY1);
4338 /* and append it to the display list */
4339 if (dpy->list == NULL)
4342 last_entry->next = entry;
4344 /* parse display entry */
4345 if (mpl->token == T_NAME)
4348 get_token(mpl /* <symbolic name> */);
4349 next_token = mpl->token;
4351 if (!(next_token == T_COMMA || next_token == T_SEMICOLON))
4352 { /* symbolic name begins expression */
4355 /* display entry is dummy index or model object */
4356 node = avl_find_node(mpl->tree, mpl->image);
4358 error(mpl, "%s not defined", mpl->image);
4359 entry->type = avl_get_node_type(node);
4360 switch (avl_get_node_type(node))
4363 (DOMAIN_SLOT *)avl_get_node_link(node);
4366 entry->u.set = (SET *)avl_get_node_link(node);
4369 entry->u.par = (PARAMETER *)avl_get_node_link(node);
4372 entry->u.var = (VARIABLE *)avl_get_node_link(node);
4374 error(mpl, "invalid reference to variable %s above"
4375 " solve statement", entry->u.var->name);
4378 entry->u.con = (CONSTRAINT *)avl_get_node_link(node);
4380 error(mpl, "invalid reference to %s %s above solve"
4382 entry->u.con->type == A_CONSTRAINT ?
4383 "constraint" : "objective", entry->u.con->name);
4386 xassert(node != node);
4388 get_token(mpl /* <symbolic name> */);
4391 expr: { /* display entry is expression */
4392 entry->type = A_EXPRESSION;
4393 entry->u.code = expression_13(mpl);
4395 /* check a token that follows the entry parsed */
4396 if (mpl->token == T_COMMA)
4397 get_token(mpl /* , */);
4401 /* close the domain scope */
4402 if (dpy->domain != NULL) close_scope(mpl, dpy->domain);
4403 /* the display statement has been completely parsed */
4404 if (mpl->token != T_SEMICOLON)
4405 error(mpl, "syntax error in display statement");
4406 get_token(mpl /* ; */);
4411 /*----------------------------------------------------------------------
4412 -- printf_statement - parse printf statement.
4414 -- This routine parses print statement using the syntax:
4416 -- <printf statement> ::= <printf clause> ;
4417 -- <printf statement> ::= <printf clause> > <file name> ;
4418 -- <printf statement> ::= <printf clause> >> <file name> ;
4419 -- <printf clause> ::= printf <domain> : <format> <printf list>
4420 -- <printf clause> ::= printf <domain> <format> <printf list>
4421 -- <domain> ::= <empty>
4422 -- <domain> ::= <indexing expression>
4423 -- <format> ::= <expression 5>
4424 -- <printf list> ::= <empty>
4425 -- <printf list> ::= <printf list> , <printf entry>
4426 -- <printf entry> ::= <expression 9>
4427 -- <file name> ::= <expression 5> */
4429 PRINTF *printf_statement(MPL *mpl)
4431 PRINTF1 *entry, *last_entry;
4432 xassert(is_keyword(mpl, "printf"));
4433 /* create printf descriptor */
4434 prt = alloc(PRINTF);
4437 prt->list = last_entry = NULL;
4438 get_token(mpl /* printf */);
4439 /* parse optional indexing expression */
4440 if (mpl->token == T_LBRACE)
4441 { prt->domain = indexing_expression(mpl);
4443 if (mpl->token != T_COLON)
4444 error(mpl, "colon missing where expected");
4447 /* skip optional colon */
4448 if (mpl->token == T_COLON) get_token(mpl /* : */);
4449 /* parse expression for format string */
4450 prt->fmt = expression_5(mpl);
4451 /* convert it to symbolic type, if necessary */
4452 if (prt->fmt->type == A_NUMERIC)
4453 prt->fmt = make_unary(mpl, O_CVTSYM, prt->fmt, A_SYMBOLIC, 0);
4454 /* check that now the expression is of symbolic type */
4455 if (prt->fmt->type != A_SYMBOLIC)
4456 error(mpl, "format expression has invalid type");
4457 /* parse printf list */
4458 while (mpl->token == T_COMMA)
4459 { get_token(mpl /* , */);
4460 /* create new printf entry */
4461 entry = alloc(PRINTF1);
4464 /* and append it to the printf list */
4465 if (prt->list == NULL)
4468 last_entry->next = entry;
4470 /* parse printf entry */
4471 entry->code = expression_9(mpl);
4472 if (!(entry->code->type == A_NUMERIC ||
4473 entry->code->type == A_SYMBOLIC ||
4474 entry->code->type == A_LOGICAL))
4475 error(mpl, "only numeric, symbolic, or logical expression a"
4478 /* close the domain scope */
4479 if (prt->domain != NULL) close_scope(mpl, prt->domain);
4480 #if 1 /* 14/VII-2006 */
4481 /* parse optional redirection */
4482 prt->fname = NULL, prt->app = 0;
4483 if (mpl->token == T_GT || mpl->token == T_APPEND)
4484 { prt->app = (mpl->token == T_APPEND);
4485 get_token(mpl /* > or >> */);
4486 /* parse expression for file name string */
4487 prt->fname = expression_5(mpl);
4488 /* convert it to symbolic type, if necessary */
4489 if (prt->fname->type == A_NUMERIC)
4490 prt->fname = make_unary(mpl, O_CVTSYM, prt->fname,
4492 /* check that now the expression is of symbolic type */
4493 if (prt->fname->type != A_SYMBOLIC)
4494 error(mpl, "file name expression has invalid type");
4497 /* the printf statement has been completely parsed */
4498 if (mpl->token != T_SEMICOLON)
4499 error(mpl, "syntax error in printf statement");
4500 get_token(mpl /* ; */);
4504 /*----------------------------------------------------------------------
4505 -- for_statement - parse for statement.
4507 -- This routine parses for statement using the syntax:
4509 -- <for statement> ::= for <domain> <statement>
4510 -- <for statement> ::= for <domain> { <statement list> }
4511 -- <domain> ::= <indexing expression>
4512 -- <statement list> ::= <empty>
4513 -- <statement list> ::= <statement list> <statement>
4514 -- <statement> ::= <check statement>
4515 -- <statement> ::= <display statement>
4516 -- <statement> ::= <printf statement>
4517 -- <statement> ::= <for statement> */
4519 FOR *for_statement(MPL *mpl)
4521 STATEMENT *stmt, *last_stmt;
4522 xassert(is_keyword(mpl, "for"));
4523 /* create for descriptor */
4526 fur->list = last_stmt = NULL;
4527 get_token(mpl /* for */);
4528 /* parse indexing expression */
4529 if (mpl->token != T_LBRACE)
4530 error(mpl, "indexing expression missing where expected");
4531 fur->domain = indexing_expression(mpl);
4532 /* skip optional colon */
4533 if (mpl->token == T_COLON) get_token(mpl /* : */);
4534 /* parse for statement body */
4535 if (mpl->token != T_LBRACE)
4536 { /* parse simple statement */
4537 fur->list = simple_statement(mpl, 1);
4540 { /* parse compound statement */
4541 get_token(mpl /* { */);
4542 while (mpl->token != T_RBRACE)
4543 { /* parse statement */
4544 stmt = simple_statement(mpl, 1);
4545 /* and append it to the end of the statement list */
4546 if (last_stmt == NULL)
4549 last_stmt->next = stmt;
4552 get_token(mpl /* } */);
4554 /* close the domain scope */
4555 xassert(fur->domain != NULL);
4556 close_scope(mpl, fur->domain);
4557 /* the for statement has been completely parsed */
4561 /*----------------------------------------------------------------------
4562 -- end_statement - parse end statement.
4564 -- This routine parses end statement using the syntax:
4566 -- <end statement> ::= end ; <eof> */
4568 void end_statement(MPL *mpl)
4569 { if (!mpl->flag_d && is_keyword(mpl, "end") ||
4570 mpl->flag_d && is_literal(mpl, "end"))
4571 { get_token(mpl /* end */);
4572 if (mpl->token == T_SEMICOLON)
4573 get_token(mpl /* ; */);
4575 warning(mpl, "no semicolon following end statement; missing"
4576 " semicolon inserted");
4579 warning(mpl, "unexpected end of file; missing end statement in"
4581 if (mpl->token != T_EOF)
4582 warning(mpl, "some text detected beyond end statement; text ig"
4587 /*----------------------------------------------------------------------
4588 -- simple_statement - parse simple statement.
4590 -- This routine parses simple statement using the syntax:
4592 -- <statement> ::= <set statement>
4593 -- <statement> ::= <parameter statement>
4594 -- <statement> ::= <variable statement>
4595 -- <statement> ::= <constraint statement>
4596 -- <statement> ::= <objective statement>
4597 -- <statement> ::= <solve statement>
4598 -- <statement> ::= <check statement>
4599 -- <statement> ::= <display statement>
4600 -- <statement> ::= <printf statement>
4601 -- <statement> ::= <for statement>
4603 -- If the flag spec is set, some statements cannot be used. */
4605 STATEMENT *simple_statement(MPL *mpl, int spec)
4607 stmt = alloc(STATEMENT);
4608 stmt->line = mpl->line;
4610 if (is_keyword(mpl, "set"))
4612 error(mpl, "set statement not allowed here");
4614 stmt->u.set = set_statement(mpl);
4616 else if (is_keyword(mpl, "param"))
4618 error(mpl, "parameter statement not allowed here");
4619 stmt->type = A_PARAMETER;
4620 stmt->u.par = parameter_statement(mpl);
4622 else if (is_keyword(mpl, "var"))
4624 error(mpl, "variable statement not allowed here");
4625 stmt->type = A_VARIABLE;
4626 stmt->u.var = variable_statement(mpl);
4628 else if (is_keyword(mpl, "subject") ||
4629 is_keyword(mpl, "subj") ||
4630 mpl->token == T_SPTP)
4632 error(mpl, "constraint statement not allowed here");
4633 stmt->type = A_CONSTRAINT;
4634 stmt->u.con = constraint_statement(mpl);
4636 else if (is_keyword(mpl, "minimize") ||
4637 is_keyword(mpl, "maximize"))
4639 error(mpl, "objective statement not allowed here");
4640 stmt->type = A_CONSTRAINT;
4641 stmt->u.con = objective_statement(mpl);
4643 #if 1 /* 11/II-2008 */
4644 else if (is_keyword(mpl, "table"))
4646 error(mpl, "table statement not allowed here");
4647 stmt->type = A_TABLE;
4648 stmt->u.tab = table_statement(mpl);
4651 else if (is_keyword(mpl, "solve"))
4653 error(mpl, "solve statement not allowed here");
4654 stmt->type = A_SOLVE;
4655 stmt->u.slv = solve_statement(mpl);
4657 else if (is_keyword(mpl, "check"))
4658 { stmt->type = A_CHECK;
4659 stmt->u.chk = check_statement(mpl);
4661 else if (is_keyword(mpl, "display"))
4662 { stmt->type = A_DISPLAY;
4663 stmt->u.dpy = display_statement(mpl);
4665 else if (is_keyword(mpl, "printf"))
4666 { stmt->type = A_PRINTF;
4667 stmt->u.prt = printf_statement(mpl);
4669 else if (is_keyword(mpl, "for"))
4670 { stmt->type = A_FOR;
4671 stmt->u.fur = for_statement(mpl);
4673 else if (mpl->token == T_NAME)
4675 error(mpl, "constraint statement not allowed here");
4676 stmt->type = A_CONSTRAINT;
4677 stmt->u.con = constraint_statement(mpl);
4679 else if (is_reserved(mpl))
4680 error(mpl, "invalid use of reserved keyword %s", mpl->image);
4682 error(mpl, "syntax error in model section");
4686 /*----------------------------------------------------------------------
4687 -- model_section - parse model section.
4689 -- This routine parses model section using the syntax:
4691 -- <model section> ::= <empty>
4692 -- <model section> ::= <model section> <statement>
4694 -- Parsing model section is terminated by either the keyword 'data', or
4695 -- the keyword 'end', or the end of file. */
4697 void model_section(MPL *mpl)
4698 { STATEMENT *stmt, *last_stmt;
4699 xassert(mpl->model == NULL);
4701 while (!(mpl->token == T_EOF || is_keyword(mpl, "data") ||
4702 is_keyword(mpl, "end")))
4703 { /* parse statement */
4704 stmt = simple_statement(mpl, 0);
4705 /* and append it to the end of the statement list */
4706 if (last_stmt == NULL)
4709 last_stmt->next = stmt;