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

context lookup
	alias list_lang_object
		list<lang_object>

	alias list_declaration_data
		list<declaration_data>

	alias list_declarator_data
		list<declarator_data>

	alias list_int
		list<int>
	
	alias map_list_lang_object
		map<str, list_lang_object>
	
	#
	# Data types for global data.
	#

	# Language objects. 
	struct lang_object
		typeId: int 
		name: str

		# If the object is a typedef, this points to the real object.
		typedefOf: lang_object

		objectMap: map_list_lang_object
		inherited: list_lang_object
		lookupParent: lang_object
		specializationOf: lang_object
	end

	# This structure is used to keep track of information necessary to make a
	# declaration. While parsing a declaration it records the declaration's
	# attributes.
	struct declaration_data
		isTypedef: int
		isFriend: int
		isTemplate: int

		typeObj: lang_object
	end

	struct declarator_data
		qualObj: lang_object
		pdcScope: lang_object
		lookupObj: lang_object
	end

	# Constants for language object types.
	NamespaceType: int
	ClassType: int 
	TemplateClassType: int
	EnumType: int
	IdType: int
	TypedefType: int
	TemplateIdType: int

	#
	# Global data declarations
	#

	# Object stacks.
	curNamespace: list_lang_object
	declNs: list_lang_object
	lookupNs: list_lang_object
	qualNs: list_lang_object
	templateParamNs: list_lang_object

	# Declaration, declarator data.
	declarationData: list_declaration_data
	declaratorData: list_declarator_data 

	# Template declarations
	templDecl: list_int

	# Root namespace object
	rootNamespace: lang_object

	#
	# Identifier lookup.
	#

	# Lookup the token in the members of an object.
	lang_object lookupInObject( obj: lang_object, name: str )
	{
		# LOG print( '  looking in ', obj->name, '\n' )

		ol: list_lang_object = obj->objectMap->find( name )
		if ol {
			# LOG print( '  * found an object: ', ol.head, '\n' )
			return ol->head
		}

		return nil
	}

	# Lookup in an object and all the objects beneath it in the inheritance
	# tree.
	lang_object lookupWithInheritance( obj: lang_object, name: str )
	{
		found: lang_object = lookupInObject( obj, name )
		if found
			return found

		for inh: lang_object in obj->inherited {
			# First check if the inherited object is the one we are after.
			if inh->name == name && inh->typeId == ClassType {
				# LOG print( '  * found a class name\n' )
				return inh
			}

			# Otherwise look inside the inherited object.
			found = lookupWithInheritance( inh, name )
			if found
				return found
		}

		return nil
	}

	lang_object unqualifiedLookup( name: str )
	{
		found: lang_object

		# Start with the objects in the templateParamNs.
		for Obj: lang_object in rev_list_iter( templateParamNs ) {
			found = lookupWithInheritance( Obj, name )
			if found
				break
		}

		if !found {
			# Iterator over the objects starting at the head of the lookup stack
			# and going up through the lookup parents.
			lookupIn: lang_object = lookupNs->tail
			while lookupIn {
				found = lookupWithInheritance( lookupIn, name )
				if found
					break
				lookupIn = lookupIn->lookupParent
			}
		}

		return found
	}

	# The C++ scanner.
	lex
		rl fract_const / digit* '.' digit+ | digit+ '.' /
		rl exponent / [eE] [+\-]? digit+ /
		rl float_suffix / [flFL] /

		# Single and double literals.
		token TK_SingleLit /( 'L'? "'" ( [^'\\\n] | '\\' any )* "'" )/
		token TK_DoubleLit /( 'L'? '"' ( [^"\\\n] | '\\' any )* '"' )/

		literal `extern `namespace `friend `typedef `auto `register
				`static `mutable `inline `virtual `explicit `const
				`volatile `restrict `class `struct `union `template
				`private `protected `public `using `void `char
				`wchar_t `bool `int `float `double `short `long
				`signed `unsigned `enum `new `delete `operator
				`typename `export `throw `try `catch `sizeof
				`dynamic_cast `static_cast `reinterpret_cast `const_cast
				`typeid `this `true `false `switch `case `default
				`if `else `while `do `for `break `continue
				`return `goto

		# Extensions
		literal `__typeof `__is_pod `__is_empty

		literal `{ `} `; `, `= `( `) `: `& `* `[ `] `~ `+ `-
			`/ `< `> `| `^ `% `! `? `.

		literal `:: `== `!= `&& `|| `*= `/= `%= `+= `-= `&=
			`^= `|= `++ `-- `-> `->* `.* `... `<<= `>>=

		# Token translation targets.
		def unknown_id [lookup_id]
		def class_id [lookup_id]
		def namespace_id [lookup_id]
		def templ_class_id [lookup_id]
		def enum_id [lookup_id]
		def typedef_id [lookup_id]
		def identifier [lookup_id]
		def template_id [lookup_id]

		# Identifiers
		token lookup_id 
			obj: lang_object
			qualObj: lang_object

			/( [a-zA-Z_] [a-zA-Z0-9_]* )/
			{
				name: str = match_text
				found: lang_object = nil
				qualObj: lang_object = nil
				if qualNs->tail {
					# LOG print( 'qualified lookup of ', name, '\n' )

					# Transfer the qualification to the token and reset it.
					qualObj = qualNs->tail
					qualNs->pop_tail()
					qualNs->push_tail( nil )

					# Lookup using the qualification. 
					found = lookupWithInheritance( qualObj, name )
				}
				else {
					# No qualification, full search.
					# LOG print( 'unqualified lookup of ', name, '\n' )
					found = unqualifiedLookup( name )
				}

				# If no match, return an Unknown ID
				id: int = typeid<unknown_id>
				if found
					id = found->typeId

				LookupId: lookup_id = make_token( typeid<lookup_id>,
						input->pull(match_length) )
				LookupId.obj = found
				LookupId.qualObj = qualObj

				input->push( make_tree( id, LookupId ) )
			}

		# Floats.
		token TK_Float /( fract_const exponent? float_suffix? |
			digit+ exponent float_suffix? )/

		# Integer decimal. Leading part buffered by float.
		token TK_IntegerDecimal /( ( '0' | [1-9] [0-9]* ) [ulUL]{0,3} )/

		# Integer octal. Leading part buffered by float.
		token TK_IntegerOctal /( '0' [0-9]+ [ulUL]{0,2} )/

		# Integer hex. Leading 0 buffered by float.
		token TK_IntegerHex /( '0x' [0-9a-fA-F]+ [ulUL]{0,2} )/

		# Preprocessor line.
		ignore /'#' [^\n]* '\n'/

		# Comments and whitespace.
		ignore /( '/*' (any | '\n')* :>> '*/' )/
		ignore /( '//' any* :> '\n' )/
		ignore /( any - 33..126 )+/
	end

	#
	# Support functions
	#

		typeId: int 
		name: str

		# If the object is a typedef, this points to the real object.
		typedefOf: lang_object

		objectMap: map_list_lang_object
		inherited: list_lang_object
		lookupParent: lang_object
		specializationOf: lang_object

	lang_object createLangObject( typeId: int, name: str, lookupParent: lang_object )
	{
		Obj: lang_object = new lang_object()

		Obj->typeId = typeId 
		Obj->name = name
		Obj->typedefOf = nil
		Obj->objectMap = new map_list_lang_object()
		Obj->inherited = new list_lang_object()
		Obj->lookupParent = lookupParent

		return Obj
	}

	# Building the language object tree.
	int insertObject( definedIn: lang_object, name: str, obj: lang_object )
	{
		ol: list_lang_object = definedIn->objectMap->find( name )
		if !ol {
			# Element not in the map already
			ol = new list_lang_object()
			definedIn->objectMap->insert( name, ol )
		}
		ol->push_tail( obj )
	}

	lang_object findClass( inObj: lang_object, lang_objectname: str )
	{
		List: list_lang_object = inObj->objectMap->find( name )
		if List { 
			for obj: lang_object in List {
				if obj->typeId == ClassType {
					return obj
				}
			}
		}
		return nil
	}

	lang_object findTemplateClass( inObj: lang_object, name: str )
	{
		List: list_lang_object = inObj->objectMap->find( name )
		if List { 
			for obj: lang_object in List {
				if obj->typeId == TemplateClassType
					return obj
			}
		}
		return nil
	}

	def root_qual_opt 
		[]
	|	[`::]

	def nested_name_specifier_opt 
		[nested_name_specifier_opt qualifying_name `:: designated_qualifying_name `::]
	|	[nested_name_specifier_opt qualifying_name `::]
	|	[]

	def nested_name_specifier 
		[nested_name_specifier designated_qualifying_name `::]
	|	[nested_name_specifier qualifying_name `::]
	|	[qualifying_name `::]

	def qualifying_name 
		[class_name]
		{
			qualNs->pop_tail()
			qualNs->push_tail( r1.lookupId.obj )
		}

	|	[namespace_id]
		{
			match r1 [Id: lookup_id]
			qualNs->pop_tail()
			qualNs->push_tail( Id.obj )
		}

	|	[typedef_id]
		{
			match r1 [Id: lookup_id]
			qualNs->pop_tail()
			qualNs->push_tail( Id.obj->typedefOf )
		}

	def designated_qualifying_name 
		[`template any_id]
		{
			# FIXME: nulling qualNs is not the right thing to do here.
			qualNs->pop_tail()
			qualNs->push_tail( nil )
		}

	|	[`template any_id 
			templ_arg_open template_argument_list_opt templ_arg_close]
		{
			# FIXME: nulling qualNs is not the right thing to do here.
			qualNs->pop_tail()
			qualNs->push_tail( nil )
		}

	#
	# Id Expression
	#

	def id_expression 
		lookupId: lookup_id 

		[root_qual_opt nested_name_specifier_opt unknown_id]
		{
			lhs.lookupId = lookup_id in r3
		}

	|	[root_qual_opt nested_name_specifier_opt identifier]
		{
			lhs.lookupId = lookup_id in r3
		}

	|	[root_qual_opt nested_name_specifier_opt operator_function_id]
		{
			# Normally the token translation transfers the qualification. Since
			# the operator_function_id does not end in a lookup we must do it ourselves.
			qualObj: lang_object = qualNs->pop_tail()
			qualNs->push_tail( nil )

			lhs.lookupId = construct lookup_id ["x"]
			lhs.lookupId.data = '<operator_function_id>'
			lhs.lookupId.qualObj = qualObj
		}

	|	[root_qual_opt nested_name_specifier_opt conversion_function_id]
		{
			# Normally the token translation transfers the qualification. Since
			# the operator_function_id does not } in a lookup we must do it ourselves.
			qualObj: lang_object = qualNs->pop_tail()
			qualNs->push_tail( nil )

			# Do we need qual reset here becauase operator_function_id does not do it?
			lhs.lookupId = construct lookup_id ["x"]
			lhs.lookupId.data = '<conversion_function_id>'
			lhs.lookupId.qualObj = qualObj
		}

	|	[root_qual_opt nested_name_specifier_opt `~ class_name]
		{
			lhs.lookupId = r4.lookupId
		}

	|	[root_qual_opt nested_name_specifier_opt template_name]
		{
			lhs.lookupId = r3.lookupId
		}
			
	def template_name 
		lookupId: lookup_id 

		[template_id templ_arg_open template_argument_list_opt templ_arg_close]
		{
			lhs.lookupId = lookup_id in r1
		}

	|	[template_id]
		{
			lhs.lookupId = lookup_id in r1
		}


	#
	# Class Names
	#

	def class_name 
		lookupId: lookup_id

		[class_id]
		{
			lhs.lookupId = lookup_id in r1
		}

	|	[templ_class_id]
		{
			lhs.lookupId = lookup_id in r1
		}

	|	[templ_class_id templ_arg_open template_argument_list_opt templ_arg_close]
		{
			# TODO: Look for a specialization.
			lhs.lookupId = lookup_id in r1
		}

	def templ_arg_open 
		[`<]
		{
			qualNs->push_tail( nil )
		}

	def templ_arg_close
		[`>]
		{
			qualNs->pop_tail()
		}

	def declaration 
		[block_declaration] commit
	|	[function_definition] commit
	|	[template_declaration] commit
	|	[explicit_instantiation] commit
	|	[explicit_specialization] commit
	|	[linkage_specification] commit
	|	[namespace_definition] commit

	#
	# Declarations
	#

	def block_declaration 
		[simple_declaration]
	|	[using_declaration]
	|	[using_directive]

	def simple_declaration 
		[declaration_start simple_declaration_forms declaration_end `;]

	# Ordering is important for optimization. The form with the optional
	# decl_specifier_sing should go second.
	def simple_declaration_forms
		[decl_specifier_mult_seq_opt decl_specifier_sing 
			decl_specifier_mult_seq_opt init_declarator_list_opt]

	|	[decl_specifier_mult_seq_opt init_declarator_list_opt]

	def declaration_start 
		[]
		{
			# LOG print( 'opening new declaration_data with templDecl: ', templDecl.tail, '\n' )
			DD: declaration_data = new declaration_data()
			DD->isTypedef = 0
			DD->isFriend = 0
			DD->isTemplate = 0
			declarationData->push_tail( DD )

			# Transfer the template flag and reset it.
			declarationData->tail->isTemplate = templDecl->tail
			templDecl->push_tail( 0 )
		}

	def declaration_end 
		[]
		{
			# LOG print( 'closing declaration_data\n' )
			declarationData->pop_tail()
			templDecl->pop_tail()
		}

	def decl_specifier_sing 
		[type_specifier_sing]
		{
			# Store the object type of the declaration (if any) for use
			# by typedefs. 
			declarationData->tail->typeObj = r1.lookupId.obj
		}

	def type_specifier_seq
		lookupId: lookup_id

		[type_specifier_mult_seq_opt type_specifier_sing type_specifier_mult_seq_opt]
		{
			lhs.lookupId = r2.lookupId
		}

	def type_specifier_sing
		lookupId: lookup_id 

		[simple_type_specifier]
		{
			lhs.lookupId = r1.lookupId
		}
			
	|	[class_specifier]
		{
			lhs.lookupId = construct lookup_id ["x"]
			lhs.lookupId.data = '<class_specifier>'
		}

	|	[enum_specifier]
		{
			lhs.lookupId = construct lookup_id ["x"]
			lhs.lookupId.data = '<enum_specifier>'
		}

	|	[elaborated_type_specifier]
		{
			lhs.lookupId = construct lookup_id ["x"]
			lhs.lookupId.data = '<elaborated_type_specifier>'
		}

	# Type specifier sequence without enum specifier or class specifier.
	def necs_type_specifier_seq
		[type_specifier_mult_seq_opt necs_type_specifier_sing type_specifier_mult_seq_opt]

	# Type specifier singular without enum specifier or class specifier.
	def necs_type_specifier_sing 
		[simple_type_specifier]
	|	[elaborated_type_specifier]

	def type_specifier_mult_seq_opt 
		[type_specifier_mult_seq_opt type_specifier_mult]
	|	[]

	def type_specifier_mult_seq 
		[type_specifier_mult_seq type_specifier_mult]
	|	[type_specifier_mult]

	def simple_type_specifier
		lookupId: lookup_id 

		[simple_type_specifier_name]
		{
			lhs.lookupId = r1.lookupId
		}

	|	[simple_type_specifier_kw_seq]
		{
			lhs.lookupId = construct lookup_id ["x"]
			lhs.lookupId.data = '<simple_type_specifier_kw_seq>'
		}

	|	[`typename root_qual_opt nested_name_specifier type_name]
		{
			lhs.lookupId = r4.lookupId
		}

	|	[`typename root_qual_opt nested_name_specifier identifier]
		{
			lhs.lookupId = lookup_id in r4
		}

	|	[`typename root_qual_opt nested_name_specifier unknown_id]
		{
			lhs.lookupId = lookup_id in r4
		}

		# Extension.
	|	[`__typeof `( expression `)]
		{
			lhs.lookupId = construct lookup_id ["x"]
			lhs.lookupId.data = '<simple_type_specifier_kw_seq>'
		}

	def simple_type_specifier_name
		lookupId: lookup_id 

		[qual_type_name]
		{
			lhs.lookupId = r1.lookupId
		}

	def simple_type_specifier_kw_seq 
		[simple_type_specifier_kw_seq simple_type_specifier_kw]
	|	[simple_type_specifier_kw]

	def simple_type_specifier_kw 
		[`void]
	|	[`char]
	|	[`wchar_t]
	|	[`bool]
	|	[`int]
	|	[`float]
	|	[`double]
	|	[`short]
	|	[`long]
	|	[`signed]
	|	[`unsigned]

	def qual_type_name
		lookupId: lookup_id 

		[root_qual_opt nested_name_specifier_opt type_name]
		{
			lhs.lookupId = r3.lookupId
		}

	def type_name
		lookupId: lookup_id

		[class_name]
		{
			lhs.lookupId = r1.lookupId
		}

	|	[enum_id]
		{
			lhs.lookupId = lookup_id in r1
		}

	|	[typedef_id]
		{
			lhs.lookupId = lookup_id in r1
		}

	# NOTE: the typename case is moved to simple type specifier
	# to take advantage of its conflict resolution.
	def elaborated_type_specifier
		[class_key nested_name_specifier_opt class_head_name]
		{
			Id: lookup_id = lookup_id in r3
			name: str = Id.data

			# Get the ns the class is declared in.
			parentObj: lang_object = declNs->tail
			if Id.qualObj
				parentObj = Id.qualObj

			# Look for the class in the given scope.
			declaredClass: lang_object = findClass( parentObj, name )
			if !declaredClass
				declaredClass = findTemplateClass( parentObj, name )

			if !declaredClass {
				# LOG print( 'creating new class: ', name, '\n' )

				# Class does not exist in the parent scope, create it.
				nsType: int = declaredClassType()

				declaredClass = createLangObject( nsType, name, lookupNs->tail )

				# FIXME: handle friends. Make the class visible only if we are NOT
				# in a friend declaration.  The new class object is necessary to
				# properly process the body of the class.
				if declarationData->tail->isFriend == 0
					insertObject( parentObj, name, declaredClass )
			}
		}

		# TODO: Lookup type specialization.
	|	[class_key nested_name_specifier_opt templ_class_id
			templ_arg_open template_argument_list_opt templ_arg_close]

	|	[`enum nested_name_specifier_opt enum_head_name]
		{
			# TODO: should look for existing enums of the same name.
			Id: lookup_id = lookup_id in r3
			# LOG print( 'creating enumeration ' Id.data '\n' )
			enum: lang_object = createLangObject( EnumType, Id.data, lookupNs->tail )
			insertObject( declNs->tail, Id.data, enum )
		}

	def decl_specifier_mult_seq_opt 
		[decl_specifier_mult_seq_opt decl_specifier_mult]
	|	[]

	def decl_specifier_mult_seq 
		[decl_specifier_mult_seq decl_specifier_mult]
	|	[decl_specifier_mult]

	def decl_specifier_mult 
		[type_specifier_mult]
	|	[storage_class_specifier]
	|	[function_specifier]

	|	[`friend]
		{
			declarationData->tail->isFriend = 1
		}

	|	[`typedef]
		{
			declarationData->tail->isTypedef = 1
		}

	def storage_class_specifier 
		[`auto]
	|	[`register]
	|	[`static]
	|	[`extern]
	|	[`mutable]

	def function_specifier 
		[`inline]
	|	[`virtual]
	|	[`explicit]

	def type_specifier_mult 
		[cv_qualifier]

	def cv_qualifier 
		[`const]
	|	[`volatile]
	|	[`restrict]

	def cv_qualifier_rep 
		[cv_qualifier_rep cv_qualifier]
	|	[]

	def namespace_definition 
		[named_namespace_definition]
	|	[unnamed_namespace_definition]

	def named_namespace_definition
		[original_namespace_definition]
	|	[extension_namespace_definition]

	#
	# Enumerations
	#

	def enum_specifier 
		[`enum nested_name_specifier_opt 
				enum_head_name `{ enumerator_list_opt `}]
		{
			# TODO: should look for existing enums of the same name.
			Id: lookup_id = lookup_id in r3
			# LOG print( 'creating enumeration ' Id.data '\n' )
			enum: lang_object = createLangObject( EnumType, Id.data, lookupNs->tail )
			insertObject( declNs->tail, Id.data, enum )
		}

	|	[`enum `{ enumerator_list_opt `}]

	def enum_head_name
		[class_id] 
	|	[templ_class_id]
	|	[namespace_id]
	|	[typedef_id]
	|	[enum_id]
	|	[identifier]
	|	[template_id]
	|	[unknown_id]

	def enumerator_list_opt 
		[enumerator_list]
	|	[enumerator_list `,]
	|	[]

	def enumerator_list 
		[enumerator_list `, enumerator_definition]
	|	[enumerator_definition]

	def enumerator_definition 
		[enumerator_id]
		{
			Id: lookup_id = lookup_id in r1
			enumId: lang_object = createLangObject( IdType, Id.data, lookupNs->tail )
			insertObject( declNs->tail, Id.data, enumId )
		}

	|	[enumerator_id `= constant_expression]
		{
			Id: lookup_id = lookup_id in r1
			enumId: lang_object = createLangObject( IdType, Id.data, lookupNs->tail )
			insertObject( declNs->tail, Id.data, enumId )
		}

	def enumerator_id
		[namespace_id] 
	|	[typedef_id]
	|	[enum_id]
	|	[class_id] 
	|	[templ_class_id]
	|	[template_id]
	|	[identifier]
	|	[unknown_id]

	#
	# Declarators
	#

	def init_declarator_list_opt 
		[init_declarator_list]
	|	[]

	def init_declarator_list
		[init_declarator_list `, init_declarator]
	|	[init_declarator]

	def init_declarator
		[declarator initializer_opt]

	def initializer_opt
		[`= initializer_clause]
	|	[`( expression `)]
	|	[]

	def initializer_clause 
		[assignment_expression]
	|	[`{ initializer_list `}]
	|	[`{ initializer_list `, `}]
	|	[`{ `}]

	def initializer_list
		[initializer_list `, initializer_clause]
	|	[initializer_clause]

	#
	# Expressions
	#

	def expression 
		[expression `, assignment_expression]
	|	[assignment_expression]

	def expression_opt
		[expression]
	|	[]

	def constant_expression
		[conditional_expression]

	def constant_expression_opt 
		[constant_expression]
	|	[]

	def assignment_expression 
		[conditional_expression]
	|	[logical_or_expression assignment_op assignment_expression]
	|	[throw_expression]

	def assignment_op 
		[`=]
	|	[`*=]
	|	[`/=]
	|	[`%=]
	|	[`+=]
	|	[`-=]
	|	[`>>=]
	|	[`<<=]
	|	[`&=]
	|	[`^=]
	|	[`|=]

	def conditional_expression 
		[logical_or_expression]
	|	[logical_or_expression `? expression `: assignment_expression]

	def logical_or_expression 
		[logical_or_expression `|| logical_and_expression]
	|	[logical_and_expression]

	def logical_and_expression 
		[logical_and_expression `&& inclusive_or_expression]
	|	[inclusive_or_expression]

	def inclusive_or_expression 
		[inclusive_or_expression `| exclusive_or_expression]
	|	[exclusive_or_expression]

	def exclusive_or_expression 
		[exclusive_or_expression `^ and_expression]
	|	[and_expression]

	def and_expression 
		[and_expression `& equality_expression]
	|	[equality_expression]

	def equality_expression 
		[equality_expression `== relational_expression]
	|	[equality_expression `!= relational_expression]
	|	[relational_expression]

	def relational_expression 
		[relational_expression `< shift_expression]
	|	[relational_expression `> shift_expression]
	|	[relational_expression lt_eq shift_expression]
	|	[relational_expression gt_eq shift_expression]
	|	[shift_expression]

	def shift_expression 
		[shift_expression shift_left additive_expression]
	|	[shift_expression shift_right additive_expression]
	|	[additive_expression]

	def additive_expression 
		[additive_expression `+ multiplicative_expression]
	|	[additive_expression `- multiplicative_expression]
	|	[multiplicative_expression]

	def multiplicative_expression 
		[multiplicative_expression `* pm_expression]
	|	[multiplicative_expression `/ pm_expression]
	|	[multiplicative_expression `% pm_expression]
	|	[pm_expression]

	def pm_expression 
		[pm_expression `->* cast_expression]
	|	[pm_expression `.* cast_expression]
	|	[cast_expression]

	def cast_expression 
		[unary_expression]
	|	[`( type_id `) cast_expression]

	def delete_expression 
		[root_qual_opt `delete cast_expression]
	|	[root_qual_opt `delete `[ `] cast_expression]

	def new_initializer_opt 
		[new_initializer]
	|	[]

	def new_initializer 
		[`( expression_opt `)]

	def direct_new_declarator 
		[`[ expression `]]
	|	[direct_new_declarator `[ constant_expression `]]

	def new_declarator_opt 
		[new_declarator]
	|	[]

	def new_declarator 
		[direct_new_declarator]
	|	[ptr_operator_seq direct_new_declarator]
	|	[ptr_operator_seq]

	def new_type_id 
		[necs_type_specifier_seq new_declarator_opt]

	def new_placement 
		[`( expression `)]

	def new_expression 
		[root_qual_opt `new new_type_id new_initializer_opt]
	|	[root_qual_opt `new new_placement new_type_id new_initializer_opt]
	|	[root_qual_opt `new `( type_id `) new_initializer_opt]
	|	[root_qual_opt `new new_placement `( type_id `) new_initializer_opt]

	def unary_operator 
		[`*]
	|	[`&]
	|	[`+]
	|	[`-]
	|	[`!]
	|	[`~]

	def unary_expression 
		[postfix_expression]
	|	[`++ cast_expression]
	|	[`-- cast_expression]
	|	[unary_operator cast_expression]
	|	[`sizeof `( type_id `)]
	|	[`sizeof unary_expression]
	|	[new_expression]
	|	[delete_expression]

	def function_style_type_conv 
		[simple_type_specifier]


	def postfix_expression 
		[primary_expression]
	|	[postfix_expression `[ expression `]]
	|	[postfix_expression `( expression_opt `)]
	|	[function_style_type_conv `( expression_opt `)]
	|	[member_request_expr dot_arrow id_expression]
	|	[member_request_expr dot_arrow pseudo_destructor_call]
	|	[postfix_expression `++]
	|	[postfix_expression `--]
	|	[`dynamic_cast templ_arg_open type_id templ_arg_close `( expression `)]
	|	[`static_cast templ_arg_open type_id templ_arg_close `( expression `)]
	|	[`reinterpret_cast templ_arg_open type_id templ_arg_close `( expression `)]
	|	[`const_cast templ_arg_open type_id templ_arg_close `( expression `)]
	|	[`typeid `( expression `)]
	|	[`typeid `( type_id `)]

	def pseudo_destructor_call
		[root_qual_opt nested_name_specifier_opt `~ pdc_type_name]

	def primary_expression
		[expr_lit]
	|	[`this]
	|	[`( expression `)]
	|	[id_expression]
	# GNU extensions
	|	[`( `{ statement_rep `} `)]
	|	[`__is_pod `( type_id `)]
	|	[`__is_empty `( type_id `)]

	def expr_lit 
		[TK_IntegerDecimal]
	|	[TK_IntegerOctal]
	|	[TK_IntegerHex]
	|	[TK_SingleLit]
	|	[TK_Float]
	|	[double_lit_list]
	|	[`true]
	|	[`false]

	def double_lit_list 
		[TK_DoubleLit double_lit_list]
	|	[TK_DoubleLit]

	def member_request_expr 
		[postfix_expression]
	#	{
	#		# FIXME: If no proper type is found, we must fail.
	#		# LOG print( 'setting member request scope\n' )
	#		# qualNs.set( $1->type != 0 ? $1->type->getObject() : 0 );
	#	}

	def dot_arrow 
		[`->]
	|	[`.]

	def pdc_type_name 
		[enum_id]
	|	[typedef_id]

	#
	# Statements
	#

	def statement_rep 
		[statement_rep statement]
	|	[]

	def statement 
		[declaration_statement]
	|	[labeled_statement]
	|	[expression_statement]
	|	[compound_statement]
	|	[selection_statement]
	|	[iteration_statement]
	|	[jump_statement]
	|	[try_block]

	def labeled_statement 
		[label_id `: statement]
	|	[`case constant_expression `: statement]
	|	[`default `: statement]

	def label_id 
		[unknown_id]
	|	[identifier]
	|	[class_id]
	|	[templ_class_id]
	|	[namespace_id]
	|	[typedef_id]
	|	[enum_id]
	|	[template_id]

	def compound_statement
		[`{ compound_begin statement_rep compound_end `}]

	def compound_begin 
		[]
		{
			newCompound: lang_object = createLangObject( 0,
					'<compound_begin>', lookupNs->tail )
			lookupNs->push_tail( newCompound )

			declNs->push_tail( newCompound )
			# LOG print( 'opening <compound>\n' )
		}

	def compound_end 
		[]
		{
			lookupNs->pop_tail()
			declNs->pop_tail()
			# LOG print( 'closing <compound>\n' )
		}

	def selection_statement 
		[`if `( condition `) statement elseif_clauses else_clause]
	|	[`switch `( condition `) statement]

	def elseif_clauses 
		[elseif_clauses `else `if `( condition `) statement]
	|	[]

	def else_clause 
		[`else statement]
	|	[]

	def iteration_statement
		[`while `( condition `) statement]
	|	[`do statement `while `( expression `) `;]
	|	[`for `( for_init_statement condition_opt `; expression_opt `) statement]

	def jump_statement 
		[`break `;]
	|	[`continue `;]
	|	[`return expression_opt `;]
	|	[`goto any_id `;]

	def any_id
		[unknown_id]
	|	[class_id]
	|	[namespace_id]
	|	[templ_class_id]
	|	[enum_id]
	|	[typedef_id]
	|	[identifier]
	|	[template_id]
		

	def for_init_statement 
		[expression_statement]
	|	[stmt_block_declaration_forms `;]

	def condition 
		[expression]
	|	[type_specifier_seq declarator `= assignment_expression]

	def condition_opt 
		[condition]
	|	[]

	def expression_statement 
		[expression `;]
	|	[`;]

	def declaration_statement 
		[stmt_block_declaration]

	def stmt_block_declaration 
		[declaration_start stmt_block_declaration_forms declaration_end `;]
	|	[using_declaration]
	|	[using_directive]

	def stmt_block_declaration_forms
		[decl_specifier_mult_seq_opt decl_specifier_sing decl_specifier_mult_seq_opt 
			init_declarator_list_opt]
	|	[decl_specifier_mult_seq init_declarator_list_opt]

	#
	# Declarators
	#

	def declarator
		lookupObj: lang_object

		[ptr_operator_seq_opt declarator_id decl_array_or_param_rep declarator_end]
		{
			lhs.lookupObj = r4.lookupObj
		}

	|	[ptr_operator_seq_opt `( sub_declarator `) decl_array_or_param_rep declarator_end]
		{
			lhs.lookupObj = r6.lookupObj
		}

	def sub_declarator 
		[ptr_operator_seq declarator_id decl_array_or_param_rep]
	|	[ptr_operator_seq `( sub_declarator `) decl_array_or_param_rep]
	|	[`( sub_declarator `) decl_array_or_param_rep]
	|	[declarator_id decl_array_or_param_rep]

	def decl_array_or_param_rep 
		[decl_array_or_param_rep decl_array_or_param]
	|	[]

	def decl_array_or_param 
		[`[ constant_expression_opt `]]
	|	[`( parameter_declaration_clause `) cv_qualifier_rep exception_specification_opt]

	def declarator_id
		[declarator_id_forms]
		{
			name: str = r1.lookupId.data
			qualObj: lang_object = r1.lookupId.qualObj

			parentObj: lang_object = declNs->tail
			if qualObj
				parentObj = qualObj

			# Decide if we are declaring a constructor/destructor.
			isConstructor: bool 
			if parentObj == r1.lookupId.obj {
				isConstructor = true
				# LOG print( 'making declarator ' name ' a constructor/destructor\n' )
			}

			if parentObj->specializationOf && 
					parentObj->specializationOf == r1.lookupId.obj
			{
				isConstructor = true
				# LOG print( 'making declarator ' name ' a constructor/destructor\n' )
			}

			obj: lang_object = nil
			if name && !isConstructor && declarationData->tail->isFriend == 0 {
				if declarationData->tail->isTypedef {
					obj = createLangObject( TypedefType, name, lookupNs->tail )
					obj->typedefOf = declarationData->tail->typeObj
					insertObject( parentObj, name, obj )

					# LOG print( 'making declarator ' name ' a typedef\n' )
				}
				else {
					if !qualObj {
						if declarationData->tail->isTemplate {
							# If in a template declaration and the name is not qualified then
							# create the template id.
							obj = createLangObject( TemplateIdType, name, lookupNs->tail )
							#object->objType = declarationData.tail.type
							insertObject( declNs->tail, name, obj )

							# LOG print( 'making declarator ' name ' a template id\n' )
						}
						else {
							obj = createLangObject( IdType, name, lookupNs->tail )
							#object->objType = declarationData.tail().type;
							insertObject( declNs->tail, name, obj )

							# LOG print( 'making declarator ' name ' an id\n' ) 
						}
					}
				}
			}


			DD: declarator_data = new declarator_data()
			DD->qualObj = qualObj
			DD->pdcScope = nil
			DD->lookupObj = lookupNs->tail

			declaratorData->push_tail( DD )

			# If the declarator is qualified, push the qualification to the lookup
			# stack. Also save it in the declarator data so it can be passed to a
			# function body if needed.
			if qualObj {
				lookupNs->push_tail( qualObj )
				declaratorData->tail->lookupObj = qualObj
			}

			# LOG print( 'reduced declarator_id: ' name '\n' )
		}

	# Undoes the setup done by declarator_id and pdc_start.
	def declarator_end
		lookupObj: lang_object

		[]
		{
			# Get the lookupObject from the scope and pass it up. If we are about to
			# parse a function body it will be needed.
			lhs.lookupObj = declaratorData->tail->lookupObj

			pdcScope: lang_object = declaratorData->tail->pdcScope
			qualObj: lang_object = declaratorData->tail->qualObj

			declaratorData->pop_tail()

			if pdcScope {
				# LOG print( 'closing <pdc_scope>\n' )
				lookupNs->pop_tail()
				declNs->pop_tail()
			}

			if qualObj {
				# LOG print( 'popping lookupNs\n' )
				lookupNs->pop_tail()
			}
		}

	def declarator_id_forms
		lookupId: lookup_id 

		[id_expression]
		{
			lhs.lookupId = r1.lookupId
		}

	|	[root_qual_opt nested_name_specifier_opt type_name]
		{
			lhs.lookupId = r3.lookupId
		}

	|	[root_qual_opt nested_name_specifier_opt `~ class_id]
		{
			lhs.lookupId = lookup_id in r4
		}

	|	[root_qual_opt nested_name_specifier_opt `~ templ_class_id]
		{
			lhs.lookupId = lookup_id in r4
		}
	|	[root_qual_opt nested_name_specifier_opt `~ unknown_id]
		{
			lhs.lookupId = lookup_id in r4
		}

	def type_id
		lookupId: lookup_id 

		[type_specifier_seq abstract_declarator_opt]
		{
			lhs.lookupId = r1.lookupId
		}

	def abstract_declarator_opt 
		[abstract_declarator]
	|	[]

	def abstract_declarator
		[ptr_operator_seq abstract_noid abstract_decl_array_or_param_seq_opt declarator_end]
	|	[ptr_operator_seq `( sub_abstract_declarator `) 
			abstract_decl_array_or_param_seq_opt declarator_end]
	|	[abstract_noid abstract_decl_array_or_param_seq declarator_end]
	|	[`( sub_abstract_declarator `) abstract_decl_array_or_param_seq_opt declarator_end]

	def sub_abstract_declarator
		[ptr_operator_seq abstract_noid abstract_decl_array_or_param_seq_opt]

	|	[ptr_operator_seq `( sub_abstract_declarator `) 
			abstract_decl_array_or_param_seq_opt]
		
	|	[`( sub_abstract_declarator `) abstract_decl_array_or_param_seq_opt]

	def abstract_noid 
		[]
		{
			#  Make scope for declarator.
			DD: declarator_data = new declarator_data()
			declaratorData->push_tail( DD )
		}

	def abstract_decl_array_or_param_seq_opt
		[abstract_decl_array_or_param_seq_opt abstract_decl_array_or_param]
	|	[]

	def abstract_decl_array_or_param_seq
		[abstract_decl_array_or_param_seq abstract_decl_array_or_param]
	|	[abstract_decl_array_or_param]

	def abstract_decl_array_or_param
		[`[ constant_expression_opt `]]
	|	[`( parameter_declaration_clause `) cv_qualifier_rep 
			exception_specification_opt]

	def parameter_declaration_clause
		[pdc_start parameter_declaration_list]
	|	[pdc_start parameter_declaration_list `...]
	|	[pdc_start parameter_declaration_list `, `...]
	|	[pdc_start `...]
	|	[pdc_start]

	def pdc_start 
		[]
		{
			if !declaratorData->tail->pdcScope {
				# We are going to need a scope for the declarator.
				pdcScope: lang_object = createLangObject( 0,
						'<pdc_scope>', lookupNs->tail )

				lookupNs->push_tail( pdcScope )
				declNs->push_tail( pdcScope )

				declaratorData->tail->pdcScope = pdcScope
				declaratorData->tail->lookupObj = pdcScope
				# LOG print( 'opening <pdc_scope>\n' )
			}
		}

	def parameter_declaration_list 
		[parameter_declaration_list `, parameter_declaration]
	|	[parameter_declaration]

	def parameter_declaration 
		[declaration_start parameter_declaration_forms declaration_end]

	# Ordering the productions such that decl_specifier_sing is tried first is good
	# for performance.
	def parameter_declaration_forms
		[decl_specifier_mult_seq_opt decl_specifier_sing decl_specifier_mult_seq_opt 
			param_maybe_declarator maybe_parameter_init]

	|	[decl_specifier_mult_seq param_maybe_declarator maybe_parameter_init]

	def param_maybe_declarator 
		[abstract_declarator]
	|	[declarator]
	|	[]

	def maybe_parameter_init 
		[`= constant_expression]
	|	[]

	def ptr_operator 
		[`&]
	|	[root_qual_opt nested_name_specifier_opt `* cv_qualifier_rep]

	def ptr_operator_seq 
		[ptr_operator_seq ptr_operator]
	|	[ptr_operator]

	def ptr_operator_seq_opt 
		[ptr_operator_seq_opt ptr_operator]
	|	[]

	#
	# Functions
	#

	def function_definition
		[function_def_declaration ctor_initializer_opt function_body function_def_end]

	def function_def_declaration
		[declaration_start function_def_declaration_forms declaration_end]

	def function_def_declaration_forms 
		[decl_specifier_mult_seq_opt decl_specifier_sing 
			decl_specifier_mult_seq_opt function_def_declarator]
	|	[decl_specifier_mult_seq function_def_declarator]
	|	[function_def_declarator]

	def function_def_declarator 
		[declarator]
		{
			# The lookupObj from the declarator is the deepest lookup object found
			# while parsing the declarator. Make it visible in the function body.
			# This could be the args, the qualObj, or the parent to the function.
			lookupNs->push_tail( r1.lookupObj )
		}

	def function_def_end 
		[]
		{
			# Pop the lookup object.
			lookupNs->pop_tail()
		}

	def function_body
		[function_body_begin `{ statement_rep function_body_end `}]

	def function_body_begin 
		[]
		{
			newFunctionBody: lang_object = createLangObject( 0,
					'<function_body_begin>', lookupNs->tail )

			lookupNs->push_tail( newFunctionBody )

			declNs->push_tail( newFunctionBody )

			templDecl->push_tail( 0 )
			# LOG print( 'opening <function_body>\n' )
		}

	def function_body_end 
		[]
		{
			# First undoes the function body begin work. Then undoes the setup in
			# function_def_declarator.
			declNs->pop_tail()
			lookupNs->pop_tail()
			templDecl->pop_tail()
			# LOG print( 'closing <function_body>\n' )
		}



	#
	# Classs
	#

	int declaredClassType()
	{
		if declarationData->tail->isTemplate {
			return TemplateClassType
		} else {
			return ClassType
		}
	}

	def class_specifier 
		[class_head base_clause_opt `{ class_member_rep class_body_end `}]
		{
	# FIXME: reparse not implemented yet
	# FIXME FIXME: reparse is actually implemented now implemented
	#		# Visit class function bodies, but skip nested classes.
	#		for CFB: class_function_body in lhs {
	#			skipping class_specifier
	#
	#			# Reparse the text of the class function body as a function body
	#			function_body FB = parse function_body[ $CFB ]
	#
	#			# Replace the class function body with the parsed function body.
	#			CFB = cons class_function_body [FB.tree]
	#		}
		}

	def class_head 
		[class_key]
		{
			nsType: int = declaredClassType()

			# LOG print( 'creating new anonymous class\n' )
			newClass: lang_object = createLangObject( nsType,
					'<anon_class>', lookupNs->tail )

			lookupNs->push_tail( newClass )
			declNs->push_tail( newClass )
		}

	|	[class_key nested_name_specifier_opt class_head_name]
		{
			Id: lookup_id = lookup_id in r3
			name: str = Id.data

			# Get the ns the class is declared in.
			parentObj: lang_object = declNs->tail
			if Id.qualObj
				parentObj = Id.qualObj

			# Look for the class in the given scope.
			declaredClass: lang_object = findClass( parentObj, name )
			if !declaredClass
				declaredClass = findTemplateClass( parentObj, name )

			if !declaredClass {
				# LOG print( 'creating new class: ' name '\n' )

				# Class does not exist in the parent scope, create it.
				nsType: int = declaredClassType()

				declaredClass = createLangObject( nsType, name, lookupNs->tail )

				# FIXME: handle friends. Make the class visible only if we are NOT
				# in a friend declaration.  The new class object is necessary to
				# properly process the body of the class.
				if declarationData->tail->isFriend == 0
					insertObject( parentObj, name, declaredClass )
			}

			# Push the found/new class.
			lookupNs->push_tail( declaredClass )
			declNs->push_tail( declaredClass )
		}

	|	[class_key nested_name_specifier_opt templ_class_id
			templ_arg_open template_argument_list_opt templ_arg_close]
		{
			match r3 [Id: lookup_id]
			id: str = Id.data
			classObj: lang_object = Id.obj

			# TODO: Try to find the specializaition in the template class object.
			# TypeList typeList;
			# makeTypeList( typeList $6->last );

			declaredClass: lang_object
			#declaredClass = classObj->findSpecExact( typeList );
			if !declaredClass {
				# LOG print( 'making new template specialization\n' )
				nsType: int = declaredClassType()
				declaredClass = createLangObject( nsType, id, lookupNs->tail )
				# LOG print( 'declaredClass: ' declaredClass '\n' )
				declaredClass->specializationOf = classObj
				# $$->typeListMapEl = classObj->typeListMap.insert( typeList, declaredClass );
			}

			# Push the found/new class.
			lookupNs->push_tail( declaredClass )
			declNs->push_tail( declaredClass )
		}

	def class_body_end 
		[]
		{
			# Pop the class ns.
			lookupNs->pop_tail()
			declNs->pop_tail()

			# LOG print( 'closing off class\n' )
		}

	def class_head_name
		[class_id]
	|	[templ_class_id]
	|	[namespace_id]
	|	[typedef_id]
	|	[enum_id]
	|	[unknown_id]
	|	[identifier]
	|	[template_id]

	def class_key 
		[`class]
	|	[`struct]
	|	[`union]

	def class_member_rep 
		[class_member_rep class_member]
	|	[]

	def class_member 
		[member_declaration]
	|	[access_specifier `:]

	def member_declaration 
		[declaration_start member_declaration_forms declaration_end `;]
	|	[class_function_definition]
	|	[using_declaration]
	|	[template_declaration]

	def class_function_definition
		[function_def_declaration ctor_initializer_opt class_function_body function_def_end]

	lex
		token cfb_open /'{'/
		token cfb_close /'}'/
		token cfb_string /
					"'" ( [^'\\\n] | '\\' any )* "'" |
					'"' ( [^"\\\n] | '\\' any )* '"'/
		token cfb_comment /
					( '/*' (any | '\n')* :>> '*/' ) |
					( '//' any* :> '\n' )/
		token cfb_data /[^{}'"/]+ | '/'/
	end

	def cfb_item
		[cfb_data]
	|	[cfb_string]
	|	[cfb_comment]
	|	[cfb_open cfb_item* cfb_close]

	def cfb_conts
		[cfb_item* cfb_close]



	def class_function_body
	#	['{' cfb_conts]
	#|	[function_body]
		[function_body]

	# Get better performance if the form with decl_specifier_sing comes first.
	def member_declaration_forms
		[decl_specifier_mult_seq_opt decl_specifier_sing 
			decl_specifier_mult_seq_opt member_declarator_list_opt]
	|	[decl_specifier_mult_seq_opt member_declarator_list_opt]

	def member_declarator_list_opt 
		[member_declarator_list]
	|	[]

	def member_declarator_list 
		[member_declarator_list `, member_declarator]
	|	[member_declarator]

	def member_declarator 
		[declarator]
	|	[declarator `= constant_expression]
	|	[declarator `: constant_expression]
	|	[`: constant_expression]

	def access_specifier 
		[`private]
	|	[`protected]
	|	[`public]

	def access_specifier_opt 
		[access_specifier]
	|	[]

	def using_declaration
		[`using id_expression `;]
		{
			obj: lang_object = r2.lookupId.obj
			if obj
				insertObject( declNs->tail, obj->name, obj )
		}

	|	[`using type_id `;]
		{
			obj: lang_object = r2.lookupId.obj
			if obj
				insertObject( declNs->tail, obj->name, obj )
		}

	def using_directive
		[`using `namespace root_qual_opt nested_name_specifier_opt 
			namespace_id `;]
		{
			# This uses a simple, incomplete guard against cycles in the graph of
			# using namespaces. A more sophisticated and complete guard would look
			# for longer cycles as well. Note that even gcc 3.3.5 does not bother.
			match r5 [Id: lookup_id]
			usingObject: lang_object = Id.obj
			inObject: lang_object = declNs->tail
			if usingObject != inObject
				inObject->inherited->push_tail( usingObject )
		}


	#
	# Derived classes
	#

	def base_clause_opt 
		[base_clause]
	|	[]

	def base_clause 
		[`: base_specifier_list]

	def base_specifier_list 
		[base_specifier_list `, base_specifier]
	|	[base_specifier]

	int addBaseSpecifier( inObject: lang_object, inheritedObject: lang_object )
	{
		# Resolve typedefs.
		if inheritedObject->typeId == TypedefType
			inheritedObject = inheritedObject->typedefOf

		inObject->inherited->push_tail( inheritedObject )
	}

	def base_specifier 
		[root_qual_opt nested_name_specifier_opt type_name]
		{
			addBaseSpecifier( declNs->tail, r3.lookupId.obj )
		}

	|	[`virtual access_specifier_opt root_qual_opt nested_name_specifier_opt type_name]
		{
			addBaseSpecifier( declNs->tail, r5.lookupId.obj )
		}

	|	[access_specifier virtual_opt root_qual_opt nested_name_specifier_opt type_name]
		{
			addBaseSpecifier( declNs->tail, r5.lookupId.obj )
		}

	def virtual_opt 
		[`virtual]
	|	[]

	#
	# Special member functions
	#

	def conversion_function_id 
		[`operator conversion_type_id]

	def conversion_type_id 
		[necs_type_specifier_seq ptr_operator_seq_opt]

	def ctor_initializer_opt 
		[ctor_initializer]
	|	[]

	def ctor_initializer
		[`: mem_initializer_list]

	def mem_initializer_list 
		[mem_initializer_list `, mem_initializer]
	|	[mem_initializer]

	def mem_initializer 
		[mem_initializer_id `( expression_opt `)]

	def mem_initializer_id 
		[root_qual_opt nested_name_specifier_opt unknown_id]
	|	[root_qual_opt nested_name_specifier_opt identifier]
	|	[root_qual_opt nested_name_specifier_opt type_name]
	|	[root_qual_opt nested_name_specifier_opt template_name]


	#
	# Overloading
	#
	def operator_function_id 
		[`operator operator]

	def operator 
		[`+] | [`-] | [`*] | [`/] | [`=] | [`<] | [`>] | [`&] | [`|] |
		[`^] | [`%] | [`~] |	[`!] | [`( `)] | [`[ `]] | [`new] |
		[`delete] | [`->] | [`++] | [`--] | [`*=] | [`/=] | [`%=] |
		[`+=] | [`-=] | [`>>=] | [`<<=] | [`&=] | [`^=] | [`|=] | [`==] |
		[`!=] | [`&&] | [`||] | [lt_eq] | [gt_eq] | [shift_left] | [shift_right]

	def lt_eq 
		[`< `=]
	#	try {
	#		if ( $2->leader != 0 ) {
	#			#ifdef LOG_REDUCE
	#			cerr << "rejecting less-than equals-to" << endl;
	#			#endif
	#			reject();
	#		}
	#	};

	def gt_eq 
		[`> `=]
	#	try {
	#		if ( $2->leader != 0 ) {
	#			#ifdef LOG_REDUCE
	#			cerr << "rejecting greater-than equals-to" << endl;
	#			#endif
	#			reject();
	#		}
	#	};

	def shift_left 
		[`< `<]
	#	try {
	#		if ( $2->leader != 0 ) {
	#			#ifdef LOG_REDUCE
	#			cerr << "rejecting shift left" << endl;
	#			#endif
	#			reject();
	#		}
	#	};

	def shift_right 
		[`> `>]
	#	try {
	#		if ( $2->leader != 0 ) {
	#			#ifdef LOG_REDUCE
	#			cerr << "rejecting shift right" << endl;
	#			#endif
	#			reject();
	#		}
	#	};

	#
	# Templates
	#

	def template_declaration 
		[template_declaration_params declaration]
		{
			templDecl->pop_tail()
			templateParamNs->pop_tail()
		}

	def template_declaration_params
		[`template `< tpl_start template_parameter_list `>]
		{
			templDecl->push_tail( 1 )
		}

	|	[`export `template `< tpl_start template_parameter_list `>]
		{
			templDecl->push_tail( 1 )
		}

	def tpl_start 
		[]
		{
			# Create a new scope for the template parameters.
			newTemplateParamScope: lang_object = 
				createLangObject( 0, '<tpl_start>', lookupNs->tail )

			templateParamNs->push_tail( newTemplateParamScope )
		}

	def template_parameter_list 
		[template_parameter_list `, template_parameter]
	|	[template_parameter]

	def template_parameter 
		[type_parameter]
	|	[template_parameter_declaration]

	def template_parameter_declaration
		[declaration_start template_parameter_declaration_forms declaration_end]

	def template_parameter_declaration_forms
		[decl_specifier_mult_seq param_maybe_declarator maybe_parameter_init]

	|	[temp_param_decl_specifier_sing decl_specifier_mult_seq_opt 
			param_maybe_declarator maybe_parameter_init]

	|	[decl_specifier_mult_seq temp_param_decl_specifier_sing 
			decl_specifier_mult_seq_opt param_maybe_declarator maybe_parameter_init]

	def temp_param_decl_specifier_sing 
		[temp_param_type_specifier_sing]

	# Template parameters cannot support elaborated type specifer or class specifier.
	def temp_param_type_specifier_sing 
		[templ_simple_type_specifier]
	|	[enum_specifier]

	def templ_simple_type_specifier 
		[simple_type_specifier_name]
	|	[simple_type_specifier_kw_seq]

	def type_parameter
		[`class type_param_id type_param_init_opt]
		{
			Id: lookup_id = lookup_id in r2
			if Id {
				# The lookup ns should be a template param scope.
				newClass: lang_object = 
					createLangObject( ClassType, Id.data, lookupNs->tail )
				insertObject( templateParamNs->tail, Id.data, newClass )
			}
		}

	|	[`typename type_param_id type_param_init_opt]
		{
			Id: lookup_id = lookup_id in r2
			if Id {
				# The lookup ns should be a template param scope.
				newClass: lang_object = 
					createLangObject( ClassType, Id.data, lookupNs->tail )
				insertObject( templateParamNs->tail, Id.data, newClass )
			}
		}

	|	[`template `< tpl_start template_parameter_list `>
				`class type_param_id templ_type_param_init_opt]
		{
			Id: lookup_id = lookup_id in r7
			if Id {
				newClass: lang_object = 
					createLangObject( TemplateClassType, Id.data, lookupNs->tail )
				insertObject( templateParamNs->tail, Id.data, newClass )
			}
		}

	def templ_type_param_init_opt 
		[`= id_expression]
	|	[]

	def type_param_init_opt 
		[`= type_id]
	|	[]

	def type_param_id 
		[namespace_id]
	|	[typedef_id]
	|	[enum_id]
	|	[class_id] 
	|	[templ_class_id]
	|	[identifier]
	|	[template_id]
	|	[unknown_id]
	|	[] 

	def template_argument_list_opt 
		[template_argument_list]
	|	[]

	def template_argument_list 
		[template_argument_list `, template_argument]
	|	[template_argument]

	def template_argument 
		[type_id]
	|	[assignment_expression]

	def explicit_instantiation 
		[`template declaration]
	|	[declaration_start decl_specifier_mult_seq `template declaration declaration_end]

	def explicit_specialization 
		[`template `< `> declaration]

	## Not sure what this one is about?
	#explicit_specialization: 
	#	declaration_start decl_specifier_mult_seq KW_Template '<' '>' 
	#	declaration declaration_end;


	#
	# Original namespace definition
	#

	def original_namespace_definition
		[orig_namespace_def_name `{ declaration* namespace_end `}]

	def orig_namespace_def_name [`namespace unknown_id]
		{
			match r2 [Id: lookup_id]
			nspace: lang_object = createLangObject(
					NamespaceType, Id.data, lookupNs->tail )

			# Insert the new object into the dictionary of the parent.
			insertObject( curNamespace->tail, Id.data, nspace )

			# Push the namespace 
			curNamespace->push_tail( nspace )
			declNs->push_tail( nspace )
			lookupNs->push_tail( nspace )

			# LOG print( 'created original namespace: ' Id.data '\n' )
		}

	def namespace_end []
		{
			# Pop the namespace.
			curNamespace->pop_tail()
			declNs->pop_tail()
			lookupNs->pop_tail()

			# LOG print( 'closed namespace\n' )
		}

	#
	# Extension namespace definition
	#

	def extension_namespace_definition
		[ext_namespace_def_name `{ declaration* namespace_end `}]

	def ext_namespace_def_name [`namespace namespace_id]
		{
			match r2 [Id: lookup_id]
			nspace: lang_object = Id.obj

			# Push the namespace 
			curNamespace->push_tail( nspace )
			declNs->push_tail( nspace )
			lookupNs->push_tail( nspace )

			# LOG print( 'found extended namespace: ' Id.data '\n' )
		}

	#
	# Unnamed namespace definition
	#
	def unnamed_namespace_definition
		[unnamed_namespace_def_name `{ declaration* namespace_end `}]

	def unnamed_namespace_def_name [`namespace]
		{
			nspace: lang_object = createLangObject(
					NamespaceType, '<unnamed_namespace>',
					lookupNs->tail )

			# Push the namespace 
			curNamespace->push_tail( nspace )
			declNs->push_tail( nspace )
			lookupNs->push_tail( nspace )

			# LOG print( 'parsed unnamed namespace\n' )
		}

	#
	# linkage_specification
	#
	def linkage_specification 
		[`extern TK_DoubleLit `{ declaration* `}]
	|	[`extern TK_DoubleLit declaration]

	#
	# Exception Handling.
	#

	def try_block 
		[`try compound_statement handler_seq]

	def handler_seq 
		[handler_seq handler]
	|	[handler]

	def handler 
		[`catch `( exception_declaration `) compound_statement]

	def exception_declaration 
		[type_specifier_seq declarator]
	|	[type_specifier_seq abstract_declarator]
	|	[type_specifier_seq]
	|	[`...]

	def throw_expression 
		[`throw assignment_expression]
	|	[`throw]

	def exception_specification_opt 
		[exception_specification]
	|	[]

	def exception_specification 
		[`throw `( type_id_list_opt `)]

	def type_id_list_opt 
		[type_id_list]
	|	[]

	def type_id_list 
		[type_id_list `, type_id]
	|	[type_id]

	def start
		[declaration*]

	#
	# Grammar done.
	#

	int printObject( indent: str, obj: lang_object )
	{
		print( indent, obj->name )
		
		if obj->objectMap->length > 0
			print( ' {\n' )
		
		Map: map_list_lang_object = obj->objectMap
		for List: list_lang_object in Map  {
			List2: list_lang_object = List
			for Obj: lang_object in List2 {
				printObject( indent + '  ', Obj )
			}
		}


		if obj->objectMap->length > 0
			print( indent, '}' )

		print( '\n' )
	}

end # lookup

#
# Global data declarations
#

Lookup: lookup = new lookup()

# Constants for language object types.
Lookup->NamespaceType = typeid<lookup::namespace_id>
Lookup->ClassType = typeid<lookup::class_id>
Lookup->TemplateClassType = typeid<lookup::templ_class_id>
Lookup->EnumType = typeid<lookup::enum_id>
Lookup->IdType = typeid<lookup::identifier>
Lookup->TypedefType = typeid<lookup::typedef_id>
Lookup->TemplateIdType = typeid<lookup::template_id>


# Object stacks.
Lookup->curNamespace = new lookup::list_lang_object()
Lookup->declNs = new lookup::list_lang_object()
Lookup->lookupNs = new lookup::list_lang_object()
Lookup->qualNs = new lookup::list_lang_object()
Lookup->templateParamNs = new lookup::list_lang_object()

# Declaration, declarator data.
Lookup->declarationData = new lookup::list_declaration_data()
Lookup->declaratorData = new lookup::list_declarator_data()

# Template declarations
Lookup->templDecl = new lookup::list_int()

# Root namespace object
Lookup->rootNamespace = lookup::createLangObject( Lookup->NamespaceType, '<root_namespace>', nil )

# Initialize the namespace and declaration stacks with the root namespace
Lookup->curNamespace->push_tail( Lookup->rootNamespace )
Lookup->declNs->push_tail( Lookup->rootNamespace )
Lookup->lookupNs->push_tail( Lookup->rootNamespace )

# Start with no qualification (note variables are initialized to zero)
Lookup->qualNs->push_tail( nil )

Lookup->templDecl->push_tail( 0 )

DD: lookup::declaration_data = new lookup::declaration_data()
DD->isTypedef = 0
DD->isFriend = 0
DD->isTemplate = 0
Lookup->declarationData->push_tail( DD )

parse S: lookup::start( Lookup )[ stdin ]
if ! S {
	print( error )
	exit( 1 )
}

print( '***** NAMSPACES *****\n' )
lookup::printObject( '', Lookup->rootNamespace )
print( '***** UNKNOWN DECLARATORS *****\n' )
for DI: lookup::declarator_id in S {
	if match DI 
		[lookup::root_qual_opt lookup::nested_name_specifier_opt lookup::`~ UID: lookup::unknown_id]
	{
		print( UID, '\n' )
	}
}