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

📄 cpp_parser.g

📁 C Preprocessor,antlr的grammar语言描述,用于学习词法分析,语法分析
💻 G
📖 第 1 页 / 共 5 页
字号:
//ctor_definition
ctor_definition 
	:
		ctor_head
		ctor_body
		{endConstructorDefinition();}
	;

//ctor_head
ctor_head 
	:
		ctor_decl_spec
		ctor_declarator[1]
	;

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

//ctor_declarator
ctor_declarator[int definition]
	{char *q;}
	: 
		q = qualified_ctor_id
		{declaratorParameterList(definition);}
		LPAREN (parameter_list)? RPAREN
		{declaratorEndParameterList(definition);}
		(exception_specification)?
	;

// This matches a generic qualified identifier ::T::B::foo
// that is satisfactory for a ctor (no operator, no trailing <>)
qualified_ctor_id returns [char *q = NULL]
	{
	char *so;
	char qitem03[CPPParser_MaxQualifiedItemSize+1];
	qitem03[0] = '\0';
	}
	: 
		so = scope_override
		{strcpy(qitem03, so);}
		id:ID	// DW 24/05/04 Note. Neither Ctor or Dtor recorded in dictionary
		{strcat(qitem03,(id->getText()).data());
		q = qitem03;
		//printf("CPP_parser.g qualified_ctor_id q %s\n",q);
		} 
	;

//ctor_body
ctor_body
	:
		(ctor_initializer)?
		compound_statement
	;

//ctor_initializer
ctor_initializer
	:
		COLON superclass_init (COMMA superclass_init)*
	;

//superclass_init
superclass_init
	{char *q;} 
	: 
		q = qualified_id LPAREN (expression_list)? RPAREN
	;

//dtor_head
dtor_head[int definition]
	:
		dtor_decl_spec
		dtor_declarator[definition]
	;

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

//dtor_declarator
dtor_declarator[int definition]
	{char *s;}
	:	
		s = scope_override
		TILDE ID
		{declaratorParameterList(definition);}
		LPAREN ("void")? RPAREN
		{declaratorEndParameterList(definition);}
		(exception_specification)?
	;

//dtor_body
dtor_body
	:
		compound_statement
		{endDestructorDefinition();}
	;

//parameter_list
parameter_list
	:	
		parameter_declaration_list (ELLIPSIS)?
	;

//parameter_declaration_list
parameter_declaration_list
	:	
		(parameter_declaration (COMMA parameter_declaration)* )
	;

//parameter_declaration	(See also template_parameter_declaration)
parameter_declaration
	:	
		{beginParameterDeclaration();}
		(
			{!((LA(1)==SCOPE) && (LA(2)==STAR||LA(2)==OPERATOR)) &&
			 (!(LA(1)==SCOPE||LA(1)==ID) || qualifiedItemIsOneOf(qiType|qiCtor) )}?
			
			declaration_specifiers	// DW 24/3/98 Mods for K & R
			(  
				(declarator)=> declarator        // if arg name given
			| 
				abstract_declarator     // if arg name not given  // can be empty
			)
		|
			(declarator)=> declarator			// DW 24/3/98 Mods for K & R
		|
			ELLIPSIS
		)
		(ASSIGNEQUAL 
		 remainder_expression // DW 18/4/01 assignment_expression
		)?
	;

//type_id
type_id
	:
		declaration_specifiers abstract_declarator
	;

/* This rule looks a bit weird because (...) can happen in two
 * places within the declaration such as "void (*)()" (ptr to
 * function returning nothing).  However, the () of a function
 * can only occur after having seen either a (abstract_declarator)
 * and not after a [..] or simple '*'.  These are the only two
 * valid () func-groups:
 *    int (*)();     // ptr to func
 *    int (*[])();   // array of ptr to func
 */
//abstract_declarator
abstract_declarator
	:	
		ptr_operator abstract_declarator 
	|	
		(LPAREN abstract_declarator RPAREN (LSQUARE|LPAREN) )=> LPAREN abstract_declarator RPAREN
		(options {warnWhenFollowAmbig = false;}:
		abstract_declarator_suffix)
	|	
		(options {warnWhenFollowAmbig = false;}:
		abstract_declarator_suffix)?
	;

//abstract_declarator_suffix
abstract_declarator_suffix
	:	
		(LSQUARE (constant_expression)? RSQUARE)+
		{declaratorArray();}
	|
		LPAREN
		{declaratorParameterList(0);}
		(parameter_list)?
		RPAREN
		{declaratorEndParameterList(0);}
		cv_qualifier_seq
		(exception_specification)?
	;

//exception_specification
exception_specification
	{char *so;}
	:	
		"throw" 
		LPAREN 
		(	(so = scope_override ID (COMMA so = scope_override ID)* )? 
		|	ELLIPSIS
		)
		RPAREN
	;

//template_head
template_head
	:	
		"template"
		LESSTHAN template_parameter_list GREATERTHAN
	;

//template_parameter_list
template_parameter_list
	:	
		{beginTemplateParameterList();}
		template_parameter (COMMA template_parameter)*
		{endTemplateParameterList();}
	;

/* Rule requires >2 lookahead tokens. The ambiguity is resolved 
 * correctly, however. According to the manual "...A template argument
 * that can be interpreted either as a parameter-declaration or a
 * type-argument (because its identifier is the name of an
 * already existing class) is taken as type-argument."
 * Therefore, any "class ID" that is seen on the input, should
 * match the first alternative here (it should be a type-argument).
 */
//template_parameter
template_parameter
	:	
		(options{generateAmbigWarnings = false;}:
		 type_parameter
		|	
		 (parameter_declaration)=>
		  parameter_declaration
		|
		 template_parameter_declaration
		)
	;

//type_parameter
type_parameter
	:
		(	
			("class"|"typename") 
			(id:ID
				{
				templateTypeParameter((id->getText()).data());
				}
				(ASSIGNEQUAL assigned_type_name)?
			)?
		|
			template_head "class" 
			(id2:ID
				{
				templateTypeParameter((id2->getText()).data());
				}
				(ASSIGNEQUAL assigned_type_name)?
			)?
		)
	;

/* This is to allow an assigned type_name in a template parameter
 *	list to be defined previously in the same parameter list,
 *	as type setting is ineffective whilst guessing
 */
//assigned_type_name
assigned_type_name
	{char* qt; TypeSpecifier ts;}
	:
		(options{generateAmbigWarnings = false;}:
			qt = qualified_type abstract_declarator	
		|
			ts = simple_type_specifier abstract_declarator
		)
	;

//template_parameter_declaration	(See also parameter_declaration)
template_parameter_declaration
	:	
		{beginParameterDeclaration();}
		(
			{!((LA(1)==SCOPE) && (LA(2)==STAR||LA(2)==OPERATOR)) &&
			 (!(LA(1)==SCOPE||LA(1)==ID) || qualifiedItemIsOneOf(qiType|qiCtor) )}?
			
			declaration_specifiers	// DW 24/3/98 Mods for K & R
			(  
				(declarator)=> declarator        // if arg name given
			| 
				abstract_declarator     // if arg name not given  // can be empty
			)
		|
			(declarator)=> declarator			// DW 24/3/98 Mods for K & R
		|
			ELLIPSIS
		)
		(ASSIGNEQUAL
		 additive_expression	// DW 04/09/07 because of ambiguity of ">"
		)?
	;

// This rule refers to an instance of a template class or function
//template_id
template_id	// aka template_class_name
	:	
		ID LESSTHAN template_argument_list GREATERTHAN
	;

//template_argument_list
template_argument_list
	:	
		template_argument (COMMA template_argument)*
	;

/* Here assignment_expression was changed to shift_expression to rule out
 *  x< 1<2 > which causes ambiguities. As a result, these can be used only
 *  by enclosing parentheses x<(1<2)>. This is true for x<1+2> ==> bad,
 *  x<(1+2)> ==> ok.
 */
//template_argument
template_argument
	:
		// DW 07/04/05 This predicate only used here if next is SCOPE or ID
		{( !(LA(1)==SCOPE||LA(1)==ID) || qualifiedItemIsOneOf(qiType|qiCtor) )}?
		type_id
	|	
		shift_expression // failed in iosfwd
	;

///////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////
//////////////////////////////  STATEMENTS ////////////////////////////
///////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////

//statement_list
statement_list
	:	
		(statement)+
	;

//statement
statement
	{
	lineNo = LT(1)->getLine();
	FunctionSpecifier fs = fsInvalid;	// inline,virtual,explicit
	}
	:
		(	("namespace"|"using")=>
			block_declaration
		|	(("typedef")? class_specifier (qualified_id)? LCURLY)=>
			member_declaration
		|	(declaration_specifiers ((ptr_operator)=>ptr_operator)? qualified_id)=>
			member_declaration
		|	labeled_statement
		|	case_statement
		|	default_statement
		|	expression SEMICOLON {end_of_stmt();}
		|	compound_statement
		|	selection_statement
		|	iteration_statement
		|	jump_statement
		|	SEMICOLON {end_of_stmt();}
		|	try_block
		|	throw_statement
			// 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);}
		)
	;

//block_declaration
block_declaration
	:	simple_declaration
	|	namespace_alias_definition
	|	using_statement
	;

//simple_declaration
simple_declaration
	:
		declaration_specifiers (init_declarator_list)? SEMICOLON {end_of_stmt();}
	;

//labeled_statement
labeled_statement
	:	
		ID COLON statement
	;

//case_statement
case_statement
	:	"case"
		constant_expression COLON statement
	;

//default_statement
default_statement
	:	
		"default" COLON statement
	;

//compound_statement
compound_statement
	:	
		LCURLY 
		{end_of_stmt();
		 enterNewLocalScope();
		}
		(statement_list)?
		RCURLY 
		{exitLocalScope();}
	;

/* NOTE: cannot remove ELSE ambiguity, but it parses correctly.
 * The warning is removed with the options statement
 */
//selection_statement
selection_statement
	:	
		"if" LPAREN 
		{enterNewLocalScope();}
		condition RPAREN
		statement
		(options {warnWhenFollowAmbig = false;}:
		 "else" statement)?
		{exitLocalScope();}
	|	
		"switch" LPAREN
		{enterNewLocalScope();}
		condition RPAREN statement
		{exitLocalScope();}
	;

//iteration_statement
iteration_statement
	:	
		"while"	LPAREN
		{enterNewLocalScope();}
		condition RPAREN 
		statement  
		{exitLocalScope();}
	|	
		"do" 
		{enterNewLocalScope();}
		statement "while"
		LPAREN expression RPAREN 
		{exitLocalScope();}
		SEMICOLON {end_of_stmt();} 
	|	
		"for" LPAREN
		{enterNewLocalScope();}
		(	(declaration)=> declaration 
		|	(expression)? SEMICOLON {end_of_stmt();}
		)
		(condition)? SEMICOLON {end_of_stmt();}
		(expression)?
		RPAREN statement	 
		{exitLocalScope();}
	;

//condition
condition
	:
		(	(declaration_specifiers declarator ASSIGNEQUAL)=> 
			 declaration_specifiers declarator ASSIGNEQUAL remainder_expression
		|	expression
		)
	;

//jump_statement
jump_statement

⌨️ 快捷键说明

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