summaryrefslogtreecommitdiff
path: root/lily/parser.yy
blob: 159786b93c1dbc13268e72140f1cf7a485996177 (about) (plain)
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
/* -*- mode: c++; c-file-style: "linux"; indent-tabs-mode: t -*- */
/*
  This file is part of LilyPond, the GNU music typesetter.

  Copyright (C) 1997--2015 Han-Wen Nienhuys <hanwen@xs4all.nl>
                 Jan Nieuwenhuizen <janneke@gnu.org>

  LilyPond is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  LilyPond is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
*/

/* Mode and indentation are at best a rough approximation based on TAB
 * formatting (reasonable for compatibility with unspecific editor
 * modes as Bison modes are hard to find) and need manual correction
 * frequently.  Without a reasonably dependable way of formatting a
 * Bison file sensibly, there is little point in trying to fix the
 * inconsistent state of indentation.
 */

%{

#define yyerror Lily_parser::parser_error

/* We use custom location type: Input objects */
#define YYLTYPE Input
#define YYSTYPE SCM
#define YYLLOC_DEFAULT(Current,Rhs,N) \
	((Current).set_location ((Rhs)[1], (Rhs)[N]))

#define YYPRINT(file, type, value)					\
	do {								\
		if (scm_is_eq (value, SCM_UNSPECIFIED))			\
			break;						\
		SCM s = Display::value_to_lily_string (value);		\
		char *p = scm_to_locale_string (s);			\
		fputs (p, file);					\
		free (p);						\
	} while (0)

%}

%parse-param {Lily_parser *parser}
%parse-param {SCM *retval}
%lex-param {Lily_parser *parser}
%error-verbose
%debug

/* We use SCMs to do strings, because it saves us the trouble of
deleting them.  Let's hope that a stack overflow doesn't trigger a move
of the parse stack onto the heap. */

%left PREC_BOT
%nonassoc REPEAT
%nonassoc ALTERNATIVE

/* The above precedences tackle the shift/reduce problem

1.  \repeat
	\repeat .. \alternative

    \repeat { \repeat .. \alternative }

or

    \repeat { \repeat } \alternative
*/

%nonassoc COMPOSITE
%left ADDLYRICS

%right ':' UNSIGNED REAL E_UNSIGNED EVENT_IDENTIFIER EVENT_FUNCTION '^' '_'
       HYPHEN EXTENDER DURATION_IDENTIFIER '!'

 /* The above are needed for collecting tremoli and other items (that
    could otherwise be interpreted as belonging to the next function
    argument) greedily, and together with the next rule will serve to
    join numbers and units greedily instead of allowing them into
    separate function arguments
 */

%nonassoc NUMBER_IDENTIFIER

%left PREC_TOP




%pure-parser
%locations



%{ // -*-Fundamental-*-

/*
FIXME:

   * The rules for who is protecting what are very shady.  Uniformise
     this.

   * There are too many lexical modes?
*/

#include "config.hh"

#include <cctype>
#include <cstdlib>
#include <cstdio>
using namespace std;

#include "book.hh"
#include "context.hh"
#include "context-def.hh"
#include "context-mod.hh"
#include "dimensions.hh"
#include "file-path.hh"
#include "input.hh"
#include "international.hh"
#include "lily-guile.hh"
#include "lily-lexer.hh"
#include "lily-parser.hh"
#include "ly-module.hh"
#include "main.hh"
#include "misc.hh"
#include "music.hh"
#include "output-def.hh"
#include "paper-book.hh"
#include "scm-hash.hh"
#include "score.hh"
#include "text-interface.hh"
#include "warn.hh"
#include "lily-imports.hh"

void
Lily_parser::parser_error (Input const *i, Lily_parser *parser, SCM *, const string &s)
{
	parser->parser_error (*i, s);
}

// The following are somewhat precarious constructs as they may change
// the value of the lookahead token.  That implies that the lookahead
// token must not yet have made an impact on the state stack other
// than causing the reduction of the current rule, or switching the
// lookahead token while Bison is mulling it over will cause trouble.

#define MYBACKUP(Token, Value, Location)				\
	do {								\
		if (yychar != YYEMPTY)					\
			parser->lexer_->push_extra_token		\
				(yylloc, yychar, yylval);		\
		if (Token)						\
			parser->lexer_->push_extra_token		\
				(Location, Token, Value);		\
		parser->lexer_->push_extra_token (Location, BACKUP);	\
		yychar = YYEMPTY;					\
	} while (0)


#define MYREPARSE(Location, Pred, Token, Value)				\
	do {								\
		if (yychar != YYEMPTY)					\
			parser->lexer_->push_extra_token		\
				(yylloc, yychar, yylval);		\
		parser->lexer_->push_extra_token			\
			(Location, Token, Value);			\
		parser->lexer_->push_extra_token			\
			(Location, REPARSE, Pred);			\
		yychar = YYEMPTY;					\
	} while (0)

%}


%{

#define MY_MAKE_MUSIC(x, spot) \
	make_music_with_input (ly_symbol2scm (x), \
			       parser->lexer_->override_input (spot))

/* ES TODO:
- delay application of the function
*/

#define LOWLEVEL_MAKE_SYNTAX(location, proc, ...)			\
	with_location							\
		(parser->lexer_->override_input (location).smobbed_copy (), \
		 proc,							\
		 ##__VA_ARGS__)

/* Syntactic Sugar. */
#define MAKE_SYNTAX(name, location, ...)				\
	LOWLEVEL_MAKE_SYNTAX (location, Syntax::name, ##__VA_ARGS__)

#define START_MAKE_SYNTAX(name, ...)					\
	scm_list_n (Syntax::name, ##__VA_ARGS__, SCM_UNDEFINED)

#define FINISH_MAKE_SYNTAX(start, location, ...)			\
	LOWLEVEL_MAKE_SYNTAX						\
		(location,						\
		 Guile_user::apply,					\
		 scm_car (start),					\
		 scm_append_x						\
		 (scm_list_2 (scm_cdr (start),				\
			      scm_list_n (__VA_ARGS__, SCM_UNDEFINED))))

SCM get_next_unique_context_id ();
SCM get_next_unique_lyrics_context_id ();

#undef _
#if !HAVE_GETTEXT
#define _(x) x
#else
#include <libintl.h>
#define _(x) gettext (x)
#endif


static Music *make_music_with_input (SCM name, Input where);
SCM check_scheme_arg (Lily_parser *parser, Input loc,
		      SCM arg, SCM args, SCM pred, SCM disp = SCM_UNDEFINED);
SCM make_music_from_simple (Lily_parser *parser, Input loc, SCM pitch);
SCM loc_on_music (Lily_parser *parser, Input loc, SCM arg);
SCM make_chord_elements (Input loc, SCM pitch, SCM dur, SCM modification_list);
SCM make_chord_step (SCM step, Rational alter);
SCM make_simple_markup (SCM a);
SCM make_duration (SCM t, int dots = 0, SCM factor = SCM_UNDEFINED);
bool is_regular_identifier (SCM id, bool multiple=false);
SCM try_string_variants (SCM pred, SCM str);
int yylex (YYSTYPE *s, YYLTYPE *loc, Lily_parser *parser);

%}

/* The third option is an alias that will be used to display the
   syntax error.  Bison CVS now correctly handles backslash escapes.

   FIXME: Bison needs to translate some of these, eg, STRING.

*/

/* Keyword tokens with plain escaped name.  */
%token END_OF_FILE 0 "end of input"
%token ACCEPTS "\\accepts"
%token ADDLYRICS "\\addlyrics"
%token ALIAS "\\alias"
%token ALTERNATIVE "\\alternative"
%token BOOK "\\book"
%token BOOKPART "\\bookpart"
%token CHANGE "\\change"
%token CHORDMODE "\\chordmode"
%token CHORDS "\\chords"
%token CONSISTS "\\consists"
%token CONTEXT "\\context"
%token DEFAULT "\\default"
%token DEFAULTCHILD "\\defaultchild"
%token DENIES "\\denies"
%token DESCRIPTION "\\description"
%token DRUMMODE "\\drummode"
%token DRUMS "\\drums"
%token ETC "\\etc"
%token FIGUREMODE "\\figuremode"
%token FIGURES "\\figures"
%token HEADER "\\header"
%token INVALID "\\version-error"
%token LAYOUT "\\layout"
%token LYRICMODE "\\lyricmode"
%token LYRICS "\\lyrics"
%token LYRICSTO "\\lyricsto"
%token MARKUP "\\markup"
%token MARKUPLIST "\\markuplist"
%token MIDI "\\midi"
%token NAME "\\name"
%token NOTEMODE "\\notemode"
%token OVERRIDE "\\override"
%token PAPER "\\paper"
%token REMOVE "\\remove"
%token REPEAT "\\repeat"
%token REST "\\rest"
%token REVERT "\\revert"
%token SCORE "\\score"
%token SCORELINES "\\score-lines"
%token SEQUENTIAL "\\sequential"
%token SET "\\set"
%token SIMULTANEOUS "\\simultaneous"
%token TEMPO "\\tempo"
%token TYPE "\\type"
%token UNSET "\\unset"
%token WITH "\\with"

/* Keyword token exceptions.  */
%token NEWCONTEXT "\\new"


/* Other string tokens.  */

%token CHORD_BASS "/+"
%token CHORD_CARET "^"
%token CHORD_COLON ":"
%token CHORD_MINUS "-"
%token CHORD_SLASH "/"
%token ANGLE_OPEN "<"
%token ANGLE_CLOSE ">"
%token DOUBLE_ANGLE_OPEN "<<"
%token DOUBLE_ANGLE_CLOSE ">>"
%token E_BACKSLASH "\\"
%token E_EXCLAMATION "\\!"
%token E_PLUS "\\+"
%token EXTENDER "__"

/*
If we give names, Bison complains.
*/
%token FIGURE_CLOSE /* "\\>" */
%token FIGURE_OPEN /* "\\<" */
%token FIGURE_SPACE "_"
%token HYPHEN "--"

%token MULTI_MEASURE_REST


%token E_UNSIGNED
%token UNSIGNED

/* Artificial tokens, for more generic function syntax */
%token EXPECT_MARKUP "markup?"
%token EXPECT_SCM "scheme?"
%token BACKUP "(backed-up?)"
%token REPARSE "(reparsed?)"
%token EXPECT_MARKUP_LIST "markup-list?"
%token EXPECT_OPTIONAL "optional?"
/* After the last argument. */
%token EXPECT_NO_MORE_ARGS;

/* An artificial token for parsing embedded Lilypond */
%token EMBEDDED_LILY "#{"

%token BOOK_IDENTIFIER
%token CHORD_MODIFIER
%token CHORD_REPETITION
%token DRUM_PITCH
 /* Artificial token for durations in argument lists */
%token DURATION_ARG
%token DURATION_IDENTIFIER
%token EVENT_IDENTIFIER
%token EVENT_FUNCTION
%token FRACTION
%token LOOKUP_IDENTIFIER
%token LYRIC_ELEMENT
%token MARKUP_FUNCTION
%token MARKUP_LIST_FUNCTION
%token MARKUP_IDENTIFIER
%token MARKUPLIST_IDENTIFIER
%token MUSIC_FUNCTION
%token MUSIC_IDENTIFIER
%token NOTENAME_PITCH
%token NUMBER_IDENTIFIER
%token PITCH_IDENTIFIER
%token REAL
%token RESTNAME
%token SCM_ARG
%token SCM_FUNCTION
%token SCM_IDENTIFIER
%token SCM_TOKEN
%token STRING
%token SYMBOL_LIST
%token TONICNAME_PITCH

%left '-' '+'

/* We don't assign precedence to / and *, because we might need varied
prec levels in different prods */

%left UNARY_MINUS

%%

start_symbol:
	lilypond
	| EMBEDDED_LILY {
		parser->lexer_->push_note_state (Lily::pitchnames);
	} embedded_lilypond {
		parser->lexer_->pop_state ();
                *retval = $3;
 	}
	;

lilypond:	/* empty */ { $$ = SCM_UNSPECIFIED; }
	| lilypond toplevel_expression {
	}
	| lilypond assignment {
	}
	| lilypond error {
		parser->error_level_ = 1;
	}
	| lilypond INVALID	{
		parser->error_level_ = 1;
	}
	;


toplevel_expression:
	header_block {
		parser->lexer_->set_identifier (ly_symbol2scm ("$defaultheader"), $1);
	}
	| book_block {
		SCM proc = parser->lexer_->lookup_identifier ("toplevel-book-handler");
		scm_call_1 (proc, $1);
	}
	| bookpart_block {
		SCM proc = parser->lexer_->lookup_identifier ("toplevel-bookpart-handler");
		scm_call_1 (proc, $1);
	}
	| BOOK_IDENTIFIER {
		SCM proc = parser->lexer_->lookup_identifier
			(unsmob<Book>($1)->paper_
			 ? "toplevel-book-handler"
			 : "toplevel-bookpart-handler");
		scm_call_1 (proc, $1);
	}
	| score_block {
		SCM proc = parser->lexer_->lookup_identifier ("toplevel-score-handler");
		scm_call_1 (proc, $1);
	}
	| composite_music {
		SCM proc = parser->lexer_->lookup_identifier ("toplevel-music-handler");
		scm_call_1 (proc, $1);
	}
	| full_markup {
		SCM proc = parser->lexer_->lookup_identifier ("toplevel-text-handler");
		scm_call_1 (proc, scm_list_1 ($1));
	}
	| full_markup_list {
		SCM proc = parser->lexer_->lookup_identifier ("toplevel-text-handler");
		scm_call_1 (proc, $1);
	}
	| SCM_TOKEN {
		// Evaluate and ignore #xxx, as opposed to \xxx
		parser->lexer_->eval_scm_token ($1, @1);
	}
	| embedded_scm_active
	{
		SCM out = SCM_UNDEFINED;
		if (Text_interface::is_markup ($1))
			out = scm_list_1 ($1);
		else if (Text_interface::is_markup_list ($1))
			out = $1;
		if (scm_is_pair (out))
		{
			SCM proc = parser->lexer_->lookup_identifier ("toplevel-text-handler");
			scm_call_1 (proc, out);
		} else if (unsmob<Score> ($1))
		{
			SCM proc = parser->lexer_->lookup_identifier ("toplevel-score-handler");
			scm_call_1 (proc, $1);
		} else if (Output_def * od = unsmob<Output_def> ($1)) {
			SCM id = SCM_EOL;

			if (to_boolean (od->c_variable ("is-paper")))
				id = ly_symbol2scm ("$defaultpaper");
			else if (to_boolean (od->c_variable ("is-midi")))
				id = ly_symbol2scm ("$defaultmidi");
			else if (to_boolean (od->c_variable ("is-layout")))
				id = ly_symbol2scm ("$defaultlayout");

			parser->lexer_->set_identifier (id, $1);
		} else if (ly_is_module ($1))
		{
			SCM module = get_header (parser);
			ly_module_copy (module, $1);
			parser->lexer_->set_identifier
				(ly_symbol2scm ("$defaultheader"), module);
		} else if (!scm_is_eq ($1, SCM_UNSPECIFIED))
			parser->parser_error (@1, _("bad expression type"));
	}
	| output_def {
		SCM id = SCM_EOL;
		Output_def * od = unsmob<Output_def> ($1);

		if (to_boolean (od->c_variable ("is-paper")))
			id = ly_symbol2scm ("$defaultpaper");
		else if (to_boolean (od->c_variable ("is-midi")))
			id = ly_symbol2scm ("$defaultmidi");
		else if (to_boolean (od->c_variable ("is-layout")))
			id = ly_symbol2scm ("$defaultlayout");

		parser->lexer_->set_identifier (id, $1);
	}
	;

lookup:
	LOOKUP_IDENTIFIER
	| LOOKUP_IDENTIFIER '.' symbol_list_rev
	{
		$$ = loc_on_music (parser, @$,
				   nested_property ($1, scm_reverse_x ($3, SCM_EOL)));
	}
	;

embedded_scm_bare:
	SCM_TOKEN
	{
		$$ = parser->lexer_->eval_scm_token ($1, @1);
	}
	| SCM_IDENTIFIER
	;

embedded_scm_active:
	SCM_IDENTIFIER
	| scm_function_call
	| lookup
	;

embedded_scm_bare_arg:
	SCM_ARG
	| SCM_TOKEN
	{
		$$ = parser->lexer_->eval_scm_token ($1, @1);
	}
	| FRACTION
	| partial_markup
	| full_markup_list
	| context_modification
	| header_block
	| score_block
	| context_def_spec_block
	| book_block
	| bookpart_block
	| output_def
	| lookup
	;

/* The generic version may end in music, or not */

embedded_scm:
	embedded_scm_bare
	| scm_function_call
	| lookup
	;

/* embedded_scm_arg is _not_ casting pitches to music by default, this
 * has to be done by the function itself.  Note that this may cause
 * the results of scm_function_call or embedded_scm_bare_arg to be
 * turned into music from pitches as well.  Note that this creates a
 * distinctly awkward situation for calculated drum pitches.  Those
 * are at the current point of time rejected as music constituents as
 * they can't be distinguished from "proper" symbols.
 */

embedded_scm_arg:
	embedded_scm_bare_arg
	| scm_function_call
	| music_assign
	;

scm_function_call:
	SCM_FUNCTION function_arglist {
		$$ = MAKE_SYNTAX (music_function, @$,
				  $1, $2);
	}
	;

embedded_lilypond_number:
	'-' embedded_lilypond_number
	{
		$$ = scm_difference ($2, SCM_UNDEFINED);
	}
	| bare_number_common
	| UNSIGNED NUMBER_IDENTIFIER
	{
		$$ = scm_product ($1, $2);
	}
	;

embedded_lilypond:
	/* empty */
	{
		// FIXME: @$ does not contain a useful source location
		// for empty rules, and the only token in the whole
		// production, EMBEDDED_LILY, is synthetic and also
		// contains no source location.
		$$ = MAKE_SYNTAX (void_music, @$);
	}
	| identifier_init_nonumber
	| embedded_lilypond_number
	| post_event post_events
	{
		$$ = scm_reverse_x ($2, SCM_EOL);
		if (Music *m = unsmob<Music> ($1))
		{
			if (m->is_mus_type ("post-event-wrapper"))
				$$ = scm_append
					(scm_list_2 (m->get_property ("elements"),
						     $$));
			else
				$$ = scm_cons ($1, $$);
		}
		if (scm_is_pair ($$)
		    && scm_is_null (scm_cdr ($$)))
			$$ = scm_car ($$);
		else
		{
			Music * m = MY_MAKE_MUSIC ("PostEvents", @$);
			m->set_property ("elements", $$);
			$$ = m->unprotect ();
		}
	}
	| multiplied_duration
	| music_embedded music_embedded music_list {
		$3 = scm_reverse_x ($3, SCM_EOL);
		if (unsmob<Music> ($2))
			$3 = scm_cons ($2, $3);
		if (unsmob<Music> ($1))
			$3 = scm_cons ($1, $3);
		$$ = MAKE_SYNTAX (sequential_music, @$, $3);
	}
	| error {
		parser->error_level_ = 1;
                $$ = SCM_UNSPECIFIED;
	}
	| INVALID embedded_lilypond {
		parser->error_level_ = 1;
                $$ = $2;
	}
	;


lilypond_header_body:
	/* empty */ { $$ = SCM_UNSPECIFIED; }
	| lilypond_header_body assignment  {

	}
	| lilypond_header_body SCM_TOKEN {
		// Evaluate and ignore #xxx, as opposed to \xxx
		parser->lexer_->eval_scm_token ($2, @2);
	}
	| lilypond_header_body embedded_scm_active {
		if (ly_is_module ($2))
			ly_module_copy (scm_current_module (), $2);
		else if (!scm_is_eq ($2, SCM_UNSPECIFIED))
			parser->parser_error (@2, _("bad expression type"));
	}
	;

lilypond_header:
	HEADER '{' lilypond_header_body '}'	{
		$$ = parser->lexer_->remove_scope ();
	}
	;

header_block:
	{
		parser->lexer_->add_scope (get_header (parser));
	} lilypond_header {
		$$ = $2;
	}
	;

/*
	DECLARATIONS
*/
assignment_id:
	STRING		{ $$ = $1; }
	;

assignment:
	assignment_id '=' identifier_init  {
	        parser->lexer_->set_identifier ($1, $3);
                $$ = SCM_UNSPECIFIED;
	}
	| assignment_id '.' property_path '=' identifier_init {
		SCM path = scm_cons (scm_string_to_symbol ($1), $3);
		parser->lexer_->set_identifier (path, $5);
                $$ = SCM_UNSPECIFIED;
	}
	| assignment_id ',' property_path '=' identifier_init {
		SCM path = scm_cons (scm_string_to_symbol ($1), $3);
		parser->lexer_->set_identifier (path, $5);
                $$ = SCM_UNSPECIFIED;
	}
	;


identifier_init:
	identifier_init_nonumber
	| number_expression
	| symbol_list_part_bare '.' property_path
	{
		$$ = scm_reverse_x ($1, $3);
	}
	| symbol_list_part_bare ',' property_path
	{
		$$ = scm_reverse_x ($1, $3);
	}
	| post_event_nofinger post_events
	{
		$$ = scm_reverse_x ($2, SCM_EOL);
		if (Music *m = unsmob<Music> ($1))
		{
			if (m->is_mus_type ("post-event-wrapper"))
				$$ = scm_append
					(scm_list_2 (m->get_property ("elements"),
						     $$));
			else
				$$ = scm_cons ($1, $$);
		}
		if (scm_is_pair ($$)
		    && scm_is_null (scm_cdr ($$)))
			$$ = scm_car ($$);
		else
		{
			Music * m = MY_MAKE_MUSIC ("PostEvents", @$);
			m->set_property ("elements", $$);
			$$ = m->unprotect ();
		}
	}
	;

identifier_init_nonumber:
	header_block
	| score_block
	| book_block
	| bookpart_block
	| output_def
	| context_def_spec_block
	| music_assign
	| pitch_or_music
	| FRACTION
	| string
	| embedded_scm
	| partial_markup
	| full_markup_list
        | context_modification
	| partial_function ETC
	{
		$$ = MAKE_SYNTAX (partial_music_function, @$,
				  scm_reverse_x ($1, SCM_EOL));
	}
	;

// Partial functions
partial_function:
	MUSIC_FUNCTION function_arglist_partial
	{
		$$ = scm_acons ($1, $2, SCM_EOL);
	}
	| EVENT_FUNCTION function_arglist_partial
	{
		$$ = scm_acons ($1, $2, SCM_EOL);
	}
	| SCM_FUNCTION function_arglist_partial
	{
		$$ = scm_acons ($1, $2, SCM_EOL);
	}
	| OVERRIDE grob_prop_path '='
	{
		if (SCM_UNBNDP ($2))
			$$ = scm_list_1 (SCM_BOOL_F);
		else
			$$ = scm_cons
				(scm_list_3 (Syntax::property_override_function,
					     scm_cdr ($2), scm_car ($2)),
				 SCM_EOL);
	}
	| SET context_prop_spec '='
	{
		if (SCM_UNBNDP ($2))
			$$ = scm_list_1 (SCM_BOOL_F);
		else
			$$ = scm_cons
				(scm_list_3 (Syntax::property_set_function,
					     scm_cadr ($2), scm_car ($2)),
				 SCM_EOL);
	}
	| MUSIC_FUNCTION EXPECT_SCM function_arglist_optional partial_function
	{
		$$ = scm_acons ($1, $3, $4);
	}
	| EVENT_FUNCTION EXPECT_SCM function_arglist_optional partial_function
	{
		$$ = scm_acons ($1, $3, $4);
	}
	| SCM_FUNCTION EXPECT_SCM function_arglist_optional partial_function
	{
		$$ = scm_acons ($1, $3, $4);
	}
	| OVERRIDE grob_prop_path '=' partial_function
	{
		if (SCM_UNBNDP ($2))
			$$ = scm_list_1 (SCM_BOOL_F);
		else
			$$ = scm_cons
				(scm_list_3 (Syntax::property_override_function,
					     scm_cdr ($2), scm_car ($2)),
				 $4);
	}
	| SET context_prop_spec '=' partial_function
	{
		if (SCM_UNBNDP ($2))
			$$ = scm_list_1 (SCM_BOOL_F);
		else
			$$ = scm_cons
				(scm_list_3 (Syntax::property_set_function,
					     scm_cadr ($2), scm_car ($2)),
				 $4);
	}
	| MUSIC_FUNCTION EXPECT_OPTIONAL EXPECT_SCM function_arglist_nonbackup partial_function
	{
		$$ = scm_acons ($1, $4, $5);
	}
	| EVENT_FUNCTION EXPECT_OPTIONAL EXPECT_SCM function_arglist_nonbackup partial_function
	{
		$$ = scm_acons ($1, $4, $5);
	}
	| SCM_FUNCTION EXPECT_OPTIONAL EXPECT_SCM function_arglist_nonbackup partial_function
	{
		$$ = scm_acons ($1, $4, $5);
	}
	;

context_def_spec_block:
	CONTEXT '{' context_def_spec_body '}'
	{
		$$ = $3;
		Context_def *td = unsmob<Context_def> ($$);
		if (!td) {
			$$ = Context_def::make_scm ();
			td = unsmob<Context_def> ($$);
		}
		td->origin ()->set_spot (@$);
	}
	;

context_mod_arg:
	embedded_scm
	|
	{
		parser->lexer_->push_note_state (Lily::pitchnames);
	}
	composite_music
	{
		parser->lexer_->pop_state ();
		$$ = $2;
	}
	;


context_def_spec_body:
	/**/ {
		$$ = SCM_UNSPECIFIED;
	}
	| context_def_spec_body context_mod {
		if (!SCM_UNBNDP ($2)) {
			Context_def *td = unsmob<Context_def> ($$);
			if (!td) {
				$$ = Context_def::make_scm ();
				td = unsmob<Context_def> ($$);
			}
			unsmob<Context_def> ($$)->add_context_mod ($2);
		}
	}
	| context_def_spec_body context_modification {
                Context_def *td = unsmob<Context_def> ($$);
		if (!td) {
			$$ = Context_def::make_scm ();
			td = unsmob<Context_def> ($$);
		}
                SCM new_mods = unsmob<Context_mod> ($2)->get_mods ();
                for (SCM m = new_mods; scm_is_pair (m); m = scm_cdr (m)) {
                    td->add_context_mod (scm_car (m));
                }
	}
	| context_def_spec_body context_mod_arg {
		Context_def *td = unsmob<Context_def> ($1);
		if (scm_is_eq ($2, SCM_UNSPECIFIED))
			;
		else if (!td && unsmob<Context_def> ($2))
			$$ = $2;
		else {
			if (!td) {
				$$ = Context_def::make_scm ();
				td = unsmob<Context_def> ($$);
			}
			if (unsmob<Music> ($2)) {
				SCM proc = parser->lexer_->lookup_identifier ("context-mod-music-handler");
				$2 = scm_call_1 (proc, $2);
			}
			if (Context_mod *cm = unsmob<Context_mod> ($2)) {
				for (SCM m = cm->get_mods (); scm_is_pair (m); m = scm_cdr (m)) {
					td->add_context_mod (scm_car (m));
				}
			} else
				parser->parser_error (@2, _ ("not a context mod"));
		}
	}
	;



book_block:
	BOOK '{' book_body '}' 	{
		$$ = $3;
		unsmob<Book> ($$)->origin ()->set_spot (@$);
		pop_paper (parser);
		parser->lexer_->set_identifier (ly_symbol2scm ("$current-book"), SCM_BOOL_F);
	}
	;

/* FIXME:
   * Use 'handlers' like for toplevel-* stuff?
   * grok \layout and \midi?  */
book_body:
	{
		Book *book = new Book;
		init_papers (parser);
		book->paper_ = dynamic_cast<Output_def*> (unsmob<Output_def> (parser->lexer_->lookup_identifier ("$defaultpaper"))->clone ());
		book->paper_->unprotect ();
		push_paper (parser, book->paper_);
		book->header_ = get_header (parser);
                $$ = book->unprotect ();
		parser->lexer_->set_identifier (ly_symbol2scm ("$current-book"), $$);
	}
	| BOOK_IDENTIFIER {
		parser->lexer_->set_identifier (ly_symbol2scm ("$current-book"), $1);
	}
	| book_body paper_block {
		unsmob<Book> ($1)->paper_ = unsmob<Output_def> ($2);
		set_paper (parser, unsmob<Output_def> ($2));
	}
	| book_body bookpart_block {
		SCM proc = parser->lexer_->lookup_identifier ("book-bookpart-handler");
		scm_call_2 (proc, $1, $2);
	}
	| book_body score_block {
		SCM proc = parser->lexer_->lookup_identifier ("book-score-handler");
		scm_call_2 (proc, $1, $2);
	}
	| book_body composite_music {
		SCM proc = parser->lexer_->lookup_identifier ("book-music-handler");
		scm_call_2 (proc, $1, $2);
	}
	| book_body full_markup {
		SCM proc = parser->lexer_->lookup_identifier ("book-text-handler");
		scm_call_2 (proc, $1, scm_list_1 ($2));
	}
	| book_body full_markup_list {
		SCM proc = parser->lexer_->lookup_identifier ("book-text-handler");
		scm_call_2 (proc, $1, $2);
	}
	| book_body SCM_TOKEN {
		// Evaluate and ignore #xxx, as opposed to \xxx
		parser->lexer_->eval_scm_token ($2, @2);
	}
	| book_body embedded_scm_active
	{
		SCM out = SCM_UNDEFINED;
		if (Text_interface::is_markup ($2))
			out = scm_list_1 ($2);
		else if (Text_interface::is_markup_list ($2))
			out = $2;
		if (scm_is_pair (out))
		{
			SCM proc = parser->lexer_->lookup_identifier ("book-text-handler");
			scm_call_2 (proc, $1, out);
		} else if (unsmob<Score> ($2))
		{
			SCM proc = parser->lexer_->lookup_identifier ("book-score-handler");
			scm_call_2 (proc, $1, $2);
		} else if (Output_def *od = unsmob<Output_def> ($2)) {
			if (to_boolean (od->lookup_variable (ly_symbol2scm ("is-paper")))) {
				unsmob<Book> ($1)->paper_ = od;
				set_paper (parser, od);
			} else {
				parser->parser_error (@2, _ ("need \\paper for paper block"));
			}
		} else if (ly_is_module ($2))
		{
			ly_module_copy (unsmob<Book> ($1)->header_, $2);
		} else if (!scm_is_eq ($2, SCM_UNSPECIFIED))
			parser->parser_error (@2, _("bad expression type"));
	}
	| book_body
	{
		parser->lexer_->add_scope (unsmob<Book> ($1)->header_);
	} lilypond_header
	| book_body error {
                Book *book = unsmob<Book> ($1);
		book->paper_ = 0;
		book->scores_ = SCM_EOL;
		book->bookparts_ = SCM_EOL;
	}
	;

bookpart_block:
	BOOKPART '{' bookpart_body '}' {
		$$ = $3;
		unsmob<Book> ($$)->origin ()->set_spot (@$);
		parser->lexer_->set_identifier (ly_symbol2scm ("$current-bookpart"), SCM_BOOL_F);
	}
	;

bookpart_body:
	{
		Book *book = new Book;
                $$ = book->unprotect ();
		parser->lexer_->set_identifier (ly_symbol2scm ("$current-bookpart"), $$);
	}
	| BOOK_IDENTIFIER {
		parser->lexer_->set_identifier (ly_symbol2scm ("$current-bookpart"), $1);
	}
	| bookpart_body paper_block {
		unsmob<Book> ($$)->paper_ = unsmob<Output_def> ($2);
	}
	| bookpart_body score_block {
		SCM proc = parser->lexer_->lookup_identifier ("bookpart-score-handler");
		scm_call_2 (proc, $1, $2);
	}
	| bookpart_body composite_music {
		SCM proc = parser->lexer_->lookup_identifier ("bookpart-music-handler");
		scm_call_2 (proc, $1, $2);
	}
	| bookpart_body full_markup {
		SCM proc = parser->lexer_->lookup_identifier ("bookpart-text-handler");
		scm_call_2 (proc, $1, scm_list_1 ($2));
	}
	| bookpart_body full_markup_list {
		SCM proc = parser->lexer_->lookup_identifier ("bookpart-text-handler");
		scm_call_2 (proc, $1, $2);
	}
	| bookpart_body SCM_TOKEN {
		// Evaluate and ignore #xxx, as opposed to \xxx
		parser->lexer_->eval_scm_token ($2, @2);
	}
	| bookpart_body embedded_scm_active
	{
		SCM out = SCM_UNDEFINED;
		if (Text_interface::is_markup ($2))
			out = scm_list_1 ($2);
		else if (Text_interface::is_markup_list ($2))
			out = $2;
		if (scm_is_pair (out))
		{
			SCM proc = parser->lexer_->lookup_identifier ("bookpart-text-handler");
			scm_call_2 (proc, $1, out);
		} else if (unsmob<Score> ($2))
		{
			SCM proc = parser->lexer_->lookup_identifier ("bookpart-score-handler");
			scm_call_2 (proc, $1, $2);
		} else if (Output_def *od = unsmob<Output_def> ($2)) {
			if (to_boolean (od->lookup_variable (ly_symbol2scm ("is-paper")))) {
				unsmob<Book> ($1)->paper_ = od;
			} else {
				parser->parser_error (@2, _ ("need \\paper for paper block"));
			}
		} else if (ly_is_module ($2)) {
			Book *book = unsmob<Book> ($1);
			if (!ly_is_module (book->header_))
				book->header_ = ly_make_module (false);
			ly_module_copy (book->header_, $2);
		} else if (!scm_is_eq ($2, SCM_UNSPECIFIED))
			parser->parser_error (@2, _("bad expression type"));
	}
	| bookpart_body
	{
                Book *book = unsmob<Book> ($1);
		if (!ly_is_module (book->header_))
			book->header_ = ly_make_module (false);
		parser->lexer_->add_scope (book->header_);
	} lilypond_header
	| bookpart_body error {
                Book *book = unsmob<Book> ($1);
		book->paper_ = 0;
		book->scores_ = SCM_EOL;
	}
	;

score_block:
	SCORE '{' score_body '}' 	{
		unsmob<Score> ($3)->origin ()->set_spot (@$);
		$$ = $3;
	}
	;

score_body:
	score_items {
		if (!unsmob<Score> ($1)) {
			parser->parser_error (@1, _("Missing music in \\score"));
			$$ = (new Score)->unprotect ();
			if (scm_is_pair ($1) && ly_is_module (scm_car ($1)))
			{
				unsmob<Score> ($$)->set_header (scm_car ($1));
				$1 = scm_cdr ($1);
			}
			for (SCM p = scm_reverse_x ($1, SCM_EOL);
			     scm_is_pair (p); p = scm_cdr (p))
			{
				unsmob<Score> ($$)->
					add_output_def (unsmob<Output_def> (scm_car (p)));
			}
		}
	}
	| score_body error {
		unsmob<Score> ($$)->error_found_ = true;
	}
	;

score_item:
	embedded_scm
	| music
	| output_def
	;

score_items:
	/* empty */
	{
		$$ = SCM_EOL;
	}
	| score_items score_item
	{
		Output_def *od = unsmob<Output_def> ($2);
		if (od) {
			if (to_boolean (od->lookup_variable (ly_symbol2scm ("is-paper"))))
			{
				parser->parser_error (@2, _("\\paper cannot be used in \\score, use \\layout instead"));
				od = 0;
				$2 = SCM_UNSPECIFIED;
			}
		} else if (!unsmob<Score> ($$)) {
			if (unsmob<Music> ($2)) {
				$2 = Lily::scorify_music ($2);
			}
			if (unsmob<Score> ($2))
			{
				$$ = $2;
				$2 = SCM_UNSPECIFIED;
			}
		}
		Score *score = unsmob<Score> ($$);
		if (score && scm_is_pair ($1)) {
			if (ly_is_module (scm_car ($1)))
			{
				score->set_header (scm_car ($1));
				$1 = scm_cdr ($1);
			}
			for (SCM p = scm_reverse_x ($1, SCM_EOL);
			     scm_is_pair (p); p = scm_cdr (p))
			{
				score->add_output_def (unsmob<Output_def> (scm_car (p)));
			}
		}
		if (od) {
			if (score)
				score->add_output_def (od);
			else if (scm_is_pair ($$) && ly_is_module (scm_car ($$)))
				scm_set_cdr_x ($$, scm_cons ($2, scm_cdr ($$)));
			else
				$$ = scm_cons ($2, $$);
		} else if (ly_is_module ($2)) {
			SCM module = SCM_UNSPECIFIED;
			if (score) {
				module = score->get_header ();
				if (!ly_is_module (module))
				{
					module = ly_make_module (false);
					score->set_header (module);
				}
			} else if (scm_is_pair ($$) && ly_is_module (scm_car ($$)))
				module = scm_car ($$);
			else {
				module = ly_make_module (false);
				$$ = scm_cons (module, $$);
			}
			ly_module_copy (module, $2);
		} else if (!scm_is_eq ($2, SCM_UNSPECIFIED))
			parser->parser_error (@2, _("Spurious expression in \\score"));
	}
	| score_items
	{
		if (Score *score = unsmob<Score> ($1)) {
			if (!ly_is_module (score->get_header ()))
				score->set_header (ly_make_module (false));
			parser->lexer_->add_scope (score->get_header ());
		} else {
			if (!scm_is_pair ($1) || !ly_is_module (scm_car ($1)))
				$1 = scm_cons (ly_make_module (false), $1);
			parser->lexer_->add_scope (scm_car ($1));
		}
	} lilypond_header
	{
		$$ = $1;
	}
	;


/*
	OUTPUT DEF
*/

paper_block:
	output_def {
                Output_def *od = unsmob<Output_def> ($1);

		if (!to_boolean (od->lookup_variable (ly_symbol2scm ("is-paper"))))
		{
			parser->parser_error (@1, _ ("need \\paper for paper block"));
			$$ = get_paper (parser)->unprotect ();
		}
	}
	;


output_def:
	output_def_body '}' {
		if (scm_is_pair ($1))
			$$ = scm_car ($1);

		parser->lexer_->remove_scope ();
		parser->lexer_->pop_state ();
	}
	;

output_def_head:
	PAPER {
                Output_def *p = get_paper (parser);
		p->input_origin_ = @$;
		parser->lexer_->add_scope (p->scope_);
                $$ = p->unprotect ();
	}
	| MIDI    {
		Output_def *p = get_midi (parser);
		$$ = p->unprotect ();
		parser->lexer_->add_scope (p->scope_);
	}
	| LAYOUT 	{
		Output_def *p = get_layout (parser);

		parser->lexer_->add_scope (p->scope_);
		$$ = p->unprotect ();
	}
	;

output_def_head_with_mode_switch:
	output_def_head {
		parser->lexer_->push_initial_state ();
		$$ = $1;
	}
	;

// We need this weird nonterminal because both music as well as a
// context definition can start with \context and the difference is
// only apparent after looking at the next token.  If it is '{', there
// is still time to escape from notes mode.

music_or_context_def:
	music_assign
	| context_def_spec_block
	;

output_def_body:
	output_def_head_with_mode_switch '{' {
		unsmob<Output_def> ($1)->input_origin_.set_spot (@$);
		// This is a stupid trick to mark the beginning of the
		// body for deciding whether to allow
		// embedded_scm_active to have an output definition
		$$ = scm_list_1 ($1);
	}
	| output_def_body assignment  {
		if (scm_is_pair ($1))
			$$ = scm_car ($1);
	}
	| output_def_body embedded_scm_active
	{
		// We don't switch into note mode for Scheme functions
		// here.  Does not seem warranted/required in output
		// definitions.
		if (scm_is_pair ($1))
		{
			Output_def *o = unsmob<Output_def> ($2);
			if (o) {
				o->input_origin_.set_spot (@$);
				$1 = o->self_scm ();
				parser->lexer_->remove_scope ();
				parser->lexer_->add_scope (o->scope_);
				$2 = SCM_UNSPECIFIED;
			} else
				$1 = scm_car ($1);
		}
		if (unsmob<Context_def> ($2))
			assign_context_def (unsmob<Output_def> ($1), $2);
		// Seems unlikely, but let's be complete:
		else if (unsmob<Music> ($2))
		{
			SCM proc = parser->lexer_->lookup_identifier
				("output-def-music-handler");
			scm_call_2 (proc, $1, $2);
		} else if (!scm_is_eq ($2, SCM_UNSPECIFIED))
			parser->parser_error (@2, _("bad expression type"));
		$$ = $1;
	}
	| output_def_body SCM_TOKEN {
		if (scm_is_pair ($1))
			$$ = scm_car ($1);
		// Evaluate and ignore #xxx, as opposed to \xxx
		parser->lexer_->eval_scm_token ($2, @2);
	}
	| output_def_body
	{
		if (scm_is_pair ($1))
			$1 = scm_car ($1);
		parser->lexer_->push_note_state (Lily::pitchnames);
	} music_or_context_def
	{
		parser->lexer_->pop_state ();
		if (unsmob<Context_def> ($3))
			assign_context_def (unsmob<Output_def> ($1), $3);
		else {

			SCM proc = parser->lexer_->lookup_identifier
				     ("output-def-music-handler");
			scm_call_2 (proc, $1, $3);
		}
		$$ = $1;
	}
	| output_def_body error {

	}
	;

tempo_event:
	TEMPO steno_duration '=' tempo_range	{
		$$ = MAKE_SYNTAX (tempo, @$, SCM_EOL, $2, $4);
	}
	| TEMPO text steno_duration '=' tempo_range	{
		$$ = MAKE_SYNTAX (tempo, @$, $2, $3, $5);
	}
	| TEMPO text {
		$$ = MAKE_SYNTAX (tempo, @$, $2);
	} %prec ':'
	;

/*
The representation of a  list is reversed to have efficient append.  */

music_list:
	/* empty */ {
		$$ = SCM_EOL;
	}
	| music_list music_embedded {
		if (unsmob<Music> ($2))
			$$ = scm_cons ($2, $1);
	}
	| music_list error {
		Music *m = MY_MAKE_MUSIC("Music", @$);
		// ugh. code dup
		m->set_property ("error-found", SCM_BOOL_T);
		$$ = scm_cons (m->self_scm (), $1);
		m->unprotect (); /* UGH */
	}
	;

braced_music_list:
	'{' music_list '}'
	{
		$$ = scm_reverse_x ($2, SCM_EOL);
	}
	;

music:	music_assign
	| lyric_element_music
	| pitch_as_music
	;

pitch_as_music:
	pitch_or_music
	{
	        $$ = make_music_from_simple (parser, @1, $1);
                if (!unsmob<Music> ($$))
		{
                        parser->parser_error (@1, _ ("music expected"));
			$$ = MAKE_SYNTAX (void_music, @$);
		}
	}
	;

music_embedded:
	music
	{
		if (unsmob<Music> ($1)->is_mus_type ("post-event")) {
			parser->parser_error (@1, _ ("unexpected post-event"));
			$$ = SCM_UNSPECIFIED;
		}
	}
	| music_embedded_backup
	{
		$$ = $1;
	}
	| music_embedded_backup BACKUP lyric_element_music
	{
		$$ = $3;
	}
	| multiplied_duration post_events
	{
		Music *n = MY_MAKE_MUSIC ("NoteEvent", @$);

		parser->default_duration_ = *unsmob<Duration> ($1);
		n->set_property ("duration", $1);

		if (scm_is_pair ($2))
			n->set_property ("articulations",
					 scm_reverse_x ($2, SCM_EOL));
		$$ = n->unprotect ();
	}
	;

music_embedded_backup:
	embedded_scm
	{
		if (scm_is_eq ($1, SCM_UNSPECIFIED))
			$$ = $1;
		else if (Music *m = unsmob<Music> ($1)) {
			if (m->is_mus_type ("post-event")) {
				parser->parser_error
					(@1, _ ("unexpected post-event"));
				$$ = SCM_UNSPECIFIED;
			} else
				$$ = $1;
		} else if (parser->lexer_->is_lyric_state ()
			   && Text_interface::is_markup ($1))
			MYBACKUP (LYRIC_ELEMENT, $1, @1);
		else {
			@$.warning (_ ("Ignoring non-music expression"));
			$$ = $1;
		}
	}
	;

// music_assign does not need to contain lyrics: there are no
// assignments in lyricmode.
music_assign:
	simple_music
	| composite_music %prec COMPOSITE
	;

repeated_music:
	REPEAT simple_string unsigned_number music
	{
		$$ = MAKE_SYNTAX (repeat, @$, $2, $3, $4, SCM_EOL);
	}
	| REPEAT simple_string unsigned_number music ALTERNATIVE braced_music_list
	{
		$$ = MAKE_SYNTAX (repeat, @$, $2, $3, $4, $6);
	}
	;

sequential_music:
	SEQUENTIAL braced_music_list {
		$$ = MAKE_SYNTAX (sequential_music, @$, $2);
	}
	| braced_music_list {
		$$ = MAKE_SYNTAX (sequential_music, @$, $1);
	}
	;

simultaneous_music:
	SIMULTANEOUS braced_music_list {
		$$ = MAKE_SYNTAX (simultaneous_music, @$, $2);
	}
	| DOUBLE_ANGLE_OPEN music_list DOUBLE_ANGLE_CLOSE	{
		$$ = MAKE_SYNTAX (simultaneous_music, @$, scm_reverse_x ($2, SCM_EOL));
	}
	;

simple_music:
	event_chord
	| music_property_def
	| context_change
	;

context_modification:
        WITH
	{
		parser->lexer_->push_note_state (Lily::pitchnames);
	} '{' context_mod_list '}'
        {
                parser->lexer_->pop_state ();
                $$ = $4;
        }
	| WITH context_modification_arg
	{
		if (unsmob<Music> ($2)) {
			SCM proc = parser->lexer_->lookup_identifier ("context-mod-music-handler");
			$2 = scm_call_1 (proc, $2);
		}
		if (unsmob<Context_mod> ($2))
			$$ = $2;
		else {
			// let's permit \with #*unspecified* to go for
			// an empty context mod
			if (!scm_is_eq ($2, SCM_UNSPECIFIED))
				parser->parser_error (@2, _ ("not a context mod"));
			$$ = Context_mod ().smobbed_copy ();
		}
	}
        ;

context_modification_arg:
	embedded_scm
	| MUSIC_IDENTIFIER
	;

/* A list of single mods collected from a (possibly empty) sequence of
 * context modifications, usually written as \with ... \with ...
 */

optional_context_mods:
	context_modification_mods_list
        {
		if (scm_is_pair ($1))
			$$ = scm_append_x (scm_reverse_x ($1, SCM_EOL));
        }
        ;

/* The worker for optional_context_mods conses a (reversed) list where
 * each element contains the list of single context mods from one
 * context modification block.  Context_mod::get_mods creates fresh
 * copies, so it's okay to use append! on them.
 */

context_modification_mods_list:
	/**/ {
		$$ = SCM_EOL;
	}
	| context_modification_mods_list context_modification
	{
		if (Context_mod *m = unsmob<Context_mod> ($2))
			$$ = scm_cons (m->get_mods (), $1);
	}
	;

/* A Context_mod is a container for a list of context mods like
 * \consists ...  \override ... .  context_mod_list produces a
 * Context_mod from the inside of a \with { ... } statement.
 */

context_mod_list:
        /**/ {
            $$ = Context_mod ().smobbed_copy ();
        }
        | context_mod_list context_mod  {
		if (!SCM_UNBNDP ($2))
			unsmob<Context_mod> ($1)->add_context_mod ($2);
        }
	| context_mod_list context_mod_arg {
		if (unsmob<Music> ($2)) {
			SCM proc = parser->lexer_->lookup_identifier ("context-mod-music-handler");
			$2 = scm_call_1 (proc, $2);
		}
		if (unsmob<Context_mod> ($2))
			unsmob<Context_mod> ($$)->add_context_mods
				(unsmob<Context_mod> ($2)->get_mods ());
		else if (!scm_is_eq ($2, SCM_UNSPECIFIED))
			parser->parser_error (@2, _ ("not a context mod"));
        }
        ;

context_prefix:
	CONTEXT symbol optional_id optional_context_mods {
		$$ = START_MAKE_SYNTAX (context_specification, $2, $3, $4, SCM_BOOL_F);
	}
	| NEWCONTEXT symbol optional_id optional_context_mods {
		$$ = START_MAKE_SYNTAX (context_specification, $2, $3, $4, SCM_BOOL_T);
	}
	;

new_lyrics:
	ADDLYRICS optional_context_mods lyric_mode_music {
		$$ = scm_acons ($3, $2, SCM_EOL);
	}
	| new_lyrics ADDLYRICS optional_context_mods lyric_mode_music {
		$$ = scm_acons ($4, $3, $1);
	}
	;

/* basic_music is basically the same as composite_music but with
 * context-prefixed music and lyricized music explicitly removed.  The
 * reason is that in a sequence
 *
 *   \new Staff \new Voice { ... } \addlyrics { ... } \addlyrics { ... }
 *
 * we need to group both \addlyrics together (as they go with the same
 * voice) but then combine them with \new Voice { ... }, meaning that
 * combining \new Voice { ... } needs higher priority than
 * { ... } \addlyrics, and *not* have \new Staff \new Voice { ... }
 * combine before combining \new Voice { ... } \addlyrics: only one
 * layer of context-prefixed music should assemble before \addlyrics
 * is integrated.  Total mess, and we sort this mess out with explicit
 * rules preferring a single context-prefix.
 */

basic_music:
	music_function_call
	| repeated_music
	| music_bare
	| LYRICSTO simple_string lyric_mode_music {
		$$ = MAKE_SYNTAX (lyric_combine, @$, $2, SCM_EOL, $3);
	}
	| LYRICSTO symbol '=' simple_string lyric_mode_music
	{
		$$ = MAKE_SYNTAX (lyric_combine, @$, $3, $2, $4);
	}
	;

contextable_music:
	basic_music
	| pitch_as_music
	| event_chord
	;

contexted_basic_music:
	context_prefix contextable_music new_lyrics
	{
		Input i;
		i.set_location (@1, @2);
		$$ = FINISH_MAKE_SYNTAX ($1, i, $2);
		$$ = MAKE_SYNTAX (add_lyrics, @$, $$, scm_reverse_x ($3, SCM_EOL));
	} %prec COMPOSITE
	| context_prefix contextable_music
	{
		$$ = FINISH_MAKE_SYNTAX ($1, @$, $2);
	} %prec COMPOSITE
	| context_prefix contexted_basic_music
	{
		$$ = FINISH_MAKE_SYNTAX ($1, @$, $2);
	}
	;

composite_music:
	basic_music %prec COMPOSITE
	| contexted_basic_music
	| basic_music new_lyrics
	{
		$$ = MAKE_SYNTAX (add_lyrics, @$, $1, scm_reverse_x ($2, SCM_EOL));
	} %prec COMPOSITE
	;

music_bare:
	mode_changed_music
	| MUSIC_IDENTIFIER
	| grouped_music_list
	;

grouped_music_list:
	simultaneous_music		{ $$ = $1; }
	| sequential_music		{ $$ = $1; }
	;

/* Function argument lists are arguably the most complex part in the
 * parser.  They are pretty tricky to understand because of the way
 * they are processed, and because of optional arguments that can be
 * omitted.  When there are several optional arguments in a row,
 * omitting one automatically omits all following arguments.  Optional
 * arguments can only be omitted when either
 *
 * a) the omission is explicitly started with \default
 * b) the omission is implicitly started by an argument not matching
 *    its predicate, and there is a mandatory argument later that can
 *    "catch" the argument that does not fit.
 *
 * When argument parsing starts, the lexer pushes EXPECT_SCM tokens
 * (corresponding to mandatory arguments and having a predicate
 * function as semantic value) or EXPECT_OPTIONAL EXPECT_SCM (where
 * the semantic value of the EXPECT_OPTIONAL token is the default to
 * use when the optional argument is omitted, and EXPECT_SCM again has
 * the argument predicate as semantic value) in reverse order to the
 * parser, followed by EXPECT_NO_MORE_ARGS.  The argument list is then
 * processed inside-out while actual tokens are consumed.
 *
 * This means that the argument list tokens determine the actions
 * taken as they arrive.  The structure of the argument list is known
 * to the parser and stored in its parse stack when the first argument
 * is being parsed.  What the parser does not know is which predicates
 * will match and whether or not \default will be appearing in the
 * argument list, and where.
 *
 * Sequences of 0 or more optional arguments are scanned using either
 * function_arglist_backup or function_arglist_nonbackup.  The first
 * is used when optional arguments are followed by at least one
 * mandatory argument: in that case optional arguments may be skipped
 * by either a false predicate (in which case the expression will be
 * pushed back as one or more tokens, preceded by a BACKUP token) or
 * by using \default.
 *
 * If optional arguments are at the end of the argument list, they are
 * instead scanned using function_arglist_nonbackup: here the only
 * manner to enter into skipping of optional arguments is the use of
 * \default.
 *
 * The argument list of a normal function call is parsed using
 * function_arglist.  The part of an argument list before a mandatory
 * argument is parsed using function_arglist_optional.
 *
 * The difference is that leading optional arguments are scanned using
 * function_arglist_nonbackup and function_arglist_backup,
 * respectively.
 *
 * Most other details are obvious in the rules themselves.
 *
 */

symbol_list_arg:
	SYMBOL_LIST
	| SYMBOL_LIST '.' symbol_list_rev
	{
		$$ = scm_append (scm_list_2 ($1, scm_reverse_x ($3, SCM_EOL)));
	}
	| SYMBOL_LIST ',' symbol_list_rev
	{
		$$ = scm_append (scm_list_2 ($1, scm_reverse_x ($3, SCM_EOL)));
	}
	;

symbol_list_rev:
	symbol_list_part
	| symbol_list_rev '.' symbol_list_part
	{
		$$ = scm_append_x (scm_list_2 ($3, $1));
	}
	| symbol_list_rev ',' symbol_list_part
	{
		$$ = scm_append_x (scm_list_2 ($3, $1));
	}
	;

// symbol_list_part delivers elements in reverse copy.

symbol_list_part:
	symbol_list_element
	{
		$$ = try_string_variants (Lily::key_list_p, $1);
		if (SCM_UNBNDP ($$)) {
			parser->parser_error (@1, _("not a key"));
			$$ = SCM_EOL;
		} else
			$$ = scm_reverse ($$);
	}
	;


symbol_list_element:
	STRING
	| embedded_scm_bare
	| UNSIGNED
	;

symbol_list_part_bare:
	STRING
	{
		$$ = try_string_variants (Lily::key_list_p, $1);
		if (SCM_UNBNDP ($$)) {
			parser->parser_error (@1, _("not a key"));
			$$ = SCM_EOL;
		} else
			$$ = scm_reverse ($$);
	}
	| UNSIGNED
	{
		$$ = scm_list_1 ($1);
	}
	;

function_arglist_nonbackup:
	function_arglist_common
	| EXPECT_OPTIONAL EXPECT_SCM function_arglist_nonbackup post_event_nofinger
	{
		$$ = check_scheme_arg (parser, @4, $4, $3, $2);
	}
	| EXPECT_OPTIONAL EXPECT_SCM function_arglist_nonbackup '-' UNSIGNED
	{
		SCM n = scm_difference ($5, SCM_UNDEFINED);
		if (scm_is_true (scm_call_1 ($2, n)))
			$$ = scm_cons (n, $3);
		else {
			Music *t = MY_MAKE_MUSIC ("FingeringEvent", @5);
			t->set_property ("digit", $5);
			$$ = check_scheme_arg (parser, @4, t->unprotect (),
					       $3, $2, n);
		}
	}
	| EXPECT_OPTIONAL EXPECT_SCM function_arglist_nonbackup '-' REAL
	{
		$$ = check_scheme_arg (parser, @4,
				       scm_difference ($5, SCM_UNDEFINED),
				       $3, $2);
	}
	| EXPECT_OPTIONAL EXPECT_SCM function_arglist_nonbackup '-' NUMBER_IDENTIFIER
	{
		$$ = check_scheme_arg (parser, @4,
				       scm_difference ($5, SCM_UNDEFINED),
				       $3, $2);
	}
	| EXPECT_OPTIONAL EXPECT_SCM function_arglist_nonbackup embedded_scm_arg
	{
		if (scm_is_true (scm_call_1 ($2, $4)))
			$$ = scm_cons ($4, $3);
		else
			$$ = check_scheme_arg (parser, @4,
					       make_music_from_simple
					       (parser, @4, $4),
					       $3, $2);
	}
	| EXPECT_OPTIONAL EXPECT_SCM function_arglist_nonbackup bare_number_common
	{
		$$ = check_scheme_arg (parser, @4, $4, $3, $2);
	}
	| function_arglist_nonbackup_reparse REPARSE pitch_or_music
	{
		if (scm_is_true (scm_call_1 ($2, $3)))
			$$ = scm_cons ($3, $1);
		else
			$$ = check_scheme_arg (parser, @3,
					       make_music_from_simple
					       (parser, @3, $3),
					       $1, $2);
	}
	| function_arglist_nonbackup_reparse REPARSE multiplied_duration
	{
		$$ = check_scheme_arg (parser, @3, $3, $1, $2);
	}
	| function_arglist_nonbackup_reparse REPARSE reparsed_rhythm
	{
		$$ = check_scheme_arg (parser, @3, $3, $1, $2);
	}
	| function_arglist_nonbackup_reparse REPARSE bare_number_common
	{
		$$ = check_scheme_arg (parser, @3, $3, $1, $2);
	}
	| function_arglist_nonbackup_reparse REPARSE SCM_ARG
	{
		$$ = check_scheme_arg (parser, @3, $3, $1, $2);
	}
	| function_arglist_nonbackup_reparse REPARSE lyric_element_music
	{
		$$ = check_scheme_arg (parser, @3, $3, $1, $2);
	}
	| function_arglist_nonbackup_reparse REPARSE symbol_list_arg
	{
		$$ = check_scheme_arg (parser, @3, $3, $1, $2);
	}
	;


reparsed_rhythm:
	DURATION_ARG dots multipliers post_events
	{
		$$ = make_music_from_simple (parser, @$,
					     make_duration ($1, scm_to_int ($2), $3));
		Music *m = unsmob<Music> ($$);
		assert (m);
		if (scm_is_pair ($4))
			m->set_property ("articulations",
					 scm_reverse_x ($4, SCM_EOL));
	} %prec ':'
	;

function_arglist_nonbackup_reparse:
	EXPECT_OPTIONAL EXPECT_SCM function_arglist_nonbackup SCM_IDENTIFIER
	{
		$$ = $3;
		SCM res = try_string_variants ($2, $4);
		if (!SCM_UNBNDP (res))
			if (scm_is_pair (res))
				MYREPARSE (@4, $2, SYMBOL_LIST, res);
			else
				MYREPARSE (@4, $2, SCM_ARG, res);
		else if (scm_is_true
			 (scm_call_1
			  ($2, make_music_from_simple
			   (parser, @4, $4))))
			MYREPARSE (@4, $2, STRING, $4);
		else
			MYREPARSE (@4, $2, SCM_ARG, $4);
	}
	| EXPECT_OPTIONAL EXPECT_SCM function_arglist_nonbackup pitch
	{
		$$ = $3;
		if (scm_is_true
		    (scm_call_1
		     ($2, make_music_from_simple
		      (parser, @4, $4))))
			MYREPARSE (@4, $2, PITCH_IDENTIFIER, $4);
		else
			MYREPARSE (@4, $2, SCM_ARG, $4);
	}
	| EXPECT_OPTIONAL EXPECT_SCM function_arglist_nonbackup steno_tonic_pitch
	{
		$$ = $3;
		if (scm_is_true
		    (scm_call_1
		     ($2, make_music_from_simple
		      (parser, @4, $4))))
			MYREPARSE (@4, $2, TONICNAME_PITCH, $4);
		else
			MYREPARSE (@4, $2, SCM_ARG, $4);
	}
	| EXPECT_OPTIONAL EXPECT_SCM function_arglist_nonbackup STRING
	{
		$$ = $3;
		SCM res = try_string_variants ($2, $4);
		if (!SCM_UNBNDP (res))
			if (scm_is_pair (res))
				MYREPARSE (@4, $2, SYMBOL_LIST, res);
			else
				MYREPARSE (@4, $2, SCM_ARG, res);
		else if (scm_is_true
			 (scm_call_1
			  ($2, make_music_from_simple
			   (parser, @4, $4))))
			MYREPARSE (@4, $2, STRING, $4);
		else
			MYREPARSE (@4, $2, SCM_ARG, $4);
	}
	| EXPECT_OPTIONAL EXPECT_SCM function_arglist_nonbackup full_markup
	{
		$$ = $3;
		if (scm_is_true (scm_call_1 ($2, $4)))
			MYREPARSE (@4, $2, SCM_ARG, $4);
		else if (scm_is_true
			 (scm_call_1
			  ($2, make_music_from_simple
			   (parser, @4, $4))))
			MYREPARSE (@4, $2, STRING, $4);
		else
			MYREPARSE (@4, $2, SCM_ARG, $4);
	}
	| EXPECT_OPTIONAL EXPECT_SCM function_arglist_nonbackup UNSIGNED
	{
		$$ = $3;
		if (scm_is_true (scm_call_1 ($2, $4)))
			// May be 3 \cm or similar
			MYREPARSE (@4, $2, REAL, $4);
		else if (scm_is_true (scm_call_1 ($2, scm_list_1 ($4))))
			MYREPARSE (@4, $2, SYMBOL_LIST, scm_list_1 ($4));
		else {
			SCM d = make_duration ($4);
			if (!SCM_UNBNDP (d)) {
				if (scm_is_true (scm_call_1 ($2, d)))
					MYREPARSE (@4, $2, DURATION_IDENTIFIER, d);
				else if (scm_is_true
					 (scm_call_1
					  ($2, make_music_from_simple (parser, @4, d))))
					MYREPARSE (@4, $2, DURATION_ARG, d);
				else
					MYREPARSE (@4, $2, SCM_ARG, $4); // trigger error
			} else
				MYREPARSE (@4, $2, SCM_ARG, $4); // trigger error
		}
	}
	| EXPECT_OPTIONAL EXPECT_SCM function_arglist_nonbackup DURATION_IDENTIFIER
	{
		$$ = $3;
		if (scm_is_true (scm_call_1 ($2, $4)))
			MYREPARSE (@4, $2, DURATION_IDENTIFIER, $4);
		else if (scm_is_true
			 (scm_call_1
			  ($2, make_music_from_simple (parser, @4, $4))))
			MYREPARSE (@4, $2, DURATION_ARG, $4);
		else
			MYREPARSE (@4, $2, SCM_ARG, $4); // trigger error
	}
	;


// function_arglist_backup can't occur at the end of an argument
// list.
function_arglist_backup:
	function_arglist_common
	| EXPECT_OPTIONAL EXPECT_SCM function_arglist_backup embedded_scm_arg
	{
		if (scm_is_true (scm_call_1 ($2, $4)))
			$$ = scm_cons ($4, $3);
		else {
			$$ = make_music_from_simple (parser, @4, $4);
			if (scm_is_true (scm_call_1 ($2, $$)))
				$$ = scm_cons ($$, $3);
			else
			{
				$$ = scm_cons (loc_on_music (parser, @3, $1), $3);
				MYBACKUP (SCM_ARG, $4, @4);
			}
		}
	}
	| EXPECT_OPTIONAL EXPECT_SCM function_arglist_backup post_event_nofinger
	{
		if (scm_is_true (scm_call_1 ($2, $4)))
		{
			$$ = scm_cons ($4, $3);
		} else {
			$$ = scm_cons (loc_on_music (parser, @3, $1), $3);
			MYBACKUP (EVENT_IDENTIFIER, $4, @4);
		}
	}
	| EXPECT_OPTIONAL EXPECT_SCM function_arglist_backup pitch
	{
		if (scm_is_true
		    (scm_call_1
		     ($2, make_music_from_simple
		      (parser, @4, $4))))
		{
			$$ = $3;
			MYREPARSE (@4, $2, PITCH_IDENTIFIER, $4);
		} else if (scm_is_true (scm_call_1 ($2, $4)))
			$$ = scm_cons ($4, $3);
		else {
			$$ = scm_cons (loc_on_music (parser, @3, $1), $3);
			MYBACKUP (PITCH_IDENTIFIER, $4, @4);
		}
	}
	| EXPECT_OPTIONAL EXPECT_SCM function_arglist_backup steno_tonic_pitch
	{
		if (scm_is_true
		    (scm_call_1
		     ($2, make_music_from_simple
		      (parser, @4, $4))))
		{
			$$ = $3;
			MYREPARSE (@4, $2, TONICNAME_PITCH, $4);
		} else if (scm_is_true (scm_call_1 ($2, $4)))
			$$ = scm_cons ($4, $3);
		else {
			$$ = scm_cons (loc_on_music (parser, @3, $1), $3);
			MYBACKUP (TONICNAME_PITCH, $4, @4);
		}
	}
	| EXPECT_OPTIONAL EXPECT_SCM function_arglist_backup full_markup
	{
		if (scm_is_true (scm_call_1 ($2, $4)))
			$$ = scm_cons ($4, $3);
		else {
			$$ = scm_cons (loc_on_music (parser, @3, $1), $3);
			MYBACKUP (SCM_IDENTIFIER, $4, @4);
		}
	}
	| EXPECT_OPTIONAL EXPECT_SCM function_arglist_backup UNSIGNED
	{
		$$ = $3;
		if (scm_is_true (scm_call_1 ($2, $4)))
			// May be 3 \cm or similar
			MYREPARSE (@4, $2, REAL, $4);
		else if (scm_is_true (scm_call_1 ($2, scm_list_1 ($4))))
			MYREPARSE (@4, $2, SYMBOL_LIST, scm_list_1 ($4));
		else {
			SCM d = make_duration ($4);
			if (!SCM_UNBNDP (d)) {
				if (scm_is_true (scm_call_1 ($2, d)))
					MYREPARSE (@4, $2, DURATION_IDENTIFIER, d);
				else if (scm_is_true
					 (scm_call_1
					  ($2, make_music_from_simple (parser, @4, d))))
					MYREPARSE (@4, $2, DURATION_ARG, d);
				else {
					$$ = scm_cons (loc_on_music (parser, @3, $1), $3);
					MYBACKUP (UNSIGNED, $4, @4);
				}
			} else {
				$$ = scm_cons (loc_on_music (parser, @3, $1), $3);
				MYBACKUP (UNSIGNED, $4, @4);
			}
		}
	}
	| EXPECT_OPTIONAL EXPECT_SCM function_arglist_backup REAL
	{
		if (scm_is_true (scm_call_1 ($2, $4)))
		{
			$$ = $3;
			MYREPARSE (@4, $2, REAL, $4);
		} else {
			$$ = scm_cons (loc_on_music (parser, @3, $1), $3);
			MYBACKUP (REAL, $4, @4);
		}
	}
	| EXPECT_OPTIONAL EXPECT_SCM function_arglist_backup NUMBER_IDENTIFIER
	{
		if (scm_is_true (scm_call_1 ($2, $4)))
		{
			$$ = scm_cons ($4, $3);
		} else {
			$$ = scm_cons (loc_on_music (parser, @3, $1), $3);
			MYBACKUP (NUMBER_IDENTIFIER, $4, @4);
		}
	}
	| EXPECT_OPTIONAL EXPECT_SCM function_arglist_backup '-' UNSIGNED
	{
		SCM n = scm_difference ($5, SCM_UNDEFINED);
		if (scm_is_true (scm_call_1 ($2, n))) {
			$$ = $3;
			MYREPARSE (@5, $2, REAL, n);
		} else {
			Music *t = MY_MAKE_MUSIC ("FingeringEvent", @5);
			t->set_property ("digit", $5);
			$$ = t->unprotect ();
			if (scm_is_true (scm_call_1 ($2, $$)))
				$$ = scm_cons ($$, $3);
			else {
				$$ = scm_cons (loc_on_music (parser, @3, $1), $3);
				MYBACKUP (UNSIGNED, $5, @5);
				parser->lexer_->push_extra_token (@4, '-');
			}
		}
	}
	| EXPECT_OPTIONAL EXPECT_SCM function_arglist_backup '-' REAL
	{
		SCM n = scm_difference ($5, SCM_UNDEFINED);
		if (scm_is_true (scm_call_1 ($2, n))) {
			MYREPARSE (@5, $2, REAL, n);
			$$ = $3;
		} else {
			$$ = scm_cons (loc_on_music (parser, @3, $1), $3);
			MYBACKUP (REAL, n, @5);
		}
	}
	| EXPECT_OPTIONAL EXPECT_SCM function_arglist_backup '-' NUMBER_IDENTIFIER
	{
		SCM n = scm_difference ($5, SCM_UNDEFINED);
		if (scm_is_true (scm_call_1 ($2, n))) {
			$$ = scm_cons (n, $3);
		} else {
			$$ = scm_cons (loc_on_music (parser, @3, $1), $3);
			MYBACKUP (NUMBER_IDENTIFIER, n, @5);
		}
	}
	| EXPECT_OPTIONAL EXPECT_SCM function_arglist_backup DURATION_IDENTIFIER
	{
		$$ = $3;
		if (scm_is_true (scm_call_1 ($2, $4)))
			MYREPARSE (@4, $2, DURATION_IDENTIFIER, $4);
		else if (scm_is_true
			 (scm_call_1
			  ($2, make_music_from_simple (parser, @4, $4))))
			MYREPARSE (@4, $2, DURATION_ARG, $4);
		else {
			$$ = scm_cons (loc_on_music (parser, @3, $1), $3);
			MYBACKUP (DURATION_IDENTIFIER, $4, @4);
		}
	}
	| EXPECT_OPTIONAL EXPECT_SCM function_arglist_backup SCM_IDENTIFIER
	{
		SCM res = try_string_variants ($2, $4);
		if (!SCM_UNBNDP (res))
			if (scm_is_pair (res)) {
				$$ = $3;
				MYREPARSE (@4, $2, SYMBOL_LIST, res);
			}
			else
				$$ = scm_cons (res, $3);
		else {
			$$ = scm_cons (loc_on_music (parser, @3, $1), $3);
			MYBACKUP (SCM_IDENTIFIER, $4, @4);
		}
	}
	| EXPECT_OPTIONAL EXPECT_SCM function_arglist_backup STRING
	{
		SCM res = try_string_variants ($2, $4);
		if (!SCM_UNBNDP (res))
			if (scm_is_pair (res)) {
				$$ = $3;
				MYREPARSE (@4, $2, SYMBOL_LIST, res);
			}
			else
				$$ = scm_cons (res, $3);
		else {
			$$ = scm_cons (loc_on_music (parser, @3, $1), $3);
			MYBACKUP (STRING, $4, @4);
		}
	}
	| function_arglist_backup REPARSE pitch_or_music
	{
		if (scm_is_true (scm_call_1 ($2, $3)))
			$$ = scm_cons ($3, $1);
		else
			$$ = check_scheme_arg (parser, @3,
					       make_music_from_simple
					       (parser, @3, $3),
					       $1, $2);
	}
	| function_arglist_backup REPARSE bare_number_common
	{
		$$ = check_scheme_arg (parser, @3,
				       $3, $1, $2);
	}
	| function_arglist_backup REPARSE multiplied_duration
	{
		$$ = check_scheme_arg (parser, @3,
				       $3, $1, $2);
	}
	| function_arglist_backup REPARSE reparsed_rhythm
	{
		$$ = check_scheme_arg (parser, @3,
				       $3, $1, $2);
	}
	| function_arglist_backup REPARSE symbol_list_arg
	{
		$$ = check_scheme_arg (parser, @3, $3, $1, $2);
	}
	;

function_arglist:
	function_arglist_nonbackup
	| EXPECT_OPTIONAL EXPECT_SCM function_arglist_skip_nonbackup DEFAULT
	{
		$$ = scm_cons (loc_on_music (parser, @4, $1), $3);
	}
	;

function_arglist_skip_nonbackup:
	function_arglist_nonbackup
	| EXPECT_OPTIONAL EXPECT_SCM function_arglist_skip_nonbackup
	{
		$$ = scm_cons (loc_on_music (parser, @3, $1), $3);
	}
	;

// Partial function arglists are returned just in their incomplete
// state: when combined with the music function, the missing parts of
// the signature can be reconstructed
//
// To serve as a partial arglist, the argument list must absolutely
// _not_ be in "skipping optional arguments" mode since then there is
// some backup token that has nowhere to go before \etc.
//
// So we can skim off an arbitrary number of arguments from the end of
// the argument list.  The argument list remaining afterwards has to
// be in not-skipping-optional-arguments mode.

function_arglist_partial:
	EXPECT_SCM function_arglist_optional
	{
		$$ = $2;
	}
	| EXPECT_SCM function_arglist_partial_optional
	{
		$$ = $2;
	}
	| EXPECT_OPTIONAL EXPECT_SCM function_arglist_nonbackup
	{
		$$ = $3;
	}
	| EXPECT_OPTIONAL EXPECT_SCM function_arglist_partial
	{
		$$ = $3;
	}
	;

function_arglist_partial_optional:
	EXPECT_SCM function_arglist_optional
	{
		$$ = $2;
	}
	| EXPECT_SCM function_arglist_partial_optional
	{
		$$ = $2;
	}
	| EXPECT_OPTIONAL EXPECT_SCM function_arglist_backup
	{
		$$ = $3;
	}
	| EXPECT_OPTIONAL EXPECT_SCM function_arglist_partial_optional
	{
		$$ = $3;
	}
	;

function_arglist_common:
	EXPECT_NO_MORE_ARGS {
		$$ = SCM_EOL;
	}
	| EXPECT_SCM function_arglist_optional embedded_scm_arg
	{
		if (scm_is_true (scm_call_1 ($1, $3)))
			$$ = scm_cons ($3, $2);
		else
			$$ = check_scheme_arg (parser, @3,
					       make_music_from_simple
					       (parser, @3, $3),
					       $2, $1);
	}
	| EXPECT_SCM function_arglist_optional bare_number_common
	{
		$$ = check_scheme_arg (parser, @3,
				       $3, $2, $1);
	}
	| EXPECT_SCM function_arglist_optional post_event_nofinger
	{
		$$ = check_scheme_arg (parser, @3,
				       $3, $2, $1);
	}
	| EXPECT_SCM function_arglist_optional '-' NUMBER_IDENTIFIER
	{
		SCM n = scm_difference ($4, SCM_UNDEFINED);
		$$ = check_scheme_arg (parser, @4, n, $2, $1);
	}
	| function_arglist_common_reparse REPARSE SCM_ARG
	{
		$$ = check_scheme_arg (parser, @3,
				       $3, $1, $2);
	}
	| function_arglist_common_reparse REPARSE lyric_element_music
	{
		$$ = check_scheme_arg (parser, @3,
				       $3, $1, $2);
	}
	| function_arglist_common_reparse REPARSE pitch_or_music
	{
		if (scm_is_true (scm_call_1 ($2, $3)))
			$$ = scm_cons ($3, $1);
		else
			$$ = check_scheme_arg (parser, @3,
					       make_music_from_simple
					       (parser, @3, $3),
					       $1, $2);
	}
	| function_arglist_common_reparse REPARSE bare_number_common
	{
		$$ = check_scheme_arg (parser, @3,
				       $3, $1, $2);
	}
	| function_arglist_common_reparse REPARSE multiplied_duration
	{
		$$ = check_scheme_arg (parser, @3,
				       $3, $1, $2);
	}
	| function_arglist_common_reparse REPARSE reparsed_rhythm
	{
		$$ = check_scheme_arg (parser, @3,
				       $3, $1, $2);
	}
	| function_arglist_common_reparse REPARSE symbol_list_arg
	{
		$$ = check_scheme_arg (parser, @3, $3, $1, $2);
	}
	;

function_arglist_common_reparse:
	EXPECT_SCM function_arglist_optional SCM_IDENTIFIER
	{
		$$ = $2;
		SCM res = try_string_variants ($1, $3);
		if (!SCM_UNBNDP (res))
			if (scm_is_pair (res))
				MYREPARSE (@3, $1, SYMBOL_LIST, res);
			else
				MYREPARSE (@3, $1, SCM_ARG, res);
		else if (scm_is_true
			 (scm_call_1
			  ($1, make_music_from_simple (parser, @3, $3))))
			MYREPARSE (@3, $1, LYRIC_ELEMENT, $3);
		else
			// This is going to flag a syntax error, we
			// know the predicate to be false.
			MYREPARSE (@3, $1, SCM_ARG, $3);
	}
	| EXPECT_SCM function_arglist_optional pitch
	{
		$$ = $2;
		if (scm_is_true
		    (scm_call_1
		     ($1, make_music_from_simple
		      (parser, @3, $3))))
			MYREPARSE (@3, $1, PITCH_IDENTIFIER, $3);
		else
			MYREPARSE (@3, $1, SCM_ARG, $3);
	}
	| EXPECT_SCM function_arglist_optional steno_tonic_pitch
	{
		$$ = $2;
		if (scm_is_true
		    (scm_call_1
		     ($1, make_music_from_simple
		      (parser, @3, $3))))
			MYREPARSE (@3, $1, TONICNAME_PITCH, $3);
		else
			MYREPARSE (@3, $1, SCM_ARG, $3);
	}
	| EXPECT_SCM function_arglist_optional STRING
	{
		$$ = $2;
		SCM res = try_string_variants ($1, $3);
		if (!SCM_UNBNDP (res))
			if (scm_is_pair (res))
				MYREPARSE (@3, $1, SYMBOL_LIST, res);
			else
				MYREPARSE (@3, $1, SCM_ARG, res);
		else if (scm_is_true
			 (scm_call_1
			  ($1, make_music_from_simple (parser, @3, $3))))
			MYREPARSE (@3, $1, LYRIC_ELEMENT, $3);
		else
			// This is going to flag a syntax error, we
			// know the predicate to be false.
			MYREPARSE (@3, $1, SCM_ARG, $3);
	}
	| EXPECT_SCM function_arglist_optional full_markup
	{
		$$ = $2;
		if (scm_is_true (scm_call_1 ($1, $3)))
			MYREPARSE (@3, $1, SCM_ARG, $3);
		else if (scm_is_true
			 (scm_call_1
			  ($1, make_music_from_simple (parser, @3, $3))))
			MYREPARSE (@3, $1, LYRIC_ELEMENT, $3);
		else
			// This is going to flag a syntax error, we
			// know the predicate to be false.
			MYREPARSE (@3, $1, SCM_ARG, $3);
	}
	| EXPECT_SCM function_arglist_optional UNSIGNED
	{
		$$ = $2;
		if (scm_is_true (scm_call_1 ($1, $3)))
			// May be 3 \cm or similar
			MYREPARSE (@3, $1, REAL, $3);
		else if (scm_is_true (scm_call_1 ($1, scm_list_1 ($3))))
			MYREPARSE (@3, $1, SYMBOL_LIST, scm_list_1 ($3));
		else {
			SCM d = make_duration ($3);
			if (!SCM_UNBNDP (d)) {
				if (scm_is_true (scm_call_1 ($1, d)))
					MYREPARSE (@3, $1, DURATION_IDENTIFIER, d);
				else if (scm_is_true
					 (scm_call_1
					  ($1, make_music_from_simple (parser, @3, d))))
					MYREPARSE (@3, $1, DURATION_ARG, d);
				else
					MYREPARSE (@3, $1, SCM_ARG, $3); // trigger error
			} else
				MYREPARSE (@3, $1, SCM_ARG, $3); // trigger error
		}
	}
	| EXPECT_SCM function_arglist_optional DURATION_IDENTIFIER
	{
		$$ = $2;
		if (scm_is_true (scm_call_1 ($1, $3)))
			MYREPARSE (@3, $1, DURATION_IDENTIFIER, $3);
		else if (scm_is_true
			 (scm_call_1
			  ($1, make_music_from_simple (parser, @3, $3))))
			MYREPARSE (@3, $1, DURATION_ARG, $3);
		else
			MYREPARSE (@3, $1, SCM_ARG, $3); // trigger error
	}
	| EXPECT_SCM function_arglist_optional '-' UNSIGNED
	{
		$$ = $2;
		SCM n = scm_difference ($4, SCM_UNDEFINED);
		if (scm_is_true (scm_call_1 ($1, n)))
			MYREPARSE (@4, $1, REAL, n);
		else {
			Music *t = MY_MAKE_MUSIC ("FingeringEvent", @4);
			t->set_property ("digit", $4);
			SCM m = t->unprotect ();
			if (scm_is_true (scm_call_1 ($1, m)))
				MYREPARSE (@4, $1, SCM_ARG, m);
			else
				MYREPARSE (@4, $1, SCM_ARG, $4);
		}
	}
	| EXPECT_SCM function_arglist_optional '-' REAL
	{
		$$ = $2;
		SCM n = scm_difference ($4, SCM_UNDEFINED);
		MYREPARSE (@4, $1, REAL, n);
	}
	;

function_arglist_optional:
	function_arglist_backup
	| EXPECT_OPTIONAL EXPECT_SCM function_arglist_skip_backup DEFAULT
	{
		$$ = scm_cons (loc_on_music (parser, @4, $1), $3);
	}
	| function_arglist_skip_backup BACKUP
	;

function_arglist_skip_backup:
	function_arglist_backup
	| EXPECT_OPTIONAL EXPECT_SCM function_arglist_skip_backup
	{
		$$ = scm_cons (loc_on_music (parser, @3, $1), $3);
	}
	;

music_function_call:
	MUSIC_FUNCTION function_arglist {
		$$ = MAKE_SYNTAX (music_function, @$,
				  $1, $2);
	}
	;


optional_id:
	/**/ { $$ = SCM_EOL; }
	| '=' simple_string {
		$$ = $2;
	}
	;

// We must not have lookahead tokens parsed in lyric mode.  In order
// to save confusion, we take almost the same set as permitted with
// \lyricmode and/or \lyrics.  However, music identifiers are also
// allowed, and they obviously do not require switching into lyrics
// mode for parsing.

lyric_mode_music:
	{
		parser->lexer_->push_lyric_state ();
	} grouped_music_list
	{
		parser->lexer_->pop_state ();
		$$ = $2;
	}
	| MUSIC_IDENTIFIER
	;

mode_changed_music:
	mode_changing_head grouped_music_list {
		if (scm_is_eq ($1, ly_symbol2scm ("chords")))
		{
		  $$ = MAKE_SYNTAX (unrelativable_music, @$, $2);
		}
		else
		{
		  $$ = $2;
		}
		parser->lexer_->pop_state ();
	}
	| mode_changing_head_with_context optional_context_mods grouped_music_list {
		$$ = MAKE_SYNTAX (context_specification, @$, $1, SCM_EOL, $2, SCM_BOOL_T, $3);
		if (scm_is_eq ($1, ly_symbol2scm ("ChordNames")))
		{
		  $$ = MAKE_SYNTAX (unrelativable_music, @$, $$);
		}
		parser->lexer_->pop_state ();
	}
	;

mode_changing_head:
	NOTEMODE {
		parser->lexer_->push_note_state (Lily::pitchnames);

		$$ = ly_symbol2scm ("notes");
	}
	| DRUMMODE
		{
		parser->lexer_->push_note_state (Lily::drum_pitch_names);

		$$ = ly_symbol2scm ("drums");
	}
	| FIGUREMODE {
		parser->lexer_->push_figuredbass_state ();

		$$ = ly_symbol2scm ("figures");
	}
	| CHORDMODE {
		parser->lexer_->chordmodifier_tab_ =
			alist_to_hashq (Lily::chordmodifiers);
		parser->lexer_->push_chord_state (Lily::pitchnames);
		$$ = ly_symbol2scm ("chords");

	}
	| LYRICMODE
		{ parser->lexer_->push_lyric_state ();
		$$ = ly_symbol2scm ("lyrics");
	}
	;

mode_changing_head_with_context:
	DRUMS {
		parser->lexer_->push_note_state (Lily::drum_pitch_names);

		$$ = ly_symbol2scm ("DrumStaff");
	}
	| FIGURES {
		parser->lexer_->push_figuredbass_state ();

		$$ = ly_symbol2scm ("FiguredBass");
	}
	| CHORDS {
		parser->lexer_->chordmodifier_tab_ =
			alist_to_hashq (Lily::chordmodifiers);
		parser->lexer_->push_chord_state (Lily::pitchnames);
		$$ = ly_symbol2scm ("ChordNames");
	}
	| LYRICS
		{ parser->lexer_->push_lyric_state ();
		$$ = ly_symbol2scm ("Lyrics");
	}
	;

context_change:
	CHANGE symbol '=' simple_string  {
		$$ = MAKE_SYNTAX (context_change, @$, $2, $4);
	}
	;


property_path:
	symbol_list_rev  {
		$$ = scm_reverse_x ($1, SCM_EOL);
	}
	;

property_operation:
	symbol '=' scalar {
		$$ = scm_list_3 (ly_symbol2scm ("assign"), $1, $3);
	}
	| UNSET symbol {
		$$ = scm_list_2 (ly_symbol2scm ("unset"), $2);
	}
	| OVERRIDE revert_arg '=' scalar {
		if (scm_ilength ($2) < 2) {
			parser->parser_error (@2, _("bad grob property path"));
			$$ = SCM_UNDEFINED;
		} else {
			$$ = scm_cons (ly_symbol2scm ("push"),
				       scm_cons2 (scm_car ($2),
						  $4,
						  scm_cdr ($2)));
		}
	}
	| REVERT revert_arg {
		$$ = scm_cons (ly_symbol2scm ("pop"), $2);
	}
	;

// This is all quite awkward for the sake of substantial backward
// compatibility while at the same time allowing a more "natural" form
// of specification not separating grob specification from grob
// property path.  The purpose of this definition of revert_arg is to
// allow the symbol list which specifies grob and property to revert
// to be optionally be split into two parts after the grob (which in
// this case is just the first element of the list).  symbol_list_part
// is only one path component, but it can be parsed without lookahead,
// so we can follow it with a synthetic BACKUP token when needed.  If
// the first symbol_list_part already contains multiple elements (only
// possible if a Scheme expression provides them), we just parse for
// additional elements introduced by '.', which is what the
// SYMBOL_LIST backup in connection with the immediately following
// rule using symbol_list_arg does.
//
// As long as we don't have our coffers filled with both grob and at
// least one grob property specification, the rest of the required
// symbol list chain may be provided either with or without a leading
// dot.  This is for both allowing the traditional
// \revert Accidental #'color
// as well as well as the "naive" form
// \revert Accidental.color

revert_arg:
	revert_arg_backup BACKUP symbol_list_arg
	{
		$$ = $3;
	}
	;

revert_arg_backup:
	revert_arg_part
	{
		if (scm_is_null ($1)
		    || scm_is_null (scm_cdr ($1)))
			MYBACKUP (SCM_ARG, $1, @1);
		else
			MYBACKUP (SYMBOL_LIST, scm_reverse_x ($1, SCM_EOL), @1);
	}
	;

// revert_arg_part delivers results in reverse
revert_arg_part:
	symbol_list_part
	| revert_arg_backup BACKUP SCM_ARG '.' symbol_list_part
	{
		$$ = scm_append_x (scm_list_2 ($5, $3));
	}
	| revert_arg_backup BACKUP SCM_ARG ',' symbol_list_part
	{
		$$ = scm_append_x (scm_list_2 ($5, $3));
	}
	| revert_arg_backup BACKUP SCM_ARG symbol_list_part
	{
		$$ = scm_append_x (scm_list_2 ($4, $3));
	}
	;

context_def_mod:
	CONSISTS { $$ = ly_symbol2scm ("consists"); }
	| REMOVE { $$ = ly_symbol2scm ("remove"); }

	| ACCEPTS { $$ = ly_symbol2scm ("accepts"); }
	| DEFAULTCHILD { $$ = ly_symbol2scm ("default-child"); }
	| DENIES { $$ = ly_symbol2scm ("denies"); }

	| ALIAS { $$ = ly_symbol2scm ("alias"); }
	| TYPE { $$ = ly_symbol2scm ("translator-type"); }
	| DESCRIPTION { $$ = ly_symbol2scm ("description"); }
	| NAME { $$ = ly_symbol2scm ("context-name"); }
	;

context_mod:
	property_operation { $$ = $1; }
	| context_def_mod STRING {
		$$ = scm_list_2 ($1, $2);
	}
	| context_def_mod embedded_scm
	{
		if (!scm_is_string ($2)
		    && ly_symbol2scm ("consists") != $1
		    && ly_symbol2scm ("remove") != $1)
		{
			$$ = SCM_EOL;
			parser->parser_error (@1, _ ("only \\consists and \\remove take non-string argument."));
		}
		else
		{
			$$ = scm_list_2 ($1, $2);
		}
	}
	;

// If defined, at least two members.
grob_prop_spec:
	symbol_list_rev
	{
		SCM l = scm_reverse_x ($1, SCM_EOL);
		if (scm_is_pair (l)
		    && to_boolean
		    (scm_object_property (scm_car (l),
					  ly_symbol2scm ("is-grob?"))))
			l = scm_cons (ly_symbol2scm ("Bottom"), l);
		if (scm_is_null (l) || scm_is_null (scm_cdr (l))) {
			parser->parser_error (@1, _ ("bad grob property path"));
			l = SCM_UNDEFINED;
		}
		$$ = l;
	}
	;

// If defined, at least three members
grob_prop_path:
	grob_prop_spec
	{
		if (!SCM_UNBNDP ($1) && scm_is_null (scm_cddr ($1)))
		{
			parser->parser_error (@1, _ ("bad grob property path"));
			$$ = SCM_UNDEFINED;
		}
	}
	| grob_prop_spec property_path
	{
		if (!SCM_UNBNDP ($1)) {
			$$ = scm_append_x (scm_list_2 ($1, $2));
			if (scm_is_null (scm_cddr ($$))) {
				parser->parser_error (@$, _ ("bad grob property path"));
				$$ = SCM_UNDEFINED;
			}
		}

	}
	;

// Exactly two elements or undefined
context_prop_spec:
	symbol_list_rev
	{
		SCM l = scm_reverse_x ($1, SCM_EOL);
		switch (scm_ilength (l)) {
		case 1:
			l = scm_cons (ly_symbol2scm ("Bottom"), l);
		case 2:
			break;
		default:
			parser->parser_error (@1, _ ("bad context property path"));
			l = SCM_UNDEFINED;
		}
		$$ = l;
	}
	;


// This is all quite awkward for the sake of substantial backward
// compatibility while at the same time allowing a more "natural" form
// of specification not separating grob specification from grob
// property path.  The purpose of this definition of
// simple_revert_context is to allow the symbol list which specifies
// grob and property to revert to be optionally be split into two
// parts after the grob (which may be preceded by a context
// specification, a case which we distinguish by checking whether the
// first symbol is a valid grob symbol instead).
//
// See revert_arg above for the main work horse of this arrangement.
// simple_revert_context just caters for the context and delegates the
// rest of the job to revert_arg.

simple_revert_context:
	symbol_list_part
	{
		$1 = scm_reverse_x ($1, SCM_EOL);
		if (scm_is_null ($1)
		    || to_boolean
		    (scm_object_property (scm_car ($1),
					  ly_symbol2scm ("is-grob?")))) {
			$$ = ly_symbol2scm ("Bottom");
			parser->lexer_->push_extra_token (@1, SCM_IDENTIFIER, $1);
		} else {
			$$ = scm_car ($1);
			parser->lexer_->push_extra_token (@1, SCM_IDENTIFIER,
							  scm_cdr ($1));
		}
	}
	;

music_property_def:
	OVERRIDE grob_prop_path '=' scalar {
		if (SCM_UNBNDP ($2))
			$$ = MAKE_SYNTAX (void_music, @$);
		else
			$$ = MAKE_SYNTAX (property_override, @$,
					  scm_car ($2),
					  scm_cdr ($2),
					  $4);
	}
	| REVERT simple_revert_context revert_arg {
		$$ = MAKE_SYNTAX (property_revert, @$, $2, $3);
	}
	| SET context_prop_spec '=' scalar {
		if (SCM_UNBNDP ($2))
			$$ = MAKE_SYNTAX (void_music, @$);
		else
			$$ = MAKE_SYNTAX (property_set, @$,
					  scm_car ($2),
					  scm_cadr ($2),
					  $4);
	}
	| UNSET context_prop_spec {
		if (SCM_UNBNDP ($2))
			$$ = MAKE_SYNTAX (void_music, @$);
		else
			$$ = MAKE_SYNTAX (property_unset, @$,
					  scm_car ($2),
					  scm_cadr ($2));
	}
	;

string:
	STRING
	| full_markup
	;

text:
	STRING
	| full_markup
	| embedded_scm_bare
	{
		if (Text_interface::is_markup ($1)) {
			$$ = $1;
		} else {
			parser->parser_error (@1, (_ ("markup expected")));
			$$ = scm_string (SCM_EOL);
		}
	}
	;

simple_string: STRING
	| embedded_scm_bare
	{
		if (scm_is_string ($1)) {
			$$ = $1;
		} else {
			parser->parser_error (@1, (_ ("simple string expected")));
			$$ = scm_string (SCM_EOL);
		}
	}
	;

symbol:
	STRING {
		$$ = scm_string_to_symbol ($1);
	}
	| embedded_scm_bare
	{
		// This is a bit of overkill but makes the same
		// routine responsible for all symbol interpretations.
		$$ = try_string_variants (Guile_user::symbol_p, $1);
		if (SCM_UNBNDP ($$))
		{
			parser->parser_error (@1, (_ ("symbol expected")));
			// Generate a unique symbol in case it is used
			// for an assignment or similar
			$$ = scm_make_symbol (ly_string2scm ("undefined"));
		}
	}
	;

scalar:
	embedded_scm_arg
	| pitch_or_music
	| SCM_IDENTIFIER
	| bare_number
	// The following is a rather defensive variant of admitting
	// negative numbers: the grammar would permit number_factor or
	// even number_expression.  However, function arguments allow
	// only this simple kind of negative number, so to have things
	// like \tweak and \override behave reasonably similar, it
	// makes sense to rule out things like -- which are rather an
	// accent in function argument contexts.
	| '-' bare_number
	{
		$$ = scm_difference ($2, SCM_UNDEFINED);
	}
	| string
	| symbol_list_part_bare '.' property_path
	{
		$$ = scm_reverse_x ($1, $3);
	}
	| symbol_list_part_bare ',' property_path
	{
		$$ = scm_reverse_x ($1, $3);
	}
	;

event_chord:
	simple_element post_events {
		// Let the rhythmic music iterator sort this mess out.
		if (scm_is_pair ($2)) {
			unsmob<Music> ($$)->set_property ("articulations",
							 scm_reverse_x ($2, SCM_EOL));
		}
	} %prec ':'
	| CHORD_REPETITION optional_notemode_duration post_events {
		$$ = MAKE_SYNTAX (repetition_chord, @$,
				  $2, scm_reverse_x ($3, SCM_EOL));
	} %prec ':'
	| MULTI_MEASURE_REST optional_notemode_duration post_events {
		$$ = MAKE_SYNTAX (multi_measure_rest, @$, $2,
				  scm_reverse_x ($3, SCM_EOL));
	} %prec ':'
	| tempo_event
	| note_chord_element
	;


note_chord_element:
	chord_body optional_notemode_duration post_events
	{
		Music *m = unsmob<Music> ($1);
		SCM dur = unsmob<Duration> ($2)->smobbed_copy ();
		SCM es = m->get_property ("elements");
		SCM postevs = scm_reverse_x ($3, SCM_EOL);

		for (SCM s = es; scm_is_pair (s); s = scm_cdr (s))
		  unsmob<Music> (scm_car (s))->set_property ("duration", dur);
		es = ly_append2 (es, postevs);

		m->set_property ("elements", es);
		m->set_spot (parser->lexer_->override_input (@$));
		$$ = m->self_scm ();
	} %prec ':'
	;

chord_body:
	ANGLE_OPEN chord_body_elements ANGLE_CLOSE
	{
		$$ = MAKE_SYNTAX (event_chord, @$, scm_reverse_x ($2, SCM_EOL));
	}
	| FIGURE_OPEN figure_list FIGURE_CLOSE
	{
		$$ = MAKE_SYNTAX (event_chord, @$, scm_reverse_x ($2, SCM_EOL));
	}
	;

chord_body_elements:
	/* empty */ 		{ $$ = SCM_EOL; }
	| chord_body_elements chord_body_element {
		if (!SCM_UNBNDP ($2))
			$$ = scm_cons ($2, $1);
	}
	;

chord_body_element:
	pitch_or_tonic_pitch exclamations questions octave_check post_events
	{
		bool q = to_boolean ($3);
		bool ex = to_boolean ($2);
		SCM check = $4;
		SCM post = $5;

		Music *n = MY_MAKE_MUSIC ("NoteEvent", @$);
		n->set_property ("pitch", $1);
		if (q)
			n->set_property ("cautionary", SCM_BOOL_T);
                if (ex || q)
			n->set_property ("force-accidental", SCM_BOOL_T);

		if (scm_is_pair (post)) {
			SCM arts = scm_reverse_x (post, SCM_EOL);
			n->set_property ("articulations", arts);
		}
		if (scm_is_number (check))
		{
			int q = scm_to_int (check);
			n->set_property ("absolute-octave", scm_from_int (q-1));
		}

		$$ = n->unprotect ();
	}
	| DRUM_PITCH post_events {
		Music *n = MY_MAKE_MUSIC ("NoteEvent", @$);
		n->set_property ("drum-type", $1);

		if (scm_is_pair ($2)) {
			SCM arts = scm_reverse_x ($2, SCM_EOL);
			n->set_property ("articulations", arts);
		}
		$$ = n->unprotect ();
	}
	| music_function_chord_body
	{
		Music *m = unsmob<Music> ($1);

		while (m && m->is_mus_type ("music-wrapper-music")) {
			$$ = m->get_property ("element");
			m = unsmob<Music> ($$);
		}

		if (!(m && m->is_mus_type ("rhythmic-event"))) {
			parser->parser_error (@$, _ ("not a rhythmic event"));
			$$ = SCM_UNDEFINED;
		}
	}
	;

music_function_chord_body:
	music_function_call
	| MUSIC_IDENTIFIER
	| embedded_scm
	;

event_function_event:
	EVENT_FUNCTION function_arglist {
		$$ = MAKE_SYNTAX (music_function, @$,
				  $1, $2);
	}
	;

post_events:
	/* empty */ {
		$$ = SCM_EOL;
	}
	| post_events post_event {
		$$ = $1;
		if (Music *m = unsmob<Music> ($2))
		{
			if (m->is_mus_type ("post-event-wrapper"))
			{
				for (SCM p = m->get_property ("elements");
				     scm_is_pair (p);
				     p = scm_cdr (p))
				{
					$$ = scm_cons (scm_car (p), $$);
				}
			} else {
				m->set_spot (parser->lexer_->override_input (@2));
				$$ = scm_cons ($2, $$);
			}
		}
	}
	;

post_event_nofinger:
	direction_less_event {
		$$ = $1;
	}
	| script_dir music_function_call {
		$$ = $2;
		if (!unsmob<Music> ($2)->is_mus_type ("post-event")) {
			parser->parser_error (@2, _ ("post-event expected"));
			$$ = SCM_UNSPECIFIED;
		} else if (!SCM_UNBNDP ($1))
		{
			unsmob<Music> ($$)->set_property ("direction", $1);
		}
	}
	| HYPHEN {
		if (!parser->lexer_->is_lyric_state ())
			parser->parser_error (@1, _ ("have to be in Lyric mode for lyrics"));
		$$ = MY_MAKE_MUSIC ("HyphenEvent", @$)->unprotect ();
	}
	| EXTENDER {
		if (!parser->lexer_->is_lyric_state ())
			parser->parser_error (@1, _ ("have to be in Lyric mode for lyrics"));
		$$ = MY_MAKE_MUSIC ("ExtenderEvent", @$)->unprotect ();
	}
	| script_dir direction_reqd_event {
		if (!SCM_UNBNDP ($1))
		{
			Music *m = unsmob<Music> ($2);
			m->set_property ("direction", $1);
		}
		$$ = $2;
	}
	| script_dir direction_less_event {
		if (!SCM_UNBNDP ($1))
		{
			Music *m = unsmob<Music> ($2);
			m->set_property ("direction", $1);
		}
		$$ = $2;
	}
	| '^' fingering
	{
		$$ = $2;
		unsmob<Music> ($$)->set_property ("direction", scm_from_int (UP));
	}
	| '_' fingering
	{
		$$ = $2;
		unsmob<Music> ($$)->set_property ("direction", scm_from_int (DOWN));
	}
	;

post_event:
	post_event_nofinger
	| '-' fingering {
		$$ = $2;
	}
	;

string_number_event:
	E_UNSIGNED {
		Music *s = MY_MAKE_MUSIC ("StringNumberEvent", @$);
		s->set_property ("string-number", $1);
		$$ = s->unprotect ();
	}
	;

direction_less_event:
	string_number_event
	| EVENT_IDENTIFIER	{
		$$ = $1;
	}
	| tremolo_type  {
               Music *a = MY_MAKE_MUSIC ("TremoloEvent", @$);
               a->set_property ("tremolo-type", $1);
               $$ = a->unprotect ();
        }
	| event_function_event
	;

direction_reqd_event:
	gen_text_def {
		$$ = $1;
	}
	| script_abbreviation {
		SCM s = parser->lexer_->lookup_identifier ("dash" + ly_scm2string ($1));
		if (scm_is_string (s)) {
			Music *a = MY_MAKE_MUSIC ("ArticulationEvent", @$);
			a->set_property ("articulation-type", s);
			$$ = a->unprotect ();
		} else {
			Music *original = unsmob<Music> (s);
			if (original && original->is_mus_type ("post-event")) {
				Music *a = original->clone ();
				a->set_spot (parser->lexer_->override_input (@$));
				$$ = a->unprotect ();
			} else {
				parser->parser_error (@1, _ ("expecting string or post-event as script definition"));
				$$ = MY_MAKE_MUSIC ("PostEvents", @$)->unprotect ();
			}
		}
	}
	;

octave_check:
	/**/ { $$ = SCM_EOL; }
	| '=' quotes { $$ = $2; }
	;

quotes:
	/* empty */
	{
                $$ = SCM_INUM0;
        }
	| sub_quotes
        | sup_quotes
        ;

sup_quotes:
	'\'' {
		$$ = scm_from_int (1);
	}
	| sup_quotes '\'' {
		$$ = scm_oneplus ($1);
	}
	;

sub_quotes:
	',' {
		$$ = scm_from_int (-1);
	}
	| sub_quotes ',' {
		$$ = scm_oneminus ($1);
	}
	;

steno_pitch:
	NOTENAME_PITCH quotes {
                if (!scm_is_eq (SCM_INUM0, $2))
                {
                        Pitch p = *unsmob<Pitch> ($1);
                        p = p.transposed (Pitch (scm_to_int ($2), 0));
                        $$ = p.smobbed_copy ();
                }
	}
	;

/*
ugh. duplication
*/

steno_tonic_pitch:
	TONICNAME_PITCH	quotes {
                if (!scm_is_eq (SCM_INUM0, $2))
                {
                        Pitch p = *unsmob<Pitch> ($1);
                        p = p.transposed (Pitch (scm_to_int ($2), 0));
                        $$ = p.smobbed_copy ();
                }
	}
	;

pitch:
	steno_pitch
	| PITCH_IDENTIFIER quotes {
                if (!scm_is_eq (SCM_INUM0, $2))
                {
                        Pitch p = *unsmob<Pitch> ($1);
                        p = p.transposed (Pitch (scm_to_int ($2), 0));
                        $$ = p.smobbed_copy ();
                }
	}
	;

pitch_or_tonic_pitch:
	pitch
	| steno_tonic_pitch
	;

gen_text_def:
	full_markup {
		Music *t = MY_MAKE_MUSIC ("TextScriptEvent", @$);
		t->set_property ("text", $1);
		$$ = t->unprotect ();
	}
	| STRING {
		Music *t = MY_MAKE_MUSIC ("TextScriptEvent", @$);
		t->set_property ("text",
			make_simple_markup ($1));
		$$ = t->unprotect ();
	}
	| embedded_scm
	{
		Music *m = unsmob<Music> ($1);
		if (m && m->is_mus_type ("post-event"))
			$$ = $1;
		else if (Text_interface::is_markup ($1)) {
			Music *t = MY_MAKE_MUSIC ("TextScriptEvent", @$);
			t->set_property ("text", $1);
			$$ = t->unprotect ();
		} else
			parser->parser_error (@1, _ ("not an articulation"));
	}
	;

fingering:
	UNSIGNED {
		Music *t = MY_MAKE_MUSIC ("FingeringEvent", @$);
		t->set_property ("digit", $1);
		$$ = t->unprotect ();
	}
	;

script_abbreviation:
	'^'		{
		$$ = scm_from_ascii_string ("Hat");
	}
	| '+'		{
		$$ = scm_from_ascii_string ("Plus");
	}
	| '-' 		{
		$$ = scm_from_ascii_string ("Dash");
	}
 	| '!'		{
		$$ = scm_from_ascii_string ("Bang");
	}
	| ANGLE_CLOSE	{
		$$ = scm_from_ascii_string ("Larger");
	}
	| '.' 		{
		$$ = scm_from_ascii_string ("Dot");
	}
	| '_' {
		$$ = scm_from_ascii_string ("Underscore");
	}
	;

script_dir:
	'_'	{ $$ = scm_from_int (DOWN); }
	| '^'	{ $$ = scm_from_int (UP); }
	| '-'	{ $$ = SCM_UNDEFINED; }
	;

maybe_notemode_duration:
	{
		$$ = SCM_UNDEFINED;
	} %prec ':'
	| multiplied_duration	{
		$$ = $1;
		parser->default_duration_ = *unsmob<Duration> ($$);
	}
;


optional_notemode_duration:
	maybe_notemode_duration
	{
		if (SCM_UNBNDP ($$))
			$$ = parser->default_duration_.smobbed_copy ();
	}
	;

steno_duration:
	UNSIGNED dots		{
		$$ = make_duration ($1, scm_to_int ($2));
		if (SCM_UNBNDP ($$))
		{
			parser->parser_error (@1, _ ("not a duration"));
			$$ = Duration ().smobbed_copy ();
		}
	}
	| DURATION_IDENTIFIER dots	{
		$$ = make_duration ($1, scm_to_int ($2));
	}
	;

multiplied_duration:
	steno_duration multipliers {
		$$ = make_duration ($1, 0, $2);
	}
	;

dots:
	/* empty */ 	{
		$$ = SCM_INUM0;
	}
	| dots '.' {
		$$ = scm_oneplus ($1);
	}
	;

multipliers:
	/* empty */
	{
		$$ = SCM_UNDEFINED;
	}
	| multipliers '*' UNSIGNED
	{
		if (!SCM_UNBNDP ($1))
			$$ = scm_product ($1, $3);
		else
			$$ = $3;
	}
	| multipliers '*' FRACTION
	{
		if (!SCM_UNBNDP ($1))
			$$ = scm_product ($1, scm_divide (scm_car ($3),
							  scm_cdr ($3)));
		else
			$$ = scm_divide (scm_car ($3), scm_cdr ($3));
	}
	;

tremolo_type:
	':'	{
		$$ = scm_from_int (parser->default_tremolo_type_);
	}
	| ':' UNSIGNED {
		if (SCM_UNBNDP (make_duration ($2))) {
			parser->parser_error (@2, _ ("not a duration"));
			$$ = scm_from_int (parser->default_tremolo_type_);
		} else {
			$$ = $2;
			parser->default_tremolo_type_ = scm_to_int ($2);
		}
	}
	;

bass_number:
	UNSIGNED { $$ = $1; }
	| STRING { $$ = $1; }
	| full_markup { $$ = $1; }
	| embedded_scm_bare
	{
		// as an integer, it needs to be non-negative, and otherwise
		// it needs to be suitable as a markup.
		if (scm_is_integer ($1)
		    ? scm_is_true (scm_negative_p ($1))
		    : !Text_interface::is_markup ($1))
		{
			parser->parser_error (@1, _ ("bass number expected"));
			$$ = SCM_INUM0;
		}
	}
	;

figured_bass_alteration:
	'-' 	{ $$ = ly_rational2scm (FLAT_ALTERATION); }
	| '+'	{ $$ = ly_rational2scm (SHARP_ALTERATION); }
	| '!'	{ $$ = scm_from_int (0); }
	;

bass_figure:
	FIGURE_SPACE {
		Music *bfr = MY_MAKE_MUSIC ("BassFigureEvent", @$);
		$$ = bfr->unprotect ();
	}
	| bass_number  {
		Music *bfr = MY_MAKE_MUSIC ("BassFigureEvent", @$);
		$$ = bfr->self_scm ();

		if (scm_is_number ($1))
			bfr->set_property ("figure", $1);
		else if (Text_interface::is_markup ($1))
			bfr->set_property ("text", $1);

		bfr->unprotect ();
	}
	| bass_figure ']' {
		$$ = $1;
		unsmob<Music> ($1)->set_property ("bracket-stop", SCM_BOOL_T);
	}
	| bass_figure figured_bass_alteration {
		Music *m = unsmob<Music> ($1);
		if (scm_to_double ($2)) {
			SCM salter = m->get_property ("alteration");
			SCM alter = scm_is_number (salter) ? salter : scm_from_int (0);
			m->set_property ("alteration",
					 scm_sum (alter, $2));
		} else {
			m->set_property ("alteration", scm_from_int (0));
		}
	}
	| bass_figure figured_bass_modification  {
		Music *m = unsmob<Music> ($1);
		m->set_property ($2, SCM_BOOL_T);
	}
	;


figured_bass_modification:
	E_PLUS		{
		$$ = ly_symbol2scm ("augmented");
	}
	| E_EXCLAMATION {
		$$ = ly_symbol2scm ("no-continuation");
	}
	| '/'		{
		$$ = ly_symbol2scm ("diminished");
	}
	| E_BACKSLASH {
		$$ = ly_symbol2scm ("augmented-slash");
	}
	;

br_bass_figure:
	bass_figure {
		$$ = $1;
	}
	| '[' bass_figure {
		$$ = $2;
		unsmob<Music> ($$)->set_property ("bracket-start", SCM_BOOL_T);
	}
	;

figure_list:
	/**/		{
		$$ = SCM_EOL;
	}
	| figure_list br_bass_figure {
		$$ = scm_cons ($2, $1);
	}
	;

optional_rest:
	/**/   { $$ = SCM_BOOL_F; }
	| REST { $$ = SCM_BOOL_T; }
	;

pitch_or_music:
	pitch exclamations questions octave_check maybe_notemode_duration optional_rest post_events {
		if (!parser->lexer_->is_note_state ())
			parser->parser_error (@1, _ ("have to be in Note mode for notes"));
		if (!SCM_UNBNDP ($2)
                    || !SCM_UNBNDP ($3)
                    || scm_is_number ($4)
                    || !SCM_UNBNDP ($5)
                    || scm_is_true ($6)
		    || scm_is_pair ($7))
		{
			Music *n = 0;
			if (scm_is_true ($6))
				n = MY_MAKE_MUSIC ("RestEvent", @$);
			else
				n = MY_MAKE_MUSIC ("NoteEvent", @$);

			n->set_property ("pitch", $1);
			if (SCM_UNBNDP ($5))
				n->set_property ("duration",
						 parser->default_duration_.smobbed_copy ());
			else
				n->set_property ("duration", $5);

			if (scm_is_number ($4))
			{
				int q = scm_to_int ($4);
				n->set_property ("absolute-octave", scm_from_int (q-1));
			}

			if (to_boolean ($3))
				n->set_property ("cautionary", SCM_BOOL_T);
			if (to_boolean ($2) || to_boolean ($3))
				n->set_property ("force-accidental", SCM_BOOL_T);
			if (scm_is_pair ($7))
				n->set_property ("articulations",
						 scm_reverse_x ($7, SCM_EOL));
			$$ = n->unprotect ();
		}
	} %prec ':'
	| new_chord post_events {
		if (!parser->lexer_->is_chord_state ())
                        parser->parser_error (@1, _ ("have to be in Chord mode for chords"));
		if (scm_is_pair ($2)) {
			if (unsmob<Pitch> ($1))
				$1 = make_chord_elements (@1,
							  $1,
							  parser->default_duration_.smobbed_copy (),
							  SCM_EOL);

			SCM elts = ly_append2 ($1, scm_reverse_x ($2, SCM_EOL));

			$$ = MAKE_SYNTAX (event_chord, @1, elts);
		} else if (!unsmob<Pitch> ($1))
			$$ = MAKE_SYNTAX (event_chord, @1, $1);
		// A mere pitch drops through.
	} %prec ':'
	;

simple_element:
	DRUM_PITCH optional_notemode_duration {
		Music *n = MY_MAKE_MUSIC ("NoteEvent", @$);
		n->set_property ("duration", $2);
		n->set_property ("drum-type", $1);

		$$ = n->unprotect ();
	}
 	| RESTNAME optional_notemode_duration		{
		Music *ev = 0;
 		if (ly_scm2string ($1) == "s") {
			/* Space */
			ev = MY_MAKE_MUSIC ("SkipEvent", @$);
		  }
		else {
			ev = MY_MAKE_MUSIC ("RestEvent", @$);

		    }
		ev->set_property ("duration", $2);
 		$$ = ev->unprotect ();
	}
	;

lyric_element:
	full_markup {
		if (!parser->lexer_->is_lyric_state ())
			parser->parser_error (@1, _ ("markup outside of text script or \\lyricmode"));
		$$ = $1;
	}
	| STRING {
		if (!parser->lexer_->is_lyric_state ())
			parser->parser_error (@1, _ ("unrecognized string, not in text script or \\lyricmode"));
		$$ = $1;
	}
	| LYRIC_ELEMENT
	;

lyric_element_music:
	lyric_element optional_notemode_duration post_events {
		$$ = MAKE_SYNTAX (lyric_event, @$, $1, $2);
		if (scm_is_pair ($3))
			unsmob<Music> ($$)->set_property
				("articulations", scm_reverse_x ($3, SCM_EOL));
	} %prec ':'
	;

// Can return a single pitch rather than a list.
new_chord:
	steno_tonic_pitch maybe_notemode_duration   {
		if (SCM_UNBNDP ($2))
			$$ = $1;
		else
			$$ = make_chord_elements (@$, $1, $2, SCM_EOL);
	}
	| steno_tonic_pitch optional_notemode_duration chord_separator chord_items {
		SCM its = scm_reverse_x ($4, SCM_EOL);
		$$ = make_chord_elements (@$, $1, $2, scm_cons ($3, its));
	} %prec ':'
	;

chord_items:
	/**/ {
		$$ = SCM_EOL;
	}
	| chord_items chord_item {
		$$ = scm_cons ($2, $$);
	}
	;

chord_separator:
	CHORD_COLON {
		$$ = ly_symbol2scm ("chord-colon");
	}
	| CHORD_CARET {
		$$ = ly_symbol2scm ("chord-caret");
	}
	| CHORD_SLASH steno_tonic_pitch {
 		$$ = scm_list_2 (ly_symbol2scm ("chord-slash"), $2);
	}
	| CHORD_BASS steno_tonic_pitch {
		$$ = scm_list_2 (ly_symbol2scm ("chord-bass"), $2);
	}
	;

chord_item:
	chord_separator {
		$$ = $1;
	}
	| step_numbers {
		$$ = scm_reverse_x ($1, SCM_EOL);
	}
	| CHORD_MODIFIER  {
		$$ = $1;
	}
	;

step_numbers:
	step_number { $$ = scm_cons ($1, SCM_EOL); }
	| step_numbers '.' step_number {
		$$ = scm_cons ($3, $$);
	}
	;

step_number:
	UNSIGNED {
		$$ = make_chord_step ($1, 0);
        }
	| UNSIGNED '+' {
		$$ = make_chord_step ($1, SHARP_ALTERATION);
	}
	| UNSIGNED CHORD_MINUS {
		$$ = make_chord_step ($1, FLAT_ALTERATION);
	}
	;

tempo_range:
	unsigned_number {
		$$ = $1;
	} %prec ':'
	| unsigned_number '-' unsigned_number {
		$$ = scm_cons ($1, $3);
	}
	;

/*
	UTILITIES

TODO: should deprecate in favor of Scheme?

 */
number_expression:
	number_expression '+' number_term {
		$$ = scm_sum ($1, $3);
	}
	| number_expression '-' number_term {
		$$ = scm_difference ($1, $3);
	}
	| number_term
	;

number_term:
	number_factor {
		$$ = $1;
	}
	| number_factor '*' number_factor {
		$$ = scm_product ($1, $3);
	}
	| number_factor '/' number_factor {
		$$ = scm_divide ($1, $3);
	}
	;

number_factor:
	'-'  number_factor { /* %prec UNARY_MINUS */
		$$ = scm_difference ($2, SCM_UNDEFINED);
	}
	| bare_number
	;

bare_number_common:
	REAL
	| NUMBER_IDENTIFIER
	| REAL NUMBER_IDENTIFIER
	{
		$$ = scm_product ($1, $2);
	}
	;

bare_number:
	bare_number_common
	| UNSIGNED
	| UNSIGNED NUMBER_IDENTIFIER	{
		$$ = scm_product ($1, $2);
	}
	;

unsigned_number:
	UNSIGNED
	| NUMBER_IDENTIFIER
	{
		if (!scm_is_integer ($1)
		    || scm_is_true (scm_negative_p ($1)))
		{
			parser->parser_error (@1, _("not an unsigned integer"));
			$$ = SCM_INUM0;
		}
	}
	| embedded_scm
	{
		if (!scm_is_integer ($1)
		    || scm_is_true (scm_negative_p ($1)))
		{
			parser->parser_error (@1, _("not an unsigned integer"));
			$$ = SCM_INUM0;
		}
	}
	;

exclamations:
		{ $$ = SCM_UNDEFINED; }
	| exclamations '!'
        {
                if (SCM_UNBNDP ($1))
                        $$ = SCM_BOOL_T;
                else
                        $$ = scm_not ($1);
        }
	;

questions:
// This precedence rule is rather weird.  It triggers when '!' is
// encountered after a pitch, and is used for deciding whether to save
// this instead for a figure modification.  This should not actually
// occur in practice as pitches and figures are generated in different
// modes.  Using a greedy (%right) precedence makes sure that we don't
// get stuck in a wrong interpretation.
	{ $$ = SCM_UNDEFINED; } %prec ':'
	| questions '?'
        {
                if (SCM_UNBNDP ($1))
                        $$ = SCM_BOOL_T;
                else
                        $$ = scm_not ($1);
        }
	;

full_markup_list:
	MARKUPLIST
		{ parser->lexer_->push_markup_state (); }
	markup_list {
		$$ = $3;
		parser->lexer_->pop_state ();
	}
	;

markup_mode:
	MARKUP
	{
		parser->lexer_->push_markup_state ();
	}
	;

full_markup:
	markup_mode markup_top {
		$$ = $2;
		parser->lexer_->pop_state ();
	}
	;

partial_markup:
	markup_mode markup_partial_function ETC
	{
		$$ = MAKE_SYNTAX (partial_markup, @2, $2);
		parser->lexer_->pop_state ();
	}
	;

markup_top:
	markup_list {
		$$ = scm_list_2 (Lily::line_markup,  $1);
	}
	| markup_head_1_list simple_markup
	{
		$$ = scm_car (MAKE_SYNTAX (composed_markup_list,
					   @2, $1, scm_list_1 ($2)));
	}
	| simple_markup	{
		$$ = $1;
	}
	;

markup_scm:
	embedded_scm
	{
		if (Text_interface::is_markup ($1))
			MYBACKUP (MARKUP_IDENTIFIER, $1, @1);
		else if (Text_interface::is_markup_list ($1))
			MYBACKUP (MARKUPLIST_IDENTIFIER, $1, @1);
		else {
			parser->parser_error (@1, _ ("not a markup"));
			MYBACKUP (MARKUP_IDENTIFIER, scm_string (SCM_EOL), @1);
		}
	} BACKUP
	;


markup_list:
	markup_composed_list {
		$$ = $1;
	}
	| markup_uncomposed_list
	;

markup_uncomposed_list:
	markup_braced_list {
		$$ = $1;
	}
	| markup_command_list {
		$$ = scm_list_1 ($1);
	}
	| markup_scm MARKUPLIST_IDENTIFIER
	{
		$$ = $2;
	}
	| SCORELINES {
		parser->lexer_->push_note_state (Lily::pitchnames);
	} '{' score_body '}' {
		Score *sc = unsmob<Score> ($4);
		sc->origin ()->set_spot (@$);
		if (sc->defs_.empty ()) {
			Output_def *od = get_layout (parser);
			sc->add_output_def (od);
			od->unprotect ();
		}
		$$ = scm_list_1 (scm_list_2 (Lily::score_lines_markup_list, $4));
		parser->lexer_->pop_state ();
	}
	;

markup_composed_list:
	markup_head_1_list markup_uncomposed_list {
		$$ = MAKE_SYNTAX (composed_markup_list,
				  @2, $1, $2);
	}
	;

markup_braced_list:
	'{' markup_braced_list_body '}'	{
		$$ = scm_reverse_x ($2, SCM_EOL);
	}
	;

markup_braced_list_body:
	/* empty */	{  $$ = SCM_EOL; }
	| markup_braced_list_body markup {
		$$ = scm_cons ($2, $1);
	}
	| markup_braced_list_body markup_list {
		$$ = scm_reverse_x ($2, $1);
	}
	;

markup_command_list:
	MARKUP_LIST_FUNCTION markup_command_list_arguments {
	  $$ = scm_cons ($1, scm_reverse_x($2, SCM_EOL));
	}
	;

markup_command_basic_arguments:
	EXPECT_MARKUP_LIST markup_command_list_arguments markup_list {
	  $$ = scm_cons ($3, $2);
	}
	| EXPECT_SCM markup_command_list_arguments embedded_scm {
	  $$ = check_scheme_arg (parser, @3, $3, $2, $1);
	}
	| EXPECT_NO_MORE_ARGS {
	  $$ = SCM_EOL;
	}
	;

markup_command_list_arguments:
	markup_command_basic_arguments { $$ = $1; }
	| EXPECT_MARKUP markup_command_list_arguments markup {
	  $$ = scm_cons ($3, $2);
	}
	;

markup_partial_function:
	MARKUP_FUNCTION markup_arglist_partial
	{
		$$ = scm_list_1 (scm_cons ($1, scm_reverse_x ($2, SCM_EOL)));
	}
	| markup_head_1_list MARKUP_FUNCTION markup_arglist_partial
	{
		$$ = scm_cons (scm_cons ($2, scm_reverse_x ($3, SCM_EOL)),
			       $1);
	}
	;

markup_arglist_partial:
	EXPECT_MARKUP markup_arglist_partial
	{
		$$ = $2;
	}
	| EXPECT_SCM markup_arglist_partial
	{
		$$= $2;
	}
	| EXPECT_MARKUP markup_command_list_arguments
	{
		$$ = $2;
	}
	| EXPECT_SCM markup_command_list_arguments
	{
		$$ = $2;
	}
	;

markup_head_1_item:
	MARKUP_FUNCTION EXPECT_MARKUP markup_command_list_arguments {
	  $$ = scm_cons ($1, scm_reverse_x ($3, SCM_EOL));
	}
	;

markup_head_1_list:
	markup_head_1_item	{
		$$ = scm_list_1 ($1);
	}
	| markup_head_1_list markup_head_1_item	{
		$$ = scm_cons ($2, $1);
	}
	;

simple_markup:
	STRING {
		$$ = make_simple_markup ($1);
	}
	| SCORE {
		parser->lexer_->push_note_state (Lily::pitchnames);
	} '{' score_body '}' {
		Score *sc = unsmob<Score> ($4);
		sc->origin ()->set_spot (@$);
		if (sc->defs_.empty ()) {
			Output_def *od = get_layout (parser);
			sc->add_output_def (od);
			od->unprotect ();
		}
		$$ = scm_list_2 (Lily::score_markup, $4);
		parser->lexer_->pop_state ();
	}
	| MARKUP_FUNCTION markup_command_basic_arguments {
		$$ = scm_cons ($1, scm_reverse_x ($2, SCM_EOL));
	}
	| markup_scm MARKUP_IDENTIFIER
	{
		$$ = $2;
	}
	;

markup:
	markup_head_1_list simple_markup
	{
		$$ = scm_car (MAKE_SYNTAX (composed_markup_list,
					   @2, $1, scm_list_1 ($2)));
	}
	| simple_markup	{
		$$ = $1;
	}
	;

%%

void
Lily_parser::set_yydebug (bool x)
{
	yydebug = x;
}

SCM
Lily_parser::do_yyparse ()
{
	return scm_c_with_fluid (Lily::f_parser,
				 self_scm (),
				 do_yyparse_trampoline,
				 static_cast <void *>(this));
}

SCM
Lily_parser::do_yyparse_trampoline (void *parser)
{
	SCM retval = SCM_UNDEFINED;
	yyparse (static_cast <Lily_parser *>(parser), &retval);
	return retval;
}



/*

It is a little strange to have this function in this file, but
otherwise, we have to import music classes into the lexer.

*/
int
Lily_lexer::try_special_identifiers (SCM *destination, SCM sid)
{
	if (unsmob<Book> (sid)) {
		Book *book =  unsmob<Book> (sid)->clone ();
		*destination = book->self_scm ();
		book->unprotect ();

		return BOOK_IDENTIFIER;
	} else if (scm_is_number (sid)) {
		*destination = sid;
		return NUMBER_IDENTIFIER;
	} else if (unsmob<Context_def> (sid))
	{
		*destination = unsmob<Context_def> (sid)->clone ()->unprotect ();
		return SCM_IDENTIFIER;
        } else if (unsmob<Context_mod> (sid)) {
                *destination = unsmob<Context_mod> (sid)->smobbed_copy ();
                return SCM_IDENTIFIER;
	} else if (Music *mus = unsmob<Music> (sid)) {
		mus = mus->clone ();
		*destination = mus->self_scm ();
		bool is_event = mus->is_mus_type ("post-event");
		mus->unprotect ();
		return is_event ? EVENT_IDENTIFIER : MUSIC_IDENTIFIER;
	} else if (unsmob<Pitch> (sid)) {
		*destination = unsmob<Pitch> (sid)->smobbed_copy ();
		return PITCH_IDENTIFIER;
	} else if (unsmob<Duration> (sid)) {
		*destination = unsmob<Duration> (sid)->smobbed_copy ();
		return DURATION_IDENTIFIER;
	} else if (unsmob<Output_def> (sid)) {
		*destination = unsmob<Output_def> (sid)->clone ()->unprotect ();
		return SCM_IDENTIFIER;
	} else if (unsmob<Score> (sid)) {
		*destination = unsmob<Score> (sid)->clone ()->unprotect ();
		return SCM_IDENTIFIER;
	} else if (scm_is_pair (sid)
		   && scm_is_pair (scm_car (sid))
		   && scm_is_true (Lily::key_p (scm_caar (sid)))) {
		*destination = sid;
		return LOOKUP_IDENTIFIER;
	}
	return -1;
}

SCM
get_next_unique_context_id ()
{
	return scm_from_ascii_string ("$uniqueContextId");
}


SCM
get_next_unique_lyrics_context_id ()
{
	static int new_context_count;
	char s[128];
	snprintf (s, sizeof (s)-1, "uniqueContext%d", new_context_count++);
	return scm_from_ascii_string (s);
}

// check_scheme_arg checks one argument with a given predicate for use
// in an argument list and throws a syntax error if it is unusable.
// The argument is prepended to the argument list in any case.  After
// throwing a syntax error, the argument list is terminated with #f as
// its last cdr in order to mark it as uncallable while not losing
// track of its total length.
//
// There are a few special considerations: if optional argument disp
// is given (otherwise it defaults to SCM_UNDEFINED), it will be used
// instead of arg in a prospective error message.  This is useful if
// arg is not the actual argument but rather a transformation of it.
//
// If arg itself is SCM_UNDEFINED, the predicate is considered false
// and an error message using disp is produced unconditionally.

SCM check_scheme_arg (Lily_parser *parser, Input loc,
		      SCM arg, SCM args, SCM pred, SCM disp)
{
	if (SCM_UNBNDP (arg))
		args = scm_cons (disp, args);
	else {
		args = scm_cons (arg, args);
		if (scm_is_true (scm_call_1 (pred, arg)))
			return args;
	}
	scm_set_cdr_x (scm_last_pair (args), SCM_EOL);
	MAKE_SYNTAX (argument_error, loc, scm_length (args), pred,
		     SCM_UNBNDP (disp) ? arg : disp);
	scm_set_cdr_x (scm_last_pair (args), SCM_BOOL_F);
	return args;
}

SCM loc_on_music (Lily_parser *parser, Input loc, SCM arg)
{
	if (Music *m = unsmob<Music> (arg))
	{
		m = m->clone ();
		m->set_spot (parser->lexer_->override_input (loc));
		return m->unprotect ();
	}
	return arg;
}

SCM
try_string_variants (SCM pred, SCM str)
{
	// a matching predicate is always ok
	if (scm_is_true (scm_call_1 (pred, str)))
		return str;
	// a symbol may be interpreted as a list of symbols if it helps
	if (scm_is_true (Lily::key_p (str))) {
		str = scm_list_1 (str);
		if (scm_is_true (scm_call_1 (pred, str)))
			return str;
		return SCM_UNDEFINED;
	}

	// If this cannot be a string representation of a symbol list,
	// we are through.

	if (!is_regular_identifier (str, true))
		return SCM_UNDEFINED;

	str = scm_string_split (str, SCM_MAKE_CHAR ('.'));
	for (SCM p = str; scm_is_pair (p); p = scm_cdr (p))
		scm_set_car_x (p, scm_string_split (scm_car (p),
						    SCM_MAKE_CHAR (',')));
	str = scm_append_x (str);
	for (SCM p = str; scm_is_pair (p); p = scm_cdr (p))
		scm_set_car_x (p, scm_string_to_symbol (scm_car (p)));

	// Let's attempt the symbol list interpretation first.

	if (scm_is_true (scm_call_1 (pred, str)))
		return str;

	// If there is just one symbol in the list, we might interpret
	// it as a single symbol

	if (scm_is_null (scm_cdr (str)))
	{
		str = scm_car (str);
		if (scm_is_true (scm_call_1 (pred, str)))
			return str;
	}

	return SCM_UNDEFINED;
}

bool
is_regular_identifier (SCM id, bool multiple)
{
  if (!scm_is_string (id))
	  return false;

  string str = ly_scm2string (id);

  bool middle = false;

  for (string::iterator it=str.begin(); it != str.end (); it++)
  {
	  int c = *it & 0xff;
	  if ((c >= 'a' && c <= 'z')
	      || (c >= 'A' && c <= 'Z')
	      || c > 0x7f)
		  middle = true;
	  else if (middle && (c == '-' || c == '_' || (multiple &&
						       (c == '.' || c == ','))))
		  middle = false;
	  else
		  return false;
  }
  return middle;
}

SCM
make_music_from_simple (Lily_parser *parser, Input loc, SCM simple)
{
	if (unsmob<Music> (simple))
		return simple;
	if (parser->lexer_->is_note_state ()) {
		if (scm_is_symbol (simple)) {
			Music *n = MY_MAKE_MUSIC ("NoteEvent", loc);
			n->set_property ("duration", parser->default_duration_.smobbed_copy ());
			n->set_property ("drum-type", simple);
			return n->unprotect ();
		}
		if (unsmob<Pitch> (simple)) {
			Music *n = MY_MAKE_MUSIC ("NoteEvent", loc);
			n->set_property ("duration", parser->default_duration_.smobbed_copy ());
			n->set_property ("pitch", simple);
			return n->unprotect ();
		}
		SCM d = simple;
		if (scm_is_integer (simple))
			d = make_duration (simple);
		if (unsmob<Duration> (d)) {
			Music *n = MY_MAKE_MUSIC ("NoteEvent", loc);
			n->set_property ("duration", d);
			return n->unprotect ();
		}
		return simple;
	} else if (parser->lexer_->is_lyric_state ()) {
		if (Text_interface::is_markup (simple))
			return MAKE_SYNTAX (lyric_event, loc, simple,
					    parser->default_duration_.smobbed_copy ());
	} else if (parser->lexer_->is_chord_state ()) {
		if (unsmob<Pitch> (simple))
			return MAKE_SYNTAX
				(event_chord,
				 loc,
				 make_chord_elements (loc, simple,
						      parser->default_duration_.smobbed_copy (),
						      SCM_EOL));
	}
	return simple;
}

Music *
make_music_with_input (SCM name, Input where)
{
       Music *m = make_music_by_name (name);
       m->set_spot (where);
       return m;
}

SCM
make_simple_markup (SCM a)
{
	return a;
}

SCM
make_duration (SCM d, int dots, SCM factor)
{
	Duration k;

	if (Duration *dur = unsmob<Duration> (d)) {
		if (!dots && SCM_UNBNDP (factor))
			return d;
		k = *dur;
		if (dots)
			k = Duration (k.duration_log (), k.dot_count () + dots)
				.compressed (k.factor ());
	} else {
		int t = scm_to_int (d);
		if (t > 0 && (t & (t-1)) == 0)
			k = Duration (intlog2 (t), dots);
		else
			return SCM_UNDEFINED;
	}

	if (!SCM_UNBNDP (factor))
		k = k.compressed (ly_scm2rational (factor));

	return k.smobbed_copy ();
}

SCM
make_chord_step (SCM step_scm, Rational alter)
{
	Pitch m (0, scm_to_int (step_scm) - 1, alter);

	// Notename/octave are normalized
	if (m.get_notename () == 6)
		m = m.transposed (Pitch (0, 0, FLAT_ALTERATION));

	return m.smobbed_copy ();
}


SCM
make_chord_elements (Input loc, SCM pitch, SCM dur, SCM modification_list)
{
	SCM res = Lily::construct_chord_elements (pitch, dur, modification_list);
	for (SCM s = res; scm_is_pair (s); s = scm_cdr (s))
	{
		unsmob<Music> (scm_car (s))->set_spot (loc);
	}
	return res;
}

int
yylex (YYSTYPE *s, YYLTYPE *loc, Lily_parser *parser)
{
	Lily_lexer *lex = parser->lexer_;

	lex->lexval_ = s;
	lex->lexloc_ = loc;
	int tok = lex->pop_extra_token ();
	if (tok >= 0)
		return tok;
	lex->prepare_for_next_token ();
	return lex->yylex ();
}