File: terms.c

package info (click to toggle)
jbofihe 0.38-5
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k, lenny
  • size: 1,576 kB
  • ctags: 2,420
  • sloc: ansic: 18,597; yacc: 1,865; perl: 1,190; makefile: 269; lex: 152; sh: 60
file content (1735 lines) | stat: -rw-r--r-- 45,435 bytes parent folder | download | duplicates (4)
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
/***************************************
  $Header: /cvs/src/jbofihe/terms.c,v 1.23 2001/05/30 20:48:31 richard Exp $

  Processing to work out which x-place of a particular selbri any term
  in the text occupies.
  ***************************************/

/**********************************************************************
 * Copyright (C) Richard P. Curnow  1998-2001
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of version 2 of the GNU General Public License as
 * published by the Free Software Foundation.
 * 
 * This program 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 this program; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
 * 
 *********************************************************************/

/*{{{ #Includes  */
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "functions.h"
#include "rpc_tab.h"
#include "nodes.h"
#include "cmavotab.h"
/*}}}*/

#define MAX_TERMS_IN_VECTOR 20

/* Maximum argument position.  Even the most horrific abstraction or
   lujvo would be pushed to get this many args!!! */

#define MAX_POS 20

/*+ Value used to denote 'fai' as a place.  So fa,fe,fi,fo,fu are
  1..5, f[aeiou] xi xa is 6, ... f[aeiou] xi soso is 99, fai or fai xi
  pa is 101 etc +*/
#define FAI_OFFSET 101

/* Type definitions *//*{{{*/

typedef enum {
  TRM_POS, /* positional, i.e. just bare sumti */
  TRM_FA,  /* a term with a FA in front */
  TRM_FAhI,/* a term with a fa'i in front */
  TRM_TAG  /* a term with a modal tag in front */
} TermType;

typedef struct TermVectorEntry{
  TermType type;
  int      pos; /* For the FA case, which position the sumti has */
  TreeNode *node; /* The treenode corresponding to this term */
} TermVectorEntry;

typedef struct TermVector{
  int n_nodes;
  TermVectorEntry nodes[MAX_TERMS_IN_VECTOR];
} TermVector;

/*+ Enumeration tags for the LinkConvEntry type. +*/
typedef enum {
  LC_LINKS, /* TermVector of sumti links */
  LC_SE, /* SE conversion */
  LC_JAI, /* JAI conversion */
  LC_TAG /* JAI tag conversion */
} LCType;

typedef struct {
  int conv;
  TreeNode *senode;
} SeInfo;

typedef struct {
  LCType type;
  union {
    TermVector *links;
    SeInfo se;
    struct {
      TreeNode *tag; /* The tag */
      TreeNode *inner_tu2; /* The modified tanru_unit_2 */
    } jai_tag;
  } data;
} LinkConvEntry;

/*+ Structure containing an array of linked sumti blocks and
  conversions.  This is accumulated whilst drilling down into 'KE
  selbri3 KE'E BE links' constructions, which could recurse a lot. +*/
typedef struct {
  int n;
  LinkConvEntry e[MAX_TERMS_IN_VECTOR];
} LinkConv;
/*}}}*/
/* Forward prototypes *//*{{{*/
static void process_bridi_tail(TreeNode *bt, TermVector *pre, TermVector *post);
static void process_selbri_args(TreeNode *s, TermVector *pre, TermVector *post, LinkConv *lc);
static void process_selbri_3_args(TreeNode *s3, TermVector *pre, TermVector *post, LinkConv *lc);
/*}}}*/

static void tv_init(TermVector *tv)/*{{{*/
{
  tv->n_nodes = 0;
}
/*}}}*/
static void tv_catenate(TermVector *s1, TermVector *s2, TermVector *r)/*{{{*/
{
  int tn;
  int i, n1, n2;
  tn = s1->n_nodes + s2->n_nodes;

  /* If this is exceeded, we'll need to reassess the design.  Really
     ought to make the arrays dynamic but that incurs hassle of
     releasing them. */
  assert(tn <= MAX_TERMS_IN_VECTOR);

  n1 = s1->n_nodes;
  n2 = s2->n_nodes;

  r->n_nodes = tn;

  for (i=0; i<n1; i++) {
    r->nodes[i] = s1->nodes[i];
  }

  for (i=0; i<n2; i++) {
    r->nodes[n1+i] = s2->nodes[i];
  }
}
/*}}}*/
static void tv_reverse(TermVector *dest, TermVector *src)/*{{{*/
{
  /* Reverse the order of a Termector. */
  int n, i;

  assert(dest != src); /* Not designed to cope with this case */

  n = dest->n_nodes = src->n_nodes;

  for (i=0; i<n; i++) {
    dest->nodes[i] = src->nodes[n-1-i];
  }
}/*}}}*/
static void lc_init(LinkConv *lc)/*{{{*/
{
  /* Initialise a linkconv list to empty */
  lc->n = 0;
}
/*}}}*/
static void lc_append_se(LinkConv *lc, int conv, TreeNode *senode)/*{{{*/
{
  /* Append a SE conversion to a linkconv chain */
  assert(lc->n < MAX_TERMS_IN_VECTOR);

  lc->e[lc->n].data.se.conv = conv;
  lc->e[lc->n].data.se.senode = senode;
  lc->e[lc->n].type = LC_SE;
  ++(lc->n);
}
/*}}}*/
static void lc_append_jai_tag(LinkConv *lc, TreeNode *tag, TreeNode *inner_tu2)/*{{{*/
{
  assert(lc->n < MAX_TERMS_IN_VECTOR);

  lc->e[lc->n].data.jai_tag.tag = tag;
  lc->e[lc->n].data.jai_tag.inner_tu2 = inner_tu2;
  lc->e[lc->n].type = LC_TAG;
  ++(lc->n);
  
}
/*}}}*/
static void lc_append_jai(LinkConv *lc)/*{{{*/
{
  assert(lc->n < MAX_TERMS_IN_VECTOR);

  lc->e[lc->n].type = LC_JAI;
  ++(lc->n);
  
}
/*}}}*/
static void lc_append_links(LinkConv *lc, TermVector *v)/*{{{*/
{
  /* Append a set of linked sumti to a linkconv chain.  Note, a dynamic COPY is
     made of 'v'. */
  assert(lc->n < MAX_TERMS_IN_VECTOR);
  lc->e[lc->n].data.links = new(TermVector);

  /* Deep structure copy */
  *(lc->e[lc->n].data.links) = *v;
  
  lc->e[lc->n].type = LC_LINKS;
  ++(lc->n);
}
/*}}}*/
static void lc_copy(const LinkConv *src, LinkConv *dest)/*{{{*/
{
/* Copy a link/conv vector.  A semi-deep structure copy is made, i.e. the
  pointers to the vectors of linked sumti get aliased.  The intention is to
  produce a local copy onto which extra terms can be appended, rather than to
  make a completely general copy.  */
  
  *dest = *src;
}
/*}}}*/
static int recover_se_conv(TreeNode *x)/*{{{*/
{
  /* Turn se, te etc into 2..5 */
  int se_code;
  char *se_str;
  TreeNode *se;

  se = strip_attitudinal(x);
  assert (se->data.cmavo.selmao == SE);

  se_code = se->data.cmavo.code;
  se_str = cmavo_table[se_code].cmavo;

  /* Unfortunately, faxixa, faxize etc to access the 6th places
     onwards are just too much to handle for now!! */
  
  if (!strcmp(se_str, "se")) {
    return 2;
  } else if (!strcmp(se_str, "te")) {
    return 3;
  } else if (!strcmp(se_str, "ve")) {
    return 4;
  } else if (!strcmp(se_str, "xe")) {
    return 5;
  } else {
    abort();
  }
}
/*}}}*/
static int recover_fa_conv(TreeNode *x)/*{{{*/
{
/* Turn fa, fe... into 1 .. 5

  EXTEND TO COPE WITH SUBSCRIPTED VALUES!!!

  static int recover_fa_conv Return the value in the range 1 .. 5
  corresponding to the FA cmavo.

  TreeNode *x The parse node, must be a cmavo of selma'o FA.  */
  int fa_code;
  char *fa_str;

  x = strip_attitudinal(x);
  assert (x->data.cmavo.selmao == FA);

  fa_code = x->data.cmavo.code;
  fa_str = cmavo_table[fa_code].cmavo;

  /* Unfortunately, faxixa, faxize etc to access the 6th places
     onwards are just too much to handle for now!! */
  
  if (!strcmp(fa_str, "fi'a")) {
    return 0;
  } else if (!strcmp(fa_str, "fa")) {
    return 1;
  } else if (!strcmp(fa_str, "fe")) {
    return 2;
  } else if (!strcmp(fa_str, "fi")) {
    return 3;
  } else if (!strcmp(fa_str, "fo")) {
    return 4;
  } else if (!strcmp(fa_str, "fu")) {
    return 5;
  } else if (!strcmp(fa_str, "fai")) {
    return FAI_OFFSET;
  } else {
    abort();
  }
}
/*}}}*/
static void tv_build(TermVector *r, TreeNode *x)/*{{{*/
{
/* Build a TermVector from a terms node in the parse tree.  Any terms_1
  or terms_2 with CEhE or PEhE inside is ignored - to defer the problem of what
  to do about afterthought termsets to somewhere else.  */

  TermVector vv;
  TreeNode *xx, *t1, *t2, *t, *tc, *tcc;
  struct nonterm *ntx, *ntt;
  int nc, ntc;

  type_check(x, TERMS);

  /* The main hassle is that the <terms> rule is left recursive, so we
     build the vector backwards and reverse it at the end. */
  tv_init(&vv);

  xx = x;
  do {
    ntx = &xx->data.nonterm;
    nc = ntx->nchildren;

    if (nc == 1) { /* last term in the series */
      t1 = ntx->children[0];
    } else if (nc == 2) {
      t1 = ntx->children[1];
      xx = ntx->children[0]; /* next term in the series */
    } else {
      abort();
    }

    type_check(t1, TERMS_1);

    /* No point assertion checking whether the children are
       nonterminals, they must be from the grammar */
    ntt = &t1->data.nonterm;
    ntc = ntt->nchildren;

    if (ntc > 1) {
      /* aaaargh!! termset land */
      fprintf(stderr, "No place tagging for termset at line %d\n", t1->start_line);
      continue;
    }

    t2 = ntt->children[0];
    type_check(t2, TERMS_2);
    ntt = &t2->data.nonterm;
    ntc = ntt->nchildren;

    if (ntc > 1) {
      /* aaaargh!! termset land */
      fprintf(stderr, "No place tagging for termset at line %d\n", t2->start_line);
      continue;
    }

    t = ntt->children[0];
    assert(t->data.nonterm.type == TERM);
    
    /* Now, what sort of term is it? Have to drill down one layer further */
    tc = t->data.nonterm.children[0];

    switch (tc->data.nonterm.type) {
      case TERM_PLAIN_SUMTI:
        vv.nodes[vv.n_nodes].type = TRM_POS;
        vv.nodes[vv.n_nodes].node = t;
        vv.n_nodes++;
        break;
      case TERM_PLACED_SUMTI:
        {
          int pos;
          tcc = child_ref(tc, 0);
          pos = recover_fa_conv(tcc);
          if (pos == 0) {
            vv.nodes[vv.n_nodes].type = TRM_FAhI;
            vv.nodes[vv.n_nodes].node = t;
            vv.nodes[vv.n_nodes].pos = 0;
          } else {            
            vv.nodes[vv.n_nodes].type = TRM_FA;
            vv.nodes[vv.n_nodes].node = t;
            vv.nodes[vv.n_nodes].pos = pos;
          }
          vv.n_nodes++;
        }
        break;
      case TERM_TAGGED_SUMTI:
      case TAGGED_TERMSET:
        vv.nodes[vv.n_nodes].type = TRM_TAG;
        vv.nodes[vv.n_nodes].node = t;
        vv.n_nodes++;
        break;
      case TERMSET:
        fprintf(stderr, "No place tagging for termset at line %d\n", t->start_line);
        break;
      case TERM_FLOATING_TENSE:
      case TERM_FLOATING_NEGATE:
      case TERM_OTHER:
      default:
        /* None of these are interesting for place tagging */
        break;
    }

  } while (nc == 2); /* Keep looping whilst were in the left recursive <terms> rule */

  /* Reverse the order to put the terms in natural order */
  tv_reverse(r, &vv);

}
/*}}}*/
static void process_subsentence(TreeNode *ss, TermVector *pre, TermVector *post)/*{{{*/
{
  struct nonterm *nt, *ntc;
  int nc;
  TreeNode *sss;
  TreeNode *c;
  TermVector head_terms, new_pre;
  TreeNode *terms, *btail;

  sss = ss;
  do {
    type_check(ss, SUBSENTENCE);

    nt = &sss->data.nonterm;
    nc = nt->nchildren;

    if (nc == 2) {
      /* Pick the 'subsentence' following the 'prenex' out of the 2 child version */
      sss = child_ref(sss, 1);
    } else {
      /* Pick 'sentence' out of the 1 child version */
      sss = child_ref(sss, 0);
    }
  } while (nc == 2);

  type_check(sss, SENTENCE);
  nt = &sss->data.nonterm;

  if (nt->nchildren == 1) {
    c = nt->children[0];
    if (c->type == N_NONTERM) {
      ntc = & c->data.nonterm;
      if ((ntc->type == NO_CU_SENTENCE) || (ntc->type == OBSERVATIVE_SENTENCE)) {
        sss = c;
        nt = ntc;
      }
    }
  }

  /* Now looking at the '[terms [CU #]] bridi_tail' node. */
  terms = find_nth_child(sss, 1, TERMS);
  btail = find_nth_child(sss, 1, BRIDI_TAIL);
  assert(btail); /* Every sentence has one of these! */

  if (terms) {
    tv_build(&head_terms, terms);
    tv_catenate(pre, &head_terms, &new_pre);
    process_bridi_tail(btail, &new_pre, post);
  } else {
    process_bridi_tail(btail, pre, post);
  }
}
/*}}}*/

/* Type definitions (tags, places etc) *//*{{{*/

typedef enum {
  PT_ORD, /* one of the x1 .. x5 of the BRIVLA etc */
  PT_TAG, /* e.g. the x1 of 'jai bau cusku' */
  PT_JAI  /* e.g. the x1 of 'jai rinka' */
} PlaceType;

typedef struct {
  TreeNode *tag;
  TreeNode *inner_tu2;
} TagPlace;

typedef struct {
  int pad;
} JaiPlace;

typedef struct {
  PlaceType type;
  int valid;
  int taken;
  int pos;
  TagPlace tag;
  JaiPlace jai;
} Place;
/*}}}*/

static void fixup_term_place(TreeNode *x, Place *pl, XTermTag *tt)/*{{{*/
{
  /* Given a term treenode and a single place descriptor, chain the place
     information to the node's property list for later display. */
  XTermTags *ts, *nts;

  type_check(x, TERM);
  ts = prop_term_tags(x, NO);
  if (ts) {
    while(ts->next) ts = ts->next; /* Get to end of list */

    nts = new(XTermTags);
    ts->next = nts;
    nts->next = NULL;
    ts = nts;
  } else {
    ts = prop_term_tags(x, YES); /* Create it */
    ts->next = NULL;
  }

  ts->tag = *tt; /* deep copy, tt is in automatic storage */

  ts->tag.pos = pl->pos;

  switch (pl->type) {
    case PT_ORD:
      break;

    case PT_TAG:
      ts->tag.jaitag.tag = pl->tag.tag;
      ts->tag.jaitag.inner_tu2 = pl->tag.inner_tu2;
      ts->tag.type = TTT_JAITAG;
      break;

    case PT_JAI:
      ts->tag.type = TTT_JAI;
      break;
  }
  
}/*}}}*/
static void assign_terms_to_places(TermVector *t, Place *place, Place *fai, int abase, XTermTag *tt)/*{{{*/
{
  int i, n;
  int base;
  int pos;

  base = abase;

  /* Assign FA places */
  n = t->n_nodes;
  for (i=0; i<n; i++) {
    if (t->nodes[i].type == TRM_FA) {
      pos = t->nodes[i].pos;
      if (pos < FAI_OFFSET) {
        if (place[pos].valid) {
          fixup_term_place(t->nodes[i].node, &place[pos], tt);
          place[pos].taken = 1;
        } else {
          fprintf(stderr, "Invalid place\n");
        }
      } else {
        pos -= FAI_OFFSET;
        pos++;
        if (fai[pos].valid) {
          fixup_term_place(t->nodes[i].node, &fai[pos], tt);
          fai[pos].taken = 1;
        } else {
          fprintf(stderr, "Invalid place\n");
        }          
      }
    }
  }

  /* Now fix up unmarked places */
  for (i=0; i<n; i++) {
    switch (t->nodes[i].type) {
      case TRM_POS :
        /* Look for first unassigned place */
        while (place[base].taken) ++base;
        if (base > MAX_POS) {
          fprintf(stderr, "Too many places assigned\n");
        } else {
          fixup_term_place(t->nodes[i].node, &place[base], tt);
          place[base].taken = 1;
          ++base;
        }
        break;

      case TRM_FA :
        pos = t->nodes[i].pos;
        if (pos < FAI_OFFSET) {
          base = pos + 1;
        }

        break;

      default:
        break;

    }
  }

}
/*}}}*/
static void assign_conversion(LinkConv *lc, TreeNode *convertible)/*{{{*/
{
  Place place[MAX_POS];
  XConversion *ext;

  int i, n;

  /* Init the place and vectors */
  for (i=0; i<MAX_POS; i++) {
    place[i].valid = 1;
    place[i].taken = 0;
    place[i].type = PT_ORD;
    place[i].pos = i;
  }

  /* Work back through the linked sumti */
  n = lc->n;
  for (i=n-1; i>=0; i--) {
    switch (lc->e[i].type) {
      case LC_SE:
        /* Just swap 2 positions in the place[] array */
        {
          Place temp;
          SeInfo *j;
          XDontGloss *dg;

          j = &lc->e[i].data.se;
          temp = place[1];
          place[1] = place[j->conv];
          place[j->conv] = temp;
          dg = prop_dont_gloss(j->senode, YES); /* prevent the node being glossed */
        }        
        break;

      case LC_LINKS:
        /* Binding of linked sumti does not affect the glossing of the
           brivla or whatever, just drop through to next loop
           iteration */
        break;

      case LC_TAG:
        /* Break out at this point */
        goto loop_done;
        break;

      case LC_JAI:
        fprintf(stderr, "Don't know what to do with <jai> for gloss conversion\n");
        break;
    }
  }

loop_done:
  
  ext = prop_conversion(convertible, YES);
  ext->conv = place[1].pos;

}
/*}}}*/
static void assign_places(TermVector *pre, TermVector *post, LinkConv *lc, XTermTag *tt)/*{{{*/
{
  /* Work out which terms have which places in the bridi, and tag them
     accordingly */

  /* Variable declarations */

  /*+ Array for the ordinary places in the bridi.  e.g. if you get a
    term with 'fe' in front of it, look in place[2] to find what to do
    with it. +*/
  Place place[MAX_POS];

  /*+ Array for the 'fai' places.  e.g. if a term has 'fai xi re' in
    front, look in fai[2] to find what to do. +*/

  Place fai[MAX_POS];
  int i, n;

  /* Init the place and fai vectors */
  for (i=0; i<MAX_POS; i++) {
    place[i].valid = 1;
    place[i].taken = 0;
    place[i].type = PT_ORD;
    place[i].pos = i;
    
    fai[i].valid = 0;
    fai[i].taken = 0;
  }

  /* Work back through the linked sumti */
  n = lc->n;
  for (i=n-1; i>=0; i--) {
    switch (lc->e[i].type) {
      case LC_SE:
        /* Just swap 2 positions in the place[] array */
        {
          Place temp;
          SeInfo *j;
          j = &lc->e[i].data.se;
          temp = place[1];
          place[1] = place[j->conv];
          place[j->conv] = temp;
        }        
        break;
      case LC_TAG:
        {
          int j;
          /* Shift fai place array up (i.e. need subscripts if more
             than one fai place is in scope). */
          for (j=1; ; j++) {
            if (!fai[j].valid) break;
          }
          for (; j>1; j--) {
            fai[j] = fai[j-1];
          }
          fai[1] = place[1];
          place[1].type = PT_TAG;
          place[1].valid = 1;
          place[1].tag.tag = lc->e[i].data.jai_tag.tag;
          place[1].tag.inner_tu2 = lc->e[i].data.jai_tag.inner_tu2;
        }
      break;

      case LC_JAI:
        {
          int j;
          /* Shift fai place array up (i.e. need subscripts if more
             than one fai place is in scope). */
          for (j=1; ; j++) {
            if (!fai[j].valid) break;
          }
          for (; j>1; j--) {
            fai[j] = fai[j-1];
          }
          fai[1] = place[1];
          place[1].type = PT_ORD;
          place[1].valid = 1;
        }
        break;

      case LC_LINKS:
        assign_terms_to_places(lc->e[i].data.links, place, fai, 2, tt);
        break;
    }
  }

  /* Do the pre and post terms */
  assign_terms_to_places(pre, place, fai, 1, tt);
  assign_terms_to_places(post, place, fai, 2, tt);

}
/*}}}*/
static void process_tanru_unit_2_args(TreeNode *tu2, TermVector *pre, TermVector *post, LinkConv *lc)/*{{{*/
{
  /* Handle argument processing at the level of a tanru_unit_2.  This is
     where the clever tag assignment stuff is done! */

  TreeNode *c1;

  type_check(tu2, TANRU_UNIT_2);
  c1 = maybe_strip_attitudinal(child_ref(tu2, 0));

  if (c1->type == N_CMAVO) {/*{{{*/
    switch (c1->data.cmavo.selmao) {
      case GOhA:/*{{{*/
        {
          XTermTag tt;

          tt.type = TTT_GOhA;
          tt.goha.goha = c1;
          assign_places(pre, post, lc, &tt);
          assign_conversion(lc, c1);
        }
        break;/*}}}*/
      case ME:/*{{{*/
        {
          XTermTag tt;
          XRequireBrac *xrb;
          TreeNode *me_node;

          tt.type = TTT_ME;
          tt.me.sumti = find_nth_child(tu2, 1, SUMTI);
          me_node = find_nth_cmavo_child(tu2, 1, ME);
          assert(tt.me.sumti);
          xrb = prop_require_brac (tt.me.sumti, YES);
          assign_places(pre, post, lc, &tt);
          /* Conversion can't occur on ME, there is only an x1 place */
        }
      break;/*}}}*/
      case NUhA:/*{{{*/
        {
          XTermTag tt;
          XRequireBrac *xrb;

          tt.type = TTT_NUhA;
          tt.nuha.mex_operator = find_nth_child(tu2, 1, MEX_OPERATOR);
          assign_places(pre, post, lc, &tt);
          assign_conversion(lc, c1);
          xrb = prop_require_brac (tt.nuha.mex_operator, YES);
        }
        break;/*}}}*/
        
    }
/*}}}*/
  } else if (c1->type == N_BRIVLA) {/*{{{*/

    XTermTag tt;

    tt.type = TTT_BRIVLA;
    tt.brivla.x = c1;
    assign_places(pre, post, lc, &tt);
    assign_conversion(lc, c1);/*}}}*/
  } else if (c1->type == N_NONTERM) {
    switch (c1->data.nonterm.type) {
      case NUMBER_MOI_TU2:/*{{{*/
        {
          TreeNode *norl, *moi;
          XRequireBrac *xrb;
          XTermTag tt;
          /* Remember : The NUMBER_MOI marker that's in the bison file is NOT
             built into the parse tree!! */
          norl = child_ref(c1, 0);
          moi = find_nth_cmavo_child(c1, 1, MOI);
          tt.type = TTT_NUMBERMOI;
          tt.numbermoi.number_or_lerfu = norl;
          tt.numbermoi.moi = moi;
          assign_places(pre, post, lc, &tt);
          if ((pre->n_nodes > 0) ||
              (post->n_nodes > 0)) {
            xrb = prop_require_brac (norl, YES);
          }
        }
        break;
/*}}}*/
      case KE_SELBRI3_TU2:/*{{{*/
        {
          TreeNode *cs3;

          cs3 = find_nth_child(c1, 1, SELBRI_3);
          assert(cs3);
          process_selbri_3_args(cs3, pre, post, lc);
        }

        break;/*}}}*/
      case SE_TU2:/*{{{*/
        {
          LinkConv newlc;
          int conv;
          TreeNode *se_child, *tu2_child;

          lc_copy(lc, &newlc);
          se_child = child_ref(c1, 0);
          tu2_child = find_nth_child(c1, 1, TANRU_UNIT_2);
          assert(tu2_child);

          conv = recover_se_conv(se_child);
          lc_append_se(&newlc, conv, se_child);

          process_tanru_unit_2_args(tu2_child, pre, post, &newlc);

        }

        break;/*}}}*/
      case JAI_TAG_TU2:/*{{{*/
        {
          LinkConv newlc;
          TreeNode *ctag, *tu2_child;
          XRequireBrac *xrb;

          lc_copy(lc, &newlc);
          ctag = find_nth_child(c1, 1, TAG);
          tu2_child = find_nth_child(c1, 1, TANRU_UNIT_2);
          assert(ctag);
          assert(tu2_child);
          lc_append_jai_tag(&newlc, ctag, tu2_child);
          xrb = prop_require_brac(tu2_child, YES);
          process_tanru_unit_2_args(tu2_child, pre, post, &newlc);
        }

        break;
/*}}}*/
      case JAI_TU2:/*{{{*/
        {
          LinkConv newlc;
          TreeNode *tu2_child;

          lc_copy(lc, &newlc);
          tu2_child = find_nth_child(c1, 1, TANRU_UNIT_2);
          assert(tu2_child);
          lc_append_jai(&newlc);
          process_tanru_unit_2_args(tu2_child, pre, post, &newlc);
        }
        break;
/*}}}*/
      case NAHE_TU2:/*{{{*/
        {
          TreeNode *tu2_child;
          tu2_child = find_nth_child(c1, 1, TANRU_UNIT_2);
          assert(tu2_child);
          process_tanru_unit_2_args(tu2_child, pre, post, lc);
        }
        break;
/*}}}*/
      case ABSTRACTION:/*{{{*/
        {
          TreeNode *nns, *c2, *nu, *nai;
          XTermTag tt;

          nns = child_ref(c1, 0);
          type_check(nns, NU_NAI_SEQ);
          c2 = maybe_strip_attitudinal(child_ref(nns, 0));
          if ((c2->type == N_CMAVO) &&
              (c2->data.cmavo.selmao == NU)) {
            /* A simple NU <subsentence> thing */
            nu = c2;
            /* Maybe null - not sure what to do with it as I haven't
               seen any examples */
            nai = find_nth_child(nns, 1, NAI);
            if (nai) {
              fprintf(stderr, "Don't know how to handle negated abstractor yet at line %d column %d - ignoring negation\n",
                      c1->start_line, c1->start_column);
            }

            tt.type = TTT_ABSTRACTION;
            tt.abstraction.nu = nu;
            assign_places(pre, post, lc, &tt);
            assign_conversion(lc, nu);
            
          } else {
            fprintf(stderr, "Can't handle connected abstractors wrt places yet at line %d column %d\n",
                    c1->start_line, c1->start_column);
          }
        }
        break;
/*}}}*/
      default:
        break;

    }
  } else if (c1->type == N_ZEI) {/*{{{*/

    XTermTag tt;

    tt.type = TTT_ZEI;
    tt.zei.zei = c1;
    assign_places(pre, post, lc, &tt);
    assign_conversion(lc, c1);
    /*}}}*/
  } else {
    abort();
  }
  

}
/*}}}*/
static void process_tanru_unit_1_args(TreeNode *tu1, TermVector *pre, TermVector *post, LinkConv *lc)/*{{{*/
{
/* Handle argument processing at the level of a tanru_unit_1.  */

  XTermVector *xtv; /* The linked sumti vector of the tu1 if any */
  XDoneTU1 *xdtu1; /* Property whose existence on the TU1 tree node
                      shows we have processed its linked sumti */
  TreeNode *tu2;

  xtv = prop_term_vector(tu1, NO);

  tu2 = child_ref(tu1, 0);

  if (xtv) {
    LinkConv newlc;
    lc_copy(lc, &newlc);
    lc_append_links(&newlc, xtv->vec);
    process_tanru_unit_2_args(tu2, pre, post, &newlc);
  } else {
    process_tanru_unit_2_args(tu2, pre, post, lc);
  }

  /* Put marker on the TU1 node so that we know its linked sumti have
     now been processed.  Later on we scan the entire parse tree for
     TU1 nodes that don't have this property already set - these are
     ones that are not part of tertau */

  xdtu1 = prop_done_tu1(tu1, YES);

}
/*}}}*/
static void process_selbri_6_args(TreeNode *s6, TermVector *pre, TermVector *post, LinkConv *lc)/*{{{*/
{
  /* Drill down into a selbri_6 etc */

  TreeNode *tu, *tu1, *cs6, *cs;
  
  /* For the cases with BO, I think it's only the very final term
     that's relevant.  For the guhek cases, I think these are like
     connectives, in selbri_4/5, it's like gek_sentence?? */

  type_check(s6, SELBRI_6);

  if (nch(s6) == 1) {
    tu = child_ref(s6, 0); /* Always first child */

    if (nch(tu) > 1) {
      fprintf(stderr, "tanru_unit at line %d column %d contains CEI, can't handle this yet\n",
              tu->start_line, tu->start_column);
    } else {
      tu1 = child_ref(tu, 0);
      process_tanru_unit_1_args(tu1, pre, post, lc);
    }
    
  } else {
    tu = find_nth_child(s6, 1, TANRU_UNIT);
    if (tu) {
      /* BO form */
      cs6 = find_nth_child(s6, 1, SELBRI_6);
      process_selbri_6_args(cs6, pre, post, lc);
    } else {
      cs = find_nth_child(s6, 1, SELBRI);
      cs6 = find_nth_child(s6, 1, SELBRI_6);
      process_selbri_args(cs, pre, post, lc);
      process_selbri_6_args(cs6, pre, post, lc);
    }
  }
}
/*}}}*/
static void process_selbri_5_args(TreeNode *s5, TermVector *pre, TermVector *post, LinkConv *lc)/*{{{*/
{
  /* Drill down into a selbri_5 etc */

  TreeNode *s6, *cs5;

  cs5 = s5;
  do {
    s6 = find_nth_child(cs5, 1, SELBRI_6);
    process_selbri_6_args(s6, pre, post, lc);
    cs5 = find_nth_child(cs5, 1, SELBRI_5);
  } while (cs5);

}
/*}}}*/
static void process_selbri_3_args(TreeNode *s3, TermVector *pre, TermVector *post, LinkConv *lc)/*{{{*/
{
  /* Drill down into a selbri_3 to try to recover a single tanru_unit_2 which is
     the tertau.  Apply the supplied args to that - i.e. work out the place
     structure, then go and mark all the referenced terms accordingly.  */

  TreeNode *s4, *s5, *ks3, *cs3; /* The selbri chain */

  type_check(s3, SELBRI_3);

  /* The tertau is always the last entry in the sequence of selbri_4's.
     Fortunately, this is immediately available via left recursion */
  s4 = find_nth_child(s3, 1, SELBRI_4);

  /* If the selbri_4 is non-trivial (i.e. has connectives in), we have
     to descend each branch, more or less as though there was a gihek
     at the sentence level.  Fortunately from this function onwards,
     the same termvectors apply to every child, so at least we're over
     that. */

  if (nch(s4) > 1) {

    do {
      s5 = find_nth_child(s4, 1, SELBRI_5);
      if (s5) {
        process_selbri_5_args(s5, pre, post, lc);
      } else {
        ks3 = find_nth_child(s4, 1, KE_SELBRI_3);
        assert(ks3);
        cs3 = find_nth_child(ks3, 1, SELBRI_3);
        assert(cs3);
        process_selbri_3_args(cs3, pre, post, lc);
      }

      s4 = find_nth_child(s4, 1, SELBRI_4);

    } while (s4);

  } else {
    s5 = find_nth_child(s4, 1, SELBRI_5);
    process_selbri_5_args(s5, pre, post, lc);
  }

}
/*}}}*/
static void process_selbri_args(TreeNode *s, TermVector *pre, TermVector *post, LinkConv *lc)/*{{{*/
{
  /* Drill down into a selbri etc */

  TreeNode *cs, *s1, *s2, *s3; /* The selbri chain */
  TermVector empty_tv;

  tv_init(&empty_tv);

  type_check(s, SELBRI);

  cs = s;
  do {
    s1 = find_nth_child(cs, 1, SELBRI_1);

    /* Try to get selbri_2, or go round again if there's a NA */
    s2 = find_nth_child(s1, 1, SELBRI_2);
    if (!s2) {
      cs = find_nth_child(s1, 1, SELBRI);
      assert(cs);
    }
  } while (!s2);

  /* OK, have selbri_2, the fun starts here if there's a CO. */

  s3 = child_ref(s2, 0); 
  if (nch(s2) > 1) {
    /* There is a CO following. Only the head terms apply to this
       first selbri_3, its other arguments are either linked sumti or
       are unspecified. */
    process_selbri_3_args(s3, pre, &empty_tv, lc);

    while (nch(s2) > 1) {
      s2 = find_nth_child(s2, 1, SELBRI_2);
    }

    s3 = child_ref(s2, 0);
    process_selbri_3_args(s3, &empty_tv, post, lc);

  } else {
    /* All arguments apply to the selbri_3 */
    process_selbri_3_args(s3, pre, post, lc);
  }
}/*}}}*/
static void process_main_selbri(TreeNode *ms, TermVector *pre, TermVector *post)/*{{{*/
{
  /* Handle each main selbri after it has been located.  */

  TermVector *cpre, *cpost;
  XTermVectors *xtv;
  TreeNode *s;
  LinkConv lc;

  type_check(ms, MAIN_SELBRI);

  cpre = new(TermVector);
  cpost = new(TermVector);
  *cpre = *pre;
  *cpost = *post;
  /* Do we really need to store this property???!!! We do all the
     processing on these vectors below! */
  xtv = prop_term_vectors(ms, YES);
  xtv->pre = cpre;
  xtv->post = cpost;

  lc_init(&lc);

  /*

    Roll up the sleeves ..., this is where the real grunge starts.
    
   */

  s = child_ref(ms, 0); /* selbri */

  process_selbri_args(s, pre, post, &lc);

}/*}}}*/
static void process_bridi_tail_3(TreeNode *bt3, TermVector *pre, TermVector *post)/*{{{*/
{
  TreeNode *c1, *gsc, *tt, *ttc;
  TermVector tail_terms, new_post;

  type_check(bt3, BRIDI_TAIL_3);

  c1 = child_ref(bt3, 0);
  if (c1->data.nonterm.type == MAIN_SELBRI) {
    /* Do main_selbri processing --- make a property record for the
       pre and post vectors and tag it onto that node */
    TreeNode *ms;
    ms = c1;

    tt = find_nth_child(bt3, 1, TAIL_TERMS);
    if (tt) {
      ttc = find_nth_child(tt, 1, TERMS);
      if (ttc) {
        tv_build(&tail_terms, ttc);
        tv_catenate(&tail_terms, post, &new_post);
        process_main_selbri(ms, pre, &new_post);
      } else {
        process_main_selbri(ms, pre, post);
      }
    } else {
      process_main_selbri(ms, pre, post);
    }
  } else {
    /* Gek sentence */
    TreeNode *gs, *ss1, *ss2, *tt, *ttt;
    gs = c1;
    type_check(gs, GEK_SENTENCE);

    /* Do gek_sentence processing.

       If first child is not a 'gek', the sentence is a 'tag KE ..'
       format one.  Drill down through that until we come to something
       of the form 'gek subsentence...' .*/
    while (gsc = child_ref(gs, 0),
           ((gsc->type != N_NONTERM) ||
            (gsc->data.nonterm.type != GEK))) {
      gs = find_nth_child(gs, 1, GEK_SENTENCE);
    }

    /* OK , got it. Extract the two subsentences */
    ss1 = find_nth_child(gs, 1, SUBSENTENCE);
    ss2 = find_nth_child(gs, 2, SUBSENTENCE);
    assert(ss1);
    assert(ss2);
    tt = find_nth_child(gs, 1, TAIL_TERMS);
    if (tt) {
      ttt = find_nth_child(tt, 1, TERMS);
      if (ttt) {
        TermVector new_post, tail_terms;

        tv_build(&tail_terms, ttt);
        tv_catenate(&tail_terms, post, &new_post);
        process_subsentence(ss1, pre, &new_post);
        process_subsentence(ss2, pre, &new_post);
      } else {
        process_subsentence(ss1, pre, post);
        process_subsentence(ss2, pre, post);
      }
    } else {
      process_subsentence(ss1, pre, post);
      process_subsentence(ss2, pre, post);
    }

  }
    
  return;
}/*}}}*/
static void process_bridi_tail_2(TreeNode *bt2, TermVector *pre, TermVector *post)/*{{{*/
{

  TreeNode *bt3, *bt2a, *bt2b, *tt, *ttt; /* The children */
  struct nonterm *nt;
  int nc;

  /* The structure of a bridi_tail_2 is, for this fn's purposes,

     bridi_tail_3
   | bridi_tail_2 [CONNECTIVE bridi_tail_2 [tail_terms]]

     */

  nt = & bt2->data.nonterm;
  nc = nt->nchildren;

  if (nc == 1) {
    /* Just ananlyse the child */
    bt3 = nt->children[0];
    process_bridi_tail_3(bt3, pre, post);
  } else {
    TermVector new_post, tail_terms;

    bt2a = find_nth_child(bt2, 1, BRIDI_TAIL_2);
    assert(bt2a);
    bt2b = find_nth_child(bt2, 2, BRIDI_TAIL_2);
    assert(bt2b);

    tt = find_nth_child(bt2, 1, TAIL_TERMS);
    if (tt) {
      ttt = find_nth_child(tt, 1, TERMS);
      if (ttt) {
        tv_build(&tail_terms, ttt);
        tv_catenate(&tail_terms, post, &new_post);
        process_bridi_tail_2(bt2a, pre, &new_post);
        process_bridi_tail_2(bt2b, pre, &new_post);
      } else {
        process_bridi_tail_2(bt2a, pre, post);
        process_bridi_tail_2(bt2b, pre, post);
      }
    } else {
      process_bridi_tail_2(bt2a, pre, post);
      process_bridi_tail_2(bt2b, pre, post);
    }
  }

  return;

}/*}}}*/
static void process_bridi_tail_1(TreeNode *bt1, TermVector *pre, TermVector *post)/*{{{*/
{

  TreeNode *bt2, *bt1c, *tt, *ttt; /* The children */
  struct nonterm *nt;
  int nc;

  /* The structure of a bridi_tail_1 is, for this fn's purposes,

     bridi_tail_2 
   | bridi_tail_1 [CONNECTIVE bridi_tail_2 [tail_terms]]

     */

  nt = & bt1->data.nonterm;
  nc = nt->nchildren;

  if (nc == 1) {
    /* Just ananlyse the child */
    bt2 = nt->children[0];
    process_bridi_tail_2(bt2, pre, post);
  } else {
    TermVector new_post, tail_terms;

    bt1c = find_nth_child(bt1, 1, BRIDI_TAIL_1);
    assert(bt1c);
    bt2 = find_nth_child(bt1, 1, BRIDI_TAIL_2);
    assert(bt2);

    tt = find_nth_child(bt1, 1, TAIL_TERMS);
    if (tt) {
      ttt = find_nth_child(tt, 1, TERMS);
      if (ttt) {
        tv_build(&tail_terms, ttt);
        tv_catenate(&tail_terms, post, &new_post);
        process_bridi_tail_1(bt1c, pre, &new_post);
        process_bridi_tail_2(bt2, pre, &new_post);
      } else {
        process_bridi_tail_1(bt1c, pre, post);
        process_bridi_tail_2(bt2, pre, post);
      }
    } else {
      process_bridi_tail_1(bt1c, pre, post);
      process_bridi_tail_2(bt2, pre, post);
    }
  }

  return;

}/*}}}*/
static void process_bridi_tail(TreeNode *bt, TermVector *pre, TermVector *post)/*{{{*/
{
/*
  Analyse a bridi_tail down to each vector of tail_terms and find each
  main_selbri within it.  Ultimately tag each main_selbri with the
  vectors of pre- and post- terms that apply to it.

  TreeNode *bt The bridi_tail to analyse

  TermVector *pre The vector of all sumti-like terms in before the
  'CU'.  Note, for gek-sentences, this is the concatenation of all
  such pre-term sequences.

  TermVector *post The vector of tail_terms accumulated from outer
  bridi_tail constructions.  (Initially this will be empty).

  */

  TreeNode *bt1, *btc, *tt, *ttt; /* The children */
  struct nonterm *nt;
  int nc;

  /* The structure of a bridi_tail is, for this fn's purposes,

     bridi_tail_1 [CONNECTIVE bridi_tail [tail_terms]]

     */

  nt = & bt->data.nonterm;
  nc = nt->nchildren;
  

  if (nc == 1) {
    /* Just ananlyse the child */
    bt1 = nt->children[0];
    process_bridi_tail_1(bt1, pre, post);
  } else {
    TermVector new_post, tail_terms;

    btc = find_nth_child(bt, 1, BRIDI_TAIL);
    assert(btc);

    bt1 = find_nth_child(bt, 1, BRIDI_TAIL_1);
    assert(bt1);
    
    tt = find_nth_child(bt, 1, TAIL_TERMS);
    if (tt) {
      ttt = find_nth_child(tt, 1, TERMS);
      if (ttt) {
        tv_build(&tail_terms, ttt);
        tv_catenate(&tail_terms, post, &new_post);
        process_bridi_tail_1(bt1, pre, &new_post);
        process_bridi_tail(btc, pre, &new_post);
      } else {
        process_bridi_tail_1(bt1, pre, &new_post);
        process_bridi_tail(btc, pre, post);
      }
    } else {
      process_bridi_tail_1(bt1, pre, &new_post);
      process_bridi_tail(btc, pre, post);
    }
  }
}/*}}}*/
static void process_statement_3(TreeNode *x)/*{{{*/
{
  struct nonterm *nt, *nts, *ntc;
  TreeNode *sent, *terms, *btail, *c;
  TermVector pre_terms, tail_terms;

  assert(x->type == N_NONTERM && x->data.nonterm.type == STATEMENT_3);

  nt = & x->data.nonterm;

  if (nt->nchildren == 1) {
    /* Must be the form where 'sentence' is the only child.  All the
       forms with TUhE and text_1 have at least 2 child nodes */

    sent = nt->children[0]; /* This is the 'sentence' inside */

    /* If the sentence is 'no_cu_sentence' or 'observative_sentence', drill down */

    assert(sent->type == N_NONTERM);

    nts = &sent->data.nonterm;
    if (nts->nchildren == 1) {
      c = nts->children[0];
      if (c->type == N_NONTERM) {
        ntc = & c->data.nonterm;
        if ((ntc->type == NO_CU_SENTENCE) || (ntc->type == OBSERVATIVE_SENTENCE)) {
          sent = c;
          nts = ntc;
        }
      }
    }

    /* Now looking at the '[terms [CU #]] bridi_tail' node. */
    terms = find_nth_child(sent, 1, TERMS);
    btail = find_nth_child(sent, 1, BRIDI_TAIL);
    assert(btail); /* Every sentence has one of these! */

    tv_init(&tail_terms);
    if (terms) {
      tv_build(&pre_terms, terms);
    } else {
      tv_init(&pre_terms);
    }

    process_bridi_tail(btail, &pre_terms, &tail_terms);

#if 0
    if (terms) {
      /* Test code, for now */
      TermVector tv;
      int i, n;
      TreeNode *c;

      tv_build(&tv, terms);
      n = tv.n_nodes;
      for (i=0; i<n; i++) {
        c = tv.nodes[i].node;
        fprintf(stderr, "Term at line %d col %d is ", c->start_line, c->start_column);
        switch (tv.nodes[i].type) {
          case TRM_POS:
            fprintf(stderr, "Ordinary positional sumti\n");
            break;
          case TRM_FA:
            fprintf(stderr, "Positioned sumti, place=%d\n", tv.nodes[i].pos);
            break;
          case TRM_TAG:
            fprintf(stderr, "Tagged sumti\n");
            break;
        }
      }
    }
#endif
  }
}/*}}}*/
static void process_abstraction(TreeNode *x)/*{{{*/
{
  TreeNode *ss;
  TermVector pre, post;

  tv_init(&pre);
  tv_init(&post);
  ss = child_ref(x, 1);
  type_check(ss, SUBSENTENCE);
  process_subsentence(ss, &pre, &post);
}/*}}}*/
static void process_relative_clause(TreeNode *x)/*{{{*/
{
  TreeNode *ss, *fc;
  TermVector pre, post;

  tv_init(&pre);
  tv_init(&post);
  fc = child_ref(x, 0);
  if (fc->data.nonterm.type == FULL_RELATIVE_CLAUSE) {
    ss = find_nth_child(fc, 1, SUBSENTENCE);
    if (ss) {
      process_subsentence(ss, &pre, &post);
    }
  }
}/*}}}*/
static void process_metalinguistic(TreeNode *x)/*{{{*/
{
  /* Handle metalinguistic constructions (SEI...).  This is slightly
    different to the other cases, in that we have to resolve the terms and selbri
    ourselves.  */

  TermVector pre, post;
  TreeNode *terms, *mmselbri, *selbri;
  LinkConv lc;

  terms = find_nth_child(x, 1, TERMS);
  mmselbri = find_nth_child(x, 1, METALINGUISTIC_MAIN_SELBRI);
  assert(mmselbri);
  selbri = child_ref(mmselbri, 0);
  tv_init(&pre);
  tv_init(&post);
  
  if (terms) {
    tv_build(&pre, terms);
  }

  lc_init(&lc);
  process_selbri_args(selbri, &pre, &post, &lc);

}/*}}}*/
static void scan_for_sentence_parents(TreeNode *x)/*{{{*/
{
  /* Seek recursively downwards looking for treenodes of type
    STATEMENT_3, ABSTRACTION.  We're really interested in SENTENCE, but that
    occurs within ge subsentence->sentence gi subsentence->sentence, and we
    have to track term strings into gek_sentences because you can have terms in
    front plus tail terms behind, that are part of both clauses.

    Relative clauses and NU abstractions (the 2 other places where sentences
    can occur) will have to be looked at afterwards.  */

  int nc, i;
  struct nonterm *nt;
  TreeNode *c;

  if (x->type == N_NONTERM) {
    nt = &x->data.nonterm;

    if (nt->type == STATEMENT_3) {
      process_statement_3(x);
    } else if (nt->type == ABSTRACTION) {
      process_abstraction(x);
    } else if (nt->type == RELATIVE_CLAUSE) {
      process_relative_clause(x);
    } else if (nt->type == METALINGUISTIC) {
      process_metalinguistic(x);
    }

    /* Traverse down even for statement_3 (there may be abstractors,
       TO..TOI etc buried inside) */
    nc = nt->nchildren;
    for (i=0; i<nc; i++) {
      c = nt->children[i];
      scan_for_sentence_parents(c);
    }

  }
}/*}}}*/
static void process_sumti_5b(TreeNode *x)/*{{{*/
{
  /* Scan down into the selbri inside a 'quantifier selbri' sumti, to mark up the
     tertau for conversions.  */

  TreeNode *c;
  LinkConv lc;

  TermVector empty_tv;
  tv_init(&empty_tv);

  c = find_nth_child(x, 1, SELBRI);
  assert(c);
  lc_init(&lc);
  process_selbri_args(c, &empty_tv, &empty_tv, &lc);
}/*}}}*/
static void process_sumti_tail_1a(TreeNode *x)/*{{{*/
{
  /* Scan down into the selbri inside a sumti_tail construction.  */ 

  TreeNode *c;
  LinkConv lc;

  TermVector empty_tv;
  tv_init(&empty_tv);

  c = find_nth_child(x, 1, SELBRI);
  assert(c);
  lc_init(&lc);
  process_selbri_args(c, &empty_tv, &empty_tv, &lc);
}/*}}}*/
static void scan_for_selbri_in_sumti(TreeNode *x)/*{{{*/
{
  /* Process selbri in the context of sumti.  Allows conversions to be
    handled with full generality.  */

  int nc, i;
  struct nonterm *nt;
  TreeNode *c;

  if (x->type == N_NONTERM) {
    nt = &x->data.nonterm;

    if (nt->type == SUMTI_5B) {
      process_sumti_5b(x);
    } else if (nt->type == SUMTI_TAIL_1A) {
      process_sumti_tail_1a(x);
    }

    nc = nt->nchildren;
    for (i=0; i<nc; i++) {
      c = nt->children[i];
      scan_for_selbri_in_sumti(c);
    }
  }
}/*}}}*/
static void check_tu1_for_links(TreeNode *tu1)/*{{{*/
{
/* Check whether there are linked sumti and add property */

  TreeNode *la, *tm, *lk, *tc, *tcc;
  TermVector tv;
  XTermVector *xtv;

  type_check(tu1, TANRU_UNIT_1);

  if (tu1->data.nonterm.nchildren > 1) {
    /* There is a linkargs field */
    tv_init(&tv);
    la = tu1->data.nonterm.children[1]; /* linkargs */
    type_check(la, LINKARGS);
    do {
      tm = find_nth_child(la, 1, TERM);
      lk = find_nth_child(la, 1, LINKS);
      assert(tm); /* Always a term present */
      /* But not always an lk */
      
      /* Now, what sort of term is it? Have to drill down one layer further */
      tc = child_ref(tm, 0);

      switch (tc->data.nonterm.type) {
        case TERM_PLAIN_SUMTI:
          tv.nodes[tv.n_nodes].type = TRM_POS;
          tv.nodes[tv.n_nodes].node = tm;
          tv.n_nodes++;
          break;
        case TERM_PLACED_SUMTI:
          {
            int pos;
            tcc = child_ref(tc, 0);
            pos = recover_fa_conv(tcc);
            if (pos == 0) {
              tv.nodes[tv.n_nodes].type = TRM_FAhI;
              tv.nodes[tv.n_nodes].node = tm;
              tv.nodes[tv.n_nodes].pos = 0;
            } else {
              tv.nodes[tv.n_nodes].type = TRM_FA;
              tv.nodes[tv.n_nodes].node = tm;
              tv.nodes[tv.n_nodes].pos = pos;
            }
            tv.n_nodes++;
          }
          break;
        case TERM_TAGGED_SUMTI:
        case TAGGED_TERMSET:
          tv.nodes[tv.n_nodes].type = TRM_TAG;
          tv.nodes[tv.n_nodes].node = tm;
          tv.n_nodes++;
          break;
        case TERMSET:
          fprintf(stderr, "Termset at line %d ignored, place tagging will be faulty\n", tm->start_line);
          break;
        case TERM_FLOATING_TENSE:
        case TERM_FLOATING_NEGATE:
        case TERM_OTHER:
        default:
          /* None of these are interesting for place tagging */
          break;
      }
      
      la = lk;
    } while (la);

    /* Attach property to node */
    xtv = prop_term_vector(tu1, YES);
    xtv->vec = new(TermVector);
    *(xtv->vec) = tv;
  }
}/*}}}*/
static void scan_tu1_phase1(TreeNode *x)/*{{{*/
{
  /* Find any tanru_unit_1 with linked sumti on it, and build a termvector
     property to attach to it.  */

  int nc, i;
  struct nonterm *nt;
  TreeNode *c;

  if (x->type == N_NONTERM) {
    nt = &x->data.nonterm;

    if (nt->type == TANRU_UNIT_1) {
      check_tu1_for_links(x);
    }

    nc = nt->nchildren;
    for (i=0; i<nc; i++) {
      c = nt->children[i];
      scan_tu1_phase1(c);
    }
  }
}
/*}}}*/
static void scan_tu1_phase2(TreeNode *x)/*{{{*/
{
  int nc, i;
  struct nonterm *nt;
  TreeNode *c;

  if (x->type == N_NONTERM) {
    nt = &x->data.nonterm;

    if (nt->type == TANRU_UNIT_1) {
      XTermVector *xtv;
      XDoneTU1 *xdtu1;

      xtv = prop_term_vector(x, NO);
      xdtu1 = prop_done_tu1(x, NO);
      if (xtv && !xdtu1) {
        LinkConv lc;
        TreeNode *tu2;
        TermVector pre, post;
        
        tu2 = child_ref(x, 0);
        
        lc_init(&lc);
        lc_append_links(&lc, xtv->vec);
        tv_init(&pre);
        tv_init(&post);
        
        process_tanru_unit_2_args(tu2, &pre, &post, &lc);
        xdtu1 = prop_done_tu1(x, YES);
      }
    }

    nc = nt->nchildren;
    for (i=0; i<nc; i++) {
      c = nt->children[i];
      scan_tu1_phase2(c);
    }
  }

}
/*}}}*/
void terms_processing(TreeNode *top)/*{{{*/
{
  scan_tu1_phase1(top);
  scan_for_sentence_parents(top);
  scan_for_selbri_in_sumti(top);
  scan_tu1_phase2(top);
}
/*}}}*/