1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460
|
/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
* Main authors:
* Guido Tack <guido.tack@monash.edu>
*/
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
%define api.pure
%parse-param {void *parm}
%define api.header.include {<minizinc/parser.tab.hh>}
%lex-param {void* SCANNER}
%{
#define SCANNER static_cast<ParserState*>(parm)->yyscanner
#include <iostream>
#include <fstream>
#include <map>
#include <cerrno>
#include <regex>
namespace MiniZinc{ class ParserLocation; }
#define YYLTYPE MiniZinc::ParserLocation
#define YYLTYPE_IS_DECLARED 1
#define YYLTYPE_IS_TRIVIAL 0
#define YYMAXDEPTH 10000
#define YYINITDEPTH 10000
#include <minizinc/parser.hh>
#include <minizinc/file_utils.hh>
using namespace std;
using namespace MiniZinc;
#define YYLLOC_DEFAULT(Current, Rhs, N) \
do { \
if (N > 0) { \
(Current).filename(YYRHSLOC(Rhs, 1).filename()); \
(Current).firstLine(YYRHSLOC(Rhs, 1).firstLine()); \
(Current).firstColumn(YYRHSLOC(Rhs, 1).firstColumn()); \
(Current).lastLine(YYRHSLOC(Rhs, N).lastLine()); \
(Current).lastColumn(YYRHSLOC(Rhs, N).lastColumn()); \
} else { \
(Current).filename(YYRHSLOC(Rhs, 0).filename()); \
(Current).firstLine(YYRHSLOC(Rhs, 0).lastLine()); \
(Current).firstColumn(YYRHSLOC(Rhs, 0).lastColumn()); \
(Current).lastLine(YYRHSLOC(Rhs, 0).lastLine()); \
(Current).lastColumn(YYRHSLOC(Rhs, 0).lastColumn()); \
} \
} while (false)
int mzn_yyparse(void*);
int mzn_yylex(YYSTYPE*, YYLTYPE*, void* scanner);
int mzn_yylex_init (void** scanner);
int mzn_yylex_destroy (void* scanner);
int mzn_yyget_lineno (void* scanner);
void mzn_yyset_extra (void* user_defined ,void* yyscanner );
extern int yydebug;
namespace MiniZinc {
void yyerror(YYLTYPE* location, void* parm, const string& str) {
ParserState* pp = static_cast<ParserState*>(parm);
Model* m = pp->model;
std::vector<ASTString> includeStack;
while (m->parent() != nullptr) {
m = m->parent();
includeStack.push_back(m->filename());
}
auto currentLine = pp->getCurrentLine(location->firstColumn(), location->lastColumn());
pp->hadError = true;
pp->syntaxErrors.emplace_back(Location(*location), currentLine, includeStack, str);
}
bool notInDatafile(YYLTYPE* location, void* parm, const string& item) {
ParserState* pp = static_cast<ParserState*>(parm);
if (pp->isDatafile) {
yyerror(location,parm,item+" item not allowed in data file");
return false;
}
return true;
}
Expression* createDocComment(const ParserLocation& loc, const std::string& s) {
std::vector<Expression*> args(1);
args[0] = new StringLit(loc, s);
Call* c = Call::a(Location(loc), Constants::constants().ann.doc_comment, args);
Expression::type(c, Type::ann());
return c;
}
Expression* createAccess(const ParserLocation& loc, Expression* e, const std::vector<Expression*>& idx) {
Expression* ret = e;
for (auto* expr : idx) {
// Set location of access expression (TODO: can this be more accurate?)
Expression::loc(expr, loc);
// Insert member being accessed
if (auto* fa = Expression::dynamicCast<FieldAccess>(expr)) {
fa->v(ret);
} else {
auto* aa = Expression::cast<ArrayAccess>(expr);
aa->v(ret);
}
// Set access expression as current incumbent
ret = expr;
}
return ret;
}
// Variant of definition in lexer.lxx
IntVal fast_strtointval(const std::string& s) {
MiniZinc::IntVal x = 0;
try {
for (size_t i = 0; i < s.size(); ++i) {
x = (x*10) + (s[i] - '0');
}
} catch (MiniZinc::ArithmeticError&) {
return false;
}
return x;
}
void parseFieldTail(const ParserLocation& loc, std::vector<Expression*>& parsed, const std::string& tail) {
auto it = tail.begin();
auto isWS = [&]() {
return (*it == ' ' || *it == '\f' || *it == '\xd' || *it == '\t');
};
while (it != tail.end()) {
// skip whitespace and dot
while(isWS()) { ++it; }
assert(*it == '.'); // otherwise error in the lexer regex
++it;
while(isWS()) { ++it; }
// parse field name
auto field_start = it;
bool is_num = isdigit(*it);
std::string field;
if (*it == '\'') {
++field_start;
++it;
while (*it != '\'') {
++it;
assert(it != tail.end()); // otherwise error in the lexer regex
}
field = std::string(field_start, it);
++it;
} else {
while (it != tail.end() && !isWS() && *it != '.') {
is_num = is_num && isdigit(*it);
++it;
}
field = std::string(field_start, it);
}
// emit field
Expression* field_expr = nullptr;
if (is_num) {
IntVal fieldVal = MiniZinc::fast_strtointval(field);
field_expr = IntLit::a(fieldVal);
} else {
field_expr = new Id(loc, field, nullptr);
}
parsed.push_back({ new FieldAccess(loc, nullptr, field_expr) });
}
}
}
%}
%union { long long int iValue; char* sValue; bool bValue; double dValue;
MiniZinc::Item* item;
MiniZinc::VarDecl* vardeclexpr;
std::vector<MiniZinc::VarDecl*>* vardeclexprs;
MiniZinc::TypeInst* tiexpr;
std::vector<MiniZinc::TypeInst*>* tiexprs;
MiniZinc::Expression* expression;
std::vector<MiniZinc::Expression*>* expressions1d;
std::vector<std::vector<MiniZinc::Expression*> >* expressions2d;
std::vector<std::pair<std::vector<MiniZinc::Expression*>,std::vector<MiniZinc::Expression*>>>* indexedexpressions2d;
std::pair<std::vector<MiniZinc::Expression*>,std::vector<MiniZinc::Expression*>>* indexedexpression2d;
std::vector<std::vector<std::vector<MiniZinc::Expression*> > >* expressions3d;
MiniZinc::Generator* generator;
std::vector<MiniZinc::Generator>* generators;
std::vector<std::string>* strings;
std::vector<std::pair<MiniZinc::Expression*,MiniZinc::Expression*> >* expressionPairs;
MiniZinc::Generators* generatorsPointer;
}
%locations
%define parse.error verbose
%initial-action
{
GCLock lock;
@$.filename(ASTString(static_cast<ParserState*>(parm)->filename));
}
%token <iValue> MZN_INTEGER_LITERAL "integer literal" MZN_BOOL_LITERAL "bool literal"
%token <dValue> MZN_FLOAT_LITERAL "float literal"
%token <sValue> MZN_IDENTIFIER "identifier" MZN_QUOTED_IDENTIFIER "quoted identifier" MZN_STRING_LITERAL "string literal"
%token <sValue> MZN_STRING_QUOTE_START "interpolated string start" MZN_STRING_QUOTE_MID "interpolated string middle" MZN_STRING_QUOTE_END "interpolated string end"
%token <sValue> MZN_TI_IDENTIFIER "type-inst identifier" MZN_TI_ENUM_IDENTIFIER "type-inst enum identifier" MZN_DOC_COMMENT "documentation comment" MZN_DOC_FILE_COMMENT "file-level documentation comment"
%token <sValue> MZN_FIELD_TAIL "field access"
%token MZN_VAR "var" MZN_PAR "par"
%token MZN_ABSENT "<>"
%token MZN_ANN "ann"
%token MZN_ANNOTATION "annotation"
%token MZN_ANY "any"
%token MZN_ARRAY "array"
%token MZN_BOOL "bool"
%token MZN_CASE "case"
%token MZN_CONSTRAINT "constraint"
%token MZN_DEFAULT "default"
%token MZN_ELSE "else"
%token MZN_ELSEIF "elseif"
%token MZN_ENDIF "endif"
%token MZN_ENUM "enum"
%token MZN_FLOAT "float"
%token MZN_FUNCTION "function"
%token MZN_IF "if"
%token MZN_INCLUDE "include"
%token MZN_INFINITY "infinity"
%token MZN_INT "int"
%token MZN_LET "let"
%token MZN_LIST "list"
%token <bValue> MZN_MAXIMIZE "maximize"
%token <bValue> MZN_MINIMIZE "minimize"
%token MZN_OF "of"
%token MZN_OPT "opt"
%token MZN_SATISFY "satisfy"
%token MZN_OUTPUT "output"
%token MZN_PREDICATE "predicate"
%token MZN_RECORD "record"
%token MZN_SET "set"
%token MZN_SOLVE "solve"
%token MZN_STRING "string"
%token MZN_TEST "test"
%token MZN_THEN "then"
%token MZN_TUPLE "tuple"
%token MZN_TYPE "type"
%token MZN_UNDERSCORE "_"
%token MZN_VARIANT_RECORD "variant_record"
%token MZN_WHERE "where"
%token MZN_MAX_NEGATIVE_INTEGER_LITERAL "9223372036854775808"
%token MZN_LEFT_BRACKET "["
%token MZN_LEFT_2D_BRACKET "[|"
%token MZN_RIGHT_BRACKET "]"
%token MZN_RIGHT_2D_BRACKET "|]"
// Used to signal an error when parsing a FlatZinc file
// that contains quoted identifiers
%token QUOTED_IDENTIFIER
%token MZN_INVALID_INTEGER_LITERAL "invalid integer literal"
%token MZN_INVALID_FLOAT_LITERAL "invalid float literal"
%token MZN_UNTERMINATED_STRING "unterminated string"
%token MZN_END_OF_LINE_IN_STRING "end of line inside string literal"
%token MZN_INVALID_NULL "null character"
%token MZN_INVALID_STRING_LITERAL "invalid string literal"
%token END 0 "end of file"
%token MZN_EQUIV "<->"
%token MZN_IMPL "->" MZN_RIMPL "<-"
%token MZN_OR "\\/" MZN_XOR "xor"
%token MZN_AND "/\\"
%token MZN_LE "<" MZN_GR ">" MZN_LQ "<=" MZN_GQ ">=" MZN_EQ "=" MZN_NQ "!=" MZN_WEAK_EQ "~=" MZN_WEAK_NQ "~!="
%token MZN_IN "in" MZN_SUBSET "subset" MZN_SUPERSET "superset"
%token MZN_UNION "union" MZN_DIFF "diff" MZN_SYMDIFF "symdiff"
%token MZN_DOTDOT ".."
%token MZN_DOTDOT_LE "..<"
%token MZN_LE_DOTDOT "<.."
%token MZN_LE_DOTDOT_LE "<..<"
%token MZN_PLUS "+" MZN_MINUS "-" MZN_WEAK_PLUS "~+" MZN_WEAK_MINUS "~-"
%token MZN_MULT "*" MZN_DIV "/" MZN_IDIV "div" MZN_MOD "mod" MZN_WEAK_DIV "~/" MZN_WEAK_IDIV "~div"
%token MZN_INTERSECT "intersect" MZN_WEAK_MULT "~*"
%token MZN_POW "^"
%token MZN_POW_MINUS1 "^-1"
%token MZN_NOT "not"
%token MZN_PLUSPLUS "++"
%token MZN_COLONCOLON "::"
%right PREC_ANNO
%left MZN_EQUIV
%left MZN_IMPL MZN_RIMPL
%left MZN_OR MZN_XOR
%left MZN_AND
%nonassoc MZN_LE MZN_GR MZN_LQ MZN_GQ MZN_EQ MZN_NQ MZN_WEAK_EQ MZN_WEAK_NQ
%nonassoc MZN_IN MZN_SUBSET MZN_SUPERSET
%left MZN_UNION MZN_DIFF MZN_SYMDIFF
%left MZN_INTERSECT
%nonassoc MZN_DOTDOT MZN_DOTDOT_LE MZN_LE_DOTDOT MZN_LE_DOTDOT_LE
%left MZN_PLUS MZN_MINUS MZN_WEAK_PLUS MZN_WEAK_MINUS
%left MZN_MULT MZN_DIV MZN_IDIV MZN_MOD MZN_WEAK_MULT MZN_WEAK_DIV MZN_WEAK_IDIV
%left MZN_POW MZN_POW_MINUS1
%nonassoc MZN_NOT
%left MZN_PLUSPLUS
%left MZN_DEFAULT
%left MZN_QUOTED_IDENTIFIER
%left MZN_COLONCOLON
%token MZN_EQUIV_QUOTED "'<->'"
%token MZN_IMPL_QUOTED "'->'" MZN_RIMPL_QUOTED "'<-'"
%token MZN_OR_QUOTED "'\\/'" MZN_XOR_QUOTED "'xor'"
%token MZN_AND_QUOTED "'/\\'"
%token MZN_LE_QUOTED "'<'" MZN_GR_QUOTED "'>'" MZN_LQ_QUOTED "'<='" MZN_GQ_QUOTED "'>='" MZN_EQ_QUOTED "'='" MZN_NQ_QUOTED "'!='"
%token MZN_IN_QUOTED "'in'" MZN_SUBSET_QUOTED "'subset'" MZN_SUPERSET_QUOTED "'superset'"
%token MZN_UNION_QUOTED "'union'" MZN_DIFF_QUOTED "'diff'" MZN_SYMDIFF_QUOTED "'symdiff'"
%token MZN_DOTDOT_QUOTED "'..'"
%token MZN_LE_DOTDOT_QUOTED "'<..'"
%token MZN_DOTDOT_LE_QUOTED "'..<'"
%token MZN_LE_DOTDOT_LE_QUOTED "'<..<'"
%token MZN_PLUS_QUOTED "'+'" MZN_MINUS_QUOTED "'-'"
%token MZN_MULT_QUOTED "'*'" MZN_DIV_QUOTED "'/'" MZN_IDIV_QUOTED "'div'" MZN_MOD_QUOTED "'mod'" MZN_INTERSECT_QUOTED "'intersect'"
%token MZN_POW_QUOTED "'^'"
%token MZN_NOT_QUOTED "'not'"
%token MZN_COLONCOLON_QUOTED "'::'"
%token MZN_PLUSPLUS_QUOTED "'++'"
%type <item> item item_tail include_item vardecl_item assign_item constraint_item solve_item output_item predicate_item annotation_item function_item
%type <vardeclexpr> ti_expr_and_id ti_expr_and_id_or_anon let_vardecl_item ann_param record_field
%type <vardeclexprs> params params_list params_list_head
%type <tiexpr> ti_expr base_ti_expr base_ti_expr_tail
%type <tiexprs> ti_expr_list ti_expr_list_head
%type <vardeclexprs> ti_expr_and_id_list ti_expr_and_id_list_head
%type <expression> expr expr_atom_head expr_atom_head_nonstring array_access_expr
%type <expression> set_expr string_expr string_quote_rest annotation_expr enum_construct
%type <expression> simple_array_literal simple_array_literal_2d simple_array_comp if_then_else_expr call_expr quoted_op_call let_expr operation_item_tail set_literal set_comp tuple_literal record_literal output_annotation
%type <expressions1d> expr_list expr_list_head array_access_expr_list array_access_expr_list_head elseif_list let_vardecl_item_list enum_init enum_id_list string_lit_list access_tail record_field_list_head
%type <indexedexpression2d> comp_expr_list comp_expr_list_head
%type <expressions2d> simple_array_literal_2d_list
%type <indexedexpressions2d> simple_array_literal_2d_indexed_list simple_array_literal_2d_indexed_list_head
%type <indexedexpression2d> simple_array_literal_2d_indexed_list_row simple_array_literal_2d_indexed_list_row_head
%type <expressions3d> simple_array_literal_3d_list
%type <generatorsPointer> comp_tail
%type <generator> generator generator_eq
%type <generators> generator_list generator_list_head
%type <strings> id_list id_list_head
%type <expressionPairs> comp_or_expr comp_or_expr_head
%type <expressions1d> annotations ne_annotations
%type <iValue> quoted_op
%type <sValue> id_or_quoted_op
%type <bValue> opt_opt
%%
/********************************/
/* main goal and item lists */
/********************************/
model : item_list
item_list :
/* empty */
| item_list_head semi_or_none
item_list_head:
item
{
ParserState* pp = static_cast<ParserState*>(parm);
if ($1) {
pp->model->addItem($1);
GC::unlock();
GC::lock();
}
}
| doc_file_comments item
{
ParserState* pp = static_cast<ParserState*>(parm);
if ($2) {
pp->model->addItem($2);
GC::unlock();
GC::lock();
}
}
| item_list_head ';' item
{
ParserState* pp = static_cast<ParserState*>(parm);
if ($3) {
pp->model->addItem($3);
GC::unlock();
GC::lock();
}
}
| item_list_head ';' doc_file_comments item
{
ParserState* pp = static_cast<ParserState*>(parm);
if ($4) {
pp->model->addItem($4);
GC::unlock();
GC::lock();
}
}
| item error_item_start
{ yyerror(&@2, parm, "unexpected item, expecting ';' or end of file"); YYERROR; }
| error ';' item
doc_file_comments:
MZN_DOC_FILE_COMMENT
{
ParserState* pp = static_cast<ParserState*>(parm);
if (pp->parseDocComments && $1) {
pp->model->addDocComment($1);
}
free($1);
}
| doc_file_comments MZN_DOC_FILE_COMMENT
{
ParserState* pp = static_cast<ParserState*>(parm);
if (pp->parseDocComments && $2) {
pp->model->addDocComment($2);
}
free($2);
}
semi_or_none : | ';'
item : MZN_DOC_COMMENT item_tail
{ $$ = $2;
ParserState* pp = static_cast<ParserState*>(parm);
if (FunctionI* fi = Item::dynamicCast<FunctionI>($$)) {
if (pp->parseDocComments) {
fi->ann().add(createDocComment(@1,$1));
}
} else if (VarDeclI* vdi = Item::dynamicCast<VarDeclI>($$)) {
if (pp->parseDocComments) {
Expression::addAnnotation(vdi->e(), createDocComment(@1,$1));
}
} else {
yyerror(&@2, parm, "documentation comments are only supported for function, predicate and variable declarations");
}
free($1);
}
| item_tail
{ $$ = $1; }
item_tail :
include_item
{ $$=notInDatafile(&@$,parm,"include") ? $1 : nullptr; }
| vardecl_item
{ $$=notInDatafile(&@$,parm, $1->cast<VarDeclI>()->e()->isTypeAlias() ? "type alias" : "variable declaration") ? $1 : nullptr; }
| assign_item
| constraint_item
{ $$=notInDatafile(&@$,parm,"constraint") ? $1 : nullptr; }
| solve_item
{ $$=notInDatafile(&@$,parm,"solve") ? $1 : nullptr; }
| output_item
{ $$=notInDatafile(&@$,parm,"output") ? $1 : nullptr; }
| predicate_item
{ $$=notInDatafile(&@$,parm,"predicate") ? $1 : nullptr; }
| function_item
{ $$=notInDatafile(&@$,parm,"predicate") ? $1 : nullptr; }
| annotation_item
{ $$=notInDatafile(&@$,parm,"annotation") ? $1 : nullptr; }
error_item_start : MZN_INCLUDE | MZN_ENUM | MZN_OUTPUT
| MZN_CONSTRAINT | MZN_SOLVE | MZN_PREDICATE | MZN_FUNCTION | MZN_TEST
| MZN_ANNOTATION
include_item :
MZN_INCLUDE MZN_STRING_LITERAL
{ ParserState* pp = static_cast<ParserState*>(parm);
string canonicalName=pp->canonicalFilename($2);
map<string,Model*>::iterator ret = pp->seenModels.find(canonicalName);
IncludeI* ii = new IncludeI(@$,ASTString($2));
$$ = ii;
if (ret == pp->seenModels.end()) {
Model* im = new Model;
im->setParent(pp->model);
im->setFilename(canonicalName);
string fpath = FileUtils::dir_name(pp->filename);
if (fpath=="")
fpath="./";
pp->files.emplace_back(im, ii, fpath, canonicalName, pp->isSTDLib);
ii->m(im);
pp->seenModels.insert(pair<string,Model*>(canonicalName,im));
} else {
ii->m(ret->second, false);
}
free($2);
}
vardecl_item :
ti_expr_and_id
{ if ($1) {
if (Expression::type($1->ti()).any() && $1->ti()->domain() == nullptr) {
// This is an any type, not allowed without a right hand side
yyerror(&@1, parm, "declarations with `any' type-inst require definition");
}
$$ = VarDeclI::a(@$,$1);
}
}
| ti_expr_and_id MZN_EQ expr
{
if ($1) {
$1->e($3);
$$ = VarDeclI::a(@$,$1);
}
}
| MZN_ENUM MZN_IDENTIFIER annotations
{
TypeInst* ti = new TypeInst(@$,Type::parsetint());
ti->setIsEnum(true);
VarDecl* vd = new VarDecl(@$,ti,$2);
if ($2 && $3)
Expression::addAnnotations(vd, *$3);
free($2);
$$ = VarDeclI::a(@$,vd);
}
| MZN_ENUM MZN_IDENTIFIER annotations MZN_EQ enum_init
{
if ($5) {
TypeInst* ti = new TypeInst(@$,Type::parsetint());
ti->setIsEnum(true);
Expression* e;
if ($5->size()==1) {
e = (*$5)[0];
} else {
ArrayLit* al = new ArrayLit(@$,*$5);
e = Call::a(@$, ASTString("enumFromConstructors"), {al});
}
VarDecl* vd = new VarDecl(@$,ti,$2,e);
$$ = VarDeclI::a(@$,vd);
}
free($2);
delete $5;
}
| MZN_ENUM MZN_IDENTIFIER annotations MZN_EQ MZN_LEFT_BRACKET string_lit_list MZN_RIGHT_BRACKET
{
TypeInst* ti = new TypeInst(@$,Type::parsetint());
ti->setIsEnum(true);
vector<Expression*> args;
args.push_back(new ArrayLit(@$,*$6));
Call* sl = Call::a(@$, Constants::constants().ids.anonEnumFromStrings, args);
VarDecl* vd = new VarDecl(@$,ti,$2,sl);
if ($2 && $3)
Expression::addAnnotations(vd, *$3);
free($2);
delete $6;
$$ = VarDeclI::a(@$,vd);
}
| MZN_TYPE MZN_IDENTIFIER annotations MZN_EQ ti_expr
{
TypeInst* ti = $5;
VarDecl* vd = new VarDecl(@$, nullptr, $2, ti);
if ($3) {
Expression::addAnnotations(vd, *$3);
}
free($2);
$$ = VarDeclI::a(@$, vd);
}
enum_init :
enum_construct
{
$$ = new std::vector<Expression*>({$1});
}
| enum_init MZN_PLUSPLUS enum_construct
{
$$ = $1;
if ($$) {
$$->push_back($3);
}
}
enum_construct :
'{' enum_id_list comma_or_none '}'
{
$$ = new SetLit(@$, *$2);
delete $2;
}
| MZN_IDENTIFIER '(' expr ')'
{
vector<Expression*> args({$3});
$$ = Call::a(@$, ASTString($1), args);
free($1);
}
| MZN_UNDERSCORE '(' expr ')'
{
$$ = Call::a(@$, Constants::constants().ids.anon_enum_set, {$3});
}
string_lit_list :
// empty
{ $$ = new std::vector<Expression*>(); }
| MZN_STRING_LITERAL
{ $$ = new std::vector<Expression*>();
$$->push_back(new StringLit(@$, $1)); free($1);
}
| string_lit_list ',' MZN_STRING_LITERAL
{ $$ = $1;
if ($$) $$->push_back(new StringLit(@$, $3));
free($3);
}
enum_id_list :
// empty
{ $$ = new std::vector<Expression*>(); }
| MZN_IDENTIFIER
{ $$ = new std::vector<Expression*>();
$$->push_back(new Id(@$,$1,nullptr)); free($1);
}
| enum_id_list ',' MZN_IDENTIFIER
{ $$ = $1; if ($$) $$->push_back(new Id(@$,$3,nullptr)); free($3); }
assign_item :
MZN_IDENTIFIER MZN_EQ expr
{ $$ = new AssignI(@$,$1,$3);
free($1);
}
constraint_item :
MZN_CONSTRAINT expr
{ $$ = new ConstraintI(@$,$2);}
| MZN_CONSTRAINT MZN_COLONCOLON string_expr expr
{ $$ = new ConstraintI(@$,$4);
if ($4 && $3)
Expression::ann($$->cast<ConstraintI>()->e()).add(Call::a(@2, ASTString("mzn_constraint_name"), {$3}));
}
solve_item :
MZN_SOLVE annotations MZN_SATISFY
{ $$ = SolveI::sat(@$);
if ($$ && $2) $$->cast<SolveI>()->ann().add(*$2);
delete $2;
}
| MZN_SOLVE annotations MZN_MINIMIZE expr
{ $$ = SolveI::min(@$,$4);
if ($$ && $2) $$->cast<SolveI>()->ann().add(*$2);
delete $2;
}
| MZN_SOLVE annotations MZN_MAXIMIZE expr
{ $$ = SolveI::max(@$,$4);
if ($$ && $2) $$->cast<SolveI>()->ann().add(*$2);
delete $2;
}
output_item :
MZN_OUTPUT expr
{ $$ = new OutputI(@$,$2); }
| MZN_OUTPUT MZN_COLONCOLON output_annotation expr
{ $$ = new OutputI(@$,$4);
if ($$ && $3) {
$$->cast<OutputI>()->ann().add(Call::a(@$, ASTString("mzn_output_section"), {$3}));
}
}
output_annotation :
string_expr
{ $$ = $1; }
| MZN_IDENTIFIER '(' ')'
{ $$ = Call::a(@$, $1, std::vector<Expression*>()); free($1); }
| MZN_IDENTIFIER '(' expr_list ')'
{ $$ = Call::a(@$, $1, *$3); free($1); }
| '(' expr ')'
{ $$ = $2; }
predicate_item :
MZN_PREDICATE MZN_IDENTIFIER params ann_param annotations operation_item_tail
{
ParserState* pp = static_cast<ParserState*>(parm);
if ($3 && $4) $3->push_back($4);
if ($3) $$ = new FunctionI(@$,ASTString($2),new TypeInst(@$,
Type::varbool()),*$3,$6,pp->isSTDLib,$4 != nullptr);
if ($$ && $5) $$->cast<FunctionI>()->ann().add(*$5);
free($2);
delete $3;
delete $5;
}
| MZN_TEST MZN_IDENTIFIER params ann_param annotations operation_item_tail
{
ParserState* pp = static_cast<ParserState*>(parm);
if ($3 && $4) $3->push_back($4);
if ($3) $$ = new FunctionI(@$,ASTString($2),new TypeInst(@$,
Type::parbool()),*$3,$6,pp->isSTDLib,$4 != nullptr);
if ($$ && $5) $$->cast<FunctionI>()->ann().add(*$5);
free($2);
delete $3;
delete $5;
}
| MZN_PREDICATE MZN_IDENTIFIER MZN_POW_MINUS1 params ann_param annotations operation_item_tail
{
ParserState* pp = static_cast<ParserState*>(parm);
if ($4 && $5) $4->push_back($5);
if ($4) $$ = new FunctionI(@$,ASTString(std::string($2)+"⁻¹"),new TypeInst(@$,
Type::varbool()),*$4,$7,pp->isSTDLib,$5 != nullptr);
if ($$ && $6) $$->cast<FunctionI>()->ann().add(*$6);
free($2);
delete $4;
delete $6;
}
| MZN_TEST MZN_IDENTIFIER MZN_POW_MINUS1 params ann_param annotations operation_item_tail
{
ParserState* pp = static_cast<ParserState*>(parm);
if ($4 && $5) $4->push_back($5);
if ($4) $$ = new FunctionI(@$,ASTString(std::string($2)+"⁻¹"),new TypeInst(@$,
Type::parbool()),*$4,$7,pp->isSTDLib,$5 != nullptr);
if ($$ && $6) $$->cast<FunctionI>()->ann().add(*$6);
free($2);
delete $4;
delete $6;
}
function_item :
MZN_FUNCTION ti_expr ':' id_or_quoted_op params ann_param annotations operation_item_tail
{
ParserState* pp = static_cast<ParserState*>(parm);
if ($5 && $6) $5->push_back($6);
if ($2 && Expression::type($2).any() && $2->domain() == nullptr) {
// This is an any type, not allowed without a right hand side
yyerror(&@2, parm, "return type cannot have `any' type-inst without type-inst variable");
}
if ($5) $$ = new FunctionI(@$,ASTString($4),$2,*$5,$8,pp->isSTDLib,$6 != nullptr);
if ($$ && $7) $$->cast<FunctionI>()->ann().add(*$7);
free($4);
delete $5;
delete $7;
}
| ti_expr ':' MZN_IDENTIFIER '(' params_list ')' ann_param annotations operation_item_tail
{
ParserState* pp = static_cast<ParserState*>(parm);
if ($1 && Expression::type($1).any() && $1->domain() == nullptr) {
// This is an any type, not allowed without a right hand side
yyerror(&@1, parm, "return type cannot have `any' type-inst without type-inst variable");
}
if ($5 && $7) $5->push_back($7);
if ($5) $$ = new FunctionI(@$,ASTString($3),$1,*$5,$9,pp->isSTDLib,$7 != nullptr);
if ($$ && $8) $$->cast<FunctionI>()->ann().add(*$8);
free($3);
delete $5;
delete $8;
}
annotation_item :
MZN_ANNOTATION MZN_IDENTIFIER params
{
ParserState* pp = static_cast<ParserState*>(parm);
TypeInst* ti=new TypeInst(@1,Type::ann());
if ($3==nullptr || $3->empty()) {
VarDecl* vd = new VarDecl(@$,ti,$2);
$$ = VarDeclI::a(@$,vd);
} else {
$$ = new FunctionI(@$,ASTString($2),ti,*$3,nullptr,pp->isSTDLib);
}
free($2);
delete $3;
}
| MZN_ANNOTATION MZN_IDENTIFIER params MZN_EQ expr
{
ParserState* pp = static_cast<ParserState*>(parm);
TypeInst* ti=new TypeInst(@1,Type::ann());
if ($3) $$ = new FunctionI(@$,ASTString($2),ti,*$3,$5,pp->isSTDLib);
delete $3;
}
ann_param :
/*empty*/
{ $$=nullptr; }
| MZN_ANN ':' MZN_IDENTIFIER
{ if ($3) {
auto* ident = new Id(@3, $3, nullptr);
auto* ti = new TypeInst(@$,Type::ann(1));
$$ = new VarDecl(@$, ti, ident);
$$->toplevel(false);
} }
operation_item_tail :
/*empty*/
{ $$=nullptr; }
| MZN_EQ expr
{ $$=$2; }
params :
/* empty */
{ $$=new vector<VarDecl*>(); }
| '(' params_list ')'
{ $$=$2; }
| '(' error ')'
{ $$=new vector<VarDecl*>(); }
params_list :
/* empty */
{ $$=new vector<VarDecl*>(); }
| params_list_head comma_or_none
{ $$=$1; }
params_list_head :
ti_expr_and_id_or_anon
{ $$=new vector<VarDecl*>();
if ($1) {
if (Expression::type($1->ti()).any() && $1->ti()->domain() == nullptr) {
// This is an any type, not allowed in parameter list
yyerror(&@1, parm, "parameter declaration cannot have `any' type-inst without type-inst variable");
}
$1->toplevel(false);
$$->push_back($1);
}
}
| params_list_head ',' ti_expr_and_id_or_anon
{ $$=$1;
if ($3) $3->toplevel(false);
if ($1 && $3) {
$1->push_back($3);
if (Expression::type($3->ti()).any() && $3->ti()->domain() == nullptr) {
// This is an any type, not allowed in parameter list
yyerror(&@3, parm, "parameter declaration cannot have `any' type-inst without type-inst variable");
}
}
}
comma_or_none : | ','
pipe_or_none : | '|'
ti_expr_and_id_or_anon :
ti_expr_and_id
{ $$=$1; }
| ti_expr
{ if ($1) $$=new VarDecl(@$, $1, ""); }
ti_expr_and_id :
ti_expr ':' MZN_IDENTIFIER annotations
{ if ($1 && $3) {
Id* ident = new Id(@3, $3, nullptr);
$$ = new VarDecl(@$, $1, ident);
if ($4) Expression::ann($$).add(*$4);
}
free($3);
delete $4;
}
ti_expr_and_id_list : ti_expr_and_id_list_head comma_or_none
{ $$=$1; }
ti_expr_and_id_list_head :
ti_expr_and_id
{ $$=new vector<VarDecl*>(); $$->push_back($1); }
| ti_expr_and_id_list_head ',' ti_expr_and_id
{ $$=$1; if ($1 && $3) $1->push_back($3); }
ti_expr_list : ti_expr_list_head comma_or_none
{ $$=$1; }
ti_expr_list_head :
ti_expr
{ $$=new vector<TypeInst*>(); $$->push_back($1); }
| ti_expr_list_head ',' ti_expr
{ $$=$1; if ($1 && $3) $1->push_back($3); }
ti_expr :
base_ti_expr
| MZN_ARRAY MZN_LEFT_BRACKET ti_expr_list MZN_RIGHT_BRACKET MZN_OF ti_expr %prec MZN_NOT
{
if ($6 != nullptr && $6->isarray()) {
$$ = new TypeInst(@$, Type::tuple(), $6);
} else {
$$ = $6;
}
if ($$ != nullptr && $3 != nullptr) {
$$->setRanges(*$3);
}
delete $3;
}
| MZN_LIST MZN_OF base_ti_expr
{
$$ = $3;
std::vector<TypeInst*> ti(1);
ti[0] = new TypeInst(@$,Type::parint());
if ($$ != nullptr){
$$->setRanges(ti);
}
}
| ti_expr MZN_PLUSPLUS base_ti_expr
{
$$ = $1;
if ($$ != nullptr) {
Type tt = Expression::type($$);
tt.dim(0);
TypeInst* lhs = new TypeInst(@$, tt, $1->domain());
BinOp* bop = new BinOp(@$, lhs, BOT_PLUSPLUS, $3);
bop->type(tt);
$$->domain(bop);
}
}
base_ti_expr :
base_ti_expr_tail
{ $$ = $1;
}
| MZN_OPT base_ti_expr_tail
{ $$ = $2;
if ($$ != nullptr) {
Type tt = Expression::type($$);
tt.ot(Type::OT_OPTIONAL);
tt.otExplicit(true);
$$->type(tt);
}
}
| MZN_PAR opt_opt base_ti_expr_tail
{ $$ = $3;
if ($$ != nullptr) {
Type tt = Expression::type($$);
tt.tiExplicit(true);
if ($2) {
tt.ot(Type::OT_OPTIONAL);
tt.otExplicit(true);
}
$$->type(tt);
}
}
| MZN_VAR opt_opt base_ti_expr_tail
{ $$ = $3;
if ($$ != nullptr) {
Type tt = Expression::type($$);
tt.ti(Type::TI_VAR);
tt.tiExplicit(true);
if ($2) {
tt.ot(Type::OT_OPTIONAL);
tt.otExplicit(true);
}
$$->type(tt);
}
}
| MZN_SET MZN_OF base_ti_expr_tail
{ $$ = $3;
if ($$ != nullptr) {
Type tt = Expression::type($$);
tt.st(Type::ST_SET);
$$->type(tt);
}
}
| MZN_OPT MZN_SET MZN_OF base_ti_expr_tail
{ $$ = $4;
if ($$ != nullptr) {
Type tt = Expression::type($$);
tt.st(Type::ST_SET);
tt.ot(Type::OT_OPTIONAL);
tt.otExplicit(true);
$$->type(tt);
}
}
| MZN_PAR opt_opt MZN_SET MZN_OF base_ti_expr_tail
{ $$ = $5;
if ($$ != nullptr) {
Type tt = Expression::type($$);
tt.tiExplicit(true);
tt.st(Type::ST_SET);
if ($2) {
tt.ot(Type::OT_OPTIONAL);
tt.otExplicit(true);
}
$$->type(tt);
}
}
| MZN_VAR opt_opt MZN_SET MZN_OF base_ti_expr_tail
{ $$ = $5;
if ($$ != nullptr) {
Type tt = Expression::type($$);
tt.ti(Type::TI_VAR);
tt.tiExplicit(true);
tt.st(Type::ST_SET);
if ($2) {
tt.ot(Type::OT_OPTIONAL);
tt.otExplicit(true);
}
$$->type(tt);
}
}
| MZN_ANY MZN_TI_IDENTIFIER
{ $$ = new TypeInst(@$,Type::mkAny(),new TIId(@$, $2));
free($2);
}
| MZN_ANY
{ $$ = new TypeInst(@$,Type::mkAny()); }
opt_opt:
/* nothing */
{ $$ = false; }
| MZN_OPT
{ $$ = true; }
base_ti_expr_tail :
MZN_INT
{ $$ = new TypeInst(@$,Type::parint()); }
| MZN_BOOL
{ $$ = new TypeInst(@$,Type::parbool()); }
| MZN_FLOAT
{ $$ = new TypeInst(@$,Type::parfloat()); }
| MZN_STRING
{ $$ = new TypeInst(@$,Type::parstring()); }
| MZN_ANN
{ $$ = new TypeInst(@$,Type::ann()); }
| MZN_TUPLE '(' ti_expr_list ')'
{
std::vector<Expression*> tmp($3->begin(), $3->end());
ArrayLit* al = ArrayLit::constructTuple(@$, tmp);
$$ = new TypeInst(@$, Type::tuple(), al);
delete $3;
}
| MZN_RECORD '(' ti_expr_and_id_list ')'
{
for (auto* vd : *$3) {
vd->toplevel(false);
}
std::vector<Expression*> tmp($3->begin(), $3->end());
ArrayLit* al = ArrayLit::constructTuple(@$, tmp);
$$ = new TypeInst(@$, Type::record(), al);
delete $3;
}
| set_expr
{ if ($1) $$ = new TypeInst(@$,Type(),$1); }
| MZN_TI_IDENTIFIER
{ $$ = new TypeInst(@$,Type::top(),
new TIId(@$, $1));
free($1);
}
| MZN_TI_ENUM_IDENTIFIER
{ $$ = new TypeInst(@$,Type::parint(),
new TIId(@$, $1));
free($1);
}
array_access_expr_list : array_access_expr_list_head comma_or_none
array_access_expr_list_head :
array_access_expr
{ $$=new std::vector<MiniZinc::Expression*>; $$->push_back($1); }
| array_access_expr_list_head ',' array_access_expr
{ $$=$1; if ($$ && $3) $$->push_back($3); }
array_access_expr :
expr
{ $$ = $1; }
| MZN_DOTDOT
{ $$=new SetLit(@$, IntSetVal::a(-IntVal::infinity(),IntVal::infinity())); }
| MZN_DOTDOT_LE
{ $$=Call::a(@$, ASTString("'..<'"), {}); }
| MZN_LE_DOTDOT
{ $$=Call::a(@$, ASTString("'<..'"), {}); }
| MZN_LE_DOTDOT_LE
{ $$=Call::a(@$, ASTString("'<..<'"), {}); }
expr_list : expr_list_head comma_or_none
expr_list_head :
expr
{ $$=new std::vector<MiniZinc::Expression*>; $$->push_back($1); }
| expr_list_head ',' expr
{ $$=$1; if ($$ && $3) $$->push_back($3); }
///
set_expr :
expr_atom_head
| set_expr MZN_COLONCOLON annotation_expr
{ if ($1 && $3) Expression::addAnnotation($1, $3); $$=$1; }
| set_expr MZN_UNION set_expr
{ $$=new BinOp(@$, $1, BOT_UNION, $3); }
| set_expr MZN_DIFF set_expr
{ $$=new BinOp(@$, $1, BOT_DIFF, $3); }
| set_expr MZN_SYMDIFF set_expr
{ $$=new BinOp(@$, $1, BOT_SYMDIFF, $3); }
| set_expr MZN_DOTDOT set_expr
{ if ($1==nullptr || $3==nullptr) {
$$ = nullptr;
} else if (Expression::isa<IntLit>($1) && Expression::isa<IntLit>($3)) {
$$=new SetLit(@$, IntSetVal::a(IntLit::v(Expression::cast<IntLit>($1)),IntLit::v(Expression::cast<IntLit>($3))));
} else {
$$=new BinOp(@$, $1, BOT_DOTDOT, $3);
}
}
| set_expr MZN_DOTDOT_LE set_expr
{ $$=Call::a(@$, ASTString("'..<'"), {$1, $3}); }
| set_expr MZN_LE_DOTDOT set_expr
{ $$=Call::a(@$, ASTString("'<..'"), {$1, $3}); }
| set_expr MZN_LE_DOTDOT_LE set_expr
{ $$=Call::a(@$, ASTString("'<..<'"), {$1, $3}); }
| set_expr MZN_DOTDOT
{ $$=Call::a(@$, ASTString("..o"), {$1}); }
| set_expr MZN_DOTDOT_LE
{ $$=Call::a(@$, ASTString("..<o"), {$1}); }
| set_expr MZN_LE_DOTDOT
{ $$=Call::a(@$, ASTString("<..o"), {$1}); }
| set_expr MZN_LE_DOTDOT_LE
{ $$=Call::a(@$, ASTString("<..<o"), {$1}); }
| MZN_DOTDOT set_expr
{ $$=Call::a(@$, ASTString("o.."), {$2}); }
| MZN_DOTDOT_LE set_expr
{ $$=Call::a(@$, ASTString("o..<"), {$2}); }
| MZN_LE_DOTDOT set_expr
{ $$=Call::a(@$, ASTString("o<.."), {$2}); }
| MZN_LE_DOTDOT_LE set_expr
{ $$=Call::a(@$, ASTString("o<..<"), {$2}); }
| MZN_DOTDOT_LE_QUOTED '(' set_expr ',' set_expr ')'
{ $$=Call::a(@$, ASTString("'..<'"), {$3, $5}); }
| MZN_LE_DOTDOT_QUOTED '(' set_expr ',' set_expr ')'
{ $$=Call::a(@$, ASTString("'<..'"), {$3, $5}); }
| MZN_LE_DOTDOT_LE_QUOTED '(' set_expr ',' set_expr ')'
{ $$=Call::a(@$, ASTString("'<..<'"), {$3, $5}); }
| MZN_DOTDOT_QUOTED '(' expr ',' expr ')'
{ if ($3==nullptr || $5==nullptr) {
$$ = nullptr;
} else if (Expression::isa<IntLit>($3) && Expression::isa<IntLit>($5)) {
$$=new SetLit(@$, IntSetVal::a(IntLit::v(Expression::cast<IntLit>($3)),IntLit::v(Expression::cast<IntLit>($5))));
} else {
$$=new BinOp(@$, $3, BOT_DOTDOT, $5);
}
}
| MZN_DOTDOT_LE_QUOTED '(' set_expr ')'
{ $$=Call::a(@$, ASTString("'..<'"), {$3}); }
| MZN_LE_DOTDOT_QUOTED '(' set_expr ')'
{ $$=Call::a(@$, ASTString("'<..'"), {$3}); }
| MZN_LE_DOTDOT_LE_QUOTED '(' set_expr ')'
{ $$=Call::a(@$, ASTString("'<..<'"), {$3}); }
| MZN_DOTDOT_QUOTED '(' expr ')'
{ $$=Call::a(@$, ASTString("'..'"), {$3}); }
| set_expr MZN_INTERSECT set_expr
{ $$=new BinOp(@$, $1, BOT_INTERSECT, $3); }
| set_expr MZN_PLUS set_expr
{ $$=new BinOp(@$, $1, BOT_PLUS, $3); }
| set_expr MZN_MINUS set_expr
{ $$=new BinOp(@$, $1, BOT_MINUS, $3); }
| set_expr MZN_MULT set_expr
{ $$=new BinOp(@$, $1, BOT_MULT, $3); }
| set_expr MZN_DIV set_expr
{ $$=new BinOp(@$, $1, BOT_DIV, $3); }
| set_expr MZN_IDIV set_expr
{ $$=new BinOp(@$, $1, BOT_IDIV, $3); }
| set_expr MZN_MOD set_expr
{ $$=new BinOp(@$, $1, BOT_MOD, $3); }
| set_expr MZN_POW set_expr
{ $$=new BinOp(@$, $1, BOT_POW, $3); }
| set_expr MZN_WEAK_PLUS set_expr
{ vector<Expression*> args;
args.push_back($1); args.push_back($3);
$$=Call::a(@$, ASTString("~+"), args);
}
| set_expr MZN_WEAK_MINUS set_expr
{ vector<Expression*> args;
args.push_back($1); args.push_back($3);
$$=Call::a(@$, ASTString("~-"), args);
}
| set_expr MZN_WEAK_MULT set_expr
{ vector<Expression*> args;
args.push_back($1); args.push_back($3);
$$=Call::a(@$, ASTString("~*"), args);
}
| set_expr MZN_WEAK_DIV set_expr
{ vector<Expression*> args;
args.push_back($1); args.push_back($3);
$$=Call::a(@$, ASTString("~/"), args);
}
| set_expr MZN_WEAK_IDIV set_expr
{ vector<Expression*> args;
args.push_back($1); args.push_back($3);
$$=Call::a(@$, ASTString("~div"), args);
}
| set_expr MZN_WEAK_EQ set_expr
{ vector<Expression*> args;
args.push_back($1); args.push_back($3);
$$=Call::a(@$, ASTString("~="), args);
}
| set_expr MZN_WEAK_NQ set_expr
{ vector<Expression*> args;
args.push_back($1); args.push_back($3);
$$=Call::a(@$, ASTString("~!="), args);
}
| set_expr MZN_DEFAULT set_expr
{
vector<Expression*> args;
args.push_back($1); args.push_back($3);
$$=Call::a(@$, ASTString("default"), args);
}
| set_expr MZN_QUOTED_IDENTIFIER set_expr
{ vector<Expression*> args;
args.push_back($1); args.push_back($3);
$$=Call::a(@$, $2, args);
free($2);
}
| MZN_PLUS set_expr %prec MZN_NOT
{ $$=new UnOp(@$, UOT_PLUS, $2); }
| MZN_MINUS set_expr %prec MZN_NOT
{ if ($2 && Expression::isa<IntLit>($2)) {
$$ = IntLit::a(-IntLit::v(Expression::cast<IntLit>($2)));
} else if ($2 && Expression::isa<FloatLit>($2)) {
$$ = FloatLit::a(-FloatLit::v(Expression::cast<FloatLit>($2)));
} else {
$$=new UnOp(@$, UOT_MINUS, $2);
}
}
| MZN_MINUS MZN_MAX_NEGATIVE_INTEGER_LITERAL %prec MZN_NOT
{ $$ = IntLit::a((-9223372036854775807ll)-1); }
///
expr :
expr_atom_head
| expr MZN_COLONCOLON annotation_expr
{ if ($1 && $3) Expression::addAnnotation($1, $3); $$=$1; }
| expr MZN_EQUIV expr
{ $$=new BinOp(@$, $1, BOT_EQUIV, $3); }
| expr MZN_IMPL expr
{ $$=new BinOp(@$, $1, BOT_IMPL, $3); }
| expr MZN_RIMPL expr
{ $$=new BinOp(@$, $1, BOT_RIMPL, $3); }
| expr MZN_OR expr
{ $$=new BinOp(@$, $1, BOT_OR, $3); }
| expr MZN_XOR expr
{ $$=new BinOp(@$, $1, BOT_XOR, $3); }
| expr MZN_AND expr
{ $$=new BinOp(@$, $1, BOT_AND, $3); }
| expr MZN_LE expr
{ $$=new BinOp(@$, $1, BOT_LE, $3); }
| expr MZN_GR expr
{ $$=new BinOp(@$, $1, BOT_GR, $3); }
| expr MZN_LQ expr
{ $$=new BinOp(@$, $1, BOT_LQ, $3); }
| expr MZN_GQ expr
{ $$=new BinOp(@$, $1, BOT_GQ, $3); }
| expr MZN_EQ expr
{ $$=new BinOp(@$, $1, BOT_EQ, $3); }
| expr MZN_NQ expr
{ $$=new BinOp(@$, $1, BOT_NQ, $3); }
| expr MZN_IN expr
{ $$=new BinOp(@$, $1, BOT_IN, $3); }
| expr MZN_SUBSET expr
{ $$=new BinOp(@$, $1, BOT_SUBSET, $3); }
| expr MZN_SUPERSET expr
{ $$=new BinOp(@$, $1, BOT_SUPERSET, $3); }
| expr MZN_UNION expr
{ $$=new BinOp(@$, $1, BOT_UNION, $3); }
| expr MZN_DIFF expr
{ $$=new BinOp(@$, $1, BOT_DIFF, $3); }
| expr MZN_SYMDIFF expr
{ $$=new BinOp(@$, $1, BOT_SYMDIFF, $3); }
| expr MZN_DOTDOT expr
{ if ($1==nullptr || $3==nullptr) {
$$ = nullptr;
} else if (Expression::isa<IntLit>($1) && Expression::isa<IntLit>($3)) {
$$=new SetLit(@$, IntSetVal::a(IntLit::v(Expression::cast<IntLit>($1)),IntLit::v(Expression::cast<IntLit>($3))));
} else {
$$=new BinOp(@$, $1, BOT_DOTDOT, $3);
}
}
| expr MZN_DOTDOT_LE expr
{ $$=Call::a(@$, ASTString("'..<'"), {$1, $3}); }
| expr MZN_LE_DOTDOT expr
{ $$=Call::a(@$, ASTString("'<..'"), {$1, $3}); }
| expr MZN_LE_DOTDOT_LE expr
{ $$=Call::a(@$, ASTString("'<..<'"), {$1, $3}); }
| expr MZN_DOTDOT
{ $$=Call::a(@$, ASTString("..o"), {$1}); }
| expr MZN_DOTDOT_LE
{ $$=Call::a(@$, ASTString("..<o"), {$1}); }
| expr MZN_LE_DOTDOT
{ $$=Call::a(@$, ASTString("<..o"), {$1}); }
| expr MZN_LE_DOTDOT_LE
{ $$=Call::a(@$, ASTString("<..<o"), {$1}); }
| MZN_DOTDOT expr
{ $$=Call::a(@$, ASTString("o.."), {$2}); }
| MZN_DOTDOT_LE expr
{ $$=Call::a(@$, ASTString("o..<"), {$2}); }
| MZN_LE_DOTDOT expr
{ $$=Call::a(@$, ASTString("o<.."), {$2}); }
| MZN_LE_DOTDOT_LE expr
{ $$=Call::a(@$, ASTString("o<..<"), {$2}); }
| MZN_DOTDOT_LE_QUOTED '(' expr ',' expr ')'
{ $$=Call::a(@$, ASTString("'..<'"), {$3, $5}); }
| MZN_LE_DOTDOT_QUOTED '(' expr ',' expr ')'
{ $$=Call::a(@$, ASTString("'<..'"), {$3, $5}); }
| MZN_LE_DOTDOT_LE_QUOTED '(' expr ',' expr ')'
{ $$=Call::a(@$, ASTString("'<..<'"), {$3, $5}); }
| MZN_DOTDOT_QUOTED '(' expr ',' expr ')'
{ if ($3==nullptr || $5==nullptr) {
$$ = nullptr;
} else if (Expression::isa<IntLit>($3) && Expression::isa<IntLit>($5)) {
$$=new SetLit(@$, IntSetVal::a(IntLit::v(Expression::cast<IntLit>($3)),IntLit::v(Expression::cast<IntLit>($5))));
} else {
$$=new BinOp(@$, $3, BOT_DOTDOT, $5);
}
}
| MZN_DOTDOT_LE_QUOTED '(' expr ')'
{ $$=Call::a(@$, ASTString("'..<'"), {$3}); }
| MZN_LE_DOTDOT_QUOTED '(' expr ')'
{ $$=Call::a(@$, ASTString("'<..'"), {$3}); }
| MZN_LE_DOTDOT_LE_QUOTED '(' expr ')'
{ $$=Call::a(@$, ASTString("'<..<'"), {$3}); }
| MZN_DOTDOT_QUOTED '(' expr ')'
{ $$=Call::a(@$, ASTString("'..'"), {$3}); }
| expr MZN_INTERSECT expr
{ $$=new BinOp(@$, $1, BOT_INTERSECT, $3); }
| expr MZN_PLUSPLUS expr
{ $$=new BinOp(@$, $1, BOT_PLUSPLUS, $3); }
| expr MZN_PLUS expr
{ $$=new BinOp(@$, $1, BOT_PLUS, $3); }
| expr MZN_MINUS expr
{ $$=new BinOp(@$, $1, BOT_MINUS, $3); }
| expr MZN_MULT expr
{ $$=new BinOp(@$, $1, BOT_MULT, $3); }
| expr MZN_DIV expr
{ $$=new BinOp(@$, $1, BOT_DIV, $3); }
| expr MZN_IDIV expr
{ $$=new BinOp(@$, $1, BOT_IDIV, $3); }
| expr MZN_MOD expr
{ $$=new BinOp(@$, $1, BOT_MOD, $3); }
| expr MZN_POW expr
{ $$=new BinOp(@$, $1, BOT_POW, $3); }
| expr MZN_WEAK_PLUS expr
{ vector<Expression*> args;
args.push_back($1); args.push_back($3);
$$=Call::a(@$, ASTString("~+"), args);
}
| expr MZN_WEAK_MINUS expr
{ vector<Expression*> args;
args.push_back($1); args.push_back($3);
$$=Call::a(@$, ASTString("~-"), args);
}
| expr MZN_WEAK_MULT expr
{ vector<Expression*> args;
args.push_back($1); args.push_back($3);
$$=Call::a(@$, ASTString("~*"), args);
}
| expr MZN_WEAK_DIV expr
{ vector<Expression*> args;
args.push_back($1); args.push_back($3);
$$=Call::a(@$, ASTString("~/"), args);
}
| expr MZN_WEAK_IDIV expr
{ vector<Expression*> args;
args.push_back($1); args.push_back($3);
$$=Call::a(@$, ASTString("~div"), args);
}
| expr MZN_WEAK_EQ expr
{ vector<Expression*> args;
args.push_back($1); args.push_back($3);
$$=Call::a(@$, ASTString("~="), args);
}
| expr MZN_WEAK_NQ expr
{ vector<Expression*> args;
args.push_back($1); args.push_back($3);
$$=Call::a(@$, ASTString("~!="), args);
}
| expr MZN_DEFAULT expr
{ vector<Expression*> args;
args.push_back($1); args.push_back($3);
$$=Call::a(@$, ASTString("default"), args);
}
| expr MZN_QUOTED_IDENTIFIER expr
{ vector<Expression*> args;
args.push_back($1); args.push_back($3);
$$=Call::a(@$, $2, args);
free($2);
}
| MZN_NOT expr %prec MZN_NOT
{ $$=new UnOp(@$, UOT_NOT, $2); }
| MZN_PLUS expr %prec MZN_MULT
{ if (($2 && Expression::isa<IntLit>($2)) || ($2 && Expression::isa<FloatLit>($2))) {
$$ = $2;
} else {
$$=new UnOp(@$, UOT_PLUS, $2);
}
}
| MZN_MINUS expr %prec MZN_MULT
{ if ($2 && Expression::isa<IntLit>($2)) {
$$ = IntLit::a(-IntLit::v(Expression::cast<IntLit>($2)));
} else if ($2 && Expression::isa<FloatLit>($2)) {
$$ = FloatLit::a(-FloatLit::v(Expression::cast<FloatLit>($2)));
} else {
$$=new UnOp(@$, UOT_MINUS, $2);
}
}
| MZN_MINUS MZN_MAX_NEGATIVE_INTEGER_LITERAL %prec MZN_NOT
{ $$ = IntLit::a((-9223372036854775807ll)-1); }
expr_atom_head :
expr_atom_head_nonstring
{ $$=$1; }
| string_expr
{ $$=$1; }
expr_atom_head_nonstring :
'(' expr ')'
{ $$=$2; }
| '(' expr ')' access_tail
{ if ($4) $$=createAccess(@$, $2, *$4); delete $4; }
| '(' expr ')' MZN_POW_MINUS1
{ $$=new BinOp(@$, $2, BOT_POW, IntLit::a(-1)); }
| '(' expr ')' access_tail MZN_POW_MINUS1
{ if ($4) $$=new BinOp(@$,createAccess(@$, $2, *$4), BOT_POW, IntLit::a(-1)); delete $4; }
| MZN_IDENTIFIER
{ $$=new Id(@$, $1, nullptr); free($1); }
| MZN_IDENTIFIER access_tail
{ if ($2) $$=createAccess(@$, new Id(@1,$1,nullptr), *$2);
free($1); delete $2; }
| MZN_IDENTIFIER MZN_POW_MINUS1
{ $$=new BinOp(@$,new Id(@$, $1, nullptr), BOT_POW, IntLit::a(-1)); free($1); }
| MZN_IDENTIFIER access_tail MZN_POW_MINUS1
{ if ($2) $$=new BinOp(@$,createAccess(@$, new Id(@1,$1,nullptr), *$2), BOT_POW, IntLit::a(-1));
free($1); delete $2; }
| MZN_UNDERSCORE
{ $$=new AnonVar(@$); }
| MZN_UNDERSCORE access_tail
{ if ($2) $$=createAccess(@$, new AnonVar(@$), *$2);
delete $2; }
| MZN_UNDERSCORE MZN_POW_MINUS1
{ $$=new BinOp(@$,new AnonVar(@$), BOT_POW, IntLit::a(-1)); }
| MZN_UNDERSCORE access_tail MZN_POW_MINUS1
{ if ($2) $$=new BinOp(@$,createAccess(@$, new AnonVar(@$), *$2), BOT_POW, IntLit::a(-1));
delete $2; }
| MZN_UNDERSCORE '(' expr ')'
{ $$ = Call::a(@$, Constants::constants().ids.anon_enum_set, {$3}); }
| MZN_BOOL_LITERAL
{ $$=Constants::constants().boollit(($1!=0)); }
| MZN_BOOL_LITERAL MZN_POW_MINUS1
{ $$=new BinOp(@$,Constants::constants().boollit(($1!=0)), BOT_POW, IntLit::a(-1)); }
| MZN_INTEGER_LITERAL
{ $$=IntLit::a($1); }
| MZN_INTEGER_LITERAL MZN_POW_MINUS1
{ $$=new BinOp(@$,IntLit::a($1), BOT_POW, IntLit::a(-1)); }
| MZN_INFINITY
{ $$=IntLit::a(IntVal::infinity()); }
| MZN_INFINITY MZN_POW_MINUS1
{ $$=new BinOp(@$,IntLit::a(IntVal::infinity()), BOT_POW, IntLit::a(-1)); }
| MZN_FLOAT_LITERAL
{ $$=FloatLit::a($1); }
| MZN_FLOAT_LITERAL MZN_POW_MINUS1
{ $$=new BinOp(@$,FloatLit::a($1), BOT_POW, IntLit::a(-1)); }
| MZN_ABSENT
{ $$=Constants::constants().absent; }
| MZN_ABSENT MZN_POW_MINUS1
{ $$=Constants::constants().absent; }
| set_literal
| set_literal access_tail
{ if ($2) $$=createAccess(@$, $1, *$2);
delete $2; }
| set_literal MZN_POW_MINUS1
{ $$ = new BinOp(@$,$1, BOT_POW, IntLit::a(-1)); }
| set_literal access_tail MZN_POW_MINUS1
{ if ($2) $$=new BinOp(@$,createAccess(@$, $1, *$2), BOT_POW, IntLit::a(-1));
delete $2; }
| set_comp
| set_comp access_tail
{ if ($2) $$=createAccess(@$, $1, *$2);
delete $2; }
| set_comp MZN_POW_MINUS1
{ $$ = new BinOp(@$,$1, BOT_POW, IntLit::a(-1)); }
| set_comp access_tail MZN_POW_MINUS1
{ if ($2) $$=new BinOp(@$,createAccess(@$, $1, *$2), BOT_POW, IntLit::a(-1));
delete $2; }
| simple_array_literal
| simple_array_literal access_tail
{ if ($2) $$=createAccess(@$, $1, *$2);
delete $2; }
| simple_array_literal MZN_POW_MINUS1
{ $$ = new BinOp(@$,$1, BOT_POW, IntLit::a(-1)); }
| simple_array_literal access_tail MZN_POW_MINUS1
{ if ($2) $$=new BinOp(@$,createAccess(@$, $1, *$2), BOT_POW, IntLit::a(-1));
delete $2; }
| simple_array_literal_2d
| simple_array_literal_2d access_tail
{ if ($2) $$=createAccess(@$, $1, *$2);
delete $2; }
| simple_array_literal_2d MZN_POW_MINUS1
{ $$ = new BinOp(@$,$1, BOT_POW, IntLit::a(-1)); }
| simple_array_literal_2d access_tail MZN_POW_MINUS1
{ if ($2) $$=new BinOp(@$,createAccess(@$, $1, *$2), BOT_POW, IntLit::a(-1));
delete $2; }
| simple_array_comp
| simple_array_comp access_tail
{ if ($2) $$=createAccess(@$, $1, *$2);
delete $2; }
| simple_array_comp MZN_POW_MINUS1
{ $$ = new BinOp(@$,$1, BOT_POW, IntLit::a(-1)); }
| simple_array_comp access_tail MZN_POW_MINUS1
{ if ($2) $$=new BinOp(@$,createAccess(@$, $1, *$2), BOT_POW, IntLit::a(-1));
delete $2; }
| if_then_else_expr
| if_then_else_expr access_tail
{ if ($2) $$=createAccess(@$, $1, *$2);
delete $2; }
| if_then_else_expr MZN_POW_MINUS1
{ $$ = new BinOp(@$,$1, BOT_POW, IntLit::a(-1)); }
| if_then_else_expr access_tail MZN_POW_MINUS1
{ if ($2) $$=new BinOp(@$,createAccess(@$, $1, *$2), BOT_POW, IntLit::a(-1));
delete $2; }
| let_expr
| call_expr
| call_expr access_tail
{ if ($2) $$=createAccess(@$, $1, *$2);
delete $2; }
| call_expr MZN_POW_MINUS1
| call_expr access_tail MZN_POW_MINUS1
{ if ($2) $$=createAccess(@$, $1, *$2);
delete $2; }
| tuple_literal
| tuple_literal access_tail
{ if ($2) $$=createAccess(@$, $1, *$2);
delete $2; }
| tuple_literal MZN_POW_MINUS1
{ $$ = new BinOp(@$,$1, BOT_POW, IntLit::a(-1)); }
| tuple_literal access_tail MZN_POW_MINUS1
{ if ($2) $$=new BinOp(@$,createAccess(@$, $1, *$2), BOT_POW, IntLit::a(-1));
delete $2; }
| record_literal
| record_literal access_tail
{ if ($2) $$=createAccess(@$, $1, *$2);
delete $2; }
| record_literal MZN_POW_MINUS1
{ $$ = new BinOp(@$,$1, BOT_POW, IntLit::a(-1)); }
| record_literal access_tail MZN_POW_MINUS1
{ if ($2) $$=new BinOp(@$,createAccess(@$, $1, *$2), BOT_POW, IntLit::a(-1));
delete $2; }
string_expr:
MZN_STRING_LITERAL
{ $$=new StringLit(@$, $1); free($1); }
| MZN_STRING_QUOTE_START string_quote_rest
{ $$=new BinOp(@$, new StringLit(@$, $1), BOT_PLUSPLUS, $2);
free($1);
}
string_quote_rest:
expr MZN_STRING_QUOTE_END
{ if ($1) {
$$=new BinOp(@$, Call::a(@$, ASTString("format"), {$1}), BOT_PLUSPLUS, new StringLit(@$,$2));
}
free($2);
}
| expr MZN_STRING_QUOTE_MID string_quote_rest
{ if ($1) {
$$=new BinOp(@$, Call::a(@$, ASTString("format"), {$1}), BOT_PLUSPLUS,
new BinOp(@$, new StringLit(@$,$2), BOT_PLUSPLUS, $3));
}
free($2);
}
access_tail :
MZN_LEFT_BRACKET array_access_expr_list MZN_RIGHT_BRACKET
{
$$=new std::vector<Expression*>();
if ($2) {
auto* al = new ArrayAccess(@$, nullptr, *$2);
$$->push_back(al);
delete $2;
}
}
| MZN_FIELD_TAIL
{
$$=new std::vector<Expression*>();
std::string tail($1); free($1);
parseFieldTail(@$, *$$, tail);
}
| access_tail MZN_LEFT_BRACKET array_access_expr_list MZN_RIGHT_BRACKET
{
$$=$1;
if ($$ && $3) {
auto* al = new ArrayAccess(@$, nullptr, *$3);
$$->push_back(al);
delete $3;
}
}
| access_tail MZN_FIELD_TAIL
{
$$=$1;
std::string tail($2); free($2);
parseFieldTail(@$, *$$, tail);
}
set_literal :
'{' '}'
{ $$ = new SetLit(@$, std::vector<Expression*>()); }
| '{' expr_list '}'
{ if ($2) $$ = new SetLit(@$, *$2);
delete $2; }
tuple_literal :
'(' expr ',' ')'
{
std::vector<Expression*> list({ $2 });
$$=ArrayLit::constructTuple(@$, list);
}
| '(' expr ',' expr_list ')'
{
auto* list = $4;
if (list == nullptr) {
list = new std::vector<Expression*>();
}
if ($2) {
list->insert(list->begin(), $2);
}
$$=ArrayLit::constructTuple(@$, *list);
delete list;
}
record_literal :
'(' record_field_list_head comma_or_none ')'
{
$$ = ArrayLit::constructTuple(@$, *$2);
Expression::type($$, Type::record());
delete($2);
}
record_field_list_head :
record_field
{ $$ = new vector<Expression*>(1); (*$$)[0] = $1; }
| record_field_list_head ',' record_field
{ $$=$1; $$->push_back($3); }
record_field:
MZN_IDENTIFIER ':' expr
{
$$ = new VarDecl(@$, new TypeInst(@$, Type()), $1, $3);
free($1);
}
set_comp :
'{' expr '|' comp_tail '}'
{ if ($4) $$ = new Comprehension(@$, $2, *$4, true);
delete $4;
}
comp_tail :
generator_list
{ if ($1) $$=new Generators; $$->g = *$1; delete $1; }
generator_list : generator_list_head comma_or_none
generator_list_head :
generator
{ $$=new std::vector<Generator>; if ($1) $$->push_back(*$1); delete $1; }
| generator_eq
{ $$=new std::vector<Generator>; if ($1) $$->push_back(*$1); delete $1; }
| generator_eq MZN_WHERE expr
{ $$=new std::vector<Generator>;
if ($1) $$->push_back(*$1);
if ($1 && $3) $$->push_back(Generator(static_cast<int>($$->size()),$3));
delete $1;
}
| generator_list_head ',' generator
{ $$=$1; if ($$ && $3) $$->push_back(*$3); delete $3; }
| generator_list_head ',' generator_eq
{ $$=$1; if ($$ && $3) $$->push_back(*$3); delete $3; }
| generator_list_head ',' generator_eq MZN_WHERE expr
{ $$=$1;
if ($$ && $3) $$->push_back(*$3);
if ($$ && $3 && $5) $$->push_back(Generator(static_cast<int>($$->size()),$5));
delete $3;
}
generator :
id_list MZN_IN expr
{ if ($1 && $3) $$=new Generator(*$1,$3,nullptr); else $$=nullptr; delete $1; }
| id_list MZN_IN expr MZN_WHERE expr
{ if ($1 && $3) $$=new Generator(*$1,$3,$5); else $$=nullptr; delete $1; }
generator_eq :
MZN_IDENTIFIER MZN_EQ expr
{ if ($3) $$=new Generator({$1},nullptr,$3); else $$=nullptr; free($1); }
id_list : id_list_head comma_or_none
id_list_head :
MZN_IDENTIFIER
{ $$=new std::vector<std::string>; $$->push_back($1); free($1); }
| MZN_UNDERSCORE
{ $$=new std::vector<std::string>; $$->push_back(""); }
| id_list_head ',' MZN_IDENTIFIER
{ $$=$1; if ($$ && $3) $$->push_back($3); free($3); }
| id_list_head ',' MZN_UNDERSCORE
{ $$=$1; if ($$) $$->push_back(""); }
simple_array_literal :
MZN_LEFT_BRACKET MZN_RIGHT_BRACKET
{ $$=new ArrayLit(@$, std::vector<MiniZinc::Expression*>()); }
| MZN_LEFT_BRACKET comp_expr_list MZN_RIGHT_BRACKET
{ if ($2) {
if ($2->first.empty()) {
$$=new ArrayLit(@$, $2->second);
} else {
const auto* tuple = Expression::dynamicCast<ArrayLit>($2->first[0]);
if (tuple) {
std::vector<std::vector<Expression*>> dims(tuple->size());
for (const auto* t : $2->first) {
if ( (tuple = Expression::dynamicCast<ArrayLit>(t)) != nullptr ) {
if (tuple->size() == dims.size()) {
for (unsigned int i = 0; i < dims.size(); i++) {
dims[i].push_back((*tuple)[i]);
}
} else {
yyerror(&@2, parm, "syntax error, non-uniform indexed array literal");
}
} else {
yyerror(&@2, parm, "syntax error, non-uniform indexed array literal");
}
}
std::vector<Expression*> arrayNdArgs(dims.size());
for (unsigned int i = 0; i < dims.size(); i++) {
arrayNdArgs[i] = new ArrayLit(@$, dims[i]);
}
arrayNdArgs.push_back(new ArrayLit(@$, $2->second));
if ($2->first.size() != $2->second.size()) {
yyerror(&@2, parm, "syntax error, non-uniform indexed array literal");
$$=nullptr;
} else {
$$=Call::a(@$, "arrayNd", arrayNdArgs);
}
} else {
for (const auto* t : $2->first) {
if (Expression::isa<ArrayLit>(t)) {
yyerror(&@2, parm, "syntax error, non-uniform indexed array literal");
}
}
$$=Call::a(@$, "arrayNd", {new ArrayLit(@$, $2->first), new ArrayLit(@$, $2->second)});
}
}
delete $2;
}
}
simple_array_literal_2d :
MZN_LEFT_2D_BRACKET MZN_RIGHT_2D_BRACKET
{ $$=new ArrayLit(@$, std::vector<std::vector<Expression*> >()); }
| MZN_LEFT_2D_BRACKET simple_array_literal_2d_indexed_list MZN_RIGHT_2D_BRACKET
{ if ($2) {
std::vector<std::vector<Expression*>> v($2->size());
std::vector<Expression*> columnHeader;
std::vector<Expression*> rowHeader;
bool hadHeaderRow = false;
for (unsigned int i = 0; i < $2->size(); i++) {
if (i == 0 && (*$2)[i].second.empty() && !(*$2)[i].first.empty()) {
hadHeaderRow = true;
columnHeader = (*$2)[i].first;
v.resize(v.size()-1);
continue;
}
if (i > 0 && (*$2)[i].second.size() != (*$2)[i-1].second.size()) {
if (i == 1 && hadHeaderRow) {
if ((*$2)[i].second.size() != (*$2)[i-1].first.size()) {
yyerror(&@2, parm, "syntax error, sub-array of 2d array literal has different length from index row");
}
} else {
yyerror(&@2, parm, "syntax error, all sub-arrays of 2d array literal must have the same length");
}
}
if (i > static_cast<unsigned int>(hadHeaderRow) && (*$2)[i].first.size() != (*$2)[i-1].first.size()) {
yyerror(&@2, parm, "syntax error, mixing indexed and non-indexed sub-arrays in 2d array literal");
}
if (i >= static_cast<unsigned int>(hadHeaderRow) && !(*$2)[i].first.empty()) {
rowHeader.push_back((*$2)[i].first[0]);
}
for (unsigned int j = 0; j < (*$2)[i].second.size(); j++) {
v[i - hadHeaderRow].push_back((*$2)[i].second[j]);
}
}
if (columnHeader.empty() && rowHeader.empty()) {
$$=new ArrayLit(@$, v);
} else {
std::vector<Expression*> vv;
for (auto& row : v) {
for (auto* e : row) {
vv.push_back(e);
}
}
if (rowHeader.empty()) {
auto nRows = vv.size() / columnHeader.size();
rowHeader.resize(nRows);
for (unsigned int i = 0; i < nRows; i++) {
rowHeader[i] = IntLit::a(i+1);
}
} else if (columnHeader.empty()) {
auto nCols = vv.size() / rowHeader.size();
columnHeader.resize(nCols);
for (unsigned int i = 0; i < nCols; i++) {
columnHeader[i] = IntLit::a(i+1);
}
}
$$=Call::a(@$, "array2d", {new ArrayLit(@$, rowHeader), new ArrayLit(@$, columnHeader), new ArrayLit(@$, vv)});
}
delete $2;
} else {
$$ = nullptr;
}
}
| MZN_LEFT_2D_BRACKET simple_array_literal_3d_list MZN_RIGHT_2D_BRACKET
{
if ($2) {
std::vector<std::pair<int,int> > dims(3);
dims[0] = std::pair<int,int>(1,static_cast<int>($2->size()));
if ($2->size()==0) {
dims[1] = std::pair<int,int>(1,0);
dims[2] = std::pair<int,int>(1,0);
} else {
dims[1] = std::pair<int,int>(1,static_cast<int>((*$2)[0].size()));
if ((*$2)[0].size()==0) {
dims[2] = std::pair<int,int>(1,0);
} else {
dims[2] = std::pair<int,int>(1,static_cast<int>((*$2)[0][0].size()));
}
}
std::vector<Expression*> a;
for (int i=0; i<dims[0].second; i++) {
if ((*$2)[i].size() != dims[1].second) {
yyerror(&@2, parm, "syntax error, all sub-arrays of 3d array literal must have the same length");
} else {
for (int j=0; j<dims[1].second; j++) {
if ((*$2)[i][j].size() != dims[2].second) {
yyerror(&@2, parm, "syntax error, all sub-arrays of 3d array literal must have the same length");
} else {
for (int k=0; k<dims[2].second; k++) {
a.push_back((*$2)[i][j][k]);
}
}
}
}
}
$$ = new ArrayLit(@$,a,dims);
delete $2;
} else {
$$ = nullptr;
}
}
simple_array_literal_3d_list :
'|' '|'
{ $$=new std::vector<std::vector<std::vector<MiniZinc::Expression*> > >;
}
| '|' simple_array_literal_2d_list '|'
{ $$=new std::vector<std::vector<std::vector<MiniZinc::Expression*> > >;
if ($2) $$->push_back(*$2);
delete $2;
}
| simple_array_literal_3d_list ',' '|' simple_array_literal_2d_list '|'
{ $$=$1;
if ($$ && $4) $$->push_back(*$4);
delete $4;
}
simple_array_literal_2d_list :
expr_list
{ $$=new std::vector<std::vector<MiniZinc::Expression*> >;
if ($1) $$->push_back(*$1);
delete $1;
}
| simple_array_literal_2d_list '|' expr_list
{ $$=$1; if ($$ && $3) $$->push_back(*$3); delete $3; }
simple_array_literal_2d_indexed_list: simple_array_literal_2d_indexed_list_head pipe_or_none
simple_array_literal_2d_indexed_list_head :
simple_array_literal_2d_indexed_list_row
{ $$=new std::vector<std::pair<std::vector<MiniZinc::Expression*>,std::vector<MiniZinc::Expression*>>>();
if ($1) {
if ($1->first.size() > 1 || ($1->first.size() == 1 && $1->second.empty())) {
yyerror(&@$,parm,"invalid array literal, mixing indexes and values");
}
$$->push_back(*$1);
delete $1;
}
}
| simple_array_literal_2d_indexed_list_row_head ':'
{ $$=new std::vector<std::pair<std::vector<MiniZinc::Expression*>,std::vector<MiniZinc::Expression*>>>();
if ($1) {
if ($1->second.size() != 1) {
yyerror(&@$,parm,"invalid array literal, mixing indexes and values");
}
$1->first.push_back($1->second.back());
$1->second.pop_back();
$$->push_back(*$1);
delete $1;
}
}
| simple_array_literal_2d_indexed_list_head '|' simple_array_literal_2d_indexed_list_row
{ $$=$1;
if ($$ && $3) {
if ($3->first.size() > 1 || ($3->first.size() == 1 && $3->second.empty())) {
yyerror(&@3,parm,"invalid array literal, mixing indexes and values");
}
$$->push_back(*$3);
delete $3;
}
}
simple_array_literal_2d_indexed_list_row: simple_array_literal_2d_indexed_list_row_head comma_or_none
simple_array_literal_2d_indexed_list_row_head :
expr
{ $$=new std::pair<std::vector<MiniZinc::Expression*>,std::vector<MiniZinc::Expression*>>();
$$->second.push_back($1);
}
| simple_array_literal_2d_indexed_list_row_head ':' expr
{ $$=$1;
if ($$) {
if ($$->second.size() != 1) {
yyerror(&@$,parm,"invalid array literal, mixing indexes and values");
}
$$->first.push_back($$->second.back());
$$->second.pop_back();
$$->second.push_back($3);
}
}
| simple_array_literal_2d_indexed_list_row_head ',' expr
{ $$=$1;
if ($$) {
if ($$->second.empty()) {
yyerror(&@$,parm,"invalid array literal, mixing indexes and values");
}
$$->second.push_back($3);
}
}
simple_array_comp :
MZN_LEFT_BRACKET expr ':' expr '|' comp_tail MZN_RIGHT_BRACKET
{ if ($2 && $6) {
std::vector<Expression*> tv;
if (auto* al = Expression::dynamicCast<ArrayLit>($2)) {
for (unsigned int i=0; i<al->size(); i++) {
tv.push_back((*al)[i]);
}
} else {
tv.push_back($2);
}
tv.push_back($4);
auto* t = ArrayLit::constructTuple(@$,tv);
Type ty = Type::tuple();
ty.typeId(Type::COMP_INDEX);
t->type(ty);
$$=new Comprehension(@$, t, *$6, false);
delete $6;
}
}
| MZN_LEFT_BRACKET expr '|' comp_tail MZN_RIGHT_BRACKET
{ if ($4) $$=new Comprehension(@$, $2, *$4, false);
delete $4;
}
/***********************************************/
comp_expr_list : comp_expr_list_head comma_or_none
/// TODO: push index into first vector, make sure either all elements have indices or none
comp_expr_list_head :
expr
{ $$=new std::pair<std::vector<MiniZinc::Expression*>,std::vector<MiniZinc::Expression*>>;
$$->second.push_back($1); }
| expr ':' expr
{ $$=new std::pair<std::vector<MiniZinc::Expression*>,std::vector<MiniZinc::Expression*>>;
$$->first.push_back($1);
$$->second.push_back($3); }
| comp_expr_list_head ',' expr
{ $$=$1;
if ($$ && $3) {
if ($$->first.size() > 1) {
yyerror(&@$,parm,"invalid array literal, mixing indexed and non-indexed values");
$$ = nullptr;
} else {
$$->second.push_back($3);
}
}
}
| comp_expr_list_head ',' expr ':' expr
{ $$=$1;
if ($$ && $3) {
if ($$->first.size() != $$->second.size()) {
yyerror(&@$,parm,"invalid array literal, mixing indexed and non-indexed values");
$$ = nullptr;
} else if (Expression::isa<ArrayLit>($3) && Expression::cast<ArrayLit>($3)->isTuple() && Expression::cast<ArrayLit>($3)->size() == 1) {
$$->first.push_back((*Expression::cast<ArrayLit>($3))[0]);
$$->second.push_back($5);
delete $3;
} else {
$$->first.push_back($3);
$$->second.push_back($5);
}
}
}
/***********************************************/
if_then_else_expr :
MZN_IF expr MZN_THEN expr elseif_list MZN_ELSE expr MZN_ENDIF
{
std::vector<Expression*> iexps;
iexps.push_back($2);
iexps.push_back($4);
if ($5) {
for (unsigned int i=0; i<$5->size(); i+=2) {
iexps.push_back((*$5)[i]);
iexps.push_back((*$5)[i+1]);
}
}
$$=new ITE(@$, iexps,$7);
delete $5;
}
| MZN_IF expr MZN_THEN expr elseif_list MZN_ENDIF
{
std::vector<Expression*> iexps;
iexps.push_back($2);
iexps.push_back($4);
if ($5) {
for (unsigned int i=0; i<$5->size(); i+=2) {
iexps.push_back((*$5)[i]);
iexps.push_back((*$5)[i+1]);
}
}
$$=new ITE(@$, iexps, nullptr);
delete $5;
}
elseif_list :
{ $$=new std::vector<MiniZinc::Expression*>; }
| elseif_list MZN_ELSEIF expr MZN_THEN expr
{ $$=$1; if ($$ && $3 && $5) { $$->push_back($3); $$->push_back($5); } }
quoted_op :
MZN_EQUIV_QUOTED
{ $$=BOT_EQUIV; }
| MZN_IMPL_QUOTED
{ $$=BOT_IMPL; }
| MZN_RIMPL_QUOTED
{ $$=BOT_RIMPL; }
| MZN_OR_QUOTED
{ $$=BOT_OR; }
| MZN_XOR_QUOTED
{ $$=BOT_XOR; }
| MZN_AND_QUOTED
{ $$=BOT_AND; }
| MZN_LE_QUOTED
{ $$=BOT_LE; }
| MZN_GR_QUOTED
{ $$=BOT_GR; }
| MZN_LQ_QUOTED
{ $$=BOT_LQ; }
| MZN_GQ_QUOTED
{ $$=BOT_GQ; }
| MZN_EQ_QUOTED
{ $$=BOT_EQ; }
| MZN_NQ_QUOTED
{ $$=BOT_NQ; }
| MZN_IN_QUOTED
{ $$=BOT_IN; }
| MZN_SUBSET_QUOTED
{ $$=BOT_SUBSET; }
| MZN_SUPERSET_QUOTED
{ $$=BOT_SUPERSET; }
| MZN_UNION_QUOTED
{ $$=BOT_UNION; }
| MZN_DIFF_QUOTED
{ $$=BOT_DIFF; }
| MZN_SYMDIFF_QUOTED
{ $$=BOT_SYMDIFF; }
| MZN_PLUS_QUOTED
{ $$=BOT_PLUS; }
| MZN_MINUS_QUOTED
{ $$=BOT_MINUS; }
| MZN_MULT_QUOTED
{ $$=BOT_MULT; }
| MZN_POW_QUOTED
{ $$=BOT_POW; }
| MZN_DIV_QUOTED
{ $$=BOT_DIV; }
| MZN_IDIV_QUOTED
{ $$=BOT_IDIV; }
| MZN_MOD_QUOTED
{ $$=BOT_MOD; }
| MZN_INTERSECT_QUOTED
{ $$=BOT_INTERSECT; }
| MZN_PLUSPLUS_QUOTED
{ $$=BOT_PLUSPLUS; }
| MZN_NOT_QUOTED
{ $$=-1; }
quoted_op_call :
quoted_op '(' expr ',' expr ')'
{ if ($1==-1) {
$$=nullptr;
yyerror(&@3, parm, "syntax error, unary operator with two arguments");
} else {
$$=new BinOp(@$, $3,static_cast<BinOpType>($1),$5);
}
}
| quoted_op '(' expr ')'
{ int uot=-1;
switch ($1) {
case -1:
uot = UOT_NOT;
break;
case BOT_MINUS:
uot = UOT_MINUS;
break;
case BOT_PLUS:
uot = UOT_PLUS;
break;
default:
yyerror(&@3, parm, "syntax error, binary operator with unary argument list");
break;
}
if (uot==-1)
$$=nullptr;
else {
if (uot==UOT_PLUS && $3 && (Expression::isa<IntLit>($3) || Expression::isa<FloatLit>($3))) {
$$ = $3;
} else if (uot==UOT_MINUS && $3 && Expression::isa<IntLit>($3)) {
$$ = IntLit::a(-IntLit::v(Expression::cast<IntLit>($3)));
} else if (uot==UOT_MINUS && $3 && Expression::isa<FloatLit>($3)) {
$$ = FloatLit::a(-FloatLit::v(Expression::cast<FloatLit>($3)));
} else {
$$=new UnOp(@$, static_cast<UnOpType>(uot),$3);
}
}
}
call_expr :
MZN_IDENTIFIER '(' ')'
{ $$=Call::a(@$, $1, std::vector<Expression*>()); free($1); }
| MZN_IDENTIFIER MZN_POW_MINUS1 '(' ')'
{ $$=Call::a(@$, std::string($1)+"⁻¹", std::vector<Expression*>()); free($1); }
| quoted_op_call
| MZN_IDENTIFIER '(' comp_or_expr ')'
{
if ($3!=nullptr) {
bool hadWhere = false;
std::vector<Expression*> args;
for (unsigned int i=0; i<$3->size(); i++) {
if ((*$3)[i].second) {
yyerror(&@3, parm, "syntax error, 'where' expression outside generator call");
hadWhere = true;
$$=nullptr;
}
args.push_back((*$3)[i].first);
}
if (!hadWhere) {
$$=Call::a(@$, $1, args);
}
}
free($1);
delete $3;
}
| MZN_IDENTIFIER '(' comp_or_expr ')' '(' expr ')'
{
vector<Generator> gens;
vector<Id*> ids;
if ($3) {
for (unsigned int i=0; i<$3->size(); i++) {
if (Id* id = Expression::dynamicCast<Id>((*$3)[i].first)) {
if ((*$3)[i].second) {
ParserLocation loc = Expression::loc((*$3)[i].second).parserLocation();
yyerror(&loc, parm, "illegal where expression in generator call");
}
ids.push_back(id);
} else {
if (BinOp* boe = Expression::dynamicCast<BinOp>((*$3)[i].first)) {
if (boe->lhs() && boe->rhs()) {
Id* id = Expression::dynamicCast<Id>(boe->lhs());
if (id && boe->op() == BOT_IN) {
ids.push_back(id);
gens.push_back(Generator(ids,boe->rhs(),(*$3)[i].second));
ids = vector<Id*>();
} else if (id && boe->op() == BOT_EQ && ids.empty()) {
ids.push_back(id);
gens.push_back(Generator(ids,nullptr,boe->rhs()));
if ((*$3)[i].second) {
gens.push_back(Generator(static_cast<int>(gens.size()),(*$3)[i].second));
}
ids = vector<Id*>();
} else {
ParserLocation loc = Expression::loc((*$3)[i].first).parserLocation();
yyerror(&loc, parm, "illegal expression in generator call");
}
}
} else {
ParserLocation loc = Expression::loc((*$3)[i].first).parserLocation();
yyerror(&loc, parm, "illegal expression in generator call");
}
}
}
}
if (ids.size() != 0) {
yyerror(&@3, parm, "illegal expression in generator call");
}
ParserState* pp = static_cast<ParserState*>(parm);
if (pp->hadError) {
$$=nullptr;
} else {
Generators g; g.g = gens;
Comprehension* ac = new Comprehension(@$, $6,g,false);
vector<Expression*> args; args.push_back(ac);
$$=Call::a(@$, $1, args);
}
free($1);
delete $3;
}
| MZN_IDENTIFIER MZN_POW_MINUS1 '(' comp_or_expr ')'
{
if ($4!=nullptr) {
bool hadWhere = false;
std::vector<Expression*> args;
for (unsigned int i=0; i<$4->size(); i++) {
if ((*$4)[i].second) {
yyerror(&@4, parm, "syntax error, 'where' expression outside generator call");
hadWhere = true;
$$=nullptr;
}
args.push_back((*$4)[i].first);
}
if (!hadWhere) {
$$=Call::a(@$, std::string($1)+"⁻¹", args);
}
}
free($1);
delete $4;
}
| MZN_IDENTIFIER MZN_POW_MINUS1 '(' comp_or_expr ')' '(' expr ')'
{
vector<Generator> gens;
vector<Id*> ids;
if ($4) {
for (unsigned int i=0; i<$4->size(); i++) {
if (Id* id = Expression::dynamicCast<Id>((*$4)[i].first)) {
if ((*$4)[i].second) {
ParserLocation loc = Expression::loc((*$4)[i].second).parserLocation();
yyerror(&loc, parm, "illegal where expression in generator call");
}
ids.push_back(id);
} else {
if (BinOp* boe = Expression::dynamicCast<BinOp>((*$4)[i].first)) {
if (boe->lhs() && boe->rhs()) {
Id* id = Expression::dynamicCast<Id>(boe->lhs());
if (id && boe->op() == BOT_IN) {
ids.push_back(id);
gens.push_back(Generator(ids,boe->rhs(),(*$4)[i].second));
ids = vector<Id*>();
} else if (id && boe->op() == BOT_EQ && ids.empty()) {
ids.push_back(id);
gens.push_back(Generator(ids,nullptr,boe->rhs()));
if ((*$4)[i].second) {
gens.push_back(Generator(static_cast<int>(gens.size()),(*$4)[i].second));
}
ids = vector<Id*>();
} else {
ParserLocation loc = Expression::loc((*$4)[i].first).parserLocation();
yyerror(&loc, parm, "illegal expression in generator call");
}
}
} else {
ParserLocation loc = Expression::loc((*$4)[i].first).parserLocation();
yyerror(&loc, parm, "illegal expression in generator call");
}
}
}
}
if (ids.size() != 0) {
yyerror(&@4, parm, "illegal expression in generator call");
}
ParserState* pp = static_cast<ParserState*>(parm);
if (pp->hadError) {
$$=nullptr;
} else {
Generators g; g.g = gens;
Comprehension* ac = new Comprehension(@$, $7,g,false);
vector<Expression*> args; args.push_back(ac);
$$=Call::a(@$, std::string($1)+"⁻¹", args);
}
free($1);
delete $4;
}
comp_or_expr : comp_or_expr_head comma_or_none
comp_or_expr_head :
expr
{ $$=new vector<pair<Expression*,Expression*> >;
if ($1) {
$$->push_back(pair<Expression*,Expression*>($1,nullptr));
}
}
| expr MZN_WHERE expr
{ $$=new vector<pair<Expression*,Expression*> >;
if ($1 && $3) {
$$->push_back(pair<Expression*,Expression*>($1,$3));
}
}
| comp_or_expr_head ',' expr
{ $$=$1; if ($$ && $3) $$->push_back(pair<Expression*,Expression*>($3,nullptr)); }
| comp_or_expr_head ',' expr MZN_WHERE expr
{ $$=$1; if ($$ && $3 && $5) $$->push_back(pair<Expression*,Expression*>($3,$5)); }
let_expr :
MZN_LET '{' '}' MZN_IN expr %prec PREC_ANNO
{ $$=$5; }
| MZN_LET '{' let_vardecl_item_list '}' MZN_IN expr %prec PREC_ANNO
{ if ($3 && $6) {
$$=new Let(@$, *$3, $6); delete $3;
} else {
$$=nullptr;
}
}
| MZN_LET '{' let_vardecl_item_list comma_or_semi '}' MZN_IN expr %prec PREC_ANNO
{ if ($3 && $7) {
$$=new Let(@$, *$3, $7); delete $3;
} else {
$$=nullptr;
}
}
let_vardecl_item_list :
let_vardecl_item
{ $$=new vector<Expression*>; $$->push_back($1); }
| constraint_item
{ $$=new vector<Expression*>;
if ($1) {
ConstraintI* ce = $1->cast<ConstraintI>();
$$->push_back(ce->e());
ce->e(nullptr);
}
}
| let_vardecl_item_list comma_or_semi let_vardecl_item
{ $$=$1; if ($$ && $3) $$->push_back($3); }
| let_vardecl_item_list comma_or_semi constraint_item
{ $$=$1;
if ($$ && $3) {
ConstraintI* ce = $3->cast<ConstraintI>();
$$->push_back(ce->e());
ce->e(nullptr);
}
}
comma_or_semi : ',' | ';'
let_vardecl_item :
ti_expr_and_id
{ $$ = $1;
if ($1 && Expression::type($1->ti()).any() && $1->ti()->domain() == nullptr) {
// This is an any type, not allowed without a right hand side
yyerror(&@1, parm, "declarations with `any' type-inst require definition");
}
if ($$) $$->toplevel(false);
}
| ti_expr_and_id MZN_EQ expr
{ if ($1) {
$1->e($3);
}
$$ = $1;
if ($$) Expression::loc($$, @$);
if ($$) $$->toplevel(false);
}
annotations :
/* empty */
{ $$=nullptr; }
| ne_annotations
annotation_expr :
expr_atom_head_nonstring
{ $$ = $1; }
| MZN_OUTPUT
{ $$ = new Id(@1, Constants::constants().ids.output, nullptr); }
| string_expr
{ $$ = Call::a(@1, ASTString("mzn_expression_name"), {$1}); }
ne_annotations :
MZN_COLONCOLON annotation_expr
{ $$=new std::vector<Expression*>(1);
(*$$)[0] = $2;
}
| ne_annotations MZN_COLONCOLON annotation_expr
{ $$=$1; if ($$) $$->push_back($3); }
id_or_quoted_op :
MZN_IDENTIFIER
{ $$=$1; }
| MZN_IDENTIFIER MZN_POW_MINUS1
{ $$=strdup((std::string($1)+"⁻¹").c_str()); }
| MZN_EQUIV_QUOTED
{ $$=strdup("'<->'"); }
| MZN_IMPL_QUOTED
{ $$=strdup("'->'"); }
| MZN_RIMPL_QUOTED
{ $$=strdup("'<-'"); }
| MZN_OR_QUOTED
{ $$=strdup("'\\/'"); }
| MZN_XOR_QUOTED
{ $$=strdup("'xor'"); }
| MZN_AND_QUOTED
{ $$=strdup("'/\\'"); }
| MZN_LE_QUOTED
{ $$=strdup("'<'"); }
| MZN_GR_QUOTED
{ $$=strdup("'>'"); }
| MZN_LQ_QUOTED
{ $$=strdup("'<='"); }
| MZN_GQ_QUOTED
{ $$=strdup("'>='"); }
| MZN_EQ_QUOTED
{ $$=strdup("'='"); }
| MZN_NQ_QUOTED
{ $$=strdup("'!='"); }
| MZN_IN_QUOTED
{ $$=strdup("'in'"); }
| MZN_SUBSET_QUOTED
{ $$=strdup("'subset'"); }
| MZN_SUPERSET_QUOTED
{ $$=strdup("'superset'"); }
| MZN_UNION_QUOTED
{ $$=strdup("'union'"); }
| MZN_DIFF_QUOTED
{ $$=strdup("'diff'"); }
| MZN_SYMDIFF_QUOTED
{ $$=strdup("'symdiff'"); }
| MZN_DOTDOT_QUOTED
{ $$=strdup("'..'"); }
| MZN_LE_DOTDOT_QUOTED
{ $$=strdup("'<..'"); }
| MZN_DOTDOT_LE_QUOTED
{ $$=strdup("'..<'"); }
| MZN_LE_DOTDOT_LE_QUOTED
{ $$=strdup("'<..<'"); }
| MZN_PLUS_QUOTED
{ $$=strdup("'+'"); }
| MZN_MINUS_QUOTED
{ $$=strdup("'-'"); }
| MZN_MULT_QUOTED
{ $$=strdup("'*'"); }
| MZN_POW_QUOTED
{ $$=strdup("'^'"); }
| MZN_DIV_QUOTED
{ $$=strdup("'/'"); }
| MZN_IDIV_QUOTED
{ $$=strdup("'div'"); }
| MZN_MOD_QUOTED
{ $$=strdup("'mod'"); }
| MZN_INTERSECT_QUOTED
{ $$=strdup("'intersect'"); }
| MZN_NOT_QUOTED
{ $$=strdup("'not'"); }
| MZN_PLUSPLUS_QUOTED
{ $$=strdup("'++'"); }
|