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
let version = "20191116"
let list_map f l = List.rev (List.rev_map f l)
include Dyplex
open Printf
type ('token,'obj,'data,'local_data,'lexbuf) dypgen_toolbox = {
parser_pilot : ('token,'obj,'data,'local_data,'lexbuf) parser_pilot;
global_data : 'data;
local_data : 'local_data;
last_local_data : 'local_data;
next_lexeme : unit -> string list;
symbol_start : unit -> int;
symbol_start_pos : unit -> Lexing.position;
symbol_end : unit -> int;
symbol_end_pos : unit -> Lexing.position;
rhs_start : int -> int;
rhs_start_pos : int -> Lexing.position;
rhs_end : int -> int;
rhs_end_pos : int -> Lexing.position;
print_state : out_channel -> unit;
print_grammar : out_channel -> unit;
}
type ('token,'obj,'gd,'ld,'l) dyp_action =
| Add_rules of
(rule * (('token,'obj,'gd,'ld,'l) dypgen_toolbox ->
('obj list -> 'obj * ('token,'obj,'gd,'ld,'l) dyp_action list))) list
| Bind_to_cons of (string * string) list
| Global_data of 'gd
| Keep_grammar
| Parser of ('token,'obj,'gd,'ld,'l) parsing_device
| Local_data of 'ld
| Next_grammar of out_channel
| Next_state of out_channel
| Relation of string list list
| Dont_shift
exception Giveup
exception Undefined_nt of string
exception Undefined_ter of string
exception Bad_constructor of (string * string * string)
exception Constructor_mismatch of (string * string)
exception Syntax_error
(** This exception is raised by the function parse if the parser is
stuck in a situtation where no shift and no reduction is possible. *)
(** The way this module handles GLR is documented in the following paper :
Scott McPeak, Elkhound: A fast, practical GLR parser generator, University of
California Berkeley, Report No. UCB/CSD-2-1214, December 2002 ; figures 7 and 8.
Optimizations which are specific to Elkhound are not implemented.
This implementation adds dynamic changes to the grammar at run-time during
parsing. These modifications take place in the function reduceViaPath.
When a reduction is performed the parser checks whether the action is 'classic'
(no change to the grammar) or 'dynamic' and proceeds with the modifications to
the grammar and the automaton if needed. If the parsing follows simultaneously
several paths, as it may be the case with GLR parsing, then there is a distinct
grammar and a distinct automaton for each path if needed.
Following Scott McPeak's report the stack is implemented as a graph.
*)
let countred = ref 0
type counters = {
mutable countsn : int;
mutable counted : int;
mutable count_token : int;
mutable last_layout : int;
mutable count_g : int;
mutable count_lex : int }
let new_counters () = {
countsn = 0;
counted = 0;
count_token = 0;
last_layout = 0;
count_g = 0;
count_lex = 0 }
module Ordered_urule =
struct
type t = nrule
let compare = Stdlib.compare
end
module Urule_map = Map.Make(Ordered_urule)
module NTS = Set.Make(Ordered_non_ter)
module Tools =
struct
type ('token,'obj,'gd,'ld,'l) action_com = {
ac_gd : 'gd;
ac_ld : 'ld;
add_rules : (rule * (('token,'obj,'gd,'ld,'l) dypgen_toolbox ->
('obj list -> 'obj * ('token,'obj,'gd,'ld,'l) dyp_action list))) list;
new_nt_cons : (string * string) list;
ac_relations : string list list;
will_shift : bool;
keep_grammar : bool;
ac_parser : ('token,'obj,'gd,'ld,'l) parsing_device option;
next_state : out_channel option;
next_grammar : out_channel option }
let rec action_command dal da = match dal with
| [] -> da
| (Global_data gd)::t -> action_command t { da with ac_gd = gd }
| (Local_data ld)::t -> action_command t { da with ac_ld = ld }
| (Add_rules ar)::t ->
action_command t { da with add_rules = ar@da.add_rules }
| (Bind_to_cons l)::t ->
action_command t { da with new_nt_cons = l@da.new_nt_cons }
| (Relation l)::t ->
action_command t { da with ac_relations = l@da.ac_relations }
| Dont_shift::t -> action_command t { da with will_shift = false }
| Keep_grammar::t -> action_command t { da with keep_grammar = true }
| (Parser pdev)::t -> action_command t { da with ac_parser = Some pdev }
| (Next_state ns)::t -> action_command t { da with next_state = Some ns }
| (Next_grammar ng)::t -> action_command t { da with next_grammar = Some ng }
let make_dyp symbol_pos position_list data_arg local_data_arg
last_local_data_arg debug_infos parser_pilot next_lexeme =
{ parser_pilot = parser_pilot;
global_data = data_arg;
local_data = local_data_arg;
last_local_data = last_local_data_arg;
next_lexeme = next_lexeme;
symbol_start = (fun () -> (fst symbol_pos).Lexing.pos_cnum);
symbol_start_pos = (fun () -> fst symbol_pos);
symbol_end = (fun () -> (snd symbol_pos).Lexing.pos_cnum);
symbol_end_pos = (fun () -> snd symbol_pos);
rhs_start =
(fun i -> (fst (List.nth position_list (i-1))).Lexing.pos_cnum);
rhs_start_pos = (fun i -> fst (List.nth position_list (i-1)));
rhs_end = (fun i -> (snd (List.nth position_list (i-1))).Lexing.pos_cnum);
rhs_end_pos = (fun i -> snd (List.nth position_list (i-1)));
print_state =
debug_infos.prt_state;
print_grammar =
debug_infos.prt_grammar
}
let rec transform_action a av_list symbol_pos position_list data_arg
local_data_arg last_local_data_arg debug_infos parser_pilot next_lexeme =
let dyp =
make_dyp symbol_pos position_list data_arg local_data_arg
last_local_data_arg debug_infos parser_pilot next_lexeme
in
let new_obj, dal = a dyp av_list in
let mapfun (r, ac) = (r, (Dypgen_action
(fun ol pos posl gd ld lld di p nl ->
(transform_action ac) ol pos posl gd ld lld di p nl)), [])
in
let dyp_a = action_command dal {
ac_gd = data_arg;
ac_ld = local_data_arg;
add_rules = [];
new_nt_cons = [];
ac_relations = [];
will_shift = true;
keep_grammar = false;
ac_parser = None;
next_state = None;
next_grammar = None }
in
let add_rules_transformed = List.map mapfun dyp_a.add_rules in
(new_obj, dyp_a.will_shift, dyp_a.keep_grammar, dyp_a.ac_gd,
dyp_a.ac_ld, add_rules_transformed, dyp_a.new_nt_cons,
dyp_a.ac_relations, dyp_a.next_state, dyp_a.next_grammar, dyp_a.ac_parser)
let transform_inh_val a av_list symbol_pos position_list data_arg
local_data_arg last_local_data_arg debug_infos parser_pilot next_lexeme =
let dyp =
make_dyp symbol_pos position_list data_arg local_data_arg
last_local_data_arg debug_infos parser_pilot next_lexeme
in
a dyp av_list
let keep_zero l = match l with
| (_,gd,ld)::_ -> [], gd, ld
| [] -> assert false
let array_of_list =
let rec aux a l n = match l with
| [] -> a
| h::t -> a.(n) <- h; aux a t (n+1)
in
fun l -> match l with
| [] -> [||]
| h::_ -> let a = Array.make (List.length l) h in aux a l 0
let hashtbl_of_array a =
let len = Array.length a in
if len = 0 then failwith "hashtbl_of_array" else
let ht = Hashtbl.create len in
for i=0 to len-1 do
Hashtbl.add ht a.(i) i
done;
ht
let make_nt_cons_map nt_cons_list =
List.fold_left
(fun ncm (nt_str,cons_i) -> String_map.add nt_str cons_i ncm)
String_map.empty nt_cons_list
end
open Tools
let dummy_lhs = (0,0,0)
let make_real_grammar user_g str_non_ter nt_table _ =
if !dypgen_verbose>1 then
(let nbr = Urule_map.fold (fun _ _ n -> n+1) user_g 0 in
Printf.fprintf !log_channel "size of the grammar : %d rules\n" nbr);
let nt_nb = Hashtbl.length nt_table + 1 in
if !dypgen_verbose>1 then
Printf.fprintf !log_channel "number of non terminals : %d\n" nt_nb;
if false then
(let check_nt_def = Array.make nt_nb (false,false) in
let rec f4 rhs = match rhs with
| (Ps_Non_ter (nt,_))::tl
| (Ps_Non_ter_NL (nt,_))::tl ->
let b,_ = check_nt_def.(nt) in
check_nt_def.(nt) <- (b,true);
f4 tl
| _::tl -> f4 tl
| [] -> ()
in
let f3 (nt,rhs,_,_) _ =
let _,b = check_nt_def.(nt) in
check_nt_def.(nt) <- (true,b);
f4 rhs
in
Urule_map.iter f3 user_g;
for i=0 to nt_nb-1 do
if check_nt_def.(i) = (false,true) then
raise (Undefined_nt(str_non_ter.(i)))
done);
let newnt_nb = Array.length str_non_ter in
let g = Array.make newnt_nb Map_rhs.empty in
let array_of_list l =
let a = Array.make (List.length l) (Ps_Ter 0) in
let _ = List.fold_left (fun i x -> a.(i) <- x; (i+1)) 0 l in
a
in
Urule_map.iter
(fun (ind, litl, _, b) a ->
g.(ind) <- Map_rhs.add ((array_of_list litl), b) a g.(ind))
user_g;
let nbr =
let rec f nt i =
if nt = newnt_nb then i else
f (nt+1) (Map_rhs.fold (fun _ _ i -> i+1) g.(nt) i) in
f 0 0
in
if !dypgen_verbose>1 then
(Printf.fprintf !log_channel
"size of the extended grammar : %d rules\n" nbr;
Printf.fprintf !log_channel
"number of non terminals : %d\n" newnt_nb;
flush stdout);
g, nt_nb, nbr
module Ordered_nrule =
struct
type t = nrule
let compare = Stdlib.compare
end
module RS = Set.Make (Ordered_nrule)
let make_nullable (gram_rhs:rhs array) lhs_table sub_g nullable =
let f rn b =
let _,_,ind = lhs_table.(rn) in
if nullable.(ind) then b else
let rec all_nullable rhs = function 0 -> true | i ->
match rhs.(i-1) with
| Ps_Non_ter (nt, _) | Ps_Non_ter_NL (nt, _) ->
if nullable.(nt) then all_nullable rhs (i-1) else false
| _ -> assert false
in
let rhs = gram_rhs.(rn) in
if all_nullable rhs (Array.length rhs)
then (nullable.(ind) <- true; true) else b
in
let rec aux () = if Int_set.fold f sub_g false then aux () in
aux ()
let closure_of_include (nodes:int list array) (result:Int_set.t array) =
let node_nb = Array.length nodes in
let color = Array.make node_nb `white in
let root = Array.make node_nb (-1) in
let d = Array.make node_nb (-1) in
let rec pop res date = function
| (w,d)::t when d >= date ->
result.(w) <- res;
color.(w) <- `blue;
pop res date t
| stack -> stack
in
let rec visit time stack visited v =
if d.(v) = -1 then
(color.(v) <- `gray;
d.(v) <- time;
root.(v) <- time);
match nodes.(v) with
| w::t ->
nodes.(v) <- t;
if color.(w)=`white then visit (time+1) stack (v::visited) w
else
(if (color.(w)=`blue || color.(w)=`black) && d.(v) >= d.(w)
then result.(v) <- Int_set.union result.(v) result.(w);
if color.(w) <> `blue && root.(w) < root.(v)
then root.(v) <- root.(w);
visit time stack visited v)
| [] ->
let stack =
if root.(v) = d.(v) then
(color.(v) <- `blue;
pop result.(v) d.(v) stack)
else
(color.(v) <- `black;
(v,time)::stack)
in
(match visited with
| u::t ->
result.(u) <- Int_set.union result.(u) result.(v);
visit time stack t u
| [] -> time)
in
let rec aux id time =
if id < node_nb then
let time = visit time [] [] id in
aux (id+1) time
in
aux 0 0
let array_for_all f a =
let rec aux i =
if i=0 then true else
if f (i-1) a.(i-1) then aux (i-1) else false
in
aux (Array.length a)
let make_po_array gram_rhs lhs_table sub_g nullable po_ht lhs_nb cyclic_rule =
let result = Array.make lhs_nb Int_set.empty in
let nodes = Array.make lhs_nb [] in
let check_cyclic_rule = Array.make (Array.length cyclic_rule) [] in
let f rn =
let _, _, ind = lhs_table.(rn) in
let rhs = Array.map
(fun symb -> match symb with
| Ps_Non_ter (nt, _) | Ps_Non_ter_NL (nt, _) ->
nt, nullable.(nt)
| _ -> assert false)
gram_rhs.(rn)
in
let g i (ind', _) =
if array_for_all (fun j x -> i=j || snd x) rhs then
if not (Hashtbl.mem po_ht (ind, ind')) then
(Hashtbl.add po_ht (ind, ind') true;
nodes.(ind) <- ind'::nodes.(ind);
result.(ind) <- Int_set.add ind' result.(ind);
check_cyclic_rule.(rn) <- (i, ind')::check_cyclic_rule.(rn))
in
Array.iteri g rhs
in
Int_set.iter f sub_g;
closure_of_include nodes result;
for i=0 to lhs_nb -1 do
Int_set.iter (fun j -> Hashtbl.add po_ht (i, j) true) result.(i)
done;
for i=0 to (Array.length cyclic_rule) -1 do
let ind, _, _ = lhs_table.(i) in
cyclic_rule.(i) <- List.fold_left
(fun res (j, ind') -> match res with
| k::_ when k=j -> res
| _ -> if (Hashtbl.mem po_ht (ind, ind'))
&& (Hashtbl.mem po_ht (ind', ind))
then j::res else res)
[] check_cyclic_rule.(i)
done
let htc ht i1 i2 = Hashtbl.mem ht (i1, i2)
let int_set_of_list l =
List.fold_left (fun res x -> Int_set.add x res) Int_set.empty l
let list_of_int_set s = Int_set.fold (fun x res -> x::res) s []
let compute_L gram_rhs gram_lhs =
let n = Array.length gram_lhs in
let r_L = Array.make n [] in
let marked = Array.make n false in
for i=0 to n-1 do
let l =
List.fold_left (fun l rn -> match gram_rhs.(rn).(0) with
| Ps_Non_ter (nt, _) | Ps_Non_ter_NL (nt, _) ->
if marked.(nt) then l
else (marked.(nt) <- true; nt::l)
| _ -> l) [] (fst gram_lhs.(i))
in
let l2 = if marked.(i) then l else i::l in
r_L.(i) <- l2;
List.iter (fun j -> marked.(j) <- false) l
done;
let result = Array.make n Int_set.empty in
for i=0 to n-1 do
result.(i) <- int_set_of_list r_L.(i)
done;
closure_of_include r_L result;
for i=0 to n-1 do
r_L.(i) <- list_of_int_set result.(i)
done;
r_L
let str_rule rn gram_rhs lhs_table str_non_ter str_ter regexp_array =
Printf.sprintf "%s -> %s"
(str_lhs lhs_table.(rn) str_non_ter)
(str_handle gram_rhs.(rn) (-1) str_non_ter str_ter regexp_array)
let print_grammar chan gram_rhs lhs_table str_non_ter str_ter regexp_array =
for i=0 to (Array.length lhs_table)-1 do
Printf.fprintf chan "rn:%d %s\n" i
(str_rule i gram_rhs lhs_table str_non_ter str_ter regexp_array)
done;
print_newline ()
let remove_implicit gram_lhs implicit =
Array.init (Array.length gram_lhs)
(fun i ->
let rl, er = gram_lhs.(i) in
List.filter (fun rn -> not implicit.(rn)) rl, er)
let create_aut gram_rhs gram_lhs gram_parnt bnt_array r_L it_nb ist_nt_nb lhs_table str_non_ter entry_points_list _ token_nb str_ter regexp_array implicit_rule =
let () = countst := 0 in
let is_trace =
((Array.make token_nb Map_is.empty),
(Array.make token_nb Map_is.empty)),
((Array.make ist_nt_nb Map_is.empty),
(Array.make ist_nt_nb Map_is.empty))
in
let gram_lhs' = remove_implicit gram_lhs implicit_rule in
let state_list, n, stations, entry_points, is_trace =
build_automaton is_trace gram_rhs gram_lhs gram_lhs' gram_parnt bnt_array
!dypgen_verbose
it_nb
r_L lhs_table ist_nt_nb token_nb str_non_ter
str_ter entry_points_list regexp_array implicit_rule
in
state_list, n, stations, entry_points, is_trace
let list_of_array a =
let rec aux i l =
if i=(-1) then l else
aux (i-1) (a.(i)::l)
in
aux (Array.length a -1) []
let create_po (gram_rhs:rhs array) lhs_table lhs_nb str_non_ter =
let time1 = Sys.time () in
let po_ht = Hashtbl.create lhs_nb in
let nullable = Array.make lhs_nb false in
let rec rhs_tokless rhs = function 0 -> true | i ->
match rhs.(i-1) with
| (Ps_Ter _) | (Ps_Ter_NL _) -> false
| (Ps_Non_ter _) | (Ps_Non_ter_NL _) -> rhs_tokless rhs (i-1)
in
let rnb = Array.length gram_rhs in
let rec f i sub_g =
if i=rnb then sub_g else
let _,_,ind = lhs_table.(i) in
let sub_g = match gram_rhs.(i) with
| [||] -> nullable.(ind) <- true; sub_g
| rhs ->
if rhs_tokless rhs (Array.length rhs)
then Int_set.add i sub_g else sub_g
in
f (i+1) sub_g
in
let sub_g = f 0 Int_set.empty in
let cyclic_rule = Array.make (Array.length gram_rhs) [] in
make_nullable gram_rhs lhs_table
sub_g nullable;
make_po_array gram_rhs lhs_table
sub_g nullable po_ht lhs_nb cyclic_rule;
if !dypgen_verbose>1 then
for i=0 to lhs_nb -1 do
if Hashtbl.mem po_ht (i, i) then
(Printf.printf
"Warning: non terminal `%s' can derive itself\n"
str_non_ter.(i)
)
done;
let time2 = Sys.time () in
if !dypgen_verbose>1 then
(Printf.fprintf !log_channel "po_ht built, %.3f sec\n" (time2-.time1);
flush stdout) else ();
po_ht, cyclic_rule
let print_table_state chan i table table_it table_lit_trans gram_rhs lhs_table str_non_ter str_ter regexp_array =
let f tab name_fun =
Hashtbl.iter (fun i u -> Printf.printf "(%s,%d) " (name_fun i) u) tab
in
Printf.fprintf chan " State %d\n" i;
Printf.fprintf chan " li: %s\n items:\n"
(str_literal_trans table_lit_trans.(i) str_non_ter str_ter);
print_item_set chan table_it.(i) gram_rhs lhs_table
str_non_ter str_ter regexp_array;
Printf.fprintf chan " next states (shift):\n ";
f table.(i).(0) (fun i -> try str_ter.(i) with _ ->
(Printf.sprintf "<regexp:%d>" i));
f table.(i).(2) (fun i -> try "- "^str_ter.(i) with _ ->
(Printf.sprintf "- <regexp:%d>" i));
print_newline ();
Printf.fprintf chan " next states (reduction):\n ";
f table.(i).(1)
(fun i -> str_non_ter.(i));
print_newline (); print_newline ()
let make_table state_list n _ gram_rhs _ _ token_nb _ _ _ =
let table = Array.init n
(fun _ -> [|
Hashtbl.create (token_nb/10);
Hashtbl.create (Array.length gram_rhs /10);
Hashtbl.create (token_nb/10);
Hashtbl.create (Array.length gram_rhs /10)|])
in
let table_it = Array.make n dummy_item_set in
let state_is_mergeable = Array.make n true in
let state_bnt = Array.make n None in
let table_lit_trans = Array.make n (Ps_Ter 0) in
let g num (succ,_) = match succ.li with
| Ps_Ter t ->
Hashtbl.replace table.(num).(0) t succ.number
| Ps_Non_ter nt ->
Hashtbl.replace table.(num).(1) nt succ.number
| Ps_Ter_NL t ->
Hashtbl.replace table.(num).(2) t succ.number
| Ps_Non_ter_NL nt ->
Hashtbl.replace table.(num).(3) nt succ.number
in
let f v =
List.iter (g v.number) v.succ_states;
table_it.(v.number) <- v.items;
table_lit_trans.(v.number) <- v.li;
state_bnt.(v.number) <- v.bestowing_nt;
state_is_mergeable.(v.number) <- v.mergeable
in
List.iter f state_list;
table, table_it, table_lit_trans, state_bnt, state_is_mergeable
let update_user_g ral user_g =
let f (user_g, i) (r, a, inh_l, ier, ilr) =
let (al, j, inh_ll, _, _), i =
try Urule_map.find r user_g, i
with Not_found ->
([], i+1, List.map (fun (p,_) -> p, []) inh_l, false, false), i+1
in
let inh_ll = List.map2 (fun (p, a) (_, l) -> p, a::l) inh_l inh_ll in
(Urule_map.add r (a::al, j, inh_ll, ier, ilr) user_g), i
in
fst (List.fold_left f (user_g, -1) (List.rev ral))
let make_grams_actions g nbr str_non_ter ntp_ntl_ht =
let gram_lhs = Array.make (Array.length g) ([],None) in
let gram_rhs = Array.make nbr [||] in
let gram_parnt = Array.make nbr [] in
let bnt_array = Array.make (Array.length str_non_ter) false in
let rule_options = Array.make nbr 3 in
let lhs_table = Array.make nbr dummy_lhs in
let new_actions = Array.make nbr [] in
let implicit_rule = Array.make nbr false in
let left_rec_rule = Array.make nbr false in
let inh_list = ref [] in
let inh = ref 0 in
let f i (rhs, ro) (ac, rn, inh_l, ier, ilr) (inh, inh_list) =
gram_rhs.(rn) <- rhs;
rule_options.(rn) <- ro;
new_actions.(rn) <- ac;
implicit_rule.(rn) <- ier;
left_rec_rule.(rn) <- ilr;
lhs_table.(rn) <- (i, 0, i);
let rnl, iop = gram_lhs.(i) in
let de =
if Array.length rhs > 0 then
(gram_lhs.(i) <- (rn::rnl, iop);
match rhs.(0) with
| Ps_Non_ter (nt, _) | Ps_Non_ter_NL (nt, _)
when str_non_ter.(nt).[0] = '0' -> true
| _ -> false)
else
(gram_lhs.(i) <- (rnl, Some rn); false)
in
let parnt, inh, inh_list = List.fold_left
(fun (parnt, inh, inh_list) (place, inh_val) ->
let nt = match rhs.(place-1) with
| Ps_Non_ter (nt, _) | Ps_Non_ter_NL (nt, _) -> nt
| _ -> assert false
in
if not bnt_array.(nt) then
(bnt_array.(nt) <- true;
let nt_list =
try Hashtbl.find ntp_ntl_ht nt
with Not_found -> assert false
in
List.iter (fun nt -> bnt_array.(nt) <- true) nt_list);
(place, inh)::parnt, inh+1,
(inh_val, place-1, i, nt, de)::inh_list)
([], inh, inh_list) inh_l
in
gram_parnt.(rn) <- List.rev parnt;
inh, inh_list
in
for i = 0 to Array.length g - 1 do
let a, b = Map_rhs.fold (f i) g.(i) (!inh, !inh_list) in
inh := a; inh_list := b
done;
let inherited = Array.make !inh ([],-1,-1,-1,false) in
let _ = List.fold_left
(fun i (inh_val, nbarg, lhs, nt, de) ->
inherited.(i) <- inh_val, nbarg, lhs, nt, de; i+1)
0 (List.rev !inh_list)
in
gram_lhs, gram_rhs, gram_parnt, bnt_array, lhs_table, new_actions,
inherited, rule_options, implicit_rule, left_rec_rule
module String_set = Set.Make(Ordered_string)
module Ordered_rule =
struct
type t = (non_ter * priority) * rhs * int
let compare = Stdlib.compare
end
module Rule_map = Map.Make(Ordered_rule)
let lit_of_nlit str_non_ter str_ter (regexp_array:regexp array) symb =
match symb with
| Ps_Ter t ->
(try Ter (str_ter.(t))
with _ ->
(
Regexp (regexp_array.(t-(Array.length str_ter))) ) )
| Ps_Non_ter (nt,_) ->
Non_ter (str_non_ter.(nt), No_priority)
| Ps_Ter_NL t ->
(try Ter_NL (str_ter.(t))
with _ -> Regexp_NL (regexp_array.(t-(Array.length str_ter))))
| Ps_Non_ter_NL (nt,_) ->
Non_ter_NL (str_non_ter.(nt), No_priority)
let nl_inside = [No_layout_inside]
let nl_follows = [No_layout_follows]
let nl_inside_nl_follows = [No_layout_inside;No_layout_follows]
let make_rn_of_rule (gram_rhs:rhs array) rule_options lhs_table
str_non_ter str_ter (regexp_array:regexp array) =
let n = Array.length gram_rhs in
let rn_of_rule = Hashtbl.create n in
for i=0 to n-1 do
let nt,_,_ = lhs_table.(i) in
let litl = List.map
(lit_of_nlit str_non_ter str_ter regexp_array)
(list_of_array gram_rhs.(i)) in
let rol = match rule_options.(i) with
| 3 -> []
| 2 -> nl_inside
| 1 -> nl_follows
| 0 -> nl_inside_nl_follows
| _ -> assert false
in
Hashtbl.add rn_of_rule
(str_non_ter.(nt),litl,"",rol) i
done;
rn_of_rule
let find_lhspm nt lhs_prio_map =
try String_map.find nt lhs_prio_map
with Not_found -> String_set.empty
let string_of_nt_prio = function
| No_priority -> "(_)", None
| Eq_priority p -> "("^p^")", Some p
| Less_priority p -> "(<"^p^")", Some p
| Lesseq_priority p -> "(<="^p^")", Some p
| Greater_priority p -> "(>"^p^")", Some p
| Greatereq_priority p -> "(>="^p^")", Some p
let string_prio_of_nt_prio ntp = match string_of_nt_prio ntp with
| _, (Some p) -> p | _, None -> ""
let add_prio_nt nt_prio_map lhs_prio_map priodata nt_table ntntp_ht ntp_ntl_ht =
String_map.fold (fun nt_lhs (nt, ntp) ral ->
let ps = find_lhspm nt lhs_prio_map in
let ps = String_set.fold
(fun p s ->
Prio_set.add
(try Hashtbl.find priodata.prd_ind p
with Not_found -> assert false) s)
ps Prio_set.empty
in
let pset, is_eq = match ntp with
| No_priority -> ps, false
| Less_priority p ->
let i =
try Hashtbl.find priodata.prd_ind p
with Not_found -> assert false
in
Prio_set.inter (fst priodata.prd_rel.(i)) ps, false
| Lesseq_priority p ->
let i =
try Hashtbl.find priodata.prd_ind p
with Not_found -> assert false
in
Prio_set.inter (Prio_set.add i (fst priodata.prd_rel.(i))) ps, false
| Greater_priority p ->
let i =
try Hashtbl.find priodata.prd_ind p
with Not_found -> assert false
in
Prio_set.inter (snd priodata.prd_rel.(i)) ps, false
| Greatereq_priority p ->
let i =
try Hashtbl.find priodata.prd_ind p
with Not_found -> assert false
in
Prio_set.inter (Prio_set.add i (snd priodata.prd_rel.(i))) ps, false
| Eq_priority p ->
let i =
try Hashtbl.find priodata.prd_ind p
with Not_found -> assert false
in
(if Prio_set.mem i ps then Prio_set.add i Prio_set.empty
else Prio_set.empty), true
in
let lhs =
try Hashtbl.find nt_table nt_lhs
with Not_found -> assert false
in
let ral, nt_list =
Prio_set.fold (fun i (ral, nt_list) ->
let p = priodata.prd_names.(i) in
let nt =
try Hashtbl.find nt_table (nt^"("^p^")")
with Not_found -> assert false
in
let s =
try Hashtbl.find ntntp_ht nt
with Not_found -> Int_set.empty
in
Hashtbl.replace ntntp_ht nt (Int_set.add lhs s);
if is_eq then ral, nt_list else
let rule = (lhs, [Ps_Non_ter (nt, No_priority)], 0, 3) in
let action = Dypgen_action
(fun _ _ _ _ _ _ _ _ _ ->
assert false)
in
(rule, action, [], true, false)::ral,
nt::nt_list)
pset (ral, [])
in
Hashtbl.add ntp_ntl_ht lhs nt_list;
ral)
nt_prio_map []
let make_grammar ral old_ral (relations:string list list) ppar ter_table str_ter regexp_table nt_cons_map =
let token_nb = Hashtbl.length ter_table in
let fold_rhs (nts, ps, ntpm, ntcm) = function
| (Non_ter (nt, ntp)) | (Non_ter_NL (nt, ntp)) ->
let prio, p = string_of_nt_prio ntp in
String_set.add (nt^prio) nts,
(match p with Some p -> String_set.add p ps | None -> ps),
(String_map.add (nt^prio) (nt, ntp) ntpm),
(try
let cons = String_map.find nt nt_cons_map in
String_map.add (nt^prio) cons ntcm
with Not_found -> ntcm)
| Ter _ | Ter_NL _ | Regexp _ | Regexp_NL _ -> nts, ps, ntpm, ntcm
in
let nt_set, prio_set, nt_prio_map, lhs_prio_map, ntcm =
List.fold_left
(fun (nts, ps, ntpm, lhspm, ntcm) ((lhs, rhs, p, _), _, _) ->
let nts = String_set.add (lhs^"("^p^")") nts in
let nts = String_set.add (lhs^"(_)") nts in
let ntpm = String_map.add (lhs^"(_)") (lhs, No_priority) ntpm in
let ntpm = String_map.add (lhs^"("^p^")") (lhs, Eq_priority p) ntpm in
let cons =
try String_map.find lhs nt_cons_map
with Not_found -> assert false
in
let ntcm = String_map.add (lhs^"("^p^")") cons ntcm in
let ntcm = String_map.add (lhs^"(_)") cons ntcm in
let ps = String_set.add p ps in
let lhsps = find_lhspm lhs lhspm in
let lhspm = String_map.add lhs (String_set.add p lhsps) lhspm in
let nts, ps, ntpm, ntcm =
List.fold_left fold_rhs (nts, ps, ntpm, ntcm) rhs in
nts, ps, ntpm, lhspm, ntcm)
(String_set.empty, String_set.empty, String_map.empty,
String_map.empty, String_map.empty) (old_ral@ral)
in
let prio_set = List.fold_left
(fun prio_set l -> List.fold_left
(fun prio_set p -> String_set.add p prio_set) prio_set l)
prio_set relations
in
let pr_nb = String_set.cardinal prio_set in
let prio_table = Hashtbl.create pr_nb in
let fold_ht ht str n = Hashtbl.add ht str n; (n+1) in
let nt_set_card = String_set.cardinal nt_set in
let nt_table = Hashtbl.create nt_set_card in
let _ = String_set.fold (fold_ht nt_table) nt_set 0 in
let cons_of_nt = Array.make (String_set.cardinal nt_set) 0 in
let f nt cons =
try
let i = Hashtbl.find nt_table nt in
cons_of_nt.(i) <- cons
with Not_found -> ()
in
String_map.iter f ntcm;
let _ = String_set.fold (fold_ht prio_table) prio_set 0 in
let re_id = ref (token_nb + (Hashtbl.length regexp_table)) in
let regexp_list = (ref []),(ref []) in
let regexp_actions = (ref []),(ref []) in
let make_regexp re =
if not ppar.use_dyplex then
failwith "make_grammar: Regular expression not allowed in a rule when using an external lexer generator.";
try Hashtbl.find regexp_table re
with Not_found ->
(Hashtbl.add regexp_table re !re_id;
(match re with
| RE_String _ ->
(fst regexp_list) := re::(!(fst regexp_list));
(fst regexp_actions) := !re_id::
(!(fst regexp_actions))
| _ ->
(snd regexp_list) := re::(!(snd regexp_list));
(snd regexp_actions) :=
!re_id::(!(snd regexp_actions)) );
incr re_id;
!re_id-1)
in
let make_non_ter nt p =
let prio, _ = string_of_nt_prio p in
let nt = nt^prio in
try (Hashtbl.find nt_table nt, No_priority)
with Not_found ->
failwith ("make_non_ter did not find non terminal \""^nt^"\"")
in
let f4 l = match l with
| Ter t -> Ps_Ter (try Hashtbl.find ter_table t
with Not_found -> raise (Undefined_ter t)
)
| Regexp re -> Ps_Ter (make_regexp re)
| Non_ter (nt, p) -> Ps_Non_ter (make_non_ter nt p)
| Ter_NL t -> Ps_Ter_NL (try Hashtbl.find ter_table t
with Not_found -> raise (Undefined_ter t))
| Regexp_NL re -> Ps_Ter_NL (make_regexp re)
| Non_ter_NL (nt, p) -> Ps_Non_ter_NL (make_non_ter nt p)
in
let is_left_rec lhs = function
| (Non_ter (nt, _))::_ | (Non_ter_NL (nt, _))::_
-> lhs = nt
| _ -> false
in
let f3 ral (((lhs:string),rhs,(p:string),rol),a,inhl) =
let ilr = is_left_rec lhs rhs in
let lhs =
try Hashtbl.find nt_table (lhs^"("^p^")")
with Not_found -> assert false
in
let rhs = List.map f4 rhs in
let x = List.fold_left (fun x ro ->
match ro with
| No_layout_inside -> (lnot 1) land x
| No_layout_follows -> (lnot 2) land x) 3 rol
in
((lhs, rhs, 0, x), a, inhl, false, ilr)::ral
in
let str_non_ter = Array.make (nt_set_card+1) "" in
let str_non_ter_prio = Array.make (nt_set_card+1) ("","") in
let f5 str n =
str_non_ter.(n) <- str;
let nt, p = String_map.find str nt_prio_map in
let p_str = string_prio_of_nt_prio p in
str_non_ter_prio.(n) <- nt, p_str
in
Hashtbl.iter f5 nt_table;
let prio_dat = {
prd_rel = Array.make pr_nb (Prio_set.empty,Prio_set.empty);
prd_ind = prio_table;
prd_names = Array.make pr_nb "";
prd_nb = pr_nb }
in
let f9 s =
try Hashtbl.find prio_table s
with Not_found -> assert false
in
let f8 l = List.map f9 l in
let relations = List.map f8 relations in
let f7 l = add_list_relations prio_dat l in
List.iter f7 relations;
let f10 s i = prio_dat.prd_names.(i) <- s in
Hashtbl.iter f10 prio_table;
let lhs_n =
String_map.fold (fun _ s n -> n+(String_set.cardinal s))
lhs_prio_map 0
in
let ntntp_ht = Hashtbl.create lhs_n in
let implicit_nt_nb = String_map.fold (fun _ _ n -> n+1) nt_prio_map 0 in
let ntp_ntl_ht = Hashtbl.create implicit_nt_nb in
let ral0 =
add_prio_nt nt_prio_map lhs_prio_map prio_dat nt_table ntntp_ht ntp_ntl_ht
in
let ral = List.fold_left f3 ral0 ral in
let user_g = update_user_g ral Urule_map.empty in
let regexp_array = Array.make
(Hashtbl.length regexp_table)
RE_Eof_char in
Hashtbl.iter (fun re i -> regexp_array.(i-token_nb) <- re) regexp_table;
let g, nt_nb, nbr =
make_real_grammar user_g str_non_ter nt_table ppar
in
let gram_lhs, gram_rhs, gram_parnt, bnt_array, lhs_table, actions,
inherited, rule_options, implicit_rule, left_rec_rule
= make_grams_actions g nbr str_non_ter ntp_ntl_ht
in
let nt_ntl_array = Array.make (Array.length gram_lhs) Int_set.empty in
Hashtbl.iter
(fun lhs nts ->
nt_ntl_array.(lhs) <- nts)
ntntp_ht;
let rn_of_rule =
make_rn_of_rule gram_rhs rule_options lhs_table str_non_ter
str_ter regexp_array in
let po_ht, cyclic_rules =
create_po gram_rhs lhs_table
(Array.length gram_lhs) str_non_ter
in
gram_lhs, gram_rhs, lhs_table, actions, inherited, nt_nb, po_ht,
user_g,
nt_table, str_non_ter, str_non_ter_prio, rn_of_rule, regexp_table,
((List.rev !(fst regexp_list)), (List.rev !(snd regexp_list))),
((List.rev !(fst regexp_actions)), (List.rev !(snd regexp_actions))),
regexp_array, rule_options, cyclic_rules, gram_parnt, bnt_array,
cons_of_nt, nt_ntl_array, implicit_rule, left_rec_rule
let create_parsing_device ra_list relations nt_cons_map entry_point_list ppar regexp_decl_list main_lexer aux_lexer token_nb str_ter ter_table =
let gram_lhs, gram_rhs, lhs_table, actions, inherited, nt_nb,
po_ht, _, nt_table, str_non_ter, str_non_ter_prio, rn_of_rule,
regexp_table, regexp_list, regexp_actions, regexp_array,
rule_options, cyclic_rules, gram_parnt, bnt_array, cons_of_nt,
nt_ntl_array, implicit_rule, left_rec_rule
= make_grammar ra_list [] relations ppar ter_table str_ter
(Hashtbl.create 10) nt_cons_map
in
let regexp_nb = (List.length (fst regexp_list)) +
(List.length (snd regexp_list)) in
let new_token_nb = token_nb + regexp_nb in
let entry_point_list =
List.map
(fun ep ->
try Hashtbl.find nt_table (ep^"(_)")
with Not_found -> assert false)
entry_point_list
in
let time1 = Sys.time () in
let r_L =
compute_L gram_rhs gram_lhs in
let time2 = Sys.time () in
if !dypgen_verbose>1 then
Printf.fprintf !log_channel "r_L computed, %.3f sec\n"
(time2-.time1);
let state_list, n, stations, entry_points, is_trace =
create_aut gram_rhs gram_lhs gram_parnt bnt_array r_L
0 nt_nb
lhs_table str_non_ter entry_point_list ppar new_token_nb
str_ter regexp_array implicit_rule
in
let table, table_it, table_lit_trans, state_bnt, state_is_mergeable =
make_table state_list n (Array.length gram_lhs) gram_rhs
lhs_table ppar new_token_nb
str_ter str_non_ter regexp_array
in
let regexp_decl = compile_regexp_decl regexp_decl_list in
let rl = List.map (fun (_,r) -> r) (fst main_lexer) in
let rl = (fst regexp_list)@rl@(snd regexp_list) in
let main_table, main_start = make_lexer regexp_decl rl in
let mla_nb = (Array.length (snd main_lexer)) in
let main_lexer_actions = Array.make
(mla_nb+regexp_nb)
ppar.regexp_fun in
let main_lexer_ter_id = Array.make
(mla_nb+regexp_nb)
0 in
let i = List.fold_left
(fun i x ->
main_lexer_ter_id.(i) <- x;
i+1)
0
(fst regexp_actions) in
let arr = snd main_lexer in
for j=0 to mla_nb-1 do
main_lexer_actions.(j+i) <- snd arr.(j);
main_lexer_ter_id.(j+i) <- fst arr.(j)
done;
let _ = List.fold_left
(fun i x ->
main_lexer_ter_id.(i) <- x;
i+1)
(i+mla_nb)
(snd regexp_actions) in
let aux_nb = List.length aux_lexer in
let aux_table = Hashtbl.create aux_nb in
let aux_start = Hashtbl.create aux_nb in
let aux_actions = Hashtbl.create aux_nb in
List.iter (fun (name,(rl,al)) ->
let table, start = make_lexer regexp_decl rl in
Hashtbl.add aux_table name table;
Hashtbl.add aux_start name start;
Hashtbl.add aux_actions name (array_of_list al)) aux_lexer;
{ ra_list = ra_list;
gram_rhs = gram_rhs ;
gram_lhs = gram_lhs ;
gram_parnt = gram_parnt ;
bnt_array = bnt_array ;
lhs_table = lhs_table ;
entry_points = entry_points ;
g_nb = 0;
lex_nb = 0;
nt_table = nt_table ;
stations = stations ;
state_list = state_list ;
is_trace = is_trace;
st_nb = n;
table = table ;
state_bnt = state_bnt ;
state_is_mergeable = state_is_mergeable ;
table_it = table_it ;
table_lit_trans = table_lit_trans ;
r_L = r_L ;
po = po_ht ;
cyclic_rules = cyclic_rules ;
nt_nb = Array.length gram_lhs;
token_nb = new_token_nb;
str_non_ter = str_non_ter;
str_non_ter_prio = str_non_ter_prio;
cons_of_nt = cons_of_nt;
relations = relations;
nt_cons_map = nt_cons_map;
rn_of_rule = rn_of_rule;
entry_point = 0;
actions = actions;
inherited = inherited;
regexp_decl = regexp_decl;
regexp_decl_list = regexp_decl_list;
main_lexer_start = main_start;
main_lexer_table = main_table;
main_lexer_actions = main_lexer_actions;
main_lexer_ter_id = main_lexer_ter_id;
main_lexer_init_id = List.length (fst regexp_actions);
aux_lexer = {
aux_lexer_start = aux_start;
aux_lexer_table = aux_table;
aux_lexer_actions = aux_actions };
str_ter = str_ter;
ter_table = ter_table;
layout_id = (try Hashtbl.find ter_table "__dypgen_layout"
with Not_found -> -1 );
regexp_table = regexp_table;
regexp_array = regexp_array;
rule_options = rule_options;
nt_ntl_array = nt_ntl_array;
implicit_rule = implicit_rule;
left_rec_rule = left_rec_rule }
let make_parser
ra_list relations global_data local_data nt_cons_map entry_point_list
merge_warning token_nb undef_nt
get_value get_name str_token global_data_equal local_data_equal
test_cons str_cons cons_str cons_table merge_array lexbuf_position_fun
regexp_decl_list main_lexer aux_lexer ter_string_list regexp_fun use_dyplex =
let ppar = {
merge_warning = merge_warning;
undef_nt = undef_nt;
get_value = get_value;
get_name = get_name;
str_token = str_token;
global_data_equal = global_data_equal;
local_data_equal = local_data_equal;
find_rightSib_global_data_equal = (fun _ _ -> true);
find_rightSib_local_data_equal = (fun _ _ -> true);
test_cons = test_cons;
str_cons = str_cons;
cons_str = cons_str;
cons_table = cons_table;
merge_array = merge_array;
lexbuf_position_fun = lexbuf_position_fun;
regexp_fun = regexp_fun;
use_dyplex = use_dyplex;
use_rule_order = false;
use_all_actions = false;
main_lexer_action_nb = List.length (fst main_lexer) }
in
let main_lexer = match main_lexer with
| ("dummy_entry",_)::_, _ -> [],[||]
| a, b -> a, array_of_list b
in
let str_ter = Array.make token_nb "" in
let ter_table = Hashtbl.create token_nb in
List.iter (fun (name,id) ->
str_ter.(id) <- name;
Hashtbl.add ter_table name id) ter_string_list;
let pdev = create_parsing_device
ra_list relations nt_cons_map entry_point_list
ppar regexp_decl_list main_lexer aux_lexer token_nb str_ter ter_table
in
{ pp_dev = pdev;
pp_par = ppar;
pp_gd = global_data;
pp_ld = local_data }
module Ordered_edge =
struct
type t = lit_trans * priority
let compare = Stdlib.compare
end
module Edge_map = Map.Make(Ordered_edge)
let compute_nt_to_add is gram_rhs r_L non_kernel_array =
let aux (rn,dp) (nt_to_add,nk) =
try (match gram_rhs.(rn).(dp) with
| Ps_Non_ter nt | Ps_Non_ter_NL nt ->
(
let g1 nt_to_add ind =
Int_set.add ind nt_to_add
in
let g2 nt_to_add (ind,_) =
let ind_list = r_L.(ind) in
List.fold_left g1 nt_to_add ind_list
in
(g2 nt_to_add nt),
if non_kernel_array.(rn) then nk
else (non_kernel_array.(rn) <- true; rn::nk))
| _ -> nt_to_add,nk)
with Invalid_argument _ -> nt_to_add,nk
in
let nt_to_add,nk =
Intc_set.fold aux is.kernel_nt (Int_set.empty,is.non_kernel)
in
is.non_kernel <- nk;
List.iter (fun rn -> non_kernel_array.(rn) <- false) nk;
nt_to_add
let new_start_state is key_is lit_trans _ is_trace state_list gram_rhs gram_lhs gram_parnt _ r_L non_kernel_array map_succ =
let key = key_is,
(Int_set.cardinal key_is.reducible) +
(Intc_set.cardinal key_is.kernel_t) +
(Intc_set.cardinal key_is.kernel_nt)
in
try
let state_ind = match lit_trans with
| Ps_Ter t -> (Map_is.find key (fst (fst is_trace)).(t)).number
| Ps_Non_ter nt -> (Map_is.find key (fst (snd is_trace)).(nt)).number
| Ps_Ter_NL t -> (Map_is.find key (snd (fst is_trace)).(t)).number
| Ps_Non_ter_NL nt -> (Map_is.find key (snd (snd is_trace)).(nt)).number
in
state_list, (Some state_ind), is_trace
with Not_found ->
let nt_to_add =
compute_nt_to_add key_is gram_rhs r_L non_kernel_array
in
closure_v0_LR0 key_is gram_rhs gram_lhs nt_to_add non_kernel_array;
let v = {
li = Ps_Ter (-1);
items = key_is;
number = !countst;
mergeable = is_mergeable is gram_parnt;
bestowing_nt = make_bestowing_nt is gram_parnt;
succ_states = [] }
in
let state_ind = !countst in
incr countst;
let is_trace, state_list = map_succ (is_trace,[]) v in
state_list, (Some state_ind), is_trace
let make_key_single is0 gram_rhs =
let is = new_item_set () in
is.reducible <- Int_set.union is.reducible
(Int_set.filter
(fun rn -> Array.length gram_rhs.(rn)>0)
is0.reducible);
is.kernel_t <- Intc_set.union is.kernel_t is0.kernel_t;
is.kernel_nt <- Intc_set.union is.kernel_nt is0.kernel_nt;
is,
(Int_set.cardinal is.reducible) +
(Intc_set.cardinal is.kernel_t) +
(Intc_set.cardinal is.kernel_nt)
let rule_of_nrule (gram_rhs:rhs array) rule_options lhs_table str_non_ter str_ter regexp_array rn =
let len = Array.length gram_rhs.(rn) in
let rec f i l =
if i= -1 then l else
f (i-1) ((lit_of_nlit str_non_ter str_ter
regexp_array gram_rhs.(rn).(i))::l)
in
let litl = f (len-1) [] in
let nt,_,_ = lhs_table.(rn) in
let rol = match rule_options.(rn) with
| 3 -> []
| 2 -> nl_inside
| 1 -> nl_follows
| 0 -> nl_inside_nl_follows
| _ -> assert false
in
(str_non_ter.(nt),litl,"",rol)
let extend_parsing_device pdev pl is lit_trans ppar =
let f =
rule_of_nrule pl.gram_rhs pl.rule_options pl.lhs_table pl.str_non_ter
pl.str_ter pl.regexp_array
in
is.reducible <- Int_set.fold (fun rn red ->
let rn =
try Hashtbl.find pdev.rn_of_rule (f rn)
with Not_found -> assert false
in
Int_set.add rn red) is.reducible Int_set.empty;
is.kernel_t <- Intc_set.fold (fun (rn,x) k ->
let rn =
try Hashtbl.find pdev.rn_of_rule (f rn)
with Not_found -> assert false
in
Intc_set.add (rn,x) k) is.kernel_t Intc_set.empty;
is.kernel_nt <- Intc_set.fold (fun (rn,x) k ->
let rn =
try Hashtbl.find pdev.rn_of_rule (f rn)
with Not_found -> assert false
in
Intc_set.add (rn,x) k) is.kernel_nt Intc_set.empty;
let is, inb = make_key_single is pdev.gram_rhs in
let key = is, inb in
let lit_trans = match lit_trans with
| Ps_Non_ter nt ->
Ps_Non_ter
(try Hashtbl.find pdev.nt_table (pl.str_non_ter.(nt))
with Not_found -> assert false)
| Ps_Ter t -> Ps_Ter t
| Ps_Non_ter_NL nt ->
Ps_Non_ter_NL
(try Hashtbl.find pdev.nt_table (pl.str_non_ter.(nt))
with Not_found -> assert false)
| Ps_Ter_NL t -> Ps_Ter_NL t
in
try
let s = match lit_trans with
| Ps_Non_ter nt -> Map_is.find key (fst (snd pdev.is_trace)).(nt)
| Ps_Ter t -> Map_is.find key (fst (fst pdev.is_trace)).(t)
| Ps_Non_ter_NL nt -> Map_is.find key (snd (snd pdev.is_trace)).(nt)
| Ps_Ter_NL t -> Map_is.find key (snd (fst pdev.is_trace)).(t)
in
s.number, pdev
with Not_found ->
let array_lt_ter = Array.make pdev.token_nb None in
let array_lt_ter_nl = Array.make pdev.token_nb None in
let nt_nb = Array.length pdev.gram_lhs in
let array_lt_nt = Array.make nt_nb None in
let array_lt_nt_nl = Array.make nt_nb None in
let array_lhs = Array.make nt_nb false in
let succ_states_array =
ref (Array.make (2*(Array.length pdev.table_it)) false)
in
let non_kernel_array = Array.make (Array.length pdev.gram_rhs) false in
let rec map_succ (is_trace,state_list) v =
let vl =
move_LR0 v is_trace pdev.gram_rhs pdev.gram_lhs pdev.gram_parnt
pdev.bnt_array pdev.r_L
array_lt_ter array_lt_ter_nl
array_lt_nt array_lt_nt_nl array_lhs
succ_states_array non_kernel_array
in
List.fold_left map_succ (is_trace,v::state_list) vl
in
let nt_to_add =
let aux1 (rn,dp) (nt_to_add,nk) =
try (match pdev.gram_rhs.(rn).(dp) with
| Ps_Non_ter nt | Ps_Non_ter_NL nt ->
(
let g1 nt_to_add ind =
Int_set.add ind nt_to_add
in
let g2 nt_to_add (ind,_) =
let ind_list = pdev.r_L.(ind) in
List.fold_left g1 nt_to_add ind_list
in
(g2 nt_to_add nt),
if non_kernel_array.(rn) then nk
else (non_kernel_array.(rn) <- true; rn::nk))
| _ -> assert false)
with Invalid_argument _ -> assert false
in
let nt_to_add, nk =
Intc_set.fold aux1 is.kernel_nt (Int_set.empty,[])
in
is.non_kernel <- nk;
nt_to_add
in
closure_v0_LR0 is pdev.gram_rhs pdev.gram_lhs nt_to_add non_kernel_array;
countst := pdev.st_nb+1;
let predict =
List.fold_left
(fun predict rn -> try (match pdev.gram_rhs.(rn).(0) with
| Ps_Non_ter nt | Ps_Non_ter_NL nt -> Predict.add nt predict
| _ -> predict)
with Invalid_argument _ -> assert false)
Predict.empty is.non_kernel
in
let predict =
Intc_set.fold
(fun (rn,dp) predict -> try (match pdev.gram_rhs.(rn).(dp) with
| Ps_Non_ter nt | Ps_Non_ter_NL nt -> Predict.add nt predict
| _ -> assert false)
with Invalid_argument _ -> assert false)
is.kernel_nt predict
in
is.predict <- predict;
let v = {
li = lit_trans;
items = is;
number = !countst;
mergeable = is_mergeable is pdev.gram_parnt;
bestowing_nt = make_bestowing_nt is pdev.gram_parnt;
succ_states = [] }
in
let v_rightSib = !countst in
incr countst;
let is_trace, state_list = map_succ (pdev.is_trace,pdev.state_list) v in
let table, table_it, table_lit_trans, state_bnt, state_is_mergeable =
make_table state_list (!countst) (Array.length pdev.gram_lhs)
pdev.gram_rhs pdev.lhs_table
ppar pdev.token_nb pdev.str_ter
pdev.str_non_ter pdev.regexp_array
in
let pdev_rightSib = { pdev with
table = table;
table_it = table_it;
table_lit_trans = table_lit_trans;
state_bnt = state_bnt;
state_is_mergeable = state_is_mergeable;
is_trace = is_trace;
state_list = state_list;
st_nb = !countst }
in
v_rightSib, pdev_rightSib
let new_rn old_rn old_pdev rn_of_rule =
let rule =
rule_of_nrule old_pdev.gram_rhs old_pdev.rule_options
old_pdev.lhs_table old_pdev.str_non_ter old_pdev.str_ter
old_pdev.regexp_array old_rn
in
try Hashtbl.find rn_of_rule rule
with Not_found -> assert false
let new_rn_red red old_pdev rn_of_rule =
Int_set.fold (fun rn s -> Int_set.add (new_rn rn old_pdev rn_of_rule) s)
red Int_set.empty
let new_rn_kernel k old_pdev rn_of_rule =
Intc_set.fold
(fun (rn, dp) s ->
Intc_set.add (new_rn rn old_pdev rn_of_rule, dp) s)
k Intc_set.empty
let update_parsing_device ppar pdev_leftSib new_relations rapf_add new_nt_cons counters start_is lit_trans =
let time1_update_parsing_device = Sys.time () in
let new_nt_cons =
List.map
(fun (nt, cons) ->
(nt, try Hashtbl.find ppar.cons_table cons
with Not_found -> assert false))
new_nt_cons
in
let nt_cons_map =
List.fold_left
(fun m (nt, cons) ->
try
let prev_cons = String_map.find nt m in
if prev_cons = cons then m else
raise (Constructor_mismatch
(ppar.cons_str.(prev_cons), ppar.cons_str.(cons)))
with Not_found -> String_map.add nt cons m)
pdev_leftSib.nt_cons_map new_nt_cons
in
let relations = new_relations@pdev_leftSib.relations in
let interm_pdev, state_ind =
let ra_list,
entry_point_list, _, _, _,
str_ter, ter_table, pdev
= pdev_leftSib.ra_list@rapf_add,
[], [], ([],[||]), [],
pdev_leftSib.str_ter, pdev_leftSib.ter_table, pdev_leftSib
in
let gram_lhs, gram_rhs, lhs_table, actions, inherited, nt_nb,
po_ht, _,
nt_table, str_non_ter, str_non_ter_prio, rn_of_rule,
_, regexp_list, regexp_actions, regexp_array,
rule_options, cyclic_rules, gram_parnt, bnt_array,
cons_of_nt, nt_ntl_array, implicit_rule, left_rec_rule
= make_grammar ra_list [] relations ppar ter_table
str_ter pdev_leftSib.regexp_table
nt_cons_map
in
let main_table, main_start, main_lexer_actions, main_lexer_ter_id, main_lexer_init_id, lex_nb =
match regexp_list with
| [], [] ->
pdev.main_lexer_table, pdev.main_lexer_start,
pdev.main_lexer_actions, pdev.main_lexer_ter_id, pdev.main_lexer_init_id, pdev.lex_nb
| _ ->
let prev_mla_nb = Array.length pdev_leftSib.main_lexer_actions in
let main_table, main_start =
let node_nb =
(Array.length pdev_leftSib.main_lexer_table.tbl_trans)/257 in
extend_lexer pdev_leftSib.main_lexer_start regexp_list
pdev_leftSib.regexp_decl node_nb prev_mla_nb in
let main_lexer_init_id =
pdev_leftSib.main_lexer_init_id + (List.length (fst regexp_actions)) in
let main_lexer_actions = Array.make
(prev_mla_nb+(List.length (fst regexp_list))+
(List.length (snd regexp_list)))
ppar.regexp_fun in
let main_lexer_ter_id = Array.make
(Array.length main_lexer_actions) 0 in
let i = List.fold_left
(fun i x ->
main_lexer_ter_id.(i) <- x;
i+1)
0
(fst regexp_actions) in
let arr1 = pdev_leftSib.main_lexer_ter_id in
let arr2 = pdev_leftSib.main_lexer_actions in
for j=0 to prev_mla_nb-1 do
main_lexer_ter_id.(j+i) <- arr1.(j);
main_lexer_actions.(j+i) <- arr2.(j)
done;
let _ = List.fold_left
(fun i x ->
main_lexer_ter_id.(i) <- x;
i+1)
(i+prev_mla_nb)
(snd regexp_actions) in
counters.count_lex <- counters.count_lex + 1;
main_table, main_start, main_lexer_actions, main_lexer_ter_id, main_lexer_init_id, counters.count_lex
in
let new_token_nb = (Hashtbl.length ter_table) +
(Array.length regexp_array) in
let entry_point_list =
List.map (fun ep ->
try Hashtbl.find nt_table ep
with Not_found -> assert false) entry_point_list
in
let time1 = Sys.time () in
let r_L = compute_L gram_rhs gram_lhs
in
let time2 = Sys.time () in
if !dypgen_verbose>1 then
Printf.fprintf !log_channel "r_L computed, %.3f sec\n"
(time2-.time1);
let state_list, n, stations, entry_points, is_trace =
create_aut gram_rhs gram_lhs gram_parnt bnt_array r_L
0 nt_nb
lhs_table str_non_ter entry_point_list ppar new_token_nb
str_ter regexp_array implicit_rule
in
let table, table_it, table_lit_trans, state_bnt, state_is_mergeable =
make_table state_list n (Array.length gram_lhs) gram_rhs
lhs_table ppar new_token_nb
str_ter str_non_ter regexp_array
in
let new_nt nt = Hashtbl.find nt_table pdev_leftSib.str_non_ter.(nt) in
let lhs_nb = Array.length bnt_array in
let array_lt_ter = Array.make new_token_nb None in
let array_lt_ter_nl = Array.make new_token_nb None in
let array_lt_nt = Array.make lhs_nb None in
let array_lt_nt_nl = Array.make lhs_nb None in
let array_lhs = Array.make nt_nb false in
let new_ssa = ref (Array.make (2*n) false) in
let non_kernel_array = Array.make (Array.length gram_rhs) false in
let rec map_succ (is_trace, state_list) v =
let vl =
move_LR0 v is_trace gram_rhs gram_lhs gram_parnt bnt_array r_L
array_lt_ter array_lt_ter_nl array_lt_nt
array_lt_nt_nl array_lhs new_ssa non_kernel_array
in
List.fold_left map_succ (is_trace,v::state_list) vl
in
let state_list, state_ind, is_trace =
match start_is, lit_trans with
| (Some is), (Some lt) ->
let lt = match lt with
| Ps_Non_ter nt -> Ps_Non_ter (new_nt nt)
| Ps_Ter t -> Ps_Ter t
| Ps_Non_ter_NL nt -> Ps_Non_ter_NL (new_nt nt)
| Ps_Ter_NL t -> Ps_Ter_NL t
in
let key_is = {
reducible = (Int_set.filter
(fun rn -> Array.length gram_rhs.(rn)>0)
(new_rn_red is.reducible pdev_leftSib rn_of_rule));
kernel_t = new_rn_kernel is.kernel_t pdev_leftSib rn_of_rule;
kernel_nt = new_rn_kernel is.kernel_nt pdev_leftSib rn_of_rule;
non_kernel = [];
predict = Predict.empty }
in
new_start_state is key_is lt nt_nb is_trace
state_list gram_rhs gram_lhs gram_parnt bnt_array r_L
non_kernel_array map_succ
| _ -> state_list, None, is_trace
in
{ ra_list = ra_list;
gram_rhs = gram_rhs ;
gram_lhs = gram_lhs ;
gram_parnt = gram_parnt ;
bnt_array = bnt_array ;
lhs_table = lhs_table ;
entry_points = entry_points ;
g_nb = counters.count_g+1;
lex_nb = lex_nb;
nt_table = nt_table ;
stations = stations ;
state_list = state_list ;
is_trace = is_trace;
st_nb = n;
table = table ;
state_bnt = state_bnt ;
state_is_mergeable = state_is_mergeable ;
table_it = table_it ;
table_lit_trans = table_lit_trans ;
r_L = r_L ;
po = po_ht ;
cyclic_rules = cyclic_rules ;
nt_nb = Array.length gram_lhs;
token_nb = new_token_nb;
str_non_ter = str_non_ter;
str_non_ter_prio = str_non_ter_prio;
cons_of_nt = cons_of_nt;
relations = relations;
nt_cons_map = nt_cons_map;
rn_of_rule = rn_of_rule;
entry_point = 0;
actions = actions;
inherited = inherited;
regexp_decl = pdev.regexp_decl;
regexp_decl_list = pdev.regexp_decl_list;
main_lexer_start = main_start;
main_lexer_table = main_table;
main_lexer_actions = main_lexer_actions;
main_lexer_ter_id = main_lexer_ter_id;
main_lexer_init_id = main_lexer_init_id;
aux_lexer = pdev.aux_lexer;
str_ter = pdev.str_ter;
ter_table = pdev.ter_table;
layout_id = pdev.layout_id;
regexp_table = pdev.regexp_table;
regexp_array = regexp_array;
rule_options = rule_options;
nt_ntl_array = nt_ntl_array;
implicit_rule = implicit_rule;
left_rec_rule = left_rec_rule },
state_ind
in
let time2 = Sys.time () in
counters.count_g <- counters.count_g + 1;
if !dypgen_verbose>0 then
Printf.fprintf !log_channel
"Parser updated in %.3f sec\n" (time2-.time1_update_parsing_device);
state_ind, interm_pdev
let update_pp pp command_list =
let dyp_a = action_command command_list {
ac_gd = pp.pp_gd;
ac_ld = pp.pp_ld;
add_rules = [];
new_nt_cons = [];
ac_relations = [];
will_shift = true;
keep_grammar = false;
ac_parser = None;
next_state = None;
next_grammar = None }
in
let mapfun (r, ac) =
(r,
(Dypgen_action
(fun ol pos posl gd ld lld di p nl ->
(transform_action ac) ol pos posl gd ld lld di p nl)),
[])
in
let add_rules_transformed = List.map mapfun dyp_a.add_rules in
let counter = {
countsn = 0;
counted = 0;
count_token = 0;
last_layout = 0;
count_g = 0;
count_lex = 0 }
in
let _, new_pdev =
update_parsing_device pp.pp_par pp.pp_dev dyp_a.ac_relations add_rules_transformed
dyp_a.new_nt_cons counter None None
in
{ pp with pp_dev = new_pdev; pp_gd = dyp_a.ac_gd; pp_ld = dyp_a.ac_ld }
type ('token,'obj,'data,'local_data,'lexbuf) vertex = {
state_nb : int;
mutable pdev : ('token,'obj,'data,'local_data,'lexbuf) parsing_device;
mutable global_data : 'data;
mutable local_data : 'local_data;
sn_nb : int;
last_token : int;
sn_non_ter : int;
layout_flags : int;
lexer_pos : Lexing.position;
mutable succ_edges : (('token,'obj,'data,'local_data,'lexbuf) edge) list;
mutable prev_nodes_eps : ('token,'obj,'data,'local_data,'lexbuf) vertex list;
mutable inherited_values : 'obj list;
mutable det_depth : int;
mutable ref_count : int;
visited_states : int Intc_map.t
}
and ('token,'obj,'data,'local_data,'lexbuf) edge = {
mutable edge_label : 'obj list;
mutable edge_id : int;
mutable dest : ('token,'obj,'data,'local_data,'lexbuf) vertex;
mutable parse_tree : string;
e_lexer_pos : Lexing.position;
}
let create_e v1 label id v2 _ _ pt e_lexer_pos =
let new_edge = {
edge_label = label; edge_id = id; dest = v2;
parse_tree = pt;
e_lexer_pos = e_lexer_pos
}
in
v1.succ_edges <- new_edge::(v1.succ_edges);
if v1.last_token = v2.last_token then
v2.prev_nodes_eps <- v1::v2.prev_nodes_eps;
new_edge
let create_v state_nb pdev global_data local_data sn_nb last_token lexer_pos layout_flags depth non_ter vs = {
state_nb = state_nb;
pdev = pdev;
global_data = global_data;
local_data = local_data;
sn_nb = sn_nb;
sn_non_ter = non_ter;
last_token = last_token;
lexer_pos = lexer_pos;
layout_flags = layout_flags;
succ_edges = [];
prev_nodes_eps = [];
inherited_values = [];
det_depth = depth;
ref_count = 0;
visited_states = vs }
(** [find_paths] returns a list of couples ([path],[token_nb]),
where a path is a list of edges of the graph structured stack
[gs]. The returned paths have the following properties : they
begin at stack node [sn], their length is [len], the nodes
along each path contain states which associated literals are in
[litl], these literals take place along the path in the same
order as in the list [litl] (actually in reverse). These paths
are the ones along which a reduction can be performed by a
production rule which rhs is [litl].
[token_nb] is the token number of the leftmost stack node of a
path, i.e. the dest stack node of the edge which is at the end
of the list of edges which is [path]. It is the number of the
token which is the first in the part of the input which would be
reduced by the rule given as argument.
[find_paths] is used in [do_reductions] and in [insert_reduction]
and in [insert_reduction2]. *)
let find_paths ?(skip=0) ?(init=[]) sn (rhs:rhs) =
let rec aux n succ path =
if n = skip then match path with
| e::_ -> let s = e.dest in [path,s.last_token]
| [] -> [[],sn.last_token]
else
match succ with
| e::t ->
let sn2 = e.dest in
let succ2 = sn2.succ_edges in
(aux (n-1) succ2 (e::path))@(aux n t path)
| [] -> []
in
aux (Array.length rhs) sn.succ_edges init
let find_paths_no_layout ?(skip=0) ?(init=[]) sn (rhs:rhs) last_layout =
let rec aux n succ path =
if n = skip then match path with
| e::_ -> let s = e.dest in [path,s.last_token]
| [] -> [[],sn.last_token]
else
match succ with
| e::t ->
let sn2 = e.dest in
if sn2.last_token < last_layout then []
else
let succ2 = sn2.succ_edges in
(aux (n-1) succ2 (e::path))@(aux n t path)
| [] -> []
in
aux (Array.length rhs) sn.succ_edges init
let stack_node_equal sn1 sn2 = sn1.sn_nb = sn2.sn_nb
let edge_equal e1 e2 = e1.edge_id = e2.edge_id
let edge_list_equal el1 el2 = List.for_all2 edge_equal el1 el2
let print_path sn p =
let snnb = sn.sn_nb in
let f e =
let ednb = e.edge_id in
Printf.fprintf !log_channel "[%d]-%d-" e.dest.sn_nb ednb
in
List.iter f p;
Printf.fprintf !log_channel "[%d]\n" snnb
let rec revcat a b = match a with
| [] -> b
| h :: t -> revcat t (h::b)
(** Maintains a partially ordered list of couples (path,rule_bis), where a path is
a list of edges of the graph structured stack. This function inserts the couple
of the path [p] and the rule_bis [r] in the list [l] at its right place. The
partial order is the field [po] of the state which is held by the source
stack node of the first edge of the path [p].
The partial order is implemented as a 2 dim array of type bool option.
The need for this partial order is explained in Scott McPeak's report.
A better data structure than a list should be used, maybe a
mutable list. *)
let insert_partially_ordered l (((start0,p0,tnb0),(ind0,_),rn0) as pr0) =
if !dypgen_verbose>2 then
(output_string !log_channel "inserting reduction along:\n";
print_path start0 p0);
let pdev0 = start0.pdev in
let rec aux result b l = match l with
| [] -> revcat result [pr0]
| (((start1,p1,tnb1),(ind1,_),rn1) as pr1)::tl ->
if tnb1<tnb0 then revcat result (pr0::l)
else if tnb1>tnb0 then aux (pr1::result) false tl
else if pdev0.g_nb <> start1.pdev.g_nb
then aux (pr1::result) false tl
else if rn0=rn1 && (stack_node_equal start0 start1) &&
(edge_list_equal p0 p1) then revcat result l else
if htc pdev0.po ind0 ind1 then
aux (pr1::result) (htc pdev0.po ind1 ind0) tl
else
if (htc pdev0.po ind1 ind0) || ind0=ind1 then
revcat result (pr0::l)
else
if b then revcat result (pr0::l)
else aux (pr1::result) false tl
in
aux [] false l
let str_symb_in_rhs_reduction str_non_ter = function
| Ps_Non_ter (nt,_) | Ps_Non_ter_NL (nt,_) -> str_non_ter.(nt)
| Ps_Ter t | Ps_Ter_NL t -> "t"^(string_of_int t)
let str_rhs_reduction sn p rhs str_non_ter =
let _, l = List.fold_left
(fun (lt,l) e -> e.dest.last_token, (e.dest.last_token,lt)::l)
(sn.last_token,[]) p
in
let rec aux i l s = if i=Array.length rhs then s else
match l with
| (a,b)::tl -> let s = Printf.sprintf "%s %s:%d-%d"
s (str_symb_in_rhs_reduction str_non_ter rhs.(i)) a b in
aux (i+1) tl s
| _ -> assert false
in
(aux 0 l "")^"\n"
let insert_reduction2 rl sn counters use_rule_order =
let v = sn.state_nb and pdev = sn.pdev in
let aux2 rn rl =
let rhs = pdev.gram_rhs.(rn)
and _, _, ind = pdev.lhs_table.(rn) in
let paths =
if 1 land pdev.rule_options.(rn) = 1 then find_paths sn rhs
else find_paths_no_layout sn rhs counters.last_layout
in
List.fold_left
(fun rl (p,tnb) ->
insert_partially_ordered rl ((sn,p,tnb),(ind,rhs),rn))
rl paths
in
if use_rule_order then
let rn =
Int_set.fold (fun rn minrn -> min minrn rn)
pdev.table_it.(v).reducible max_int
in
(if rn < max_int then aux2 rn rl else rl)
else
try Int_set.fold aux2 pdev.table_it.(v).reducible rl
with Invalid_argument _ ->
(Printf.fprintf stderr "v = %d, table_it len = %d\n" v
(Array.length pdev.table_it); exit 2)
let insert_reduction_with_link rl sn link counters use_rule_order =
if !dypgen_verbose>2 then
output_string !log_channel "insert_reduction_with_link called\n";
let v = sn.state_nb and pdev = sn.pdev in
let sn1 = link.dest in
let aux2 rn rl =
let rhs = pdev.gram_rhs.(rn)
and _, _, ind = pdev.lhs_table.(rn) in
let paths =
if rhs = [||] then [[],sn.last_token] else
if 1 land pdev.rule_options.(rn) = 1
then find_paths ~skip:1 ~init:[link] sn1 rhs
else if sn1.last_token < counters.last_layout then []
else find_paths_no_layout ~skip:1 ~init:[link] sn1 rhs
counters.last_layout
in
List.fold_left
(fun rl (p,tnb) ->
insert_partially_ordered rl ((sn,p,tnb),(ind,rhs),rn))
rl paths
in
if use_rule_order then
let rn =
Int_set.fold (fun rn minrn -> min minrn rn)
pdev.table_it.(v).reducible max_int
in
(if rn < max_int then aux2 rn rl else rl)
else
try Int_set.fold aux2 pdev.table_it.(v).reducible rl
with Invalid_argument _ ->
(Printf.fprintf stderr "v = %d, table_it len = %d\n" v
(Array.length pdev.table_it); exit 2)
let find_all_prev_nodes sn =
let rec aux (n_list,n_set) curr_sn =
if Int_set.mem curr_sn.sn_nb n_set then (n_list,n_set) else
let n_set = Int_set.add curr_sn.sn_nb n_set in
let n_list = curr_sn::n_list in
List.fold_left aux (n_list,n_set) curr_sn.prev_nodes_eps
in
let pn, _ = aux ([],Int_set.empty) sn in
pn
let insert_reduction rl sn link counters _ use_rule_order =
match sn.prev_nodes_eps with
| [] -> insert_reduction_with_link rl sn link
counters use_rule_order
| _ ->
if !dypgen_verbose>2 then
output_string !log_channel "insert_reduction called\n";
let prev_nodes = find_all_prev_nodes sn in
let edge_nb = link.edge_id in
let aux1 rl sn =
let v = sn.state_nb and pdev = sn.pdev in
let aux2 rn rl =
let rhs = pdev.gram_rhs.(rn)
and _, _, ind = pdev.lhs_table.(rn) in
let paths =
if 1 land pdev.rule_options.(rn) = 1 then find_paths sn rhs
else find_paths_no_layout sn rhs counters.last_layout in
List.fold_left
(fun rl (p,tnb) ->
insert_partially_ordered rl ((sn,p,tnb),(ind,rhs),rn))
rl
(List.filter (fun (p,_) ->
List.exists (fun e -> e.edge_id=edge_nb) p)
paths)
in
if use_rule_order then
(let rn =
Int_set.fold (fun rn minrn -> min minrn rn)
pdev.table_it.(v).reducible max_int
in
if rn < max_int then aux2 rn rl else rl)
else
Int_set.fold aux2 pdev.table_it.(v).reducible rl
in
List.fold_left aux1 rl prev_nodes
exception Find_rightSib_failed
let str_bool = function true -> "true" | false -> "false"
(** [find_rightSib] is used in [reduceViaPath] and [doShift].
Its purpose is to find in the stack nodes list [snl] a stack node which
holds the same grammar as [g_leftSib] and which holds a state which has an
items set equal to [is_rightSib]. *)
let find_rightSib g_nb_rightSib st_nb snl (gd:'gd) (ld:'ld) layout_flags (gd_equal:'gd->'gd->bool) (ld_equal:'ld->'ld->bool) non_ter test_mergeable =
if !dypgen_verbose>2 then Printf.fprintf !log_channel "find_rightSib called\n";
let rec aux snl = match snl with
| [] -> raise Find_rightSib_failed
| sn::tl ->
if !dypgen_verbose>2 then
Printf.fprintf !log_channel
"sn.sn_nb = %d, sn.state_nb = %d, st_nb = %d, sn.pdev.g_nb = %d, g_nb_rightSib = %d, gd_equal = %s, ld_equal = %s, layout_flags = %d, sn.layout_flags = %d, test_mergeable = %s, sn.sn_non_ter = %d, non_ter = %d\n"
sn.sn_nb
sn.state_nb st_nb sn.pdev.g_nb g_nb_rightSib
(str_bool (gd_equal sn.global_data gd))
(str_bool (ld_equal sn.local_data ld))
layout_flags sn.layout_flags
(str_bool (test_mergeable sn))
sn.sn_non_ter non_ter;
if sn.state_nb = st_nb && sn.pdev.g_nb = g_nb_rightSib &&
gd_equal sn.global_data gd &&
ld_equal sn.local_data ld &&
layout_flags = sn.layout_flags &&
(test_mergeable sn) &&
sn.sn_non_ter = non_ter
then sn
else aux tl
in
aux snl
let print_sn sn =
let chan = !log_stack_channel in
let { state_nb = v; pdev = parsing_device; sn_nb = snnb;
last_token = last_token; _ } = sn
in
let gram_rhs = parsing_device.gram_rhs in
let lhs_table = parsing_device.lhs_table in
let is = parsing_device.table_it.(v) in
let str_non_ter = parsing_device.str_non_ter in
output_string chan "____________________________________\n";
Printf.fprintf chan "STACK NODE <%d>, last token:%d\n" snnb last_token;
output_string chan "\n";
print_item_set chan is gram_rhs lhs_table
str_non_ter sn.pdev.str_ter parsing_device.regexp_array;
Printf.fprintf chan " state number: %d\n" v;
output_string chan "\n";
let f3 f4 ed =
let ednb = ed.edge_id in
let { state_nb = v; sn_nb = snnb; _} = f4 ed in
Printf.fprintf chan " sn:%d ed:%d st:%d\n" snnb ednb v
in
output_string chan "\n";
output_string chan " predecessor stack nodes :\n";
output_string chan " successor stack nodes :\n";
let () = List.iter (f3 (fun e -> e.dest)) sn.succ_edges in
output_string chan "\n";
flush_all ()
let check_last_token last_token vl =
vl.last_token >= last_token
open Lexing
exception Find_link_failed
let remove_path_edge edge_nb pathList =
let aux1 e = match e with
| { edge_id = n ; _ } when n=edge_nb -> false
| _ -> true
in
let aux2 ((_,p,_),_,_) = List.for_all aux1 p in
List.filter aux2 pathList
let rec remove_sn_from_list sn_nb l res = match l with
| [] -> res
| sn::t when sn.sn_nb = sn_nb -> res@t
| sn::t -> remove_sn_from_list sn_nb t (sn::res)
let remove_edge edge_nb sn =
let rec aux succ_edges res = match succ_edges with
| [] -> res
| {edge_id = n; dest = sn_dest; _ }::t when n=edge_nb ->
if sn.last_token = sn_dest.last_token then
sn_dest.prev_nodes_eps <-
remove_sn_from_list sn.sn_nb sn_dest.prev_nodes_eps [];
res@t
| e::t -> aux t (e::res)
in
sn.succ_edges <- aux sn.succ_edges []
let merge_in_edge ppar counters edge_nb (sn, link, cons_index, objdata_list) (topmost, rl) =
if !dypgen_verbose>2 then
Printf.fprintf !log_channel "do_merge for edge: [%d]-%d-[%d]\nobjdata_list length = %d\n"
link.dest.sn_nb edge_nb sn.sn_nb (List.length objdata_list);
let obj_list, glo_dat, loc_dat =
ppar.merge_array.(cons_index) objdata_list in
if !dypgen_verbose>2 then
Printf.fprintf !log_channel "obj_list length = %d\n"
(List.length obj_list);
let pdev = sn.pdev in
if (ppar.global_data_equal glo_dat sn.global_data &&
ppar.local_data_equal loc_dat sn.local_data)
then
(link.edge_label <- obj_list;
link.edge_id <- edge_nb;
if !dypgen_verbose>2 then
Printf.fprintf !log_channel
"do_merge updates the contents of the edge: [%d]-%d-[%d]\n"
link.dest.sn_nb edge_nb sn.sn_nb;
topmost, rl)
else
match sn.succ_edges with _::_::_ ->
(if !dypgen_verbose>2 then
Printf.fprintf !log_channel
"do_merge removes the edge: [%d]-%d-[%d]\n"
link.dest.sn_nb edge_nb sn.sn_nb;
remove_edge edge_nb sn;
let rl = remove_path_edge edge_nb rl in
let rightSib, new_countsn, topmost, rS_is_new =
try
find_rightSib pdev.g_nb sn.state_nb topmost
sn.global_data sn.local_data sn.layout_flags
ppar.global_data_equal ppar.local_data_equal sn.sn_non_ter
(fun sn -> sn.pdev.state_is_mergeable.(sn.state_nb)),
counters.countsn, topmost, false
with Find_rightSib_failed ->
let rightSib =
create_v sn.state_nb pdev glo_dat loc_dat
counters.countsn sn.last_token sn.lexer_pos sn.layout_flags
(link.dest.det_depth+1) sn.sn_non_ter sn.visited_states
in
rightSib, counters.countsn+1, rightSib::topmost, true
in
let new_edge = create_e rightSib
obj_list counters.counted link.dest
(check_last_token link.dest.last_token) topmost link.parse_tree link.e_lexer_pos in
if !dypgen_verbose>2 then
(print_sn rightSib;
Printf.fprintf !log_channel
"do_merge creates a new edge%s:\n[%d]-%d-[%d]\n"
(if rS_is_new then " and a new node" else "")
link.dest.sn_nb counters.counted rightSib.sn_nb);
counters.countsn <- new_countsn;
counters.counted <- counters.counted + 1;
let rl =
if rS_is_new then
insert_reduction2 rl rightSib counters ppar.use_rule_order
else
insert_reduction rl rightSib new_edge counters
topmost ppar.use_rule_order
in
topmost, rl)
| _ ->
(sn.global_data <- glo_dat;
sn.local_data <- loc_dat;
link.edge_label <- obj_list;
link.edge_id <- edge_nb;
topmost, rl)
let collect_objs path start_node =
let rec aux res = function
| e1::((e2::_) as tl) ->
let str_non_ter = e2.dest.pdev.str_non_ter in
(match e2.dest.pdev.table_lit_trans.(e2.dest.state_nb) with
| Ps_Non_ter nt when str_non_ter.(nt).[0] = '0' -> aux res tl
| _ -> aux (e1.edge_label::res) tl)
| [e] ->
let str_non_ter = start_node.pdev.str_non_ter in
(match start_node.pdev.table_lit_trans.(start_node.state_nb) with
| Ps_Non_ter nt when str_non_ter.(nt).[0] = '0' -> res
| _ -> e.edge_label::res)
| [] -> []
in
let rec collect = function
| obj_list::tl ->
let obj_ll = collect tl in
let f1 new_obj_ll obj_l =
let f2 new_obj_ll obj =
(obj::obj_l)::new_obj_ll
in
List.fold_left f2 new_obj_ll obj_list
in
let obj_ll = List.fold_left f1 [] obj_ll in
obj_ll
| [] -> [[]]
in
collect (List.rev (aux [] path))
let print_s start_node last_pdev outchan =
print_table_state outchan
start_node.state_nb
last_pdev.table
last_pdev.table_it
last_pdev.table_lit_trans
last_pdev.gram_rhs
last_pdev.lhs_table
last_pdev.str_non_ter
last_pdev.str_ter
last_pdev.regexp_array
let print_g last_pdev outchan =
print_grammar outchan
last_pdev.gram_rhs
last_pdev.lhs_table
last_pdev.str_non_ter
last_pdev.str_ter
last_pdev.regexp_array
let position_map p start_node =
let rec aux res lpos = function
| [] -> res
| e::t ->
let pos = e.dest.lexer_pos in
aux ((e.e_lexer_pos, lpos)::res) pos t
in
aux [] start_node.lexer_pos (List.rev p)
let rec make_path sn n res =
if n = 0 then res else match sn.succ_edges with
e::_ -> make_path e.dest (n-1) (e::res) | _ -> assert false
let add_inherited_val sn0 sn1 argll nt =
let nt =
if sn0.pdev.g_nb = sn1.pdev.g_nb then nt
else
try Hashtbl.find sn0.pdev.nt_table sn1.pdev.str_non_ter.(nt)
with Not_found -> assert false
in
if sn0.pdev.bnt_array.(nt) then
let arg0l = sn0.inherited_values in
let arglll =
List.map (fun argl ->
List.map (fun arg0 -> arg0::argl) arg0l)
argll
in
List.flatten arglll
else argll
let find_end_node_pos sn_lexer_pos path =
let rec aux last_pos = function
| e::tl ->
if last_pos = e.dest.lexer_pos
then aux e.e_lexer_pos tl
else last_pos
| [] -> last_pos
in
match path with
| e::tl -> aux e.e_lexer_pos tl
| [] -> sn_lexer_pos
let compute_inherited_values sn ppar next_lexeme =
let state_id = sn.state_nb in
let pdev = sn.pdev in
let inh_opt = pdev.state_bnt.(state_id) in
match inh_opt with None -> () | Some inh ->
(let actl, argnb, lhs, _, ed = pdev.inherited.(inh) in
let path = make_path sn argnb [] in
let sn0 = match path with
| e::_ -> e.dest
| _ -> assert false
in
let path = if ed then List.tl path else path in
let argll = collect_objs path sn in
let argll = add_inherited_val sn0 sn argll lhs in
let position_list = position_map path sn in
let end_node_pos = find_end_node_pos sn.lexer_pos path in
let start_node_pos = sn.lexer_pos in
let symbol_pos = (end_node_pos, start_node_pos) in
let pp = {
pp_dev = pdev;
pp_par = ppar;
pp_gd = sn.global_data;
pp_ld = sn.local_data; }
in
let objll =
List.map (fun objl ->
List.map (fun ac ->
ac objl symbol_pos position_list sn.global_data sn.local_data
sn.local_data
{ prt_state = (print_s sn pdev); prt_grammar = (print_g pdev)}
pp next_lexeme)
actl)
argll
in
let res_l = List.flatten objll in
sn.inherited_values <- res_l)
let complete_reduction topmost rl mm parse_res _ leftSib pdev_rightSib global_data_rS local_data_rS v_rightSib new_obj nt symbol_pos counters ppar cons_index layout_flags _ tnb selfderiv pt next_lexeme start_node =
countred := !countred + 1;
try
let sn_id =
try Intc_map.find (pdev_rightSib.g_nb,v_rightSib) start_node.visited_states
with Not_found -> -1
in
let rightSib =
find_rightSib pdev_rightSib.g_nb v_rightSib topmost
global_data_rS local_data_rS layout_flags
ppar.find_rightSib_global_data_equal
ppar.find_rightSib_local_data_equal nt
(fun _ -> true)
in
let find_link rightSib leftSib =
let rec aux el = match el with
| [] ->
if rightSib.pdev.state_is_mergeable.(rightSib.state_nb)
|| start_node.sn_nb = rightSib.sn_nb || sn_id = rightSib.sn_nb
then raise Find_link_failed
else raise Find_rightSib_failed
| e::tl -> if (stack_node_equal e.dest leftSib) then e else aux tl
in
aux rightSib.succ_edges
in
try
let link = find_link rightSib leftSib in
let old_obj_list = link.edge_label in
let edge_nb = link.edge_id in
if (!dypgen_verbose>2 || ppar.merge_warning) then
(let (start_pos, end_pos) = symbol_pos in
let col1 = start_pos.pos_cnum - start_pos.pos_bol in
let col2 = end_pos.pos_cnum - end_pos.pos_bol in
Printf.fprintf !log_channel "Warning: the parser will merge the non terminal `%s'\nin file \"%s\", from l:%d,c:%d to l:%d,c:%d, constructor : %s\n"
(pdev_rightSib.str_non_ter.(nt))
start_pos.pos_fname
start_pos.pos_lnum col1 end_pos.pos_lnum col2
ppar.cons_str.(cons_index));
if !dypgen_verbose>2 then
Printf.fprintf !log_channel
"complete_reduction prepares a merge for [%d]-%d-[%d]\n"
leftSib.sn_nb edge_nb rightSib.sn_nb;
if tnb = counters.count_token || selfderiv then
let objdata_list =
List.map (fun o -> o, global_data_rS, local_data_rS)
old_obj_list
in
let topmost, rl =
merge_in_edge ppar counters edge_nb
(rightSib, link, cons_index, objdata_list) (topmost, rl)
in
topmost, rl, mm, parse_res
else
let mm =
let merge_item =
try
let (sn,_,_,objdata_list) = Int_map.find edge_nb mm in
sn, link, cons_index, (new_obj, global_data_rS, local_data_rS)::objdata_list
with Not_found ->
rightSib, link, cons_index,
match old_obj_list with
| _::_ ->
(new_obj, global_data_rS, local_data_rS)::
(List.map
(fun o -> o, global_data_rS, local_data_rS)
old_obj_list)
| _ -> assert false
in
Int_map.add edge_nb merge_item mm
in
topmost, rl, mm, parse_res
with
Find_link_failed ->
if ppar.global_data_equal global_data_rS rightSib.global_data &&
ppar.local_data_equal rightSib.local_data local_data_rS then ()
else raise Find_rightSib_failed;
let link = create_e rightSib [new_obj] counters.counted leftSib
(check_last_token leftSib.last_token) topmost pt (fst symbol_pos) in
if !dypgen_verbose>2 then
Printf.fprintf !log_channel
"complete_reduction creates a new edge:\n[%d]-%d-[%d]\n"
leftSib.sn_nb counters.counted rightSib.sn_nb;
counters.counted <- counters.counted + 1;
let rl =
insert_reduction rl rightSib link counters topmost
ppar.use_rule_order
in
topmost, rl, mm, parse_res
with
Find_rightSib_failed ->
let new_vs =
Intc_map.add (start_node.pdev.g_nb,start_node.state_nb)
start_node.sn_nb start_node.visited_states
in
let rightSib =
create_v v_rightSib pdev_rightSib global_data_rS local_data_rS counters.countsn
counters.count_token (snd symbol_pos) layout_flags (leftSib.det_depth+1) nt new_vs
in
let _ = create_e rightSib [new_obj] counters.counted leftSib
(check_last_token leftSib.last_token) topmost pt (fst symbol_pos)
in
if !dypgen_verbose>2 then
(print_sn rightSib;
Printf.fprintf !log_channel
"complete_reduction creates a new edge and a new node:\n[%d]-%d-[%d]\n"
leftSib.sn_nb counters.counted rightSib.sn_nb);
counters.countsn <- counters.countsn + 1;
counters.counted <- counters.counted + 1;
compute_inherited_values rightSib ppar next_lexeme;
let rl =
insert_reduction2 rl rightSib counters ppar.use_rule_order in
rightSib::topmost, rl, mm, parse_res
let print_pathList pathList =
output_string !log_channel " pathList :\n";
List.iter (fun ((start_node,p,_),_,rn) ->
output_string !log_channel " > ";
print_path start_node p;
let pdev = start_node.pdev in
output_string !log_channel
(" > "^(str_rule rn pdev.gram_rhs pdev.lhs_table pdev.str_non_ter
pdev.str_ter pdev.regexp_array)^"\n"))
pathList
let rec try_actions ac_l start_node toPass symbol_pos position_list pdev_leftSib local_data_lS pp next_lexeme use_all_actions res_list =
match ac_l with
| (Dypgen_action f)::tl_ac_l ->
let res_list, b =
(try
(f toPass symbol_pos position_list
pp.pp_gd local_data_lS pp.pp_ld
{ prt_state = (print_s start_node pp.pp_dev);
prt_grammar = (print_g pp.pp_dev)}
pp next_lexeme)::res_list, true
with Giveup ->
if !dypgen_verbose>2 then output_string !log_channel "Giveup\n";
res_list, false)
in
if b && not use_all_actions then res_list else
try_actions tl_ac_l start_node toPass symbol_pos
position_list pdev_leftSib local_data_lS pp next_lexeme use_all_actions res_list
| [] -> res_list
let match_chan_s_chan_g v_rightSib pdev_rightSib chan_s chan_g =
(match chan_s with
| None -> ()
| Some chan ->
print_table_state chan v_rightSib
pdev_rightSib.table
pdev_rightSib.table_it
pdev_rightSib.table_lit_trans
pdev_rightSib.gram_rhs
pdev_rightSib.lhs_table
pdev_rightSib.str_non_ter
pdev_rightSib.str_ter
pdev_rightSib.regexp_array);
(match chan_g with
| None -> ()
| Some chan ->
print_grammar chan
pdev_rightSib.gram_rhs
pdev_rightSib.lhs_table
pdev_rightSib.str_non_ter
pdev_rightSib.str_ter
pdev_rightSib.regexp_array)
let fold_rightSib_l pr counters symbol_pos start_node last_pdev _ leftSib ppar non_ter rn _ _ snd_node next_lexeme layout_flags ind tnb selfderiv pt cons_index (new_obj, will_shift2, keep_gram, newdata, newlocal_data, rapf_add, new_nt_cons, new_relations, chan_s, chan_g, pdev_option) (topmost, rl, mm, parse_res, will_shift) v_rightSib =
let v_rightSib, pdev_rightSib =
if rapf_add = [] then
match keep_gram, last_pdev.left_rec_rule.(rn) with
| true, true when snd_node.pdev.g_nb = last_pdev.g_nb ->
snd_node.state_nb, snd_node.pdev
| true, _ ->
let time1 = Sys.time () in
let v_rightSib, pdev_rightSib =
let is = copy_item_set leftSib.pdev.table_it.(v_rightSib) in
let lit_trans = leftSib.pdev.table_lit_trans.(v_rightSib) in
extend_parsing_device last_pdev leftSib.pdev is lit_trans ppar
in
let time2 = Sys.time () in
if !dypgen_verbose>0 then
Printf.fprintf !log_channel
"Automaton extended in %.3f sec\n" (time2-.time1);
v_rightSib, pdev_rightSib
| _ ,_ ->
(match pdev_option with
| None -> v_rightSib, leftSib.pdev
| Some pdev ->
let time1 = Sys.time () in
let v_rightSib, pdev_rightSib =
let is = copy_item_set leftSib.pdev.table_it.(v_rightSib) in
let lit_trans = leftSib.pdev.table_lit_trans.(v_rightSib) in
extend_parsing_device pdev leftSib.pdev is lit_trans ppar
in
let time2 = Sys.time () in
counters.count_g <- counters.count_g+1;
if !dypgen_verbose>0 then
Printf.fprintf !log_channel
"Automaton extended in %.3f sec\n" (time2-.time1);
v_rightSib,
{ pdev_rightSib with g_nb = counters.count_g })
else
let is = Some (leftSib.pdev.table_it.(v_rightSib)) in
let lit_trans = Some (leftSib.pdev.table_lit_trans.(v_rightSib)) in
match update_parsing_device ppar leftSib.pdev new_relations rapf_add
new_nt_cons counters is lit_trans with
| (Some start_ind), pdev -> start_ind, pdev
| _ -> assert false
in
let _ = match_chan_s_chan_g v_rightSib pdev_rightSib chan_s chan_g in
let topmost, rl, mm, parse_res =
let nt_entry_point, _ = leftSib.pdev.str_non_ter_prio.(leftSib.pdev.entry_point) in
let nt_ind, prio_ind = last_pdev.str_non_ter_prio.(ind) in
if (!dypgen_verbose>2) then
(fprintf !log_channel "nt_entry_point = %s, nt_ind = %s, prio_ind = %s\n"
nt_entry_point nt_ind prio_ind);
if
nt_entry_point = nt_ind && leftSib.last_token = 0 then
let parse_res = (new_obj, prio_ind)::parse_res in
complete_reduction topmost rl mm parse_res pr leftSib
pdev_rightSib newdata newlocal_data v_rightSib new_obj non_ter
symbol_pos counters ppar cons_index layout_flags
ind tnb selfderiv pt next_lexeme start_node
else
complete_reduction topmost rl mm parse_res pr leftSib
pdev_rightSib newdata newlocal_data v_rightSib new_obj non_ter
symbol_pos counters ppar cons_index layout_flags
ind tnb selfderiv pt next_lexeme start_node
in
topmost, rl, mm, parse_res, (will_shift2 && will_shift)
let reduce_with_result pr counters symbol_pos start_node last_pdev position_list leftSib ppar non_ter rn v_rightSib _ rhs snd_node next_lexeme layout_flags ind tnb selfderiv pt (topmost, rl, mm, parse_res, will_shift) ((new_obj, will_shift2, _, _, _, _, _, _, _, _, _) as res) =
let cons_index = try leftSib.pdev.cons_of_nt.(non_ter)
with e -> (Printf.printf "dyp.ml reduce_with, error cons_of_nt:%d\n"
(Array.length leftSib.pdev.cons_of_nt); raise e)
in
if ppar.test_cons.(cons_index) new_obj = false then
(let sr = str_rule rn leftSib.pdev.gram_rhs
leftSib.pdev.lhs_table leftSib.pdev.str_non_ter
leftSib.pdev.str_ter
leftSib.pdev.regexp_array
in
let cons = ppar.cons_str.(leftSib.pdev.cons_of_nt.(non_ter)) in
raise (Bad_constructor(sr,cons,(ppar.str_cons new_obj))));
if v_rightSib = -1 then
(if !dypgen_verbose>2 then output_string !log_channel
"v_rightSib = -1\n";
if !dypgen_verbose>2 then output_string !log_channel
"reduce_with found a new parse_result\n";
let _, prio_ind = last_pdev.str_non_ter_prio.(ind) in
let parse_res =
(new_obj,prio_ind)::parse_res
in
topmost, rl, mm, parse_res, (will_shift2 && will_shift))
else
fold_rightSib_l pr counters symbol_pos start_node last_pdev
position_list leftSib ppar non_ter rn ind rhs snd_node
next_lexeme layout_flags ind tnb selfderiv pt cons_index res
(topmost, rl, mm, parse_res, will_shift) v_rightSib
let table_find ht i = try Hashtbl.find ht i with Not_found -> -1
let reduce_with_action ac_l pr counters symbol_pos start_node position_list leftSib pp non_ter rn v_rightSib ind rhs snd_node next_lexeme layout_flags tnb selfderiv pt (topmost, rl, mm, parse_res, will_shift) toPass =
let res_list =
try_actions ac_l start_node toPass symbol_pos
position_list leftSib.pdev leftSib.local_data pp next_lexeme pp.pp_par.use_all_actions []
in
List.fold_left
(reduce_with_result pr counters symbol_pos start_node pp.pp_dev
position_list leftSib pp.pp_par non_ter rn v_rightSib ind rhs snd_node
next_lexeme layout_flags ind tnb selfderiv pt)
(topmost, rl, mm, parse_res, will_shift) res_list
let process_v_rightSib_l ac_l pr counters symbol_pos start_node last_pdev position_list leftSib ppar non_ter rn _ rhs snd_node layout_flags ind tnb pt obj_ll next_lexeme_precursor (topmost, rl, mm, parse_res, will_shift) (v_rightSib, lS_ind) =
if !dypgen_verbose>2 then
(Printf.fprintf !log_channel "obj_ll length=%d\n"
(List.length obj_ll);
List.iter (fun ol ->
Printf.fprintf !log_channel "obj_l length=%d\n" (List.length ol))
obj_ll;
Printf.fprintf !log_channel "v_rightSib = %d\n" v_rightSib;
print_pathList rl; flush_all ());
if v_rightSib = -1 && not (leftSib.pdev.entry_point = lS_ind
&& leftSib.last_token = 0)
then (topmost, rl, mm, parse_res, will_shift) else
let next_lexeme () = next_lexeme_precursor leftSib.pdev leftSib.global_data leftSib.local_data in
let selfderiv = htc last_pdev.po ind ind in
let pp = {
pp_dev = last_pdev;
pp_par = ppar;
pp_gd = start_node.global_data;
pp_ld = start_node.local_data }
in
List.fold_left
(reduce_with_action ac_l pr counters
symbol_pos start_node position_list leftSib
pp non_ter rn v_rightSib ind rhs snd_node
next_lexeme layout_flags tnb selfderiv pt)
(topmost, rl, mm, parse_res, will_shift)
obj_ll
let find_first_no_eps start_node p =
let rec aux = function
| e1::((e2::_) as tl) ->
if e1.dest.last_token = e2.dest.last_token then aux tl
else e2.dest
| [_] | [] -> start_node
in
aux p
let merge_in_path ppar counters p topmost rl mm =
let f (topmost, rl, mm) e =
if !dypgen_verbose>2 then
Printf.fprintf !log_channel "Try to merge edge %d.\n" e.edge_id;
try
let merge_item = Int_map.find e.edge_id mm in
let mm = Int_map.remove e.edge_id mm in
let topmost, rl =
merge_in_edge ppar counters e.edge_id merge_item (topmost, rl)
in
topmost, rl, mm
with Not_found ->
(if !dypgen_verbose>2 then
Printf.fprintf !log_channel "Edge %d not found in merge map.\n" e.edge_id;
topmost, rl, mm)
in
List.fold_left f (topmost, rl, mm) p
let reduceViaPath (((start_node,p,tnb),(ind,rhs),rn) as pr) rl mm topmost parse_res ppar counters next_lexeme_precursor last_pr merge_reduce =
if !dypgen_verbose>2 then
(Printf.fprintf !log_channel
"\nreduceViaPath called, start_node=%d, pathList length=%d\n "
start_node.sn_nb (List.length rl);
let pdev = start_node.pdev in
output_string !log_channel
((str_rule rn pdev.gram_rhs pdev.lhs_table pdev.str_non_ter
pdev.str_ter pdev.regexp_array)^"\n ");
print_path start_node p;
flush_all ());
let topmost, rl, mm = merge_in_path ppar counters p topmost rl mm in
let position_list = position_map p start_node in
let leftSib, snd_node = match p with
| [] -> start_node, start_node
| [h] -> h.dest, start_node
| h1::h2::_ -> h1.dest, h2.dest
in
let end_node_pos = find_end_node_pos start_node.lexer_pos p in
let start_node_pos = start_node.lexer_pos in
let symbol_pos = (end_node_pos, start_node_pos) in
let pt =
if !dypgen_verbose>2 then
(let pdev = start_node.pdev in
let lhs_s =
Printf.sprintf "%s:%d-%d -> "
(str_lhs pdev.lhs_table.(rn) pdev.str_non_ter)
leftSib.last_token start_node.last_token
in
Printf.fprintf !log_channel " %s%s\n" lhs_s
(str_rhs_reduction start_node p pdev.gram_rhs.(rn) pdev.str_non_ter);
let pt_rhs = String.concat " " (List.map (fun e -> e.parse_tree) p) in
let pt = "("^lhs_s^pt_rhs^")" in
output_string !log_channel (pt^"\n");
pt)
else ""
in
let first_no_eps = find_first_no_eps start_node p in
let layout = (1 land first_no_eps.layout_flags = 1) && p <> [] in
let layout_flags =
let first_flag =
match p with [] -> 0 | _ -> 1 land first_no_eps.layout_flags
in
first_flag lor
((2 land start_node.pdev.rule_options.(rn)) land
(2 land start_node.layout_flags))
in
if !dypgen_verbose>2 then
(Printf.fprintf !log_channel "first_no_eps = [%d], layout_flags = %d\n" first_no_eps.sn_nb layout_flags);
let v = leftSib.state_nb in
let last_pdev = start_node.pdev in
let obj_ll =
collect_objs p start_node in
let obj_ll =
add_inherited_val leftSib start_node obj_ll ind in
let ac_l = start_node.pdev.actions.(rn) in
let non_ter =
try Hashtbl.find leftSib.pdev.nt_table last_pdev.str_non_ter.(ind)
with Not_found -> assert false
in
let v_rightSib_l =
if layout then
Int_set.fold
(fun nt l -> (table_find leftSib.pdev.table.(v).(1) nt, nt)::l)
leftSib.pdev.nt_ntl_array.(non_ter) []
else
Int_set.fold
(fun nt l ->
let ns = table_find leftSib.pdev.table.(v).(3) nt in
if ns = -1 then (table_find leftSib.pdev.table.(v).(1) nt, nt)::l
else (ns, nt)::l)
leftSib.pdev.nt_ntl_array.(non_ter) []
in
let topmost, rl, mm, parse_res, will_shift =
List.fold_left (process_v_rightSib_l ac_l pr counters
symbol_pos start_node last_pdev position_list leftSib
ppar non_ter rn ind rhs snd_node
layout_flags ind tnb pt obj_ll
next_lexeme_precursor)
(topmost, rl, mm, parse_res, true) v_rightSib_l
in
let topmost =
if not will_shift then
(start_node.prev_nodes_eps <- [];
List.filter (function x -> x!=start_node) topmost)
else topmost
in
merge_reduce rl mm topmost parse_res
(if tnb=counters.count_token then last_pr else Some pr)
let do_merge mm rl topmost ppar counters =
if !dypgen_verbose>2 then
(Printf.fprintf !log_channel "do_merge called\n";
flush_all ());
Int_map.fold (merge_in_edge ppar counters) mm (topmost, rl)
let make_reduction_list topmost counters ppar =
let aux1 pathList sn =
let st_nb = sn.state_nb and pdev = sn.pdev in
let aux2 rn pathList =
let (_,_,ind) = pdev.lhs_table.(rn) in
let rhs = pdev.gram_rhs.(rn) in
let paths =
if 1 land pdev.rule_options.(rn) = 1 then find_paths sn rhs
else find_paths_no_layout sn rhs counters.last_layout in
let paths = list_map (function (p,tnb) -> sn,p,tnb) paths in
let aux3 pathList p =
insert_partially_ordered pathList (p,(ind,rhs),rn) in
List.fold_left aux3 pathList paths
in
if ppar.use_rule_order then
let rn =
Int_set.fold (fun rn minrn -> min minrn rn)
pdev.table_it.(st_nb).reducible max_int
in
if rn = max_int then pathList
else aux2 rn pathList
else
Int_set.fold aux2 pdev.table_it.(st_nb).reducible pathList
in
List.fold_left aux1 [] topmost
let do_reductions topmost counters ppar next_lexeme_precursor =
let rl = make_reduction_list topmost counters ppar in
let rec merge_reduce rl mm topmost parse_res last_pr =
match rl with
| pr::tl ->
reduceViaPath pr tl mm topmost parse_res ppar counters
next_lexeme_precursor last_pr merge_reduce
| [] -> topmost, parse_res, mm
in
let topmost, parse_res, mm = merge_reduce rl Int_map.empty topmost [] None in
let topmost, _ = do_merge mm [] topmost ppar counters in
topmost, parse_res
let do_shifts_for_each _ tok_value _ lexbuf counters ppar _ _ pt sn layout_flags parsing_device next_lexeme_precursor topmost next_state =
try
let rightSib =
find_rightSib parsing_device.g_nb next_state topmost
sn.global_data sn.local_data layout_flags
ppar.global_data_equal ppar.local_data_equal 0
(fun sn -> sn.pdev.state_is_mergeable.(sn.state_nb))
in
let _ = create_e rightSib [tok_value] counters.counted sn
(check_last_token sn.last_token) topmost pt
(fst (ppar.lexbuf_position_fun lexbuf))
in
if (!dypgen_verbose>2) then
Printf.fprintf !log_channel
"do_shifts creates a new edge:\n [%d]-%d-[%d]\n"
sn.sn_nb counters.counted rightSib.sn_nb;
counters.counted <- counters.counted + 1;
topmost
with Find_rightSib_failed ->
let fst_pos, snd_pos = ppar.lexbuf_position_fun lexbuf in
let rightSib =
create_v next_state parsing_device sn.global_data sn.local_data counters.countsn
counters.count_token snd_pos
layout_flags (sn.det_depth+1) 0 Intc_map.empty
in
let _ = create_e rightSib [tok_value] counters.counted sn
(check_last_token sn.last_token) topmost pt fst_pos in
if !dypgen_verbose>2 then
(print_sn rightSib;
Printf.fprintf !log_channel
"do_shifts creates a new edge and a new node:\n [%d]-%d-[%d]\n"
sn.sn_nb counters.counted rightSib.sn_nb);
counters.countsn <- counters.countsn + 1;
counters.counted <- counters.counted + 1;
let next_lexeme () = next_lexeme_precursor rightSib.pdev rightSib.global_data rightSib.local_data in
compute_inherited_values rightSib ppar next_lexeme;
rightSib::topmost
let do_shifts tok_name tok_value prevTops lexbuf counters ppar layout lexeme next_lexeme_precursor =
let layout_flags = if layout then 3 else 2 in
let pt =
if !dypgen_verbose>2 then
"\""^lexeme^"\":"^(string_of_int counters.count_token)
else ""
in
let f topmost sn =
let state_nb, parsing_device =
sn.state_nb, sn.pdev
in
if layout && (2 land sn.layout_flags = 0)
then topmost else
let table = parsing_device.table in
let next_state =
if layout then table_find table.(state_nb).(0) tok_name
else
let ns = table_find table.(state_nb).(2) tok_name in
if ns = -1 then table_find table.(state_nb).(0) tok_name
else ns
in
if next_state = -1 then topmost else
do_shifts_for_each tok_name tok_value prevTops lexbuf counters
ppar layout lexeme pt sn layout_flags parsing_device
next_lexeme_precursor topmost next_state
in
List.fold_left f [] prevTops
let end_of_parsing_info time1 counters =
if !dypgen_verbose>1 then
(let time2 = Sys.time () in
Printf.fprintf !log_channel "parsing time = %.3f\n"
(time2-.time1);
output_string !log_channel ("number of stack nodes = "^
(string_of_int counters.countsn)^"\n");
output_string !log_channel ("number of edges = "^
(string_of_int counters.counted)^"\n");
output_string !log_channel ("number of reductions = "^
(string_of_int !countred)^"\n");
flush_all ());
()
let init_parser parser_pilot entry_point lexpos lexbuf keep_data use_rule_order use_all_actions =
let counters = new_counters () in
let { pp_dev = parsing_device; pp_par = ppar; pp_gd = gd; pp_ld = ld } = parser_pilot in
let entry_point =
try Hashtbl.find parsing_device.nt_table (entry_point^"(_)")
with Not_found -> assert false
in
let parsing_device = { parsing_device with entry_point = entry_point } in
let ppar =
let f = match lexpos with
| Some f -> f
| None -> ppar.lexbuf_position_fun
in
let frS_global_de, frS_local_de = match keep_data with
| `both -> ppar.global_data_equal, ppar.local_data_equal
| `global -> ppar.global_data_equal, ppar.find_rightSib_local_data_equal
| `local -> ppar.find_rightSib_global_data_equal, ppar.local_data_equal
| `none -> ppar.find_rightSib_global_data_equal,
ppar.find_rightSib_local_data_equal
in
{ ppar with lexbuf_position_fun = f;
find_rightSib_global_data_equal = frS_global_de;
find_rightSib_local_data_equal = frS_local_de;
use_rule_order = use_rule_order;
use_all_actions = use_all_actions }
in
let log_count = ref 0 in
if !dypgen_verbose>2 then (
(try
let log_count_chan = open_in "dypgen_log_count" in
let dlc = input_line log_count_chan in
let dlc = if dlc = "" then "0" else dlc in
let () = log_count := int_of_string dlc in
close_in log_count_chan
with _ -> log_count := 0);
let log_count_chan = open_out "dypgen_log_count" in
output_string log_count_chan (string_of_int (!log_count+1));
close_out log_count_chan;
log_channel :=
open_out ("dypgen_"^(string_of_int !log_count)^".log");
log_stack_channel :=
open_out ("dypgen_gs_"^(string_of_int !log_count)^".log");
output_string !log_stack_channel
"\n\n--------------- Graph Structured Stack ---------------\n\n");
let start_state =
try
Hashtbl.find parsing_device.entry_points entry_point
with Not_found ->
let p = parsing_device in
match p.stations.(entry_point) with
| Some s ->
(Hashtbl.add p.entry_points entry_point s;
s)
| None -> assert false
in
let start =
create_v start_state.number parsing_device gd ld counters.countsn 0
(snd (ppar.lexbuf_position_fun lexbuf)) 2 0 0 Intc_map.empty
in
if !dypgen_verbose>2 then print_sn start;
{ counters with countsn=counters.countsn+1 },
[start], parsing_device, ppar
let clean_topmost topmost =
List.iter (fun sn ->
sn.prev_nodes_eps <- [])
topmost
let parse parser_pilot (entry_point:string)
?global_data
?local_data
?(match_len=`longest)
?(keep_data=`both)
?lexpos
?(use_rule_order=false)
?(use_all_actions=false)
(lexfun:('a -> 'token)) (lexbuf:'a) =
let time1 = Sys.time () in
let parser_pilot = match global_data with
Some gd -> { parser_pilot with pp_gd = gd } | None -> parser_pilot
in
let parser_pilot = match local_data with
Some ld -> { parser_pilot with pp_ld = ld } | None -> parser_pilot
in
let counters, topmost, _, ppar =
init_parser parser_pilot entry_point
lexpos lexbuf keep_data use_rule_order use_all_actions
in
let next_lexeme_precursor _ _ _ =
failwith "next_lexeme must not be called when using an external lexer"
in
let rec aux_LR0 topmost t prev_parse_result =
if !dypgen_verbose>2 then
(Printf.fprintf !log_channel "\ndo_shift : %s, size of topmost = %d\n"
(ppar.str_token t) (List.length topmost); flush_all ());
counters.count_token <- counters.count_token + 1;
clean_topmost topmost;
let topmost =
do_shifts (ppar.get_name t) (ppar.get_value t) topmost
lexbuf counters ppar true "" next_lexeme_precursor
in
let topmost, parse_result =
do_reductions topmost counters ppar next_lexeme_precursor
in
if topmost = []
then (
match parse_result with _::_ -> parse_result
| _ -> (match prev_parse_result with _::_ -> prev_parse_result
| _ -> raise Syntax_error))
else match match_len, parse_result with
| `shortest, _::_ -> parse_result
| _, _::_ -> aux_LR0 topmost (lexfun lexbuf) parse_result
| _ -> aux_LR0 topmost (lexfun lexbuf) prev_parse_result
in
let parse_result =
try (
let topmost, _ =
do_reductions topmost counters ppar next_lexeme_precursor
in
aux_LR0 topmost (lexfun lexbuf) [])
with Syntax_error ->
(flush_all ();
raise Syntax_error)
in
end_of_parsing_info time1 counters;
parse_result
let select_act_id =
let choose_id a b = match a, b with
| a, b when a<0 -> b
| a, b when b<0 -> a
| _ -> min a b
in
function h::t ->
let rec aux k = function
| p::t when p>=0 -> aux (choose_id k p) t
| [] -> k
| _::t -> aux k t
in
aux h t
| [] -> failwith "min_list: empty list"
let all_neg l = List.for_all (fun x -> x<0) l
let no_double = function [] -> assert false | i::t ->
let rec aux i t l = match t with
| j::u when j=i -> aux i u l
| j::u -> aux j u (j::l)
| [] -> l
in
aux i t [i]
let select_token topmost lexbuf all_token reset_start_pos select_unexpected layout =
if !dypgen_verbose>2 then
output_string !log_channel "select_token called\n";
let topmost = List.sort (fun v1 v2 ->
Stdlib.compare v1.pdev.lex_nb v2.pdev.lex_nb) topmost in
let tbl_l, pdev_l, snll, snl, _ = List.fold_left
(fun (tl,pl,snll,snl,i) sn ->
if i=sn.pdev.lex_nb then tl,pl,snll,sn::snl,i else
sn.pdev.main_lexer_table::tl,sn.pdev::pl,
(match snl with [] -> snll | _ -> snl::snll),
[sn],sn.pdev.lex_nb)
([],[],[],[],-1) topmost
in
let snll = match snl with [] -> snll | _ -> snl::snll in
let res = lex_engine true tbl_l lexbuf.lb_lexbuf reset_start_pos in
if !dypgen_verbose>2 then
(Printf.fprintf !log_channel
"lex_engine: res length=%d, snll len=%d, pdev_l len=%d\n"
(List.length res)
(List.length snll)
(List.length pdev_l);
List.iter (fun l -> List.iter (fun sn -> Printf.fprintf !log_channel
"sn:%d " sn.sn_nb) l; Printf.fprintf !log_channel "\n") snll);
let find_match3 pdev l snl =
let aux1 sn l =
let f select_unexpected l = List.fold_left (fun l aid ->
if pdev.main_lexer_ter_id.(aid) = pdev.layout_id
then (-aid-1)::l else
let ter_id =
try pdev.main_lexer_ter_id.(aid)
with _ -> assert false
in
if table_find sn.pdev.table.(sn.state_nb).(0) ter_id <> -1 ||
(table_find sn.pdev.table.(sn.state_nb).(2) ter_id <> -1
&& not layout) || select_unexpected
then (
aid::l)
else ( l))
[] l
in
let rec aux2 layout_matched l0 = match l0 with
| act_id::t ->
let ter_id =
try pdev.main_lexer_ter_id.(act_id)
with _ -> assert false
in
if table_find sn.pdev.table.(sn.state_nb).(0) ter_id <> -1 ||
(table_find sn.pdev.table.(sn.state_nb).(2) ter_id <> -1
&& not layout)
then
(
(Some (if all_token then (f select_unexpected l) else [act_id])),
layout_matched)
else if ter_id = pdev.layout_id then
if all_token then
(Some (f select_unexpected l)), layout_matched
else aux2 (Some [-act_id-1]) t
else if select_unexpected then
(Some (if all_token then (f true l) else [act_id])), layout_matched
else (
aux2 layout_matched t)
| [] ->
None, layout_matched
in
let matched, layout_matched = aux2 None l in
match matched with
| Some _ -> matched
| _ -> layout_matched
in
let act_id_l = List.fold_left
(fun ail sn -> match aux1 sn l with
| None -> ail
| Some idl -> idl@ail)
[] snl
in
match act_id_l with [] -> None
| x ->
let x = no_double (List.sort Stdlib.compare x) in
Some ((if all_token then x else [select_act_id x]), pdev)
in
let rec find_match2 aid_pdev_layout accu_layout = function
| ([-1]::t1),(_::t2),(_::t3) ->
find_match2 aid_pdev_layout accu_layout (t1,t2,t3)
| (l::t1),(pdev::t2),(snl::t3) -> (match find_match3 pdev l snl with
| None -> find_match2 aid_pdev_layout accu_layout (t1,t2,t3)
| Some (action_id_l, pdev) ->
if all_neg action_id_l then
find_match2 (Some ([List.hd action_id_l], pdev))
(snl@accu_layout) (t1,t2,t3)
else Some (action_id_l, pdev, snl))
| _ -> (match aid_pdev_layout with None -> None
| Some (aid, pdev) -> Some (aid, pdev, accu_layout))
in
let rec find_match1 layout_match = function
| (p,l)::t ->
if !dypgen_verbose>4 then
Printf.fprintf !log_channel "pos=%d\n"
(p - lexbuf.lb_lexbuf.lex_abs_pos);
(match find_match2 None [] (List.rev l, pdev_l, snll) with
| None -> find_match1 layout_match t
| Some ((aid, _, _) as x) ->
if all_neg aid then (match layout_match with
| None -> find_match1 (Some (x,p)) t
| Some _ -> find_match1 layout_match t)
else
(
(Some (x,p)), layout_match))
| [] -> None, layout_match
in
let x = find_match1 None res in
(if !dypgen_verbose>2 then match x with
| None, None -> output_string !log_channel "select_token ends, no match\n"
| _ ->
Printf.fprintf !log_channel
"select_token ends, lexbuf.lb_lexbuf.lex_curr_p.pos_cnum = %d\n"
lexbuf.lb_lexbuf.lex_curr_p.pos_cnum);
x
let rec lex_token topmost lexbuf layout all_token =
let old_pos = lexbuf.lb_lexbuf.lex_curr_p.pos_cnum in
match select_token topmost lexbuf all_token true false layout with
| (None, None) -> None
| (_, Some ((i::_, pdev, new_topmost),p)) ->
lexbuf.lb_lexbuf.lex_curr_p <-
{ lexbuf.lb_lexbuf.lex_curr_p with Lexing.pos_cnum = p };
lexbuf.lb_lexbuf.lex_curr_pos <-
p - lexbuf.lb_lexbuf.lex_abs_pos;
let lexbuf = { lexbuf with lb_aux_lex = pdev.aux_lexer } in
if old_pos = lexbuf.lb_lexbuf.lex_curr_p.pos_cnum
then failwith("lexing: empty token");
if !dypgen_verbose>2 then
output_string !log_channel "lex_token called bis\n";
let action =
try pdev.main_lexer_actions.(-i-1)
with Invalid_argument _ -> assert false
in
let _ = action lexbuf in
lex_token new_topmost lexbuf true all_token
| (Some ((action_id_l, pdev, new_topmost),p), None) ->
lexbuf.lb_lexbuf.lex_curr_p <-
{ lexbuf.lb_lexbuf.lex_curr_p with Lexing.pos_cnum = p };
lexbuf.lb_lexbuf.lex_curr_pos <-
p - lexbuf.lb_lexbuf.lex_abs_pos;
let lexbuf = { lexbuf with lb_aux_lex = pdev.aux_lexer } in
let ter_obj_l = List.fold_left
(fun l action_id ->
if action_id<0 then l else
let ter, action =
try (pdev.main_lexer_ter_id.(action_id),
pdev.main_lexer_actions.(action_id))
with Invalid_argument _ -> assert false
in
(ter, action lexbuf)::l)
[] action_id_l
in
Some (ter_obj_l, new_topmost, lexbuf, layout)
| (_, Some (([], _, _),_)) -> assert false
let next_lexeme_precur_lb_pdev lexbuf pdev gd ld =
let snl =
[create_v 0 pdev gd ld 0 0 Lexing.dummy_pos 2 0 0 Intc_map.empty]
in
let first_curr_pos = lexbuf.lb_lexbuf.lex_curr_pos in
let old_start_pos = lexbuf.lb_lexbuf.lex_start_pos in
let rec next_token res =
let prev_curr_pos = lexbuf.lb_lexbuf.lex_curr_pos in
let prev_start_pos = lexbuf.lb_lexbuf.lex_start_pos in
let x =
try select_token snl lexbuf false false true false
with Failure _ ->
(
None, None)
in
let prev_curr_pos =
prev_curr_pos - prev_start_pos + lexbuf.lb_lexbuf.lex_start_pos in
let first_curr_pos =
first_curr_pos - old_start_pos + lexbuf.lb_lexbuf.lex_start_pos in
let len = lexbuf.lb_lexbuf.lex_curr_pos - prev_curr_pos in
match x with
| None, None ->
lexbuf.lb_lexbuf.lex_curr_pos <- first_curr_pos;
res
| None, (Some _) ->
let lexeme =
try Bytes.to_string (Bytes.sub lexbuf.lb_lexbuf.lex_buffer prev_curr_pos len)
with Invalid_argument _ ->
(Printf.printf "1; %s\n" (Bytes.to_string (lexbuf.lb_lexbuf.lex_buffer));
raise (Invalid_argument("String.sub")))
in
(if !dypgen_verbose>2 then
output_string !log_channel "next_lexeme called bis\n";
next_token (lexeme::res))
| (Some _), _ ->
let lexeme =
try Bytes.to_string (Bytes.sub lexbuf.lb_lexbuf.lex_buffer prev_curr_pos len)
with Invalid_argument _ ->
(Printf.printf "2; %i, %i\n" prev_curr_pos len;
raise (Invalid_argument("String.sub")))
in
(lexbuf.lb_lexbuf.lex_curr_pos <- first_curr_pos;
lexeme::res)
in
next_token []
let print_regexp_array pdev =
let toknb = Array.length pdev.str_ter in
output_string !log_channel "regexp_array:\n";
for i=0 to Array.length pdev.regexp_array -1 do
Printf.fprintf !log_channel "%d:<%s>\n" (i+toknb)
(print_pretty_regexp pdev.regexp_array.(i))
done;
output_string !log_channel "\n"
let print_main_lexer_ter_id mlti =
output_string !log_channel "main_lexer_ter_id:\n";
for i=0 to Array.length mlti -1 do
Printf.fprintf !log_channel "%d -> %d\n" i mlti.(i)
done;
output_string !log_channel "\n"
let lexparse parser_pilot (entry_point:string)
?global_data
?local_data
?(match_len=`longest)
?(keep_data=`both)
?(choose_token=`first)
?(use_rule_order=false)
?(use_all_actions=false)
lexbuf =
let time1 = Sys.time () in
let parser_pilot = match global_data with
Some gd -> { parser_pilot with pp_gd = gd } | None -> parser_pilot
in
let parser_pilot = match local_data with
Some ld -> { parser_pilot with pp_ld = ld } | None -> parser_pilot
in
let counters, topmost, parsing_device, ppar =
init_parser parser_pilot entry_point
None lexbuf keep_data use_rule_order use_all_actions
in
if !dypgen_verbose>4 then
(print_dec_table parsing_device.main_lexer_table.tbl_trans;
print_final parsing_device.main_lexer_table.tbl_final;
print_regexp_array parsing_device;
print_main_lexer_ter_id parsing_device.main_lexer_ter_id);
let all_token = match choose_token with
`first -> false | `all -> true in
let next_lexeme_precursor pdev gd ld =
next_lexeme_precur_lb_pdev lexbuf pdev gd ld in
let rec aux_LR0 lexbuf topmost prev_parse_result =
if !dypgen_verbose>2 then
(Printf.fprintf !log_channel "prev_parse_result length = %d\n"
(List.length prev_parse_result); flush_all ());
counters.count_token <- counters.count_token + 1;
clean_topmost topmost;
match lex_token topmost lexbuf false all_token with
| None -> (match prev_parse_result with
| _::_ -> prev_parse_result
| _ -> raise Syntax_error)
| Some (ter_obj_l, topmost, lexbuf, layout) ->
if layout then counters.last_layout <- counters.count_token - 1;
if !dypgen_verbose>2 then
(List.iter (fun (ter_id,_) -> Printf.fprintf !log_channel
"\nTOKEN : %s, ter_id=%d\n"
(try (List.hd topmost).pdev.str_ter.(ter_id) with _ ->
(let pdev = (List.hd topmost).pdev in
Printf.sprintf "<regexp:%s>"
(print_pretty_regexp pdev.regexp_array.(ter_id-
(Array.length pdev.str_ter)))))
ter_id) ter_obj_l;
Printf.fprintf !log_channel
"size of topmost = %d lexbuf.lb_curr_p.pos_cnum = %d\n"
(List.length topmost)
lexbuf.lb_lexbuf.lex_curr_p.pos_cnum;
flush_all ());
let topmost = List.fold_left
(fun new_topmost (ter, obj) ->
(do_shifts ter obj topmost lexbuf counters ppar layout
(Dyplex.lexeme lexbuf) next_lexeme_precursor)
@new_topmost)
[] ter_obj_l
in
let next_lexeme_precursor pdev gd ld =
next_lexeme_precur_lb_pdev lexbuf pdev gd ld in
let topmost, parse_result =
do_reductions topmost counters ppar next_lexeme_precursor
in
if !dypgen_verbose>2 then
Printf.fprintf !log_channel "parse_result length = %d\n"
(List.length parse_result);
if topmost = []
then
(match parse_result with _::_ -> parse_result
| _ ->
(match prev_parse_result with
| _::_ -> prev_parse_result
| _ -> raise Syntax_error))
else (match match_len, parse_result with
| `shortest, _::_ -> parse_result
| _, _::_ -> aux_LR0 lexbuf topmost parse_result
| _ -> aux_LR0 lexbuf topmost prev_parse_result)
in
let parse_result =
try (
let topmost, _ =
do_reductions topmost counters ppar next_lexeme_precursor
in
aux_LR0 lexbuf topmost [])
with Syntax_error ->
(flush_all ();
raise Syntax_error)
in
end_of_parsing_info time1 counters;
parse_result
let function_free_pdev pdev =
let aux_lexer =
{ pdev.aux_lexer with aux_lexer_actions = Hashtbl.create 0 }
in
{ pdev with
ra_list = [];
actions = [||];
regexp_decl = Hashtbl.create 0;
main_lexer_actions = [||];
aux_lexer = aux_lexer }
let print_symb = function
| Ter s | Ter_NL s | Non_ter (s,_) | Non_ter_NL (s,_) -> print_string (s^" ")
| Regexp r | Regexp_NL r -> print_string ((print_regexp r)^" ")
let print_rule (s1,sl,s2,_) =
print_string (s1^" : ");
List.iter print_symb sl;
print_endline (", "^s2)
let import_functions cl_pdev pp ra_list =
let pdev = pp.pp_dev in
let ppar = pp.pp_par in
let ra_list =
List.map
(fun (r,a) -> (r, Dypgen_action (Tools.transform_action a), []))
ra_list
in
let ra_list = pdev.ra_list@ra_list in
let n = Hashtbl.fold (fun _ rn n -> max rn n) cl_pdev.rn_of_rule 0 in
let actions = Array.make (n+1) [] in
List.iter
(fun ((lhs,rhs,p,rol),a,_) ->
let rhs = List.map
(function
| Non_ter (nt,p) -> Non_ter (nt^(fst (string_of_nt_prio p)), No_priority)
| Non_ter_NL (nt,p) -> Non_ter_NL (nt^(fst (string_of_nt_prio p)), No_priority)
| x -> x)
rhs
in
let r = (lhs^"("^p^")"), rhs, "", rol in
let i =
try Hashtbl.find cl_pdev.rn_of_rule r
with Not_found ->
(print_rule r;
Hashtbl.iter (fun r _ -> print_rule r) cl_pdev.rn_of_rule;
failwith "A rule was not found in the saved parsing_device.")
in
actions.(i) <- a::actions.(i))
ra_list;
let main_lexer_actions =
Array.make (Array.length cl_pdev.main_lexer_ter_id) ppar.regexp_fun in
for i=cl_pdev.main_lexer_init_id
to cl_pdev.main_lexer_init_id + ppar.main_lexer_action_nb-1 do
main_lexer_actions.(i) <-
pdev.main_lexer_actions.(i-cl_pdev.main_lexer_init_id+pdev.main_lexer_init_id)
done;
{ cl_pdev with
ra_list = ra_list;
actions = actions;
regexp_decl = compile_regexp_decl cl_pdev.regexp_decl_list;
main_lexer_actions = main_lexer_actions;
aux_lexer = pdev.aux_lexer }
let is_re_name pp s = Hashtbl.mem pp.pp_dev.regexp_decl s