summaryrefslogtreecommitdiff
path: root/support/splint/tex/yymisc.sty
blob: b961135b0233b6e5fe9361b2f995baeaf39a35e8 (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
% Copyright 2012-2020, Alexander Shibakov
% This file is part of SPLinT
%
% SPLinT 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.
%
% SPLinT 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 SPLinT.  If not, see <http://www.gnu.org/licenses/>.

% `data structure access' macros: picking the n-th undelimited parameter
% in a parameter list inside a token register
% it is assumed that none of the arguments is \end, and that there are enough
% parameters to pick the desired one

\def\getfirst#1\to#2{%
    #2\expandafter\expandafter\expandafter{\expandafter\g@tfirst\the#1\end}%
}

\def\g@tfirst#1#2\end{#1}

\def\getsecond#1\to#2{%
    #2\expandafter\expandafter\expandafter{\expandafter\g@tsecond\the#1\end}%
}

\def\g@tsecond#1#2#3\end{#2}

\def\getthird#1\to#2{%
    #2\expandafter\expandafter\expandafter{\expandafter\g@tthird\the#1\end}%
}

\def\g@tthird#1#2#3#4\end{#3}

\def\getfourth#1\to#2{%
    #2\expandafter\expandafter\expandafter{\expandafter\g@tfourth\the#1\end}%
}

\def\g@tfourth#1#2#3#4#5\end{#4}

\def\getfifth#1\to#2{%
    #2\expandafter\expandafter\expandafter{\expandafter\g@tfifth\the#1\end}%
}

\def\g@tfifth#1#2#3#4#5#6\end{#5}

\def\getsixth#1\to#2{%
    #2\expandafter\expandafter\expandafter{\expandafter\g@tsixth\the#1\end}%
}

\def\g@tsixth#1#2#3#4#5#6#7\end{#6}

\def\getseventh#1\to#2{%
    #2\expandafter\expandafter\expandafter{\expandafter\g@tseventh\the#1\end}%
}

\def\g@tseventh#1#2#3#4#5#6#7#8\end{#7}

\def\geteightth#1\to#2{%
    #2\expandafter\expandafter\expandafter{\expandafter\g@teightth\the#1\end}%
}

\def\g@teightth#1#2#3#4#5#6#7#8#9\end{#8}

\def\getninth#1\to#2{% the `.' is necessary since \TeX's scanning mechanism will
                     % strip any potential braces surrounding the last parameter
                     % (assuming there are exactly nine) twice:
                     %  o first, expanding \g@tninth,
                     %  o second, expanding \g@tfirst
    #2\expandafter\expandafter\expandafter{\expandafter\g@tninth\the#1.\end}%
    #2\expandafter\expandafter\expandafter{\the#2}%
}

\def\g@tninth#1#2#3#4#5#6#7#8#9\end{\g@tfirst#9\end}

\def\gettenth#1\to#2{% no need for `.' (or any other placeholder) here,
                     % since the #9-th parameter to \g@ttenth is a list of
                     % at least two parameters itself, thus any existing braces
                     % have survived intact
    #2\expandafter\expandafter\expandafter{\expandafter\g@ttenth\the#1\end}%
    #2\expandafter\expandafter\expandafter{\the#2}%
}

\def\g@ttenth#1#2#3#4#5#6#7#8#9\end{\g@tsecond#9\end}

% removing the first element of a token register if it has more than one;

\def\sansfirst#1{%
    \expandafter\s@nsfirst\expandafter{\the#1}{#1}%
}

\def\s@nsfirst#1#2{%
    \expandafter\s@nsf@rst\expandafter{\eatone#1}{#2}%
}

\def\s@nsf@rst#1#2{%
    \yystringempty{#1}{}{%
        #2{#1}%
    }%
}

% a version of the macro above that expands to the original argument with the first element removed

\def\sansfirstx#1{%
    \expandafter\yystringempty\expandafter{\eatone#1}{#1}{\eatone#1}%
}

% string replacement: all arguments are registers, nothing is expanded, no \next is defined
% note that this is not a greedy replacement: this could be arranged with a more sophisticated macro
% also note that the string being replaced cannot have any braces in it

\newif\ifyytracereplacements
\newif\ifyyreplaced

\yytracereplacementstrue

\def\yyreplacestring#1\in#2\with#3{%
      \expandafter\def\expandafter\r@placestring\expandafter##\expandafter1\the#1##2\end{%
          \def\r@placestring{##2}% is this the string at the very end?
          \ifx\r@placestring\empty % then it is the one we inserted,
                                   % report
              \yyreplacedfalse
              \ifyytracereplacements
                  \errmessage{string <\the#1> not present in \the#2}% do not change the register if the string is not there
              \fi
          \else % remove the extra copy of #1\end at the end
              \yyreplacedtrue
              \expandafter#2\expandafter\expandafter\expandafter
                  {\expandafter\r@plac@string\expandafter{\the#3}{##1}##2\end}%
      \fi}% end of \r@placestring definition
      \expandafter\def\expandafter\r@plac@string
          \expandafter##\expandafter1%
          \expandafter##\expandafter2%
          \expandafter##\expandafter3%
          \the#1\end{##2##1##3}%
      \expandafter\expandafter\expandafter\r@placestring\expandafter\the\expandafter#2\the#1\end
}

% creating a sequence containing all pairs from the two given sequences;
% the (long) string produced by the \diagprod macro
% lists all the elements so that each ordered pair ab where a and b are
% different elements from each of the two sets appears exactly once;
% a simple strategy for creating such strings is to build them recursively so that if
% S is a string that lists n values in this manner and s is a new item type,
% start with sSs and then add each symbol from S on either side 
% in such a way that among any two consecutive symbols exactly one is s

% these macros are used to create switch statements that use such ordered pairs
% and are not particularly general or robust; 
% they are supposed to be used once in the setup stage;
% the assumption made by these mactos is that `.' or `;' never appear as elements 
% of the two sets

\def\gnxtelem#1\to#2\and#3{%
    \expandafter\gnxtel@m\expandafter#2\expandafter#3\the#1.;%
}

\def\gnxtel@m#1#2#3#4;{%
    \def\next{#4}%
    \ifx\next\empty
        #1{}%
    \else
        #1{#3}%
        \gnxt@l@m#2#4%
    \fi
}

\def\gnxt@l@m#1#2.{%
    #1{#2}%
}

\def\pairup{%
    \gnxtelem\toksa\to\toksc\and\toksa % get the first remaining element of set A (\toksa)
    \edef\elemofA{\the\toksc}%
    \ifx\elemofA\empty % no more elements in A
         \let\next\relax
    \else
        \ifx\elemofA\lastelemofB % the current element of A is the same as the last element of B
            \edef\next{\the\toksa}%
            \ifx\next\empty % it is the last remaining element of A
                \expandafter\p@ir@p\the\toksb.% form all pair of \the\toksc with elements in B except the first one
            \else
                \concat\toksa\toksc % move it to the end of A
            \fi
            \let\next\pairup
        \else
            \expandafter\pair@p\the\toksb.% form all pairs of \the\toksc with elements in B
            \let\next\pairup
        \fi
    \fi
    \next
}

\def\dotcontainer{.}

\def\pair@p#1{%
    \def\next{#1}%
    \ifx\next\elemofA % the next element of B is the current element of A
        \ifx\next\lastelemofB % it is the last element of B
                              % we can arrive here only if 1) B has more than one element and 
                              % 2) \elemofA is the last remaining element of A
            \edef\next{\toksd{\the\toksd\the\tokse}}\next
            \let\next\eatone % eat the remaining dot     
        \else
            \removeelem\elemofA\from\toksb % remove it from B
            \let\next\pair@p
        \fi
    \else
        \ifx\next\dotcontainer % no more elements in B
            \let\next\relax
        \else
            \toksf{#1}%
            \edef\next{\toksd{\the\toksd\the\toksc\the\toksf}}\next
            %\showthe\toksd
            \let\next\pair@p
        \fi
    \fi
    \next
}

\def\p@ir@p#1{%
    \def\next{#1}%
    \ifx\next\elemofA % the next element of B is the current element of A
                      % this can only happen if B consists of a single element
        \let\next\eatone
    \else
        \toksf{#1}%
        \edef\next{\toksd{\the\toksd\the\toksf}}\next
        \let\next\pair@p
    \fi
    \next
}

\def\removeelem#1\from#2{% #1 should be a sequence containing the element
                         % #2 is the token register
    \expandafter\def\expandafter\r@moveelem\expandafter##\expandafter1#1##2.{%
        \def\next{##2}%
        \ifx\next\empty % there was no such element
            \errmessage{Could not find \expandafter\string#1 in \the#2}%
        \else
            \expandafter\def\expandafter\r@mov@elem\expandafter####\expandafter1#1{%
                #2{##1####1}%
            }\r@mov@elem##2%
       \fi
    }%
    \expandafter\expandafter\expandafter\r@moveelem\expandafter\the\expandafter#2#1.%
}

% the intersection of A and B should be at the end of A:

\def\pushintersect{%
    \gnxtelem\toksf\to\toksc\and\toksf % get the first remaining element of set A (\toksa)
    \edef\next{\the\toksc}%
    \ifx\next\empty % there are no elements left in A
        \let\next\relax
    \else
        \expandafter\def\expandafter\p@shintersect\expandafter##\expandafter1\the\toksc##2.{%
            \def\next{##2}%
            \ifx\next\empty % there was no such element in the other set
                \edef\next{\toksa{\the\toksc\the\toksa}}\next % append it to the front
            \else
                \edef\next{\toksa{\the\toksa\the\toksc}}\next % append it to the back
            \fi
        }%
        \expandafter\expandafter\expandafter\p@shintersect\expandafter\the\expandafter\toksb\the\toksc.%
        \let\next\pushintersect
    \fi
    \next
}

\def\diagprod#1#2\in#3{%
    \toksa\expandafter{#1}%
    \toksb\expandafter{#2}%
    \gnxtelem\toksb\to\tokse\and\toksb % \tokse is a selected element in set B (\toksb)
    \concat\toksb\tokse % now \tokse is the last element of B 
    \toksf\toksa\toksa{}%
    \pushintersect % prepare the sequence representing set A so that the intersection elements are in the tail
    \edef\lastelemofB{\the\tokse}% the last element of B
    \toksd\expandafter{\the\tokse}% future sequence pairs
    \pairup
    \edef#3{\the\toksd}%
}

% namespace management for macros
% note that if one of the sequences were made \let\name. the macros would break;
% this can be fixed by using a more sophisticated comparison but was decided against
% in the interest of efficiency; the old version (that used a control sequence
% as a `stop marker') was more prone to this bug.

% the next sequence is not merely a convenient abbreviation; it is useful if one
% wants to look at an `alternative' value stored for the sequence without restoring
% it to the curent namespace; it also sets up the naming convention for namespaces

\def\restorecsname#1#2{% get the name of the sequence in storage
                       % note that #2 can be a string beginning with an arbitrary
                       % character as a placeholder for the escape character
    '#1'[\expandafter\eatone\string#2]%
}

% save macros listed in #2 in namespace #1

\def\savecs#1#2{\s@vecs{#1}#2.}

\def\s@vecs#1#2{%
    \ifx#2.%
        \yybreak{}%
    \else
        \yybreak{%
            \expandafter\let\csname\restorecsname{#1}{#2}\endcsname#2%
            \s@vecs{#1}%
        }%
    \yycontinue
}

% similar to the macro above but the list is a control sequence

\def\savecslist#1#2{%
    \expandafter\s@vecslist\expandafter{#2}{#1}%
}

\def\s@vecslist#1#2{%
    \savecs{#2}{#1}%
}

\def\restorecs#1#2{\r@storecs{#1}#2.}

\def\r@storecs#1#2{%
    \ifx#2.%
        \yybreak{}%
    \else
        \yybreak{%
            \expandafter\let\expandafter#2\csname\restorecsname{#1}{#2}\endcsname
            \r@storecs{#1}%
        }%
    \yycontinue
}

\def\restorecslist#1#2{%
    \expandafter\r@storecslist\expandafter{#2}{#1}%
}

\def\r@storecslist#1#2{%
    \restorecs{#2}{#1}%
}

\def\hidecs#1{\h@decs#1.}

\def\h@decs#1{%
  \ifx#1.%
      \yybreak{}%
  \else
      \yybreak{%
          \let#1\relax
          \h@decs
      }%
  \yycontinue
}

\def\hidecslist#1{\expandafter\hidecs\expandafter{#1}}

\def\savehcs#1#2{\savecs{#1}{#2}\hidecs{#2}}

\def\savehcslist#1#2{%
    \expandafter\s@vehcslist\expandafter{#2}{#1}%
}

\def\s@vehcslist#1#2{%
    \savehcs{#2}{#1}%
}

% a twist on the macros above: save control sequences with a postfix

\def\savecsx#1#2{\s@vecsx{#1}#2.} % there is no \savecsxlist macro

\def\s@vecsx#1#2{% #1 is the namespace, #2 is a control sequence without the postfix or prefix
  \ifx#2.%
      \yybreak{}%
  \else
      \yybreak{%
          \expandafter\s@vecsxlet\csname\expandafter\defprefix\expandafter\eatone\string#2\defpostfix\endcsname#2{#1}%
          \s@vecsx{#1}%
      }%
  \yycontinue
}

\def\s@vecsxlet#1#2#3{% #1 is the control sequence augmented by prefix and postfix,
                      % #2 is the control sequence without prefix or postfix
                      % #3 is the namespace
    \expandafter\let\expandafter#1\csname\restorecsxname{#3}{#2}\endcsname
}

\def\restorecsx#1#2{\r@storecsx{#1}#2.}

% see remarks about \restorecsname above

\def\restorecsxname#1#2{%
    '#1'[\expandafter\defprefix\expandafter\eatone\string#2\defpostfix]%
}

\def\r@storecsx#1#2{%
  \ifx#2.%
      \yybreak{}%
  \else
      \yybreak{%
          \expandafter\r@storecsxlet\expandafter#2\csname\restorecsxname{#1}{#2}\endcsname
          \r@storecsx{#1}%
      }%
  \yycontinue
}

\def\restorecsxlist#1#2{%
    \expandafter\r@storecsxlist\expandafter{#2}{#1}%
}

\def\r@storecsxlist#1#2{%
    \restorecsx{#2}{#1}%
}

\def\r@storecsxlet#1{%
  \expandafter\let\csname\expandafter\defprefix\expandafter\eatone\string#1\defpostfix\endcsname
}

\def\hidecsx#1{\h@decsx#1.}

\def\h@decsx#1{%
  \ifx#1.%
      \yybreak{}%
  \else
      \yybreak{%
          \expandafter\let\csname\expandafter\defprefix\expandafter\eatone\string#1\defpostfix\endcsname\relax
          \h@decsx
      }%
  \yycontinue
}

\def\savehcsx#1#2{\savecsx{#1}{#2}\hidecsx{#2}}

\def\defx#1{% defining sequences as above
  \toksa\expandafter{%
  \csname\expandafter\defprefix\expandafter\eatone\string#1\defpostfix\endcsname}%
  \afterassignment\d@fx
  \expandafter\def\the\toksa
}

\def\d@fx#1{%
  \toksb{#1}\edef\next{\noexpand\savehcs{\the\toksb}\the\toksa}\next
}

\def\defy#1#2#{% in addition to the definition of the contorl sequence
               % in the appropriate namespace, this macro adds a
               % preamble, a postamble and a `this' type macro; this
               % will mostly be used with indexing \TeX\ control sequences
  \toksf{\def\thisname{#1}\edef\thisnamex{\expandafter\eatone\string#1}}%
  \toksa\expandafter{%
      \csname\expandafter\defprefix\expandafter\eatone\string#1\defpostfix\endcsname}%
  \toksc\expandafter{\expandafter\def\the\toksa#2}%
  \afterassignment\d@f@
  \toksd=%
}

\def\d@f@{%
   \tokse{\defypreamble}%
   \concatl\tokse\toksd
   \concatl\toksf\toksd
   \tokse{\defypostamble}%
   \concat\toksd\tokse
   \toksd\expandafter{\expandafter{\the\toksd}}%
   \concat\toksc\toksd
   \afterassignment\d@fy
   \the\toksc
}

\def\d@fy#1{%
  \toksb{#1}\edef\next{\noexpand\savehcs{\the\toksb}\the\toksa}\next
}

% dynamic typing macros

\def\defp#1#2#{% flexible dynamic type checking
    \toksa\expandafter\expandafter\expandafter{\yyuniontag#1}%
    \expandafter\edef\yyuniontag{\the\toksa}% add the sequence to the current union
    \def#1#2{\errmessage{unexpected type: \string#1 in namespace <\currentyyunionnamespace>}}%
    \savecs\parserstrictnamespace#1%
    \toksa{#2}%
    \edef#1{\the\toksa}% save the prototype
    \savecs\parserprototypesnamespace#1%
    \let#1\relax
    \savecs\parserdebugnamespace#1% save the debug sequence for outputting the AST
    \def#1#2%
}

% define the macro with checking of the prototype

\def\defu#1#2#{%
    \restorecs\parserprototypesnamespace#1%    
    \toksa{#2}%
    \edef\next{\the\toksa}%
    \ifx#1\next
        \yybreak{\def#1#2}%
    \else
        \toksb\expandafter{#1}%
        \yybreak{%
            {\newlinechar=`^^J%
            \errhelp{the prototype of #1is from <parser-strict:headers>^^J%
                     you might want to look for a \defp#1line... somewhere}%
            \errmessage{macro definition of \noexpand#1 
                        does not match its prototype:^^J 
                        \the\toksa\space (should be \the\toksb)}%
            }%
        }%
    \yycontinue
}

% define the macro with an automatic prototype

\def\defc#1{%
    \restorecs\parserprototypesnamespace#1%    
    \expandafter\def\expandafter#1#1%
}

\def\t@yyunion#1#2{\def\currentyyunionnamespace{#2}\savecslist{#2}#1}
\def\toyyunion#1{\expandafter\t@yyunion\yyuniontag{#1}}

{\catcode`\ =13 \aftergroup\def\aftergroup\activespace\aftergroup{\aftergroup \aftergroup}}% active space
{\catcode`\% =12 \aftergroup\def\aftergroup\harmlesscomment\aftergroup{\aftergroup%\aftergroup}}% not really a comment
{\catcode`\{ =12 \catcode`\[=1 \aftergroup\def\aftergroup\lbchar\aftergroup[\aftergroup{\aftergroup}}% not really a brace
{\catcode`\} =12 \catcode`\]=2 \aftergroup\def\aftergroup\rbchar\aftergroup{\aftergroup}\aftergroup]]% not really a brace
{\catcode`\_=12 \aftergroup\def\aftergroup\uscoreletter\aftergroup{\aftergroup_\aftergroup}}
{\catcode`\^^M=12 \aftergroup\def\aftergroup\eolletter\aftergroup{\aftergroup^^M\aftergroup}}% end of line, ... not really
{\catcode`\|=0\catcode`\\=12 |aftergroup|def|aftergroup|benignescape|aftergroup{|aftergroup\|aftergroup}}% not an escape
{\catcode`\#=12 \aftergroup\def\aftergroup\hashletter\aftergroup{\aftergroup#\aftergroup}}% not a parameter token
{\catcode`\~=12 \aftergroup\def\aftergroup\safetilde\aftergroup{\aftergroup~\aftergroup}}% inactive tie
{\catcode`\$=12 \aftergroup\def\aftergroup\safemath\aftergroup{\aftergroup$\aftergroup}}% not really a start math character

\let\uscore\_ % the canonical underscore for the fonts that do not have the character

% remove the brackets from the namespace string;
% the parameter is the control sequence that expands to the namespace string

\def\stripbrackets#1{\expandafter\stripbr@ckets#1[]\end}

\def\stripbr@ckets#1[#2]#3\end{\yystringempty{#3}{#1}{#1#2}}

% token name input

\def\doascii#1{%
  \tempca=\z@ \tempcb=\@cclvi
  \let\yyasciicc\empty
  \let\sts\relax
  \loop
      \edef\yyasciicc{\sts{\the\catcode\tempca}\yyasciicc}%
      \catcode\tempca=#1
      \advance\tempca\@ne
  \ifnum\tempca<\tempcb\relax
  \repeat
  \catcode`\ =10 % keep normal spaces (otherwise \string will convert the category to 10)
  %\catcode`\^^M=5 % if newline is to be preserved
  \catcode`\^^M=12 % to match \eolletter
}

\def\undoascii{%
  \tempca=\@cclv
  \let\sts\undoacatcode
  \yyasciicc\def\yyasciicc{}%
}

\def\undoacatcode#1{\catcode\tempca=#1\advance\tempca\m@ne}

% yytname macros

\def\endcontainer{\end}%
\def\aaddname{\addname}

\def\addname{%
    \toksb{}%
    \@ddname
}

\def\@ddname#1{%
    \def\next{#1}%
    \ifx\next\aaddname
        \expandafter\edef\csname term\parsernamespace\the\toksb \endcsname{\the\tempca}%
        \appendtoyytname
        \advance\tempca1\relax
        \let\next\addname
    \else
        \ifx\next\endcontainer
            \expandafter\edef\csname term\parsernamespace\the\toksb \endcsname{\the\tempca}%
            \appendtoyytnamelast
            \let\next\relax
        \else
            \ifnum#1=`\_\relax
                \uccode`\.=#1\relax
                \uppercase{\toksa{.}}%
                \uccode`\.=`\.%
            \else
                \ifnum#1=`\-\relax
                    \toksa{-}%
                \else
                    \ifnum#1=`\ \relax
                        \toksa{ }%
                    \else
                        \uccode`\@=#1\relax
                        \uppercase{\toksa{@}}%
                        \uccode`\@=`\@\relax
                    \fi
                \fi
            \fi
            \concat\toksb\toksa
            \let\next\@ddname
        \fi
    \fi
    \next
}

\def\appendtoyytname{%
    \concat\yytname\toksb
    \toksa{\or}%
    \concat\yytname\toksa
}

\def\appendtoyytnamelast{%
    \concat\yytname\toksb
}

\def\print#1{{\endlinechar=`\^^J\immediate\write16{#1}}}

% token and state setup
% these are defined for a specific (though dynamic) namespace only

\def\tokenset#1#2{%
    \expandafter\edef\csname token\parsernamespace \fgetelemof{yytname}\at{#1}\endcsname{#2}%
}

\def\settokens{%
    \tempca=1\relax
    \loop
        \tempcb=\fgetelemof{yytranslate}\at\tempca\relax % important \relax!
        \tokenset\tempcb{\the\tempca}%
        \advance\tempca\@ne
    \ifnum\YYTRANSLATESIZE>\tempca
    \repeat
    \relax
}

\def\stateset#1#2{%
    \expandafter\def\csname flexstate\parsernamespace #1\endcsname{#2}%
}

% token equivalence for bootstrap

\def\tokeneq#1#2{%
    \toksa{}%
    \numberstochars#2\end
    \toksa\expandafter{\csname token\parsernamespace\the\toksa\endcsname}%
    \toksb\expandafter{\csname token\parsernamespace#1\endcsname}%
    \edef\next{\let\the\toksb\the\toksa}\next
}%

\def\numberstochars#1{%
    \ifx\end#1%
        \yybreak{}%
    \else
        \uccode`.=#1\relax
        \uppercase{\toksa\expandafter{\the\toksa.}}%
        \uccode`.=`\.%
        \yybreak{\numberstochars}%
    \yycontinue
}

\def\numberstocharsandspaces#1{% same as above but turn \number`\ into a real space token
    \ifx\end#1%
        \yybreak{}%
    \else
        \ifnum#1=` \relax
            \yybreak@{%
                \expandafter\expandafter\expandafter
                \toksa\expandafter\expandafter\expandafter{\expandafter\the\expandafter\toksa\space}\numberstocharsandspaces}%
        \else
            \uccode`.=#1\relax
            \uppercase{\toksa\expandafter{\the\toksa.}}%
            \uccode`.=`\.%
        \fi
        \yybreak{\numberstocharsandspaces}%
    \yycontinue
}


% other parser and lexer variables

\newtoks\pinittoks % a token register to hold the code that switches the parser to a different namespace

% tables

\def\newtable@full#1{%
    \expandafter\expandafter\csname newtoks\endcsname\csname #1\parsernamespace\endcsname
    \expandafter\expandafter\expandafter\let\expandafter
        \expandafter\csname #1\endcsname\csname #1\parsernamespace\endcsname
    \appendr\pinittoks{\let\expandafter\noexpand\csname #1\endcsname
        \expandafter\noexpand\csname #1\parsernamespace\endcsname}%
    \csname #1\parsernamespace\endcsname=%
}

\let\newtable\newtable@full % optimization can change this

% constants

\def\constset#1#2{%
    % a \mathchardef would be nicer but it cannot handle negative numbers
    \expandafter\def\csname #1\parsernamespace\endcsname{#2}%
    \expandafter\expandafter\expandafter\let\expandafter
        \expandafter\csname #1\endcsname\csname #1\parsernamespace\endcsname
    \appendr\pinittoks{\let\expandafter\noexpand\csname #1\endcsname
        \expandafter\noexpand\csname #1\parsernamespace\endcsname}%
}

\def\uconstset#1#2{%
    % a \mathchardef for positive constants
    \expandafter\mathchardef\csname #1\parsernamespace\endcsname=#2 %
    \expandafter\expandafter\expandafter\let\expandafter
        \expandafter\csname #1\endcsname\csname #1\parsernamespace\endcsname
    \appendr\pinittoks{\let\expandafter\noexpand\csname #1\endcsname
        \expandafter\noexpand\csname #1\parsernamespace\endcsname}%
}

\def\charset#1#2{%
    \expandafter\chardef\csname #1\parsernamespace\endcsname=#2\relax%
    \expandafter\expandafter\expandafter\let\expandafter
        \expandafter\csname #1\endcsname\csname #1\parsernamespace\endcsname
    \appendr\pinittoks{\let\expandafter\noexpand\csname #1\endcsname
        \expandafter\noexpand\csname #1\parsernamespace\endcsname}%
}

% parser and lexer state control

\def\settokreg#1{% do not create individual token registers for each parser namespace
    \expandafter\ifx\csname #1\endcsname\relax
        \expandafter\expandafter\csname newtoks\endcsname\csname #1\endcsname
    \fi
}

\def\setcntreg#1{% do not create individual count registers for each parser namespace
    \expandafter\ifx\csname #1\endcsname\relax
        \expandafter\expandafter\csname newcount\endcsname\csname #1\endcsname
    \fi
}

\def\setnulstack#1{%
    \expandafter\let\csname #1\parsernamespace\endcsname\empty % for the unoptimized stack
    \appendr\pinittoks{\let\expandafter\noexpand\csname #1\endcsname
        \expandafter\noexpand\csname #1\parsernamespace\endcsname}%
    \setcntreg{#1<count>}% this is only (as well as the only action that is) needed for the accelerated stack
}

\def\setcurrentcs#1{%
    \expandafter\expandafter\expandafter\let\expandafter
        \expandafter\csname #1\parsernamespace\endcsname\csname #1\endcsname
    \appendr\pinittoks{\let\expandafter\noexpand\csname #1\endcsname
        \expandafter\noexpand\csname #1\parsernamespace\endcsname}%
}

% switch macro

\let\stashswitch\setcurrentcs

\def\newparserstate{%
  \setcntreg{yytoken}%
  \setcntreg{yystate}%
  \setcntreg{yyn}%
  \setcntreg{yylen}%
  %
  \setcntreg{yyilen}% depth of stack before an inline action
  % \yyval and \yylval will be token registers
  \settokreg{yyval}%
  \settokreg{yylval}%
  \setnulstack{astarray}% an array of AST nodes
  \setnulstack{yyssa}%
  \setnulstack{yyvsa}%
}

\def\newlexerstate{%
  \settokreg{yytext@}% `dirty' buffer (contains stash and formatting)
  \settokreg{yytext@seen}% read `dirty' buffer
  \settokreg{yytext}%
  \settokreg{yytextseen}%
  \settokreg{yybyte}%
  \settokreg{yysubtext}% stash between characters
  \settokreg{yyfutureyytext}%    token register used to save read text in eob macros
  %
  \settokreg{yytextpure}%      % the registers that serve the same role
  \settokreg{yytextseenpure}%  % as \yytext et al except they collect characters
  \settokreg{yybytepure}%      % with category code 11 (letter) and the same character code
  %
  \settokreg{yyformat}%        % the registers that serve the same role
  \settokreg{yyformatseen}%    % as \yytext et al except they collect format
  \settokreg{yyfbyte}%         % commands
  %
  \settokreg{yystash}%         % the registers that serve the same role
  \settokreg{yystashseen}%     % as \yytext et al except they collect stashed
  \settokreg{yysbyte}%         % tokens
  %
  \setcntreg{yycurrentstate}%
  \setcntreg{yycurrentstatelocal}% a state variable used in eob macros
  \setcntreg{yyc}%
  \setcntreg{yyclocal}% another eob macro variable
  \setcntreg{yyact}%
  \setcntreg{yyg@yyinit}%
  \setcntreg{yyg@yystart}%
  \setcntreg{yyg@yylastacceptingstate}%
  \setcntreg{yycp@}%
  \setcntreg{YYATBOL}%
  \setcntreg{yychar}%
  %
  \setcntreg{yytextlastchar}%
  \setcntreg{yytextseenlastchar}%
  %
  \setcntreg{yyfmark}%        % the last marker in the current token
  \setcntreg{yyfmarklast}%
  \setcntreg{yyfmark@accept}%
  \setcntreg{yysmark}%
  \setcntreg{yysmarklast}%
  \setcntreg{yysmark@accept}%
  \setnulstack{yystatestack}%
  \setcurrentcs{setflexstates}%
  \setcurrentcs{ifyytextbackup}% this is purely to record the recovery command in \pinittoks
}

% use \pinittoks to compose a `parser restore' macro along with 
% \let\yyvsa\yyvsa[parser namespace] and \let\yyssa\yyssa[parser namespace];
% `parser save' macro only has to set up \yy?sa[parser namespace]'s;
%
% this mechanism creates parsers that (barely) survive namespace switches (such as when a local
% parser is used to extract information about the tokens) and does not result in a reentrant parser;
% if a fully reentrant parser is required, either use the macros above to save the contents of
% all the variables by redefining \settokreg and \setcntreg and saving \yy?sa stacks or, better still,
% take advantage of the grouping mechanism provided by \TeX; note that the grouping approach seems
% to be the only viable solution if the stack mechanism is `optimized'

% optimization options

\def\optimizeall{%
        % lexer
    \ifnum\optimization>\z@
        \optimize{yynxt}%
        \optimize{yyaccept}%
        \optimize{yydef}%
        \optimize{yychk}%
        \optimize{yybase}%
        \optimize{yyec}%
        \optimize{yymeta}%
        \tracingstats=\@ne
    \fi
        % parser
    \ifnum\optimization>\@ne
        \optimize{yytranslate}%
        \optimize{yyrone}%
        \optimize{yyrtwo}%
        \optimize{yyrthree}%
        \optimize{yydefact}%
        \optimize{yydefgoto}%
        \optimize{yypact}%
        \optimize{yypgoto}%
        \optimize{yytable}%
        \optimize{yycheck}%
        \optimize{yyprhs}%
        \optimize{yyrhs}%
        \optimize{yytoknum}%
        \optimize{yystos}%
        \optimizetext{yytname}%
    \fi
}

% parser and lexer initialization, token prettyfying

\def\genericparser name: #1, ptables: #2, ltables: #3, tokens: #4, asetup: #5, dsetup: #6, rsetup: #7, optimization: #8;{%
    % parser initialization
    %
    \expandafter\def\csname #1namespace\endcsname{[#1]}%
    \savecs{local-namespace}\parsernamespace
    \expandafter\let\expandafter\parsernamespace\csname #1namespace\endcsname
    \pinittoks{}%
    \input #2 % load main parser table
    \settokens % set the values of all tokens
    \yystringempty{#4}{}{%
        \input #4 % use token equivalence table to set the values of non-string tokens
    }%
    #5% (a)dditional setups
    %
    \input #3 % load lexer tables
    %
    % at this point the macros inside the table files (\newtable, \constset, 
    % \yybigswitch, \stashswitch, \addname, \yydoactionswitch, \setflexstates, 
    % \stateset, \tokeneq) have set up the corresponding stuctures in 
    % the `parser namespace' (e.g. if the parser namespace is `main', 
    % \newtable{yyaccept} created a token register \yyaccept[main]), 
    % assigned the `generic' names to them (to continue 
    % the example above, \newtable does \let\yyaccept\yyaccept[main]) and 
    % recorded the corresponding commands in \pinittoks for future use.
    %
    % lexer state macros are namespace specific (just like token names) 
    % so they have to be set in each namespace.
    %
    \setflexstates
    #8% possible optimization
    %
    % finally, we add the definitions for the variables used in running 
    % the lexer and the parser.
    \newparserstate
    \newlexerstate
    #6% additional (d)ata setup (say, \newlexerstateextra)
    %
    % we record all the commands necessary to switch to the desired namespace 
    % in a convenient macro
    {%
        \toks0{#7}% additional setup before switching namespaces
        \edef\next{%
            \the\toks0 % additional namespace setups
            \let\noexpand\parsernamespace\expandafter\noexpand\csname #1namespace\endcsname 
            \the\pinittoks % restore all the tables, tokens and constants, and stacks
            \let\noexpand\getcurrentparser\expandafter\noexpand\csname to#1parser\endcsname
        }%
        \toks0\expandafter{\next}%
        \edef\next{\toks0{\def\expandafter\noexpand\csname to#1parser\endcsname{\the\toks0}}}\next
        \expandafter
    }\the\toks0
    \restorecs{local-namespace}\parsernamespace
}

\def\genericprettytokens namespace: #1, tokens: #2, correction: #3, host: #4;{%
    \savecs{local-namespace}{\parsernamespace\tokeneq}%
        \yystringempty{#2}{}{%
            \expandafter\let\expandafter\parsernamespace\csname #1namespace\endcsname
            \def\tokeneq##1##2{\prettytoken{##1}}%
            \let\tokenpp\prettytoken
            \input #2 % /* re-use token equivalence table to set the typesetting of tokens */
        }%
        \yystringempty{#3}{}{%
            \expandafter\let\expandafter\parsernamespace\csname #1namespace\endcsname
            \input #3 % input customized typesetting rules for tokens
        }%
        \yystringempty{#4}{}{%
            \expandafter\let\expandafter\hostparsernamespace\csname #4namespace\endcsname
        }%
    \restorecs{local-namespace}{\parsernamespace\tokeneq}%
}

% switch and dfa macros

%% character class checking: #1 is the character, #2 is the character class control sequence

\newif\ifinclass

\def\ifclassof#1\is#2{%
  \tempca#1\relax
  \expandafter\checkclass#2\end
}

\def\checkclass#1{%
  \ifx#1\end
      \let\next\relax
      \inclassfalse
  \else
      \ifnum`#1=\tempca
          \let\next\classend
          \inclasstrue
      \else
          \let\next\checkclass
      \fi
  \fi
  \next
}

\tempca=\catcode`\^^A
\catcode`\^^A=12 % so that the characters inside sequences get the appropriate catcodes

\def\classend#1\end{}

\def\setspecialchars#1{%
  \toksa{}%
  \tempca=`\ %
  \loop
      \advance\tempca\@ne
      \tempcb=\fgetelemof{yytranslate}\at\tempca\relax
      \ifnum\tempcb>\tw@
          \tempcb=\uccode`^^A\uccode`^^A=\tempca
          \uppercase{\edef\next{\toksa{\the\toksa^^A}}}\next
          \uccode`^^A=\tempcb
      \fi
  \ifnum\tempca<\@cclv
  \repeat
  \edef#1{\the\toksa}%
}

\newif\iftraceswitchlabels
\traceswitchlabelstrue

\def\setspecialcharsfrom#1{%
  \toksb\expandafter{#1}%
  \iftraceswitchlabels
      \edef\next{\toksa{\expandafter\noexpand\csname <state: \expandafter\eatone\string#1>\endcsname}}\next
      \expandafter\let\the\toksa\empty
  \else
      \toksa{}%
  \fi
  \tempcb=\uccode`\^^A\relax
  \expandafter\s@tspecialcharsfrom\the\toksb\end
  \edef#1{\the\toksa}%
  \uccode`\^^A=\tempcb
}

\def\s@tspecialcharsfrom{%
  \futurelet\next\s@tsp@cialcharsfrom
}%

\def\s@tsp@cialcharsfrom{%
  \ifcat\space\noexpand\next
      \expandafter
      \s@tsp@cialch@rsfr@m
  \else
      \expandafter
      \s@tsp@cialch@rsfrom
  \fi
}

\def\s@tsp@cialch@rsfr@m{%
  \afterassignment\s@tspecialcharsfrom\let\next= % this is the optional space
}

\def\s@tsp@cialch@rsfrom#1{%
  \toksb{#1}%
  \ifx\next\bgroup % this is an action group
      \edef\next{\toksa{\the\toksa{\the\toksb}}}\next
      \let\next\s@tspecialcharsfrom
  \else
      \let\default\specchardefault
      \iftracedfa % to avoid conflicts while tracing actions
          \tracedfafalse
          \switchon{\the\toksb}\in\speccharswitch
          \tracedfatrue
      \else
          \switchon{\the\toksb}\in\speccharswitch
      \fi           
  \fi
  \next
}%

\def\speccharswitch{%
    \end {%
        \let\next\relax
    }
    \raw \rawcode \classexpand \meanit \statecomment {%
        %
    }
}

\def\specchardefault{%
    \uccode`\^^A\expandafter`\the\toksb
    \uppercase{\edef\next{\toksa{\the\toksa^^A}}}\next
    \let\next\s@tspecialcharsfrom
}

\def\raw#1\raw{%
  \toksb{#1}%
  \concat\toksa\toksb
  \s@tspecialcharsfrom
}

\def\rawcode#1{%
  \ifx#1\rawcode
      \let\next\s@tspecialcharsfrom
  \else
      \uccode`\^^A=#1\relax
      \uppercase{\edef\next{\toksa{\the\toksa^^A}}}\next
      \let\next\rawcode
  \fi
  \next
}

\def\classexpand#1{%
  \toksb\expandafter{#1}%
  \concat\toksa\toksb
  \s@tspecialcharsfrom
}

\def\meanit#1{%
  \toksb\expandafter{\meaning#1}%
  \concat\toksa\toksb
  \s@tspecialcharsfrom
}

\def\statecomment#1\statecomment{\edef\next{\toksa{\the\toksa
    \expandafter\noexpand\csname (#1)\endcsname}}\next\s@tspecialcharsfrom}

\def\putother#1\in#2{%
  \tempca=\catcode`\^^A\relax
  \tempcb=\uccode`\^^A\relax
  \uccode`\^^A=#1\relax
  \uppercase{\edef\next{#2{^^A}}}\next
  \uccode`\^^A=\tempcb         
}

% listing all the rules in a macro

\def\listrules{%
  \tempcb=\tw@ % start with a user-set rule
  \loop
      \listrule
  \ifnum\tempcb<\YYNRULES\relax
      \advance\tempcb\@ne
  \repeat
}

\catcode`\$=11

\def\listrule{%
  \tempcc=\fgetelemof{yyrone}\at\tempcb\relax % get the symbol this rule derives
  \edef\next{\toksa{\fgetelemof{yytname}\at\tempcc}}\next
  \expandafter\checkforimplicit\the\toksa $@\end%$
  \ifimplicit
      \edef\next{\toksc{\noexpand\ih{\the\toksa}}}\next
      \edef\next{\toksa{\noexpand\implicitrule[\the\toksa]}}\next
  \else
      \edef\next{\toksa{\the\tempcc}}\next
      \toksc{}%
  \fi
  \appendr\toksa{:}%
  \toksb{}%
  \tempcc=\fgetelemof{yyprhs}\at\tempcb\relax
  \tempcd=\fgetelemof{yyrhs}\at\tempcc\relax
  \listr@le
  \edef\next{\newsymswitch{\the\newsymswitch\the\toksa\def{\the\toksc:\the\tempcb}\noexpand\ruleor}}\next
}

\def\implicitsymbol#1{}%

\newif\ifimplicit

\def\listr@le{%
  \ifnum\tempcd>\m@ne
      \ifnum\tempcd<\YYNTOKENS\relax % it is a terminal
          \edef\next{\toksb{\the\tempcd}}\next
      \else
          \edef\next{\toksb{\fgetelemof{yytname}\at\tempcd}}\next
          \expandafter\checkforimplicit\the\toksb $@\end%$
          \ifimplicit
              \appendl\toksc{\noexpand\imn{\the\toksb}}
              \toksb{@implicit@}%
          \else
              \edef\next{\toksb{\the\tempcd}}\next
          \fi
      \fi
      \appendr\toksa{ }\concat\toksa\toksb
      \advance\tempcc\@ne
      \tempcd=\fgetelemof{yyrhs}\at\tempcc\relax
      \let\next\listr@le
  \else % this is the end of the rhs
      \let\next\relax
  \fi
  \next
}

\def\checkforimplicit#1$@#2\end{% $
  \def\next{#1}%
  \ifx\next\empty
      \implicittrue
  \else
      \implicitfalse
  \fi
}
\catcode`\$=3

% symbol switch macros

\newtoks\oneimplicitrule

% inline explicit symbolic names

\def\itermstack#1#2{%
  \toksa=\oneimplicitrule
  \appendl\toksa{\noexpand\lhs{#1}{#2}}%
  \expandafter
    \yypush
  \expandafter
    {\the\toksa}\on\yyirulestack
  \appendr\oneimplicitrule{\noexpand\implicitterm{#1}{#2}}%
}

\def\makeisymnames#1:#2\end{%
  \tempcc=#2\relax
  \def\next{#1}%
  \ifx\next\empty
      \relax
  \else
      \let\imn\assigninames
  \fi
  #1%
}

\def\assigninames#1{%
  \yypop\yyirulestack\into\toksc
  \def\sts##1{##1}%
  \savehcs{symn}{\term\lhs\implicitterm\onerule}%
  \edef\next{\noexpand\onerule{\the\toksc}}%
  \edef\next{\toksc{\next}}\next
  \toksb{\implicitrule[#1]:\def}%
  \yyreplacestring\toksb\in\newsymswitch\with\toksc
  \restorecs{symn}{\term\lhs\implicitterm\onerule}%
}

\def\oneruleid#1#2{%
  \oneimplicitrule{}%
  \yyinitstack\yyirulestack
  \toksb{#1}%
  #1%
  \makeisymnames#2\end
}

\def\termexsymname#1#2{%
  \appendr\oneimplicitrule{\noexpand\term{#1}{#2}}%
}

\def\iruleplaceholder#1:\def#2{}

\def\setexplicitinlinerules#1{%
  \let\onerule\oneruleid
  \let\term\termexsymname
  \let\lhs\eattwo
  \let\implicitterm\itermstack
  \let\ruleor\relax
  \let\ih\eatone
  \let\imn\eatone
  \let\implicitrule\iruleplaceholder
  \the#1% set explicit names  
}

\catcode`\^^A=\tempca

\def\makesymrefs#1{% #1 is the symbolic rule switch
    \let\ruleor\or
    \nameflagtoks{}%
    \edef\next{\setsncommands{\noexpand\or\space\harmlesscomment\space (rule 0)^^J%
                              \noexpand\or\space\harmlesscomment\space (rule 1)^^J}}\next
    \edef\next{\unsetsncommands{\noexpand\or\space\harmlesscomment\space (rule 0)^^J%
                                \noexpand\or\space\harmlesscomment\space (rule 1)^^J}}\next
    \yyn=\tw@
    \loop
        \yylen=\fgetelemof{yyrtwo}\at\yyn\relax
        \ifnum\yylen>\z@
        \else 
            \yylen=\fgetelemof{yyrthree}\at\yyn\relax
        \fi
        \xm@kesymrefs#1%
        \xm@k@symrefs#1%
        \addseparator
    \ifnum\yyn<\YYNRULES
        \advance\yyn\@ne
    \repeat
}

% these macros are here to hide \else and \fi tokens

\def\xm@kesymrefs#1{\expandafter\m@kesymrefs\expandafter\yyn\the#1\else\fi} % explicit names
\def\xm@k@symrefs#1{\expandafter\m@k@symrefs\expandafter\yyn\the#1\else\fi} % implicit names

\def\m@kesymrefs#1{%
  \let\onerule\rulesymsetup
  \ifcase#1\or\or % skip the first two rules
}

\def\m@k@symrefs#1{%
  \let\onerule\r@lesymsetup
  \ifcase#1\or\or % skip the first two rules
}

\def\addseparator{%
    \sprintrule\yyn\to\toksa
    \edef\next{\setsncommands{\the\setsncommands\noexpand\or\space\harmlesscomment\the\toksa^^J}}\next
    \edef\next{\unsetsncommands{\the\unsetsncommands\noexpand\or\space\harmlesscomment\the\toksa^^J}}\next
    \the\nameflagtoks
    \nameflagtoks{}%
}

\def\rulesymsetup#1#2{%
  \let\term\termsymname
  \let\lhs\lhssymname
  \let\implicitterm\term
  \tempca\@ne
  #1% set explicit names
}

\newtoks\setsncommands
\newtoks\unsetsncommands

\def\termsymname#1#2{% set the symbolic name, if it is nonempty
  \def\next{#2}%
  \ifx\next\empty
  \else % there is a symolic name
      \expandafter\ifx\csname$[#2]\endcsname\relax% i$ this name unassigned? 
          \setnameflag{#2}\end % ... this name has been explicitly set
          \setuprefs{#2}{\the\tempca}%
      \else
          \errmessage{ambiguous symbolic name: #2}%
      \fi
  \fi
  \advance\tempca\@ne
}

\newtoks\nameflagtoks

\def\setnameflag#1#2{%
    \toksa\expandafter{\csname$[#1]sym\endcsname}% $
    \edef\next{\let\the\toksa\noexpand#2}\next
    \appendr\nameflagtoks{\noexpand\unsetnameflag{#1}}%
}

\def\unsetnameflag#1{%
    \toksa\expandafter{\csname$[#1]sym\endcsname}% $
    \edef\next{\let\the\toksa\relax}\next
}

\def\unsetsymname#1{%
    \toksa\expandafter{\csname$[#1]\endcsname}% $
    \edef\next{\let\the\toksa\relax}\next
}

\def\unsetsym#1{%
    \toksa\expandafter{\csname$[#1]\endcsname}% $
    \toksb\expandafter{\csname$$[#1]\endcsname}% $
    \edef\next{\let\the\toksa\relax\let\the\toksb\relax}\next
}

\def\lhssymname#1#2{% set an explicit symbolic name for the left hand side
  \setuplhsref{#2}%
  \setnameflag{#2}\end % signal that this name has ben explicitly set
}

% the code below is just an example. One has to figure out how to record percent symbols and other 
% dangerous \TeX\ symbols; it is rather slow, a faster two stage scheme can be envisioned 
% where the first stage builds the command while iterating over the rule numbers; this is left as an exercise. 

\let\hc\harmlesscomment
\let\uu\space
\def\setuprefs#1#2{%
    \expandafter\let\csname$[#1]\endcsname.% $o the \ifx ... \relax test makes sense ...
    \appendr\nameflagtoks{\noexpand\unsetsymname{#1}}% clean it up later
    \toksa{}\expandafter\charstonumbers#1\end
    \edef\next{\toksb{\space\space\noexpand\setsym{\the\toksa}{#2}\hc^^J\uu\uu\hc\hc\hc\uu#1 --> #2^^J}}\next
    \edef\next{\toksc{\space\space\noexpand\unsetsym{\the\toksa}\hc^^J\uu\uu\hc\hc\hc\uu#1 --> \relax^^J}}\next
    \concat\setsncommands\toksb
    \concat\unsetsncommands\toksc
}%

\def\setuplhsref#1{%
    \def\next{#1}%
    \ifx\next\empty
    \else
        \expandafter\let\csname$[#1]\endcsname.% $o the \ifx ... \relax test makes sense ...
        \appendr\nameflagtoks{\noexpand\unsetsymname{#1}}% clean it up later
        \toksa{}\expandafter\charstonumbers#1\end
        \edef\next{\toksb{\uu\uu\noexpand\setlhs{\the\toksa}\hc^^J\uu\uu\hc\hc\hc\uu#1 --> $$^^J}}\next
        \edef\next{\toksc{\uu\uu\noexpand\unsetsym{\the\toksa}\hc^^J\uu\uu\hc\hc\hc\uu#1 --> \relax^^J}}\next
        \concat\setsncommands\toksb
        \concat\unsetsncommands\toksc
    \fi
}%

\def\sprintrule#1\to#2{%
    \tempcc=#1\relax  
    \tempcd=\fgetelemof{yyrone}\at\tempcc\relax
    \edef\ruleline{\space(rule \number#1)\space\fgetelemof{yytname}\at\tempcd:}%
    \tempcd=\fgetelemof{yyprhs}\at\tempcc\relax
    \tempcc=\fgetelemof{yyrhs}\at\tempcd\relax
    \ifnum\tempcc>\m@ne
        \fillruleline
    \else
        \edef\ruleline{\ruleline\space<empty>}%
    \fi
    #2\expandafter{\ruleline}%
}

\def\fillruleline{%
    \edef\ruleline{\ruleline\space\fgetelemof{yytname}\at\tempcc}%
    \advance\tempcd\@ne
    \tempcc=\fgetelemof{yyrhs}\at\tempcd\relax
    \ifnum\tempcc>\m@ne
        \let\next\fillruleline
    \else
        \let\next\relax
    \fi
    \next
}

\def\r@lesymsetup#1#2{%
  \let\term\termsymextra
  \let\lhs\lhssymextra
  \let\implicitterm\term
  \tempca=\@ne
  #1% set implicit names
}

\def\termsymextra#1#2{% set an implicit symbolic name: 
                      %   1) if it has not been defined yet, define it
                      %   2) if it is defined implicitly, make it \def
  \expandafter\let\expandafter\next\csname$[#1]\endcsname% $ this is the name we are trying to define
  \ifx\next\relax % it is not defined yet
      \setuprefs{#1}{\the\tempca}%
  \else % already defined
      \expandafter\ifx\csname$[#1]sym\endcsname\end % it i$ an existing explicit symbolic name, done
      \else
          \expandafter\ifx\csname$[#1]sym\endcsname\noindent % it i$ an existing implicit symbolic name of lhs, overwrite it
              \setuprefs{#1}{\the\tempca}%
          \else
              \edef\next{\toksa{\space\space\noexpand\locksymname{#1}\harmlesscomment^^J}}\next
              \edef\next{\toksb{\space\space\noexpand\unlocksymname{#1}\harmlesscomment^^J}}\next
              \concat\setsncommands\toksa % symbolic name defined implicitly, disallow it
              \concat\unsetsncommands\toksb
          \fi
      \fi
  \fi
  \advance\tempca\@ne
}

\def\locksymname#1{\expandafter\let\csname$[#1]\endcsname\def} %$
\def\unlocksymname#1{\expandafter\let\csname$[#1]\endcsname\relax} %$

\def\lhssymextra#1#2{% set the name of the left hand side implicitly
  \expandafter\let\expandafter\next\csname$[#1]\endcsname%$
  \ifx\next\relax % not defined yet
      \setuplhsref{#1}%
      \setnameflag{#1}\noindent % this lhs name has been implicitly set
  \fi % it is already defined
}

\catcode`\^^A=\tempca

% stack variable access

\def\ym#1){%
  \ifnum#1<\@ne
      \putyyvalt
  \else
      \ifnum#1>\yyilen
          \errmessage{parameter out of range (#1\space out of\space \the\yyilen)}%
      \else
          \expandafter\putyytoksx\csname$$'#1\endcsname
      \fi
  \fi
}

\def\yn#1#{%
  \ifnum#1<\@ne
      \putyyval
  \else
      \ifnum#1>\yyilen
          \errmessage{parameter out of range}%
      \else
          \expandafter\putyyassignment\csname$'\number#1\endcsname%$
      \fi
  \fi
}

\def\yx#1[{%
  \expandafter\ifx\csname$[#1]\endcsname\yyval%$
      \putyyvalt
  \else
      \expandafter\ifx\csname$[#1]\endcsname\def%$
          \errmessage{reference to ambiguous symbolic name: #1}%
      \else
          \expandafter\putyytoksx\csname$$[#1]\endcsname
      \fi
  \fi
}

\def\yz#1]{%
  \expandafter\ifx\csname$[#1]\endcsname\yyval%$
      \putyyval
  \else
      \expandafter\ifx\csname$[#1]\endcsname\def%$
          \errmessage{reference to ambiguous symbolic name: #1}%
      \else
          \expandafter\putyyassignment\csname$[#1]\endcsname%$
      \fi
  \fi
}

\def\putyyval\else#1\fi\fi{%
  \fi\yyvalx
}

\def\yyvalx#1{%
  \edef\next{\yyval{#1}}\next
}

\def\putyyvalt\else#1\fi\fi{%
  \fi\yyval
}

\def\putyyassignment#1\fi\fi{%
  \fi\fi\p@tyyassignment#1%
}

\def\putyytoksx#1\fi\fi{%
  \fi\fi#1%
}

\def\p@tyyassignment#1#2{%
  \yystringempty{#2}{#1}{#2\expandafter{#1}}%
}

% natural order stack access macros

\def\p@twwassignment#1#2{%
    \edef\sts{\noexpand\p@@wwassignment{\expandafter\xdecrement\expandafter{\number#1}}}%
    \expandafter\def\expandafter\sts\expandafter{\sts{#2}}%
    \yyvsa\empty
}

\def\p@@wwassignment#1#2#3{%
  \ifnum#1=\z@ %we have got to the element we need
      \yybreak{\yystringempty{#2}{#3}{#2{#3}}\let\sts\eatone}%
  \else
      \yybreak{\edef\sts{\noexpand\p@@wwassignment{\xdecrement{#1}}}%
               \expandafter\def\expandafter\sts\expandafter{\sts{#2}}}%
  \yycontinue
}

\def\yy#1{%
  \ifx#1[%
      \yybreak{\yz}%
  \else
      \ifx#1]%
          \yybreak@{\yx}%
      \else
          \ifx#1(%
              \yybreak@@{\ym}%
          \else
              \yybreak@@{\yn#1}%
          \fi
      \fi
  \yycontinue
}

% a macro to access the value stack in direct order, i.e.\ from right to left
% no \yylen is necessary

\def\bb#1#{%
  \ifnum#1<\@ne
      \yybreak{\putyyval}%
  \else
      \yybreak{\p@twwassignment{#1}}%
  \yycontinue
}

% symbolic name macros that prepare the environment for the use of the stack access macros above

\def\setsymcs#1#2{%
    \toksa{}\numberstochars#1\end
    \toksb\expandafter{\csname$[\the\toksa]\endcsname}% $
    \toksc\expandafter{\csname$'#2\endcsname}% $
    \edef\next{\let\the\toksb\the\toksc}\next
}

\def\setsymtr#1#2{%
    \toksa{}\numberstochars#1\end
    \toksb\expandafter{\csname$$[\the\toksa]\endcsname}%
    \toksc\expandafter{\csname$$'#2\endcsname}%
    \edef\next{\let\the\toksb\the\toksc}\next
}

\def\setlhs#1{%
    \toksa{}\numberstochars#1\end
    \toksb\expandafter{\csname$[\the\toksa]\endcsname}% $
    \edef\next{\let\the\toksb\yyval}\next
}

\def\setsym#1#2{%
    \setsymcs{#1}{#2}%
    \setsymtr{#1}{#2}%
}

% message output

\def\ferrmessage#1{{\newlinechar`\^^J\immediate\write16{\parsernamespace::#1^^J}}}