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 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488
|
\input texinfo @c -*-texinfo-*-
@c %**start of header
@setfilename api.info
@include version.texi
@settitle Libmarpa @value{VERSION}
@finalout
@c %**end of header
@copying
This manual (@value{UPDATED})
is for Libmarpa @value{VERSION}.
Copyright @copyright{} 2014 Jeffrey Kegler.
@quotation
Permission is granted to copy, distribute and/or modify this document
under the terms of the @acronym{GNU} Free Documentation License,
Version 1.3 or any later version published by the Free Software
Foundation.
A copy of the license is included in the section entitled
``@acronym{GNU} Free Documentation License.''
@end quotation
@end copying
@dircategory Development
@direntry
* Marpa R2: (marpa-r2). Marpa R2 parser library
@end direntry
@titlepage
@title Libmarpa
@subtitle Version @value{VERSION}
@subtitle @value{UPDATED}
@author Jeffrey Kegler
@c The following two commands
@c start the copyright page.
@page
@vskip 0pt plus 1filll
@insertcopying
Published @value{UPDATED} by Jeffrey Kegler
@end titlepage
@c So the toc is printed at the start.
@contents
@ifnottex
@node Top, Copying, (dir), (dir)
@top Libmarpa: The Marpa low-level library
@insertcopying
@end ifnottex
@menu
* Copying::
* About this document::
* About Libmarpa::
* Architecture::
* Input::
* Semantics::
* Threads::
* Fatal Errors::
* Introduction to the external interface::
* Static methods::
* Configuration methods::
* Grammar methods::
* Recognizer methods::
* Progress reports::
* Bocage methods::
* Ordering methods::
* Tree methods::
* Value methods::
* Events::
* Error methods macros and codes::
* Design notes::
* Work in Progress::
* Deprecated techniques and methods::
* GNU Free Documentation License::
@detailmenu
--- The Detailed Node Listing ---
About this document
* How to read this document::
* Prerequisites::
* Parsing theory::
Architecture
* Major objects::
* Time objects::
* Reference counting::
* Numbered objects::
Input
* Earlemes::
* Terminals::
Earlemes
* The traditional model::
* The earleme variables::
* The significances of the earleme variables::
* The initial earleme settings::
* The standard model of input::
* Ambiguous input::
* Variable length tokens::
* The generalized model::
* General rules for the earleme variables::
Introduction to the external interface
* About the overviews::
* Return values::
* Naming conventions::
Grammar methods
* Grammar overview::
* Grammar constructor::
* Grammar reference counting::
* Symbols::
* Rules::
* Sequences::
* Ranks::
* Grammar precomputation::
Recognizer methods
* Recognizer overview::
* Recognizer constructor::
* Recognizer reference counting::
* Recognizer life cycle mutators::
* Location accessors::
* Other parse status methods::
* Untested recognizer methods::
Bocage methods
* Bocage overview::
* Bocage constructor::
* Bocage reference counting::
* Bocage accessor::
Ordering methods
* Ordering overview::
* Ordering constructor::
* Ordering reference counting::
* Order accessor::
* Non-default ordering::
Tree methods
* Tree overview::
* Tree constructor::
* Tree reference counting::
* Tree iteration::
Value methods
* Value overview::
* How to use the valuator::
* Advantages of step-driven valuation::
* Maintaining the stack::
* Valuator constructor::
* Valuator reference counting::
* Stepping through the valuator::
* Valuator steps by type::
* Basic step accessors::
* Other step accessors::
Maintaining the stack
* Sizing the stack::
* Initializing locations in the stack::
Events
* Events overview::
* Event methods::
* Event codes::
Error methods, macros and codes
* Error methods::
* Error Macros::
* External error codes::
* Internal error codes::
Design notes
* Why so many time objects::
* Design of numbered objects::
* LHS Terminals::
Work in Progress
* Untested methods::
Untested methods
* Ranking methods::
* Zero-width assertion methods::
* Methods for revising parses::
Deprecated techniques and methods
* Valued and unvalued symbols::
Valued and unvalued symbols
* What unvalued symbols were::
* Grammar methods dealing with unvalued symbols::
* Registering semantics in the valuator::
@end detailmenu
@end menu
@node Copying, About this document, Top, Top
@unnumbered GNU LESSER GENERAL PUBLIC LICENSE
@include lgpl-3.0.texi
@node About this document, About Libmarpa, Copying, Top
@chapter About this document
@menu
* How to read this document::
* Prerequisites::
* Parsing theory::
@end menu
@node How to read this document, Prerequisites, About this document, About this document
@section How to read this document
This is essentially a reference document,
but its early chapters lay out concepts
essential to the others.
Readers will usually want to read the
chapters up and including
@ref{Introduction to the external interface}
in order.
Otherwise, they should follow their interests.
@node Prerequisites, Parsing theory, How to read this document, About this document
@section Prerequisites
This document is very far from self-contained.
It assumes the following:
@itemize
@item
The reader knows the C programming language
at least well
enough to understand function prototypes and return values.
@item
The reader
has read the documents for one of Libmarpa's upper layers.
As of this writing, the only such layer is @code{Marpa::R2}
or @code{Marpa::R3},
in Perl.
@item
The reader knows some parsing theory.
@xref{Parsing theory}.
@end itemize
@node Parsing theory, , Prerequisites, About this document
@section Parsing theory
This document assumes some acquaintance
with parsing theory.
The reader's
level of knowledge is probably adequate
if he can
answer the following questions,
either immediately or after a little reflection.
@itemize @bullet
@item
What is a BNF rule?
@item
What is a Marpa sequence rule?
@item
As a reminder,
Marpa's sequence rules are implemented
as left recursions.
What does that mean?
@item
Take a Marpa sequence rule at random.
What does it look like when rewritten in BNF?
@item
What does the sequence look like when rewritten
in BNF as a right-recursion?
@end itemize
@node About Libmarpa, Architecture, About this document, Top
@chapter About Libmarpa
Libmarpa implements the Marpa parsing algorithm.
Marpa is named
after the legendary 11th century Tibetan translator,
Marpa Lotsawa.
In creating Marpa,
I depended heavily on previous work by Jay Earley,
Joop Leo,
John Aycock and Nigel Horspool.
Libmarpa implements the entire Marpa algorithm.
This library does
the necessary grammar preprocessing, recognizes the input,
and produces parse trees.
It also supports the ordering, iteration
and evaluation of the parse
trees.
Libmarpa is very low-level.
For example, it has no strings.
Rules, symbols, and token values are all represented
by integers.
This, of course, will not suffice for many applications.
Users will very often want
names for the symbols, non-integer values for
tokens, or both.
Typically, applications will use arrays to
translate Libmarpa's integer ID's to strings or other
values as required.
Libmarpa also does @strong{not} implement most of the semantics.
Libmarpa does have an evaluator (called a ``valuator''),
but it does @strong{not}
manipulate the stack directly.
Instead, Libmarpa,
based on its traversal of the parse tree,
passes optimized step by step stack manipulation
instructions to the upper layer.
These instructions indicate the token or rule involved,
and the proper location for the true token value or
the result of the rule evaluation.
For rule evaluations, the instructions include the stack location
of the arguments.
Marpa requires most semantics to be
implemented in the application.
This allows the application total flexibility.
It also puts
the application is in a much better position to prevent errors,
to catch errors at runtime or,
failing all else,
to successfully debug the logic.
@node Architecture, Input, About Libmarpa, Top
@chapter Architecture
@menu
* Major objects::
* Time objects::
* Reference counting::
* Numbered objects::
@end menu
@node Major objects, Time objects, Architecture, Architecture
@section Major objects
The classes of
Libmarpa's object system fall into two types:
major and numbered.
These are the Libmarpa's major classes,
in sequence.
@itemize
@item
Configuration:
A configuration object is
a thread-safe way to hold configuration variables,
as well as the return code from failed attempts
to create grammar objects.
@item
Grammar:
A grammar object contains rules and symbols,
with their properties.
@item
Recognizer:
A recognizer object reads input.
@item
Bocage:
A bocage object is a collection of
parse trees, as found by a recognizer.
Bocages are similar to parse forests.
@item
Ordering:
An ordering object
is an ordering of the trees
in a bocage.
@item
Tree:
A tree object is a bocage iterator.
@item
Value:
A value object is a tree iterator.
Iteration of tree using a value object
produces ``steps''.
These ``steps'' are
instructions to
the application on how
to evaluate the semantics,
and how to manipulate the stack.
@end itemize
The major objects have one letter abbreviations,
which are used frequently.
These are, in the standard sequence,
@itemize
@item
Configuration: C
@item
Grammar: G
@item
Recognizer: R
@item
Bocage: B
@item
Ordering: O
@item
Tree: T
@item
Value: V
@end itemize
@node Time objects, Reference counting, Major objects, Architecture
@section Time objects
All of Libmarpa's major classes,
except the configuration class,
are ``time'' classes.
Except for objects in the grammar class,
all time objects are created from another time
object.
Each time object is created from a time object
of the class before it in the sequence.
A recognizer cannot be created without a precomputed grammar;
a bocage cannot be created without a recognizer;
and so on.
When one time object is used to create a second
time object,
the first time object is the @dfn{parent object}
and the second time object is the @dfn{child object}.
For example, when a bocage is created from a
recognizer,
the recognizer is the parent object,
and the bocage is the child object.
Grammars have no parent object.
Every other time object has exactly one parent object.
Value objects have no child objects.
All other time objects can have any number of children,
from zero up to a number determined by memory or
some other machine-determined limit.
Every time object has a @dfn{base grammar}.
A grammar object is its own base grammar.
The base grammar of a recognizer is the grammar
that it was created with.
The base grammar of any other time object is the base
grammar of its parent object.
For example,
the base grammar of a bocage is the base
grammar of the recognizer that it was created
with.
@node Reference counting, Numbered objects, Time objects, Architecture
@section Reference counting
Every object in a ``time'' class
has its own, distinct, lifetime,
which is controlled by the object's reference count.
Reference counting follows the usual practice.
Contexts which take a share of the
``ownership'' of an object
increase the reference count by 1.
When a context relinquishes its share of
the ownership of an object, it decreases the reference
count by 1.
Each class of time object has a ``ref'' and an ``unref''
method, to be used by those contexts which need to
explicitly increment and decrement the reference count.
For example, the ``ref'' method for the grammar class is
@code{marpa_g_ref()}
and the ``unref'' method for the grammar class is
@code{marpa_g_unref()}.
Time objects do not have explicit destructors.
When the reference count of a time object reaches
0, that time object is destroyed.
Much of the necessary reference counting
is performed automatically.
The context calling the constructor of a time object
does not need to explicitly increase the reference
count, because
Libmarpa time objects are
always created with a reference count of 1.
Child objects ``own'' their parents,
and when a child object is successfully created,
the reference count of its parent object is
automatically incremented to reflect this.
When a child object is destroyed, it
automatically decrements the reference count of its parent.
In a typical application, a calling context needs only
to remember
to ``unref'' each time object that it creates,
once it is finished with that time object.
All other reference decrements and increments are taken
care of automatically.
The typical application never needs to explicitly
call one of the ``ref'' methods.
More complex applications may find it convenient
to have one or more contexts share ownership of objects
created in another context.
These more complex situations
are the only cases in which the ``ref'' methods
will be needed.
@node Numbered objects, , Reference counting, Architecture
@section Numbered objects
In addition to its major, ``time'' objects, Libmarpa also has
numbered objects.
Numbered objects do not have lifetimes of their own.
Every numbered object belongs to a time object,
and is destroyed with it.
Rules and symbols are numbered objects.
Tokens values are another class of numbered
objects.
@node Input, Semantics, Architecture, Top
@chapter Input
@menu
* Earlemes::
* Terminals::
@end menu
@node Earlemes, Terminals, Input, Input
@section Earlemes
@menu
* The traditional model::
* The earleme variables::
* The significances of the earleme variables::
* The initial earleme settings::
* The standard model of input::
* Ambiguous input::
* Variable length tokens::
* The generalized model::
* General rules for the earleme variables::
@end menu
@node The traditional model, The earleme variables, Earlemes, Earlemes
@subsection The traditional model
In traditional Earley parsers, the concept of location is very simple.
Locations are numbered from 0 to @var{n}, where @var{n} is the length of
the input.
Every location has an Earley set, and vice versa.
Location 0 is the start location.
Every location after the start location has exactly one input token
associated with it.
Some applications
do not fit this traditional input model ---
natural language processing requires ambiguous tokens,
for example.
Libmarpa allows a wide variety of alternative input models.
This document assumes that the reader knows the concepts
behind Libmarpa's
alternative input models, either from the documentation
of a higher level interface, such as
@code{Marpa::XS},
@code{Marpa::R2},
or @code{Marpa::R3},
or from Marpa's
@uref{https://docs.google.com/file/d/0B9_mR_M2zOc4Ni1zSW5IYzk3TGc/edit?usp=sharing, theory document}.
As a reminder,
in Libmarpa a location is called a @dfn{earleme}.
The number of an Earley set is the @dfn{ID of the Earley set},
or its @dfn{ordinal}.
In the traditional model, the ordinal of an Earley set and
its earleme are always exactly the same, but in Libmarpa
they will be different.
@node The earleme variables, The significances of the earleme variables, The traditional model, Earlemes
@subsection The earleme variables
The important earleme variables are the current earleme, the furthest earleme
and the latest earleme.
The @dfn{current earleme} is the earleme that Libmarpa is currently working on.
More specifically, it is the one at which new tokens will @strong{start}.
Since tokens are never zero length, a new token will always end after the
current earleme.
The current earleme is initially earleme 0.
Every call to @code{marpa_r_earleme_complete()} advances the
current earleme by 1.
The @dfn{furthest earleme} is the highest numbered (and therefore ``furthest'')
earleme at which a token ends.
The furthest earleme is initially earleme 0.
With every call to @code{marpa_r_alternative()}, the end of the token
it adds is calculated.
A token ends at the earleme location @var{current}+@var{length},
where @var{current} is the current earleme,
and @var{length} is the length of the newly added token.
After a call to @code{marpa_r_alternative()},
the furthest earleme is its value before the call,
or @var{current}+@var{length},
whichever is greater.
The @dfn{latest earleme} is the earleme of the latest
Earley set.
The @dfn{latest Earley set} is the last Earley set completed.
This is always the highest numbered Earley set.
If there is an Earley set at the current earleme,
it is the latest Earley set and the latest earleme
is equal to the current earleme.
There is never an Earley set after the current earleme.
After every call to the @code{marpa_r_earleme_complete()} method
that adds a token,
the value of the latest earleme is
same as the value of the current earleme.
After every call to the @code{marpa_r_earleme_complete()} method
that does @strong{not} add a token,
the value of the latest earleme is unchanged
from its value before the call.
@node The significances of the earleme variables, The initial earleme settings, The earleme variables, Earlemes
@subsection The significances of the earleme variables
The current earleme tracks the advance of the recognizer through the input.
Input tokens always start at the current earleme.
An application can advance past the current earleme,
by calling @code{marpa_r_earleme_complete()}, which
increments the current earleme by 1.
After initialization,
@code{marpa_r_earleme_complete()} is
the only way to manipulate the value of the current earleme.
The furthest earleme tracks how ``far out'' tokens can be found.
In the standard input model, calling
@code{marpa_r_earleme_complete()} after each
@code{marpa_r_alternative()} call is sufficient to process
all inputs,
and the furthest earleme's value
can be typically be ignored.
In alternative input models, if tokens have lengths greater than
1, calling
@code{marpa_r_earleme_complete()} once after the last token
is read may not be enough to ensure that all tokens have been processed.
To ensure that all tokens have been processed,
an application must advance the current earleme
by calling @code{marpa_r_earleme_complete()},
until the current earleme is equal to the furthest earleme.
The lastest earleme is the earleme of the last Earley set.
The latest earleme is different from the current earleme if and only if
there is no Earley set at the current earleme.
A different end of parsing can be specified,
but by default, parsing is of the input
in the range
from earleme 0 to the latest earleme.
@node The initial earleme settings, The standard model of input, The significances of the earleme variables, Earlemes
@subsection The initial earleme settings
All input models have the same initial values.
Initially the current, latest and furthest earleme
are always earleme 0.
Understanding the
settings of current, latest and furthest earleme is
crucial to working with advanced input models,
and for this reason the next sections will go
through the possibilities carefully.
The presentation will start with the most traditional
and restrictive models.
It will proceed to less restrictive models.
@node The standard model of input, Ambiguous input, The initial earleme settings, Earlemes
@subsection The standard model of input
In the standard model of input,
calls to @code{marpa_r_alternative()}
and @code{marpa_r_earleme_complete()} are
made in pairs.
There is, first, exactly one call
to @code{marpa_r_alternative()},
and it is for a token with length 1.
Following it must be a call
to @code{marpa_r_earleme_complete()}.
For an input of length @var{n}, there will be
exactly @var{n} such paired calls.
Suppose, in the standard model that,
for a call
to @code{marpa_r_alternative()},
the following is true:
@itemize
@item The current earleme before the call was @var{c}.
@end itemize
Because this is the standard model,
this means that we also know that
@itemize
@item The latest earleme before the call was @var{c}.
@item The furthest earleme before the call was @var{c}.
@end itemize
In that case,
after the call to
@code{marpa_r_alternative()},
the following will be true:
@itemize
@item The current earleme will still be @var{c}.
@item The latest earleme will still be @var{c}.
@item The furthest earleme will be @var{c}+1.
@end itemize
Suppose, in the standard model that,
for a call
to @code{marpa_r_earleme_complete()},
the following is true:
@itemize
@item The current earleme before the call is @var{c}.
@end itemize
Because this is the standard model,
this means that we also know that
@itemize
@item The latest earleme before the call was @var{c}.
@item The furthest earleme before the call was @var{c+1}.
@end itemize
In that case,
after the call
to @code{marpa_r_earleme_complete()},
the current, latest and furthest earlemes will
be the same.
Specifically,
the following will be true:
@itemize
@item The current earleme will be advanced to @var{c+1}.
@item The latest earleme will be advanced to @var{c+1}.
@item The furthest earleme will remain at @var{c+1}.
@end itemize
@node Ambiguous input, Variable length tokens, The standard model of input, Earlemes
@subsection Ambiguous input
As a first loosening of the standard model,
we no longer require calls to @code{marpa_r_alternative()}
to be paired with calls to
@code{marpa_r_earleme_complete()}.
Instead,
we allow a series of one or more calls
to @code{marpa_r_alternative()}
before each call to
@code{marpa_r_earleme_complete()}.
We still require that there be at least one call
to @code{marpa_r_alternative()}
before each call to
@code{marpa_r_earleme_complete()},
and we still require that all tokens have
a length of 1.
In the ambiguous input model, the behavior of the current,
latest and furthest earlemes are exactly
as described for the standard model, with one minor
exception:
Where the current earleme is @var{c},
and a call to @code{marpa_r_alternative()} is not the first
of a series,
the value of the furthest earleme before the call will be
@var{c}+1.
@node Variable length tokens, The generalized model, Ambiguous input, Earlemes
@subsection Variable length tokens
Our next loosening of the restrictions is to allow
variable length tokens.
That is, instead of requiring that all tokens
be of length 1,
we allow tokens to be of length 1 or longer.
This does change the behavior of the earleme variables.
Suppose, in the variable length token model that,
for a call
to @code{marpa_r_alternative()},
the following is true:
@itemize
@item The current earleme before the call was @var{c}.
In this model, this means that
the latest earleme before the call is also @var{c}.
@item The furthest earleme before the call was @var{old_f}.
@item The length of the token is @var{length}.
@end itemize
In that case,
after the call to
@code{marpa_r_alternative()},
the following will be true:
@itemize
@item The current and latest earlemes will still be @var{c}.
The current and latest earlemes are never changed by a call
to @code{marpa_r_alternative()}.
@item The furthest earleme will be either @var{old_f} or
@var{c}+@var{length}, whichever was greater.
@end itemize
Suppose, in the variable length token model that,
for a call
to @code{marpa_r_earleme_complete()},
the following is true:
@itemize
@item The current earleme before the call is @var{c}.
In this model, this means that
the latest earleme before the call is also @var{c}.
@item The furthest earleme before the call is @var{old_f}.
@end itemize
In that case,
after the call
to @code{marpa_r_earleme_complete()},
the following will be true:
@itemize
@item The current earleme will be advanced to @var{c+1}.
@item The latest earleme will also be @var{c+1}.
@item The furthest earleme will still be @var{old_f}.
The furthest earleme is never changed by a call
to @code{marpa_r_earleme_complete()}.
@end itemize
@node The generalized model, General rules for the earleme variables, Variable length tokens, Earlemes
@subsection The generalized model
To fully generalize the input model,
we now need to remove only one restriction.
We now allow empty earlemes --- earlemes with
no tokens and no Earley set.
For the generalized input model,
the effect on the earleme variables of
a call
to @code{marpa_r_alternative()} is exactly
the same as it is for the variable length input model.
The effect on the earleme variables of a call to
to @code{marpa_r_earleme_complete()} depends on
whether or not that call creates an empty earleme.
A call
to @code{marpa_r_earleme_complete()}
creates an empty earleme if and only if
it falls into one of these two cases:
@itemize
@item
There has been no call
to @code{marpa_r_alternative()} since
recognizer initialization.
@item
There has been no call
to @code{marpa_r_alternative()} since
the previous call
to @code{marpa_r_earleme_complete()}.
@end itemize
Suppose, in the generalized input model that,
for a call
to @code{marpa_r_earleme_complete()}
that creates an empty earleme,
the following is true:
@itemize
@item The current earleme before the call is @var{c}.
@item The latest earleme before the call is @var{old_l}.
@item The furthest earleme before the call is @var{old_f}.
@end itemize
In that case,
after the call
to @code{marpa_r_earleme_complete()},
the following will be true:
@itemize
@item The current earleme will be advanced to @var{c+1}.
@item The latest earleme will remain at @var{old_l}.
This means that the latest earleme will be less than
the current earleme.
@item The furthest earleme will remain at @var{old_f}.
The furthest earleme is never changed by a call
to @code{marpa_r_earleme_complete()}.
@end itemize
Suppose, in the generalized input model that,
for a call
to @code{marpa_r_earleme_complete()}
that does not creates an empty earleme,
the following is true:
@itemize
@item The current earleme before the call is @var{c}.
@item The latest earleme before the call is @var{old_l}.
@item The furthest earleme before the call is @var{old_f}.
@end itemize
In that case,
after the call
to @code{marpa_r_earleme_complete()},
the following will be true:
@itemize
@item The current earleme will be advanced to @var{c+1}.
@item The latest earleme will be advanced to @var{c+1}.
@item The furthest earleme will remain at @var{old_f}.
The furthest earleme is never changed by a call
to @code{marpa_r_earleme_complete()}.
@end itemize
@node General rules for the earleme variables, , The generalized model, Earlemes
@subsection General rules for the earleme variables
At this point, the most generalized input model has been
introduced.
Here are some useful facts about the earleme variables that will always be the case,
no matter which of the input models is in use.
@itemize
@item The current earleme is always greater than
or equal to the latest earleme.
@item The furthest earleme is always greater than
or equal to the latest earleme.
@item If the furthest earleme is greater than the current earleme,
the parser is not exhausted.
@item If the furthest earleme is less than the current earleme,
the parser is exhausted.
@end itemize
If the parser is not exhausted,
the following will always be true,
no matter which of the input models is in use.
@itemize
@item
The furthest earleme is greater than
or equal to the current earleme.
@end itemize
In an exhausted parser, the following will always be true,
no matter which of the input models is in use.
@itemize
@item
The furthest earleme is less than
or equal to the current earleme.
@end itemize
@node Terminals, , Earlemes, Input
@section Terminals
A terminal symbol is a symbol which
may appear in the input.
Traditionally,
all LHS symbols, as well as
the start symbol, must be non-terminals.
This is Marpa's behavior, by default.
Marpa allows the user to eliminate the distinction
between terminals and non-terminals.
In this, it
differs from traditional parsers.
A Libmarpa
can arrange for a a terminal
to appear on the LHS of one or more rules,
or for a terminal to be the start symbol.
However,
since terminals can never be zero length,
it is a logical contradiction for a nulling
symbol to also be a terminal
and Marpa does not allow it.
Token values are @code{int}'s.
Libmarpa does nothing with token values except accept
them from the application and return them during
parse evaluation.
@node Semantics, Threads, Input, Top
@chapter Semantics
Libmarpa handling of semantics is unusual.
Most semantics are left up to the application,
but Libmarpa guides them.
Specifically, the application is expected to maintain the evaluation
stack.
Libmarpa's valuator provides instructions on how to handle the stack.
Libmarpa's stack handling instructions
are called ``steps''.
For example, a Libmarpa step might tell the application that the value
of a token needs to go into a certain stack position.
Or a Libmarpa step might tell the application that a rule is to be evaluated.
For rule evalution, Libmarpa will tell the application where the operands
are to be found,
and where the result must go.
The detailed discussion of
Libmarpa's handling of semantics is in the reference chapters
of this document,
under the appropriate methods and classes.
The most extensive discussion of the semantics
is in the section that deals with the methods of the value time class.
@xref{Value methods}.
@node Threads, Fatal Errors, Semantics, Top
@chapter Threads
Libmarpa is thread-safe,
given circumstances as described below.
The Libmarpa methods are not reentrant.
Libmarpa is C89-compliant.
It uses no global data,
and calls only the routines
that are defined in the C89 standard
and that can be made thread-safe.
In most modern implementations,
the default C89 implementation is thread-safe
to the extent possible.
But the C89 standard does not require thread-safety,
and even most modern environments allow the user
to turn thread safety off.
To be thread-safe, Libmarpa must be compiled
and linked in an environment that provides
thread-safety.
While Libmarpa can be used safely across
multiple threads,
a Libmarpa grammar cannot be.
Further, a Libmarpa time object can
only be used safely in the same thread
as its base grammar.
This is because all
time objects with the same base grammar share data
from that base grammar.
To work around this limitation,
the same grammar definition can be
used to a create a new
Libmarpa grammar
time object in each thread.
If there is sufficient interest, future versions of
Libmarpa could allow thread-safe
cloning of grammars and other
time objects.
@node Fatal Errors, Introduction to the external interface, Threads, Top
@chapter Fatal Errors
Libmarpa avoids fatal errors,
leaving that decision up to the application,
with one exception.
Currently, if @code{malloc} fails to allocate memory,
Libmarpa terminates the program with a fatal error.
While this is in keeping with current practice,
future versions of Libmarpa are likely to both allow
an alternative memory allocator to be specified,
and to allow the user to specifier a handler to
be called when an out-of-memory condition occurs.
@node Introduction to the external interface, Static methods, Fatal Errors, Top
@chapter Introduction to the external interface
The following chapters describe Libmarpa's external
interface in detail.
@menu
* About the overviews::
* Return values::
* Naming conventions::
@end menu
@node About the overviews, Return values, Introduction to the external interface, Introduction to the external interface
@section About the overviews
The reference sections for each major class begin with an overview.
These sections name the
most important Libmarpa methods
in the order in which they are typically used,
and very briefly describe their purpose.
The overviews are intended as ``cheat sheets''.
The overview sections often speak of
an ``archetypal'' application.
The archetypal Libmarpa application
implements a complete logic flow,
starting with the creation of a grammar,
and proceeding all the way
to the return of the final result from a value object.
In the archetypal Libmarpa application,
the grammar, input and semantics are
all small but non-trivial.
@node Return values, Naming conventions, About the overviews, Introduction to the external interface
@section Return values
Return values are discussed method by method,
but some general practices are worth
mentioning.
For methods that return an integer,
Libmarpa usually reserves @minus{}1 for special purposes,
such as indicating loop termination in an iterator.
In Libmarpa, methods usually indicate failure
by returning @minus{}2.
Any result greater than @minus{}2 usually indicates success.
If a method returns an pointer value,
@code{NULL} usually indicates failure.
Any other result usually indicates success.
On failure, a Libmarpa method always sets
the error code.
On success, a Libmarpa method may take
one of three actions:
@itemize
@item Set
an informational error code.
@item Leave
the error code as it is.
@item Reset the error code to @code{MARPA_ERROR_NONE}.
@end itemize
Which of the three actions the method
takes on success
should be regarded as unspecified,
unless stated in the description of the method
in this document.
If a method sets an informational error code on
success, that will always be stated in
its description, and the description will
specify,
for each informational error code,
the specific error code value,
the circumstances under which it is set,
and what return values will accompany it.
Informational error codes are set
because some applications may find them convenient.
Typically information error codes are redundant information,
which may be ignored.
A method can have many reasons for
failing, and many of the reasons
for failure are common to
large number of methods.
Full descriptions of the error codes
that are returned by the external methods
are given in their own section.
@xref{External error codes}.
@node Naming conventions, , Return values, Introduction to the external interface
@section Naming conventions
Methods in Libmarpa follow a strict naming convention.
All methods have a name beginning with @code{marpa_},
if they are part of the
external interface.
If an external method is not a static method,
its name is prefixed with one of
@code{marpa_c_},
@code{marpa_g_},
@code{marpa_r_},
@code{marpa_b_},
@code{marpa_o_},
@code{marpa_t_} or
@code{marpa_v_},
where the single letter between underscores
is one of the Libmarpa major class abbreviations.
The letter indicates which class
the method belongs to.
Methods that are exported,
but that are part of
the internal interface,
begin with @code{_marpa_}.
Methods that are part of the internal interface
(often called ``internal methods'')
are subject to change and are intended for use
only by Libmarpa's developers.
Libmarpa reserves the @code{marpa_}
and @code{_marpa_} prefixes for itself,
with all their capitalization variants.
All Libmarpa names visible outside the package
will begin with a capitalization variant
of one of these two prefixes.
@node Static methods, Configuration methods, Introduction to the external interface, Top
@chapter Static methods
@deftypefun Marpa_Error_Code marpa_check_version @
(int @var{required_major}, @
int @var{required_minor}, @
int @var{required_micro} @
)
Checks that the Marpa library in use is compatible with the
given version. Generally you will pass in the constants
@code{MARPA_MAJOR_VERSION},
@code{MARPA_MINOR_VERSION},
@code{MARPA_MICRO_VERSION}
as the three arguments to this function; that produces
a check that the library in use is compatible with
the version of Libmarpa the application or module was compiled
against.
Compatibility is defined by two things:
first the version
of the running library is newer than the version
@var{required_major}.@var{required_minor}.@var{required_micro}.
Second
the running library must be binary compatible with the
version
@var{required_major}.@var{required_minor}.@var{required_micro}
(same major version.)
Return value: @code{MARPA_ERR_NONE} if the Marpa library is compatible with the
requested version. If the library is not compatible,
one of the following is returned, indicating the nature of the mismatch:
@itemize
@item @code{MARPA_ERR_MAJOR_VERSION_MISMATCH},
@item @code{MARPA_ERR_MINOR_VERSION_MISMATCH}
@item @code{MARPA_ERR_MICRO_VERSION_MISMATCH}
@end itemize
@end deftypefun
@deftypefun Marpa_Error_Code marpa_version (int* version)
Returns the version number in @var{version},
which must have room for three @code{int}'s.
Return value: A non-negative number on success,
@minus{}2 on failure.
@end deftypefun
@node Configuration methods, Grammar methods, Static methods, Top
@chapter Configuration methods
The configuration object is intended for future extensions.
These may
allow the application to override Libmarpa's memory allocation
and fatal error handling without resorting to global
variables, and therefore in a thread-safe way.
Currently, the only function of the @code{Marpa_Config}
class is to give @code{marpa_g_new()}
a place to put its error code.
@code{Marpa_Config} is Libmarpa's only ``major''
class which is not a time class.
There is no constructor or destructor, although
@code{Marpa_Config} objects @strong{do} need to be initialized
before use.
Aside from its own accessor,
@code{Marpa_Config} objects are only used by @code{marpa_g_new}
and no reference to their location is not kept
in any of Libmarpa's time objects.
The intent is to that it be convenient
to have them in memory that might be deallocated
soon after @code{marpa_g_new} returns.
For example, they could be put on the stack.
@deftypefun int marpa_c_init ( @
Marpa_Config* @var{config})
Initialize the @var{config} information to ``safe'' default
values.
Unspecified behavior will result
if an initialized
configuration is used to create a grammar.
Return value: A non-negative value. Always succeeds.
@end deftypefun
@deftypefun Marpa_Error_Code marpa_c_error ( @
Marpa_Config* @var{config}, const char** @var{p_error_string} )
Error codes are usually kept in the base grammar,
which leaves @code{marpa_g_new()} no place to put
its error code on failure.
Objects of
the @code{Marpa_Config} class provide such a place.
Return value:
The error code in @var{config}.
Always succeeds.
@end deftypefun
@node Grammar methods, Recognizer methods, Configuration methods, Top
@chapter Grammar methods
@cindex grammars
@menu
* Grammar overview::
* Grammar constructor::
* Grammar reference counting::
* Symbols::
* Rules::
* Sequences::
* Ranks::
* Grammar precomputation::
@end menu
@node Grammar overview, Grammar constructor, Grammar methods, Grammar methods
@section Overview
An archtypal application has a grammar.
To create a grammar, use the @code{marpa_g_new()} method.
When a grammar is no longer in use, its memory can be freed
using the
@code{marpa_g_unref()} method.
To be precomputed,
a grammar must have one or more symbols.
To create symbols, use the
@code{marpa_g_symbol_new()} method.
To be precomputed,
a grammar must have one or more rules.
To create rules, use the
@code{marpa_g_rule_new()} and
@code{marpa_g_sequence_new()} methods.
For non-trivial parsing,
one or more of the symbols must be terminals.
To mark a symbol as a terminal,
use the
@code{marpa_g_symbol_is_terminal_set()} method.
To be precomputed,
a grammar must have exactly one start symbol.
To mark a symbol as the start symbol,
use the
@code{marpa_g_start_symbol_set()} method.
Before parsing with a grammar, it must be precomputed.
To precompute a grammar,
use the
@code{marpa_g_precompute()} method.
@node Grammar constructor, Grammar reference counting, Grammar overview, Grammar methods
@section Creating a new grammar
@cindex grammar constructor
@deftypefun Marpa_Grammar marpa_g_new ( @
Marpa_Config* @var{configuration} )
Creates a new grammar time object.
The returned grammar object is not yet precomputed,
and will have no symbols and rules.
Its reference count will be 1.
Unless the application calls @code{marpa_c_error},
Libmarpa will not reference the location
pointed to by the @var{configuration}
argument after @code{marpa_g_new} returns.
The @var{configuration} argument may be @code{NULL},
but if it is,
there will be no way to determine
the error code on failure.
Return value: On success, the grammar object.
On failure, @code{NULL},
and the error code is set in @var{configuration}.
@end deftypefun
@deftypefun int marpa_g_force_valued ( @
Marpa_Grammar @var{g} )
It is recommended that
this call be made immediately after the
grammar constructor.
It turns off a deprecated feature.
The @code{marpa_g_force_valued} forces all the
symbols in a grammar to be ``valued''.
The opposite of a valued symbol is one about whose value
you do not care.
This distinction has been made in the past in hope
of gaining efficiencies at evaluation time.
Current thinking is that the gains do not repay the extra
complexity.
Return value: On success, a non-negative integer.
On failure, a negative integer.
@end deftypefun
@node Grammar reference counting, Symbols, Grammar constructor, Grammar methods
@section Tracking the reference count of the grammar
@cindex grammar destructor
@cindex grammar reference
@cindex grammar reference count
@deftypefun Marpa_Grammar marpa_g_ref (Marpa_Grammar @var{g})
Increases the reference count by 1.
Not needed by most applications.
Return value:
On success, the grammar object it was called with;
@code{NULL} on failure.
@end deftypefun
@deftypefun void marpa_g_unref (Marpa_Grammar @var{g})
Decreases the reference count by 1,
destroying @var{g} once the reference count reaches
zero.
@end deftypefun
@node Symbols, Rules, Grammar reference counting, Grammar methods
@section Symbols
@deftypefun Marpa_Symbol_ID marpa_g_start_symbol (Marpa_Grammar @var{g})
Returns current value of the start symbol of grammar @var{g}.
The value is that
specified in the @code{marpa_g_start_symbol_set()} call,
if there has been one.
Return value:
On failure, @minus{}2;
@minus{}1 if there is no start symbol yet;
otherwise the ID of the new start symbol.
@end deftypefun
@deftypefun Marpa_Symbol_ID marpa_g_start_symbol_set ( @
Marpa_Grammar @var{g}, @
Marpa_Symbol_ID @var{sym_id})
Sets the start symbol of grammar @var{g} to symbol @var{id}.
Return value: On success, the ID of the new start symbol.
If @var{sym_id} is well-formed, but there is no
such symbol, -1.
On failure, @minus{}2.
@end deftypefun
@deftypefun int marpa_g_highest_symbol_id (Marpa_Grammar @var{g})
Return value: On success, the numerically largest symbol ID
of @var{g}.
On failure, @minus{}2.
@end deftypefun
@deftypefun int marpa_g_symbol_is_accessible (Marpa_Grammar @var{g}, @
Marpa_Symbol_ID @var{sym_id})
A symbol is @dfn{accessible} if it can be reached from the start symbol.
Return value: On success, 1 if symbol @var{sym_id} is accessible, 0 if not.
If @var{sym_id} is well-formed, but there is no
such symbol, -1.
If the grammar is not precomputed, or on other failure, @minus{}2.
@end deftypefun
@deftypefun int marpa_g_symbol_is_completion_event ( @
Marpa_Grammar @var{g}, @
Marpa_Symbol_ID @var{sym_id})
@deftypefunx int marpa_g_symbol_is_completion_event_set ( @
Marpa_Grammar @var{g}, @
Marpa_Symbol_ID @var{sym_id}, @
int @var{value})
Libmarpa can be set up to generate an
@code{MARPA_EVENT_SYMBOL_COMPLETED}
event whenever the symbol is completed.
A symbol is said to be @strong{completed}
when a non-nulling rule with
that symbol on its LHS is completed.
For completion events to occur, the symbol must be marked
as a completion event symbol.
The @code{marpa_g_symbol_is_completion_event_set()} function
marks symbol @var{sym_id} as a completion event symbol
if @var{value} is 1,
and unmarks it
it as a completion event symbol if @var{value} is 0.
The @code{marpa_g_symbol_is_completion_event()} method
returns the current value of the completion event marking
for symbol @var{sym_id}.
Nulled rules and symbols will never cause completion events.
Nullable symbols
may be marked as completion event symbols,
but this will have an effect only when the symbol
is not nulled.
Nulling symbols
may be marked as completion event symbols,
but no completion events will ever be generated
for a nulling symbol.
Note that this implies at no completion event will
ever be generated at earleme 0,
the start of parsing.
The completion event marking cannot be changed once
the grammar is precomputed.
However, if a symbol is marked as a completion event symbol,
it can be deactivated
and reactivated in the recognizer.
Success: On success, 1 if symbol @var{sym_id}
is a completion event symbol after the
call, 0 otherwise.
Failures:
If @var{sym_id} is well-formed, but there is no
such symbol, -1.
if the grammar @var{g} is precomputed;
or on other failure, @minus{}2.
@end deftypefun
@deftypefun int marpa_g_symbol_is_nulled_event ( @
Marpa_Grammar @var{g}, @
Marpa_Symbol_ID @var{sym_id})
@deftypefunx int marpa_g_symbol_is_nulled_event_set ( @
Marpa_Grammar @var{g}, @
Marpa_Symbol_ID @var{sym_id}, @
int @var{value})
Libmarpa can set up to generate
an @code{MARPA_EVENT_SYMBOL_NULLED}
event whenever the symbol is nulled.
A symbol is said to be @strong{nulled}
when a zero length instance of that symbol
is recognized.
For nulled events to occur, the symbol must be marked
as a nulled event symbol.
The @code{marpa_g_symbol_is_nulled_event_set()} function
marks symbol @var{sym_id} as a nulled event symbol
if @var{value} is 1,
and unmarks it
it as a nulled event symbol if @var{value} is 0.
The @code{marpa_g_symbol_is_nulled_event()} method
returns the current value of the nulled event marking
for symbol @var{sym_id}.
Note that Earleme 0 is a exception.
It is possible for a symbol to be nulled at
earleme 0, the start of parsing,
but no nulled symbol event will be generated.
When a symbol is recognized, it can generate
a nulled event or a completion event,
but recognition of a single instance of a symbol will
generate at most one or the other event -- never
both.
Symbols recognized
as zero length can only generate nulled events.
Symbols recognized
as non-zero in length can only generate completed events.
Zero length derivations can be ambiguous.
When a zero length symbol is recognized,
all of its zero-length derivations are also considered to be
recognized.
The nulled event marking cannot be changed once
the grammar is precomputed.
However, if a symbol is marked as a nulled event symbol,
it can be deactivated
and reactivated in the recognizer.
Success: On success, 1 if symbol @var{sym_id}
is a nulled event symbol after the
call, 0 otherwise.
Failures:
If @var{sym_id} is well-formed, but there is no
such symbol, -1.
if the grammar @var{g} is precomputed;
or on other failure, @minus{}2.
@end deftypefun
@deftypefun int marpa_g_symbol_is_nullable ( @
Marpa_Grammar g, Marpa_Symbol_ID sym_id)
A symbol is @dfn{nullable} if it sometimes produces the empty string.
A @strong{nulling} symbol is always a @strong{nullable} symbol,
but not all @strong{nullable} symbols are @strong{nulling} symbols.
Return value: On success, 1 if symbol @var{sym_id} is nullable, 0 if not.
If @var{sym_id} is well-formed, but there is no
such symbol, -1.
If the grammar is not precomputed, or on other failure, @minus{}2.
@end deftypefun
@deftypefun int marpa_g_symbol_is_nulling (Marpa_Grammar @var{g}, @
Marpa_Symbol_ID @var{sym_id})
A symbol is @dfn{nulling} if it always produces the empty string.
Return value: On success, 1 if symbol @var{sym_id} is nulling, 0 if not.
If @var{sym_id} is well-formed, but there is no
such symbol, -1.
If the grammar is not precomputed, or on other failure, @minus{}2.
@end deftypefun
@deftypefun int marpa_g_symbol_is_productive (Marpa_Grammar @var{g}, @
Marpa_Symbol_ID @var{sym_id})
A symbol is @dfn{productive} if it can produce a string of terminals.
All nullable symbols are considered productive.
Return value: On success, 1 if symbol @var{sym_id} is productive, 0 if not. If the grammar
If @var{sym_id} is well-formed, but there is no
such symbol, -1.
is not precomputed, or on other failure, @minus{}2.
@end deftypefun
@deftypefun int marpa_g_symbol_is_prediction_event ( @
Marpa_Grammar @var{g}, @
Marpa_Symbol_ID @var{sym_id})
@deftypefunx int marpa_g_symbol_is_prediction_event_set ( @
Marpa_Grammar @var{g}, @
Marpa_Symbol_ID @var{sym_id}, @
int @var{value})
Libmarpa can be set up
to generate a
@code{MARPA_EVENT_SYMBOL_PREDICTED}
event when a symbol is predicted.
A symbol is said to be @strong{predicted}
when a it is acceptable at the current
earleme according to the grammar.
Note that Earleme 0 is a exception.
Predicted symbol events are not generated
for symbols predicted at the start of parsing.
For predicted events to occur, the symbol must be marked
as a predicted event symbol.
The @code{marpa_g_symbol_is_predicted_event_set()} function
marks symbol @var{sym_id} as a predicted event symbol
if @var{value} is 1,
and unmarks it
it as a predicted event symbol if @var{value} is 0.
The @code{marpa_g_symbol_is_predicted_event()} method
returns the current value of the predicted event marking
for symbol @var{sym_id}.
The predicted event marking cannot be changed once
the grammar is precomputed.
However, if a symbol is marked as a predicted event symbol,
it can be deactivated
and reactivated in the recognizer.
Success: On success, 1 if symbol @var{sym_id}
is a predicted event symbol after the
call, 0 otherwise.
Failures:
If @var{sym_id} is well-formed, but there is no
such symbol, -1.
if the grammar @var{g} is precomputed;
or on other failure, @minus{}2.
@end deftypefun
@deftypefun int marpa_g_symbol_is_start ( Marpa_Grammar @var{g}, @
Marpa_Symbol_ID @var{sym_id})
This return value of this call indicates whether @var{sym_id}
is the start symbol.
Return value:
On success, 1 if @var{sym_id} is the start symbol;
0 otherwise.
If @var{sym_id} is well-formed, but there is no
such symbol, -1.
On other failure, @minus{}2.
@end deftypefun
@deftypefun int marpa_g_symbol_is_terminal ( @
Marpa_Grammar @var{g}, @
Marpa_Symbol_ID @var{sym_id})
@deftypefunx int marpa_g_symbol_is_terminal_set ( @
Marpa_Grammar @var{g}, @
Marpa_Symbol_ID @var{sym_id}, @
int @var{value})
These methods, respectively, set
and query the ``terminal status'' of a symbol.
To be used as an input symbol
in the @code{marpa_r_alternative()} method,
a symbol must be a terminal.
This function flags symbol @var{sym_id} as a terminal if
@var{value} is 1,
or flags it as a non-terminal if @var{value} is 0.
Once set to a value with the
@code{marpa_g_symbol_is_terminal_set()} method,
the terminal status of a symbol is ``locked'' at that value.
A subsequent call to
@code{marpa_g_symbol_is_terminal_set()} that attempts
to change the terminal status
of @var{sym_id} to a value different from its current one
will fail.
The error code will be @code{MARPA_ERR_TERMINAL_IS_LOCKED}.
By default, a symbol is a terminal if and only if it
does not appear on the LHS of any rule.
An attempt to flag a nulling symbol
as a terminal will cause a failure,
but this is not necesssarily detected before precomputation.
Return value: On success, 1 if symbol @var{sym_id}
is a terminal symbol after the
call, 0 otherwise.
If @var{sym_id} is well-formed, but there is no
such symbol, -1.
If @var{value} is not 0 or 1;
if the terminal status is locked and @var{value}
is different from its current value;
if the grammar @var{g} is precomputed;
or on other failure, @minus{}2.
@end deftypefun
@deftypefun Marpa_Symbol_ID marpa_g_symbol_new (Marpa_Grammar @var{g})
Creates a new symbol.
The symbol ID will be a non-negative integer.
Return value: On success, the ID of a new symbol;
On failure, @minus{}2.
@end deftypefun
@node Rules, Sequences, Symbols, Grammar methods
@section Rules
@deftypefun int marpa_g_highest_rule_id (Marpa_Grammar @var{g})
Return value: On success, the numerically largest rule ID
of @var{g}.
On failure, @minus{}2.
@end deftypefun
@deftypefun int marpa_g_rule_is_accessible (Marpa_Grammar @var{g}, @
Marpa_Rule_ID @var{rule_id})
A rule is @dfn{accessible} if it can be reached from the start symbol.
A rule is accessible if and only if its LHS symbol is accessible.
The start rule is always an accessible rule.
Return value: On success, 1 if rule @var{rule_id}
is accessible, 0 if not.
If @var{rule_id} is well-formed, but there is no
such rule, -1.
If the grammar
is not precomputed, or on other failure, @minus{}2.
@end deftypefun
@deftypefun int marpa_g_rule_is_nullable ( @
Marpa_Grammar @var{g}, Marpa_Rule_ID @var{ruleid})
A rule is @dfn{nullable} if it sometimes produces the empty string.
A @strong{nulling} rule is always a @strong{nullable} rule,
but not all @strong{nullable} rules are @strong{nulling} rules.
Return value: On success,
1 if rule @var{ruleid} is nullable, 0 if not.
If @var{rule_id} is well-formed, but there is no
such rule, -1.
If the grammar is not precomputed, or on other failure, @minus{}2.
@end deftypefun
@deftypefun int marpa_g_rule_is_nulling (Marpa_Grammar @var{g}, @
Marpa_Rule_ID @var{ruleid})
A rule is @dfn{nulling} if it always produces the empty string.
Return value: On success,
1 if rule @var{ruleid} is nulling, 0 if not.
If @var{rule_id} is well-formed, but there is no
such rule, -1.
If the grammar is not precomputed, or on other failure, @minus{}2.
@end deftypefun
@deftypefun int marpa_g_rule_is_loop (Marpa_Grammar @var{g}, @
Marpa_Rule_ID @var{rule_id})
A rule is a loop rule if it non-trivially
produces the string of length one
which consists only of its LHS symbol.
Such a derivation takes the parse back to where
it started, hence the term ``loop''.
``Non-trivially'' means the zero-step derivation does not count --- the
derivation must have at least one step.
The presence of a loop rule makes a grammar infinitely ambiguous,
and applications will typically want to treat them as fatal errors.
But nothing forces an application to do this,
and Marpa will successfully parse and evaluate grammars with
loop rules.
Return value: On success,
1 if rule @var{rule_id} is a loop rule, 0 if not.
If @var{rule_id} is well-formed, but there is no
such rule, -1.
If the grammar
is not precomputed, or on other failure, @minus{}2.
@end deftypefun
@deftypefun int marpa_g_rule_is_productive (Marpa_Grammar @var{g}, @
Marpa_Rule_ID @var{rule_id})
A rule is @dfn{productive} if it can produce a string of terminals.
An rule is productive if and only if all the symbols on
its RHS are productive.
The empty string counts as a string of terminals,
so that a nullable rule is always a productive rule.
For that same reason,
an empty rule is considered productive.
Return value: On success,
1 if rule @var{rule_id} is productive, 0 if not.
If @var{rule_id} is well-formed, but there is no
such rule, -1.
If the grammar is not precomputed, or on other failure, @minus{}2.
@end deftypefun
@deftypefun int marpa_g_rule_length ( @
Marpa_Grammar @var{g}, @
Marpa_Rule_ID @var{rule_id})
The length of a rule is the number of symbols on its RHS.
Return value: On success, the length of rule @var{rule_id}.
On failure, @minus{}2.
@end deftypefun
@deftypefun Marpa_Symbol_ID marpa_g_rule_lhs ( @
Marpa_Grammar @var{g}, @
Marpa_Rule_ID @var{rule_id})
Return value: On success, the LHS symbol of rule @var{rule_id}.
If @var{rule_id} is well-formed, but there is no
such rule, -1.
On other failure, @minus{}2.
@end deftypefun
@deftypefun Marpa_Rule_ID marpa_g_rule_new (Marpa_Grammar @var{g}, @
Marpa_Symbol_ID @var{lhs_id}, @
Marpa_Symbol_ID *@var{rhs_ids}, @
int @var{length})
Creates a new external BNF rule in grammar @var{g}.
The ID of the new rule will be a non-negative integer,
which will be unique to that rule.
In addition to BNF rules, Marpa also allows sequence rules,
which are created by another method:
@code{marpa_g_sequence_new()}.
Sequence rules and BNF rules are numbered in the same series,
so that a BNF rule will never have the same rule ID as a sequence
rule, and vice versa.
The LHS symbol is @var{lhs_id},
and there are @var{length} symbols on the RHS.
The RHS symbols are in an array
pointed to by @var{rhs_ids}.
Possible failures, with their error codes, include:
@itemize
@item @code{MARPA_ERR_SEQUENCE_LHS_NOT_UNIQUE}: The LHS symbol is the same
as that of a sequence rule.
@item @code{MARPA_ERR_DUPLICATE_RULE}: The new rule would duplicate another BNF
rule.
Another BNF rule is considered the duplicate of the new one,
if its LHS symbol is the same as symbol @var{lhs_id},
if its length is the same as @var{length},
and if its RHS symbols match one for one those
in the array of symbols @var{rhs_ids}.
@end itemize
Return value: On success, the ID of new external rule.
On failure, @minus{}2.
@end deftypefun
@deftypefun Marpa_Symbol_ID marpa_g_rule_rhs ( @
Marpa_Grammar @var{g}, @
Marpa_Rule_ID @var{rule_id}, @
int @var{ix})
Returns the ID of the symbol in position @var{ix}
in the RHS of rule @var{rule_id}.
The RHS position, @var{ix}, is zero-based.
Return value: On success,
the ID of the symbol in position @var{ix}
on the rules RHS.
If @var{rule_id} is well-formed, but there is no
such rule, -1.
If @var{ix} is greater than or equal to the length of
the rule,
or on other failure, @minus{}2.
@end deftypefun
@node Sequences, Ranks, Rules, Grammar methods
@section Sequences
@deftypefun int marpa_g_rule_is_proper_separation ( @
Marpa_Grammar @var{g}, @
Marpa_Rule_ID @var{rule_id})
Return value:
On success, if rule @var{rule_id} is not a sequence rule, 0.
On success, if rule @var{rule_id}
is a sequence rule where the proper separation flag
is set, 1.
On success, if rule @var{rule_id}
is a sequence rule where the proper separation flag
is @strong{not} set, 0.
If @var{rule_id} is well-formed, but there is no
such rule, -1.
On failure, @minus{}2.
@end deftypefun
@deftypefun int marpa_g_sequence_min ( @
Marpa_Grammar @var{g}, @
Marpa_Rule_ID @var{rule_id})
Returns the mininum length of a sequence rule.
This accessor can be also be used to test whether
or not a rule is a sequence rule.
@minus{}1 is returned if and only if the rule is valid
but not a sequence rule.
Return value:
If rule @var{rule_id} is a sequence rule, its minimum length.
If rule @var{rule_id} is valid, but is
not the rule ID of sequence rule, @minus{}1.
If @var{rule_id} is well-formed, but there is no
such rule,
or on other failure, @minus{}2.
@end deftypefun
@deftypefun Marpa_Rule_ID marpa_g_sequence_new (Marpa_Grammar @var{g}, @
Marpa_Symbol_ID @var{lhs_id}, @
Marpa_Symbol_ID @var{rhs_id}, @
Marpa_Symbol_ID @var{separator_id}, @
int @var{min}, @
int @var{flags} )
Adds a new sequence rule to grammar @var{g}.
The ID of the new sequence rule will be a non-negative integer,
which is unique to that rule.
Sequence rules and BNF rules are numbered in the same series,
so that a BNF rule will never have the same rule ID as a sequence
rule, and vice versa.
The sequence is @var{lhs_id},
and the item to be repeated in the sequence is @var{rhs_id}.
The sequence must be repeated at least @var{min} times,
where @var{min} is 0 or 1.
If @var{separator_id} is non-negative,
it is a separator symbol.
If @code{flags & MARPA_PROPER_SEPARATION} is non-zero,
separation is ``proper'', that is,
a trailing separator is not allowed.
The term @dfn{proper} is based on the idea that
properly-speaking, separators should actually separate items.
Some higher-level Marpa interfaces offer the ability to
discard separators in the semantics,
and in fact will do this by default.
At the Libmarpa level, sequences always ``keep
separators''.
It is up to the programmer to arrange
to discard separators,
if that is what is desired.
The sequence RHS, or item,
is restricted to a single symbol,
and that symbol cannot be nullable.
If @var{separator_id} is a symbol, it also cannot
be a nullable symbol.
Nullables on the RHS of sequences are restricted
because they lead to highly ambiguous grammars.
Grammars of this kind are allowed by Libmarpa, but
they must be expressed using BNF rules, not sequence rules.
This is for two reasons:
First, sequence optimizations would not work
in the presence of nullables.
Second, since it is not completely clear what
an application intends
when it asks for a sequence of identical items,
some of which are nullable,
the user's intent can be more clearly expressed
directly in BNF.
The LHS symbol cannot be the LHS of any other rule,
whether a BNF rule or a sequence rule.
On an attempt to create an sequence rule with a duplicate
LHS,
@code{marpa_g_sequence_new()} fails,
setting the error code to
@code{MARPA_ERR_SEQUENCE_LHS_NOT_UNIQUE}.
Sequence rules do not add to the classes of grammar parsed
by Libmarpa ---
a sequence can always be written as BNF rules.
When a rule is created
with the @code{marpa_g_sequence_new()} method,
Libmarpa will understand that it is a sequence,
and will optimize accordingly.
The speedup is often considerable.
Return value: On success, the ID of the external rule.
On failure, @minus{}2.
@end deftypefun
@deftypefun int marpa_g_sequence_separator ( @
Marpa_Grammar @var{g}, @
Marpa_Rule_ID @var{rule_id})
Return value:
If rule @var{rule_id} is a sequence rule, its separator.
If rule @var{rule_id} is a sequence rule,, but there is no
separator, @minus{}1.
If @var{rule_id} is
not a sequence rule,
does not exist
or is not well-formed;
or on other failure, @minus{}2.
@end deftypefun
@deftypefun int marpa_g_symbol_is_counted (Marpa_Grammar @var{g}, @
Marpa_Symbol_ID @var{sym_id})
A symbol is @dfn{counted}
if it appears on the RHS of a sequence rule,
or if it is used as
the separator symbol of a sequence rule.
Return value: On success,
1 if symbol @var{sym_id} is counted, 0 if not.
On failure, @minus{}2.
@end deftypefun
@node Ranks, Grammar precomputation, Sequences, Grammar methods
@section Ranks
@deftypefun Marpa_Rank marpa_g_rule_rank ( @
Marpa_Grammar @var{g}, @
Marpa_Rule_ID rule_id)
@deftypefunx Marpa_Rank marpa_g_rule_rank_set ( @
Marpa_Grammar @var{g}, @
Marpa_Rule_ID @var{rule_id}, @
Marpa_Rank @var{rank})
These methods, respectively, set
and query the rank of a rule @var{rule_id}.
When @var{rule_id} is created, its rank
initialized to the default rank of the grammar.
Initially, the default rank of the grammar is 0.
Methods to reset the grammar's default rank
are currently in the experimental stage.
Return value: On success, returns
the rank @strong{after}
the call,
and sets the error code to
@code{MARPA_ERR_NONE}.
On failure, returns @minus{}2,
and sets the error code to an appropriate
value, which will never be
@code{MARPA_ERR_NONE}.
Note that when the rank is @minus{}2,
the error code is the only way to distinguish
success from failure.
The error code can be determined by using the
@code{marpa_g_error()} call.
@end deftypefun
@deftypefun int marpa_g_rule_null_high ( @
Marpa_Grammar @var{g}, @
Marpa_Rule_ID rule_id)
@deftypefunx int marpa_g_rule_null_high_set ( @
Marpa_Grammar @var{g}, @
Marpa_Rule_ID @var{rule_id}, @
int @var{flag})
These methods, respectively, set
and discover the ``null ranks high'' setting of the rule @var{rule_id}.
The ``null ranks high'' setting is either 0 or 1.
When @var{rule_id} is created, its ``null ranks high''
setting is initialized to 0.
The ``null ranks high'' setting affects the ranking of rules
with properly nullable symbols on their right hand side.
If a rule has properly nullable symbols on its RHS,
each instance in which it appears in a parse will have a pattern
of nulled and non-nulled symbols.
Such a pattern is called a ``null variant''.
If the ``null ranks high'' setting is 0 (the default),
nulled symbols rank low.
If the ``null ranks high'' setting is 1,
nulled symbols rank high.
Ranking of a null variants is done from left-to-right.
Return value: On success, the setting
of the ``null ranks high'' flag @strong{after}
the call.
If the grammar has been precomputed,
or on other failure, @minus{}2.
@end deftypefun
@node Grammar precomputation, , Ranks, Grammar methods
@section Precomputing the Grammar
@deftypefun int marpa_g_precompute (Marpa_Grammar @var{g})
@anchor{marpa_g_precompute}
Precomputation is necessary for a recognizer to be generated
from a grammar.
On success, @code{marpa_g_precompute} returns a non-negative
number to indicate that it precomputed the grammar without
issues.
On failure, @code{marpa_g_precompute} returns @minus{}2.
Precomputation may return one or more events,
which may be queried using the
@code{marpa_g_event()} method.
At this point events only occur when failure is reported,
and events always report issues.
But application writers should expect future versions
to have events which are reported on success,
as well as events which do not represent issues.
A @code{MARPA_EVENT_LOOP_RULES} event occurs
when there are infinite loop rules (cycles)
in the grammar.
The presence of one or more of these will cause failure
to be reported,
but will not prevent the grammar from being precomputed.
Each @code{MARPA_EVENT_COUNTED_NULLABLE} event is a symbol
which is a nullable on the right hand side of a sequence
rule --- a ``counted'' symbol.
The presence of one or more of these will cause failure
to be reported,
and will prevent the grammar from being precomputed.
So that the programmer can fix several at once,
these failures are delayed until events are created
for all of the counted nullables.
Each @code{MARPA_EVENT_NULLING_TERMINAL} event is a nulling
symbol which is also flagged as a terminal.
Since terminals cannot be of zero length, this is a logical
impossibility.
The presence of one or more of these will cause failure
to be reported,
and will prevent the grammar from being precomputed.
So that the programmer can fix several at once,
the failure is delayed until events are created
for all of the counted nullables.
Precomputation involves freezing
and then thoroughly checking the grammar.
Among the reasons for precomputation to fail
are the following:
@itemize
@item @code{MARPA_ERR_NO_RULES}: The grammar has no rules.
@item @code{MARPA_ERR_NO_START_SYMBOL}: No start symbol was specified.
@item @code{MARPA_ERR_INVALID_START_SYMBOL}: A start symbol ID was specified, but it
is not the ID of a valid symbol.
@item @code{MARPA_ERR_START_NOT_LHS}: The start symbol is not on the LHS of any rule.
@item @code{MARPA_ERR_UNPRODUCTIVE_START}: The start symbol is not productive.
@item @code{MARPA_ERR_COUNTED_NULLABLE}: A symbol on the RHS of a sequence rule is
nullable.
Libmarpa does not allow this.
@item @code{MARPA_ERR_NULLING_TERMINAL}: A terminal is also a nulling symbol.
Libmarpa does not allow this.
@end itemize
More details of these can be found under the
description of the appropriate code.
@xref{External error codes}.
@code{marpa_g_precompute()} is unusual in that it
is possible to treat one of its failures as ``advisory'',
and to proceed with parsing.
If
@code{marpa_g_precompute()} fails with an error code
of @code{MARPA_ERR_GRAMMAR_HAS_CYCLE},
parsing can proceed, just as it typically would for
success.
The grammar will have been precomputed, as
calling the @code{marpa_g_is_precomputed()} method
will confirm.
Most applications, however,
will want to simply treat failure
with @code{MARPA_ERR_GRAMMAR_HAS_CYCLE},
as simply another failure,
and fix the cycles before parsing.
Cycles make a grammar infinitely ambiguous,
and are considered useless in current
practice.
Cycles make processing the grammar less
efficient, sometimes considerably so.
Detection of cycles is returned as failure
because that is by far the convenient thing to do
for the vast majority of applications.
Return value: On success, a non-negative number.
On failure, @minus{}2.
@end deftypefun
@deftypefun int marpa_g_is_precomputed (Marpa_Grammar @var{g})
Return value: On success, 1
if grammar @var{g} is already precomputed,
0 otherwise.
On failure, @minus{}2.
@end deftypefun
@deftypefun int marpa_g_has_cycle (Marpa_Grammar @var{g})
This function allows the application to determine if grammar
@var{g} has a cycle.
As mentioned, most applications will want to treat these
as fatal errors.
To determine which rules are in the cycle,
@code{marpa_g_rule_is_loop()} can be used.
Return value: On success, 1 if the grammar has a cycle,
0 otherwise.
On failure, @minus{}2.
@end deftypefun
@node Recognizer methods, Progress reports, Grammar methods, Top
@chapter Recognizer methods
@menu
* Recognizer overview::
* Recognizer constructor::
* Recognizer reference counting::
* Recognizer life cycle mutators::
* Location accessors::
* Other parse status methods::
* Untested recognizer methods::
@end menu
@node Recognizer overview, Recognizer constructor, Recognizer methods, Recognizer methods
@section Overview
An archtypal application uses a recognizer to read input.
To create a recognizer, use the @code{marpa_r_new()} method.
When a recognizer is no longer in use, its memory can be freed
using the
@code{marpa_r_unref()} method.
To make a recognizer ready for input,
use the @code{marpa_r_start_input()} method.
The recognizer starts with its current earleme
at location 0.
To read a token at the current earleme,
use the @code{marpa_r_alternative()} call.
To complete the processing of the current earleme,
and move forward to a new one,
use the @code{marpa_r_earleme_complete()} call.
@node Recognizer constructor, Recognizer reference counting, Recognizer overview, Recognizer methods
@section Creating a new recognizer
@deftypefun Marpa_Recognizer marpa_r_new ( Marpa_Grammar @var{g} )
Creates a new recognizer.
The reference count of the recognizer will be 1.
The reference count of @var{g},
the base grammar,
will be incremented by one.
Return value: On success, the newly created recognizer.
If @var{g} is not precomputed, or on other failure, @code{NULL}.
@end deftypefun
@node Recognizer reference counting, Recognizer life cycle mutators, Recognizer constructor, Recognizer methods
@section Keeping the reference count of a recognizer
@deftypefun Marpa_Recognizer marpa_r_ref (Marpa_Recognizer @var{r})
Increases the reference count by 1.
Not needed by most applications.
Return value:
On success, the recognizer object, @var{r}.
On failure, @code{NULL}.
@end deftypefun
@deftypefun void marpa_r_unref (Marpa_Recognizer @var{r})
Decreases the reference count by 1,
destroying @var{r} once the reference count reaches
zero.
When @var{r} is destroyed, the reference count
of its base grammar is decreased by one.
If this takes the reference count of the base grammar
to zero, it too is destroyed.
@end deftypefun
@node Recognizer life cycle mutators, Location accessors, Recognizer reference counting, Recognizer methods
@section Life cycle mutators
@deftypefun int marpa_r_start_input (Marpa_Recognizer @var{r})
Makes @var{r} ready to accept input.
Return value: On success, a non-negative value.
On failure, @minus{}2.
@end deftypefun
@deftypefun int marpa_r_alternative (Marpa_Recognizer @var{r}, @
Marpa_Symbol_ID @var{token_id}, @
int @var{value}, @
int @var{length})
Reads a token into @var{r}.
The token will start at the current earleme.
Libmarpa allows tokens to be ambiguous, to be of
variable length and to overlap.
@var{token_id} is the symbol ID of the token,
which must be a terminal.
@var{length} is the length of the token.
@var{value} is an
integer that represents the value of the
token.
In applications where the token's actual value is not an integer, it is
expected that the application will use this value as
a ``virtual'' value,
perhaps finding the actual value by using @var{value}
to index an array.
@var{value} is not used inside Libmarpa --- it is simply
stored to be returned by the valuator
as a convenience for the application.
Some applications may prefer to track token values on
their own, perhaps based on
the earleme location and @var{token_id},
instead of using Libmarpa's token values.
A @var{value} of 0 is reserved for a now-deprecated feature.
Do not use it.
For more details on that feature, see the
section @ref{Valued and unvalued symbols}.
When @code{marpa_r_alternative()}
is successful,
the value of the furthest earleme is set to
the greater of its value before the call,
and @var{current}+@var{length},
where @var{current} is the value of the current earleme.
The values of the current and latest earlemes
are unchanged by
calls to @code{marpa_r_alternative()}.
Several error codes leave the recognizer in a fully
recoverable state, allowing the application to
retry the @code{marpa_r_alternative()} method.
Retry is efficient, and quite useable as a parsing
technique.
The error code
of primary interest from this point of view
is @code{MARPA_ERR_UNEXPECTED_TOKEN_ID},
which indicates that the token was not accepted
because of its token ID.
Retry after this condition is used in several
applications,
and is called ``the Ruby Slippers technique''.
The error codes
@code{MARPA_ERR_DUPLICATE_TOKEN},
@code{MARPA_ERR_NO_TOKEN_EXPECTED_HERE}
and @code{MARPA_ERR_INACCESSIBLE_TOKEN}
also leave the recognizer in a fully recoverable
state, and may also be useable for the
Ruby Slippers or similar techniques.
At this writing,
the author knows of no applications which
attempt to recover from these errors.
Return value: On success, @code{MARPA_ERR_NONE}.
On failure, some other error code.
@end deftypefun
@deftypefun Marpa_Earleme marpa_r_earleme_complete (Marpa_Recognizer @var{r})
This method does the final processing for the current earleme.
It then advances the current earleme by one.
Note that @code{marpa_r_earleme_complete()} may be called
even when no tokens have been read at the current earleme ---
in the character-per-earleme input model, for example, tokens
can span many characters and, if the input is unambiguous over that
span, there will be no other tokens that start inside it.
As mentioned,
@code{marpa_r_earleme_complete()} always advances the current earleme,
incrementing its value by 1.
This means that value of the current earleme after the call
will be the one plus the value of the earleme processed by the call
to @code{marpa_r_earleme_complete()}.
If any token was accepted at the earleme being processed,
@code{marpa_r_earleme_complete()} creates a new Earley set
which will be the latest Earley set
and, after the call, the latest
earleme will be equal to the new current earleme.
If no token was accepted at the
earleme being processed,
no Earley set is created,
and the value of the latest earleme remains unchanged.
The value of the furthest earleme is never changed by
a call to @code{marpa_r_earleme_complete()}.
During this method, one or more events may occur.
On success, this function returns the number of events
generated,
but it is important to note that events may be
created whether earleme completion fails or succeeds.
When this method fails,
the application must call @code{marpa_g_event()}
if it wants to determine if any events occurred.
Since the reason for failure to complete an earleme is often
detailed in the events, applications that fail will often
be at least as interested in the events as those
that succeed.
The @code{MARPA_EVENT_EARLEY_ITEM_THRESHOLD} event
indicates that an application-settable threshold
on the number of Earley items has been reached or exceeded.
What this means depends on the application,
but when the default threshold is exceeded,
it means that it is very likely
that the time and space resources consumed by
the parse will prove excessive.
A parse is ``exhausted'' when it can accept no more
input.
This can happen both on success and on failure.
Note that the failure due to parse exhaustion
only means failure
at the current earleme.
There may be successful parses
at earlier earlemes.
If a parse is exhausted, but successful,
an event with the event code
@code{MARPA_EVENT_EXHAUSTED} occurs.
Because the parse is exhausted,
no input will be accepted at later earlemes.
It is quite common for a parse to become exhausted
when it succeeds.
Many practical grammars are designed so that a
successful parse cannot be extended.
An exhausted parse may cause a failure,
in which case
@code{marpa_r_earleme_complete()} returns an
error
whose error code is @code{MARPA_ERR_PARSE_EXHAUSTED}.
For a parse to fail at an earleme due to exhaustion,
it must be the case that no alternatives were
accepted at that earleme.
In fact,
in the standard input model,
a failure due to parse exhaustion
occurs if and only if
no alternatives
were accepted at the current earleme.
The circumstances under which
failure due to parse exhaustion occurs
are slightly more complicated
when variable length tokens are in use.
Informally,
a parse will never fail due to exhaustion as long
as it is possible that a token
ending at some future
earleme will continue the parse.
More precisely,
a call to
@code{marpa_r_earleme_complete()} fails
due to parse exhaustion
if and only if, first,
no alternatives were added at the current earleme
and, second,
that call left the current
earleme equal to the furthest earleme.
Return value: On success, the number of events generated.
On failure, @minus{}2.
@end deftypefun
@node Location accessors, Other parse status methods, Recognizer life cycle mutators, Recognizer methods
@section Location accessors
@deftypefun @code{unsigned int} marpa_r_current_earleme (Marpa_Recognizer @var{r})
Return value: If input has started, the current earleme.
If input has not started, @minus{}1.
Always succeeds.
@end deftypefun
@deftypefun Marpa_Earleme marpa_r_earleme ( @
Marpa_Recognizer @var{r}, @
Marpa_Earley_Set_ID @var{set_id})
In the default, token-stream model, Earley set ID and earleme
are always equal, but this is not the case in other input
models.
(The ID of an Earley set ID is also called its ordinal.)
If there is no Earley set whose ID is
@var{set_id},
@code{marpa_r_earleme()} fails.
If @var{set_id} was negative,
the error code is set to
@code{MARPA_ERR_INVALID_LOCATION}.
If @var{set_id} is greater than the ordinal
of the latest Earley set,
the error code is set to
@code{MARPA_ERR_NO_EARLEY_SET_AT_LOCATION}.
At this writing, there is no method for
the inverse operation (conversion of an earleme to an Earley set
ID).
One consideration in writing
such a method is that not all earlemes correspond to Earley sets.
Applications that want to map earlemes
to Earley sets will have no trouble if they
are using the standard input model ---
the Earley set
ID is always exactly equal to the earleme in that model.
For other applications
that want an earleme-to-ID mapping,
the most general method is create an ID-to-earleme
array using the @code{marpa_r_earleme()} method
and invert it.
Return value:
On success,
the earleme corresponding to Earley
set @var{set_id}.
On failure, @minus{}2.
@end deftypefun
@deftypefun int marpa_r_earley_set_value (@
Marpa_Recognizer @var{r}, @
Marpa_Earley_Set_ID earley_set)
Returns the integer value of @var{earley_set}.
For more details, see
the description of @code{marpa_r_earley_set_values()}.
Return value: On success, the value of @var{earley_set}.
On failure, @minus{}2.
@end deftypefun
@deftypefun int marpa_r_earley_set_values (@
Marpa_Recognizer @var{r}, @
Marpa_Earley_Set_ID earley_set, @
int* p_value, @
void** p_pvalue @
)
If @var{p_value} is non-zero,
sets the location pointed to by
@var{p_value} to the integer value of the Earley set.
Similarly, if @var{p_pvalue} is non-zero,
sets the location pointed to by
@var{p_pvalue} to the pointer value of the Earley set.
The ``value'' and ``pointer'' of an Earley set are
an arbitrary integer
and an arbitrary pointer
that the application
can use for its own purposes.
In character-per-earleme input models, for example,
the integer can be the codepoint of the current character.
In a traditional token-per-earley input model,
they could be used to indicate the string value of the token --
the pointer could point to the start of the string,
and the integer could indicate its length.
The Earley set value and pointer can be set using
the
@code{marpa_r_latest_earley_set_values_set()} method.
The Earley set integer value defaults to @minus{}1,
and the pointer value defaults to @code{NULL}.
Return value: On success, returns a non-negative integer.
On failure, returns @minus{}2.
@end deftypefun
@deftypefun @code{unsigned int} marpa_r_furthest_earleme (Marpa_Recognizer @var{r})
Return value: On success, the furthest earleme.
Always succeeds.
@end deftypefun
@deftypefun Marpa_Earley_Set_ID marpa_r_latest_earley_set (Marpa_Recognizer @var{r})
This method returns the Earley set ID (ordinal) of the latest Earley set.
Applications that want the
value of the latest earleme can convert
this value using
the @code{marpa_r_earleme()} method.
Return value: On success, the ID of the latest Earley set.
Always succeeds.
@end deftypefun
@deftypefun int marpa_r_latest_earley_set_value_set (@
Marpa_Recognizer @var{r}, @
int value)
Sets the integer value of the latest Earley set.
For more details, see
the description of
@code{marpa_r_latest_earley_set_values_set()}.
Return value: On success, the new value of @var{earley_set}.
On failure, @minus{}2.
@end deftypefun
@deftypefun int marpa_r_latest_earley_set_values_set (@
Marpa_Recognizer @var{r}, @
int value, @
void* pvalue)
Sets the integer and pointer value of the latest Earley set.
For more about the ``integer value'' and ``pointer value''
of an Earley set,
see the description of the
@code{marpa_r_earley_set_values()} method.
Return value: On success, returns a non-negative integer.
On failure, returns @minus{}2.
@end deftypefun
@node Other parse status methods, Untested recognizer methods, Location accessors, Recognizer methods
@section Other parse status methods
@deftypefun int marpa_r_earley_item_warning_threshold (Marpa_Recognizer @var{r})
@deftypefunx int marpa_r_earley_item_warning_threshold_set (Marpa_Recognizer @var{r}, @
int @var{threshold})
These methods, respectively, report and set the Earley item warning threshold.
The @dfn{Earley item warning threshold}
is a number that is compared with
the count of Earley items in each Earley set.
When it is matched or exceeded,
a @code{MARPA_EVENT_EARLEY_ITEM_THRESHOLD} event is created.
If @var{threshold} is zero or less,
an unlimited number of Earley items
will be allowed without warning.
This will rarely be what the user wants.
By default, Libmarpa calculates a value based on the grammar.
The formula Libmarpa uses is the result of some experience,
and most applications will
be happy with it.
Return value:
The value that the Earley item warning threshold has
after the method call is finished.
Always succeeds.
@end deftypefun
@deftypefun int marpa_r_expected_symbol_event_set ( @
Marpa_Recognizer @var{r}, @
Marpa_Symbol_ID @var{symbol_id}, @
int @var{value})
Sets the ``expected symbol event bit'' for @var{symbol_id} to @var{value}.
A recognizer event is created whenever
symbol @var{symbol_id} is expected at the current earleme.
if and only if the expected symbol event bit
for @var{symbol_id} is 1.
The ``expected symbol event bit'' must be 1 or 0.
In this context, ``expected'' means ``expected as a terminal''.
Even if a symbol is predicted at the current earleme,
if it is not acceptable as a terminal,
it does not trigger an
``expected symbol event''.
By default, the ``expected symbol event bit'' is 0.
It is an error to attempt to set the
``expected symbol event bit'' to 1 for a nulling symbol,
an inaccessible symbol,
or an unproductive symbol.
Return value:
The value of the event bit after the method call is finished.
-2 if @var{symbol_id} is not the ID of a valid symbol;
if it is the ID of an nulling, inaccessible for unproductive symbol;
or on other failure.
@end deftypefun
@deftypefun int marpa_r_is_exhausted (Marpa_Recognizer @var{r})
A parser is ``exhausted'' if it cannot accept any more input.
Both successful and failed parses can be exhausted.
In many grammars,
the parse is always exhausted as soon as it succeeds.
Good parses may also exist at earlemes prior to the
current one.
Return value:
1 if the parser is exhausted, 0 otherwise.
Always succeeds.
@end deftypefun
@deftypefun int marpa_r_terminals_expected ( @
Marpa_Recognizer @var{r}, @
Marpa_Symbol_ID* @var{buffer})
Returns a list of the ID's of the symbols
that are acceptable as tokens
at the current earleme.
@var{buffer} is expected to be large enough to hold
the result.
This is guaranteed to be the case if the buffer
is large enough to hold a number of
@code{Marpa_Symbol_ID}'s that
is greater than or equal to the number of symbols
in the grammar.
Return value: On success, the number of @code{Marpa_Symbol_ID}'s
in @var{buffer}.
On failure, @minus{}2.
@end deftypefun
@deftypefun int marpa_r_terminal_is_expected ( @
Marpa_Recognizer @var{r}, @
Marpa_Symbol_ID @var{symbol_id})
Return values on success:
If @var{symbol_id} is the
ID of a valid terminal symbol that is expected at
the current earleme,
a number greater than zero.
If @var{symbol_id} is the
ID of a valid terminal symbol that is
@strong{not} expected at
the current earleme, or
if @var{symbol_id} is the ID of a valid symbol
that is not a terminal, zero.
Failure cases:
Returns @minus{}2 on failure.
It is a failure
if @var{symbol_id} is not the ID of a valid
symbol.
@end deftypefun
@node Untested recognizer methods, , Other parse status methods, Recognizer methods
@section Untested recognizer methods
@deftypefun int marpa_r_completion_symbol_activate ( @
Marpa_Recognizer @var{r}, @
Marpa_Symbol_ID @var{sym_id}, @
int @var{reactivate} )
Allows the user to deactivate and reactivate symbol completion
events in the recognizer.
If @var{reactivate} is zero, the event is deactivated.
If @var{reactivate} is one, the event is activated.
Symbol completion events are active by default if
the symbol was set up for completion events in the
grammar.
If a symbol was not set up for completion events in
the grammar, symbol completion events are inactive
by default and any attempt to change that is a fatal error.
Success cases:
On success,
the method returns the value of @var{reactivate}.
The method succeeds trivially
if the symbol is already set as indicated by
@var{reactivate}.
Failure cases:
If the active status
of the
completion event for @var{sym_id}
cannot be set as indicated by @var{reactivate},
the method fails.
On failure, @minus{}2 is returned.
@end deftypefun
@deftypefun int marpa_r_nulled_symbol_activate ( @
Marpa_Recognizer @var{r}, @
Marpa_Symbol_ID @var{sym_id}, @
int @var{boolean} )
Allows the user to deactivate and reactivate symbol nulled
events in the recognizer.
If @var{boolean} is zero, the event is deactivated.
If @var{boolean} is one, the event is activated.
Symbol nulled events are active by default if
the symbol was set up for nulled events in the
grammar.
If a symbol was not set up for nulled events in
the grammar, symbol nulled events are inactive
by default and any attempt to change that is a fatal error.
Success cases:
On success,
the method returns the value of @var{boolean}.
The method succeeds trivially
if the symbol is already set as indicated by
@var{boolean}.
Failure cases:
If the active status
of the
nulled event for @var{sym_id}
cannot be set as indicated by @var{boolean},
the method fails.
On failure, @minus{}2 is returned.
@end deftypefun
@deftypefun int marpa_r_prediction_symbol_activate ( @
Marpa_Recognizer @var{r}, @
Marpa_Symbol_ID @var{sym_id}, @
int @var{boolean} )
Allows the user to deactivate and reactivate symbol prediction
events in the recognizer.
If @var{boolean} is zero, the event is deactivated.
If @var{boolean} is one, the event is activated.
Symbol prediction events are active by default if
the symbol was set up for prediction events in the
grammar.
If a symbol was not set up for prediction events in
the grammar, symbol prediction events are inactive
by default and any attempt to change that is a fatal error.
Success cases:
On success,
the method returns the value of @var{boolean}.
The method succeeds trivially
if the symbol is already set as indicated by
@var{boolean}.
Failure cases:
If the active status
of the
prediction event for @var{sym_id}
cannot be set as indicated by @var{boolean},
the method fails.
On failure, @minus{}2 is returned.
@end deftypefun
@node Progress reports, Bocage methods, Recognizer methods, Top
@chapter Progress reports
An important advantage of the Marpa algorithm is the ability
to easily get full information about the state of the parse.
To start a progress report,
use the @code{marpa_r_progress_report_start()} command.
Only one progress report can be in use at any one time.
To get the information in a progress report,
it is necessary to step through the progress report
items.
To get the data for the current progress report item,
and advance to the next one,
use the @code{marpa_r_progress_item()} method.
To destroy a progress report,
freeing the memory it uses,
call the @code{marpa_r_progress_report_finish()} method.
@deftypefun int marpa_r_progress_report_reset ( @
Marpa_Recognizer @var{r})
Resets the progress report.
Assumes a report of the progress has already been initialized
at some Earley set
for recognizer @var{r},
with
@code{marpa_r_progress_report_start()}.
The reset progress report will
be positioned before its first item.
Return value: On success, a non-negative value.
On failure, @minus{}2.
@end deftypefun
@deftypefun int marpa_r_progress_report_start ( @
Marpa_Recognizer @var{r}, @
Marpa_Earley_Set_ID @var{set_id})
Initializes a report of the progress at Earley set @var{set_id}
for recognizer @var{r}.
If a progress report already exists, it is destroyed and its
memory is freed.
Initially,
the progress report is positioned before its first item.
If no Earley set with ID
@var{set_id} exists,
@code{marpa_r_progress_report_start()} fails.
The error code is @code{MARPA_ERR_INVALID_LOCATION} if @var{set_id}
is negative.
The error code is @code{MARPA_ERR_NO_EARLEY_SET_AT_LOCATION}
if @var{set_id} is greater than the ID of
the latest Earley set.
Return value: On success, the number of report items available.
If the recognizer has not been started;
if @var{set_id} does not exist;
or on other failure, @minus{}2.
@end deftypefun
@deftypefun int marpa_r_progress_report_finish ( @
Marpa_Recognizer @var{r} )
Destroys the report of the progress at Earley set @var{set_id}
for recognizer @var{r},
freeing the memory and other resources.
It is often not necessary to call this method.
Any previously existing progress report
is destroyed automatically
whenever a new progress report is started,
and when the recognizer is destroyed.
Return value: @minus{}2 if no progress report has been started,
or on other failure.
On success, a non-negative value.
@end deftypefun
@deftypefun Marpa_Rule_ID marpa_r_progress_item ( @
Marpa_Recognizer @var{r}, @
int* @var{position}, @
Marpa_Earley_Set_ID* @var{origin} )
This method allows access to the data
for the next item of a
progress report.
If there are no more progress report items,
it returns @minus{}1 as a termination indicator
and sets the error code to @code{MARPA_ERR_PROGRESS_REPORT_EXHAUSTED}.
Either the termination indicator,
or the item count returned by
@code{marpa_r_progress_report_start()},
can be used to determine when the last
item has been seen.
On success,
the dot position is returned in the location
pointed to by the @var{position} argument,
and the origin is returned in the location
pointed to by the @var{origin} argument.
On failure, the locations pointed to by
the @var{position} and @var{origin}
arguments are unchanged.
Return value: On success, the rule ID of
the next progress report item.
If there are no more progress report items, @minus{}1.
If either the @var{position} or the @var{origin}
argument is @code{NULL},
or on other failure, @minus{}2.
@end deftypefun
@node Bocage methods, Ordering methods, Progress reports, Top
@chapter Bocage methods
@menu
* Bocage overview::
* Bocage constructor::
* Bocage reference counting::
* Bocage accessor::
@end menu
@node Bocage overview, Bocage constructor, Bocage methods, Bocage methods
@section Overview
A bocage is structure containing the full set of parses found
by processing the input according to the grammar.
The bocage structure is new with Libmarpa, but is very similar
in purpose to the more familar parse forests.
To create a bocage, use the @code{marpa_b_new()} method.
When a bocage is no longer in use, its memory can be freed
using the
@code{marpa_b_unref()} method.
@node Bocage constructor, Bocage reference counting, Bocage overview, Bocage methods
@section Creating a new bocage
@deftypefun Marpa_Bocage marpa_b_new (Marpa_Recognizer @var{r}, @
Marpa_Earley_Set_ID @var{earley_set_ID})
Creates a new bocage object, with a reference count of 1.
The reference count of its parent recognizer object, @var{r},
is increased by 1.
If there is no parse ending at Earley set @var{earley_set_ID},
@code{marpa_b_new} fails
and the error code is set to
@code{MARPA_ERR_NO_PARSE}.
Return value: On success, the new bocage object.
On failure, @code{NULL}.
@end deftypefun
@node Bocage reference counting, Bocage accessor, Bocage constructor, Bocage methods
@section Reference counting
@deftypefun Marpa_Bocage marpa_b_ref (Marpa_Bocage @var{b})
Increases the reference count by 1.
Not needed by most applications.
Return value:
On success, @var{b}.
On failure, @code{NULL}.
@end deftypefun
@deftypefun void marpa_b_unref (Marpa_Bocage @var{b})
Decreases the reference count by 1,
destroying @var{b} once the reference count reaches
zero.
When @var{b} is destroyed, the reference count
of its parent recognizer is decreased by 1.
If this takes the reference count of the parent recognizer
to zero, it too is destroyed.
If the parent recognizer is destroyed, the reference count
of its base grammar is decreased by 1.
If this takes the reference count of the base grammar
to zero, it too is destroyed.
@end deftypefun
@node Bocage accessor, , Bocage reference counting, Bocage methods
@section Accessors
@deftypefun int marpa_b_ambiguity_metric (Marpa_Bocage @var{b})
Returns an ambiguity metric.
For the time being, it is vaguely defined, but it does differentiate
between an ambiguous and an unambiguous bocage.
Return value on success:
1 if the bocage is not for an ambiguous parse;
a number greater than 1 if the bocage is for an ambiguous parse.
Failures: On failure, @minus{}2.
@end deftypefun
@deftypefun int marpa_b_is_null (Marpa_Bocage @var{b})
Return value on success:
A number greater than or equal to 1 if the bocage is for a null parse;
otherwise, 0.
Failures: On failure, @minus{}2.
@end deftypefun
@node Ordering methods, Tree methods, Bocage methods, Top
@chapter Ordering methods
@menu
* Ordering overview::
* Ordering constructor::
* Ordering reference counting::
* Order accessor::
* Non-default ordering::
@end menu
@node Ordering overview, Ordering constructor, Ordering methods, Ordering methods
@section Overview
Before iterating the parses in the bocage,
they must be ordered.
To create an ordering, use the @code{marpa_o_new()} method.
When an ordering is no longer in use, its memory can be freed
using the
@code{marpa_o_unref()} method.
An ordering is @dfn{frozen} once the first
tree iterator is created
using it.
A frozen ordering cannot be changed.
As of this writing, the only methods to order parses
are internal and undocumented.
This is expected to change.
@node Ordering constructor, Ordering reference counting, Ordering overview, Ordering methods
@section Creating an ordering
@deftypefun Marpa_Order marpa_o_new ( @
Marpa_Bocage @var{b})
Creates a new ordering object, with a reference count of 1.
The reference count of its parent bocage object, @var{b},
is increased by 1.
Return value: On success, the new ordering object.
On failure, @code{NULL}.
@end deftypefun
@node Ordering reference counting, Order accessor, Ordering constructor, Ordering methods
@section Reference counting
@deftypefun Marpa_Order marpa_o_ref ( @
Marpa_Order @var{o})
Increases the reference count by 1.
Not needed by most applications.
Return value:
On success, @var{o}.
On failure, @code{NULL}.
@end deftypefun
@deftypefun void marpa_o_unref ( @
Marpa_Order @var{o})
Decreases the reference count by 1,
destroying @var{o} once the reference count reaches
zero.
Beginning with @var{o}'s parent bocage,
Libmarpa then proceeds up the chain of parent objects.
Every time a child is destroyed, the
reference count of its parent is decreased by 1.
Every time the reference count of an object
is decreased by 1,
if that reference count is now zero,
that object is destroyed.
Libmarpa follows this chain of decrements
and destructions as required,
all the way back to the
base grammar, if necessary.
@end deftypefun
@node Order accessor, Non-default ordering, Ordering reference counting, Ordering methods
@section Accessors
@deftypefun int marpa_o_ambiguity_metric (Marpa_Order @var{o})
Returns an ambiguity metric.
For the time being, it is vaguely defined, but it does differentiate
between an ambiguous and an unambiguous ordering.
Return value on success:
1 if the ordering is not for an ambiguous parse;
a number greater than 1 if the ordering is for an ambiguous parse.
Failures: On failure, @minus{}2.
@end deftypefun
@deftypefun int marpa_o_is_null (Marpa_Order @var{o})
Return value on success:
A number greater than or equal to 1 if the ordering is for a null parse;
otherwise, 0.
Failures: On failure, @minus{}2.
@end deftypefun
@node Non-default ordering, , Order accessor, Ordering methods
@section Non-default ordering
@deftypefun int marpa_o_high_rank_only_set ( @
Marpa_Order @var{o}, @
int @var{flag})
@deftypefunx int marpa_o_high_rank_only ( @
Marpa_Order @var{o})
These methods, respectively, set and discover
the ``high rank only'' flag of ordering @var{o}.
A @var{flag} of 1 indicates that, when ranking,
all choices should be discarded except those of the
highest rank.
A @var{flag} of 0 indicates that
no choices should be discarded on the
basis of their rank.
A value of 1 is the default.
The value of the ``high rank only'' flag has no effect
unless ranking has been turned on using the
@code{marpa_o_rank()} method.
Return value: On success, the value of the
``high rank only'' flag @strong{after}
the call.
On failure, @minus{}2.
@end deftypefun
@deftypefun int marpa_o_rank ( Marpa_Order @var{o} )
By default, the ordering of parse trees is arbitrary.
This method causes the ordering to be ranked
according to the ranks of symbols and rules,
the ``null ranks high'' flags of the rules,
and the ``high rank only'' flag of the ordering.
Once this method returns, the ordering is frozen.
Return value: On success, a non-negative value.
On failure, @minus{}2.
@end deftypefun
@node Tree methods, Value methods, Ordering methods, Top
@chapter Tree methods
@menu
* Tree overview::
* Tree constructor::
* Tree reference counting::
* Tree iteration::
@end menu
@node Tree overview, Tree constructor, Tree methods, Tree methods
@section Overview
Once the bocage has an ordering, the parses trees can be iterated.
Marpa's @dfn{parse tree iterators} iterate the parse trees contained
in a bocage object.
In Libmarpa,
``parse tree iterators'' are usually just called @dfn{trees}.
To create a tree, use the @code{marpa_t_new()} method.
A newly created tree iterator is positioned before the first parse tree.
When a tree iterator is no longer in use, its memory can be freed
using the
@code{marpa_t_unref()} method.
To position a newly created tree iterator at the first parse tree,
use the @code{marpa_t_next()} method.
Once the tree iterator is positioned at a parse tree,
the same @code{marpa_t_next()} method is used
to position it to the next parse tree.
@node Tree constructor, Tree reference counting, Tree overview, Tree methods
@section Creating a new tree iterator
@deftypefun Marpa_Tree marpa_t_new (Marpa_Order @var{o})
Creates a new tree iterator, with a reference count of 1.
The reference count of its parent ordering object, @var{o},
is increased by 1.
When initialized, a tree iterator is positioned
before the first parse tree.
To position the tree iterator to the first parse,
the application must call @code{marpa_t_next()}.
Return value: On success, a newly created tree.
On failure, @code{NULL}.
@end deftypefun
@node Tree reference counting, Tree iteration, Tree constructor, Tree methods
@section Reference counting
@deftypefun Marpa_Tree marpa_t_ref (Marpa_Tree @var{t})
Increases the reference count by 1.
Not needed by most applications.
Return value:
On success, @var{t}.
On failure, @code{NULL}.
@end deftypefun
@deftypefun void marpa_t_unref (Marpa_Tree @var{t})
Decreases the reference count by 1,
destroying @var{t} once the reference count reaches
zero.
Beginning with @var{t}'s parent ordering,
Libmarpa then proceeds up the chain of parent objects.
Every time a child is destroyed, the
reference count of its parent is decreased by 1.
Every time the reference count of an object
is decreased by 1,
if that reference count is now zero,
that object is destroyed.
Libmarpa follows this chain of decrements
and destructions as required,
all the way back to the
base grammar, if necessary.
@end deftypefun
@node Tree iteration, , Tree reference counting, Tree methods
@section Iterating through the trees
@deftypefun int marpa_t_next ( @
Marpa_Tree @var{t})
Positions @var{t} at the next parse tree
in the iteration.
Tree iterators are initialized to the position
before the first parse tree,
so this method must be called before creating a valuator
from a tree.
If a tree iterator is positioned after the last parse,
the tree is said to be ``exhausted''.
A tree iterator for a bocage with no parse trees
is considered to be ``exhausted'' when initialized.
If the tree iterator is exhausted, @code{marpa_t_next()}
returns @minus{}1 as a termination indicator,
and sets the error code to
@code{MARPA_ERR_TREE_EXHAUSTED}.
Return value: On success, a non-negative value.
If the tree iterator is exhausted, @minus{}1.
On failure, @minus{}2.
@end deftypefun
@deftypefun int marpa_t_parse_count ( @
Marpa_Tree @var{t})
The parse counter counts the number of parse trees
traversed so far.
The count includes the current iteration of the
tree, so that a value of 0 indicates that the tree iterator
is at its initialized position,
before the first parse tree.
Return value: The number of parses traversed so far.
Always succeeds.
@end deftypefun
@node Value methods, Events, Tree methods, Top
@chapter Value methods
@menu
* Value overview::
* How to use the valuator::
* Advantages of step-driven valuation::
* Maintaining the stack::
* Valuator constructor::
* Valuator reference counting::
* Stepping through the valuator::
* Valuator steps by type::
* Basic step accessors::
* Other step accessors::
@end menu
@node Value overview, How to use the valuator, Value methods, Value methods
@section Overview
The archetypal application needs
a value object (or @dfn{valuator}) to produce
the value of the parse.
To create a valuator, use the @code{marpa_v_new()} method.
When a valuator is no longer in use, its memory can be freed
using the
@code{marpa_v_unref()} method.
The application is required to maintain the stack,
and the application is also required to implement
most of the semantics, including the evaluation
of rules.
Libmarpa's valuator provides instructions to
the application on how to manipulate the stack.
To iterate through this series of instructions,
use the @code{marpa_v_step()} method.
@code{marpa_v_step()} returns the type
of step.
Most step types have values associated with them.
To access these values use the methods
described in the section @ref{Basic step accessors}.
How to perform the steps is described in
the sections
@ref{How to use the valuator}
and @ref{Stepping through the valuator}.
@node How to use the valuator, Advantages of step-driven valuation, Value overview, Value methods
@section How to use the valuator
Libmarpa's valuator provides the application with
``steps'', which are
instructions for stack manipulation.
Libmarpa itself does not maintain a stack.
This leaves the upper layer in total control of the
stack and the values which are placed on it.
As example may make this clearer.
Suppose the evalution is at a place in the parse tree
where an addition is being performed.
Libmarpa does not know that the operation
is an addition.
It will tell the application that rule number @var{R}
is to be applied to the arguments at stack locations
@var{N} and @var{N}+1, and that the result is to placed in
stack location @var{N}.
In this system
the application keeps track of the semantics for all
rules, so it looks up rule @var{R} and determines that it
is an addition.
The application can do this by using @var{R} as an index
into an array of callbacks, or by any other method
it chooses.
Let's assume a callback implements the semantics
for rule @var{R}.
Libmarpa has told the application that two arguments
are available for this operation, and that they are
at locations @var{N} and @var{N}+1 in the stack.
They might be the numbers 42 and 711.
So the callback is called with its two arguments,
and produces a return value, let's say, 753.
Libmarpa has told the application that the result
belongs at location @var{N} in the stack,
so the application writes 753 to location @var{N}.
Since Libmarpa knows nothing about the semantics,
the operation for rule R could be string concatenation
instead of addition.
Or, if it is addition, it could allow for its arguments
to be floating point or complex numbers.
Since the application maintains the stack, it is up
to the application whether the stack contains integers,
strings, complex numbers, or polymorphic objects which are
capable of being any of these things and more.
@node Advantages of step-driven valuation, Maintaining the stack, How to use the valuator, Value methods
@section Advantages of step-driven valuation
Step-driven valuation
hides Libmarpa's grammar rewrites from the application,
and is quite efficient.
Libmarpa knows which rules are sequences.
Libmarpa optimizes stack manipulations based on this knowledge.
Long sequences
are very common in practical grammars.
For these,
the stack manipulations suggested by Libmarpa's
step-driven valuator
will be significantly faster than the
traditional stack evaluation algorithm.
Step-driven evalution has another advantage.
To illustrate this,
consider what is a very common case:
The semantics are implemented in a higher-level language,
using callbacks.
If Libmarpa did not use step-driven valuation,
it would need to provide for this case.
But for generality,
Libmarpa would have to deal in C callbacks.
Therefore, a middle layer would have to create C language wrappers
for the callbacks in the higher level language.
The implementation that results is this:
The higher level language would need to wrap each callback in C.
When calling Libmarpa, it would pass the wrappered callback.
Libmarpa would then need to call the C language ``wrappered'' callback.
Next, the wrapper would call the higher-level language callback.
The return value,
which would be data native to the higher-level language,
would need to be passed to the C language wrapper,
which will need to make arrangements for it to be based
back to the higher-level language when appropriate.
A setup like this is not terribly efficient.
And exception handling across language boundaries would be
very tricky.
But neither of these is the worst problem.
Callbacks are hard to debug.
Wrappered callbacks are even worse.
Calls made across language boundaries
are harder yet to debug.
In the system described above,
by the time a return value is finally consumed,
a language boundary will have been crossed four times.
How do
programmers deal with difficulties like this?
Usually, by
doing the absolute minimum possible in the callbacks.
A horrific debugging enviroment can become a manageable
one if there is next to no code to be debugged.
And this can be accomplished by
doing as much as possible in pre- and post-processing.
In essence, callbacks force applications to do most
of the programming
via side effects.
One need not be a functional programming purist to find
this a very undesirable style of design to force on
an application.
But the ability to debug can make the difference between
code that does work and code that does not.
Unfairly or not,
code is rarely considered well-designed when it does
not work.
So, while step-driven valuation seems
a roundabout approach,
it is simpler and more direct than
the likely alternatives.
And there is something to be said for pushing
semantics up to the higher levels ---
they can be expected to know more about it.
These advantages of step-driven valuation
are strictly in
the context of a low-level interface.
The author is under no illusion
that direct use of Libmarpa's valuator will be found
satisfactory by most application programmers,
even those using the C language.
The author certainly avoids using step-driven valuation directly.
Libmarpa's valuator is intended
to be used via an upper layer,
one which @strong{does} know about semantics.
@node Maintaining the stack, Valuator constructor, Advantages of step-driven valuation, Value methods
@section Maintaining the stack
This section discusses in detail the requirements
for maintaining the stack.
In some cases,
such as implementation using a Perl array,
fulfilling these requirements is trivial.
Perl auto-extends its arrays,
and initializes the element values,
on every read or write.
For the C programmer,
things are not quite so easy.
In this section,
we will assume a C90 or C99 standard-conformant
C application.
This assumption is convenient on two grounds.
First, this will be the intended use
for many readers.
Second, standard-conformant C is
a ``worst case''.
Any issue faced by a programmer of another environment
is likely to also be one that must be solved
by the C programmer.
Libmarpa often
optimizes away unnecessary stack writes
to stack locations.
When it does so, it will not
necessarily optimize away all reads
to that stack location.
This means that a location's first access,
as suggested by the Libmarpa step instructions,
may be a read.
This possibility
requires a special awareness from the C
programmer,
as discussed in
the sections
@ref{Sizing the stack} and
@ref{Initializing locations in the stack}.
In the discussions in this document,
stack locations are non-negative integers.
The bottom of the stack is location 0.
In moving from the bottom of the stack to the top,
the numbers increase.
Stack location @var{Y} is said to be ``greater''
than stack location @var{X} if stack location
@var{Y} is closer to the top of stack than location @var{X},
and therefore stack locations are considered greater or
lesser if the integers that represent them are
greater or lesser.
Another way to state that
a stack location @var{Y} is greater (lesser)
than stack location @var{X} is to say that
a stack location @var{Y} is later (earlier)
than stack location @var{X}.
@menu
* Sizing the stack::
* Initializing locations in the stack::
@end menu
@node Sizing the stack, Initializing locations in the stack, Maintaining the stack, Maintaining the stack
@subsection Sizing the stack
If an implementation applies Libmarpa's step
instructions literally, using a physical stack,
it must make sure the stack is large enough.
Specifically, the application must do the following
@itemize
@item Ensure location 0 exists --- in other
words that the stack is at least length 1.
@item For @code{MARPA_STEP_TOKEN} steps,
ensure that location @code{marpa_v_result(v)}
exists.
@item For @code{MARPA_STEP_NULLING_SYMBOL} steps,
ensure that location @code{marpa_v_result(v)}
exists.
@item For @code{MARPA_STEP_RULE} steps,
ensure that stack locations from @code{marpa_v_arg_0(v)}
to @code{marpa_v_arg_n(v)} exist.
@end itemize
Three aspects of these requirements deserve special mention.
First,
note that the requirement for a
@code{MARPA_STEP_RULE} is that the application
size the stack to include the arguments to be
read.
Because stack writes may be optimized away,
an application,
when reading,
cannot assume
that the stack was
sized appropriately by a prior write.
The first access to a new stack location may be
a read.
Second,
note that there is no explicit requirement that
the application size the stack to include the
location for the result of the
@code{MARPA_STEP_RULE} step.
An application is allowed to assume that
result will go into one of the locations
that were read.
Third, special note should be made of the requirement
that location 0 exist.
By convention, the parse result resides
in location 0 of the stack.
Because of potential optimizations,
an application cannot assume that it
will receive a Libmarpa step instruction that
either reads from or writes to location 0.
@node Initializing locations in the stack, , Sizing the stack, Maintaining the stack
@subsection Initializing locations in the stack
Write optimizations also creates issues for implementations
which require data to be initialized before reading.
Every fully standard-conforming C application is such an
implementation.
Both C90 and C99 allow ``trap values'',
and therefore conforming applications must be
prepared for
an uninitialized location to contain one of those.
Reading a trap value may cause an abend.
(It is safe, in standard-conforming C, to write to a location
containing a trap value.)
The requirement that locations be initialized before
reading occurs in other implementations.
Any implementation that has a ``universe'' of ``safe'' values,
may require special precautions.
The required precautions may amount to a need to initialize
``uninitialized'' values.
A practical example might be an implementation that expects
all locations to contain a pointer which it can safely indirect
from.
In such implementations,
just as in standard-conformant C,
every stack location
needs to be initialized before being read.
Due to write optimizations, an application
cannot rely on Libmarpa's step instructions to
initialize every stack location before its first read.
One way to safely deal with the
initialization of stack locations,
is to do all of the following:
@itemize
@item When starting evaluation, ensure that the stack contains at least location 0.
@item Also, when starting evaluation, initialize every location in the stack.
@item Whenever the stack is extended,
initialize every stack location added.
@end itemize
Applications which try to optimize out some of
these initializations
need to be aware that
an application can never assume that activity in
the stack is safely ``beyond'' an uninitialized location.
Libmarpa steps often revisit earlier sections of the stack,
and these revisits may include reads of previously
unvisited stack locations.
@node Valuator constructor, Valuator reference counting, Maintaining the stack, Value methods
@section Creating a new valuator
@deftypefun Marpa_Value marpa_v_new ( @
Marpa_Tree @var{t} @
)
Creates a new valuator.
The parent object of the new valuator
will be the tree iterator @var{t},
and the reference count of the new valuator will be 1.
The reference count of @var{t} is increased by 1.
The parent tree iterator is ``paused'',
so that the tree iterator
cannot move on to a new parse tree
until the valuator is destroyed.
Many valuators of the same parse tree
can exist at once.
A tree iterator is ``unpaused'' when
all of the valuators of that tree iterator are destroyed.
Return value: On success, the newly created valuator.
On failure, @code{NULL}.
@end deftypefun
@node Valuator reference counting, Stepping through the valuator, Valuator constructor, Value methods
@section Reference counting
@deftypefun Marpa_Value marpa_v_ref (Marpa_Value @var{v})
Increases the reference count by 1.
Not needed by most applications.
Return value:
On success, @var{v}.
On failure, @code{NULL}.
@end deftypefun
@deftypefun void marpa_v_unref ( @
Marpa_Value @var{v})
Decreases the reference count by 1,
destroying @var{v} once the reference count reaches
zero.
Beginning with @var{v}'s parent tree,
Libmarpa then proceeds up the chain of parent objects.
Every time a child is destroyed, the
reference count of its parent is decreased by 1.
Every time the reference count of an object
is decreased by 1,
if that reference count is now zero,
that object is destroyed.
Libmarpa follows this chain of decrements
and destructions as required,
all the way back to the
base grammar, if necessary.
@end deftypefun
@node Stepping through the valuator, Valuator steps by type, Valuator reference counting, Value methods
@section Stepping through the valuator
@deftypefun Marpa_Step_Type marpa_v_step ( @
Marpa_Value @var{v})
This method ``steps through'' the valuator.
The return value is a @code{Marpa_Step_Type},
an integer which indicates the type of step.
How the application is expected to act on
each step is described below.
@xref{Valuator steps by type}.
When the iteration through the steps is finished,
@code{marpa_v_step} returns @code{MARPA_STEP_INACTIVE}.
Return value: On success, the type of the step
to be performed.
This will always be a non-negative number.
On failure, @minus{}2.
@end deftypefun
@node Valuator steps by type, Basic step accessors, Stepping through the valuator, Value methods
@section Valuator steps by type
@deftypevr Macro Marpa_Step_Type MARPA_STEP_RULE
The semantics of a rule should be performed.
The application can find the value of the rule's
children in the stack locations from
@code{marpa_v_arg_0(v)}
to @code{marpa_v_arg_n(v)}.
The semantics for the rule whose ID is
@code{marpa_v_rule(v)} should be executed
on these child values,
and the result placed in
@code{marpa_v_result(v)}.
In the case of a @code{MARPA_STEP_RULE} step,
the stack location of
@code{marpa_v_result(v)} is guaranteed to
be equal to @code{marpa_v_arg_0(v)}.
@end deftypevr
@deftypevr Macro Marpa_Step_Type MARPA_STEP_TOKEN
The semantics of a non-null token should be performed.
The application's value for the token whose ID is
@code{marpa_v_token(v)} should be
placed in
stack location @code{marpa_v_result(v)}.
Its value according to Libmarpa will be in
@code{marpa_v_token_value(v)}.
@end deftypevr
@deftypevr Macro Marpa_Step_Type MARPA_STEP_NULLING_SYMBOL
The semantics for a nulling symbol should be performed.
The ID of the symbol is
@code{marpa_v_symbol(v)} and its value should
be placed in
stack location @code{marpa_v_result(v)}.
@end deftypevr
@deftypevr Macro Marpa_Step_Type MARPA_STEP_INACTIVE
The valuator has gone through all of its steps
and is now inactive.
The value of the parse will be in stack location 0.
Because of optimizations,
it is possible for valuator to immediately
became inactive --- @code{MARPA_STEP_INACTIVE} could
be both the first and last step.
@end deftypevr
@deftypevr Macro Marpa_Step_Type MARPA_STEP_INITIAL
The valuator is new and has
yet to go through any steps.
@end deftypevr
@deftypevr Macro Marpa_Step_Type MARPA_STEP_INTERNAL1
@deftypevrx Macro Marpa_Step_Type MARPA_STEP_INTERNAL2
@deftypevrx Macro Marpa_Step_Type MARPA_STEP_TRACE
These step types are reserved for internal purposes.
@end deftypevr
@node Basic step accessors, Other step accessors, Valuator steps by type, Value methods
@section Basic step accessors
The basic step accessors are so called because their information
is basic to the stack manipulation.
The basic step accessors are implemented as macros.
They always succeed.
@deftypefn {Macro} int marpa_v_arg_0 (Marpa_Value @var{v})
For a @code{MARPA_STEP_RULE} step,
returns the stack location where the value of first child
can be found.
@end deftypefn
@deftypefn {Macro} int marpa_v_arg_n (Marpa_Value @var{v})
For a @code{MARPA_STEP_RULE} step,
returns the stack location where the value of the last child
can be found.
@end deftypefn
@deftypefn {Macro} int marpa_v_result (Marpa_Value @var{v})
For @code{MARPA_STEP_RULE},
@code{MARPA_STEP_TOKEN},
and @code{MARPA_STEP_NULLING_SYMBOL} steps,
returns the stack location where the result of the semantics
should be placed.
@end deftypefn
@deftypefn {Macro} Marpa_Rule_ID marpa_v_rule (Marpa_Value @var{v})
For the
@code{MARPA_STEP_RULE} step,
returns the ID of the rule.
@end deftypefn
@deftypefn {Macro} Marpa_Step_Type marpa_v_step_type (Marpa_Value @var{v})
Returns the current step type:
@code{MARPA_STEP_TOKEN},
@code{MARPA_STEP_RULE},
etc.
Usually not needed since this is also the return value of
@code{marpa_v_step()}.
@end deftypefn
@deftypefn {Macro} Marpa_Symbol_ID marpa_v_symbol (Marpa_Value @var{v})
For the @code{MARPA_STEP_NULLING_SYMBOL} step,
returns the ID of the symbol.
The value returned is the same as that
returned by the @code{marpa_v_token()}
macro.
@end deftypefn
@deftypefn {Macro} Marpa_Symbol_ID marpa_v_token (Marpa_Value @var{v})
For the @code{MARPA_STEP_TOKEN} step,
returns the ID of the token.
The value returned is the same as that
returned by the @code{marpa_v_symbol()}
macro.
@end deftypefn
@deftypefn {Macro} int marpa_v_token_value (Marpa_Value @var{v})
For the @code{MARPA_STEP_TOKEN} step,
returns the integer which is
(or which represents)
the value of the token.
@end deftypefn
@node Other step accessors, , Basic step accessors, Value methods
@section Other step accessors
This section contains the step accessors that
are not basic to stack manipulation, but which provide
other useful information about the parse.
These step accessors are implemented as macros.
All of these accessors always succeed, but
if called when they are irrelevant
they return an unspecified value.
In this context, an ``unspecified value'' is a value
that is either @minus{}1 or the ID of a valid Earley set,
but which is otherwise unpredictable.
@deftypefn {Macro} Marpa_Earley_Set_ID marpa_v_es_id (Marpa_Value @var{v})
Return value:
If the current step type is @code{MARPA_STEP_RULE},
the Earley Set ordinal where the rule ends.
If the current step type is
@code{MARPA_STEP_TOKEN}
or @code{MARPA_STEP_NULLING_SYMBOL},
the Earley Set ordinal where the symbol ends.
If the current step type is anything else, an unspecified value.
@end deftypefn
@deftypefn {Macro} Marpa_Earley_Set_ID marpa_v_rule_start_es_id (Marpa_Value @var{v})
Return value:
If the current step type is @code{MARPA_STEP_RULE},
the Earley Set ordinal where the rule begins.
If the current step type is anything else, an unspecified value.
@end deftypefn
@deftypefn {Macro} Marpa_Earley_Set_ID marpa_v_token_start_es_id (Marpa_Value @var{v})
Return value:
If the current step type is @code{MARPA_STEP_TOKEN}
or @code{MARPA_STEP_NULLING_SYMBOL},
the Earley Set ordinal where the token begins.
If the current step type is anything else, an unspecified value.
@end deftypefn
@node Events, Error methods macros and codes, Value methods, Top
@chapter Events
@menu
* Events overview::
* Event methods::
* Event codes::
@end menu
@node Events overview, Event methods, Events, Events
@section Overview
Events are generated by the
@code{marpa_g_precompute()},
@code{marpa_r_earleme_complete()},
and
@code{marpa_r_start_input()} methods.
The methods are called event-active.
Event-active methods always clear all previous events,
so that after an event-active method the only events
available
will be those generated by that method.
Events are volatile,
and it is expected that events will be queried
immediately after the method that generated them.
Note especially
that multiple recognizers using the same base grammar
overwrite each other's events.
To find out how many events were generated by the last
event-active method,
use the @code{marpa_g_event_count} method.
To query a specific event,
use the @code{marpa_g_event} and
@code{marpa_g_event_value} methods.
@node Event methods, Event codes, Events overview, Events
@section Methods
@deftypefun Marpa_Event_Type marpa_g_event (Marpa_Grammar @var{g}, @
Marpa_Event* @var{event}, @
int @var{ix})
On success,
the type of the @var{ix}'th event is returned
and the data for the @var{ix}'th event is placed
in the location pointed to by @var{event}.
Event indexes are in sequence.
Valid events will be in the range from 0 to @var{n},
where @var{n} is one less than the event count.
The event count
can be queried using the @code{marpa_g_event_count()}
method.
Return value: On success, the type of event @var{ix}.
If there is no @var{ix}'th event,
if @var{ix} is negative,
or on other failure, @minus{}2.
On failure,
the locations pointed to by @var{event}
are not changed.
@end deftypefun
@deftypefun int marpa_g_event_count ( Marpa_Grammar g )
Return value: On success, the number of events.
On failure, @minus{}2.
@end deftypefun
@deftypefn {Macro} int marpa_g_event_value (Marpa_Event* @var{event})
This macro provides access to the ``value'' of the event.
The semantics of the value varies according to the type
of the event, and is described in the section on event
codes.
@xref{Event codes}.
@end deftypefn
@node Event codes, , Event methods, Events
@section Event codes
@deftypevr Macro int MARPA_EVENT_NONE
Applications should never see this event.
Event value: Undefined.
Suggested message: "No event".
@end deftypevr
@deftypevr Macro int MARPA_EVENT_COUNTED_NULLABLE
A nullable symbol is either the separator
for, or the right hand side of, a sequence.
Event value: The ID of the symbol.
Suggested message: "This symbol is a counted nullable".
@end deftypevr
@deftypevr Macro int MARPA_EVENT_EARLEY_ITEM_THRESHOLD
This event indicates the current Earley item count
exceeded a
user-settable threshold.
Event value: The current Earley item count.
Suggested message: "Too many Earley items".
@end deftypevr
@deftypevr Macro int MARPA_EVENT_EXHAUSTED
The parse is exhausted.
Event value: Undefined.
Suggested message: "Recognizer is exhausted".
@end deftypevr
@deftypevr Macro int MARPA_EVENT_LOOP_RULES
One or more rules are loop rules --- rules
that are part of a cycle.
Cycles are pathological cases of recursion,
in which the same symbol string derives itself
a potentially infinite number of times.
Nonetheless, Marpa parses in the presence of these,
and it is up to the application to treat these
as fatal errors,
something they almost always will wish to do.
Event value: The count of loop rules.
Suggested message: "Grammar contains a infinite loop".
@end deftypevr
@deftypevr Macro int MARPA_EVENT_NULLING_TERMINAL
A nulling symbol is also a terminal.
Event value: The ID of the symbol.
Suggested message: "This symbol is a nulling terminal".
@end deftypevr
@deftypevr Macro int MARPA_EVENT_SYMBOL_COMPLETED
The recognizer can be set to generate an event
a symbol is completed
using its @code{marpa_g_symbol_is_completion_event_set()}
method.
(A symbol is "completed" if and only if any rule with that symbol
as its LHS is completed.)
This event code indicates that one of those events
occurred.
Event value: The ID of the completed symbol.
Suggested message: "Completed symbol".
@end deftypevr
@deftypevr Macro int MARPA_EVENT_SYMBOL_EXPECTED
The recognizer can be set to generate an event when a
symbol is expected as a terminal,
using its @code{marpa_r_expected_symbol_event_set()}
method.
Note that this event only triggers if the symbol is
expected as a terminal.
Predicted symbols which are not expected as terminals
do not trigger this event.
This event code indicates that one of those events
occurred.
Event value: The ID of the expected symbol.
Suggested message: "Expecting symbol".
@end deftypevr
@deftypevr Macro int MARPA_EVENT_SYMBOL_NULLED
The recognizer can be set to generate an event when a
symbol is nulled -- that is, recognized as a
zero-length symbol.
To set an nulled symbol event,
use the recognizer's @code{marpa_r_nulled_symbol_event_set()}
method.
This event code indicates that a nulled symbol event
occurred.
Event value: The ID of the nulled symbol.
Suggested message: "Symbol was nulled".
@end deftypevr
@deftypevr Macro int MARPA_EVENT_SYMBOL_PREDICTED
The recognizer can be set to generate an event when a
symbol is predicted.
To set an predicted symbol event,
use the recognizer's @code{marpa_r_predicted_symbol_event_set()}
method.
Unlike the
@code{MARPA_EVENT_SYMBOL_EXPECTED} event,
the @code{MARPA_EVENT_SYMBOL_PREDICTED} event
triggers for predictions of both
non-terminals and terminals.
This event code indicates that a predicted symbol event
occurred.
Event value: The ID of the predicted symbol.
Suggested message: "Symbol was predicted".
@end deftypevr
@node Error methods macros and codes, Design notes, Events, Top
@chapter Error methods, macros and codes
@menu
* Error methods::
* Error Macros::
* External error codes::
* Internal error codes::
@end menu
@node Error methods, Error Macros, Error methods macros and codes, Error methods macros and codes
@section Error methods
@deftypefun Marpa_Error_Code marpa_g_error @
( Marpa_Grammar @var{g}, @
const char** @var{p_error_string})
When a method fails,
this method allows the application to read
the error code.
@var{p_error_string} is reserved for use by
the internals.
Applications should set it to @code{NULL}.
Return value: The last error code from a Libmarpa method.
Always succeeds.
@end deftypefun
@deftypefun Marpa_Error_Code marpa_g_error_clear @
( Marpa_Grammar @var{g} )
Sets the error code
to @code{MARPA_ERR_NONE}.
Not often used,
but now and then it can be useful
to force the error code to a known state.
Return value: @code{MARPA_ERR_NONE}.
Always succeeds.
@end deftypefun
@node Error Macros, External error codes, Error methods, Error methods macros and codes
@section Error Macros
@deftypevr Macro int MARPA_ERRCODE_COUNT
The number of error codes.
All error codes, whether internal or external,
will be integers, non-negative but
strictly less than @code{MARPA_ERRCODE_COUNT}.
@end deftypevr
@node External error codes, Internal error codes, Error Macros, Error methods macros and codes
@section External error codes
This section lists the external error codes.
These are the only error codes that users
of the Libmarpa external interface should ever see.
Internal error codes are in their own section.
@xref{Internal error codes}.
@deftypevr Macro int MARPA_ERR_NONE
No error condition.
The error code is initialized to this value.
Methods which do not result in failure
sometimes reset the error code to @code{MARPA_ERR_NONE}.
Numeric value: 0.
Suggested message: "No error".
@end deftypevr
@deftypevr Macro int MARPA_ERR_BAD_SEPARATOR
A separator was specified for a sequence rule,
but its ID was not that
of a valid symbol.
Numeric value: 6.
Suggested message: "Separator has invalid symbol ID".
@end deftypevr
@deftypevr Macro int MARPA_ERR_BEFORE_FIRST_TREE
A tree iterator is positioned before the first tree,
and it was specified in a context where that is not
allowed.
A newly created tree is positioned before the first
tree.
To position a newly created tree iterator to the first tree
use the @code{marpa_t_next()} method.
Numeric value: 91.
Suggested message: "Tree iterator is before first tree".
@end deftypevr
@deftypevr Macro int MARPA_ERR_COUNTED_NULLABLE
A ``counted'' symbol was found
that is also a nullable symbol.
A ``counted'' symbol is one that appears on the RHS
of a sequence rule.
If a symbol is nullable,
counting its occurrences becomes difficult.
Questions of definition and
problems of implementation arise.
At a minimum, a sequence with counted nullables
would be wildly
ambigious.
Sequence rules are simply an optimized shorthand
for rules that can also be written in ordinary BNF.
If the equivalent of a sequence of nullables is
really what your application needs,
nothing in Libmarpa prevents you from specifying
that sequence
with ordinary BNF rules.
Numeric value: 8.
Suggested message: "Nullable symbol on RHS of a sequence rule".
@end deftypevr
@deftypevr Macro int MARPA_ERR_DUPLICATE_RULE
This error indicates an attempt to add a BNF rule which
is a duplicate of a BNF rule already in the grammar.
Two BNF rules are considered duplicates if
@itemize @bullet
@item
Both rules have the same left hand symbol, and
@item
Both rules have the same right hand symbols in the same order.
@end itemize
Duplication of sequence rules,
and duplication between BNF rules and sequence rules,
is dealt with by requiring that the LHS of a sequence rule
not be the LHS of any other rule.
Numeric value: 11.
Suggested message: "Duplicate rule".
@end deftypevr
@deftypevr Macro int MARPA_ERR_DUPLICATE_TOKEN
This error indicates an attempt to add a duplicate token.
A token is a duplicate if one already read at the same
earleme has the same symbol ID and the same length.
Numeric value: 12.
Suggested message: "Duplicate token".
@end deftypevr
@deftypevr Macro int MARPA_ERR_YIM_COUNT
This error code indicates that
an implementation-defined limit on the
number of Earley items per Earley set
was exceedeed.
This limit is different from
the Earley item warning threshold,
an optional limit on the number
of Earley items in an Earley set,
which can be set by the application.
The implementation defined-limit is very large,
at least 500,000,000 earlemes.
An application is unlikely ever to see this
error.
Libmarpa's use of memory
would almost certainly exceed the implementation's
limits before it occurred.
Numeric value: 13.
Suggested message: "Maximum number of Earley items exceeded".
@end deftypevr
@deftypevr Macro int MARPA_ERR_EVENT_IX_NEGATIVE
A negative event index was specified.
That is not allowed.
Numeric value: 15.
Suggested message: "Negative event index".
@end deftypevr
@deftypevr Macro int MARPA_ERR_EVENT_IX_OOB
An non-negative event index was specified,
but there is no event at that index.
Since the events are in sequence, this means it
was too large.
Numeric value: 16.
Suggested message: "No event at that index".
@end deftypevr
@deftypevr Macro int MARPA_ERR_GRAMMAR_HAS_CYCLE
The grammar has a cycle --- one or more loop
rules.
This is a recoverable error,
although most applications will want to treat
it as fatal.
For more see the description of @ref{marpa_g_precompute}.
Numeric value: 17.
Suggested message: "Grammar has cycle".
@end deftypevr
@deftypevr Macro int MARPA_ERR_HEADERS_DO_NOT_MATCH
This is an internal error, and indicates that
Libmarpa was wrongly built.
Libmarpa was compiled with headers which do not
match the rest of the code.
The solution is to find a correctly built
Libmarpa.
Numeric value: 98.
Suggested message: "Internal error: Libmarpa was built incorrectly"
@end deftypevr
@deftypevr Macro int MARPA_ERR_I_AM_NOT_OK
The Libmarpa base grammar is in a "not ok"
state.
Currently, the only way this can happen
is if Libmarpa memory is being overwritten.
Numeric value: 29.
Suggested message: "Marpa is in a not OK state".
@end deftypevr
@deftypevr Macro int MARPA_ERR_INACCESSIBLE_TOKEN
This error code indicates that
the token symbol is an inaccessible symbol --- one which
cannot be reached from the start symbol.
Since the inaccessibility of a symbol is a property of the grammar,
this error code typically indicates an application error.
Nevertheless, a retry at this location, using another token ID,
may succeed.
At this writing,
the author knows of no uses of this technique.
Numeric value: 18.
Suggested message: "Token symbol is inaccessible".
@end deftypevr
@deftypevr Macro int MARPA_ERR_INVALID_BOOLEAN
A function was called that takes a boolean argument,
but the value of that argument was not either 0 or 1.
Numeric value: 22.
Suggested message: "Argument is not boolean".
@end deftypevr
@deftypevr Macro int MARPA_ERR_INVALID_LOCATION
The location (Earley set ID) is not valid.
It may be invalid for one of two reasons:
@itemize
@item It is negative,
and it is being used as the argument to a method
for which that negative value does not have a special meaning.
@item It is after the latest Earley set.
@end itemize
For users of input models other than the standard one,
the term ``location'', as used in association
with this error code,
means Earley set ID or Earley set ordinal.
In the standard input model, this will always
be identical with Libmarpa's other idea of
location, the earleme.
Numeric value: 25.
Suggested message: "Location is not valid".
@end deftypevr
@deftypevr Macro int MARPA_ERR_INVALID_START_SYMBOL
A start symbol was specified,
but its symbol ID is not that of a valid symbol.
Numeric value: 27.
Suggested message: "Specified start symbol is not valid".
@end deftypevr
@deftypevr Macro int MARPA_ERR_INVALID_ASSERTION_ID
A method was called with an invalid assertion ID.
This is a assertion ID which not only does
not exist, but cannot exist.
Currently that means its value is less than zero.
Numeric value: 96.
Suggested message: "Assertion ID is malformed".
@end deftypevr
@deftypevr Macro int MARPA_ERR_INVALID_RULE_ID
A method was called with an invalid rule ID.
This is a rule ID which not only does
not exist, but cannot exist.
Currently that means its value is less than zero.
Numeric value: 26.
Suggested message: "Rule ID is malformed".
@end deftypevr
@deftypevr Macro int MARPA_ERR_INVALID_SYMBOL_ID
A method was called with an invalid symbol ID.
This is a symbol ID which not only does
not exist, but cannot exist.
Currently that means its value is less than zero.
Numeric value: 28.
Suggested message: "Symbol ID is malformed".
@end deftypevr
@deftypevr Macro int MARPA_ERR_MAJOR_VERSION_MISMATCH
There was a mismatch in the major version number
between the requested version
of libmarpa, and the actual one.
Numeric value: 30.
Suggested message: "Libmarpa major version number is a mismatch".
@end deftypevr
@deftypevr Macro int MARPA_ERR_MICRO_VERSION_MISMATCH
There was a mismatch in the micro version number
between the requested version
of libmarpa, and the actual one.
Numeric value: 31.
Suggested message: "Libmarpa micro version number is a mismatch".
@end deftypevr
@deftypevr Macro int MARPA_ERR_MINOR_VERSION_MISMATCH
There was a mismatch in the minor version number
between the requested version
of libmarpa, and the actual one.
Numeric value: 32.
Suggested message: "Libmarpa minor version number is a mismatch".
@end deftypevr
@deftypevr Macro int MARPA_ERR_NO_EARLEY_SET_AT_LOCATION
A non-negative Earley set ID (also called an Earley set ordinal)
was specified,
but there is no corresponding Earley set.
Since the Earley set ordinals are in sequence,
this means that the specified ID is greater
than that of the latest Earley set.
Numeric value: 39.
Suggested message: "Earley set ID is after latest Earley set".
@end deftypevr
@deftypevr Macro int MARPA_ERR_NOT_PRECOMPUTED
The grammar is not precomputed,
and attempt was made to do something with it
that is not allowed for unprecomputed
grammars.
For example, a recognizer cannot be
created from a grammar until it is precomputed.
Numeric value: 34.
Suggested message: "This grammar is not precomputed".
@end deftypevr
@deftypevr Macro int MARPA_ERR_NO_PARSE
The application attempted to create a bocage
from a recognizer without a parse.
Applications will often want to treat this as
a soft error.
Numeric value: 41.
Suggested message: "No parse".
@end deftypevr
@deftypevr Macro int MARPA_ERR_NO_RULES
A grammar which has no rules is being used
in a way that is not allowed.
Usually the problem is that the user is
trying to precompute the grammar.
Numeric value: 42.
Suggested message: "This grammar does not have any rules".
@end deftypevr
@deftypevr Macro int MARPA_ERR_NO_START_SYMBOL
The grammar has no start symbol,
and an attempt was made to perform an
operation which requires one.
Usually the problem is that the user is
trying to precompute the grammar.
Numeric value: 43.
Suggested message: "This grammar has no start symbol".
@end deftypevr
@deftypevr Macro int MARPA_ERR_NO_SUCH_ASSERTION_ID
A method was called with an assertion ID which is well-formed,
but the assertion does not exist.
Numeric value: 97.
Suggested message: "No assertion with this ID exists".
@end deftypevr
@deftypevr Macro int MARPA_ERR_NO_SUCH_RULE_ID
A method was called with a rule ID which is well-formed,
but the rule does not exist.
Numeric value: 89.
Suggested message: "No rule with this ID exists".
@end deftypevr
@deftypevr Macro int MARPA_ERR_NO_SUCH_SYMBOL_ID
A method was called with a symbol ID which is well-formed,
but the symbol does not exist.
Numeric value: 90.
Suggested message: "No symbol with this ID exists".
@end deftypevr
@deftypevr Macro int MARPA_ERR_NO_TOKEN_EXPECTED_HERE
This error code indicates that
no tokens at all were expected at this earleme
location.
This can only happen in alternative input models.
Typically, this indicates an application programming
error.
Retrying input at this location will always fail.
But if the application is able to leave this
earleme empty, a retry at a later location,
using this or another token,
may succeed.
At this writing,
the author knows of no uses of this technique.
Numeric value: 44.
Suggested message: "No token is expected at this earleme location".
@end deftypevr
@deftypevr Macro int MARPA_ERR_NULLING_TERMINAL
Marpa does not allow a symbol to be both nulling
and a terminal.
Numeric value: 49.
Suggested message: "A symbol is both terminal and nulling".
@end deftypevr
@deftypevr Macro int MARPA_ERR_ORDER_FROZEN
The Marpa order object has been frozen.
If a Marpa order object is frozen, it cannot be
changed.
Multiple tree iterators can share a Marpa order object,
but that order object is frozen after the first tree
iterator is created from it.
Applications can order an bocage in many ways,
but they must do so by creating multiple order objects.
Numeric value: 50.
Suggested message: "The ordering is frozen".
@end deftypevr
@deftypevr Macro int MARPA_ERR_PARSE_EXHAUSTED
The parse is exhausted.
Numeric value: 53.
Suggested message: "The parse is exhausted".
@end deftypevr
@deftypevr Macro int MARPA_ERR_PARSE_TOO_LONG
The parse is too long.
The limit on the length of a parse is implementation
dependent, but it is very large,
at least 500,000,000 earlemes.
This error code is unlikely in the standard input model.
Almost certainly memory would be exceeded
before it could occur.
If an application sees this error,
it almost certainly using one of the non-standard
input models.
Most often this messsage will occur because
of a request to add a single extremely long token,
perhaps as a result of an application error.
But it is also possible this error condition will
occur after the input of a large number
of long tokens.
Numeric value: 54.
Suggested message: "This input would make the parse too long".
@end deftypevr
@deftypevr Macro int MARPA_ERR_POINTER_ARG_NULL
In a method which takes pointers as arguments,
one of the pointer arguments is @code{NULL},
in a case where that is not allowed.
One such method is@ @code{marpa_r_progress_item()}.
Numeric value: 56.
Suggested message: "An argument is null when it should not be".
@end deftypevr
@deftypevr Macro int MARPA_ERR_PRECOMPUTED
An attempt was made to use a precomputed grammar
in a way that is not allowed.
Often this is an attempt to change the grammar.
Nearly every change to a grammar after
precomputation invalidates the precomputation,
and is therefore not allowed.
Numeric value: 57.
Suggested message: "This grammar is precomputed".
@end deftypevr
@deftypevr Macro int MARPA_ERR_PROGRESS_REPORT_NOT_STARTED
No recognizer progress report is currently active,
and an action has been attempted which
is inconsistent with that.
One such action would be a
@code{marpa_r_progress_item()} call.
Numeric value: 59.
Suggested message: "No progress report has been started".
@end deftypevr
@deftypevr Macro int MARPA_ERR_PROGRESS_REPORT_EXHAUSTED
The progress report is ``exhausted'' --- all its
items have been iterated through.
Numeric value: 58.
Suggested message: "The progress report is exhausted".
@end deftypevr
@deftypevr Macro int MARPA_ERR_RANK_TOO_LOW
A symbol or rule rank was specified which
was less than an implementation-defined minimum.
Implementations will always allow at least those
ranks in the range between
@minus{}134,217,727 and 134,217,727.
Numeric value: 85.
Suggested message: "Rule or symbol rank too low".
@end deftypevr
@deftypevr Macro int MARPA_ERR_RANK_TOO_HIGH
A symbol or rule rank was specified which
was greater than an implementation-defined maximum.
Implementations will always allow at least those
ranks in the range between
@minus{}134,217,727 and 134,217,727.
Numeric value: 86.
Suggested message: "Rule or symbol rank too high".
@end deftypevr
@deftypevr Macro int MARPA_ERR_RECCE_IS_INCONSISTENT
The recognizer is ``inconsistent'',
usually because the user has rejected one or
more rules or terminals,
and has not yet called
the @code{marpa_r_consistent()} method.
Numeric value: 95.
Suggested message: "The recognizer is inconsistent.
@end deftypevr
@deftypevr Macro int MARPA_ERR_RECCE_NOT_ACCEPTING_INPUT
The recognizer is not accepting input,
and the application has attempted something that
is inconsistent with that fact.
Numeric value: 60.
Suggested message: "The recognizer is not accepting input".
@end deftypevr
@deftypevr Macro int MARPA_ERR_RECCE_NOT_STARTED
The recognizer has not been started.
and the application has attempted something that
is inconsistent with that fact.
Numeric value: 61.
Suggested message: "The recognizer has not been started".
@end deftypevr
@deftypevr Macro int MARPA_ERR_RECCE_STARTED
The recognizer has been started.
and the application has attempted something that
is inconsistent with that fact.
Numeric value: 62.
Suggested message: "The recognizer has been started".
@end deftypevr
@deftypevr Macro int MARPA_ERR_RHS_IX_NEGATIVE
The index of a RHS symbol was specified,
and it was negative.
That is not allowed.
Numeric value: 63.
Suggested message: "RHS index cannot be negative".
@end deftypevr
@deftypevr Macro int MARPA_ERR_RHS_IX_OOB
A non-negative index of RHS symbol was specified,
but there is no symbol at that index.
Since the indexes are in sequence, this means the
index was greater than or equal to the rule length.
Numeric value: 64.
Suggested message: "RHS index must be less than rule length".
@end deftypevr
@deftypevr Macro int MARPA_ERR_RHS_TOO_LONG
An attempt was made to add a rule with too many
right hand side symbols.
The limit on the RHS symbol count is implementation
dependent, but it is very large,
at least 500,000,000 symbols.
This is
far beyond what is required in any current practical grammar.
An application with rules of this length is almost
certain to run into memory and other limits.
Numeric value: 65.
Suggested message: "The RHS is too long".
@end deftypevr
@deftypevr Macro int MARPA_ERR_SEQUENCE_LHS_NOT_UNIQUE
The LHS of a
sequence rule cannot be the LHS of any other rule,
whether a sequence rule or a BNF rule.
An attempt was made to violate this restriction.
Numeric value: 66.
Suggested message: "LHS of sequence rule would not be unique".
@end deftypevr
@deftypevr Macro int MARPA_ERR_START_NOT_LHS
The start symbol is not on the LHS on
any rule.
That means it could never match any possible input,
not even the null string.
Presumably, an error in writing the grammar.
Numeric value: 73.
Suggested message: "Start symbol not on LHS of any rule".
@end deftypevr
@deftypevr Macro int MARPA_ERR_SYMBOL_IS_NOT_COMPLETION_EVENT
An attempt was made to use a symbol in
a way that requires it to be
set up for completion events,
but the symbol was not set
set up for completion events.
The archtypal case is an attempt to activate completion
events for the symbol in the recognizer.
The archtypal case is an attempt to activate a completion
event in the recognizer for
a symbol that is not set up as a completion event.
Numeric value: 92.
Suggested message: "Symbol is not set up for completion events".
@end deftypevr
@deftypevr Macro int MARPA_ERR_SYMBOL_IS_NOT_NULLED_EVENT
An attempt was made to use a symbol in
a way that requires it to be
set up for nulled events,
but the symbol was not set
set up for nulled events.
The archtypal case is an attempt to activate a nulled
events in the recognizer for
a symbol that is not set up as a nulled event.
Numeric value: 93.
Suggested message: "Symbol is not set up for nulled events".
@end deftypevr
@deftypevr Macro int MARPA_ERR_SYMBOL_IS_NOT_PREDICTION_EVENT
An attempt was made to use a symbol in
a way that requires it to be
set up for predictino events,
but the symbol was not set
set up for predictino events.
The archtypal case is an attempt to activate a prediction
event in the recognizer for
a symbol that is not set up as a prediction event.
Numeric value: 94.
Suggested message: "Symbol is not set up for prediction events".
@end deftypevr
@deftypevr Macro int MARPA_ERR_SYMBOL_VALUED_CONFLICT
Unvalued symbols are a deprecated Marpa feature,
which may be avoided with
the @code{marpa_g_force_valued} method.
An unvalued symbol may take on any value,
and therefore a symbol which is unvalued at some points
cannot safely to be used to contain a value at
others.
This error indicates that such an unsafe use is
being attempted.
Numeric value: 74.
Suggested message: "Symbol is treated both as valued and unvalued".
@end deftypevr
@deftypevr Macro int MARPA_ERR_TERMINAL_IS_LOCKED
An attempt was made to change the terminal status
of a symbol to a different value
after it was locked.
Numeric value: 75.
Suggested message: "The terminal status of the symbol is locked".
@end deftypevr
@deftypevr Macro int MARPA_ERR_TOKEN_IS_NOT_TERMINAL
A token was specified whose symbol ID is not
a terminal.
Numeric value: 76.
Suggested message: "Token symbol must be a terminal".
@end deftypevr
@deftypevr Macro int MARPA_ERR_TOKEN_LENGTH_LE_ZERO
A token length was specified which is less than
or equal to zero.
Zero-length tokens are not allowed in Libmarpa.
Numeric value: 77.
Suggested message: "Token length must greater than zero".
@end deftypevr
@deftypevr Macro int MARPA_ERR_TOKEN_TOO_LONG
The token length is too long.
The limit on the length of a token
is implementation dependent, but it
is at least 500,000,000 earlemes.
An application using a token that long
is almost certain to run into some other
limit.
Numeric value: 78.
Suggested message: "Token is too long".
@end deftypevr
@deftypevr Macro int MARPA_ERR_TREE_EXHAUSTED
A Libmarpa parse tree iterator
is ``exhausted'', that is,
it has no more parses.
Numeric value: 79.
Suggested message: "Tree iterator is exhausted".
@end deftypevr
@deftypevr Macro int MARPA_ERR_TREE_PAUSED
A Libmarpa tree is ``paused''
and an operation was attempted which
is inconsistent with that fact.
Typically, this operation will be
a call of the @code{marpa_t_next()} method.
Numeric value: 80.
Suggested message: "Tree iterator is paused".
@end deftypevr
@deftypevr Macro int MARPA_ERR_UNEXPECTED_TOKEN_ID
An attempt was made to read a token
where a token with that symbol ID is not
expected.
This message can also occur when an
attempt is made to read a token
at a location where no token is expected.
Numeric value: 81.
Suggested message: "Unexpected token".
@end deftypevr
@deftypevr Macro int MARPA_ERR_UNPRODUCTIVE_START
The start symbol is unproductive.
That means it could never match any possible input,
not even the null string.
Presumably, an error in writing the grammar.
Numeric value: 82.
Suggested message: "Unproductive start symbol".
@end deftypevr
@deftypevr Macro int MARPA_ERR_VALUATOR_INACTIVE
The valuator is inactive in a context where that
should not be the case.
Numeric value: 83.
Suggested message: "Valuator inactive".
@end deftypevr
@deftypevr Macro int MARPA_ERR_VALUED_IS_LOCKED
Unvalued symbols are a deprecated Marpa feature,
which may be avoided with
the @code{marpa_g_force_valued} method.
This error code
indicates that the valued status of a symbol is locked,
and an attempt was made
to change it to a status different from the
current one.
Numeric value: 84.
Suggested message: "The valued status of the symbol is locked".
@end deftypevr
@deftypevr Macro int MARPA_ERR_SYMBOL_IS_NULLING
An attempt was made to do something with a nulling
symbol that is not allowed.
For example,
the ID of a nulling symbol cannot be an argument
to @code{marpa_r_expected_symbol_event_set} ---
because it is not possible to create an ``expected symbol'' event
for a nulling symbol.
Numeric value: 87.
Suggested message: "Symbol is nulling".
@end deftypevr
@deftypevr Macro int MARPA_ERR_SYMBOL_IS_UNUSED
An attempt was made to do something with a nulling
symbol that is not allowed.
An ``unused'' symbol is a inaccessible or unproductive symbol.
For example,
the ID of a unused symbol cannot be an argument
to @code{marpa_r_expected_symbol_event_set} ---
because it is not possible to create an ``expected symbol'' event
for a nulling symbol.
Numeric value: 88.
Suggested message: "Symbol is not used".
@end deftypevr
@node Internal error codes, , External error codes, Error methods macros and codes
@section Internal error codes
An internal error code may be one of two things:
First,
it can be an error code which
arises from an internal Libmarpa programming issue
(in other words, something happening in the code
that was not supposed to be able to happen.)
Second, it can be an error code which only occurs
when a method from Libmarpa's internal interface
is used.
Both kinds of internal error message share one common
trait --- users of the Libmarpa's external interface
should never see them.
Internal error messages
require someone with knowledge of the Libmarpa internals
to follow up on them.
They usually do not have descriptions or suggested messages.
@deftypevr Macro int MARPA_ERR_AHFA_IX_NEGATIVE
Numeric value: 1.
@end deftypevr
@deftypevr Macro int MARPA_ERR_AHFA_IX_OOB
Numeric value: 2.
@end deftypevr
@deftypevr Macro int MARPA_ERR_ANDID_NEGATIVE
Numeric value: 3.
@end deftypevr
@deftypevr Macro int MARPA_ERR_ANDID_NOT_IN_OR
Numeric value: 4.
@end deftypevr
@deftypevr Macro int MARPA_ERR_ANDIX_NEGATIVE
Numeric value: 5.
@end deftypevr
@deftypevr Macro int MARPA_ERR_BOCAGE_ITERATION_EXHAUSTED
Numeric value: 7.
@end deftypevr
@deftypevr Macro int MARPA_ERR_DEVELOPMENT
"Development" errors were used heavily during
Libmarpa's development,
when it was not yet clear how precisely
to classify every error condition.
Unless they are using a developer's version,
users of the external interface
should never
see development errors.
Development errors have an error string
associated with them.
The error string is a
short 7-bit ASCII error string
which describes the error.
Numeric value: 9.
Suggested message: "Development error, see string".
@end deftypevr
@deftypevr Macro int MARPA_ERR_DUPLICATE_AND_NODE
Numeric value: 10.
@end deftypevr
@deftypevr Macro int MARPA_ERR_YIM_ID_INVALID
Numeric value: 14.
@end deftypevr
@deftypevr Macro int MARPA_ERR_INTERNAL
A ``catchall'' internal error.
Numeric value: 19.
@end deftypevr
@deftypevr Macro int MARPA_ERR_INVALID_AHFA_ID
The AHFA ID was invalid. There are no AHFAs
any more, so this message should not occur.
Numeric value: 20.
@end deftypevr
@deftypevr Macro int MARPA_ERR_INVALID_AIMID
The AHM ID was invalid. The term ``AIMID''
is a legacy of earlier implementations and must
be kept for backward compatibility.
Numeric value: 21.
@end deftypevr
@deftypevr Macro int MARPA_ERR_INVALID_IRLID
Numeric value: 23.
@end deftypevr
@deftypevr Macro int MARPA_ERR_INVALID_NSYID
Numeric value: 24.
@end deftypevr
@deftypevr Macro int MARPA_ERR_NOOKID_NEGATIVE
Numeric value: 33.
@end deftypevr
@deftypevr Macro int MARPA_ERR_NOT_TRACING_COMPLETION_LINKS
Numeric value: 35.
@end deftypevr
@deftypevr Macro int MARPA_ERR_NOT_TRACING_LEO_LINKS
Numeric value: 36.
@end deftypevr
@deftypevr Macro int MARPA_ERR_NOT_TRACING_TOKEN_LINKS
Numeric value: 37.
@end deftypevr
@deftypevr Macro int MARPA_ERR_NO_AND_NODES
Numeric value: 38.
@end deftypevr
@deftypevr Macro int MARPA_ERR_NO_OR_NODES
Numeric value: 40.
@end deftypevr
@deftypevr Macro int MARPA_ERR_NO_TRACE_YS
Numeric value: 46.
@end deftypevr
@deftypevr Macro int MARPA_ERR_NO_TRACE_PIM
Numeric value: 47.
@end deftypevr
@deftypevr Macro int MARPA_ERR_NO_TRACE_YIM
Numeric value: 45.
@end deftypevr
@deftypevr Macro int MARPA_ERR_NO_TRACE_SRCL
Numeric value: 48.
@end deftypevr
@deftypevr Macro int MARPA_ERR_ORID_NEGATIVE
Numeric value: 51.
@end deftypevr
@deftypevr Macro int MARPA_ERR_OR_ALREADY_ORDERED
Numeric value: 52.
@end deftypevr
@deftypevr Macro int MARPA_ERR_PIM_IS_NOT_LIM
Numeric value: 55.
@end deftypevr
@deftypevr Macro int MARPA_ERR_SOURCE_TYPE_IS_NONE
Numeric value: 70.
@end deftypevr
@deftypevr Macro int MARPA_ERR_SOURCE_TYPE_IS_TOKEN
Numeric value: 71.
@end deftypevr
@deftypevr Macro int MARPA_ERR_SOURCE_TYPE_IS_COMPLETION
Numeric value: 68.
@end deftypevr
@deftypevr Macro int MARPA_ERR_SOURCE_TYPE_IS_LEO
Numeric value: 69.
@end deftypevr
@deftypevr Macro int MARPA_ERR_SOURCE_TYPE_IS_AMBIGUOUS
Numeric value: 67.
@end deftypevr
@deftypevr Macro int MARPA_ERR_SOURCE_TYPE_IS_UNKNOWN
Numeric value: 72.
@end deftypevr
@node Design notes, Work in Progress, Error methods macros and codes, Top
@chapter Design notes
Design apologetics are
throughout this document,
dispersed among sections where it is
hoped that they motivate the description or discussion.
The notes in this section
did not find a home elsewhere.
@menu
* Why so many time objects::
* Design of numbered objects::
* LHS Terminals::
@end menu
@node Why so many time objects, Design of numbered objects, Design notes, Design notes
@section Why so many time objects?
Marpa is an aggressively multi-pass algorithm.
Marpa achieves its efficiency,
not in spite of making multiple
passes over the data, but because of it.
Marpa regularly substitutes
two fast O(@var{n}) passes for a single
O(@var{n} log @var{n}) pass.
Marpa's proliferation of time objects is in
keeping with its multi-pass approach.
Bocage objects come at no cost,
even for unambiguous parses,
because the same pass which creates the bocage
also deals with other issues which are of major
significance for unambiguous parses.
It is the post-processing of the bocage pass
that enables Marpa to do both left-
and right-recursion in linear time.
Of the various objects, the best
case for elimination is of the
ordering object.
In many cases, the ordering is trivial.
Either the parse is unambiguous, or the
application does not care about the order in
which parses are returned.
But while it would be easy to add an option
to bypass creation of an ordering object,
there is little to be gained from it.
When the ordering is trivial,
its overhead is very small ---
essentially a handful of subroutine calls.
Many orderings accomplish nothing,
but these cost next to nothing.
Tree objects come at minimal cost to unambiguous grammars,
because the same pass that allows iteration through multiple
parse trees does the tree traversal.
This eliminates much of the work that otherwise would
need to be done in
the valuation time object.
In the current implement, the valuation time object
needs only to step through a sequence already determined
in the tree iterator.
@node Design of numbered objects, LHS Terminals, Why so many time objects, Design notes
@section Numbered objects
As the name suggests,
the choice was made to implement
numbered objects as integers,
and not as
pointers.
In standard-conformant C,
integers can be safely checked for validity,
while pointers cannot.
There are efficiency tradeoffs between pointers and
integers but they are complicated,
and they go both ways.
Pointers can be faster, but integers can be used
as indexes into more than one data structure.
Which is actually faster depends on the design.
Integers allow for a more flexible design,
so that once the choice is settled on,
careful programming can make them a win,
possibly a very big one.
The approach taken in Libmarpa was to settle,
from the outset,
on integers as the implementation for numbered
objects,
and to optimize on that basis.
The author concedes that it is
possible that
others redoing Libmarpa from scratch
might find that pointers are faster.
But the author is confident
that they will also discover,
on modern architectures,
that the lack of safe validity checking
is far too high a price to pay
for the difference in speed.
@node LHS Terminals, , Design of numbered objects, Design notes
@section LHS terminals
Marpa's idea
in losing the sharp division between terminals
and non-terminals is that the distinction,
while helpful for proving theorems,
is not essential in practice.
LHS symbols
in the input might be useful for
``short circuiting'' the rules in which they occur.
This may
prove helpful in debugging, or have other applications.
However,
it also can be useful,
for checking input validity as well as for efficiency,
to follow tradition and distinguish
non-terminals from terminals.
For this reason,
the traditional behavior is the default
in Libmarpa.
@node Work in Progress, Deprecated techniques and methods, Design notes, Top
@chapter Work in Progress
@menu
* Untested methods::
@end menu
@node Untested methods, , Work in Progress, Work in Progress
@section Untested methods
The methods of this section are not in the external interface,
because they have not been adequately tested.
Their fate is uncertain.
Users should regard these methods
as unsupported.
@menu
* Ranking methods::
* Zero-width assertion methods::
* Methods for revising parses::
@end menu
@node Ranking methods, Zero-width assertion methods, Untested methods, Untested methods
@subsection Ranking methods
@deftypefun Marpa_Rank marpa_g_default_rank ( @
Marpa_Grammar @var{g})
@deftypefunx Marpa_Rank marpa_g_default_rank_set ( @
Marpa_Grammar @var{g}, @
Marpa_Rank @var{rank})
These methods, respectively, set
and query the default rank of the grammar.
When a grammar is created, the default rank is 0.
When rules and symbols are created, their rank is
the default rank of the grammar.
Changing the grammar's default rank does not affect
those
rules and symbols already created,
only those that will be created.
This means that
the grammar's default rank can be used to,
in effect, assign ranks to groups of rules and symbols.
Applications may find this behavior useful.
Return value: On success, returns
the rank @strong{after}
the call,
and sets the error code to
@code{MARPA_ERR_NONE}.
On failure, returns @minus{}2,
and sets the error code to an appropriate
value, which will never be
@code{MARPA_ERR_NONE}.
Note that when the rank is @minus{}2,
the error code is the only way to distinguish
success from failure.
The error code can be determined by using the
@code{marpa_g_error()} call.
@end deftypefun
@deftypefun Marpa_Rank marpa_g_symbol_rank ( @
Marpa_Grammar @var{g}, @
Marpa_Symbol_ID sym_id)
@deftypefunx Marpa_Rank marpa_g_symbol_rank_set ( @
Marpa_Grammar @var{g}, @
Marpa_Symbol_ID @var{sym_id}, @
Marpa_Rank @var{rank})
These methods, respectively, set
and query the rank of a symbol @var{sym_id}.
When @var{sym_id} is created, its rank
initialized to the default rank of the grammar.
Return value: On success, returns
the rank @strong{after}
the call,
and sets the error code to
@code{MARPA_ERR_NONE}.
On failure, returns @minus{}2,
and sets the error code to an appropriate
value, which will never be
@code{MARPA_ERR_NONE}.
Note that when the rank is @minus{}2,
the error code is the only way to distinguish
success from failure.
The error code can be determined by using the
@code{marpa_g_error()} call.
@end deftypefun
@node Zero-width assertion methods, Methods for revising parses, Ranking methods, Untested methods
@subsection Zero-width assertion methods
@deftypefun Marpa_Assertion_ID marpa_g_zwa_new ( @
Marpa_Grammar @var{g}, @
int @var{default_value})
@end deftypefun
@deftypefun int marpa_g_zwa_place ( @
Marpa_Grammar @var{g}, @
Marpa_Assertion_ID @var{zwaid}, @
Marpa_Rule_ID @var{xrl_id}, @
int @var{rhs_ix})
@end deftypefun
@deftypefun int marpa_r_zwa_default ( @
Marpa_Recognizer @var{r}, @
Marpa_Assertion_ID @var{zwaid})
On success, returns previous default value of the assertion.
@end deftypefun
@deftypefun int marpa_r_zwa_default_set ( @
Marpa_Recognizer @var{r}, @
Marpa_Assertion_ID @var{zwaid}, @
int @var{default_value})
Changes default value to @var{default_value}.
On success, returns previous default value of the assertion.
@end deftypefun
@deftypefun Marpa_Assertion_ID marpa_g_highest_zwa_id ( @
Marpa_Grammar @var{g} )
@end deftypefun
@node Methods for revising parses, , Zero-width assertion methods, Untested methods
@subsection Methods for revising parses
Marpa allows an application to ``change its mind'' about a parse,
rejected rule previously recognized or predicted,
and terminals previously scanned.
The methods in this section provide that capability.
@deftypefun Marpa_Earleme marpa_r_clean ( @
Marpa_Recognizer @var{r})
@end deftypefun
@node Deprecated techniques and methods, GNU Free Documentation License, Work in Progress, Top
@chapter Deprecated techniques and methods
@menu
* Valued and unvalued symbols::
@end menu
@node Valued and unvalued symbols, , Deprecated techniques and methods, Deprecated techniques and methods
@section Valued and unvalued symbols
@menu
* What unvalued symbols were::
* Grammar methods dealing with unvalued symbols::
* Registering semantics in the valuator::
@end menu
@node What unvalued symbols were, Grammar methods dealing with unvalued symbols, Valued and unvalued symbols, Valued and unvalued symbols
@subsection What unvalued symbols were
Libmarpa symbols can have values,
which is the traditional way of doing semantics.
Libmarpa also allows symbols to be unvalued.
An @dfn{unvalued} symbol is one whose value
is unpredictable from instance to instance.
If a symbol is unvalued, we sometimes say that it
has ``whatever'' semantics.
Situations where the semantics can tolerate unvalued symbols
are surprisingly frequent.
For example, the top-level of many languages is a series
of major units, all of whose semantics are typically accomplished
via side effects.
The compiler is typically indifferent to the actual value produced
by these major units, and tracking them is a waste of time.
Similarly, the value of the separators in a list is typically
ignored.
Rules are unvalued if and only if their LHS symbols
are unvalued.
When rules and symbols are unvalued,
Libmarpa optimizes their evaluation.
It is in principle unsafe to check the value
of a symbol if it can be unvalued.
For this reason,
once a symbol has been treated as valued,
Libmarpa marks it as valued.
Similarly,
once a symbol has been treated as unvalued,
Libmarpa marks it as unvalued.
Once marked, a symbol's valued status is
@dfn{locked} and cannot be changed later.
The valued status of terminals is marked the first
time they are read.
The valued status of LHS symbols must be explicitly
marked by the application when initializing the
valuator --- this is Libmarpa's equivalent of
registering a callback.
LHS terminals are disabled by default.
If allowed, the user should be aware that the valued
status of a LHS terminal
will be locked in the recognizer
if it is used as a terminal,
and the symbol's use as a rule LHS
in the valuator must be
consistent with the recognizer's marking.
Marpa reports an error when a symbol's use
conflicts with its locked valued status.
Doing so usually saves the programmer
some tricky debugging further down the road.
@node Grammar methods dealing with unvalued symbols, Registering semantics in the valuator, What unvalued symbols were, Valued and unvalued symbols
@subsection Grammar methods dealing with unvalued symbols
@deftypefun int marpa_g_symbol_is_valued ( @
Marpa_Grammar @var{g}, @
Marpa_Symbol_ID @var{symbol_id})
@deftypefunx int marpa_g_symbol_is_valued_set ( @
Marpa_Grammar @var{g}, @
Marpa_Symbol_ID @var{symbol_id}, @
int value)
These methods, respectively, set
and query the ``valued status'' of a symbol.
Once set to a value with the
@code{marpa_g_symbol_is_valued_set()} method,
the valued status of a symbol is ``locked'' at that value.
It cannot thereafter be changed.
Subsequent calls to
@code{marpa_g_symbol_is_valued_set()}
for the same @var{sym_id} will fail,
leaving @var{sym_id}'s valued status unchanged,
unless @var{value} is the same as the locked-in value.
Return value: On success, 1 if the symbol @var{symbol_id}
is valued after the call, 0 if not.
If the valued status is locked and @var{value}
is different from the current status, @minus{}2.
If @var{value} is not 0 or 1;
or on other failure, @minus{}2.
@end deftypefun
@node Registering semantics in the valuator, , Grammar methods dealing with unvalued symbols, Valued and unvalued symbols
@subsection Registering semantics in the valuator
By default, Libmarpa's valuator objects
assume that
non-terminal symbols have
no semantics.
The archetypal application will need to register
symbols that contain semantics.
The primary method for doing this is
@code{marpa_v_symbol_is_valued()}.
Applications will typically register semantics by rule,
and these applications will find
the @code{marpa_v_rule_is_valued()} method more convenient.
@deftypefun int marpa_v_symbol_is_valued ( @
Marpa_Value @var{v}, @
Marpa_Symbol_ID @var{sym_id} )
@deftypefunx int marpa_v_symbol_is_valued_set ( @
Marpa_Value @var{v}, @
Marpa_Symbol_ID @var{sym_id}, @
int @var{value} )
These methods, respectively,
discover and set to @var{value},
the valued status for symbol @var{sym_id}.
A valued status of 1 indicates that the symbol is valued.
A valued status of 0 indicates that the symbol is unvalued.
If the valued status is locked,
an attempt to change to a status different from the
current one will fail
(error code @code{MARPA_ERR_VALUED_IS_LOCKED}).
Return value: On success, the valued status @strong{after}
the call.
If @var{value} is not either 0 or 1,
or on other failure, @minus{}2.
@end deftypefun
@deftypefun int marpa_v_rule_is_valued ( @
Marpa_Value @var{v}, @
Marpa_Rule_ID @var{rule_id} )
@deftypefunx int marpa_v_rule_is_valued_set ( @
Marpa_Value @var{v}, @
Marpa_Rule_ID @var{rule_id}, @
int @var{value} )
These methods respectively,
discover and set to @var{value},
the valued status
for the LHS symbol of rule @var{rule_id}.
A valued status of 1 indicates that the symbol is valued.
A valued status of 0 indicates that the symbol is unvalued.
If the valued status is locked,
an attempt to change to a status different from the
current one will fail
(error code @code{MARPA_ERR_VALUED_IS_LOCKED}).
Rules have no valued status of their own.
The valued status of a rule
is always that of its LHS symbol.
These methods are conveniences --- they
save the application the trouble of looking
up the rule's LHS.
Return value: On success, the valued status of the
rule @var{rule_id}'s LHS symbol @strong{after}
the call.
If @var{value} is not either 0 or 1,
or on other failure, @minus{}2.
@end deftypefun
@deftypefun int marpa_v_valued_force ( @
Marpa_Value @var{v})
This methods locks the valued status of all symbols
to 1, indicated that the symbol is valued.
If this is not possible, for example because one of
the grammar's symbols already is locked at a valued
status of 0,
failure is returned.
Return value: On success, a non-negative number.
On failure, returns @minus{}2,
and sets the error code to an appropriate
value, which will never be
@code{MARPA_ERR_NONE}.
@end deftypefun
@node GNU Free Documentation License, , Deprecated techniques and methods, Top
@appendix GNU Free Documentation License
@include fdl-1.3.texi
@bye
@c vim: expandtab shiftwidth=4:
|