File: cpp.grm

package info (click to toggle)
kelbt 0.12-2
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 2,456 kB
  • ctags: 1,986
  • sloc: cpp: 8,015; ansic: 3,331; makefile: 137; sh: 92
file content (1201 lines) | stat: -rw-r--r-- 23,759 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
%
%  Copyright 2004, 2005 Adrian Thurston <thurston@cs.queensu.ca>
%

%  This file is part of C++ Pre-Parser.
%
%  C++ Pre-Parser is free software; you can redistribute it and/or modify
%  it under the terms of the GNU General Public License as published by
%  the Free Software Foundation; either version 2 of the License, or
%  (at your option) any later version.
%
%  C++ Pre-Parser is distributed in the hope that it will be useful,
%  but WITHOUT ANY WARRANTY; without even the implied warranty of
%  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
%  GNU General Public License for more details.
%
%  You should have received a copy of the GNU General Public License
%  along with C++ Pre-Parser; if not, write to the Free Software
%  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 

%
%  Derived from ISO/IEC C++ Standard 14822 first edition 1998-09-01
%

% Want to control output formatting ourselves.
#pragma -comment -raw

tokens
	number
			% Decimal
			"0[uUlL]?[uUlL]?[uUlL]?"
		|	"[123456789][0123456789]*[uUlL]?[uUlL]?[uUlL]?"

			% Octal
		|	"0[01234567]+[uUlL]?[uUlL]?[uUlL]?"
	
			% Hex
		|	"0[xX][\dABCDEFabcdef]+[uUlL]?"

			% Float
		|	"\d+.\d*([eE][+-]?\d+)?[fFlL]?"
		|	"\d*.\d+([eE][+-]?\d+)?[fFlL]?"
		|	"\d+([eE][+-]?\d+)[fFlL]?"

end tokens

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

compounds
	':: '... '+= '-= '*= '/= '%= '^= '&= '|= '<< '>> '>>= '<<= '== '!= '<= '>=
	'&& '|| '.* '->* '-> '-- '++
end compounds

comments
	/* */
	//
end comments

compounds
		'$$
		'<*> '<&>
		'({ '})
		'(| '|)
		'<{ '}>
		'<| '|>
		':/ ':+ ':- ':~ ':|
end compounds
%
% A.2 Lexical Conventions
%
define literal
		[number]
	|	[charlit]
	|	[repeat stringlit+]
	|	'true
	|	'false
end define

%
% A.4 Expressions
%

define primary_expression
		[literal]
	|	'this
	|	'( [expression] ')
	|	[id_expression]
end define

define id_expression
		[opt '::] [opt nested_name_specifier] [unqualified_id]
end define

define unqualified_id
		[id] [opt template_id_args]
	|	[operator_function_id]
	|	[conversion_function_id]
	|	'~ [class_name]
end define

define nested_name_specifier
		[class_id] [opt template_id_args] ':: [nested_name_specifier_rest]
	|	[namespace_name] [opt template_id_args] ':: [nested_name_specifier_rest]
end define

define nested_name_specifier_rest
		[opt nested_name_specifier]
	|	'template [nested_name_specifier]
end define

define postfix_expression
		[postfix_item] [repeat postfix_operator]
end define

define postfix_item
		[primary_expression] 
	|	[simple_type_specifier] '( [opt expression] ') 
	|	'typename [opt '::] [opt nested_name_specifier] [type_name] '( [opt 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] ')
end define

define postfix_operator
		[square_open] [expression] [square_close] 
	|	'( [opt expression] ') 
	|	'. [opt 'template] [id_expression]
	|	'-> [opt 'template] [id_expression]
	|	'. [pseudo_destructor_name]
	|	'-> [pseudo_destructor_name]
	|	'++
	|	'--
end define

define templ_arg_open
		'<|
	|	'<
end define

define templ_arg_close
		'|>
	|	'>
end define

define pseudo_destructor_name
		[opt '::] [opt nested_name_specifier] [type_name] ':: '~ [type_name]
	|	[opt '::] [nested_name_specifier] 'template [template_id] ':: '~ [type_name]
	|	[opt '::] [opt nested_name_specifier] '~ [type_name]
end define

define unary_expression
		[postfix_expression]
	|	'++ [cast_expression] 
	|	'-- [cast_expression]
	|	[unary_operator] [cast_expression]
	|	'sizeof [unary_expression]
	|	'sizeof '( [type_id] ') 
	|	[new_expression]
	|	[delete_expression]
end define

define unary_operator
		'* | '& | '+ | '- | '! | '~
end define

define new_expression
		[opt '::] 'new [opt new_placement] [new_type]
end define

define new_type
		[new_type_id] [opt new_initializer]
	|	'( [type_id] ') [opt new_initializer]
end define

define new_placement
		'( [expression] ')
end define

define new_type_id
		[repeat type_specifier+] [new_declarator_opt]
end define

define new_declarator_opt
		[repeat ptr_operator] [repeat new_decl_array]
end define

define new_decl_array
		[square_open] [expression] [square_close]
end define

define new_initializer
		'( [opt expression] ')
end define

define delete_expression
		[opt '::] 'delete [cast_expression]
	|	[opt '::] 'delete [square_open] [square_close] [cast_expression]
end define

define cast_expression
		[unary_expression]
	|	'( [type_id] ') [cast_expression]
end define

define pm_expression
		[cast_expression]
	|	[pm_expression] [SP] [pm_op] [SP] [cast_expression]
end define

define pm_op
		'.* | '->*
end define

define multiplicative_expression
		[pm_expression]
	|	[multiplicative_expression] [SP] [multiplicative_op] [SP] [pm_expression]
end define

define multiplicative_op
		'* | '/ | '%
end define

define additive_expression
		[multiplicative_expression]
	|	[additive_expression] [SP] [additive_op] [SP] [multiplicative_expression]
end define

define additive_op
		'+ | '-
end define

define shift_expression
		[additive_expression]
	|	[shift_expression] [SP] [shift_op] [SP] [additive_expression]
end define

define shift_op
		'<< | '>>
end define

define relational_expression
		[shift_expression]
	|	[relational_expression] [SP] [relational_op] [SP] [shift_expression]
end define
	
define relational_op
		'< | '> | '<= | '>=
end define

define equality_expression
		[relational_expression]
	|	[equality_expression] [SP] [equality_op] [SP] [relational_expression]
end define

define equality_op
		'== | '!=
end define

define and_expression
		[equality_expression]
	|	[and_expression] [SP] '& [SP] [equality_expression]
end define

define exclusive_or_expression
		[and_expression]
	|	[exclusive_or_expression] [SP] '^ [SP] [and_expression]
end define

define inclusive_or_expression
		[exclusive_or_expression]
	|	[inclusive_or_expression] [SP] '| [SP] [exclusive_or_expression]
end define

define logical_and_expression
		[inclusive_or_expression]
	|	[logical_and_expression] [SP] '&& [SP] [inclusive_or_expression]
end define

define logical_or_expression
		[logical_and_expression]
	|	[logical_or_expression] [SP] '|| [SP] [logical_and_expression]
end define

define conditional_expression
		[logical_or_expression]
	|	[logical_or_expression] [SP] '? [SP] [expression] [SP] [conditional_colon] 
			[SP] [assignment_expression]
end define

define conditional_colon
		':|
	|	':
end define

define assignment_expression
		[conditional_expression]
	|	[logical_or_expression] [SP] [assignment_operator] [SP] [assignment_expression]
	|	[throw_expression]
end define

define assignment_operator
		'= | '*= | '/= | '%= | '+= | '-= | '>>= | '<<= | '&= | '^= | '|=
end define

define expression
		[assignment_expression]
	|	[expression] ', [SP] [assignment_expression]
end define

define constant_expression
		[conditional_expression]
end define


%
% A.5 Statements
%

define statement
		[labeled_statement]
	|	[expression_statement] [NL]
	|	[selection_statement]
	|	[iteration_statement]
	|	[jump_statement] [NL]
	|	[declaration_statement] [NL]
	|	[try_block] [NL]
	|	[comment] [NL]
	|	[compound_statement] [NL]
end define

define labeled_statement
		[EX] [id] [label_colon] [IN] [NL] [statement]
	|	[EX] 'case [SP] [constant_expression] [label_colon] [IN] [NL] [statement]
	|	[EX] 'default [SP] [label_colon] [IN] [NL] [statement]
end define

define label_colon
		':~
	|	':
end define

define expression_statement
		[opt expression] ';
end define

define compound_statement
		[EX] '{ [IN] [NL] 
			[repeat statement]
		[EX] '} [IN]
end define

define selection_statement
		'if [SP] '( [SP] [condition] [SP] ') [NL] 
			[IN] [statement] [EX]
		[repeat else_if_part] 
		[opt else_part]
	|	'switch [SP] '( [SP] [condition] [SP] ') [NL] 
			[IN] [statement] [EX]
end define

define else_if_part
		'else [SP] 'if '( [SP] [condition] [SP] ') [NL] 
			[IN] [statement] [EX]
end define

define else_part
		'else [NL] 
			[IN] [statement] [EX]
end define

define iteration_statement
		'while [SP] '( [SP] [condition] [SP] ) [NL]
			[IN] [statement] [EX]
	|	'do [NL] 
			[IN] [statement] [EX]
		'while '( [SP] [expression] [SP] ') '; [NL]
	|	'for [SP] '( [SP] [for_init_statement] [SP] [opt condition] '; [SP]
				[opt expression] [SP] ') [NL]
			[IN] [statement] [EX]
end define

define condition
		[expression]
	|	[repeat type_specifier+] [declarator] [SP] '= [SP] [assignment_expression]
end define

define for_init_statement
		[expression_statement]
	|	[simple_declaration]
end define

define jump_statement
		'break ';
	|	'continue ';
	|	'return [SPON] [opt expression] [SPOFF] ';
	|	'goto [id] ';
end define

define declaration_statement
		[block_declaration]
end define

%
% A.6 Declarations
%

define declaration
		[block_declaration] [NL]
	|	[function_definition] [NL]
	|	[template_declaration]
	|	[explicit_instantiation]
	|	[explicit_specialization]
	|	[linkage_specification] [NL]
	|	[namespace_definition] [NL]
	|	[comment] [NL]
end define

define block_declaration
		[simple_declaration]
	|	[asm_definition]
	|	[namespace_alias_definition]
	|	[using_declaration]
	|	[using_directive]
end define

define simple_declaration
		[SPON] [repeat decl_specifier] [SPOFF] [SP] [list init_declarator] ';
end define

define decl_specifier
		[storage_class_specifier]
	|	[type_specifier]
	|	[function_specifier]
	|	'friend
	|	'typedef
end define

define storage_class_specifier
		'auto
	|	'register
	|	'static
	|	'extern
	|	'mutable
end define

define function_specifier
		'inline
	|	'virtual
	|	'explicit
end define

define type_specifier
		[simple_type_specifier]
	|	[class_specifier]
	|	[enum_specifier]
	|	[elaborated_type_specifier]
	|	[cv_qualifier]
end define

define simple_type_specifier
		[opt '::] [opt nested_name_specifier] [type_name]
	|	'char | 'wchar_t | 'bool | 'short | 'int | 'long 
	|	'signed | 'unsigned | 'float | 'double | 'void
end define

define type_name
		[class_name]
	|	[enum_name]
end define

define elaborated_type_specifier
		[class_key] [opt '::] [opt nested_name_specifier] [class_name]
	|	'enum [opt '::] [opt nested_name_specifier] [enum_name]
	|	'typename [opt '::] [opt nested_name_specifier] [type_name]
end define

define enum_name
		'[ 'en [id] ']
	|	[id]
end define

define enum_specifier
		'enum [opt enum_name] [NL] '{ [NL] [IN] 
			[opt enumerator_definition_list] [opt ',] [NL] [EX] 
		'} 
end define

define enumerator_definition_list
		[enumerator_definition] [opt enumerator_definition_list_tail]
end define

define enumerator_definition_list_tail
		', [NL] [enumerator_definition_list]
end define

define enumerator_definition
		[enumerator]
	|	[enumerator] [SP] '= [SP] [constant_expression]
end define

define enumerator
		[id]
end define

define namespace_name
		'[ 'nn [id] ']
	|	[id]
end define

define namespace_definition
		'namespace [opt namespace_name] '{ [NL] [namespace_body] '}
end define

define namespace_alias_definition
		'namespace [namespace_name] [SP] '= [SP] [qualified_namespace_specifier] '; [NL]
end define

define qualified_namespace_specifier
		[opt '::] [opt nested_name_specifier] [namespace_name]
end define

define namespace_body
		[repeat declaration]
end define

define using_declaration
		'using [SPON] [id_expression] [SPOFF] ';
	|	'using [SPON] [type_id] [SPOFF]';
end define

define using_directive
		'using [SPON] 'namespace [opt '::] [opt nested_name_specifier] [namespace_name] [SPOFF]';
end define

define asm_definition
		'asm [SP] '( [stringlit] ') '; [NL]
end define

define linkage_specification
		'extern [SP] [stringlit] [SP] '{ [NL] [repeat declaration] '}
	|	'extern [SP] [stringlit] [SP] [declaration]
end define

%
% A.7 Declarators
%

define init_declarator
		[declarator] [opt initializer]
end define

define declarator
		[opt '$] [repeat ptr_operator] [declarator_or_declid] [repeat decl_array_or_param]
end define

define declarator_or_declid
		[declarator_id]
	|	[decl_open] [declarator] [decl_close]
end define

define decl_open
		'(|
	|	'(
end define

define decl_close
		'|)
	|	')
end define

define decl_array_or_param
		[param_decl_open]
			[opt parameter_declaration_clause]
		[param_decl_close] [SPON] [repeat cv_qualifier] [SPOFF]
		[opt exception_specification]
	|	[square_open] [opt constant_expression] [square_close]
end define

define square_open
		'[ '[
	|	'[
end define

define square_close
		'] ']
	|	']
end define

define param_decl_open
		'({
	|	'(
end define

define param_decl_close
		'})
	|	')
end define

define ptr_operator
		[ptr_amp]
	|	[opt '::] [opt nested_name_specifier] [ptr_star] [repeat cv_qualifier]
end define

define ptr_amp
		'<&>
	|	'&
end define

define ptr_star
		'<*>
	|	'*
end define

define cv_qualifier
		'const
	|	'volatile
end define

define declarator_id
		[opt '::] [opt nested_name_specifier] [unqual_declarator_id]
end define

define unqual_declarator_id
		[declarator_name] [opt template_id_args]
	|	'~ [declarator_name]
	|	[operator_function_id]
	|	[conversion_function_id]
end define

define declarator_name
		'[ 'di [id] ']
	|	[id]
end define


define type_id
		[repeat type_specifier+] [opt abstract_declarator]
end define

define abstract_declarator
		[repeat ptr_operator+] [opt rec_abstract_declarator] [repeat abstract_decl_array_or_param]
	|	[decl_open] [abstract_declarator] [decl_close] [repeat abstract_decl_array_or_param]
end define

define rec_abstract_declarator
		[decl_open] [abstract_declarator] [decl_close]
end define

define abstract_decl_array_or_param
		[param_decl_open]
			[opt parameter_declaration_clause]
		[param_decl_close] [repeat cv_qualifier] [opt exception_specification]
	|	[square_open] [constant_expression] [square_close]
end define

define parameter_declaration_clause
		'...
	|	[parameter_declaration_list] [opt trailing_dot_dot_dot] 
end define

define parameter_declaration_list
		[parameter_declaration] [opt parameter_declaration_list_tail]
end define

define parameter_declaration_list_tail
		', [SP] [parameter_declaration_list]
end define

define trailing_dot_dot_dot
		'...
	|	', '...
end define

define parameter_declaration
		[SPON] [repeat decl_specifier+] [SPOFF] [opt parameter_declarator] [opt parameter_init]
end define

define parameter_declarator
		[SP] [declarator]
	|	[SP] [abstract_declarator]
end define

define parameter_init
		[SP] '= [SP] [assignment_expression]
end define

define function_definition
		[SPON] [repeat decl_specifier] [SPOFF] [SP] [declarator] [NL] [function_def_body]
end define

define function_def_body
		[function_try_block]
	|	[opt ctor_initializer] [function_body]
end define

define function_body
		[IN] [compound_statement] [EX]
end define

define initializer
		[SP] '= [SP] [initializer_clause]
	|	'( [expression] ')
end define

define initializer_clause
		[assignment_expression]
	|	'{ [list initializer_clause+] [opt ',] '}
	|	'{ '}
end define


%
% A.8 Classes
%
define class_name
		[class_id] [opt template_id_args]
end define

define class_id
		'[ 'cn [id] ']
	|	[id]
end define

define class_specifier
		[class_head] [NL] '{ [NL] [IN] [repeat member_item] [EX] '}
end define

define class_head
		[class_key] [opt class_name] [opt base_clause]
	|	[class_key] [nested_name_specifier] [class_name] [opt base_clause]
end define

define class_key
		'class 
	|	'struct 
	|	'union
end define

define member_item
		[member_declaration]
	|	[access_specifier] [label_colon] [NL]
end define

define member_declaration
		[SPON] [repeat decl_specifier] [SPOFF] [SP] [list member_declarator] '; [NL]
	|	[function_definition] [opt ';] [NL]
	|	[using_declaration] [NL]
	|	[template_declaration] [NL]
end define

define member_declarator
		[declarator] [opt member_declarator_part]
	|	[declarator] [bitfield_colon] [constant_expression]
end define

define bitfield_colon
		':/
	|	':
end define

define member_declarator_part
		[pure_specifier]
	|	[constant_initializer]
end define

define pure_specifier
		[SP] '= [SP] '0
end define

define constant_initializer
		[SP] '= [SP] [constant_expression]
end define

%
% A.9 Derived Classes
%

define base_clause
		[NL] [IN_8] [base_colon] [SP] [list base_specifier+] [EX_8]
end define

define base_colon
		':+
	|	':
end define

define base_specifier
		[opt '::] [opt nested_name_specifier] [class_name]
	|	'virtual [opt access_specifier] [opt '::] [opt nested_name_specifier] [class_name]
	|	[access_specifier] [opt 'virtual] [opt '::] [opt nested_name_specifier] [class_name]
end define

define access_specifier
		'private
	|	'protected
	|	'public
end define

%
% A.10 Special Member Functions
%

define conversion_function_id
		'operator [conversion_type_id]
end define

define conversion_type_id
		[repeat type_specifier+] [opt conversion_declarator]
end define

define conversion_declarator
		[ptr_operator] [opt conversion_declarator]
end define

define ctor_initializer
		[ctor_colon] [NL] [IN] [list mem_initializer] [EX] [NL]
end define

define ctor_colon
		':-
	|	':
end define

define mem_initializer
		[mem_initializer_id] '( [opt expression] ')
end define

define mem_initializer_id
		[opt '::] [opt nested_name_specifier] [id] [opt template_id_args]
end define

%
% A.11 Operator Overloading
%

define operator_function_id
		'operator [operator]
end define

define operator
		'new | 'delete 
	|	'new [square_open] [square_close] 
	|	'delete [square_open] [square_close]
	|	'+ | '- | '* | '/ | '% | '^ | '& | '|
	|	'~ | '!  | '= | '< | '> | '+= | '-=
	|	'*= | '/= | '%= | '^= | '&= | '|=
	|	'<< | '>> | '>>= | '<<= | '== | '!=
	|	'<= | '>= | '&& | '|| | '++ | '-- | ',
	|	'.* | '->* | '-> | '( ') | [square_open] [square_close]
end define

%
% A.12 Templates
%

define template_declaration
		[SPON] [opt 'export] 'template [SPOFF] [SP] [templ_param_open] 
			[template_parameter_list] 
		[templ_param_close] [SP] [declaration]
end define

define template_parameter_list
		[template_parameter] [opt template_parameter_list_tail]
end define

define template_parameter_list_tail
		', [SP] [template_parameter_list]
end define

define templ_param_open
		'<{
	|	'<
end define

define templ_param_close
		'}>
	|	'>
end define

define template_parameter
		[type_parameter]
	|	[parameter_declaration]
end define

define type_parameter
		'class [SPON] [opt id] [SPOFF] [opt type_parameter_init]
	|	'typename [SPON] [opt id] [SPOFF] [opt type_parameter_init]
	|	'template [SP] [templ_param_open] [list template_parameter] 
			[templ_param_close] [SP] 'class [SPON] [opt id] [SPOFF] [opt type_parameter_templ_init]
end define

define type_parameter_init
		[SP] '= [SP] [type_id]
end define

define type_parameter_templ_init
		[SP] '= [SP] [id_expression]
end define

define template_id
		[template_name] [templ_arg_open] [list template_argument] [templ_arg_close]
end define

define template_id_args
		[templ_arg_open] [list template_argument] [templ_arg_close]
end define

define template_name
		[id]
end define

define template_argument
		[type_id]
	|	[assignment_expression]
end define

define explicit_instantiation
		[opt 'extern] 'template [declaration]
end define

define explicit_specialization
		'template [templ_param_open] [templ_param_close] [declaration]
end define

%
% A.13 Exception Handling
%

define try_block
		'try [compound_statement] [repeat handler+]
end define

define function_try_block
		'try [opt ctor_initializer] [function_body] [repeat handler+]
end define

define handler
		'catch '( [exception_declaration] ') [compound_statement]
end define

define exception_declaration
		[repeat type_specifier+] [declarator]
	|	[repeat type_specifier+] [abstract_declarator]
	|	[repeat type_specifier+]
	|	'...
end define

define throw_expression
		'throw [opt assignment_expression]
end define

define exception_specification
		'throw '( [list type_id] ')
end define

%
% Program
%

define cpp_program
		[SPOFF] [repeat declaration]
end define

%
% Rules for undoing the disambiguation.
%

% Undo opening of parameter declaration.
rule undoParamDeclOpen
	replace $ [param_decl_open]
		'({
	by
		'(
end rule

% Undo closing of parameter declaration.
rule undoParamDeclClose
	replace $ [param_decl_close]
		'})
	by
		')
end rule

% Undo opening of declarator brackets.
rule undoDeclOpen
	replace $ [decl_open]
		'(|
	by
		'(
end rule

% Undo closing of declarator brackets.
rule undoDeclClose
	replace $ [decl_close]
		'|)
	by
		')
end rule

% Undo opening of template parameters.
rule undoTemplParamOpen
	replace $ [templ_param_open]
		'<{
	by
		'<
end rule

% Undo closing of template parameters.
rule undoTemplParamClose
	replace $ [templ_param_close]
		'}>
	by
		'>
end rule

% Undo opening of template arguments.
rule undoTemplArgOpen
	replace $ [templ_arg_open]
		'<|
	by
		'<
end rule

% Undo closing of template arguments.
rule undoTemplArgClose
	replace $ [templ_arg_close]
		'|>
	by
		'>
end rule

% Undo opening of a square bracket.
rule undoSquareOpen
	replace $ [square_open]
		'[ '[
	by
		'[
end rule

% Undo closing of a square bracket.
rule undoSquareClose
	replace $ [square_close]
		'] ']
	by
		']
end rule


% Undo <&> in a declarator.
rule undoPtrAmp
	replace $ [ptr_amp]
		'<&>
	by
		'&
end rule

% Undo <*> in a declarator.
rule undoPtrStar
	replace $ [ptr_star]
		'<*>
	by
		'*
end rule

% Undo a bitfield colon.
rule undoBitfieldColon
	replace $ [bitfield_colon]
		':/
	by
		':
end rule

% Undo a base specifier colon.
rule undoBaseColon
	replace $ [base_colon]
		':+
	by
		':
end rule

% Undo a constructor init colon.
rule undoCtorColon
	replace $ [ctor_colon]
		':-
	by
		':
end rule

% Undo a label colon.
rule undoLabelColon
	replace $ [label_colon]
		':~
	by
		':
end rule

% Undo a label colon.
rule undoConditionalColon
	replace $ [conditional_colon]
		':|
	by
		':
end rule


% Undo the the opening of a declarator.
rule undoDeclaratorBar
	replace $ [opt '$]
		'$
	by
		% Empty
end rule

% Undo the declarator name.
rule undoDeclaratorName
	replace $ [declarator_name]
		'[ 'di Id [id] ']
	by
		Id
end rule

% Undo the namespace name.
rule undoNamespaceName
	replace $ [namespace_name]
		'[ 'nn Id [id] ']
	by
		Id
end rule

% Undo the class id.
rule undoClassId
	replace $ [class_id]
		'[ 'cn Id [id] ']
	by
		Id
end rule

% Undo the enumerator name.
rule undoEnumName
	replace $ [enum_name]
		'[ 'en Id [id] ']
	by
		Id
end rule

rule undoInclude
	replace $ [declaration]
		Comment [comment]
	where
		Comment [grep "/* CXXPREP #include"]
	construct Zero [number]
		0
	construct TailPos [number]
		Zero [# Comment] [- 3]
	by
		Comment [: 12 TailPos]
end rule


% Tie all undo rules together.
function undoDisambig
	replace [program]
		P [program]
	by
		P 
			[undoParamDeclOpen] 
			[undoParamDeclClose]
			[undoDeclOpen]
			[undoDeclClose]
			[undoTemplParamOpen]
			[undoTemplParamClose]
			[undoTemplArgOpen]
			[undoTemplArgClose]
			[undoSquareOpen]
			[undoSquareClose]
			[undoPtrAmp]
			[undoPtrStar]
			[undoBitfieldColon]
			[undoBaseColon]
			[undoCtorColon]
			[undoLabelColon]
			[undoConditionalColon]
			[undoDeclaratorName]
			[undoDeclaratorBar]
			[undoNamespaceName]
			[undoEnumName]
			[undoClassId]
			[undoInclude]
end function