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

📄 support.cpp

📁 C Preprocessor,antlr的grammar语言描述,用于学习词法分析,语法分析
💻 CPP
📖 第 1 页 / 共 2 页
字号:
	{
	symbols->dumpScope(stdout);	// Diagnostic - See CPPDictionary.hpp
	symbols->removeScope();		// Remove symbols stored in current scope
	symbols->restoreScope();	// Reduce currentScope (higher level)
	//printf("endConstructorDefinition restoreScope() now %d\n",symbols->getCurrentScopeIndex());
	functionDefinition = 0;
	}

void CPPParser::
beginConstructorDeclaration(const char *ctor)
	{ }

void CPPParser::
endConstructorDeclaration()
	{ }

void CPPParser::
beginDestructorDefinition()
	{functionDefinition = 1;}

void CPPParser::
endDestructorDefinition()
	{
	symbols->dumpScope(stdout);	// Diagnostic - See CPPDictionary.hpp
	symbols->removeScope();		// Remove symbols stored in current scope
	symbols->restoreScope();	// Reduce currentScope (higher level)
	//printf("endDestructorDefinition restoreScope() now %d\n",symbols->getCurrentScopeIndex());
	functionDefinition = 0;
	}

void CPPParser::
beginDestructorDeclaration(const char *dtor)
	{ }

void CPPParser::
endDestructorDeclaration()
	{ }

void CPPParser::
beginParameterDeclaration()
	{ }

void CPPParser::
beginFieldDeclaration()
	{ }

void CPPParser::
declarationSpecifier(bool td, bool fd, StorageClass sc, TypeQualifier tq,
			 TypeSpecifier ts, FunctionSpecifier fs)
	{
	//printf("support.cpp declarationSpecifier td %d, fd %d, sc %d, tq %d, ts %d, fs %d\n",
	//	td,fd,sc,tq,ts,fs);
	_td = td;	// For typedef
	_fd = fd;	// For friend
	_sc = sc;
	_tq = tq;
	_ts = ts;
	_fs = fs;
	}

/* Symbols from declarators are added to the symbol table here. 
 * The symbol is also added to external scope or whatever the current scope is, in the symbol table. 
 * See list of object types below.
 */
void CPPParser::
declaratorID(const char *id,QualifiedItem qi)
	{
	//printf("Support.cpp declaratorID entered for %s at ptr %d, type %d\n",id,id,qi);
	//printf("Support.cpp declaratorID %d %s found, _ts = %d, _td = %d, qi = %d \n",
	//      lineNo,id,_ts,_td,qi);
	CPPSymbol *c = NULL;

	// if already in symbol table as a class, don't add
	// of course, this is incorrect as you can rename
	// classes by entering a new scope, but our limited
	// example basically keeps all types globally visible.
	if (symbols->lookup(id,CPPSymbol::otTypename)!=NULL)
		{
		CPPSymbol *c = (CPPSymbol *) symbols->lookup(id,CPPSymbol::otTypename);
		if (statementTrace >= 2)
			printf("%d support.cpp declaratorID %s already stored in dictionary, ObjectType %d\n",
				lineNo,id,c->getType());
		return;
		}

	if (qi==qiType)	// Check for type declaration
		{
		c = new CPPSymbol(id, CPPSymbol::otTypedef);
		//printf("Support.cpp declaratorID00 id %s\n",id);
		if (c==NULL) panic("can't alloc CPPSymbol");
		symbols->defineInScope(id, c, externalScope);
		//printf("Support.cpp declaratorID01 id %s\n",id);
		if(statementTrace >= 2)
			{
			printf("%d support.cpp declaratorID declare %s in external scope 1, ObjectType %d\n",
				lineNo,id,c->getType());
			}
		//printf("Support.cpp declaratorID03 id %s\n",id);
		// DW 04/08/03 Scoping not fully implemented
		// Typedefs all recorded in 'external' scope and therefor never removed
		}
	else if (qi==qiFun)	// Check for function declaration
		{
		//printf("Support.cpp declaratorID02 id %d id %s\n",id,id);
		c = new CPPSymbol(id, CPPSymbol::otFunction);
		if (c==NULL) panic("can't alloc CPPSymbol");
		symbols->define(id, c);	// Add to current scope
		//printf("Support.cpp declaratorID04 id %d id %s\n",id,id);
		if(statementTrace >= 2)
			{
			printf("%d support.cpp declaratorID declare %s in current scope %d, ObjectType %d\n",
				lineNo,id,symbols->getCurrentScopeIndex(),c->getType());
			}
		}
	else	   
		{
		if (qi!=qiVar)
			fprintf(stderr,"%d support.cpp declaratorID warning qi (%d) not qiVar (%d) for %s\n",
				lineNo,qi,qiVar,id); 

		c = new CPPSymbol(id, CPPSymbol::otVariable);
		if (c==NULL) panic("can't alloc CPPSymbol");
		symbols->define(id, c);	// Add to current scope
		if(statementTrace >= 2)
			printf("%d support.cpp declaratorID declare %s in current scope %d, ObjectType %d\n",
				lineNo,id,symbols->getCurrentScopeIndex(),c->getType());
		}
	}
/* These are the object types
	0 = otInvalid
	1 = otFunction
	2 = otVariable
	3 = otTypedef	Note. 3-7 are type names
	4 = otStruct	Note. 4, 5 & 7 are class names
	5 = otUnion
	6 = otEnum
	7 = otClass
   10 = otTypename
   11 = otNonTypename
*/

void CPPParser::
declaratorArray()
	{ }

void CPPParser::
declaratorParameterList(int def)
	{
	symbols->saveScope();	// Advance currentScope (lower level)
	//printf("declaratorParameterList saveScope() now %d\n",symbols->getCurrentScopeIndex());
	}

void CPPParser::
declaratorEndParameterList(int def)
	{
	if (!def)
		{
		symbols->dumpScope(stdout);	// Diagnostic - See CPPDictionary.hpp
		symbols->removeScope();		// Remove symbols stored in current scope
		symbols->restoreScope();	// Reduce currentScope (higher level)
		//printf("declaratorEndParameterList restoreScope() now %d\n",symbols->getCurrentScopeIndex());
		}
	}

void CPPParser::
functionParameterList()
	{
	symbols->saveScope();	// Advance currentScope (lower level)
	//printf("functionParameterList saveScope() now %d\n",symbols->getCurrentScopeIndex());
	// DW 25/3/97 change flag from function to parameter list
	// DW 07/03/07 Taken out because it caused a problem when function declared within function and
	//               it was not actually used anywhere. (See in_parameter_list)
	//functionDefinition = 2;
	}

void CPPParser::
functionEndParameterList(int def)
	{
	// If this parameter list is not in a definition then this
	if ( !def)
		{
		symbols->dumpScope(stdout);	// Diagnostic - See CPPDictionary.hpp
		symbols->removeScope();		// Remove symbols stored in current scope
		symbols->restoreScope();	// Reduce currentScope (higher level)
		//printf("functionEndParameterList restoreScope() now %d\n",symbols->getCurrentScopeIndex());
		}
	else
		{
		// Change flag from parameter list to body of definition
		functionDefinition = 3;
		}
	// Otherwise endFunctionDefinition removes the parameters from scope
	}

void CPPParser::
enterNewLocalScope()
	{
	symbols->saveScope();	// Advance currentScope (lower level)
	//printf("enterNewLocalScope saveScope() now %d\n",symbols->getCurrentScopeIndex());
	}

void CPPParser::
exitLocalScope()
	{
	symbols->dumpScope(stdout);	// Diagnostic - See CPPDictionary.hpp
	symbols->removeScope();		// Remove symbols stored in current scope
	symbols->restoreScope();	// Reduce currentScope (higher level)
	//printf("exitLocalScope restoreScope() now %d\n",symbols->getCurrentScopeIndex());
	}

void CPPParser::
enterExternalScope()
	{// Scope has been initialised to 1 in CPPParser.init() in CPPParser.hpp
	functionDefinition = 0;
	}

void CPPParser::
exitExternalScope()
	{
	symbols->dumpScope(stdout);	// Diagnostic - See CPPDictionary.hpp
	symbols->removeScope();		// Remove symbols stored in current scope s/b 2
	symbols->restoreScope();	// Reduce currentScope (higher level)

	symbols->dumpScope(stdout);	// Diagnostic - See CPPDictionary.hpp
	symbols->removeScope();		// Remove symbols stored in current scope s/b 1
	symbols->restoreScope();	// Reduce currentScope (higher level)

	if (symbols->getCurrentScopeIndex()==0)
		fprintf(stdout,"\nSupport exitExternalScope, scope now %d as required\n",symbols->getCurrentScopeIndex());
	else
		fprintf(stderr,"\nSupport exitExternalScope, scope now %d, should be 0\n",symbols->getCurrentScopeIndex());
	}

void CPPParser::
classForwardDeclaration(const char *tag, TypeSpecifier ts, FunctionSpecifier fs)
	{
	CPPSymbol *c = NULL;

	// if already in symbol table as a class, don't add
	// of course, this is incorrect as you can rename
	// classes by entering a new scope, but our limited
	// example basically keeps all types globally visible.
	if (symbols->lookup(tag,CPPSymbol::otTypename)!=NULL)
		{
		CPPSymbol *cs = (CPPSymbol *) symbols->lookup(tag,CPPSymbol::otTypename);
		if (statementTrace >= 2)
			printf("%d support.cpp classForwardDeclaration %s already stored in dictionary, ObjectType %d\n",
				lineNo,tag,cs->getType());
		return;
		}

	switch (ts)
		{
		case tsSTRUCT :
			c = new CPPSymbol(tag, CPPSymbol::otStruct);
			break;
		case tsUNION :
			c = new CPPSymbol(tag, CPPSymbol::otUnion);
			break;
		case tsCLASS :
			c = new CPPSymbol(tag, CPPSymbol::otClass);
			break;
		}

	if ( c==NULL ) panic("can't alloc CPPSymbol");

	symbols->defineInScope(tag, c, externalScope);
	if (statementTrace >= 2)
		printf("%d support.cpp classForwardDeclaration declare %s in external scope, ObjectType %d\n",
			lineNo,tag,c->getType());

	// If it's a friend class forward decl, put in global scope also.
	// DW 04/07/03 No need if already in external scope. See above.
	//if ( ds==dsFRIEND )
	//	{
	//	CPPSymbol *ext_c = new CPPSymbol(tag, CPPSymbol::otClass);
	//	if ( ext_c==NULL ) panic("can't alloc CPPSymbol");
	//	if ( symbols->getCurrentScopeIndex()!=externalScope )	// DW 04/07/03 Not sure this test is really necessary
	//		{
	//		printf("classForwardDeclaration defineInScope(externalScope)\n");
	//		symbols->defineInScope(tag, ext_c, externalScope);
	//		}
	//	}

	}

void CPPParser::
beginClassDefinition(const char *tag, TypeSpecifier ts)
	{
	CPPSymbol *c;

	// if already in symbol table as a class, don't add
	// of course, this is incorrect as you can rename
	// classes by entering a new scope, but our limited
	// example basically keeps all types globally visible.
	if (symbols->lookup(tag,CPPSymbol::otTypename) != NULL)
		{
		symbols->saveScope();   // still have to use scope to collect members
		//printf("support.cpp beginClassDefinition_1 saveScope() now %d\n",symbols->getCurrentScopeIndex());
		if (statementTrace >= 2)
			printf("%d support.cpp beginClassDefinition classname %s already in dictionary\n",
				lineNo,tag);
		return;
		}

	switch (ts)
		{
		case tsSTRUCT:
			c = new CPPSymbol(tag, CPPSymbol::otStruct);
			break;
		case tsUNION:
			c = new CPPSymbol(tag, CPPSymbol::otUnion);
			break;
		case tsCLASS:
			c = new CPPSymbol(tag, CPPSymbol::otClass);
			break;
		}
	if (c==NULL) panic("can't alloc CPPSymbol");

	symbols->defineInScope(tag, c, externalScope);

	if (statementTrace >= 2)
		printf("%d support.cpp beginClassDefinition define %s in external scope (1), ObjectType %d\n",
			lineNo,tag,c->getType());

	strcat(qualifierPrefix, tag);
	strcat(qualifierPrefix, "::");

	// add all member type symbols into the global scope (not correct, but
	// will work for most code).
	// This symbol lives until the end of the file
	symbols->saveScope();   // Advance currentScope (lower level)
	}

void CPPParser::
endClassDefinition()
	{
	symbols->dumpScope(stdout);	// Diagnostic - See CPPDictionary.hpp
	symbols->removeScope();		// Remove symbols stored in current scope
	symbols->restoreScope();	// Reduce currentScope (higher level)
	// remove final T:: from A::B::C::T::
	// upon backing out of last class, qualifierPrefix is set to ""
	char *p = &(qualifierPrefix[strlen(qualifierPrefix)-3]);
	while ( p > &(qualifierPrefix[0]) && *p!=':' ) 
		{p--;}
	if ( p > &(qualifierPrefix[0]) ) 
		p++;
	*p = '\0';
	}

void CPPParser::
enumElement(const char *e)
	{ 
	CPPSymbol *c = new CPPSymbol(e, CPPSymbol::otVariable);
	if (c==NULL) panic("can't alloc CPPSymbol");
	symbols->define(e, c);	// Add to current scope
	if(statementTrace >= 2)
		printf("%d support.cpp declaratorID declare %s in current scope %d, ObjectType %d\n",
			lineNo,e,symbols->getCurrentScopeIndex(),c->getType());
	}

void CPPParser::
beginEnumDefinition(const char *e)
	{// DW 26/3/97 Set flag for new class
	
	// add all enum tags into the global scope (not correct, but
	// will work for most code).
	// This symbol lives until the end of the file
	CPPSymbol *c = new CPPSymbol(e, CPPSymbol::otEnum);
	if (c==NULL) panic("can't alloc CPPSymbol");
	symbols->defineInScope(e, c, externalScope);
	if (statementTrace >= 2)
		printf("%d support.cpp beginEnumDefinition %s define in external scope, ObjectType %d\n",
			lineNo,e,c->getType());
	}

void CPPParser::
endEnumDefinition()
	{	
	}

void CPPParser::
templateTypeParameter(const char *t)
	{
	//DW 11/06/03 Symbol saved in templateParameterScope (0)
	//  as a temporary measure until scope is implemented fully
	// This symbol lives until the end of the file
	CPPSymbol *e = new CPPSymbol(t, CPPSymbol::otTypedef);	// DW 22/03/05
//	CPPSymbol *e = new CPPSymbol(t, CPPSymbol::otClass);
	if (e==NULL) panic("can't alloc CPPSymbol");
//	symbols->defineInScope(t, e, templateParameterScope);	// DW 22/03/05
	// DW 10/08/05 Replaced to make template parameters local
	symbols->defineInScope(t, e, externalScope);
//	symbols->define(t,e);	// Save template parameter in local scope
	if (statementTrace >= 2)
//		printf("%d support.cpp templateTypeParameter declare %s in  parameter scope (0), ObjectType %d\n",
		printf("%d support.cpp templateTypeParameter declare %s in  external scope (1), ObjectType %d\n",
			lineNo,t,e->getType());
//		printf("%d support.cpp templateTypeParameter declare %s in current scope %d, ObjectType %d\n",
//			lineNo,t,symbols->getCurrentScopeIndex(),e->getType());
	}

void CPPParser::
beginTemplateDeclaration()
	{
	symbols->saveScope();	// Advance currentScope (lower level)
	//printf("Support beginTemplateDeclaration, Scope now %d\n",symbols->getCurrentScopeIndex());
	}

void CPPParser::
endTemplateDeclaration()
	{
	symbols->dumpScope(stdout);	// Diagnostic - See CPPDictionary.hpp
	symbols->removeScope();		// Remove symbols stored in current scope
	symbols->restoreScope();	// Reduce currentScope (higher level)
	//printf("Support endTemplateDeclaration, Scope now %d\n",symbols->getCurrentScopeIndex());
	}

void CPPParser::
beginTemplateDefinition()
	{
	}

void CPPParser::
endTemplateDefinition()
	{
	}

void CPPParser::
beginTemplateParameterList()
	{ 
	// DW 26/05/03 To scope template parameters
	symbols->saveScope();	// Advance currentScope (lower level)
	}

void CPPParser::
endTemplateParameterList()
	{ 
	// DW 26/05/03 To end scope template parameters
	symbols->restoreScope();	// Reduce currentScope (higher level)
	}

void CPPParser::
exceptionBeginHandler()
	{ }

void CPPParser::
exceptionEndHandler()
	{// remove parm elements from the handler scope
	symbols->dumpScope(stdout);	// Diagnostic - See CPPDictionary.hpp
	symbols->removeScope();		// Remove symbols stored in current scope
	symbols->restoreScope();	// Reduce currentScope (higher level)
	}

void CPPParser::
end_of_stmt()
	{
#ifdef MYCODE
	myCode_end_of_stmt();
#endif MYCODE
	}                    

void CPPParser::
panic(const char *err)
	{
	fprintf(stdout, "CPPParser: %s\n", err);
	exit(-1);
	}

//	Functions which may be overridden by MyCode subclass

void CPPParser::
myCode_pre_processing(int argc,char *argv[])
	{
	}

void CPPParser::
myCode_post_processing()
	{
	}

void CPPParser::
myCode_end_of_stmt()
	{
	}

void CPPParser::
myCode_function_direct_declarator(const char *id)
	{
	//printf("support myCode_function_direct_declarator entered for %s\n",id);
	}

⌨️ 快捷键说明

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