File: gram.y

package info (click to toggle)
nickle 2.107
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 3,756 kB
  • sloc: ansic: 27,954; yacc: 1,874; lex: 954; sh: 204; makefile: 13; lisp: 1
file content (2121 lines) | stat: -rw-r--r-- 50,668 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
%{

/*
 * Copyright © 1988-2004 Keith Packard and Bart Massey.
 * All Rights Reserved.  See the file COPYING in this directory
 * for licensing information.
 */

#include	"nickle.h"
#include	<stdio.h>

/*
 * This grammar generates 1 shift/reduce conflict and 4 reduce/reduce:
 * 
 * 2 reduce/reduce conflicts on:
 *
 *	    *int . func 
 *	    **int . func
 *
 *	Is the '*' a dereference operator or a pointer type modifier?
 *	The grammar is ordered to make this a type modifier since
 *	the other way is less common.  Use parens when you want this.
 *
 * 2 reduce/reduce conflicts on:
 *
 *	   &int . func
 *	   &&int . func
 *
 *	Is the '&' a reference operator or a reference type modifier?
 *	The grammar is ordered to make this a type modifier since
 *	the other way is less common.  Use parens when you want this.
 *
 *  shift/reduce conflict in ASSIGN in initializers
 *
 *	That's because struct initializers look like assignment
 *	expressions while array initializers permit any expression.
 *	Shift yields struct initialization, so the effect is
 *	to make assignment expressions invalid in array initializers.
 *	(Of course, you can always enclose an assignment in parens.)
 *      (I can't see any obvious way to use operator precedence to
 *      get rid of this: seems like you'd need to duplicate simpleexpr
 *      with different precedence. --BCM)
 *
 */
int ignorenl;
int notCommand;
int seeComment;
NamespacePtr	LexNamespace;
int funcDepth;

void ParseError (char *fmt, ...);
void yyerror (char *msg);

typedef enum _CanonTypeResult {
    CanonTypeUndefined,
    CanonTypeForward,
    CanonTypeDefined,
} CanonTypeResult;
    
static Expr *
ParseCanonFor (Expr *expr);

static CanonTypeResult
ParseCanonType (TypePtr type, Bool forwardAllowed);

static SymbolPtr
ParseNewSymbol (Publish publish, Class class, Type *type, Atom name);

%}

%union {
    int		    ints;
    Value	    value;
    Class	    class;
    ArgType	    *argType;
    Type	    *type;
    Publish	    publish;
    ExprPtr	    expr;
    Atom	    atom;
    DeclListPtr	    declList;
    MemListPtr	    memList;
    Fulltype	    fulltype;
    ArgDecl	    argDecl;
    SymbolPtr	    symbol;
    NamespacePtr    namespace;
    CodePtr	    code;
    Bool	    boolean;
    AtomListPtr	    atomList;
    FuncDecl	    funcDecl;
}

%type  <expr>	    fullname fullnames
%type  <expr>	    opt_rawnames rawname rawnames rawnamespace
%type  <atom>	    rawatom
%type  <expr>	    block opt_func_body func_body func_right catches catch
%type  <expr>	    statements statement simple_statement
%type  <expr>	    if_statement try_statement try_body_statement
%type  <expr>	    block_or_expr
%type  <expr>	    case_block cases case
%type  <expr>	    union_case_block union_cases union_case
%type  <expr>	    fulltype
%type  <expr>	    namespace
%type  <expr>	    comprehension compnames compname comparray
%type  <atomList>   atoms
%type  <declList>   initnames typenames
%type  <symbol>	    name opt_name
%type  <funcDecl>   func_decl func_name
%type  <atom>	    typename 
%type  <expr>	    opt_init
%type  <fulltype>   decl next_decl
%type  <type>	    opt_type type subscripts subtype
%type  <expr>	    opt_stars stars dotdotdots
%type  <type>	    basetype
%type  <expr>	    dims
%type  <memList>    struct_members union_members
%type  <class>	    class opt_class
%type  <publish>    opt_publish publish publish_extend
%type  <atom>	    namespacename

%type  <argType>    opt_argdecls argdecls
%type  <argDecl>    argdecl
%type  <argType>    opt_argdefines argdefines args
%type  <argDecl>    argdefine
%type  <boolean>    opt_dotdotdot

%type  <expr>	    opt_expr for_exprs expr opt_exprs exprs simpleexpr
%type  <expr>	    opt_actuals actuals
%type  <expr>	    comma_expr
%type  <ints>	    assignop
%type  <value>	    opt_integer integer
%type  <value>	    doc_string opt_comment
%type  <expr>	    opt_arrayinit arrayinit arrayelts  arrayelt
%type  <expr>	    opt_hashinit hashinit hashelts hashelt hashvalue
%type  <expr>	    structinit structelts structelt
%type  <expr>	    init

%token		    VAR EXPR ARRAY STRUCT UNION ENUM COMP HASH

%token		    SEMI MOD OC CC DOLLAR DOTDOTDOT ENDFILE
%token <class>	    GLOBAL AUTO STATIC CONST
%token <type>	    POLY INTEGER NATURAL RATIONAL REAL COMPLEX STRING FOREIGN
%token <type>	    FILET MUTEX SEMAPHORE CONTINUATION THREAD VOID BOOL
%token		    FUNCTION FUNC EXCEPTION RAISE
%token		    TYPEDEF IMPORT NEW ANONINIT
%token <namespace>  NAMESPACE
%token <publish>    PUBLIC PROTECTED EXTEND
%token		    WHILE DO FOR SWITCH
%token		    BREAK CONTINUE RETURNTOK FORK CASE DEFAULT
%token		    TWIXT
%token <atom>	    NAME TYPENAME NAMESPACENAME COMMAND NAMECOMMAND
%token <value>	    TEN_NUM OCTAL0_NUM OCTAL_NUM BINARY_NUM HEX_NUM
%token <value>	    TEN_FLOAT OCTAL0_FLOAT OCTAL_FLOAT BINARY_FLOAT HEX_FLOAT
%token <value>	    CHAR_CONST STRING_CONST POLY_CONST THREAD_CONST
%token <value>	    COMMENT_CONST
%token <value>	    VOIDVAL BOOLVAL
%token		    DARROW
%token		    ISTYPE HASMEMBER

%nonassoc 	POUND
%right		COMMA
%right <ints>	ASSIGN
		ASSIGNPLUS ASSIGNMINUS ASSIGNTIMES ASSIGNDIVIDE 
		ASSIGNDIV ASSIGNMOD ASSIGNPOW
		ASSIGNSHIFTL ASSIGNSHIFTR
		ASSIGNLXOR ASSIGNLAND ASSIGNLOR
                ASSIGNOR ASSIGNAND
%right		FORK
%right		QUEST COLON
%left		OR
%left		AND
%left		LOR
%left		LXOR
%left		LAND
%left		EQ NE
%left		LT GT LE GE
%left		SHIFTL SHIFTR
%left		PLUS MINUS
%left		TIMES DIVIDE DIV MOD
%right		POW STARSTAR POW2 POW3
%left		UNIONCAST
%right		UMINUS BANG FACT LNOT INC DEC STAR AMPER THREADID
%left		OS CS DOT ARROW STAROS CALL OP CP
%right		POINTER
%right		COLONCOLON

%left IF TRY NONL
%left ELSE CATCH NL

%%
lines		: lines pcommand
		|
		;
pcommand	: command
		| error reset eend
		;
eend		: NL
		    { yyerrok; }
		| SEMI
		    { yyerrok; }
		| ENDFILE
		    { yyerrok; }
		;

/*
 * Sprinkled through the grammer to switch the lexer between reporting
 * and not reporting newlines
 */
ignorenl	:
		    { ignorenl++; }
		;
attendnl	:
		    { if (ignorenl > 0) ignorenl--; }
		;
reset		:
		    { 
			ignorenl = 0; 
			LexNamespace = 0; 
			CurrentNamespace = TopNamespace; 
			funcDepth = 0; 
			notCommand = 0;
			lastThreadError = True;
		    }

		;

/*
 * To make commands only recognized as the first token on a line,
 * this production precedes every top-level production in the grammar.
 * As the parser will look one token ahead, this cleverly makes only
 * the first token on the line match commands
 */
not_command	:
		    { notCommand = 1; }
		;
opt_nl		: NL
		|  %prec NONL
		;
opt_semi	: SEMI
		|
		;
opt_comma	: COMMA
		|
		;
/*
 * Interpreter command level
 */
command		: not_command expr reset NL
		    {
			ENTER ();
			ExprPtr	e;
			Value	t;
			NamespacePtr	s;
			FramePtr	f;
			CodePtr		c;
			
			e = BuildCall ("Command", "display",
				       1,NewExprTree (EXPR, $2, 0));
			GetNamespace (&s, &f, &c);
			t = NewThread (f, CompileExpr (e, c));
			ThreadsRun (t, 0);
			EXIT ();
		    }
		| not_command expr POUND expr reset NL
		    {
			ENTER ();
			ExprPtr	e;
			Value	t;
			NamespacePtr	s;
			FramePtr	f;
			CodePtr		c;

			e = BuildCall("Command", "display_base",
				      2, NewExprTree (EXPR, $2, 0), $4);
			GetNamespace (&s, &f, &c);
			t = NewThread (f, CompileExpr (e, c));
			ThreadsRun (t, 0);
			EXIT ();
		    }
		| not_command statement reset opt_nl
		    { 
			ENTER ();
			NamespacePtr    s;
			FramePtr	f;
			CodePtr		c;
			Value		t;
			
			GetNamespace (&s, &f, &c);
			t = NewThread (f, CompileStat ($2, c));
			ThreadsRun (t, 0);
			EXIT ();
		    }
		| not_command COMMAND opt_exprs reset opt_semi NL
		    {
			ENTER();
			ExprPtr	e;
			Value	t;
			NamespacePtr	s;
			FramePtr	f;
			CodePtr		c;
			CommandPtr	cmd;

			cmd = CommandFind (CurrentCommands, $2);
			if (!cmd)
			    ParseError ("Undefined command \"%s\"", AtomName ($2));
			else
			{
			    e = NewExprTree (OP, 
					     NewExprConst (POLY_CONST, cmd->func),
					     $3);
			    GetNamespace (&s, &f, &c);
			    t = NewThread (f, CompileExpr (e, c));
			    ThreadsRun (t, 0);
			}
			EXIT ();
		    }
		| not_command NAMECOMMAND opt_rawnames reset opt_semi NL
		    {
			ENTER ();
			ExprPtr e;

			Value	t;
			NamespacePtr	s;
			FramePtr	f;
			CodePtr		c;
			CommandPtr	cmd;

			cmd = CommandFind (CurrentCommands, $2);
			if (!cmd)
			    ParseError ("Undefined command \"%s\"", AtomName ($2));
			else
			{
			    e = NewExprTree (OP, 
					     NewExprConst (POLY_CONST, cmd->func),
					     $3);
			    GetNamespace (&s, &f, &c);
			    t = NewThread (f, CompileExpr (e, c));
			    ThreadsRun (t, 0);
			}
			EXIT ();
		    }
		| NL
		| ENDFILE
		;
opt_rawnames	: rawnames
		|
		    { $$ = 0; }
		;
rawnames	: rawname COMMA rawnames
		    { $$ = NewExprTree (COMMA, $1, $3); }
		| rawname
		    { $$ = NewExprTree (COMMA, $1, 0); }
		;
rawname		: rawnamespace rawatom
		    { $$ = BuildRawname ($1, $2); }
		;
rawatom		: NAME
		| NAMESPACENAME
		| TYPENAME
		;
rawnamespace	: rawnamespace rawatom COLONCOLON
		    { $$ = NewExprTree (COLONCOLON, $1, NewExprAtom ($2, 0, False)); }
                |
		    { $$ = 0; }
                ;
fulltype	: namespace TYPENAME
		    { 
			$$ = BuildFullname ($1, $2);
			LexNamespace = 0;
		    }
		| TYPENAME
		    { 
			$$ = BuildFullname (0, $1);
			LexNamespace = 0;
		    }
		;
fullname	: namespace namespacename
		    { 
			$$ = BuildFullname ($1, $2);
			LexNamespace = 0;
		    }
		| namespacename
		    {
			$$ = BuildFullname (0, $1);
			LexNamespace = 0;
		    }
		;
fullnames	: fullname
		    {
			$$ = $1;
		    }
		| fullname COMMA fullnames
		    {
			$$ = NewExprTree (COMMA, $1, $3);
		    }
		;
namespace	: namespace NAMESPACENAME COLONCOLON
		    { 
			ExprPtr	    e;
			SymbolPtr   symbol;

			e = BuildFullname ($1, $2);
			if (e->base.tag == COLONCOLON)
			    symbol = e->tree.right->atom.symbol;
			else
			    symbol = e->atom.symbol;
			if (!symbol)
			{
			    ParseError ("non-existant namespace \"%A\"", $2);
			    YYERROR;
			}
			else if (symbol->symbol.class != class_namespace)
			{
			    ParseError ("%A is not a namespace", $2);
			    YYERROR;
			}
			LexNamespace = symbol->namespace.namespace;
			$$ = e;
		    }
		| NAMESPACENAME COLONCOLON
		    { 
			ExprPtr	    e;
			SymbolPtr   symbol;

			e = BuildFullname (0, $1);
			if (e->base.tag == COLONCOLON)
			    symbol = e->tree.right->atom.symbol;
			else
			    symbol = e->atom.symbol;
			if (!symbol)
			{
			    ParseError ("non-existant namespace \"%A\"", $1);
			    YYERROR;
			}
			else if (symbol->symbol.class != class_namespace)
			{
			    ParseError ("%A is not a namespace", $1);
			    YYERROR;
			}
			LexNamespace = symbol->namespace.namespace;
			$$ = e;
		    }
		;
namespacename	:   NAME
		|   NAMESPACENAME
		;
/*
* Statements
*/
namespace_start	:
		    { CurrentNamespace = NewNamespace (CurrentNamespace); }
		;
namespace_end	:
		    { CurrentNamespace = CurrentNamespace->previous; }
		;
block		: block_start statements block_end
		    { $$ = $2; }
		;
block_start	: OC namespace_start
		;
block_end	: namespace_end CC
		;
statements	: statement statements
		    { $$ = NewExprTree(OC, $1, $2); }
		|
		    { $$ = NewExprTree(OC, 0, 0); }
		;
if_statement	: IF ignorenl namespace_start OP expr CP statement %prec IF
		    { $$ = NewExprTree(IF, $5, $7); }
		| IF ignorenl namespace_start OP expr CP statement ELSE statement
		    { $$ = NewExprTree(ELSE, $5, NewExprTree(ELSE, $7, $9)); }

statement:	simple_statement
		| block
		;

simple_statement: if_statement namespace_end attendnl
                    { $$ = $1; }
		| WHILE ignorenl namespace_start OP expr CP statement namespace_end attendnl
		    { $$ = NewExprTree(WHILE, $5, $7); }
		| DO ignorenl namespace_start statement WHILE OP expr CP namespace_end attendnl
		    { $$ = NewExprTree(DO, $4, $7); }
		| FOR ignorenl namespace_start OP for_exprs CP statement namespace_end attendnl
		    { Expr *expr = ParseCanonFor($5);
		      if (!expr)
                          YYERROR;
		      $$ = NewExprTree(FOR, expr, $7); }
		| SWITCH ignorenl namespace_start OP expr CP case_block namespace_end attendnl
		    { $$ = NewExprTree (SWITCH, $5, $7); }
		| union_or_enum SWITCH ignorenl namespace_start OP expr CP union_case_block namespace_end attendnl
		    { $$ = NewExprTree (UNION, $6, $8); }
		| BREAK SEMI
		    { $$ = NewExprTree(BREAK, (Expr *) 0, (Expr *) 0); }
		| CONTINUE SEMI
		    { $$ = NewExprTree(CONTINUE, (Expr *) 0, (Expr *) 0); }
		| RETURNTOK opt_expr SEMI
		    { $$ = NewExprTree (RETURNTOK, (Expr *) 0, $2); }
		| expr SEMI
		    { $$ = NewExprTree(EXPR, $1, (Expr *) 0); }
		| SEMI
		    { $$ = NewExprTree(SEMI, (Expr *) 0, (Expr *) 0); }
		| func_decl doc_string opt_func_body namespace_end
		    { 
			DeclList    *decl = $1.decl;
			SymbolPtr   symbol = decl->symbol;
			Class	    class = $1.type.class;
			Publish	    publish = $1.type.publish;
			TypePtr	    type = $1.type.type;
			TypePtr	    ret = type->func.ret;
			ArgType	    *argType = type->func.args;

			if (symbol)
			{
			    if ($3)
			    {
				symbol->symbol.forward = False;
				ParseCanonType (ret, False);
				decl->init = NewExprCode (NewFuncCode (ret,
								       argType,
								       $3,
								       $2),
							  NewExprAtom (symbol->symbol.name, symbol, False));
			    }
			    else
				symbol->symbol.forward = True;
			}
			$$ = NewExprDecl (FUNC, decl, class, type, publish);
		    }
		| opt_publish EXCEPTION ignorenl NAME namespace_start opt_argdecls namespace_end doc_string SEMI attendnl
		    { 
			DeclListPtr decl;

			decl = NewDeclList ($4, 0, 0);
			decl->symbol = ParseNewSymbol ($1, 
						       class_exception, 
						       typePoly, $4);
			decl->symbol->exception.doc = $8;
			$$ = NewExprDecl (EXCEPTION,
					  decl,
					  class_exception,
					  NewTypeFunc (typePoly, $6),
					  $1);
		    }
		| RAISE fullname OP opt_exprs CP SEMI
		    { $$ = NewExprTree (RAISE, $2, $4); }
		| opt_publish TYPEDEF ignorenl typenames SEMI attendnl
		    { 
			DeclListPtr decl;

			for (decl = $4; decl; decl = decl->next)
			    decl->symbol = ParseNewSymbol ($1, class_typedef,
							   0, decl->name);
			$$ = NewExprTree (TYPEDEF, NewExprDecl (TYPEDEF, $4, class_typedef, 0, $1), 0);
		    }
		| opt_publish TYPEDEF ignorenl type typenames SEMI attendnl
		    { 
			DeclListPtr decl;

			for (decl = $5; decl; decl = decl->next)
			    decl->symbol = ParseNewSymbol ($1, class_typedef,
							   $4, decl->name);
			$$ = NewExprTree (TYPEDEF, NewExprDecl (TYPEDEF, $5, class_typedef, $4, $1), 0);
		    }
		| publish_extend NAMESPACE ignorenl namespacename
		    {
			SymbolPtr   symbol;
			Publish	    publish = $1;
			
			/*
			 * this is a hack - save the current namespace
			 * on the parser stack to be restored after
			 * the block is compiled.
			 */
			$2 = CurrentNamespace;
			if (publish == publish_extend)
			{
			    symbol = NamespaceFindName (CurrentNamespace, $4, True);
			    if (!symbol)
			    {
				ParseError ("non-existant namespace %A", $4);
				YYERROR;
			    }
			    else if (symbol->symbol.class != class_namespace)
			    {
				ParseError ("%A is not a namespace", $4);
				YYERROR;
			    }
			    else
				CurrentNamespace = symbol->namespace.namespace;
			}
			else
			{
			    symbol = ParseNewSymbol ($1, class_namespace,
						     0, $4);
			    CurrentNamespace = NewNamespace (CurrentNamespace);
			    symbol->namespace.namespace = CurrentNamespace;
			}
			/*
			 * Make all of the symbols visible while compiling within
			 * the namespace
			 */
			if (CurrentNamespace != $2)
			    CurrentNamespace->publish = publish_public;
		    }
			OC statements CC attendnl
		    {
			/*
			 * close the namespace to non-public lookups
			 */
			if (CurrentNamespace != $2)
			    CurrentNamespace->publish = publish_private;
			/*
			 * Restore to the namespace saved on
			 * the parser stack
			 */
			CurrentNamespace = $2;
			$$ = NewExprTree (NAMESPACE, NewExprAtom ($4, 0, False), $7);
		    }
		| opt_publish IMPORT ignorenl fullnames SEMI attendnl
		    {
			SymbolPtr	symbol;
			ExprPtr		p, e, n;

			p = $4;
			for (p = $4; p; p = n)
			{
			    if (p->base.tag == COMMA)
			    {
				e = p->tree.left;
				n = p->tree.right;
			    }
			    else
			    {
				e = p;
				n = 0;
			    }
			    if (e->base.tag == COLONCOLON)
				e = e->tree.right;
			    symbol = e->atom.symbol;
			    if (!symbol)
			    {
				ParseError ("non-existant namespace %A", e->atom.atom);
				YYERROR;
			    }
			    else if (symbol->symbol.class != class_namespace)
			    {
				ParseError ("%A is not a namespace", e->atom.atom);
				YYERROR;
			    }
			    NamespaceImport (CurrentNamespace, 
					     symbol->namespace.namespace, $1);
			}
			$$ = NewExprTree (IMPORT, $4, NewExprDecl (IMPORT,
								   0, 
								   class_undef,
								   0,
								   $1));
		    }
		| try_statement attendnl
		    { $$ = $1; }
		| TWIXT ignorenl namespace_start OP opt_expr SEMI opt_expr CP statement namespace_end attendnl
		    { $$ = NewExprTree (TWIXT, 
					NewExprTree (TWIXT, $5, $7),
					NewExprTree (TWIXT, $9, 0));
		    }
		;
for_exprs       : opt_expr SEMI for_exprs
                    { $$ = NewExprTree(SEMI, $1, $3); }
		| opt_expr
		    { $$ = NewExprTree(SEMI, $1, 0); }
		;

try_body_statement:  simple_statement
		| OC statements CC
		    { $$ = $2; }

try_statement:  TRY ignorenl try_body_statement catches
		    { $$ = NewExprTree (CATCH, $4, $3); }
                ;

catches		:   catches catch %prec TRY
		    { $$ = NewExprTree (CATCH, $1, $2); }
		|   
		    { $$ = 0; }
		;
catch		: CATCH fullname namespace_start args doc_string block namespace_end
		    { $$ = NewExprCode (NewFuncCode (typePrim[rep_void],
						     $4, $6, $5),
					$2); 
		    }
		;
opt_func_body	: func_body
		| SEMI
		    { $$ = 0; }
		;
func_body    	: { ++funcDepth; } block_or_expr { --funcDepth; $$ = $2; }
		;
block_or_expr	: block
		    { $$ = $1; }
		| attendnl ASSIGN simpleexpr SEMI
		    { 
			$$ = NewExprTree (OC,
					  NewExprTree (RETURNTOK, 0, $3),
					  NewExprTree (OC, 0, 0));
		    }
		;
union_or_enum	: UNION
		| ENUM
		;
see_comment	:
		    { seeComment = 1; }
		;
doc_string	: see_comment opt_comment 
		    { seeComment = 0; $$ = $2; }
		;
opt_comment	: COMMENT_CONST
		|
		    { $$ = Void; }
		;
case_block	: block_start cases block_end
		    { $$ = $2; }
		;
cases		: case cases
		    { $$ = NewExprTree (CASE, $1, $2); }
		|
		    { $$ = 0; }
		;
case		: CASE expr COLON statements
		    { $$ = NewExprTree (CASE, $2, $4); }
		| DEFAULT COLON statements
		    { $$ = NewExprTree (CASE, 0, $3); }
		;
union_case_block: block_start union_cases block_end
		    { $$ = $2; }
		;
union_cases	: union_case union_cases
		    { $$ = NewExprTree (CASE, $1, $2); }    
		|
		    { $$ = 0; }
		;
union_case	: CASE namespace_start NAME opt_name COLON statements namespace_end
		    { 
			SymbolPtr   sym = $4;
			Atom	    sym_atom = sym ? sym->symbol.name : 0;
			ExprPtr	    name = 0;
			
			if (sym)
			    name = NewExprAtom (sym_atom, sym, False);
			    
			$$ = NewExprTree (CASE, 
					  NewExprTree (CASE,
						       NewExprAtom ($3, 0, False),
						       name),
					  $6);
		    }
		| DEFAULT COLON statements
		    { $$ = NewExprTree (CASE, 0, $3); }
		;
opt_name	: NAME
		    {
			$$ = ParseNewSymbol (publish_private,
					     class_undef,
					     typePoly,
					     $1);
		    }
		|
		    { $$ = 0; }
		;
/*
* Identifiers
*/
atoms		: NAME COMMA atoms
		    { $$ = NewAtomList ($3, $1); }
		| NAME
		    { $$ = NewAtomList (0, $1); }
		;
typenames	: typename COMMA typenames
		    { $$ = NewDeclList ($1, 0, $3); }
		| typename
		    { $$ = NewDeclList ($1, 0, 0); }
		;
typename	: TYPENAME
		| NAME
		;

/*
 * Ok, a few cute hacks to fetch the fulltype from the
 * value stack -- initnames always immediately follows a decl,
 * so $0 will be a fulltype.  Note the cute trick to store the
 * type across the comma operator -- this means that name
 * will always find the fulltype at $0 as well
 */
initnames	: name opt_init next_decl initnames
		    { 
			if ($1)
			{
			    $$ = NewDeclList ($1->symbol.name, $2, $4); 
			    $$->symbol = $1;
			}
			else
			    $$ = $4;
		    }
		| name opt_init
		    { 
			if ($1)
			{
			    $$ = NewDeclList ($1->symbol.name, $2, 0);
			    $$->symbol = $1;
			}
			else
			    $1 = 0;
		    }
		;
name		: NAME
		    {	
			$$ = ParseNewSymbol ($<fulltype>0.publish,
					     $<fulltype>0.class,
					     $<fulltype>0.type,
					     $1);
		    }
		;
/* 
 * next_decl -- look backwards three entries on the stack to
 * find the previous type
 */
next_decl	: COMMA
		    { $$ = $<fulltype>-2; }
		;
/*
 * Declaration of a function
 */
func_decl	: func_name namespace_start opt_argdefines CP
		    {
			NamespacePtr	save = CurrentNamespace;
			SymbolPtr	symbol;
			Type		*type = NewTypeFunc ($1.type.type, $3);
			/*
			 * namespace_start pushed a new namespace, make sure
			 * this symbol is placed in the original namespace
			 */
			CurrentNamespace = save->previous;
			symbol = NamespaceFindName (CurrentNamespace, $1.decl->name, True);
			if (symbol && symbol->symbol.forward)
			{
			    if (!TypeIsSupertype (symbol->symbol.type, type))
			    {
				ParseError ("%A redefinition with different type",
					    $1.decl->name);
				symbol = 0;
			    }
			}
			else
			{
			    symbol = ParseNewSymbol ($1.type.publish,
						     $1.type.class, 
						     type,
						     $1.decl->name);
			}
			CurrentNamespace = save;
			$$ = $1;
			$$.decl->symbol = symbol;
			$$.type.type = type;
		    }
		;
func_name	: decl NAME OP
		    { $$.type = $1; $$.decl = NewDeclList ($2, 0, 0); }
		| decl FUNCTION NAME OP
		    { $$.type = $1; $$.decl = NewDeclList ($3, 0, 0); }
		| FUNCTION ignorenl NAME OP
		    { 
			$$.type.publish = publish_private;
			$$.type.class = class_undef;
			$$.type.type = typePoly;
			$$.decl = NewDeclList ($3, 0, 0); 
		    }
		;
opt_init	: ASSIGN simpleexpr
		    { $$ = $2; }
		| ASSIGN init
		    { $$ = $2; }
		|
		    { $$ = 0; }
		;
/*
 * Full declaration including storage, type and publication
 */
decl		: publish opt_class opt_type opt_nl
		    { $$.publish = $1; $$.class = $2; $$.type = $3; }
		| class opt_type opt_nl
		    { $$.publish = publish_private; $$.class = $1; $$.type = $2; }
		| type opt_nl
		    { $$.publish = publish_private; $$.class = class_undef; $$.type = $1; }
		;
/*
 * Type declarations
 */
subscripts	: opt_argdecls subscripts	    %prec CALL
		    { $$ = NewTypeFunc ($2, $1); }
		| OS opt_stars CS subscripts    
		    { $$ = NewTypeArray ($4, $2, False); }
		| OS dotdotdots CS subscripts    
		    { $$ = NewTypeArray ($4, $2, True); }
		| OS dims CS subscripts
		    { $$ = NewTypeArray ($4, $2, False); }
		| OS type CS subscripts
		    { $$ = NewTypeHash ($4, $2); }
		|
		    { $$ = 0; }
		;
type		: subtype subscripts	    %prec CALL
		    {
			Type	*top = $2;
			Type	*t, **bot = &top;

			/*
			 * Walk down the type chain to hang the
			 * base type off of the end.  This
			 * makes int[]() be an array of functions
			 * rather than a function returning an array
			 */
			while ((t = *bot))
			    switch (t->base.tag) {
			    case type_array:
				bot = &t->array.type;
				break;
			    case type_hash:
				bot = &t->hash.type;
				break;
			    case type_func:
				bot = &t->func.ret;
				break;
			    default:
				assert(0);
			    }
			*bot = $1;
			$$ = top;
		    }
		| TIMES type			%prec POINTER
		    { $$ = NewTypeRef ($2, True); }
		| STARSTAR type			%prec POINTER
		    { $$ = NewTypeRef (NewTypeRef ($2, True), True); }
		| AND type			%prec POINTER
		    { $$ = NewTypeRef (NewTypeRef ($2, False), False); }
		| LAND type			%prec POINTER
		    { $$ = NewTypeRef ($2, False); }
		| type PLUS type
		    { 
			if (ParseCanonType ($1, False) != CanonTypeDefined)
			    YYERROR;
			if (ParseCanonType ($3, False) != CanonTypeDefined)
			    YYERROR;
			$$ = NewTypePlus ($1, $3); 
			if (!$$)
			    YYERROR;
		    }
		;
subtype		: basetype
		| STRUCT OC struct_members CC
		    {
			AtomListPtr	al;
			StructType	*st;
			MemListPtr	ml;
			int		nelements;

			nelements = 0;
			for (ml = $3; ml; ml = ml->next)
			{
			    for (al = ml->atoms; al; al = al->next)
				nelements++;
			}
			st = NewStructType (nelements);
			nelements = 0;
			for (ml = $3; ml; ml = ml->next)
			{
			    for (al = ml->atoms; al; al = al->next)
			    {
				AddBoxType (&st->types, ml->type);
				StructTypeAtoms(st)[nelements] = al->atom;
				nelements++;
			    }
			}
			$$ = NewTypeStruct (st);
		    }
		| UNION OC union_members CC
		    {
			AtomListPtr	al;
			StructType	*st;
			MemListPtr	ml;
			int		nelements;

			nelements = 0;
			for (ml = $3; ml; ml = ml->next)
			{
			    for (al = ml->atoms; al; al = al->next)
				nelements++;
			}
			st = NewStructType (nelements);
			nelements = 0;
			for (ml = $3; ml; ml = ml->next)
			{
			    for (al = ml->atoms; al; al = al->next)
			    {
				AddBoxType (&st->types, ml->type);
				StructTypeAtoms(st)[nelements] = al->atom;
				nelements++;
			    }
			}
			$$ = NewTypeUnion (st, False);
		    }
		| ENUM OC atoms CC
		    {
			AtomListPtr	al;
			StructType	*st;
			int		nelements;

			nelements = 0;
			for (al = $3; al; al = al->next)
			    nelements++;
			
			st = NewStructType (nelements);
			nelements = 0;
			for (al = $3; al; al = al->next)
			{
			    AddBoxType (&st->types, typePrim[rep_void]);
			    StructTypeAtoms(st)[nelements] = al->atom;
			    nelements++;
			}
			$$ = NewTypeUnion (st, True);
		    }
			    
		| OP type CP
		    { $$ = $2; }
		| fulltype
		    { $$ = NewTypeName ($1, 0); }
		;
opt_type	: type
		|
		    { $$ = typePoly; }
		;
basetype    	: POLY
		| INTEGER
		| RATIONAL
		| REAL
                | COMPLEX
		| STRING
		| FILET
		| MUTEX
		| SEMAPHORE
		| CONTINUATION
		| THREAD
		| VOID
		| BOOL
		| FOREIGN
		;
opt_stars	: stars
		|
		    { $$ = 0; }
		;
stars		: stars COMMA TIMES
		    { $$ = NewExprTree (COMMA, 0, $1); }
		| TIMES
		    { $$ = NewExprTree (COMMA, 0, 0); }
		;
dotdotdots	: dotdotdots COMMA DOTDOTDOT
		    { $$ = NewExprTree (COMMA, 0, $1); }
		| DOTDOTDOT
		    { $$ = NewExprTree (COMMA, 0, 0); }
		;
dims		: simpleexpr COMMA dims
		    { $$ = NewExprTree (COMMA, $1, $3); }
		| simpleexpr
		    { $$ = NewExprTree (COMMA, $1, 0); }
		;
/*
* Structure member declarations
*/
struct_members	: opt_type atoms SEMI struct_members
		    { $$ = NewMemList ($2, $1, $4); }
		|
		    { $$ = 0; }
		;
union_members	: opt_type atoms SEMI union_members
		    { $$ = NewMemList ($2, $1, $4); }
		|
		    { $$ = 0; }
		;
/*
* Declaration modifiers
*/
opt_class	: class
		|
		    { $$ = class_undef; }
		;
class		: GLOBAL
		| AUTO
		| STATIC
		| CONST
		;
opt_publish	: publish
		|
		    { $$ = publish_private; }
		;

publish		: PUBLIC
		| PROTECTED
		;
publish_extend	: opt_publish
		| EXTEND
		;

/*
* Arguments in function declarations
*/
opt_argdecls	: OP argdecls CP
		    { $$ = $2; }
		| OP CP
		    { $$ = 0; }
		;
argdecls	: argdecl COMMA argdecls
		    { $$ = NewArgType ($1.type, False, $1.name, 0, $3); }
		| argdecl opt_dotdotdot
		    { $$ = NewArgType ($1.type, $2, $1.name, 0, 0); }
		;
argdecl		: type NAME
		    { 
			ParseCanonType ($1, False);
			$$.type = $1; 
			$$.name = $2; 
		    }
		| type
		    { 
			ParseCanonType ($1, False);
			$$.type = $1;
			$$.name = 0; 
		    }
		;

/*
* Arguments in function definitions
*/
args		: OP opt_argdefines CP
		    { $$ = $2; }
		;
opt_argdefines	: ignorenl argdefines
		    {
			ArgType	*args;
			Type	*type;

			for (args = $2; args; args = args->next)
			{
			    type = args->type;
			    if (ParseCanonType (type, False) != CanonTypeDefined)
				break;
			    if (args->varargs)
			    {
				type = NewTypeArray (type,
						     NewExprTree (COMMA,
								  NewExprConst (TEN_NUM, 
										NewInt (0)),
								  0),
						     False);
			    }
			    args->symbol = ParseNewSymbol (publish_private,
							   class_arg, 
							   type, args->name);
			}
			$$ = $2;
		    }
		| ignorenl
		    { $$ = 0; }
		;
argdefines	: argdefine COMMA argdefines
		    { $$ = NewArgType ($1.type, False, $1.name, 0, $3); }
		| argdefine opt_dotdotdot
		    { $$ = NewArgType ($1.type, $2, $1.name, 0, 0); }
		;
argdefine	: opt_type NAME
		    { $$.type = $1; $$.name = $2; }
		| type
		    { $$.type = $1; $$.name = 0; }
		;
opt_dotdotdot	: DOTDOTDOT
		    { $$ = True; }
		|
		    { $$ = False; }
		;

/*
* Expressions, top level includes comma operator and declarations
*/
opt_expr	: expr
		|
		    { $$ = 0; }
		;
expr		: comma_expr
		| decl initnames
		    { 
			DeclList    *decl;

			for (decl = $2; decl; decl = decl->next)
			{
			    if (decl->init)
			    {
				if (!decl->init->base.type)
				    decl->init->base.type = $1.type;
			    }
			}

			$$ = NewExprDecl (VAR, $2, $1.class, $1.type, $1.publish); 
		    }
		;
comma_expr	: simpleexpr
		| comma_expr COMMA simpleexpr
		    { $$ = NewExprTree(COMMA, $1, $3); }
		;
/*
* Expression list, different use of commas for function arguments et al.
*/
opt_exprs	: exprs
		|
		    { $$ = 0; }
		;
exprs		: simpleexpr COMMA exprs
		    { $$ = NewExprTree (COMMA, $1, $3); }
		| simpleexpr
		    { $$ = NewExprTree (COMMA, $1, 0); }
		;
opt_actuals	: actuals
		|
		    { $$ = 0; }
		;
actuals		: simpleexpr COMMA actuals
		    { $$ = NewExprTree (COMMA, $1, $3); }
		| simpleexpr opt_dotdotdot
		    {	
			ExprPtr	arg = $2 ? NewExprTree (DOTDOTDOT, $1, 0) : $1;
			$$ = NewExprTree (COMMA, arg, 0); 
		    }
		;
func_right	: attendnl ASSIGN simpleexpr
		    { 
			$$ = NewExprTree (OC,
					  NewExprTree (RETURNTOK, 0, $3),
					  NewExprTree (OC, 0, 0));
		    }
		| { ++funcDepth; } block { --funcDepth; $$ = $2; }
		;
/*
* Fundemental expression production
*/
simpleexpr	: simpleexpr assignop simpleexpr    		%prec ASSIGN
		    { 
			if ($2 == ASSIGNPOW)
			    $$ = NewExprTree (ASSIGNPOW, 
					      BuildName ("Math", "assign_pow"),
					      NewExprTree (ASSIGNPOW, $1, $3));
			else
			{
			    ExprPtr left = $1;
			    /*
			     * Automatically declare names used in
			     * simple assignements at the top level
			     */
			    if ($2 == ASSIGN && 
				funcDepth == 0 &&
				left->base.tag == NAME && 
				!(left->atom.symbol))
			    {
				$1->atom.symbol = ParseNewSymbol (publish_private,
								  class_undef, 
								  typePoly, 
								  $1->atom.atom);
			    }
			    $$ = NewExprTree($2, $1, $3); 
			}
		    }
		| opt_type FUNC namespace_start args doc_string func_right namespace_end	    	%prec ASSIGN
		    {
			ParseCanonType ($1, False);
			$$ = NewExprCode (NewFuncCode ($1, $4, $6, $5), 0); 
		    }
		| MOD integer						%prec THREADID
		    {   Value	t;
			t = do_Thread_id_to_thread ($2);
			if (t == Void)
			{
			    ParseError ("No thread %v", $2);
			    YYERROR;
			}
			else
			    $$ = NewExprConst(THREAD_CONST, t); 
		    }
		| TIMES simpleexpr					%prec STAR
		    { $$ = NewExprTree(STAR, $2, (Expr *) 0); }
		| STARSTAR simpleexpr					%prec STAR
		    { $$ = NewExprTree(STAR, NewExprTree (STAR, $2, 0), 0); }
		| LAND simpleexpr					%prec AMPER
		    { $$ = NewExprTree(AMPER, $2, (Expr *) 0); }
		| AND simpleexpr					%prec AMPER
		    { $$ = NewExprTree(AMPER, 
				       NewExprTree (AMPER, $2, (Expr *) 0), 
				       (Expr *) 0); }
		| MINUS simpleexpr					%prec UMINUS
		    { $$ = NewExprTree(UMINUS, $2, (Expr *) 0); }
		| LNOT simpleexpr
		    { $$ = NewExprTree(LNOT, $2, (Expr *) 0); }
		| BANG simpleexpr
		    { $$ = NewExprTree(BANG, $2, (Expr *) 0); }
		| simpleexpr BANG					%prec FACT
		    { 
			$$ = NewExprTree(FACT, 
					 BuildName ("Math", "factorial"),
					 $1);
		    }
		| INC simpleexpr
		    { $$ = NewExprTree(INC, $2, (Expr *) 0); }
		| simpleexpr INC
		    { $$ = NewExprTree(INC, (Expr *) 0, $1); }
		| DEC simpleexpr
		    { $$ = NewExprTree(DEC, $2, (Expr *) 0); }
		| simpleexpr DEC
		    { $$ = NewExprTree(DEC, (Expr *) 0, $1); }
		| FORK simpleexpr
		    { $$ = NewExprTree (FORK, (Expr *) 0, $2); }
		| simpleexpr PLUS simpleexpr
		    { $$ = NewExprTree(PLUS, $1, $3); }
		| simpleexpr MINUS simpleexpr
		    { $$ = NewExprTree(MINUS, $1, $3); }
		| simpleexpr TIMES simpleexpr
		    { $$ = NewExprTree(TIMES, $1, $3); }
		| simpleexpr DIVIDE simpleexpr
		    { $$ = NewExprTree(DIVIDE, $1, $3); }
		| simpleexpr DIV simpleexpr
		    { $$ = NewExprTree(DIV, $1, $3); }
		| simpleexpr MOD simpleexpr
		    { $$ = NewExprTree(MOD, $1, $3); }
		| simpleexpr STARSTAR simpleexpr			%prec POW
		    { 
			$$ = NewExprTree(POW, 
					 BuildName ("Math", "pow"), 
					 NewExprTree (POW, $1, $3)); 
		    }
		| simpleexpr POW2
		    {
			$$ = NewExprTree (POW,
					  BuildName("Math", "pow"),
					  NewExprTree (POW, $1,
						       NewExprConst (TEN_NUM,
								     NewInt(2))));
		    }
		| simpleexpr POW3
		    {
			$$ = NewExprTree (POW,
					  BuildName("Math", "pow"),
					  NewExprTree (POW, $1,
						       NewExprConst (TEN_NUM,
								     NewInt(3))));
		    }
		| simpleexpr SHIFTL simpleexpr
		    { $$ = NewExprTree(SHIFTL, $1, $3); }
		| simpleexpr SHIFTR simpleexpr
		    { $$ = NewExprTree(SHIFTR, $1, $3); }
		| simpleexpr QUEST simpleexpr COLON simpleexpr
		    { $$ = NewExprTree(QUEST, $1, NewExprTree(COLON, $3, $5)); }
		| simpleexpr LXOR simpleexpr
		    { $$ = NewExprTree(LXOR, $1, $3); }
		| simpleexpr LAND simpleexpr
		    { $$ = NewExprTree(LAND, $1, $3); }
		| simpleexpr LOR simpleexpr
		    { $$ = NewExprTree(LOR, $1, $3); }
		| simpleexpr AND simpleexpr
		    { $$ = NewExprTree(AND, $1, $3); }
		| simpleexpr OR simpleexpr
		    { $$ = NewExprTree(OR, $1, $3); }
		| simpleexpr EQ simpleexpr
		    { $$ = NewExprTree(EQ, $1, $3); }
		| simpleexpr NE simpleexpr
		    { $$ = NewExprTree(NE, $1, $3); }
		| simpleexpr LT simpleexpr
		    { $$ = NewExprTree(LT, $1, $3); }
		| simpleexpr GT simpleexpr
		    { $$ = NewExprTree(GT, $1, $3); }
		| simpleexpr LE simpleexpr
		    { $$ = NewExprTree(LE, $1, $3); }
		| simpleexpr GE simpleexpr
		    { $$ = NewExprTree(GE, $1, $3); }
		| fullname
		| TEN_NUM
		    { $$ = NewExprConst(TEN_NUM, $1); }
		| OCTAL_NUM
		    { $$ = NewExprConst(OCTAL_NUM, $1); }
		| OCTAL0_NUM
		    { $$ = NewExprConst(OCTAL0_NUM, $1); }
		| BINARY_NUM
		    { $$ = NewExprConst(BINARY_NUM, $1); }
		| HEX_NUM
		    { $$ = NewExprConst(HEX_NUM, $1); }
		| TEN_FLOAT
		    { $$ = NewExprConst(TEN_FLOAT, $1); }
		| OCTAL_FLOAT
		    { $$ = NewExprConst(OCTAL_FLOAT, $1); }
		| OCTAL0_FLOAT
		    { $$ = NewExprConst(OCTAL0_FLOAT, $1); }
		| BINARY_FLOAT
		    { $$ = NewExprConst(BINARY_FLOAT, $1); }
		| HEX_FLOAT
		    { $$ = NewExprConst(HEX_FLOAT, $1); }
		| CHAR_CONST
		    { $$ = NewExprConst(CHAR_CONST, $1); }
		| STRING_CONST
		    { $$ = NewExprConst(STRING_CONST, $1); }
		| VOIDVAL
		    { $$ = NewExprConst(VOIDVAL, $1); }
		| BOOLVAL
		    { $$ = NewExprConst(BOOLVAL, $1); }
		| OS CS
		    { 
			$$ = BuildFullname (0, AtomId ("[]"));
		    }
		| OP type CP namespace_start init namespace_end
		    { 
			ParseCanonType ($2, False);
			if ($5)
			    $5->base.type = $2;
			$$ = NewExprTree (NEW, $5, 0);
			$$->base.type = $2;
		    }
		| OP OS stars CS CP namespace_start arrayinit namespace_end
		    { 
			$7->base.type = NewTypeArray (typePoly, $3, True); 
			ParseCanonType ($7->base.type, False);
			$$ = NewExprTree (NEW, $7, 0); 
			$$->base.type = $7->base.type;
		    }
		| OP OS dims CS CP namespace_start opt_arrayinit namespace_end
		    { 
			TypePtr	t = NewTypeArray (typePoly, $3, False);
			ParseCanonType (t, False);
			$$ = NewExprTree (NEW, $7, 0); 
			if ($7)
			    $7->base.type = t;
			$$->base.type = t;
		    }
		| OP OS type CS CP namespace_start opt_hashinit namespace_end
		    {
			TypePtr t = NewTypeHash (typePoly, $3);
			ParseCanonType (t, False);
			$$ = NewExprTree (NEW, $7, 0);
			if ($7)
			    $7->base.type = t;
			$$->base.type = t;
		    }
		| type DOT NAME						%prec UNIONCAST
		    {
			ParseCanonType ($1, False);
			$$ = NewExprTree (UNION, NewExprAtom ($3, 0, False), 0); 
			$$->base.type = $1;
		    }
		| OP type DOT NAME CP simpleexpr 			%prec UNIONCAST
		    { 
			ParseCanonType ($2, False);
			$$ = NewExprTree (UNION, NewExprAtom ($4, 0, False), $6); 
			$$->base.type = $2;
		    }
		| DOLLAR opt_integer
		    { $$ = BuildCall ("History", "fetch", 1, NewExprConst (TEN_NUM, $2)); }
		| DOT
		    { $$ = NewExprTree (DOLLAR, 0, 0); }
		| OP expr CP
		    { $$ = $2; }
		| OP block CP
		    { $$ = $2; }
		| simpleexpr STAROS dims CS
		    { $$ = NewExprTree (OS, NewExprTree (STAR, $1, (Expr *) 0), $3); }
		| simpleexpr OS dims CS
		    { $$ = NewExprTree(OS, $1, $3); }
		| simpleexpr OP opt_actuals CP				%prec CALL
		    { $$ = NewExprTree (OP, $1, $3); }
		| ISTYPE OP simpleexpr COMMA type CP			%prec CALL
		    {
			TypePtr	type = $5;
			ParseCanonType (type, False);
			$$ = NewExprType (ISTYPE, $3, type);
		    }
		| HASMEMBER OP simpleexpr COMMA NAME CP			%prec CALL
		    { $$ = NewExprTree (HASMEMBER, $3, NewExprAtom($5, 0, False)); }
		| simpleexpr DOT NAME
		    { $$ = NewExprTree(DOT, $1, NewExprAtom ($3, 0, False)); }
		| simpleexpr ARROW NAME
		    { $$ = NewExprTree(ARROW, $1, NewExprAtom ($3, 0, False)); }
		;
opt_integer	: integer
		|
		    { $$ = Zero; }
assignop	: ASSIGNPLUS
		| ASSIGNMINUS
		| ASSIGNTIMES
		| ASSIGNDIVIDE
		| ASSIGNDIV
		| ASSIGNMOD
		| ASSIGNPOW
		| ASSIGNSHIFTL
		| ASSIGNSHIFTR
		| ASSIGNLXOR
		| ASSIGNLAND
		| ASSIGNLOR
		| ASSIGNOR
		| ASSIGNAND
		| ASSIGN
		;
integer		: TEN_NUM
		| OCTAL_NUM
		| OCTAL0_NUM
		| BINARY_NUM
		| HEX_NUM
		;
/*
 * Array initializers
 */
opt_arrayinit	: arrayinit
		| OC CC
		    { $$ = 0; }
		|
		    { $$ = 0; }
		;
arrayinit    	: OC arrayelts opt_comma opt_dotdotdot CC
		    { 
			ExprPtr	elts = $2 ? ExprRehang ($2, 0) : 0;
			if ($4)
			{
			    ExprPtr i = elts;
			    while (i->tree.right)
				i = i->tree.right;
			    i->tree.right = NewExprTree (COMMA, 
							 NewExprTree (DOTDOTDOT, 0, 0),
							 0);
			}
			$$ = NewExprTree (ARRAY, elts, 0); 
		    }
		| OC OS namespace_start compnames comparray CS comprehension namespace_end CC
		    {
			$$ = NewExprTree (COMP, NewExprTree (COMP, $4, $5), $7);
		    }
		;
comprehension	:   ASSIGN arrayelt
		    { $$ = $2; }
		|   block
		;
compnames	: compname COMMA compnames
		    { $$ = NewExprTree (COMMA, $1, $3); }
		| compname
		;
compname	: NAME
		    { 
			SymbolPtr   s;
			s = ParseNewSymbol (publish_private, class_arg, 
					    typePrim[rep_integer], $1);
			$$ = NewExprAtom ($1, s, False); 
		    }
		;
comparray	:
		    {
			SymbolPtr   s;
			Atom	    a = AtomId("[]");
			s = ParseNewSymbol (publish_private, class_undef,
					    typePoly, a);
			$$ = NewExprAtom (a, s, False);
		    }
		;
arrayelts	: arrayelts COMMA arrayelt
		    { $$ = NewExprTree (COMMA, $1, $3); }
		| arrayelt
		    { $$ = NewExprTree (COMMA, 0, $1); }
		;
arrayelt	: simpleexpr
		| init
		;

/*
 * Hash initializers
 */
opt_hashinit	: hashinit
		| OC CC
		    { $$ = 0; }
		|
		    { $$ = 0; }
		;
hashinit	: OC hashelts opt_comma CC
		    {
			ExprPtr elts = $2 ? ExprRehang ($2, 0) : 0;
			$$ = NewExprTree (HASH, elts, 0);
		    }
		;
hashelts	: hashelts COMMA hashelt
		    { $$ = NewExprTree (COMMA, $1, $3); }
		| hashelt
		    { $$ = NewExprTree (COMMA, 0, $1); }
		;
hashelt		: simpleexpr DARROW hashvalue
		    { $$ = NewExprTree (DARROW, $1, $3); }
		| DARROW hashvalue
		    { $$ = NewExprTree (DARROW, 0, $2); }
		;
hashvalue	: simpleexpr
		| init
		;
/* 
* Structure initializers
*/
structinit    	: OC structelts opt_comma CC
		    { $$ = NewExprTree (STRUCT, ExprRehang ($2, 0), 0); }
		;
structelts	: structelts COMMA structelt
		    { $$ = NewExprTree (COMMA, $1, $3); }
		| structelt
		    { $$ = NewExprTree (COMMA, 0, $1); }
		;
structelt	: NAME ASSIGN simpleexpr
		    { $$ = NewExprTree (ASSIGN, NewExprAtom ($1, 0, False), $3); }
		| NAME ASSIGN init
		    { $$ = NewExprTree (ASSIGN, NewExprAtom ($1, 0, False), $3); }
		| DOT NAME ASSIGN simpleexpr
		    { $$ = NewExprTree (ASSIGN, NewExprAtom ($2, 0, False), $4); }
		| DOT NAME ASSIGN init
		    { $$ = NewExprTree (ASSIGN, NewExprAtom ($2, 0, False), $4); }
		;
init		: arrayinit
		| structinit
		| hashinit
		| OC CC
		    { $$ = NewExprTree (ANONINIT, 0, 0); }
		;
%%

static void
DeclListMark (void *object)
{
    DeclListPtr	dl = object;

    MemReference (dl->next);
    MemReference (dl->init);
}

DataType DeclListType = { DeclListMark, 0, "DeclListType" };

DeclListPtr
NewDeclList (Atom name, ExprPtr init, DeclListPtr next)
{
    ENTER ();
    DeclListPtr	dl;

    dl = ALLOCATE (&DeclListType, sizeof (DeclList));
    dl->next = next;
    dl->name = name;
    dl->symbol = 0;
    dl->init = init;
    RETURN (dl);
}

static void
MemListMark (void *object)
{
    MemListPtr	ml = object;

    MemReference (ml->next);
    MemReference (ml->type);
    MemReference (ml->atoms);
}

DataType MemListType = { MemListMark, 0, "MemListType" };

MemListPtr
NewMemList (AtomListPtr atoms, Type *type, MemListPtr next)
{
    ENTER ();
    MemListPtr	ml;

    ml = ALLOCATE (&MemListType, sizeof (MemList));
    ml->next = next;
    ml->type = type;
    ml->atoms = atoms;
    RETURN (ml);
}


extern NamespacePtr	CurrentNamespace;
FramePtr	CurrentFrame;

Value
lookupVar (char *ns, char *n)
{
    ENTER ();
    Value	    v;
    SymbolPtr	    symbol;
    NamespacePtr    namespace;
    Bool	    search;

    search = True;
    namespace = CurrentNamespace;
    if (ns)
    {
	symbol = NamespaceFindName (CurrentNamespace, AtomId (ns), True);
	if (symbol && symbol->symbol.class == class_namespace)
	    namespace = symbol->namespace.namespace;
	else
	    namespace = 0;
    }
    if (namespace)
	symbol = NamespaceFindName (namespace, AtomId (n), search);
    else
	symbol = 0;
    if (symbol && symbol->symbol.class == class_global)
	v = BoxValue (symbol->global.value, 0);
    else
	v = Void;
    RETURN (v);
}

void
setVar (NamespacePtr namespace, char *n, Value v, Type *type)
{
    ENTER ();
    Atom	atom;
    SymbolPtr	symbol;

    atom = AtomId (n);
    symbol = NamespaceFindName (namespace, atom, True);
    if (!symbol)
	symbol = NamespaceAddName (namespace,
				   NewSymbolGlobal (atom, type),
				   publish_private);
    if (symbol->symbol.class == class_global)
	BoxValueSet (symbol->global.value, 0, v);
    EXIT ();
}

void
GetNamespace (NamespacePtr *scope, FramePtr *fp, CodePtr *cp)
{
    *scope = CurrentNamespace;
    *fp = CurrentFrame;
    if (CurrentFrame)
	*cp = CurrentFrame->function->func.code;
    else
	*cp = 0;
}

ExprPtr
BuildName (char *ns, char *n)
{
    ENTER ();
    SymbolPtr	    symbol, ns_symbol = 0;
    Atom	    atom, ns_atom = 0;
    Bool	    search = True;
    NamespacePtr    namespace = CurrentNamespace;
    ExprPtr	    e;
    Bool	    ns_privateFound = False;
    Bool	    privateFound = False;

    if (ns)
    {
	ns_atom = AtomId (ns);
	ns_symbol = NamespaceFindName (namespace, ns_atom, search);
	if (ns_symbol && ns_symbol->symbol.class == class_namespace)
	    namespace = ns_symbol->namespace.namespace;
	else
	{
	    if (!ns_symbol)
		ns_privateFound = NamespaceIsNamePrivate (namespace, ns_atom, search);
	    namespace = 0;
	}
	search = False;
    }
    atom = AtomId (n);
    if (namespace)
    {
	symbol = NamespaceFindName (namespace, atom, search);
	if (!symbol)
	    privateFound = NamespaceIsNamePrivate (namespace, atom, search);
    }
    else
	symbol = 0;
    e = NewExprAtom (atom, symbol, privateFound);
    if (ns_atom)
	e = NewExprTree (COLONCOLON, NewExprAtom (ns_atom, ns_symbol, ns_privateFound), e);
    RETURN (e);
}

static Value
AtomString (Atom id)
{
    ENTER ();
    RETURN (NewStrString (AtomName (id)));
}

ExprPtr
BuildRawname (ExprPtr colonnames, Atom name)
{
    ENTER ();
    int     len;
    Value   array;
    ExprPtr e;

    len = 1;
    for (e = colonnames; e; e = e->tree.left)
	len++;
    array = NewArray (False, False, typePrim[rep_string], 1, &len);
    len--;
    ArrayValueSet (&array->array, len, AtomString (name));
    e = colonnames;
    while (--len >= 0)
    {
	ArrayValueSet (&array->array, len, AtomString (e->tree.right->atom.atom));
	e = e->tree.left;
    }
    e = NewExprConst (POLY_CONST, array);
    RETURN (e);
}

	    
ExprPtr
BuildFullname (ExprPtr left, Atom atom)
{
    ENTER ();
    NamespacePtr    namespace;
    SymbolPtr	    symbol;
    Bool	    search;
    ExprPtr	    nameExpr;
    Bool	    privateFound = False;

    if (left)
    {
	if (left->base.tag == COLONCOLON)
	    symbol = left->tree.right->atom.symbol;
	else
	    symbol = left->atom.symbol;
	if (symbol && symbol->symbol.class == class_namespace)
	    namespace = symbol->namespace.namespace;
	else
	    namespace = 0;
        search = False;
    }
    else
    {
	namespace = CurrentNamespace;
	search = True;
    }
    if (namespace)
    {
	symbol = NamespaceFindName (namespace, atom, search);
	if (!symbol)
	    privateFound = NamespaceIsNamePrivate (namespace, atom, search);
    }
    else
	symbol = 0;
    nameExpr = NewExprAtom (atom, symbol, privateFound);
    if (left)
	nameExpr = NewExprTree (COLONCOLON, left, nameExpr);
    RETURN (nameExpr);
}

ExprPtr
BuildCall (char *scope, char *name, int nargs, ...)
{
    ENTER ();
    va_list	    alist;
    ExprPtr	    args, *prev;
    ExprPtr	    f;
    ExprPtr	    e;

    va_start (alist, nargs);
    prev = &args;
    args = 0;
    while (nargs--)
    {
	*prev = NewExprTree (COMMA, va_arg (alist, ExprPtr), 0);
	prev = &(*prev)->tree.right;
    }
    va_end (alist);
    f = BuildName (scope, name);
    e = NewExprTree (OP, f, args);
#ifdef DEBUG
    printExpr (stdout, e, -1, 0);
    printf ("\n");
#endif
    RETURN (e);
}

/*
 * Walk for() loop arguments and normalize the list
 */
static Expr *
ParseCanonFor (Expr *expr)
{
    if (!expr || !expr->tree.right) {
        ParseError ("Too few exprs in for()\n");
        return 0;
    }
    if (!expr->tree.right->tree.right) {
        /* 2-argument for() */
        expr = NewExprTree(FOR, 0, expr);
    }
    if (expr->tree.right->tree.right->tree.right) {
        ParseError ("Too many exprs in for()\n");
        return 0;
    }
    return expr;
}

/*
 * Walk a type structure and resolve any type names
 */
static CanonTypeResult
ParseCanonType (TypePtr type, Bool forwardAllowed)
{
    ArgType	    *arg;
    int		    n;
    CanonTypeResult ret = CanonTypeDefined, t;
    Bool	    anyResolved;

    if (!type)
    {
	ParseError ("Type missing inside compiler");
	return CanonTypeUndefined;
    }
    switch (type->base.tag) {
    case type_prim:
	break;
    case type_name:
	if (!TypeNameType(type))
	{
	    if (!type->name.name)
	    {
		ExprPtr e;
		e = type->name.expr;
		if (e->base.tag == COLONCOLON)
		    e = e->tree.right;
		type->name.name = e->atom.symbol;
		if (!type->name.name)
		{
		    ParseError ("No typedef \"%A\" in namespace",
				e->atom.atom);
		    ret = CanonTypeUndefined;
		    break;
		}
	    }
	    if (type->name.name->symbol.class != class_typedef)
	    {
		ParseError ("Symbol \"%A\" not a typedef", 
			    type->name.name->symbol.name);
		ret = CanonTypeUndefined;
	    }
	    else if (!TypeNameType(type))
	    {
		if (!forwardAllowed)
		    ParseError ("Typedef \"%A\" not defined yet", 
				type->name.name->symbol.name);
		ret = CanonTypeForward;
	    }
	    else
	    {
		ret = ParseCanonType (TypeNameType(type), forwardAllowed);
	    }
	}
	break;
    case type_ref:
	ret = ParseCanonType (type->ref.ref, True);
	if (ret == CanonTypeForward)
	    ret = CanonTypeDefined;
	break;
    case type_func:
	if (type->func.ret)
	    ret = ParseCanonType (type->func.ret, forwardAllowed);
	for (arg = type->func.args; arg; arg = arg->next)
	{
	    t = ParseCanonType (arg->type, forwardAllowed);
	    if (t < ret)
		ret = t;
	}
	break;
    case type_array:
	ret = ParseCanonType (type->array.type, forwardAllowed);
	break;
    case type_hash:
	ret = ParseCanonType (type->hash.type, forwardAllowed);
	t = ParseCanonType (type->hash.keyType, forwardAllowed);
	if (t < ret)
	    ret = t;
	break;
    case type_struct:
	for (n = 0; n < type->structs.structs->nelements; n++)
	{
	    StructType   *st = type->structs.structs;

	    t = ParseCanonType (BoxTypesElements(st->types)[n], forwardAllowed);
	    if (t < ret)
		ret = t;
	}
	break;
    case type_union:
	anyResolved = False;
	for (n = 0; n < type->structs.structs->nelements; n++)
	{
	    StructType   *st = type->structs.structs;

	    t = ParseCanonType (BoxTypesElements(st->types)[n], True);
	    if (t < ret)
		ret = t;
	    if (t == CanonTypeDefined)
		anyResolved = True;
	}
	if (ret == CanonTypeForward)
	{
	    if (anyResolved)
		ret = CanonTypeDefined;
	    else if (!forwardAllowed)
		ParseError ("No member of '%T' defined yet", type);
	}
	break;
    case type_types:
	break;
    }
    return ret;
}

static SymbolPtr
ParseNewSymbol (Publish publish, Class class, Type *type, Atom name)
{
    ENTER ();
    SymbolPtr	s = 0;
    
    if (class == class_undef)
	class = funcDepth ? class_auto : class_global;

    if (class == class_namespace || 
	(class == class_typedef && type == 0) || 	
	ParseCanonType (type, False) == CanonTypeDefined)
    {
	switch (class) {
	case class_const:
	    s = NewSymbolConst (name, type);
	    break;
	case class_global:
	    s = NewSymbolGlobal (name, type);
	    break;
	case class_static:
	    s = NewSymbolStatic (name, type);
	    break;
	case class_arg:
	    s = NewSymbolArg (name, type);
	    break;
	case class_auto:
	    s = NewSymbolAuto (name, type);
	    break;
	case class_exception:
	    s = NewSymbolException (name, type, Void);
	    break;
	case class_typedef:
	    /*
	     * Special case for typedefs --
	     * allow forward declaration of untyped
	     * typedef names, then hook the
	     * new type to the old name
	     */
	    if (type)
	    {
		s = NamespaceFindName (CurrentNamespace, name, False);
		if (s && s->symbol.class == class_typedef && !s->symbol.type)
		{
		    s->symbol.type = type;
		    RETURN (s);
		}
	    }
	    s = NewSymbolType (name, type);
	    break;
	case class_namespace:
	    s = NewSymbolNamespace (name, NewNamespace (CurrentNamespace));
	    break;
	case class_undef:
	    break;
	}
	if (s)
	    NamespaceAddName (CurrentNamespace, s, publish);
    }
    RETURN (s);
}

int
yywrap (void)
{
    if (LexInteractive())
	printf ("\n");
    if (CurrentFrame)
    {
	do_Debug_done ();
	return 0;
    }
    return 1;
}

extern	char *yytext;

void
ParseError (char *fmt, ...)
{
    va_list	args;

    if (LexFileName ())
	FilePrintf (FileStderr, "%A:%d: ",
		    LexFileName (), LexFileLine ());
    va_start (args, fmt);
    FileVPrintf (FileStderr, fmt, args);
    FilePrintf (FileStderr, "\n");
    va_end (args);
}

void
yyerror (char *msg)
{
    ignorenl = 0;
    ParseError ("%s before %S", msg, yytext);
}