Grammar CPPParser
ANTLR-generated HTML file from CPP_parser.g

Terence Parr, MageLang Institute
ANTLR Version 2.7.5 (20050403); 1989-2005


Definition of parser CPPParser, which is a subclass of LLkParser.


translation_unit
	:	( external_declaration )+ EOF 
	;


external_declaration
	:	(	"template" LESSTHAN GREATERTHAN declaration 
		|	declaration 
		|	template_head declaration 
		|	enum_specifier 
			(	init_declarator_list 
			|	
			) 
			SEMICOLON 
		|	
			(	template_head 
			|	
			) 
			dtor_head dtor_body 
		|	ctor_definition 
		|	
			(	"inline" 
			|	
			) 
			scope_override conversion_function_decl_or_def 
		|	declaration 
		|	function_definition 
		|	function_definition 
		|	template_head declaration_specifiers 
			(	init_declarator_list 
			|	
			) 
			SEMICOLON 
		|	template_head 
			(	ctor_definition 
			|	declaration 
			|	function_definition 
			) 
			
		|	decl_namespace 
		|	declaration 
		|	SEMICOLON 
		) 
	;


declaration
	:	linkage_specification 
	|	declaration_specifiers 
		(	(	COMMA 
			|	
			) 
			(	attribute 
			|	
			) 
			init_declarator_list 
		|	
		) 
		SEMICOLON 
	|	using_declaration 
	;


class_head
	:	
		(	"struct" 
		|	"union" 
		|	"class" 
		) 
		(	ID 
			(	LESSTHAN template_argument_list GREATERTHAN 
			|	
			) 
			(	base_clause 
			|	
			) 
		|	
		) 
		LCURLY 
	;


template_head
	:	"template" LESSTHAN template_parameter_list GREATERTHAN 
	;


enum_specifier
	:	"enum" 
		(	LCURLY enumerator_list RCURLY 
		|	ID 
			(	LCURLY enumerator_list RCURLY 
			|	
			) 
			
		) 
		
	;


init_declarator_list
	:	init_declarator ( COMMA init_declarator )* 
		(	attribute 
		|	
		) 
		
	;


dtor_head
	:	dtor_decl_spec dtor_declarator 
	;


dtor_body
	:	compound_statement 
	;


ctor_decl_spec
	:	
		(	(	"inline" 
			|	"_inline" 
			|	"__inline" 
			) 
			
		|	"explicit" 
		)* 
		
	;


ctor_definition
	:	ctor_head ctor_body 
	;


scope_override
	:	(	SCOPE 
		|	
		) 
		( ID 
			(	LESSTHAN template_argument_list GREATERTHAN 
			|	
			) 
			SCOPE )* 
	;


conversion_function_decl_or_def
	:	OPERATOR declaration_specifiers 
		(	STAR 
		|	AMPERSAND 
		|	
		) 
		(	LESSTHAN template_parameter_list GREATERTHAN 
		|	
		) 
		LPAREN 
		(	parameter_list 
		|	
		) 
		RPAREN 
		(	type_qualifier 
		|	
		) 
		(	exception_specification 
		|	
		) 
		(	compound_statement 
		|	SEMICOLON 
		) 
		
	;


declaration_specifiers
	:	
		(	(	storage_class_specifier 
			|	type_qualifier 
			|	(	"inline" 
				|	"_inline" 
				|	"__inline" 
				) 
				
			|	"virtual" 
			|	"explicit" 
			|	"typedef" 
			|	"friend" 
			|	(	"_stdcall" 
				|	"__stdcall" 
				) 
				
			)* 
			type_specifier 
		|	"typename" direct_declarator 
		) 
		
	;


function_declarator
	:	ptr_operator function_declarator 
	|	function_direct_declarator 
	;


function_definition
	:	
		(	declaration_specifiers function_declarator 
			(	( declaration )* 
			|	
			) 
			compound_statement 
		|	function_declarator 
			(	( declaration )* 
			|	
			) 
			compound_statement 
		) 
		
	;


decl_namespace
	:	"namespace" 
		(	
			(	ID 
			|	
			) 
			LCURLY ( external_declaration )* RCURLY 
		|	ID ASSIGNEQUAL qualified_id SEMICOLON 
		) 
	;


qualified_id
	:	scope_override 
		(	ID 
			(	LESSTHAN template_argument_list GREATERTHAN 
			|	
			) 
			
		|	OPERATOR optor 
		|	"this" 
		|	(	"true" 
			|	"false" 
			) 
		) 
		
	;


member_declaration
	:	(	declaration 
		|	enum_specifier 
			(	member_declarator_list 
			|	
			) 
			SEMICOLON 
		|	ctor_decl_spec ctor_declarator SEMICOLON 
		|	ctor_definition 
		|	dtor_head SEMICOLON 
		|	dtor_head dtor_body 
		|	declaration 
		|	function_definition 
		|	
			(	"inline" 
			|	
			) 
			conversion_function_decl_or_def 
		|	qualified_id SEMICOLON 
		|	declaration_specifiers 
			(	member_declarator_list 
			|	
			) 
			SEMICOLON 
		|	function_declarator SEMICOLON 
		|	function_declarator compound_statement 
		|	template_head declaration_specifiers 
			(	init_declarator_list 
			|	
			) 
			SEMICOLON 
		|	template_head 
			(	ctor_definition 
			|	declaration 
			|	function_definition 
			|	conversion_function_decl_or_def 
			) 
			
		|	access_specifier COLON 
		|	SEMICOLON 
		) 
	;


member_declarator_list
	:	member_declarator ( COMMA member_declarator )* 
	;


ctor_declarator
	:	qualified_ctor_id LPAREN 
		(	parameter_list 
		|	
		) 
		RPAREN 
		(	exception_specification 
		|	
		) 
		
	;


compound_statement
	:	LCURLY 
		(	statement_list 
		|	
		) 
		RCURLY 
	;


access_specifier
	:	"public" 
	|	"protected" 
	|	"private" 
	;


linkage_specification
	:	"extern" StringLiteral 
		(	LCURLY ( external_declaration )* RCURLY 
		|	declaration 
		) 
	;


attribute
	:	"__attribute__" LPAREN LPAREN attribute_decl_list RPAREN RPAREN 
	;


using_declaration
	:	"using" 
		(	"namespace" qualified_id 
		|	qualified_id 
		) 
		SEMICOLON 
	;


storage_class_specifier
	:	"auto" 
	|	"register" 
	|	"static" 
	|	"extern" 
	|	"mutable" 
	;


type_qualifier
	:	(	"const" 
		|	"const_cast" 
		) 
		
	|	"volatile" 
	;


type_specifier
	:	simple_type_specifier 
	|	class_specifier 
	|	enum_specifier 
	;


direct_declarator
	:	qualified_id LPAREN 
		(	parameter_list 
		|	
		) 
		RPAREN ( type_qualifier )* 
		(	exception_specification 
		|	
		) 
	|	qualified_id LPAREN expression_list RPAREN 
	|	qualified_id ( LSQUARE 
			(	constant_expression 
			|	
			) 
			RSQUARE )+ 
	|	qualified_id 
	|	TILDE ID LPAREN 
		(	parameter_list 
		|	
		) 
		RPAREN 
	|	LPAREN declarator RPAREN declarator_suffixes 
	;


attribute_decl_list
	:	"__mode__" LPAREN ID RPAREN 
	|	"const" 
	;


simple_type_specifier
	:	(	qualified_type 
		|	(	"char" 
			|	"wchar_t" 
			|	"bool" 
			|	"short" 
			|	"int" 
			|	(	"_int64" 
				|	"__int64" 
				) 
				
			|	"__w64" 
			|	"long" 
			|	"signed" 
			|	"unsigned" 
			|	"float" 
			|	"double" 
			|	"void" 
			|	(	"_declspec" 
				|	"__declspec" 
				) 
				LPAREN ID RPAREN 
			)+ 
		|	qualified_type 
		) 
		
	;


class_specifier
	:	(	"class" 
		|	"struct" 
		|	"union" 
		) 
		(	qualified_id 
			(	
				(	base_clause 
				|	
				) 
				LCURLY ( member_declaration )* RCURLY 
			|	
			) 
		|	LCURLY ( member_declaration )* RCURLY 
		) 
	;


qualified_type
	:	scope_override ID 
		(	LESSTHAN template_argument_list GREATERTHAN 
		|	
		) 
		
	;


template_argument_list
	:	template_argument ( COMMA template_argument )* 
	;


base_clause
	:	COLON base_specifier ( COMMA base_specifier )* 
	;


enumerator_list
	:	enumerator ( COMMA enumerator )* 
	;


enumerator
	:	ID 
		(	ASSIGNEQUAL constant_expression 
		|	
		) 
		
	;


constant_expression
	:	conditional_expression 
	;


optor
	:	"new" 
		(	LSQUARE RSQUARE 
		|	
		) 
	|	"delete" 
		(	LSQUARE RSQUARE 
		|	
		) 
	|	LPAREN RPAREN 
	|	LSQUARE RSQUARE 
	|	optor_simple_tokclass 
	;


typeID
	:	ID 
	;


init_declarator
	:	declarator 
		(	ASSIGNEQUAL initializer 
		|	LPAREN expression_list RPAREN 
		|	
		) 
	;


declarator
	:	ptr_operator declarator 
	|	direct_declarator 
	;


initializer
	:	remainder_expression 
	|	LCURLY initializer ( COMMA initializer )* RCURLY 
	|	gcc_designator initializer 
	|	c99_designator initializer 
	;


expression_list
	:	assignment_expression ( COMMA assignment_expression )* 
	;


remainder_expression
	:	(	assignment_expression 
		|	assignment_expression 
		) 
	;


gcc_designator
	:	ID COLON 
	;


c99_designator
	:	DOT ID EQUALS 
	;


base_specifier
	:	
		(	"virtual" 
			(	access_specifier 
			|	
			) 
			qualified_type 
		|	access_specifier 
			(	"virtual" 
			|	
			) 
			qualified_type 
		|	qualified_type 
		) 
	;


member_declarator
	:	
		(	ID 
		|	
		) 
		COLON constant_expression 
		(	ASSIGNEQUAL OCTALINT 
		|	
		) 
	|	declarator 
		(	ASSIGNEQUAL OCTALINT 
		|	
		) 
	;


template_parameter_list
	:	template_parameter ( COMMA template_parameter )* 
	;


parameter_list
	:	parameter_declaration_list 
		(	ELLIPSIS 
		|	
		) 
	;


exception_specification
	:	"throw" LPAREN 
		(	(	scope_override ID 
				(	COMMA scope_override ID 
				|	
				) 
			|	
			) 
		|	ELLIPSIS 
		) 
		RPAREN 
	;


cv_qualifier_seq
	:	( type_qualifier )* 
	;


ptr_operator
	:	
		(	AMPERSAND 
		|	(	"_cdecl" 
			|	"__cdecl" 
			) 
			
		|	(	"_near" 
			|	"__near" 
			) 
			
		|	(	"_far" 
			|	"__far" 
			) 
			
		|	"__interrupt" 
		|	(	"pascal" 
			|	"_pascal" 
			|	"__pascal" 
			) 
			
		|	(	"_stdcall" 
			|	"__stdcall" 
			) 
			
		|	ptr_to_member 
		) 
	;


declarator_suffixes
	:	(	( LSQUARE 
				(	constant_expression 
				|	
				) 
				RSQUARE )+ 
		|	LPAREN 
			(	parameter_list 
			|	
			) 
			RPAREN ( type_qualifier )* 
			(	exception_specification 
			|	
			) 
		) 
		
	;


function_direct_declarator
	:	(	LPAREN qualified_id RPAREN 
		|	qualified_id 
		) 
		LPAREN 
		(	parameter_list 
		|	
		) 
		RPAREN ( type_qualifier )* 
		(	ASSIGNEQUAL OCTALINT 
		|	
		) 
		
		(	exception_specification 
		|	
		) 
	;


ctor_head
	:	ctor_decl_spec ctor_declarator 
	;


ctor_body
	:	
		(	ctor_initializer 
		|	
		) 
		compound_statement 
	;


qualified_ctor_id
	:	scope_override ID 
	;


ctor_initializer
	:	COLON superclass_init ( COMMA superclass_init )* 
	;


superclass_init
	:	qualified_id LPAREN 
		(	expression_list 
		|	
		) 
		RPAREN 
	;


dtor_decl_spec
	:	
		(	(	"inline" 
			|	"_inline" 
			|	"__inline" 
			) 
			
		|	"virtual" 
		)* 
	;


dtor_declarator
	:	scope_override TILDE ID LPAREN RPAREN 
		(	exception_specification 
		|	
		) 
		
	;


parameter_declaration_list
	:	( parameter_declaration ( COMMA parameter_declaration )* ) 
	;


parameter_declaration
	:	
		(	declaration_specifiers 
			(	declarator 
			|	abstract_declarator 
			) 
		|	declarator 
		|	ELLIPSIS 
		) 
		(	ASSIGNEQUAL remainder_expression 
		|	
		) 
	;


abstract_declarator
	:	ptr_operator abstract_declarator 
	|	LPAREN abstract_declarator RPAREN ( abstract_declarator_suffix )+ 
	|	( LSQUARE 
			(	constant_expression 
			|	
			) 
			RSQUARE )+ 
	|	
	;


type_name
	:	declaration_specifiers abstract_declarator 
	;


abstract_declarator_suffix
	:	LSQUARE 
		(	constant_expression 
		|	
		) 
		RSQUARE 
	|	LPAREN 
		(	parameter_list 
		|	
		) 
		RPAREN cv_qualifier_seq 
		(	exception_specification 
		|	
		) 
	;


template_parameter
	:	(	(	"class" 
			|	"typename" 
			) 
			(	ID 
				(	ASSIGNEQUAL assigned_type_name 
				|	
				) 
			|	
			) 
			
		|	parameter_declaration 
		) 
		
	;


assigned_type_name
	:	(	qualified_type abstract_declarator 
		|	simple_type_specifier abstract_declarator 
		) 
	;


template_id
	:	ID LESSTHAN template_argument_list GREATERTHAN 
	;


template_argument
	:	type_name 
	|	shift_expression 
	;


shift_expression
	:	additive_expression ( (	SHIFTLEFT 
			|	SHIFTRIGHT 
			) 
			additive_expression )* 
	;


statement_list
	:	( statement )+ 
	;


statement
	:	(	declaration 
		|	labeled_statement 
		|	case_statement 
		|	default_statement 
		|	expression SEMICOLON 
		|	compound_statement 
		|	selection_statement 
		|	iteration_statement 
		|	jump_statement 
		|	SEMICOLON 
		|	try_block 
		|	throw_statement 
		|	asm_block 
		) 
	;


labeled_statement
	:	ID COLON statement 
	;


case_statement
	:	"case" constant_expression COLON statement 
	;


default_statement
	:	"default" COLON statement 
	;


expression
	:	assignment_expression ( COMMA assignment_expression )* 
	;


selection_statement
	:	"if" LPAREN expression RPAREN statement 
		(	"else" statement 
		|	
		) 
	|	"switch" LPAREN expression RPAREN statement 
	;


iteration_statement
	:	"while" LPAREN expression RPAREN statement 
	|	"do" statement "while" LPAREN expression RPAREN SEMICOLON 
	|	"for" LPAREN 
		(	declaration 
		|	expression SEMICOLON 
		|	SEMICOLON 
		) 
		
		(	expression 
		|	
		) 
		SEMICOLON 
		(	expression 
		|	
		) 
		RPAREN statement 
	;


jump_statement
	:	(	"goto" ID SEMICOLON 
		|	"continue" SEMICOLON 
		|	"break" SEMICOLON 
		|	"return" expression SEMICOLON 
		) 
	;


try_block
	:	"try" compound_statement ( handler )* 
	;


throw_statement
	:	"throw" 
		(	assignment_expression 
		|	
		) 
		SEMICOLON 
	;


asm_block
	:	std_asm_block 
	|	gcc_asm_block 
	;


handler
	:	"catch" LPAREN exception_declaration RPAREN compound_statement 
	;


exception_declaration
	:	parameter_declaration_list 
	;


assignment_expression
	:	conditional_expression 
		(	(	ASSIGNEQUAL 
			|	TIMESEQUAL 
			|	DIVIDEEQUAL 
			|	MINUSEQUAL 
			|	PLUSEQUAL 
			|	MODEQUAL 
			|	SHIFTLEFTEQUAL 
			|	SHIFTRIGHTEQUAL 
			|	BITWISEANDEQUAL 
			|	BITWISEXOREQUAL 
			|	BITWISEOREQUAL 
			) 
			remainder_expression 
		|	
		) 
	;


std_asm_block
	:	(	"_asm" 
		|	"__asm" 
		) 
		LCURLY ( ~RCURLY )* RCURLY 
	;


gcc_asm_block
	:	(	"__asm__" 
		|	"asm" 
		) 
		(	"volatile" 
		|	
		) 
		LPAREN asm_code COLON 
		(	asm_reg_spec 
		|	
		) 
		COLON 
		(	asm_reg_spec 
		|	
		) 
		RPAREN 
	;


asm_code
	:	( StringLiteral )+ 
	;


asm_reg_spec
	:	StringLiteral LPAREN ID RPAREN ( COMMA StringLiteral LPAREN ID RPAREN )* 
	;


conditional_expression
	:	logical_or_expression 
		(	QUESTIONMARK expression COLON conditional_expression 
		|	
		) 
	;


logical_or_expression
	:	logical_and_expression ( OR logical_and_expression )* 
	;


logical_and_expression
	:	inclusive_or_expression ( AND inclusive_or_expression )* 
	;


inclusive_or_expression
	:	exclusive_or_expression ( BITWISEOR exclusive_or_expression )* 
	;


exclusive_or_expression
	:	and_expression ( BITWISEXOR and_expression )* 
	;


and_expression
	:	equality_expression ( AMPERSAND equality_expression )* 
	;


equality_expression
	:	relational_expression ( (	NOTEQUAL 
			|	EQUAL 
			) 
			relational_expression )* 
	;


relational_expression
	:	shift_expression ( (	LESSTHAN 
			|	GREATERTHAN 
			|	LESSTHANOREQUALTO 
			|	GREATERTHANOREQUALTO 
			) 
			shift_expression )* 
	;


additive_expression
	:	multiplicative_expression ( (	PLUS 
			|	MINUS 
			) 
			multiplicative_expression )* 
	;


multiplicative_expression
	:	pm_expression ( (	STAR 
			|	DIVIDE 
			|	MOD 
			) 
			pm_expression )* 
	;


pm_expression
	:	cast_expression ( (	DOTMBR 
			|	POINTERTOMBR 
			) 
			cast_expression )* 
	;


cast_expression
	:	compound_literal 
	|	LPAREN 
		(	type_qualifier 
		|	
		) 
		simple_type_specifier 
		(	ptr_operator 
		|	
		) 
		RPAREN cast_expression 
	|	unary_expression 
	;


compound_literal
	:	LPAREN type_name RPAREN LCURLY initializer RCURLY 
	;


unary_expression
	:	(	postfix_expression 
		|	PLUSPLUS unary_expression 
		|	MINUSMINUS unary_expression 
		|	unary_operator cast_expression 
		|	"sizeof" 
			(	LPAREN type_name RPAREN 
			|	unary_expression 
			) 
		|	
			(	SCOPE 
			|	
			) 
			(	new_expression 
			|	delete_expression 
			) 
		) 
	;


postfix_expression
	:	(	simple_type_specifier LPAREN RPAREN LPAREN 
			(	expression_list 
			|	
			) 
			RPAREN 
		|	simple_type_specifier LPAREN 
			(	expression_list 
			|	
			) 
			RPAREN 
		|	primary_expression 
			(	LSQUARE expression RSQUARE 
			|	LPAREN 
				(	expression_list 
				|	
				) 
				RPAREN 
			|	DOT id_expression 
			|	POINTERTO id_expression 
			|	PLUSPLUS 
			|	MINUSMINUS 
			)* 
		|	(	"dynamic_cast" 
			|	"static_cast" 
			|	"reinterpret_cast" 
			|	"const_cast" 
			) 
			LESSTHAN type_specifier 
			(	ptr_operator 
			|	
			) 
			GREATERTHAN LPAREN expression RPAREN 
		) 
	;


unary_operator
	:	AMPERSAND 
	|	STAR 
	|	PLUS 
	|	MINUS 
	|	TILDE 
	|	NOT 
	;


new_expression
	:	( "new" 
			(	LPAREN expression_list RPAREN 
			|	
			) 
			(	new_type_id 
			|	LPAREN type_name RPAREN 
			) 
			(	new_initializer 
			|	
			) ) 
	;


delete_expression
	:	"delete" 
		(	LSQUARE RSQUARE 
		|	
		) 
		cast_expression 
	;


primary_expression
	:	id_expression 
	|	constant 
	|	"this" 
	|	LPAREN expression RPAREN 
	;


id_expression
	:	scope_override 
		(	ID 
		|	OPERATOR optor 
		|	TILDE 
			(	STAR 
			|	
			) 
			ID 
		) 
	;


constant
	:	OCTALINT 
	|	DECIMALINT 
	|	HEXADECIMALINT 
	|	CharLiteral 
	|	( StringLiteral )+ 
	|	FLOATONE 
	|	FLOATTWO 
	|	"true" 
	|	"false" 
	;


new_type_id
	:	declaration_specifiers 
		(	new_declarator 
		|	
		) 
	;


new_initializer
	:	LPAREN 
		(	expression_list 
		|	
		) 
		RPAREN 
	;


new_declarator
	:	ptr_operator 
		(	new_declarator 
		|	
		) 
	|	direct_new_declarator 
	;


direct_new_declarator
	:	( LSQUARE expression RSQUARE )+ 
	;


ptr_to_member
	:	scope_override STAR cv_qualifier_seq 
	;


optor_simple_tokclass
	:	(	PLUS 
		|	MINUS 
		|	STAR 
		|	DIVIDE 
		|	MOD 
		|	BITWISEXOR 
		|	AMPERSAND 
		|	BITWISEOR 
		|	TILDE 
		|	NOT 
		|	SHIFTLEFT 
		|	SHIFTRIGHT 
		|	ASSIGNEQUAL 
		|	TIMESEQUAL 
		|	DIVIDEEQUAL 
		|	MODEQUAL 
		|	PLUSEQUAL 
		|	MINUSEQUAL 
		|	SHIFTLEFTEQUAL 
		|	SHIFTRIGHTEQUAL 
		|	BITWISEANDEQUAL 
		|	BITWISEXOREQUAL 
		|	BITWISEOREQUAL 
		|	EQUAL 
		|	NOTEQUAL 
		|	LESSTHAN 
		|	GREATERTHAN 
		|	LESSTHANOREQUALTO 
		|	GREATERTHANOREQUALTO 
		|	OR 
		|	AND 
		|	PLUSPLUS 
		|	MINUSMINUS 
		|	COMMA 
		|	POINTERTO 
		|	POINTERTOMBR 
		) 
	;