⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cpp_parser.g

📁 C Preprocessor,antlr的grammar语言描述,用于学习词法分析,语法分析
💻 G
📖 第 1 页 / 共 5 页
字号:
	|  
		// Constructor definition (non-templated)
		// JEL 4/3/96 Added predicate that works, once the
		// restriction is added that ctor cannot be virtual
		// and ctor_declarator uses a more restrictive id
		(	(options {warnWhenFollowAmbig = false;}:
			 ctor_decl_spec)?
			{qualifiedItemIsOneOf(qiCtor)}?
		)=>
		{if (statementTrace>=1) 
			printf("%d external_declaration Constructor definition\n",LT(1)->getLine());
		}
		ctor_definition
	|  
		// User-defined type cast
		(("inline")? scope_override  conversion_function_decl_or_def)=>
		{if (statementTrace>=1) 
			printf("%d external_declaration Operator function\n",LT(1)->getLine());
		}
		("inline")? s = scope_override conversion_function_decl_or_def 
	|   
		// Function declaration
		(declaration_specifiers function_declarator[0] SEMICOLON)=> 
		{if (statementTrace>=1) 
			printf("%d external_declaration Function declaration\n",LT(1)->getLine());
		}
		declaration_specifiers function_declarator[0] SEMICOLON {end_of_stmt();}
	|
		// Function definition
		(declaration_specifiers	function_declarator[1] LCURLY)=> 
		{if (statementTrace>=1) 
			printf("%d external_declaration Function definition\n",LT(1)->getLine());
		}
		function_definition
	|
		// Function definition with int return assumed
		(function_declarator[1] LCURLY)=> 
		{if (statementTrace>=1) 
			printf("%d external_declaration Function definition without return type\n",LT(1)->getLine());
		}
		function_definition
	|
		// K & R Function definition
		(declaration_specifiers	function_declarator[1] declaration)=>
		{K_and_R = true;
		 if (statementTrace>=1) 
			printf("%d external_declaration K & R function definition\n",LT(1)->getLine());
		}
		function_definition
	|
		// K & R Function definition with int return assumed
		(function_declarator[1] declaration)=>
		{K_and_R = true;
		 if (statementTrace>=1) 
			printf("%d external_declaration K & R function definition without return type\n",LT(1)->getLine());
		}
		function_definition
	|
		// Class declaration or definition
		(("extern")? (fs = function_specifier)* class_specifier)=>
		{if (statementTrace>=1) 
			printf("%d external_declaration Class decl or def\n",LT(1)->getLine());
		}
		("extern")? (fs = function_specifier)* class_decl_or_def[fs] (init_declarator_list)? SEMICOLON {end_of_stmt();}
	|
		// Copied from member_declaration 31/05/07
		(declaration_specifiers (init_declarator_list)? SEMICOLON)=>
		{if (statementTrace>=1) 
			printf("%d external_declaration Declaration\n",LT(1)->getLine());
		}
		declaration
	|  
		// Templated functions and constructors matched here.
		{beginTemplateDeclaration();}
		template_head
		(  
			// templated forward class decl, init/decl of static member in template
			(declaration_specifiers (init_declarator_list)? SEMICOLON {end_of_stmt();})=>
			{if (statementTrace>=1) 
				printf("%d external_declaration Templated class forward declaration\n",LT(1)->getLine());
			}
			declaration_specifiers (init_declarator_list)? SEMICOLON {end_of_stmt();}
		|  
			// Templated function declaration
			(declaration_specifiers function_declarator[0] SEMICOLON)=> 
			{if (statementTrace>=1) 
				printf("%d external_declaration Templated function declaration\n",LT(1)->getLine());
			}
			declaration
		|  
			// Templated function definition
			(declaration_specifiers function_declarator[1] LCURLY)=> 
			{if (statementTrace>=1) 
				printf("%d external_declaration Templated function definition\n",LT(1)->getLine());
			}
			function_definition
		|
			// Templated constructor definition
            // JEL 4/3/96 Added predicate that works once the
            // restriction is added that ctor cannot be virtual
			(	ctor_decl_spec 
				{qualifiedItemIsOneOf(qiCtor)}?
			)=>
			{if (statementTrace>=1) 
				printf("%d external_declaration Templated constructor definition\n",LT(1)->getLine());
			}
			ctor_definition
		)
		{endTemplateDeclaration();}
	|  
		// Namespace definition
		{if (statementTrace>=1) 
			printf("%d external_declaration Namespace definition\n",LT(1)->getLine());
		}
		"namespace" namespace_definition
	|	
		// Semicolon
		{if (statementTrace>=1) 
			printf("%d external_declaration Semicolon\n",LT(1)->getLine());
		}
		SEMICOLON {end_of_stmt();}
	|	
		// Anything else 
		{if (statementTrace>=1) 
			printf("%d external_declaration Other Declaration\n",LT(1)->getLine());
		}
		declaration
	|	
		// The next two entries may be used for debugging
		// Use this statement in the source code to turn antlr trace on (See note above)
		"antlrTrace_on" {antlrTrace(true);}
	|
		// Use this statement in the source code to turn antlr trace off (See note above)
		"antlrTrace_off" {antlrTrace(false);}
	)
	;	// end of external_declaration

//namespace_definition
namespace_definition
	:
		(ns:ID{declaratorID((ns->getText()).data(),qiType);})?
		LCURLY 
		{enterNewLocalScope();}
		(external_declaration)*
		{exitLocalScope();}
		RCURLY
	;

//namespace_alias_definition
namespace_alias_definition
	{
	char *qid;
	}
	:
		"namespace"
		ns2:ID {declaratorID((ns2->getText()).data(),qiType);}
		ASSIGNEQUAL qid = qualified_id SEMICOLON {end_of_stmt();} 
	;

//member_declaration
member_declaration
	{
	char *q;
	lineNo = LT(1)->getLine();
	FunctionSpecifier fs = fsInvalid;	// inline,virtual,explicit
	}
	:
	(
		// Template explicit specialisation
		("template" LESSTHAN GREATERTHAN)=>
		{if(statementTrace>=1) 
			printf("%d member_declaration Template explicit-specialisation\n",LT(1)->getLine());
		}
		"template" LESSTHAN GREATERTHAN member_declaration
	|
		// All typedefs
		("typedef")=>
		(
			("typedef" "enum")=>
			{if(statementTrace>=1) 
				printf("%d member_declaration Typedef enum type\n",LT(1)->getLine());
			}
			"typedef" enum_specifier {_td = true;} (init_declarator_list)? SEMICOLON {end_of_stmt();}
		|
			(declaration_specifiers function_declarator[0] SEMICOLON)=>	// DW 11/02/05 This may not be possible member declaration
			{if(statementTrace>=1) 
				printf("%d member_declaration Typedef function type\n",LT(1)->getLine());
			}
			declaration
		|
			(declaration_specifiers (init_declarator_list)? SEMICOLON)=>
			{if(statementTrace>=1) 
				printf("%d member_declaration Typedef variable type\n",LT(1)->getLine());
			}
			declaration
		|
			("typedef" class_specifier)=>
			{if(statementTrace>=1) 
				printf("%d member_declaration Typedef class type\n",LT(1)->getLine());
			}
			"typedef" class_decl_or_def[fs] {_td = true;} (init_declarator_list)? SEMICOLON {end_of_stmt();}
		)
	|	
		// Templated class declaration or definition
		(template_head (fs = function_specifier)* class_specifier)=>
		{if (statementTrace>=1) 
			printf("%d member_declaration Templated class decl or def\n",LT(1)->getLine());
		}
		template_head (fs = function_specifier)* class_decl_or_def[fs] (init_declarator_list)? SEMICOLON {end_of_stmt();}	// declaration
	|  
		// Enum definition (don't want to backtrack over this in other alts)
		("enum" (ID)? LCURLY)=>
		{if (statementTrace>=1) 
			printf("%d member_declaration Enum definition\n",LT(1)->getLine());
		}
		enum_specifier (init_declarator_list)? SEMICOLON	{end_of_stmt();}
	|
		// Constructor declarator
		(	ctor_decl_spec
			{qualifiedItemIsOneOf(qiCtor)}?
			ctor_declarator[0] SEMICOLON
		)=>
		{if (statementTrace>=1) 
			printf("%d member_declaration Constructor declarator\n",LT(1)->getLine());
		}
		ctor_decl_spec ctor_declarator[0] SEMICOLON {end_of_stmt();}
	|  
		// JEL Predicate to distinguish ctor from function
		// This works now that ctor cannot have VIRTUAL
		// It unfortunately matches A::A where A is not enclosing
		// class -- this will have to be checked semantically
		// Constructor definition
		(	ctor_decl_spec
			{qualifiedItemIsOneOf(qiCtor)}?
			ctor_declarator[1]
			(COLON        // DEFINITION :ctor_initializer
			|LCURLY       // DEFINITION (compound Statement) ?
			)
		)=>
		{if (statementTrace>=1) 
			printf("%d member_declaration Constructor definition\n",LT(1)->getLine());
		}
		ctor_definition 
	|  
		// No template_head allowed for dtor member
		// Backtrack if not a dtor (no TILDE)
		// Destructor declaration
		(dtor_head[0] SEMICOLON)=>
		{if (statementTrace>=1) 
			printf("%d member_declaration Destructor declaration\n",LT(1)->getLine());
		}
		dtor_head[0] SEMICOLON {end_of_stmt();}
	|
		// No template_head allowed for dtor member
		// Backtrack if not a dtor (no TILDE)
		// Destructor definition
		(dtor_head[1] LCURLY)=>
		{if (statementTrace>=1) 
			printf("%d member_declaration Destructor definition\n",LT(1)->getLine());
		}
		dtor_head[1] dtor_body
	|
		// Function declaration
		(declaration_specifiers	function_declarator[0] SEMICOLON)=>
		{if (statementTrace>=1) 
			printf("%d member_declaration Function declaration\n",LT(1)->getLine());
		}
		declaration_specifiers function_declarator[0] SEMICOLON {end_of_stmt();}
	|  
		// Function definition
		(declaration_specifiers function_declarator[1] LCURLY)=>
		{if (statementTrace>=1) 
			printf("%d member_declaration Function definition\n",LT(1)->getLine());
		}
		function_definition
	|  
		// User-defined type cast
		(("inline")? conversion_function_decl_or_def)=>
		{if (statementTrace>=1) 
			printf("%d member_declaration Operator function\n",LT(1)->getLine());
		}
		("inline")? conversion_function_decl_or_def
	|  
		// Hack to handle decls like "superclass::member",
		// to redefine access to private base class public members
		// Qualified identifier
		(qualified_id SEMICOLON)=>
		{if (statementTrace>=1) 
			printf("%d member_declaration Qualified ID\n",LT(1)->getLine());
		}
		q = qualified_id SEMICOLON {end_of_stmt();}
	|
		// Class declaration or definition
		(("friend")? (fs = function_specifier)* class_specifier)=>
		{if (statementTrace>=1) 
			printf("%d member_declaration Class decl or def\n",LT(1)->getLine());
		}
		("friend")? (fs = function_specifier)* class_decl_or_def[fs] (init_declarator_list)? SEMICOLON {end_of_stmt();}
	|  
		(declaration_specifiers (init_declarator_list)? SEMICOLON)=>
		{if (statementTrace>=1) 
			printf("%d member_declaration Declaration\n",LT(1)->getLine());
		}
		declaration
	|  
		// Member without a type (I guess it can only be a function declaration or definition)
		((fs = function_specifier)* function_declarator[0] SEMICOLON)=>
		{fprintf(stderr,"%d warning Function declaration found without return type\n",LT(1)->getLine());
		 if (statementTrace>=1) 
			printf("%d member_declaration Function declaration\n",LT(1)->getLine());
		}
		(fs = function_specifier)* function_declarator[0] SEMICOLON {end_of_stmt();}
	|
		// Member without a type (I guess it can only be a function definition)
		((fs = function_specifier)* function_declarator[1] LCURLY)=>
		{fprintf(stderr,"%d warning Function definition found without return type\n",LT(1)->getLine());
		 if (statementTrace>=1) 
			printf("%d member_declaration Function definition without return type\n",LT(1)->getLine());
		}
		(fs = function_specifier)* function_declarator[1] compound_statement {endFunctionDefinition();}
	|  
		// Templated functions and constructors matched here.
		{beginTemplateDeclaration();}
		template_head
		(  
			// templated forward class decl, init/decl of static member in template
			(declaration_specifiers (init_declarator_list)? SEMICOLON)=>
			{if (statementTrace>=1) 
				printf("%d member_declaration Templated forward declaration\n",LT(1)->getLine());
			}
			declaration_specifiers (init_declarator_list)? SEMICOLON {end_of_stmt();}
		|
			// Templated function declaration
			(declaration_specifiers function_declarator[0] SEMICOLON)=>
			{if (statementTrace>=1) 
				printf("%d member_declaration Templated function declaration\n",LT(1)->getLine());
			}
			declaration
		|  
			// Templated function definition
			(declaration_specifiers function_declarator[1] LCURLY)=> 
			{if (statementTrace>=1) 
				printf("%d member_declaration Templated function definition\n",LT(1)->getLine());
			}
			function_definition
		|
			// Templated constructor declarator
			(	ctor_decl_spec
				{qualifiedItemIsOneOf(qiCtor)}?
				ctor_declarator[0] SEMICOLON
			)=>
			{if (statementTrace>=1) 
				printf("%d member_declaration Templated constructor declarator\n",LT(1)->getLine());
			}
			ctor_decl_spec ctor_declarator[0] SEMICOLON {end_of_stmt();}
		|
			// Templated constructor definition
			// JEL 4/3/96 Added predicate that works once the
			// restriction is added that ctor cannot be virtual
			(ctor_decl_spec {qualifiedItemIsOneOf(qiCtor)}?
			)=>
			{if (statementTrace>=1) 
				printf("%d member_declaration Templated constructor definition\n",LT(1)->getLine());
			}
			ctor_definition
		|
			// Templated operator function
			{if (statementTrace>=1) 
				printf("%d member_declaration Templated operator function\n",LT(1)->getLine());
			}
			conversion_function_decl_or_def
		|
			// Templated class definition
			{if (statementTrace>=1) 
				printf("%d member_declaration Templated class definition\n",LT(1)->getLine());
			}
			class_head declaration_specifiers (init_declarator_list)? SEMICOLON {end_of_stmt();}
		)
		{endTemplateDeclaration();}
	|
		// Access specifier  
		{if (statementTrace>=1) 
			printf("%d member_declaration Access specifier\n",LT(1)->getLine());
		}
		access_specifier COLON
	|
		// Semicolon
		{if (statementTrace>=1) 
			printf("%d member_declaration Semicolon\n",LT(1)->getLine());
		}
		SEMICOLON {end_of_stmt();}
	|	
		// The next two entries may be used for debugging
		// Use this statement in the source code to turn antlr trace on (See note above)
		"antlrTrace_on" {antlrTrace(true);}
	|
		// Use this statement in the source code to turn antlr trace off (See note above)
		"antlrTrace_off" {antlrTrace(false);}
	)
	;	// end member_declaration

//function_definition
function_definition
	:
	(	// Next line is equivalent to guarded predicate in PCCTS
		// (SCOPE | ID)? => <<qualifiedItemIsOneOf(qiType|qiCtor)>>?
		{( !(LA(1)==SCOPE||LA(1)==ID) || qualifiedItemIsOneOf(qiType|qiCtor) )}?
		declaration_specifiers function_declarator[1]
		(	options{warnWhenFollowAmbig = false;}:
			({lineNo = LT(1)->getLine();} declaration)*	// Possible for K & R definition
			{in_parameter_list = false;}
		)?
		compound_statement
	|	
		function_declarator[1]
		(	options{warnWhenFollowAmbig = false;}:
			({lineNo = LT(1)->getLine();} declaration)*	// Possible for K & R definition
			{in_parameter_list = false;}
		)?
		compound_statement
	)
	{endFunctionDefinition();}
	;

//declaration
declaration
	:	
		("extern" StringLiteral)=>
		linkage_specification
	|	
		simple_declaration
	|	
		using_statement
	;

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

//class_head
class_head
	:	// Used only by predicates
		("struct"  
		|"union" 
		|"class" 
		)
		(ID	
			(LESSTHAN template_argument_list GREATERTHAN)?
			(base_clause)? 
		)? 
		LCURLY
	;

//declaration_specifiers
declaration_specifiers
	{// Locals
	bool td = false;	// For typedef

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -